avr32: upgrade to 2.6.25.6
[openwrt/svn-archive/archive.git] / target / linux / avr32 / patches / 100-git_sync.patch
1 diff --exclude=.git -urN linux-2.6.25.6/arch/arm/mach-at91/at91cap9_devices.c avr32-2.6/arch/arm/mach-at91/at91cap9_devices.c
2 --- linux-2.6.25.6/arch/arm/mach-at91/at91cap9_devices.c 2008-06-09 20:27:19.000000000 +0200
3 +++ avr32-2.6/arch/arm/mach-at91/at91cap9_devices.c 2008-06-12 15:09:38.603815938 +0200
4 @@ -278,20 +278,25 @@
5 * -------------------------------------------------------------------- */
6
7 #if defined(CONFIG_MTD_NAND_AT91) || defined(CONFIG_MTD_NAND_AT91_MODULE)
8 -static struct at91_nand_data nand_data;
9 +static struct atmel_nand_data nand_data;
10
11 #define NAND_BASE AT91_CHIPSELECT_3
12
13 static struct resource nand_resources[] = {
14 - {
15 + [0] = {
16 .start = NAND_BASE,
17 .end = NAND_BASE + SZ_256M - 1,
18 .flags = IORESOURCE_MEM,
19 + },
20 + [1] = {
21 + .start = AT91_BASE_SYS + AT91_ECC,
22 + .end = AT91_BASE_SYS + AT91_ECC + SZ_512 - 1,
23 + .flags = IORESOURCE_MEM,
24 }
25 };
26
27 static struct platform_device at91cap9_nand_device = {
28 - .name = "at91_nand",
29 + .name = "atmel_nand",
30 .id = -1,
31 .dev = {
32 .platform_data = &nand_data,
33 @@ -300,7 +305,7 @@
34 .num_resources = ARRAY_SIZE(nand_resources),
35 };
36
37 -void __init at91_add_device_nand(struct at91_nand_data *data)
38 +void __init at91_add_device_nand(struct atmel_nand_data *data)
39 {
40 unsigned long csa, mode;
41
42 @@ -341,7 +346,7 @@
43 platform_device_register(&at91cap9_nand_device);
44 }
45 #else
46 -void __init at91_add_device_nand(struct at91_nand_data *data) {}
47 +void __init at91_add_device_nand(struct atmel_nand_data *data) {}
48 #endif
49
50 /* --------------------------------------------------------------------
51 diff --exclude=.git -urN linux-2.6.25.6/arch/arm/mach-at91/at91rm9200_devices.c avr32-2.6/arch/arm/mach-at91/at91rm9200_devices.c
52 --- linux-2.6.25.6/arch/arm/mach-at91/at91rm9200_devices.c 2008-06-09 20:27:19.000000000 +0200
53 +++ avr32-2.6/arch/arm/mach-at91/at91rm9200_devices.c 2008-06-12 15:09:38.603815938 +0200
54 @@ -369,7 +369,7 @@
55 * -------------------------------------------------------------------- */
56
57 #if defined(CONFIG_MTD_NAND_AT91) || defined(CONFIG_MTD_NAND_AT91_MODULE)
58 -static struct at91_nand_data nand_data;
59 +static struct atmel_nand_data nand_data;
60
61 #define NAND_BASE AT91_CHIPSELECT_3
62
63 @@ -382,7 +382,7 @@
64 };
65
66 static struct platform_device at91rm9200_nand_device = {
67 - .name = "at91_nand",
68 + .name = "atmel_nand",
69 .id = -1,
70 .dev = {
71 .platform_data = &nand_data,
72 @@ -391,7 +391,7 @@
73 .num_resources = ARRAY_SIZE(nand_resources),
74 };
75
76 -void __init at91_add_device_nand(struct at91_nand_data *data)
77 +void __init at91_add_device_nand(struct atmel_nand_data *data)
78 {
79 unsigned int csa;
80
81 @@ -429,7 +429,7 @@
82 platform_device_register(&at91rm9200_nand_device);
83 }
84 #else
85 -void __init at91_add_device_nand(struct at91_nand_data *data) {}
86 +void __init at91_add_device_nand(struct atmel_nand_data *data) {}
87 #endif
88
89
90 diff --exclude=.git -urN linux-2.6.25.6/arch/arm/mach-at91/at91sam9260_devices.c avr32-2.6/arch/arm/mach-at91/at91sam9260_devices.c
91 --- linux-2.6.25.6/arch/arm/mach-at91/at91sam9260_devices.c 2008-06-09 20:27:19.000000000 +0200
92 +++ avr32-2.6/arch/arm/mach-at91/at91sam9260_devices.c 2008-06-12 15:09:38.603815938 +0200
93 @@ -283,20 +283,25 @@
94 * -------------------------------------------------------------------- */
95
96 #if defined(CONFIG_MTD_NAND_AT91) || defined(CONFIG_MTD_NAND_AT91_MODULE)
97 -static struct at91_nand_data nand_data;
98 +static struct atmel_nand_data nand_data;
99
100 #define NAND_BASE AT91_CHIPSELECT_3
101
102 static struct resource nand_resources[] = {
103 - {
104 + [0] = {
105 .start = NAND_BASE,
106 .end = NAND_BASE + SZ_256M - 1,
107 .flags = IORESOURCE_MEM,
108 + },
109 + [1] = {
110 + .start = AT91_BASE_SYS + AT91_ECC,
111 + .end = AT91_BASE_SYS + AT91_ECC + SZ_512 - 1,
112 + .flags = IORESOURCE_MEM,
113 }
114 };
115
116 static struct platform_device at91sam9260_nand_device = {
117 - .name = "at91_nand",
118 + .name = "atmel_nand",
119 .id = -1,
120 .dev = {
121 .platform_data = &nand_data,
122 @@ -305,7 +310,7 @@
123 .num_resources = ARRAY_SIZE(nand_resources),
124 };
125
126 -void __init at91_add_device_nand(struct at91_nand_data *data)
127 +void __init at91_add_device_nand(struct atmel_nand_data *data)
128 {
129 unsigned long csa, mode;
130
131 @@ -346,7 +351,7 @@
132 platform_device_register(&at91sam9260_nand_device);
133 }
134 #else
135 -void __init at91_add_device_nand(struct at91_nand_data *data) {}
136 +void __init at91_add_device_nand(struct atmel_nand_data *data) {}
137 #endif
138
139
140 diff --exclude=.git -urN linux-2.6.25.6/arch/arm/mach-at91/at91sam9261_devices.c avr32-2.6/arch/arm/mach-at91/at91sam9261_devices.c
141 --- linux-2.6.25.6/arch/arm/mach-at91/at91sam9261_devices.c 2008-06-09 20:27:19.000000000 +0200
142 +++ avr32-2.6/arch/arm/mach-at91/at91sam9261_devices.c 2008-06-12 15:09:38.607815889 +0200
143 @@ -199,7 +199,7 @@
144 * -------------------------------------------------------------------- */
145
146 #if defined(CONFIG_MTD_NAND_AT91) || defined(CONFIG_MTD_NAND_AT91_MODULE)
147 -static struct at91_nand_data nand_data;
148 +static struct atmel_nand_data nand_data;
149
150 #define NAND_BASE AT91_CHIPSELECT_3
151
152 @@ -211,8 +211,8 @@
153 }
154 };
155
156 -static struct platform_device at91_nand_device = {
157 - .name = "at91_nand",
158 +static struct platform_device atmel_nand_device = {
159 + .name = "atmel_nand",
160 .id = -1,
161 .dev = {
162 .platform_data = &nand_data,
163 @@ -221,7 +221,7 @@
164 .num_resources = ARRAY_SIZE(nand_resources),
165 };
166
167 -void __init at91_add_device_nand(struct at91_nand_data *data)
168 +void __init at91_add_device_nand(struct atmel_nand_data *data)
169 {
170 unsigned long csa, mode;
171
172 @@ -262,11 +262,11 @@
173 at91_set_A_periph(AT91_PIN_PC1, 0); /* NANDWE */
174
175 nand_data = *data;
176 - platform_device_register(&at91_nand_device);
177 + platform_device_register(&atmel_nand_device);
178 }
179
180 #else
181 -void __init at91_add_device_nand(struct at91_nand_data *data) {}
182 +void __init at91_add_device_nand(struct atmel_nand_data *data) {}
183 #endif
184
185
186 @@ -539,6 +539,20 @@
187 at91_set_B_periph(AT91_PIN_PB28, 0); /* LCDD23 */
188 #endif
189
190 +#ifdef CONFIG_FB_INTSRAM
191 + {
192 + void __iomem *fb;
193 + struct resource *fb_res = &lcdc_resources[2];
194 + size_t fb_len = fb_res->end - fb_res->start + 1;
195 +
196 + fb = ioremap_writecombine(fb_res->start, fb_len);
197 + if (fb) {
198 + memset(fb, 0, fb_len);
199 + iounmap(fb, fb_len);
200 + }
201 + }
202 +#endif
203 +
204 lcdc_data = *data;
205 platform_device_register(&at91_lcdc_device);
206 }
207 diff --exclude=.git -urN linux-2.6.25.6/arch/arm/mach-at91/at91sam9263_devices.c avr32-2.6/arch/arm/mach-at91/at91sam9263_devices.c
208 --- linux-2.6.25.6/arch/arm/mach-at91/at91sam9263_devices.c 2008-06-09 20:27:19.000000000 +0200
209 +++ avr32-2.6/arch/arm/mach-at91/at91sam9263_devices.c 2008-06-12 15:09:38.607815889 +0200
210 @@ -353,20 +353,25 @@
211 * -------------------------------------------------------------------- */
212
213 #if defined(CONFIG_MTD_NAND_AT91) || defined(CONFIG_MTD_NAND_AT91_MODULE)
214 -static struct at91_nand_data nand_data;
215 +static struct atmel_nand_data nand_data;
216
217 #define NAND_BASE AT91_CHIPSELECT_3
218
219 static struct resource nand_resources[] = {
220 - {
221 + [0] = {
222 .start = NAND_BASE,
223 .end = NAND_BASE + SZ_256M - 1,
224 .flags = IORESOURCE_MEM,
225 + },
226 + [1] = {
227 + .start = AT91_BASE_SYS + AT91_ECC0,
228 + .end = AT91_BASE_SYS + AT91_ECC0 + SZ_512 - 1,
229 + .flags = IORESOURCE_MEM,
230 }
231 };
232
233 static struct platform_device at91sam9263_nand_device = {
234 - .name = "at91_nand",
235 + .name = "atmel_nand",
236 .id = -1,
237 .dev = {
238 .platform_data = &nand_data,
239 @@ -375,7 +380,7 @@
240 .num_resources = ARRAY_SIZE(nand_resources),
241 };
242
243 -void __init at91_add_device_nand(struct at91_nand_data *data)
244 +void __init at91_add_device_nand(struct atmel_nand_data *data)
245 {
246 unsigned long csa, mode;
247
248 @@ -416,7 +421,7 @@
249 platform_device_register(&at91sam9263_nand_device);
250 }
251 #else
252 -void __init at91_add_device_nand(struct at91_nand_data *data) {}
253 +void __init at91_add_device_nand(struct atmel_nand_data *data) {}
254 #endif
255
256
257 diff --exclude=.git -urN linux-2.6.25.6/arch/arm/mach-at91/at91sam9rl_devices.c avr32-2.6/arch/arm/mach-at91/at91sam9rl_devices.c
258 --- linux-2.6.25.6/arch/arm/mach-at91/at91sam9rl_devices.c 2008-06-09 20:27:19.000000000 +0200
259 +++ avr32-2.6/arch/arm/mach-at91/at91sam9rl_devices.c 2008-06-12 15:09:38.607815889 +0200
260 @@ -100,20 +100,25 @@
261 * -------------------------------------------------------------------- */
262
263 #if defined(CONFIG_MTD_NAND_AT91) || defined(CONFIG_MTD_NAND_AT91_MODULE)
264 -static struct at91_nand_data nand_data;
265 +static struct atmel_nand_data nand_data;
266
267 #define NAND_BASE AT91_CHIPSELECT_3
268
269 static struct resource nand_resources[] = {
270 - {
271 + [0] = {
272 .start = NAND_BASE,
273 .end = NAND_BASE + SZ_256M - 1,
274 .flags = IORESOURCE_MEM,
275 + },
276 + [1] = {
277 + .start = AT91_BASE_SYS + AT91_ECC,
278 + .end = AT91_BASE_SYS + AT91_ECC + SZ_512 - 1,
279 + .flags = IORESOURCE_MEM,
280 }
281 };
282
283 -static struct platform_device at91_nand_device = {
284 - .name = "at91_nand",
285 +static struct platform_device atmel_nand_device = {
286 + .name = "atmel_nand",
287 .id = -1,
288 .dev = {
289 .platform_data = &nand_data,
290 @@ -122,7 +127,7 @@
291 .num_resources = ARRAY_SIZE(nand_resources),
292 };
293
294 -void __init at91_add_device_nand(struct at91_nand_data *data)
295 +void __init at91_add_device_nand(struct atmel_nand_data *data)
296 {
297 unsigned long csa;
298
299 @@ -159,11 +164,11 @@
300 at91_set_A_periph(AT91_PIN_PB5, 0); /* NANDWE */
301
302 nand_data = *data;
303 - platform_device_register(&at91_nand_device);
304 + platform_device_register(&atmel_nand_device);
305 }
306
307 #else
308 -void __init at91_add_device_nand(struct at91_nand_data *data) {}
309 +void __init at91_add_device_nand(struct atmel_nand_data *data) {}
310 #endif
311
312
313 @@ -376,6 +381,20 @@
314 at91_set_B_periph(AT91_PIN_PC24, 0); /* LCDD22 */
315 at91_set_B_periph(AT91_PIN_PC25, 0); /* LCDD23 */
316
317 +#ifdef CONFIG_FB_INTSRAM
318 + {
319 + void __iomem *fb;
320 + struct resource *fb_res = &lcdc_resources[2];
321 + size_t fb_len = fb_res->end - fb_res->start + 1;
322 +
323 + fb = ioremap_writecombine(fb_res->start, fb_len);
324 + if (fb) {
325 + memset(fb, 0, fb_len);
326 + iounmap(fb, fb_len);
327 + }
328 + }
329 +#endif
330 +
331 lcdc_data = *data;
332 platform_device_register(&at91_lcdc_device);
333 }
334 diff --exclude=.git -urN linux-2.6.25.6/arch/arm/mach-at91/board-cap9adk.c avr32-2.6/arch/arm/mach-at91/board-cap9adk.c
335 --- linux-2.6.25.6/arch/arm/mach-at91/board-cap9adk.c 2008-06-09 20:27:19.000000000 +0200
336 +++ avr32-2.6/arch/arm/mach-at91/board-cap9adk.c 2008-06-12 15:09:38.607815889 +0200
337 @@ -175,7 +175,7 @@
338 return cap9adk_nand_partitions;
339 }
340
341 -static struct at91_nand_data __initdata cap9adk_nand_data = {
342 +static struct atmel_nand_data __initdata cap9adk_nand_data = {
343 .ale = 21,
344 .cle = 22,
345 // .det_pin = ... not connected
346 diff --exclude=.git -urN linux-2.6.25.6/arch/arm/mach-at91/board-dk.c avr32-2.6/arch/arm/mach-at91/board-dk.c
347 --- linux-2.6.25.6/arch/arm/mach-at91/board-dk.c 2008-06-09 20:27:19.000000000 +0200
348 +++ avr32-2.6/arch/arm/mach-at91/board-dk.c 2008-06-12 15:09:38.611815840 +0200
349 @@ -151,7 +151,7 @@
350 return dk_nand_partition;
351 }
352
353 -static struct at91_nand_data __initdata dk_nand_data = {
354 +static struct atmel_nand_data __initdata dk_nand_data = {
355 .ale = 22,
356 .cle = 21,
357 .det_pin = AT91_PIN_PB1,
358 diff --exclude=.git -urN linux-2.6.25.6/arch/arm/mach-at91/board-kb9202.c avr32-2.6/arch/arm/mach-at91/board-kb9202.c
359 --- linux-2.6.25.6/arch/arm/mach-at91/board-kb9202.c 2008-06-09 20:27:19.000000000 +0200
360 +++ avr32-2.6/arch/arm/mach-at91/board-kb9202.c 2008-06-12 15:09:38.611815840 +0200
361 @@ -102,7 +102,7 @@
362 return kb9202_nand_partition;
363 }
364
365 -static struct at91_nand_data __initdata kb9202_nand_data = {
366 +static struct atmel_nand_data __initdata kb9202_nand_data = {
367 .ale = 22,
368 .cle = 21,
369 // .det_pin = ... not there
370 diff --exclude=.git -urN linux-2.6.25.6/arch/arm/mach-at91/board-sam9260ek.c avr32-2.6/arch/arm/mach-at91/board-sam9260ek.c
371 --- linux-2.6.25.6/arch/arm/mach-at91/board-sam9260ek.c 2008-06-09 20:27:19.000000000 +0200
372 +++ avr32-2.6/arch/arm/mach-at91/board-sam9260ek.c 2008-06-12 15:09:38.611815840 +0200
373 @@ -146,7 +146,7 @@
374 return ek_nand_partition;
375 }
376
377 -static struct at91_nand_data __initdata ek_nand_data = {
378 +static struct atmel_nand_data __initdata ek_nand_data = {
379 .ale = 21,
380 .cle = 22,
381 // .det_pin = ... not connected
382 diff --exclude=.git -urN linux-2.6.25.6/arch/arm/mach-at91/board-sam9261ek.c avr32-2.6/arch/arm/mach-at91/board-sam9261ek.c
383 --- linux-2.6.25.6/arch/arm/mach-at91/board-sam9261ek.c 2008-06-09 20:27:19.000000000 +0200
384 +++ avr32-2.6/arch/arm/mach-at91/board-sam9261ek.c 2008-06-12 15:09:38.611815840 +0200
385 @@ -189,7 +189,7 @@
386 return ek_nand_partition;
387 }
388
389 -static struct at91_nand_data __initdata ek_nand_data = {
390 +static struct atmel_nand_data __initdata ek_nand_data = {
391 .ale = 22,
392 .cle = 21,
393 // .det_pin = ... not connected
394 diff --exclude=.git -urN linux-2.6.25.6/arch/arm/mach-at91/board-sam9263ek.c avr32-2.6/arch/arm/mach-at91/board-sam9263ek.c
395 --- linux-2.6.25.6/arch/arm/mach-at91/board-sam9263ek.c 2008-06-09 20:27:19.000000000 +0200
396 +++ avr32-2.6/arch/arm/mach-at91/board-sam9263ek.c 2008-06-12 15:09:38.611815840 +0200
397 @@ -192,7 +192,7 @@
398 return ek_nand_partition;
399 }
400
401 -static struct at91_nand_data __initdata ek_nand_data = {
402 +static struct atmel_nand_data __initdata ek_nand_data = {
403 .ale = 21,
404 .cle = 22,
405 // .det_pin = ... not connected
406 diff --exclude=.git -urN linux-2.6.25.6/arch/arm/mach-at91/board-sam9rlek.c avr32-2.6/arch/arm/mach-at91/board-sam9rlek.c
407 --- linux-2.6.25.6/arch/arm/mach-at91/board-sam9rlek.c 2008-06-09 20:27:19.000000000 +0200
408 +++ avr32-2.6/arch/arm/mach-at91/board-sam9rlek.c 2008-06-12 15:09:38.611815840 +0200
409 @@ -93,7 +93,7 @@
410 return ek_nand_partition;
411 }
412
413 -static struct at91_nand_data __initdata ek_nand_data = {
414 +static struct atmel_nand_data __initdata ek_nand_data = {
415 .ale = 21,
416 .cle = 22,
417 // .det_pin = ... not connected
418 diff --exclude=.git -urN linux-2.6.25.6/arch/avr32/boards/atngw100/Kconfig avr32-2.6/arch/avr32/boards/atngw100/Kconfig
419 --- linux-2.6.25.6/arch/avr32/boards/atngw100/Kconfig 1970-01-01 01:00:00.000000000 +0100
420 +++ avr32-2.6/arch/avr32/boards/atngw100/Kconfig 2008-06-12 15:09:38.711815728 +0200
421 @@ -0,0 +1,12 @@
422 +# NGW100 customization
423 +
424 +config BOARD_ATNGW100_I2C_GPIO
425 + bool "Use GPIO for i2c instead of built-in TWI module"
426 + help
427 + The driver for the built-in TWI module has been plagued by
428 + various problems, while the i2c-gpio driver is based on the
429 + trusty old i2c-algo-bit bitbanging engine, making it work
430 + on pretty much any setup.
431 +
432 + Choose 'Y' here if you're having i2c-related problems and
433 + want to rule out the i2c bus driver.
434 diff --exclude=.git -urN linux-2.6.25.6/arch/avr32/boards/atngw100/setup.c avr32-2.6/arch/avr32/boards/atngw100/setup.c
435 --- linux-2.6.25.6/arch/avr32/boards/atngw100/setup.c 2008-06-09 20:27:19.000000000 +0200
436 +++ avr32-2.6/arch/avr32/boards/atngw100/setup.c 2008-06-12 15:09:38.711815728 +0200
437 @@ -25,6 +25,13 @@
438 #include <asm/arch/init.h>
439 #include <asm/arch/portmux.h>
440
441 +/* Oscillator frequencies. These are board-specific */
442 +unsigned long at32_board_osc_rates[3] = {
443 + [0] = 32768, /* 32.768 kHz on RTC osc */
444 + [1] = 20000000, /* 20 MHz on osc0 */
445 + [2] = 12000000, /* 12 MHz on osc1 */
446 +};
447 +
448 /* Initialized by bootloader-specific startup code. */
449 struct tag *bootloader_tags __initdata;
450
451 @@ -37,11 +44,16 @@
452 static struct spi_board_info spi0_board_info[] __initdata = {
453 {
454 .modalias = "mtd_dataflash",
455 - .max_speed_hz = 10000000,
456 + .max_speed_hz = 8000000,
457 .chip_select = 0,
458 },
459 };
460
461 +static struct mci_platform_data __initdata mci0_data = {
462 + .detect_pin = GPIO_PIN_PC(25),
463 + .wp_pin = GPIO_PIN_PE(0),
464 +};
465 +
466 /*
467 * The next two functions should go away as the boot loader is
468 * supposed to initialize the macb address registers with a valid
469 @@ -124,6 +136,7 @@
470 }
471 };
472
473 +#ifdef CONFIG_BOARD_ATNGW100_I2C_GPIO
474 static struct i2c_gpio_platform_data i2c_gpio_data = {
475 .sda_pin = GPIO_PIN_PA(6),
476 .scl_pin = GPIO_PIN_PA(7),
477 @@ -139,6 +152,7 @@
478 .platform_data = &i2c_gpio_data,
479 },
480 };
481 +#endif
482
483 static int __init atngw100_init(void)
484 {
485 @@ -157,6 +171,7 @@
486 set_hw_addr(at32_add_device_eth(1, &eth_data[1]));
487
488 at32_add_device_spi(0, spi0_board_info, ARRAY_SIZE(spi0_board_info));
489 + at32_add_device_mci(0, &mci0_data);
490 at32_add_device_usba(0, NULL);
491
492 for (i = 0; i < ARRAY_SIZE(ngw_leds); i++) {
493 @@ -165,11 +180,15 @@
494 }
495 platform_device_register(&ngw_gpio_leds);
496
497 +#ifdef CONFIG_BOARD_ATNGW100_I2C_GPIO
498 at32_select_gpio(i2c_gpio_data.sda_pin,
499 AT32_GPIOF_MULTIDRV | AT32_GPIOF_OUTPUT | AT32_GPIOF_HIGH);
500 at32_select_gpio(i2c_gpio_data.scl_pin,
501 AT32_GPIOF_MULTIDRV | AT32_GPIOF_OUTPUT | AT32_GPIOF_HIGH);
502 platform_device_register(&i2c_gpio_device);
503 +#else
504 + at32_add_device_twi(0, NULL, 0);
505 +#endif
506
507 return 0;
508 }
509 diff --exclude=.git -urN linux-2.6.25.6/arch/avr32/boards/atstk1000/atstk1002.c avr32-2.6/arch/avr32/boards/atstk1000/atstk1002.c
510 --- linux-2.6.25.6/arch/avr32/boards/atstk1000/atstk1002.c 2008-06-09 20:27:19.000000000 +0200
511 +++ avr32-2.6/arch/avr32/boards/atstk1000/atstk1002.c 2008-06-12 15:09:38.711815728 +0200
512 @@ -1,7 +1,7 @@
513 /*
514 - * ATSTK1002 daughterboard-specific init code
515 + * ATSTK1002/ATSTK1006 daughterboard-specific init code
516 *
517 - * Copyright (C) 2005-2006 Atmel Corporation
518 + * Copyright (C) 2005-2007 Atmel Corporation
519 *
520 * This program is free software; you can redistribute it and/or modify
521 * it under the terms of the GNU General Public License version 2 as
522 @@ -28,6 +28,80 @@
523
524 #include "atstk1000.h"
525
526 +/* Oscillator frequencies. These are board specific */
527 +unsigned long at32_board_osc_rates[3] = {
528 + [0] = 32768, /* 32.768 kHz on RTC osc */
529 + [1] = 20000000, /* 20 MHz on osc0 */
530 + [2] = 12000000, /* 12 MHz on osc1 */
531 +};
532 +
533 +/*
534 + * The ATSTK1006 daughterboard is very similar to the ATSTK1002. Both
535 + * have the AT32AP7000 chip on board; the difference is that the
536 + * STK1006 has 128 MB SDRAM (the STK1002 uses the 8 MB SDRAM chip on
537 + * the STK1000 motherboard) and 256 MB NAND flash (the STK1002 has
538 + * none.)
539 + *
540 + * The RAM difference is handled by the boot loader, so the only
541 + * difference we end up handling here is the NAND flash.
542 + */
543 +#ifdef CONFIG_BOARD_ATSTK1006
544 +#include <linux/mtd/partitions.h>
545 +#include <asm/arch/smc.h>
546 +
547 +static struct smc_timing nand_timing __initdata = {
548 + .ncs_read_setup = 0,
549 + .nrd_setup = 10,
550 + .ncs_write_setup = 0,
551 + .nwe_setup = 10,
552 +
553 + .ncs_read_pulse = 30,
554 + .nrd_pulse = 15,
555 + .ncs_write_pulse = 30,
556 + .nwe_pulse = 15,
557 +
558 + .read_cycle = 30,
559 + .write_cycle = 30,
560 +
561 + .ncs_read_recover = 0,
562 + .nrd_recover = 15,
563 + .ncs_write_recover = 0,
564 + /* WE# high -> RE# low min 60 ns */
565 + .nwe_recover = 50,
566 +};
567 +
568 +static struct smc_config nand_config __initdata = {
569 + .bus_width = 1,
570 + .nrd_controlled = 1,
571 + .nwe_controlled = 1,
572 + .nwait_mode = 0,
573 + .byte_write = 0,
574 + .tdf_cycles = 2,
575 + .tdf_mode = 0,
576 +};
577 +
578 +static struct mtd_partition nand_partitions[] = {
579 + {
580 + .name = "main",
581 + .offset = 0x00000000,
582 + .size = MTDPART_SIZ_FULL,
583 + },
584 +};
585 +
586 +static struct mtd_partition *nand_part_info(int size, int *num_partitions)
587 +{
588 + *num_partitions = ARRAY_SIZE(nand_partitions);
589 + return nand_partitions;
590 +}
591 +
592 +static struct atmel_nand_data atstk1006_nand_data __initdata = {
593 + .cle = 21,
594 + .ale = 22,
595 + .rdy_pin = GPIO_PIN_PB(30),
596 + .enable_pin = GPIO_PIN_PB(29),
597 + .partition_info = nand_part_info,
598 +};
599 +#endif
600
601 struct eth_addr {
602 u8 addr[6];
603 @@ -83,6 +157,19 @@
604 } };
605 #endif
606
607 +static struct cf_platform_data __initdata cf0_data = {
608 +#ifdef CONFIG_BOARD_ATSTK1000_CF_HACKS
609 + .detect_pin = CONFIG_BOARD_ATSTK1000_CF_DETECT_PIN,
610 + .reset_pin = CONFIG_BOARD_ATSTK1000_CF_RESET_PIN,
611 +#else
612 + .detect_pin = GPIO_PIN_NONE,
613 + .reset_pin = GPIO_PIN_NONE,
614 +#endif
615 + .vcc_pin = GPIO_PIN_NONE,
616 + .ready_pin = GPIO_PIN_PB(27),
617 + .cs = 4,
618 +};
619 +
620 /*
621 * The next two functions should go away as the boot loader is
622 * supposed to initialize the macb address registers with a valid
623 @@ -212,6 +299,12 @@
624
625 at32_add_system_devices();
626
627 +#ifdef CONFIG_BOARD_ATSTK1006
628 + smc_set_timing(&nand_config, &nand_timing);
629 + smc_set_configuration(3, &nand_config);
630 + at32_add_device_nand(0, &atstk1006_nand_data);
631 +#endif
632 +
633 #ifdef CONFIG_BOARD_ATSTK100X_SW2_CUSTOM
634 at32_add_device_usart(1);
635 #else
636 @@ -228,16 +321,30 @@
637 #ifdef CONFIG_BOARD_ATSTK100X_SPI1
638 at32_add_device_spi(1, spi1_board_info, ARRAY_SIZE(spi1_board_info));
639 #endif
640 + at32_add_device_twi(0, NULL, 0);
641 +#ifndef CONFIG_BOARD_ATSTK100X_SW2_CUSTOM
642 + at32_add_device_mci(0, NULL);
643 +#endif
644 #ifdef CONFIG_BOARD_ATSTK1002_SW5_CUSTOM
645 set_hw_addr(at32_add_device_eth(1, &eth_data[1]));
646 #else
647 at32_add_device_lcdc(0, &atstk1000_lcdc_data,
648 - fbmem_start, fbmem_size);
649 + fbmem_start, fbmem_size, 0);
650 #endif
651 at32_add_device_usba(0, NULL);
652 +#ifdef CONFIG_BOARD_ATSTK100X_ENABLE_AC97
653 + at32_add_device_ac97c(0);
654 +#else
655 + at32_add_device_abdac(0);
656 +#endif
657 #ifndef CONFIG_BOARD_ATSTK100X_SW3_CUSTOM
658 at32_add_device_ssc(0, ATMEL_SSC_TX);
659 #endif
660 + at32_add_device_cf(0, 2, &cf0_data);
661 +#ifdef CONFIG_BOARD_ATSTK100X_ENABLE_PSIF
662 + at32_add_device_psif(0);
663 + at32_add_device_psif(1);
664 +#endif
665
666 atstk1000_setup_j2_leds();
667 atstk1002_setup_extdac();
668 diff --exclude=.git -urN linux-2.6.25.6/arch/avr32/boards/atstk1000/atstk1003.c avr32-2.6/arch/avr32/boards/atstk1000/atstk1003.c
669 --- linux-2.6.25.6/arch/avr32/boards/atstk1000/atstk1003.c 2008-06-09 20:27:19.000000000 +0200
670 +++ avr32-2.6/arch/avr32/boards/atstk1000/atstk1003.c 2008-06-12 15:09:38.711815728 +0200
671 @@ -27,6 +27,13 @@
672
673 #include "atstk1000.h"
674
675 +/* Oscillator frequencies. These are board specific */
676 +unsigned long at32_board_osc_rates[3] = {
677 + [0] = 32768, /* 32.768 kHz on RTC osc */
678 + [1] = 20000000, /* 20 MHz on osc0 */
679 + [2] = 12000000, /* 12 MHz on osc1 */
680 +};
681 +
682 #ifdef CONFIG_BOARD_ATSTK1000_EXTDAC
683 static struct at73c213_board_info at73c213_data = {
684 .ssc_id = 0,
685 @@ -59,6 +66,19 @@
686 } };
687 #endif
688
689 +static struct cf_platform_data __initdata cf0_data = {
690 +#ifdef CONFIG_BOARD_ATSTK1000_CF_HACKS
691 + .detect_pin = CONFIG_BOARD_ATSTK1000_CF_DETECT_PIN,
692 + .reset_pin = CONFIG_BOARD_ATSTK1000_CF_RESET_PIN,
693 +#else
694 + .detect_pin = GPIO_PIN_NONE,
695 + .reset_pin = GPIO_PIN_NONE,
696 +#endif
697 + .vcc_pin = GPIO_PIN_NONE,
698 + .ready_pin = GPIO_PIN_PB(27),
699 + .cs = 4,
700 +};
701 +
702 #ifdef CONFIG_BOARD_ATSTK1000_EXTDAC
703 static void __init atstk1003_setup_extdac(void)
704 {
705 @@ -147,12 +167,22 @@
706 at32_add_device_spi(1, spi1_board_info, ARRAY_SIZE(spi1_board_info));
707 #endif
708 #ifndef CONFIG_BOARD_ATSTK100X_SW2_CUSTOM
709 - at32_add_device_mci(0);
710 + at32_add_device_mci(0, NULL);
711 #endif
712 at32_add_device_usba(0, NULL);
713 +#ifdef CONFIG_BOARD_ATSTK100X_ENABLE_AC97
714 + at32_add_device_ac97c(0);
715 +#else
716 + at32_add_device_abdac(0);
717 +#endif
718 #ifndef CONFIG_BOARD_ATSTK100X_SW3_CUSTOM
719 at32_add_device_ssc(0, ATMEL_SSC_TX);
720 #endif
721 + at32_add_device_cf(0, 2, &cf0_data);
722 +#ifdef CONFIG_BOARD_ATSTK100X_ENABLE_PSIF
723 + at32_add_device_psif(0);
724 + at32_add_device_psif(1);
725 +#endif
726
727 atstk1000_setup_j2_leds();
728 atstk1003_setup_extdac();
729 diff --exclude=.git -urN linux-2.6.25.6/arch/avr32/boards/atstk1000/atstk1004.c avr32-2.6/arch/avr32/boards/atstk1000/atstk1004.c
730 --- linux-2.6.25.6/arch/avr32/boards/atstk1000/atstk1004.c 2008-06-09 20:27:19.000000000 +0200
731 +++ avr32-2.6/arch/avr32/boards/atstk1000/atstk1004.c 2008-06-12 15:09:38.715815679 +0200
732 @@ -29,6 +29,13 @@
733
734 #include "atstk1000.h"
735
736 +/* Oscillator frequencies. These are board specific */
737 +unsigned long at32_board_osc_rates[3] = {
738 + [0] = 32768, /* 32.768 kHz on RTC osc */
739 + [1] = 20000000, /* 20 MHz on osc0 */
740 + [2] = 12000000, /* 12 MHz on osc1 */
741 +};
742 +
743 #ifdef CONFIG_BOARD_ATSTK1000_EXTDAC
744 static struct at73c213_board_info at73c213_data = {
745 .ssc_id = 0,
746 @@ -130,14 +137,23 @@
747 at32_add_device_spi(1, spi1_board_info, ARRAY_SIZE(spi1_board_info));
748 #endif
749 #ifndef CONFIG_BOARD_ATSTK100X_SW2_CUSTOM
750 - at32_add_device_mci(0);
751 + at32_add_device_mci(0, NULL);
752 #endif
753 at32_add_device_lcdc(0, &atstk1000_lcdc_data,
754 - fbmem_start, fbmem_size);
755 + fbmem_start, fbmem_size, 0);
756 at32_add_device_usba(0, NULL);
757 +#ifdef CONFIG_BOARD_ATSTK100X_ENABLE_AC97
758 + at32_add_device_ac97c(0);
759 +#else
760 + at32_add_device_abdac(0);
761 +#endif
762 #ifndef CONFIG_BOARD_ATSTK100X_SW3_CUSTOM
763 at32_add_device_ssc(0, ATMEL_SSC_TX);
764 #endif
765 +#ifdef CONFIG_BOARD_ATSTK100X_ENABLE_PSIF
766 + at32_add_device_psif(0);
767 + at32_add_device_psif(1);
768 +#endif
769
770 atstk1000_setup_j2_leds();
771 atstk1004_setup_extdac();
772 diff --exclude=.git -urN linux-2.6.25.6/arch/avr32/boards/atstk1000/Kconfig avr32-2.6/arch/avr32/boards/atstk1000/Kconfig
773 --- linux-2.6.25.6/arch/avr32/boards/atstk1000/Kconfig 2008-06-09 20:27:19.000000000 +0200
774 +++ avr32-2.6/arch/avr32/boards/atstk1000/Kconfig 2008-06-12 15:09:38.711815728 +0200
775 @@ -18,6 +18,10 @@
776 bool "ATSTK1004"
777 select CPU_AT32AP7002
778
779 +config BOARD_ATSTK1006
780 + bool "ATSTK1006"
781 + select CPU_AT32AP7000
782 +
783 endchoice
784
785
786 @@ -102,4 +106,60 @@
787 depends on !BOARD_ATSTK100X_SW1_CUSTOM && !BOARD_ATSTK100X_SW3_CUSTOM
788 default y
789
790 +config BOARD_ATSTK100X_ENABLE_AC97
791 + bool "Use AC97C instead of ABDAC"
792 + help
793 + Select this if you want to use the built-in AC97 controller
794 + instead of the built-in Audio Bitstream DAC. These share
795 + the same I/O pins on the AP7000, so both can't be enabled
796 + at the same time.
797 +
798 + Note that the STK1000 kit doesn't ship with an AC97 codec on
799 + board, so say N unless you've got an expansion board with an
800 + AC97 codec on it that you want to use.
801 +
802 +config BOARD_ATSTK1000_CF_HACKS
803 + bool "ATSTK1000 CompactFlash hacks"
804 + depends on !BOARD_ATSTK100X_SW4_CUSTOM
805 + help
806 + Select this if you have re-routed the CompactFlash RESET and
807 + CD signals to GPIOs on your STK1000. This is necessary for
808 + reset and card detection to work properly, although some CF
809 + cards may be able to cope without reset.
810 +
811 +config BOARD_ATSTK1000_CF_RESET_PIN
812 + hex "CompactFlash RESET pin"
813 + default 0x30
814 + depends on BOARD_ATSTK1000_CF_HACKS
815 + help
816 + Select which GPIO pin to use for the CompactFlash RESET
817 + signal. This is specified as a hexadecimal number and should
818 + be defined as 0x20 * gpio_port + pin.
819 +
820 + The default is 0x30, which is pin 16 on PIOB, aka GPIO14.
821 +
822 +config BOARD_ATSTK1000_CF_DETECT_PIN
823 + hex "CompactFlash DETECT pin"
824 + default 0x3e
825 + depends on BOARD_ATSTK1000_CF_HACKS
826 + help
827 + Select which GPIO pin to use for the CompactFlash CD
828 + signal. This is specified as a hexadecimal number and should
829 + be defined as 0x20 * gpio_port + pin.
830 +
831 + The default is 0x3e, which is pin 30 on PIOB, aka GPIO15.
832 +
833 +config BOARD_ATSTK100X_ENABLE_PSIF
834 + bool "Enable PSIF peripheral (PS/2 support)"
835 + default n
836 + help
837 + Select this if you want to use the PSIF peripheral to hook up PS/2
838 + devices to your STK1000. This will require a hardware modification to
839 + work correctly, since PS/2 devices require 5 volt power and signals,
840 + while the STK1000 only provides 3.3 volt.
841 +
842 + Say N if you have not modified the hardware to boost the voltage, say
843 + Y if you have level convertion hardware or a PS/2 device capable of
844 + operating on 3.3 volt.
845 +
846 endif # stk 1000
847 diff --exclude=.git -urN linux-2.6.25.6/arch/avr32/boards/atstk1000/Makefile avr32-2.6/arch/avr32/boards/atstk1000/Makefile
848 --- linux-2.6.25.6/arch/avr32/boards/atstk1000/Makefile 2008-06-09 20:27:19.000000000 +0200
849 +++ avr32-2.6/arch/avr32/boards/atstk1000/Makefile 2008-06-12 15:09:38.711815728 +0200
850 @@ -2,3 +2,4 @@
851 obj-$(CONFIG_BOARD_ATSTK1002) += atstk1002.o
852 obj-$(CONFIG_BOARD_ATSTK1003) += atstk1003.o
853 obj-$(CONFIG_BOARD_ATSTK1004) += atstk1004.o
854 +obj-$(CONFIG_BOARD_ATSTK1006) += atstk1002.o
855 diff --exclude=.git -urN linux-2.6.25.6/arch/avr32/configs/atngw100_defconfig avr32-2.6/arch/avr32/configs/atngw100_defconfig
856 --- linux-2.6.25.6/arch/avr32/configs/atngw100_defconfig 2008-06-09 20:27:19.000000000 +0200
857 +++ avr32-2.6/arch/avr32/configs/atngw100_defconfig 2008-06-12 15:09:38.715815679 +0200
858 @@ -1,7 +1,7 @@
859 #
860 # Automatically generated make config: don't edit
861 -# Linux kernel version: 2.6.24-rc7
862 -# Wed Jan 9 23:20:41 2008
863 +# Linux kernel version: 2.6.25.4
864 +# Wed Jun 11 15:23:36 2008
865 #
866 CONFIG_AVR32=y
867 CONFIG_GENERIC_GPIO=y
868 @@ -13,10 +13,10 @@
869 CONFIG_GENERIC_IRQ_PROBE=y
870 CONFIG_RWSEM_GENERIC_SPINLOCK=y
871 CONFIG_GENERIC_TIME=y
872 +CONFIG_GENERIC_CLOCKEVENTS=y
873 # CONFIG_RWSEM_XCHGADD_ALGORITHM is not set
874 # CONFIG_ARCH_HAS_ILOG2_U32 is not set
875 # CONFIG_ARCH_HAS_ILOG2_U64 is not set
876 -CONFIG_ARCH_SUPPORTS_OPROFILE=y
877 CONFIG_GENERIC_HWEIGHT=y
878 CONFIG_GENERIC_CALIBRATE_DELAY=y
879 CONFIG_GENERIC_BUG=y
880 @@ -37,17 +37,15 @@
881 CONFIG_BSD_PROCESS_ACCT=y
882 CONFIG_BSD_PROCESS_ACCT_V3=y
883 # CONFIG_TASKSTATS is not set
884 -# CONFIG_USER_NS is not set
885 -# CONFIG_PID_NS is not set
886 # CONFIG_AUDIT is not set
887 # CONFIG_IKCONFIG is not set
888 CONFIG_LOG_BUF_SHIFT=14
889 # CONFIG_CGROUPS is not set
890 -CONFIG_FAIR_GROUP_SCHED=y
891 -CONFIG_FAIR_USER_SCHED=y
892 -# CONFIG_FAIR_CGROUP_SCHED is not set
893 +# CONFIG_GROUP_SCHED is not set
894 CONFIG_SYSFS_DEPRECATED=y
895 +CONFIG_SYSFS_DEPRECATED_V2=y
896 # CONFIG_RELAY is not set
897 +# CONFIG_NAMESPACES is not set
898 CONFIG_BLK_DEV_INITRD=y
899 CONFIG_INITRAMFS_SOURCE=""
900 CONFIG_CC_OPTIMIZE_FOR_SIZE=y
901 @@ -61,11 +59,13 @@
902 CONFIG_PRINTK=y
903 CONFIG_BUG=y
904 CONFIG_ELF_CORE=y
905 +# CONFIG_COMPAT_BRK is not set
906 # CONFIG_BASE_FULL is not set
907 CONFIG_FUTEX=y
908 CONFIG_ANON_INODES=y
909 CONFIG_EPOLL=y
910 CONFIG_SIGNALFD=y
911 +CONFIG_TIMERFD=y
912 CONFIG_EVENTFD=y
913 CONFIG_SHMEM=y
914 CONFIG_VM_EVENT_COUNTERS=y
915 @@ -73,6 +73,14 @@
916 # CONFIG_SLAB is not set
917 CONFIG_SLUB=y
918 # CONFIG_SLOB is not set
919 +CONFIG_PROFILING=y
920 +# CONFIG_MARKERS is not set
921 +CONFIG_OPROFILE=m
922 +CONFIG_HAVE_OPROFILE=y
923 +CONFIG_KPROBES=y
924 +CONFIG_HAVE_KPROBES=y
925 +# CONFIG_HAVE_KRETPROBES is not set
926 +CONFIG_PROC_PAGE_MONITOR=y
927 CONFIG_SLABINFO=y
928 CONFIG_RT_MUTEXES=y
929 # CONFIG_TINY_SHMEM is not set
930 @@ -101,10 +109,15 @@
931 CONFIG_DEFAULT_CFQ=y
932 # CONFIG_DEFAULT_NOOP is not set
933 CONFIG_DEFAULT_IOSCHED="cfq"
934 +CONFIG_CLASSIC_RCU=y
935
936 #
937 # System Type and features
938 #
939 +CONFIG_TICK_ONESHOT=y
940 +CONFIG_NO_HZ=y
941 +CONFIG_HIGH_RES_TIMERS=y
942 +CONFIG_GENERIC_CLOCKEVENTS_BUILD=y
943 CONFIG_SUBARCH_AVR32B=y
944 CONFIG_MMU=y
945 CONFIG_PERFORMANCE_COUNTERS=y
946 @@ -113,6 +126,7 @@
947 CONFIG_CPU_AT32AP7000=y
948 # CONFIG_BOARD_ATSTK1000 is not set
949 CONFIG_BOARD_ATNGW100=y
950 +CONFIG_BOARD_ATNGW100_I2C_GPIO=y
951 CONFIG_LOADER_U_BOOT=y
952
953 #
954 @@ -121,6 +135,7 @@
955 # CONFIG_AP700X_32_BIT_SMC is not set
956 CONFIG_AP700X_16_BIT_SMC=y
957 # CONFIG_AP700X_8_BIT_SMC is not set
958 +CONFIG_GPIO_DEV=y
959 CONFIG_LOAD_ADDRESS=0x10000000
960 CONFIG_ENTRY_ADDRESS=0x90000000
961 CONFIG_PHYS_OFFSET=0x10000000
962 @@ -146,16 +161,26 @@
963 CONFIG_ZONE_DMA_FLAG=0
964 CONFIG_VIRT_TO_BUS=y
965 # CONFIG_OWNERSHIP_TRACE is not set
966 +CONFIG_NMI_DEBUGGING=y
967 +CONFIG_DW_DMAC=y
968 # CONFIG_HZ_100 is not set
969 CONFIG_HZ_250=y
970 # CONFIG_HZ_300 is not set
971 # CONFIG_HZ_1000 is not set
972 CONFIG_HZ=250
973 +# CONFIG_SCHED_HRTICK is not set
974 CONFIG_CMDLINE=""
975
976 #
977 # Power management options
978 #
979 +CONFIG_ARCH_SUSPEND_POSSIBLE=y
980 +CONFIG_PM=y
981 +# CONFIG_PM_LEGACY is not set
982 +# CONFIG_PM_DEBUG is not set
983 +CONFIG_PM_SLEEP=y
984 +CONFIG_SUSPEND=y
985 +CONFIG_SUSPEND_FREEZER=y
986
987 #
988 # CPU Frequency scaling
989 @@ -164,9 +189,9 @@
990 CONFIG_CPU_FREQ_TABLE=y
991 # CONFIG_CPU_FREQ_DEBUG is not set
992 # CONFIG_CPU_FREQ_STAT is not set
993 -CONFIG_CPU_FREQ_DEFAULT_GOV_PERFORMANCE=y
994 +# CONFIG_CPU_FREQ_DEFAULT_GOV_PERFORMANCE is not set
995 # CONFIG_CPU_FREQ_DEFAULT_GOV_USERSPACE is not set
996 -# CONFIG_CPU_FREQ_DEFAULT_GOV_ONDEMAND is not set
997 +CONFIG_CPU_FREQ_DEFAULT_GOV_ONDEMAND=y
998 # CONFIG_CPU_FREQ_DEFAULT_GOV_CONSERVATIVE is not set
999 CONFIG_CPU_FREQ_GOV_PERFORMANCE=y
1000 # CONFIG_CPU_FREQ_GOV_POWERSAVE is not set
1001 @@ -202,6 +227,7 @@
1002 CONFIG_XFRM_USER=y
1003 # CONFIG_XFRM_SUB_POLICY is not set
1004 # CONFIG_XFRM_MIGRATE is not set
1005 +# CONFIG_XFRM_STATISTICS is not set
1006 CONFIG_NET_KEY=y
1007 # CONFIG_NET_KEY_MIGRATE is not set
1008 CONFIG_INET=y
1009 @@ -260,82 +286,33 @@
1010 # CONFIG_NETWORK_SECMARK is not set
1011 CONFIG_NETFILTER=y
1012 # CONFIG_NETFILTER_DEBUG is not set
1013 -CONFIG_BRIDGE_NETFILTER=y
1014 +# CONFIG_NETFILTER_ADVANCED is not set
1015
1016 #
1017 # Core Netfilter Configuration
1018 #
1019 -# CONFIG_NETFILTER_NETLINK is not set
1020 -CONFIG_NF_CONNTRACK_ENABLED=m
1021 +CONFIG_NETFILTER_NETLINK=m
1022 +CONFIG_NETFILTER_NETLINK_LOG=m
1023 CONFIG_NF_CONNTRACK=m
1024 -CONFIG_NF_CT_ACCT=y
1025 -CONFIG_NF_CONNTRACK_MARK=y
1026 -# CONFIG_NF_CONNTRACK_EVENTS is not set
1027 -CONFIG_NF_CT_PROTO_GRE=m
1028 -# CONFIG_NF_CT_PROTO_SCTP is not set
1029 -# CONFIG_NF_CT_PROTO_UDPLITE is not set
1030 -CONFIG_NF_CONNTRACK_AMANDA=m
1031 CONFIG_NF_CONNTRACK_FTP=m
1032 -CONFIG_NF_CONNTRACK_H323=m
1033 CONFIG_NF_CONNTRACK_IRC=m
1034 -CONFIG_NF_CONNTRACK_NETBIOS_NS=m
1035 -CONFIG_NF_CONNTRACK_PPTP=m
1036 -CONFIG_NF_CONNTRACK_SANE=m
1037 CONFIG_NF_CONNTRACK_SIP=m
1038 -CONFIG_NF_CONNTRACK_TFTP=m
1039 +CONFIG_NF_CT_NETLINK=m
1040 CONFIG_NETFILTER_XTABLES=y
1041 -CONFIG_NETFILTER_XT_TARGET_CLASSIFY=m
1042 -# CONFIG_NETFILTER_XT_TARGET_CONNMARK is not set
1043 -# CONFIG_NETFILTER_XT_TARGET_DSCP is not set
1044 CONFIG_NETFILTER_XT_TARGET_MARK=m
1045 -CONFIG_NETFILTER_XT_TARGET_NFQUEUE=m
1046 CONFIG_NETFILTER_XT_TARGET_NFLOG=m
1047 -# CONFIG_NETFILTER_XT_TARGET_NOTRACK is not set
1048 -# CONFIG_NETFILTER_XT_TARGET_TRACE is not set
1049 CONFIG_NETFILTER_XT_TARGET_TCPMSS=m
1050 -CONFIG_NETFILTER_XT_MATCH_COMMENT=m
1051 -CONFIG_NETFILTER_XT_MATCH_CONNBYTES=m
1052 -# CONFIG_NETFILTER_XT_MATCH_CONNLIMIT is not set
1053 -CONFIG_NETFILTER_XT_MATCH_CONNMARK=m
1054 CONFIG_NETFILTER_XT_MATCH_CONNTRACK=m
1055 -# CONFIG_NETFILTER_XT_MATCH_DCCP is not set
1056 -# CONFIG_NETFILTER_XT_MATCH_DSCP is not set
1057 -CONFIG_NETFILTER_XT_MATCH_ESP=m
1058 -CONFIG_NETFILTER_XT_MATCH_HELPER=m
1059 -CONFIG_NETFILTER_XT_MATCH_LENGTH=m
1060 -CONFIG_NETFILTER_XT_MATCH_LIMIT=m
1061 -CONFIG_NETFILTER_XT_MATCH_MAC=m
1062 CONFIG_NETFILTER_XT_MATCH_MARK=m
1063 CONFIG_NETFILTER_XT_MATCH_POLICY=m
1064 -CONFIG_NETFILTER_XT_MATCH_MULTIPORT=m
1065 -# CONFIG_NETFILTER_XT_MATCH_PHYSDEV is not set
1066 -CONFIG_NETFILTER_XT_MATCH_PKTTYPE=m
1067 -CONFIG_NETFILTER_XT_MATCH_QUOTA=m
1068 -CONFIG_NETFILTER_XT_MATCH_REALM=m
1069 -# CONFIG_NETFILTER_XT_MATCH_SCTP is not set
1070 CONFIG_NETFILTER_XT_MATCH_STATE=m
1071 -CONFIG_NETFILTER_XT_MATCH_STATISTIC=m
1072 -CONFIG_NETFILTER_XT_MATCH_STRING=m
1073 -CONFIG_NETFILTER_XT_MATCH_TCPMSS=m
1074 -# CONFIG_NETFILTER_XT_MATCH_TIME is not set
1075 -# CONFIG_NETFILTER_XT_MATCH_U32 is not set
1076 -CONFIG_NETFILTER_XT_MATCH_HASHLIMIT=m
1077
1078 #
1079 # IP: Netfilter Configuration
1080 #
1081 CONFIG_NF_CONNTRACK_IPV4=m
1082 CONFIG_NF_CONNTRACK_PROC_COMPAT=y
1083 -# CONFIG_IP_NF_QUEUE is not set
1084 CONFIG_IP_NF_IPTABLES=m
1085 -CONFIG_IP_NF_MATCH_IPRANGE=m
1086 -CONFIG_IP_NF_MATCH_TOS=m
1087 -CONFIG_IP_NF_MATCH_RECENT=m
1088 -CONFIG_IP_NF_MATCH_ECN=m
1089 -CONFIG_IP_NF_MATCH_AH=m
1090 -CONFIG_IP_NF_MATCH_TTL=m
1091 -CONFIG_IP_NF_MATCH_OWNER=m
1092 -CONFIG_IP_NF_MATCH_ADDRTYPE=m
1093 CONFIG_IP_NF_FILTER=m
1094 CONFIG_IP_NF_TARGET_REJECT=m
1095 CONFIG_IP_NF_TARGET_LOG=m
1096 @@ -343,54 +320,25 @@
1097 CONFIG_NF_NAT=m
1098 CONFIG_NF_NAT_NEEDED=y
1099 CONFIG_IP_NF_TARGET_MASQUERADE=m
1100 -CONFIG_IP_NF_TARGET_REDIRECT=m
1101 -CONFIG_IP_NF_TARGET_NETMAP=m
1102 -CONFIG_IP_NF_TARGET_SAME=m
1103 -CONFIG_NF_NAT_SNMP_BASIC=m
1104 -CONFIG_NF_NAT_PROTO_GRE=m
1105 CONFIG_NF_NAT_FTP=m
1106 CONFIG_NF_NAT_IRC=m
1107 -CONFIG_NF_NAT_TFTP=m
1108 -CONFIG_NF_NAT_AMANDA=m
1109 -CONFIG_NF_NAT_PPTP=m
1110 -CONFIG_NF_NAT_H323=m
1111 +# CONFIG_NF_NAT_TFTP is not set
1112 +# CONFIG_NF_NAT_AMANDA is not set
1113 +# CONFIG_NF_NAT_PPTP is not set
1114 +# CONFIG_NF_NAT_H323 is not set
1115 CONFIG_NF_NAT_SIP=m
1116 CONFIG_IP_NF_MANGLE=m
1117 -CONFIG_IP_NF_TARGET_TOS=m
1118 -CONFIG_IP_NF_TARGET_ECN=m
1119 -CONFIG_IP_NF_TARGET_TTL=m
1120 -CONFIG_IP_NF_TARGET_CLUSTERIP=m
1121 -CONFIG_IP_NF_RAW=m
1122 -CONFIG_IP_NF_ARPTABLES=m
1123 -CONFIG_IP_NF_ARPFILTER=m
1124 -CONFIG_IP_NF_ARP_MANGLE=m
1125
1126 #
1127 -# IPv6: Netfilter Configuration (EXPERIMENTAL)
1128 +# IPv6: Netfilter Configuration
1129 #
1130 CONFIG_NF_CONNTRACK_IPV6=m
1131 -CONFIG_IP6_NF_QUEUE=m
1132 CONFIG_IP6_NF_IPTABLES=m
1133 -CONFIG_IP6_NF_MATCH_RT=m
1134 -CONFIG_IP6_NF_MATCH_OPTS=m
1135 -CONFIG_IP6_NF_MATCH_FRAG=m
1136 -CONFIG_IP6_NF_MATCH_HL=m
1137 -CONFIG_IP6_NF_MATCH_OWNER=m
1138 CONFIG_IP6_NF_MATCH_IPV6HEADER=m
1139 -CONFIG_IP6_NF_MATCH_AH=m
1140 -CONFIG_IP6_NF_MATCH_MH=m
1141 -CONFIG_IP6_NF_MATCH_EUI64=m
1142 CONFIG_IP6_NF_FILTER=m
1143 CONFIG_IP6_NF_TARGET_LOG=m
1144 CONFIG_IP6_NF_TARGET_REJECT=m
1145 CONFIG_IP6_NF_MANGLE=m
1146 -CONFIG_IP6_NF_TARGET_HL=m
1147 -CONFIG_IP6_NF_RAW=m
1148 -
1149 -#
1150 -# Bridge: Netfilter Configuration
1151 -#
1152 -# CONFIG_BRIDGE_NF_EBTABLES is not set
1153 # CONFIG_IP_DCCP is not set
1154 # CONFIG_IP_SCTP is not set
1155 # CONFIG_TIPC is not set
1156 @@ -407,7 +355,6 @@
1157 # CONFIG_ECONET is not set
1158 # CONFIG_WAN_ROUTER is not set
1159 # CONFIG_NET_SCHED is not set
1160 -CONFIG_NET_CLS_ROUTE=y
1161
1162 #
1163 # Network testing
1164 @@ -415,6 +362,7 @@
1165 # CONFIG_NET_PKTGEN is not set
1166 # CONFIG_NET_TCPPROBE is not set
1167 # CONFIG_HAMRADIO is not set
1168 +# CONFIG_CAN is not set
1169 # CONFIG_IRDA is not set
1170 # CONFIG_BT is not set
1171 # CONFIG_AF_RXRPC is not set
1172 @@ -531,11 +479,18 @@
1173 CONFIG_BLK_DEV_RAM=m
1174 CONFIG_BLK_DEV_RAM_COUNT=16
1175 CONFIG_BLK_DEV_RAM_SIZE=4096
1176 -CONFIG_BLK_DEV_RAM_BLOCKSIZE=1024
1177 +# CONFIG_BLK_DEV_XIP is not set
1178 # CONFIG_CDROM_PKTCDVD is not set
1179 # CONFIG_ATA_OVER_ETH is not set
1180 -# CONFIG_MISC_DEVICES is not set
1181 -# CONFIG_IDE is not set
1182 +CONFIG_MISC_DEVICES=y
1183 +# CONFIG_ATMEL_PWM is not set
1184 +CONFIG_ATMEL_TCLIB=y
1185 +CONFIG_ATMEL_TCB_CLKSRC=y
1186 +CONFIG_ATMEL_TCB_CLKSRC_BLOCK=0
1187 +# CONFIG_EEPROM_93CX6 is not set
1188 +# CONFIG_ATMEL_SSC is not set
1189 +# CONFIG_ENCLOSURE_SERVICES is not set
1190 +# CONFIG_HAVE_IDE is not set
1191
1192 #
1193 # SCSI device support
1194 @@ -568,11 +523,13 @@
1195 # CONFIG_SMSC_PHY is not set
1196 # CONFIG_BROADCOM_PHY is not set
1197 # CONFIG_ICPLUS_PHY is not set
1198 +# CONFIG_REALTEK_PHY is not set
1199 # CONFIG_FIXED_PHY is not set
1200 # CONFIG_MDIO_BITBANG is not set
1201 CONFIG_NET_ETHERNET=y
1202 # CONFIG_MII is not set
1203 CONFIG_MACB=y
1204 +# CONFIG_ENC28J60 is not set
1205 # CONFIG_IBM_NEW_EMAC_ZMII is not set
1206 # CONFIG_IBM_NEW_EMAC_RGMII is not set
1207 # CONFIG_IBM_NEW_EMAC_TAH is not set
1208 @@ -599,7 +556,6 @@
1209 # CONFIG_PPPOL2TP is not set
1210 # CONFIG_SLIP is not set
1211 CONFIG_SLHC=m
1212 -# CONFIG_SHAPER is not set
1213 # CONFIG_NETCONSOLE is not set
1214 # CONFIG_NETPOLL is not set
1215 # CONFIG_NET_POLL_CONTROLLER is not set
1216 @@ -633,6 +589,7 @@
1217 #
1218 CONFIG_SERIAL_ATMEL=y
1219 CONFIG_SERIAL_ATMEL_CONSOLE=y
1220 +CONFIG_SERIAL_ATMEL_PDC=y
1221 # CONFIG_SERIAL_ATMEL_TTYAT is not set
1222 CONFIG_SERIAL_CORE=y
1223 CONFIG_SERIAL_CORE_CONSOLE=y
1224 @@ -640,8 +597,6 @@
1225 # CONFIG_LEGACY_PTYS is not set
1226 # CONFIG_IPMI_HANDLER is not set
1227 # CONFIG_HW_RANDOM is not set
1228 -# CONFIG_RTC is not set
1229 -# CONFIG_GEN_RTC is not set
1230 # CONFIG_R3964 is not set
1231 # CONFIG_RAW_DRIVER is not set
1232 # CONFIG_TCG_TPM is not set
1233 @@ -659,6 +614,7 @@
1234 #
1235 # I2C Hardware Bus support
1236 #
1237 +CONFIG_I2C_ATMELTWI=m
1238 CONFIG_I2C_GPIO=m
1239 # CONFIG_I2C_OCORES is not set
1240 # CONFIG_I2C_PARPORT_LIGHT is not set
1241 @@ -669,13 +625,12 @@
1242 #
1243 # Miscellaneous I2C Chip support
1244 #
1245 -# CONFIG_SENSORS_DS1337 is not set
1246 -# CONFIG_SENSORS_DS1374 is not set
1247 # CONFIG_DS1682 is not set
1248 # CONFIG_SENSORS_EEPROM is not set
1249 # CONFIG_SENSORS_PCF8574 is not set
1250 -# CONFIG_SENSORS_PCA9539 is not set
1251 +# CONFIG_PCF8575 is not set
1252 # CONFIG_SENSORS_PCF8591 is not set
1253 +# CONFIG_TPS65010 is not set
1254 # CONFIG_SENSORS_MAX6875 is not set
1255 # CONFIG_SENSORS_TSL2550 is not set
1256 # CONFIG_I2C_DEBUG_CORE is not set
1257 @@ -702,9 +657,27 @@
1258 # CONFIG_SPI_AT25 is not set
1259 CONFIG_SPI_SPIDEV=m
1260 # CONFIG_SPI_TLE62X0 is not set
1261 +CONFIG_HAVE_GPIO_LIB=y
1262 +
1263 +#
1264 +# GPIO Support
1265 +#
1266 +# CONFIG_DEBUG_GPIO is not set
1267 +
1268 +#
1269 +# I2C GPIO expanders:
1270 +#
1271 +# CONFIG_GPIO_PCA953X is not set
1272 +# CONFIG_GPIO_PCF857X is not set
1273 +
1274 +#
1275 +# SPI GPIO expanders:
1276 +#
1277 +# CONFIG_GPIO_MCP23S08 is not set
1278 # CONFIG_W1 is not set
1279 # CONFIG_POWER_SUPPLY is not set
1280 # CONFIG_HWMON is not set
1281 +# CONFIG_THERMAL is not set
1282 CONFIG_WATCHDOG=y
1283 # CONFIG_WATCHDOG_NOWAYOUT is not set
1284
1285 @@ -757,10 +730,6 @@
1286 #
1287 # NOTE: USB_STORAGE enables SCSI, and 'SCSI disk support'
1288 #
1289 -
1290 -#
1291 -# USB Gadget Support
1292 -#
1293 CONFIG_USB_GADGET=y
1294 # CONFIG_USB_GADGET_DEBUG is not set
1295 # CONFIG_USB_GADGET_DEBUG_FILES is not set
1296 @@ -787,21 +756,24 @@
1297 # CONFIG_USB_FILE_STORAGE_TEST is not set
1298 CONFIG_USB_G_SERIAL=m
1299 # CONFIG_USB_MIDI_GADGET is not set
1300 -CONFIG_MMC=m
1301 +# CONFIG_USB_G_PRINTER is not set
1302 +CONFIG_MMC=y
1303 # CONFIG_MMC_DEBUG is not set
1304 # CONFIG_MMC_UNSAFE_RESUME is not set
1305
1306 #
1307 # MMC/SD Card Drivers
1308 #
1309 -CONFIG_MMC_BLOCK=m
1310 +CONFIG_MMC_BLOCK=y
1311 CONFIG_MMC_BLOCK_BOUNCE=y
1312 # CONFIG_SDIO_UART is not set
1313
1314 #
1315 # MMC/SD Host Controller Drivers
1316 #
1317 +CONFIG_MMC_ATMELMCI=y
1318 CONFIG_MMC_SPI=m
1319 +# CONFIG_MEMSTICK is not set
1320 CONFIG_NEW_LEDS=y
1321 CONFIG_LEDS_CLASS=y
1322
1323 @@ -844,19 +816,22 @@
1324 # CONFIG_RTC_DRV_PCF8563 is not set
1325 # CONFIG_RTC_DRV_PCF8583 is not set
1326 # CONFIG_RTC_DRV_M41T80 is not set
1327 +# CONFIG_RTC_DRV_S35390A is not set
1328
1329 #
1330 # SPI RTC drivers
1331 #
1332 -# CONFIG_RTC_DRV_RS5C348 is not set
1333 # CONFIG_RTC_DRV_MAX6902 is not set
1334 +# CONFIG_RTC_DRV_R9701 is not set
1335 +# CONFIG_RTC_DRV_RS5C348 is not set
1336
1337 #
1338 # Platform RTC drivers
1339 #
1340 +# CONFIG_RTC_DRV_DS1511 is not set
1341 # CONFIG_RTC_DRV_DS1553 is not set
1342 -# CONFIG_RTC_DRV_STK17TA8 is not set
1343 # CONFIG_RTC_DRV_DS1742 is not set
1344 +# CONFIG_RTC_DRV_STK17TA8 is not set
1345 # CONFIG_RTC_DRV_M48T86 is not set
1346 # CONFIG_RTC_DRV_M48T59 is not set
1347 # CONFIG_RTC_DRV_V3020 is not set
1348 @@ -874,25 +849,23 @@
1349 #
1350 # File systems
1351 #
1352 -CONFIG_EXT2_FS=m
1353 +CONFIG_EXT2_FS=y
1354 # CONFIG_EXT2_FS_XATTR is not set
1355 # CONFIG_EXT2_FS_XIP is not set
1356 -CONFIG_EXT3_FS=m
1357 +CONFIG_EXT3_FS=y
1358 # CONFIG_EXT3_FS_XATTR is not set
1359 # CONFIG_EXT4DEV_FS is not set
1360 -CONFIG_JBD=m
1361 +CONFIG_JBD=y
1362 # CONFIG_REISERFS_FS is not set
1363 # CONFIG_JFS_FS is not set
1364 # CONFIG_FS_POSIX_ACL is not set
1365 # CONFIG_XFS_FS is not set
1366 # CONFIG_GFS2_FS is not set
1367 # CONFIG_OCFS2_FS is not set
1368 -# CONFIG_MINIX_FS is not set
1369 -# CONFIG_ROMFS_FS is not set
1370 +# CONFIG_DNOTIFY is not set
1371 CONFIG_INOTIFY=y
1372 CONFIG_INOTIFY_USER=y
1373 # CONFIG_QUOTA is not set
1374 -# CONFIG_DNOTIFY is not set
1375 # CONFIG_AUTOFS_FS is not set
1376 # CONFIG_AUTOFS4_FS is not set
1377 CONFIG_FUSE_FS=m
1378 @@ -923,7 +896,7 @@
1379 CONFIG_TMPFS=y
1380 # CONFIG_TMPFS_POSIX_ACL is not set
1381 # CONFIG_HUGETLB_PAGE is not set
1382 -CONFIG_CONFIGFS_FS=m
1383 +CONFIG_CONFIGFS_FS=y
1384
1385 #
1386 # Miscellaneous filesystems
1387 @@ -948,8 +921,10 @@
1388 # CONFIG_JFFS2_RUBIN is not set
1389 # CONFIG_CRAMFS is not set
1390 # CONFIG_VXFS_FS is not set
1391 +# CONFIG_MINIX_FS is not set
1392 # CONFIG_HPFS_FS is not set
1393 # CONFIG_QNX4FS_FS is not set
1394 +# CONFIG_ROMFS_FS is not set
1395 # CONFIG_SYSV_FS is not set
1396 # CONFIG_UFS_FS is not set
1397 CONFIG_NETWORK_FILESYSTEMS=y
1398 @@ -1030,11 +1005,6 @@
1399 # CONFIG_NLS_KOI8_U is not set
1400 CONFIG_NLS_UTF8=m
1401 # CONFIG_DLM is not set
1402 -CONFIG_INSTRUMENTATION=y
1403 -CONFIG_PROFILING=y
1404 -CONFIG_OPROFILE=m
1405 -CONFIG_KPROBES=y
1406 -# CONFIG_MARKERS is not set
1407
1408 #
1409 # Kernel hacking
1410 @@ -1053,6 +1023,7 @@
1411 # CONFIG_SCHEDSTATS is not set
1412 # CONFIG_TIMER_STATS is not set
1413 # CONFIG_SLUB_DEBUG_ON is not set
1414 +# CONFIG_SLUB_STATS is not set
1415 # CONFIG_DEBUG_RT_MUTEXES is not set
1416 # CONFIG_RT_MUTEX_TESTER is not set
1417 # CONFIG_DEBUG_SPINLOCK is not set
1418 @@ -1069,9 +1040,10 @@
1419 # CONFIG_DEBUG_LIST is not set
1420 # CONFIG_DEBUG_SG is not set
1421 CONFIG_FRAME_POINTER=y
1422 -# CONFIG_FORCED_INLINING is not set
1423 # CONFIG_BOOT_PRINTK_DELAY is not set
1424 # CONFIG_RCU_TORTURE_TEST is not set
1425 +# CONFIG_KPROBES_SANITY_TEST is not set
1426 +# CONFIG_BACKTRACE_SELF_TEST is not set
1427 # CONFIG_LKDTM is not set
1428 # CONFIG_FAULT_INJECTION is not set
1429 # CONFIG_SAMPLES is not set
1430 @@ -1084,7 +1056,9 @@
1431 # CONFIG_SECURITY_FILE_CAPABILITIES is not set
1432 CONFIG_CRYPTO=y
1433 CONFIG_CRYPTO_ALGAPI=y
1434 +CONFIG_CRYPTO_AEAD=y
1435 CONFIG_CRYPTO_BLKCIPHER=y
1436 +# CONFIG_CRYPTO_SEQIV is not set
1437 CONFIG_CRYPTO_HASH=y
1438 CONFIG_CRYPTO_MANAGER=y
1439 CONFIG_CRYPTO_HMAC=y
1440 @@ -1103,6 +1077,9 @@
1441 CONFIG_CRYPTO_PCBC=m
1442 # CONFIG_CRYPTO_LRW is not set
1443 # CONFIG_CRYPTO_XTS is not set
1444 +# CONFIG_CRYPTO_CTR is not set
1445 +# CONFIG_CRYPTO_GCM is not set
1446 +# CONFIG_CRYPTO_CCM is not set
1447 # CONFIG_CRYPTO_CRYPTD is not set
1448 CONFIG_CRYPTO_DES=y
1449 # CONFIG_CRYPTO_FCRYPT is not set
1450 @@ -1117,12 +1094,14 @@
1451 # CONFIG_CRYPTO_KHAZAD is not set
1452 # CONFIG_CRYPTO_ANUBIS is not set
1453 # CONFIG_CRYPTO_SEED is not set
1454 +# CONFIG_CRYPTO_SALSA20 is not set
1455 CONFIG_CRYPTO_DEFLATE=y
1456 # CONFIG_CRYPTO_MICHAEL_MIC is not set
1457 # CONFIG_CRYPTO_CRC32C is not set
1458 # CONFIG_CRYPTO_CAMELLIA is not set
1459 # CONFIG_CRYPTO_TEST is not set
1460 -# CONFIG_CRYPTO_AUTHENC is not set
1461 +CONFIG_CRYPTO_AUTHENC=y
1462 +# CONFIG_CRYPTO_LZO is not set
1463 CONFIG_CRYPTO_HW=y
1464
1465 #
1466 @@ -1137,10 +1116,7 @@
1467 # CONFIG_LIBCRC32C is not set
1468 CONFIG_ZLIB_INFLATE=y
1469 CONFIG_ZLIB_DEFLATE=y
1470 -CONFIG_TEXTSEARCH=y
1471 -CONFIG_TEXTSEARCH_KMP=m
1472 -CONFIG_TEXTSEARCH_BM=m
1473 -CONFIG_TEXTSEARCH_FSM=m
1474 +CONFIG_GENERIC_ALLOCATOR=y
1475 CONFIG_PLIST=y
1476 CONFIG_HAS_IOMEM=y
1477 CONFIG_HAS_IOPORT=y
1478 diff --exclude=.git -urN linux-2.6.25.6/arch/avr32/configs/atstk1002_defconfig avr32-2.6/arch/avr32/configs/atstk1002_defconfig
1479 --- linux-2.6.25.6/arch/avr32/configs/atstk1002_defconfig 2008-06-09 20:27:19.000000000 +0200
1480 +++ avr32-2.6/arch/avr32/configs/atstk1002_defconfig 2008-06-12 15:09:38.715815679 +0200
1481 @@ -1,7 +1,7 @@
1482 #
1483 # Automatically generated make config: don't edit
1484 -# Linux kernel version: 2.6.24-rc7
1485 -# Wed Jan 9 23:07:43 2008
1486 +# Linux kernel version: 2.6.25.4
1487 +# Wed Jun 11 15:29:18 2008
1488 #
1489 CONFIG_AVR32=y
1490 CONFIG_GENERIC_GPIO=y
1491 @@ -13,10 +13,10 @@
1492 CONFIG_GENERIC_IRQ_PROBE=y
1493 CONFIG_RWSEM_GENERIC_SPINLOCK=y
1494 CONFIG_GENERIC_TIME=y
1495 +CONFIG_GENERIC_CLOCKEVENTS=y
1496 # CONFIG_RWSEM_XCHGADD_ALGORITHM is not set
1497 # CONFIG_ARCH_HAS_ILOG2_U32 is not set
1498 # CONFIG_ARCH_HAS_ILOG2_U64 is not set
1499 -CONFIG_ARCH_SUPPORTS_OPROFILE=y
1500 CONFIG_GENERIC_HWEIGHT=y
1501 CONFIG_GENERIC_CALIBRATE_DELAY=y
1502 CONFIG_GENERIC_BUG=y
1503 @@ -36,15 +36,15 @@
1504 CONFIG_POSIX_MQUEUE=y
1505 # CONFIG_BSD_PROCESS_ACCT is not set
1506 # CONFIG_TASKSTATS is not set
1507 -# CONFIG_USER_NS is not set
1508 -# CONFIG_PID_NS is not set
1509 # CONFIG_AUDIT is not set
1510 # CONFIG_IKCONFIG is not set
1511 CONFIG_LOG_BUF_SHIFT=14
1512 # CONFIG_CGROUPS is not set
1513 -# CONFIG_FAIR_GROUP_SCHED is not set
1514 +# CONFIG_GROUP_SCHED is not set
1515 CONFIG_SYSFS_DEPRECATED=y
1516 +CONFIG_SYSFS_DEPRECATED_V2=y
1517 CONFIG_RELAY=y
1518 +# CONFIG_NAMESPACES is not set
1519 CONFIG_BLK_DEV_INITRD=y
1520 CONFIG_INITRAMFS_SOURCE=""
1521 CONFIG_CC_OPTIMIZE_FOR_SIZE=y
1522 @@ -58,11 +58,13 @@
1523 CONFIG_PRINTK=y
1524 CONFIG_BUG=y
1525 CONFIG_ELF_CORE=y
1526 +# CONFIG_COMPAT_BRK is not set
1527 # CONFIG_BASE_FULL is not set
1528 CONFIG_FUTEX=y
1529 CONFIG_ANON_INODES=y
1530 CONFIG_EPOLL=y
1531 CONFIG_SIGNALFD=y
1532 +CONFIG_TIMERFD=y
1533 CONFIG_EVENTFD=y
1534 CONFIG_SHMEM=y
1535 CONFIG_VM_EVENT_COUNTERS=y
1536 @@ -70,6 +72,14 @@
1537 # CONFIG_SLAB is not set
1538 CONFIG_SLUB=y
1539 # CONFIG_SLOB is not set
1540 +CONFIG_PROFILING=y
1541 +# CONFIG_MARKERS is not set
1542 +CONFIG_OPROFILE=m
1543 +CONFIG_HAVE_OPROFILE=y
1544 +CONFIG_KPROBES=y
1545 +CONFIG_HAVE_KPROBES=y
1546 +# CONFIG_HAVE_KRETPROBES is not set
1547 +CONFIG_PROC_PAGE_MONITOR=y
1548 CONFIG_SLABINFO=y
1549 CONFIG_RT_MUTEXES=y
1550 # CONFIG_TINY_SHMEM is not set
1551 @@ -98,10 +108,15 @@
1552 CONFIG_DEFAULT_CFQ=y
1553 # CONFIG_DEFAULT_NOOP is not set
1554 CONFIG_DEFAULT_IOSCHED="cfq"
1555 +CONFIG_CLASSIC_RCU=y
1556
1557 #
1558 # System Type and features
1559 #
1560 +CONFIG_TICK_ONESHOT=y
1561 +CONFIG_NO_HZ=y
1562 +CONFIG_HIGH_RES_TIMERS=y
1563 +CONFIG_GENERIC_CLOCKEVENTS_BUILD=y
1564 CONFIG_SUBARCH_AVR32B=y
1565 CONFIG_MMU=y
1566 CONFIG_PERFORMANCE_COUNTERS=y
1567 @@ -113,12 +128,16 @@
1568 CONFIG_BOARD_ATSTK1002=y
1569 # CONFIG_BOARD_ATSTK1003 is not set
1570 # CONFIG_BOARD_ATSTK1004 is not set
1571 +# CONFIG_BOARD_ATSTK1006 is not set
1572 # CONFIG_BOARD_ATSTK100X_CUSTOM is not set
1573 # CONFIG_BOARD_ATSTK100X_SPI1 is not set
1574 # CONFIG_BOARD_ATSTK1000_J2_LED is not set
1575 # CONFIG_BOARD_ATSTK1000_J2_LED8 is not set
1576 # CONFIG_BOARD_ATSTK1000_J2_RGB is not set
1577 CONFIG_BOARD_ATSTK1000_EXTDAC=y
1578 +# CONFIG_BOARD_ATSTK100X_ENABLE_AC97 is not set
1579 +# CONFIG_BOARD_ATSTK1000_CF_HACKS is not set
1580 +# CONFIG_BOARD_ATSTK100X_ENABLE_PSIF is not set
1581 CONFIG_LOADER_U_BOOT=y
1582
1583 #
1584 @@ -127,6 +146,7 @@
1585 # CONFIG_AP700X_32_BIT_SMC is not set
1586 CONFIG_AP700X_16_BIT_SMC=y
1587 # CONFIG_AP700X_8_BIT_SMC is not set
1588 +CONFIG_GPIO_DEV=y
1589 CONFIG_LOAD_ADDRESS=0x10000000
1590 CONFIG_ENTRY_ADDRESS=0x90000000
1591 CONFIG_PHYS_OFFSET=0x10000000
1592 @@ -152,16 +172,26 @@
1593 CONFIG_ZONE_DMA_FLAG=0
1594 CONFIG_VIRT_TO_BUS=y
1595 # CONFIG_OWNERSHIP_TRACE is not set
1596 +CONFIG_NMI_DEBUGGING=y
1597 +CONFIG_DW_DMAC=y
1598 # CONFIG_HZ_100 is not set
1599 CONFIG_HZ_250=y
1600 # CONFIG_HZ_300 is not set
1601 # CONFIG_HZ_1000 is not set
1602 CONFIG_HZ=250
1603 +# CONFIG_SCHED_HRTICK is not set
1604 CONFIG_CMDLINE=""
1605
1606 #
1607 # Power management options
1608 #
1609 +CONFIG_ARCH_SUSPEND_POSSIBLE=y
1610 +CONFIG_PM=y
1611 +# CONFIG_PM_LEGACY is not set
1612 +# CONFIG_PM_DEBUG is not set
1613 +CONFIG_PM_SLEEP=y
1614 +CONFIG_SUSPEND=y
1615 +CONFIG_SUSPEND_FREEZER=y
1616
1617 #
1618 # CPU Frequency scaling
1619 @@ -170,9 +200,9 @@
1620 CONFIG_CPU_FREQ_TABLE=y
1621 # CONFIG_CPU_FREQ_DEBUG is not set
1622 # CONFIG_CPU_FREQ_STAT is not set
1623 -CONFIG_CPU_FREQ_DEFAULT_GOV_PERFORMANCE=y
1624 +# CONFIG_CPU_FREQ_DEFAULT_GOV_PERFORMANCE is not set
1625 # CONFIG_CPU_FREQ_DEFAULT_GOV_USERSPACE is not set
1626 -# CONFIG_CPU_FREQ_DEFAULT_GOV_ONDEMAND is not set
1627 +CONFIG_CPU_FREQ_DEFAULT_GOV_ONDEMAND=y
1628 # CONFIG_CPU_FREQ_DEFAULT_GOV_CONSERVATIVE is not set
1629 CONFIG_CPU_FREQ_GOV_PERFORMANCE=y
1630 # CONFIG_CPU_FREQ_GOV_POWERSAVE is not set
1631 @@ -208,6 +238,7 @@
1632 CONFIG_XFRM_USER=m
1633 # CONFIG_XFRM_SUB_POLICY is not set
1634 # CONFIG_XFRM_MIGRATE is not set
1635 +# CONFIG_XFRM_STATISTICS is not set
1636 CONFIG_NET_KEY=m
1637 # CONFIG_NET_KEY_MIGRATE is not set
1638 CONFIG_INET=y
1639 @@ -279,6 +310,7 @@
1640 # CONFIG_NET_PKTGEN is not set
1641 # CONFIG_NET_TCPPROBE is not set
1642 # CONFIG_HAMRADIO is not set
1643 +# CONFIG_CAN is not set
1644 # CONFIG_IRDA is not set
1645 # CONFIG_BT is not set
1646 # CONFIG_AF_RXRPC is not set
1647 @@ -395,13 +427,18 @@
1648 CONFIG_BLK_DEV_RAM=m
1649 CONFIG_BLK_DEV_RAM_COUNT=16
1650 CONFIG_BLK_DEV_RAM_SIZE=4096
1651 -CONFIG_BLK_DEV_RAM_BLOCKSIZE=1024
1652 +# CONFIG_BLK_DEV_XIP is not set
1653 # CONFIG_CDROM_PKTCDVD is not set
1654 # CONFIG_ATA_OVER_ETH is not set
1655 CONFIG_MISC_DEVICES=y
1656 +CONFIG_ATMEL_PWM=m
1657 +CONFIG_ATMEL_TCLIB=y
1658 +CONFIG_ATMEL_TCB_CLKSRC=y
1659 +CONFIG_ATMEL_TCB_CLKSRC_BLOCK=0
1660 # CONFIG_EEPROM_93CX6 is not set
1661 CONFIG_ATMEL_SSC=m
1662 -# CONFIG_IDE is not set
1663 +# CONFIG_ENCLOSURE_SERVICES is not set
1664 +# CONFIG_HAVE_IDE is not set
1665
1666 #
1667 # SCSI device support
1668 @@ -444,6 +481,7 @@
1669 # CONFIG_SCSI_LOWLEVEL is not set
1670 CONFIG_ATA=m
1671 # CONFIG_ATA_NONSTANDARD is not set
1672 +# CONFIG_SATA_MV is not set
1673 CONFIG_PATA_AT32=m
1674 # CONFIG_PATA_PLATFORM is not set
1675 # CONFIG_MD is not set
1676 @@ -469,11 +507,13 @@
1677 # CONFIG_SMSC_PHY is not set
1678 # CONFIG_BROADCOM_PHY is not set
1679 # CONFIG_ICPLUS_PHY is not set
1680 +# CONFIG_REALTEK_PHY is not set
1681 # CONFIG_FIXED_PHY is not set
1682 # CONFIG_MDIO_BITBANG is not set
1683 CONFIG_NET_ETHERNET=y
1684 # CONFIG_MII is not set
1685 CONFIG_MACB=y
1686 +# CONFIG_ENC28J60 is not set
1687 # CONFIG_IBM_NEW_EMAC_ZMII is not set
1688 # CONFIG_IBM_NEW_EMAC_RGMII is not set
1689 # CONFIG_IBM_NEW_EMAC_TAH is not set
1690 @@ -500,7 +540,6 @@
1691 # CONFIG_PPPOL2TP is not set
1692 # CONFIG_SLIP is not set
1693 CONFIG_SLHC=m
1694 -# CONFIG_SHAPER is not set
1695 # CONFIG_NETCONSOLE is not set
1696 # CONFIG_NETPOLL is not set
1697 # CONFIG_NET_POLL_CONTROLLER is not set
1698 @@ -568,6 +607,7 @@
1699 #
1700 CONFIG_SERIAL_ATMEL=y
1701 CONFIG_SERIAL_ATMEL_CONSOLE=y
1702 +CONFIG_SERIAL_ATMEL_PDC=y
1703 # CONFIG_SERIAL_ATMEL_TTYAT is not set
1704 CONFIG_SERIAL_CORE=y
1705 CONFIG_SERIAL_CORE_CONSOLE=y
1706 @@ -575,8 +615,6 @@
1707 # CONFIG_LEGACY_PTYS is not set
1708 # CONFIG_IPMI_HANDLER is not set
1709 # CONFIG_HW_RANDOM is not set
1710 -# CONFIG_RTC is not set
1711 -# CONFIG_GEN_RTC is not set
1712 # CONFIG_R3964 is not set
1713 # CONFIG_RAW_DRIVER is not set
1714 # CONFIG_TCG_TPM is not set
1715 @@ -594,6 +632,7 @@
1716 #
1717 # I2C Hardware Bus support
1718 #
1719 +CONFIG_I2C_ATMELTWI=m
1720 CONFIG_I2C_GPIO=m
1721 # CONFIG_I2C_OCORES is not set
1722 # CONFIG_I2C_PARPORT_LIGHT is not set
1723 @@ -604,13 +643,12 @@
1724 #
1725 # Miscellaneous I2C Chip support
1726 #
1727 -# CONFIG_SENSORS_DS1337 is not set
1728 -# CONFIG_SENSORS_DS1374 is not set
1729 # CONFIG_DS1682 is not set
1730 # CONFIG_SENSORS_EEPROM is not set
1731 # CONFIG_SENSORS_PCF8574 is not set
1732 -# CONFIG_SENSORS_PCA9539 is not set
1733 +# CONFIG_PCF8575 is not set
1734 # CONFIG_SENSORS_PCF8591 is not set
1735 +# CONFIG_TPS65010 is not set
1736 # CONFIG_SENSORS_MAX6875 is not set
1737 # CONFIG_SENSORS_TSL2550 is not set
1738 # CONFIG_I2C_DEBUG_CORE is not set
1739 @@ -637,9 +675,27 @@
1740 # CONFIG_SPI_AT25 is not set
1741 CONFIG_SPI_SPIDEV=m
1742 # CONFIG_SPI_TLE62X0 is not set
1743 +CONFIG_HAVE_GPIO_LIB=y
1744 +
1745 +#
1746 +# GPIO Support
1747 +#
1748 +# CONFIG_DEBUG_GPIO is not set
1749 +
1750 +#
1751 +# I2C GPIO expanders:
1752 +#
1753 +# CONFIG_GPIO_PCA953X is not set
1754 +# CONFIG_GPIO_PCF857X is not set
1755 +
1756 +#
1757 +# SPI GPIO expanders:
1758 +#
1759 +# CONFIG_GPIO_MCP23S08 is not set
1760 # CONFIG_W1 is not set
1761 # CONFIG_POWER_SUPPLY is not set
1762 # CONFIG_HWMON is not set
1763 +# CONFIG_THERMAL is not set
1764 CONFIG_WATCHDOG=y
1765 # CONFIG_WATCHDOG_NOWAYOUT is not set
1766
1767 @@ -732,12 +788,18 @@
1768 #
1769 # Generic devices
1770 #
1771 +CONFIG_SND_AC97_CODEC=m
1772 # CONFIG_SND_DUMMY is not set
1773 # CONFIG_SND_MTPAV is not set
1774 # CONFIG_SND_SERIAL_U16550 is not set
1775 # CONFIG_SND_MPU401 is not set
1776
1777 #
1778 +# AVR32 devices
1779 +#
1780 +CONFIG_SND_ATMEL_AC97=m
1781 +
1782 +#
1783 # SPI devices
1784 #
1785 CONFIG_SND_AT73C213=m
1786 @@ -753,9 +815,14 @@
1787 #
1788
1789 #
1790 +# ALSA SoC audio for Freescale SOCs
1791 +#
1792 +
1793 +#
1794 # Open Sound System
1795 #
1796 # CONFIG_SOUND_PRIME is not set
1797 +CONFIG_AC97_BUS=m
1798 # CONFIG_HID_SUPPORT is not set
1799 CONFIG_USB_SUPPORT=y
1800 # CONFIG_USB_ARCH_HAS_HCD is not set
1801 @@ -765,10 +832,6 @@
1802 #
1803 # NOTE: USB_STORAGE enables SCSI, and 'SCSI disk support'
1804 #
1805 -
1806 -#
1807 -# USB Gadget Support
1808 -#
1809 CONFIG_USB_GADGET=y
1810 # CONFIG_USB_GADGET_DEBUG is not set
1811 # CONFIG_USB_GADGET_DEBUG_FILES is not set
1812 @@ -796,27 +859,31 @@
1813 # CONFIG_USB_FILE_STORAGE_TEST is not set
1814 CONFIG_USB_G_SERIAL=m
1815 # CONFIG_USB_MIDI_GADGET is not set
1816 -CONFIG_MMC=m
1817 +# CONFIG_USB_G_PRINTER is not set
1818 +CONFIG_MMC=y
1819 # CONFIG_MMC_DEBUG is not set
1820 # CONFIG_MMC_UNSAFE_RESUME is not set
1821
1822 #
1823 # MMC/SD Card Drivers
1824 #
1825 -CONFIG_MMC_BLOCK=m
1826 +CONFIG_MMC_BLOCK=y
1827 CONFIG_MMC_BLOCK_BOUNCE=y
1828 # CONFIG_SDIO_UART is not set
1829
1830 #
1831 # MMC/SD Host Controller Drivers
1832 #
1833 +CONFIG_MMC_ATMELMCI=y
1834 CONFIG_MMC_SPI=m
1835 +# CONFIG_MEMSTICK is not set
1836 CONFIG_NEW_LEDS=y
1837 CONFIG_LEDS_CLASS=m
1838
1839 #
1840 # LED drivers
1841 #
1842 +CONFIG_LEDS_ATMEL_PWM=m
1843 CONFIG_LEDS_GPIO=m
1844
1845 #
1846 @@ -853,19 +920,22 @@
1847 # CONFIG_RTC_DRV_PCF8563 is not set
1848 # CONFIG_RTC_DRV_PCF8583 is not set
1849 # CONFIG_RTC_DRV_M41T80 is not set
1850 +# CONFIG_RTC_DRV_S35390A is not set
1851
1852 #
1853 # SPI RTC drivers
1854 #
1855 -# CONFIG_RTC_DRV_RS5C348 is not set
1856 # CONFIG_RTC_DRV_MAX6902 is not set
1857 +# CONFIG_RTC_DRV_R9701 is not set
1858 +# CONFIG_RTC_DRV_RS5C348 is not set
1859
1860 #
1861 # Platform RTC drivers
1862 #
1863 +# CONFIG_RTC_DRV_DS1511 is not set
1864 # CONFIG_RTC_DRV_DS1553 is not set
1865 -# CONFIG_RTC_DRV_STK17TA8 is not set
1866 # CONFIG_RTC_DRV_DS1742 is not set
1867 +# CONFIG_RTC_DRV_STK17TA8 is not set
1868 # CONFIG_RTC_DRV_M48T86 is not set
1869 # CONFIG_RTC_DRV_M48T59 is not set
1870 # CONFIG_RTC_DRV_V3020 is not set
1871 @@ -883,13 +953,13 @@
1872 #
1873 # File systems
1874 #
1875 -CONFIG_EXT2_FS=m
1876 +CONFIG_EXT2_FS=y
1877 # CONFIG_EXT2_FS_XATTR is not set
1878 # CONFIG_EXT2_FS_XIP is not set
1879 -CONFIG_EXT3_FS=m
1880 +CONFIG_EXT3_FS=y
1881 # CONFIG_EXT3_FS_XATTR is not set
1882 # CONFIG_EXT4DEV_FS is not set
1883 -CONFIG_JBD=m
1884 +CONFIG_JBD=y
1885 # CONFIG_JBD_DEBUG is not set
1886 # CONFIG_REISERFS_FS is not set
1887 # CONFIG_JFS_FS is not set
1888 @@ -897,12 +967,10 @@
1889 # CONFIG_XFS_FS is not set
1890 # CONFIG_GFS2_FS is not set
1891 # CONFIG_OCFS2_FS is not set
1892 -CONFIG_MINIX_FS=m
1893 -# CONFIG_ROMFS_FS is not set
1894 +# CONFIG_DNOTIFY is not set
1895 CONFIG_INOTIFY=y
1896 CONFIG_INOTIFY_USER=y
1897 # CONFIG_QUOTA is not set
1898 -# CONFIG_DNOTIFY is not set
1899 # CONFIG_AUTOFS_FS is not set
1900 # CONFIG_AUTOFS4_FS is not set
1901 CONFIG_FUSE_FS=m
1902 @@ -933,7 +1001,7 @@
1903 CONFIG_TMPFS=y
1904 # CONFIG_TMPFS_POSIX_ACL is not set
1905 # CONFIG_HUGETLB_PAGE is not set
1906 -# CONFIG_CONFIGFS_FS is not set
1907 +CONFIG_CONFIGFS_FS=y
1908
1909 #
1910 # Miscellaneous filesystems
1911 @@ -957,8 +1025,10 @@
1912 # CONFIG_JFFS2_RUBIN is not set
1913 # CONFIG_CRAMFS is not set
1914 # CONFIG_VXFS_FS is not set
1915 +CONFIG_MINIX_FS=m
1916 # CONFIG_HPFS_FS is not set
1917 # CONFIG_QNX4FS_FS is not set
1918 +# CONFIG_ROMFS_FS is not set
1919 # CONFIG_SYSV_FS is not set
1920 # CONFIG_UFS_FS is not set
1921 CONFIG_NETWORK_FILESYSTEMS=y
1922 @@ -1028,11 +1098,6 @@
1923 # CONFIG_NLS_KOI8_U is not set
1924 CONFIG_NLS_UTF8=m
1925 # CONFIG_DLM is not set
1926 -CONFIG_INSTRUMENTATION=y
1927 -CONFIG_PROFILING=y
1928 -CONFIG_OPROFILE=m
1929 -CONFIG_KPROBES=y
1930 -# CONFIG_MARKERS is not set
1931
1932 #
1933 # Kernel hacking
1934 @@ -1051,6 +1116,7 @@
1935 # CONFIG_SCHEDSTATS is not set
1936 # CONFIG_TIMER_STATS is not set
1937 # CONFIG_SLUB_DEBUG_ON is not set
1938 +# CONFIG_SLUB_STATS is not set
1939 # CONFIG_DEBUG_RT_MUTEXES is not set
1940 # CONFIG_RT_MUTEX_TESTER is not set
1941 # CONFIG_DEBUG_SPINLOCK is not set
1942 @@ -1067,9 +1133,10 @@
1943 # CONFIG_DEBUG_LIST is not set
1944 # CONFIG_DEBUG_SG is not set
1945 CONFIG_FRAME_POINTER=y
1946 -CONFIG_FORCED_INLINING=y
1947 # CONFIG_BOOT_PRINTK_DELAY is not set
1948 # CONFIG_RCU_TORTURE_TEST is not set
1949 +# CONFIG_KPROBES_SANITY_TEST is not set
1950 +# CONFIG_BACKTRACE_SELF_TEST is not set
1951 # CONFIG_LKDTM is not set
1952 # CONFIG_FAULT_INJECTION is not set
1953 # CONFIG_SAMPLES is not set
1954 @@ -1082,7 +1149,9 @@
1955 # CONFIG_SECURITY_FILE_CAPABILITIES is not set
1956 CONFIG_CRYPTO=y
1957 CONFIG_CRYPTO_ALGAPI=m
1958 +CONFIG_CRYPTO_AEAD=m
1959 CONFIG_CRYPTO_BLKCIPHER=m
1960 +# CONFIG_CRYPTO_SEQIV is not set
1961 CONFIG_CRYPTO_HASH=m
1962 CONFIG_CRYPTO_MANAGER=m
1963 CONFIG_CRYPTO_HMAC=m
1964 @@ -1101,6 +1170,9 @@
1965 # CONFIG_CRYPTO_PCBC is not set
1966 # CONFIG_CRYPTO_LRW is not set
1967 # CONFIG_CRYPTO_XTS is not set
1968 +# CONFIG_CRYPTO_CTR is not set
1969 +# CONFIG_CRYPTO_GCM is not set
1970 +# CONFIG_CRYPTO_CCM is not set
1971 # CONFIG_CRYPTO_CRYPTD is not set
1972 CONFIG_CRYPTO_DES=m
1973 # CONFIG_CRYPTO_FCRYPT is not set
1974 @@ -1115,12 +1187,14 @@
1975 # CONFIG_CRYPTO_KHAZAD is not set
1976 # CONFIG_CRYPTO_ANUBIS is not set
1977 # CONFIG_CRYPTO_SEED is not set
1978 +# CONFIG_CRYPTO_SALSA20 is not set
1979 CONFIG_CRYPTO_DEFLATE=m
1980 # CONFIG_CRYPTO_MICHAEL_MIC is not set
1981 # CONFIG_CRYPTO_CRC32C is not set
1982 # CONFIG_CRYPTO_CAMELLIA is not set
1983 # CONFIG_CRYPTO_TEST is not set
1984 -# CONFIG_CRYPTO_AUTHENC is not set
1985 +CONFIG_CRYPTO_AUTHENC=m
1986 +# CONFIG_CRYPTO_LZO is not set
1987 # CONFIG_CRYPTO_HW is not set
1988
1989 #
1990 @@ -1135,6 +1209,7 @@
1991 # CONFIG_LIBCRC32C is not set
1992 CONFIG_ZLIB_INFLATE=y
1993 CONFIG_ZLIB_DEFLATE=y
1994 +CONFIG_GENERIC_ALLOCATOR=y
1995 CONFIG_PLIST=y
1996 CONFIG_HAS_IOMEM=y
1997 CONFIG_HAS_IOPORT=y
1998 diff --exclude=.git -urN linux-2.6.25.6/arch/avr32/configs/atstk1003_defconfig avr32-2.6/arch/avr32/configs/atstk1003_defconfig
1999 --- linux-2.6.25.6/arch/avr32/configs/atstk1003_defconfig 2008-06-09 20:27:19.000000000 +0200
2000 +++ avr32-2.6/arch/avr32/configs/atstk1003_defconfig 2008-06-12 15:09:38.715815679 +0200
2001 @@ -1,7 +1,7 @@
2002 #
2003 # Automatically generated make config: don't edit
2004 -# Linux kernel version: 2.6.24-rc7
2005 -# Wed Jan 9 22:54:34 2008
2006 +# Linux kernel version: 2.6.25.4
2007 +# Wed Jun 11 15:33:36 2008
2008 #
2009 CONFIG_AVR32=y
2010 CONFIG_GENERIC_GPIO=y
2011 @@ -13,10 +13,10 @@
2012 CONFIG_GENERIC_IRQ_PROBE=y
2013 CONFIG_RWSEM_GENERIC_SPINLOCK=y
2014 CONFIG_GENERIC_TIME=y
2015 +CONFIG_GENERIC_CLOCKEVENTS=y
2016 # CONFIG_RWSEM_XCHGADD_ALGORITHM is not set
2017 # CONFIG_ARCH_HAS_ILOG2_U32 is not set
2018 # CONFIG_ARCH_HAS_ILOG2_U64 is not set
2019 -CONFIG_ARCH_SUPPORTS_OPROFILE=y
2020 CONFIG_GENERIC_HWEIGHT=y
2021 CONFIG_GENERIC_CALIBRATE_DELAY=y
2022 CONFIG_GENERIC_BUG=y
2023 @@ -39,17 +39,15 @@
2024 CONFIG_TASKSTATS=y
2025 CONFIG_TASK_DELAY_ACCT=y
2026 # CONFIG_TASK_XACCT is not set
2027 -# CONFIG_USER_NS is not set
2028 -# CONFIG_PID_NS is not set
2029 CONFIG_AUDIT=y
2030 # CONFIG_IKCONFIG is not set
2031 CONFIG_LOG_BUF_SHIFT=14
2032 # CONFIG_CGROUPS is not set
2033 -CONFIG_FAIR_GROUP_SCHED=y
2034 -CONFIG_FAIR_USER_SCHED=y
2035 -# CONFIG_FAIR_CGROUP_SCHED is not set
2036 +# CONFIG_GROUP_SCHED is not set
2037 CONFIG_SYSFS_DEPRECATED=y
2038 +CONFIG_SYSFS_DEPRECATED_V2=y
2039 CONFIG_RELAY=y
2040 +# CONFIG_NAMESPACES is not set
2041 CONFIG_BLK_DEV_INITRD=y
2042 CONFIG_INITRAMFS_SOURCE=""
2043 CONFIG_CC_OPTIMIZE_FOR_SIZE=y
2044 @@ -63,11 +61,13 @@
2045 CONFIG_PRINTK=y
2046 CONFIG_BUG=y
2047 CONFIG_ELF_CORE=y
2048 +# CONFIG_COMPAT_BRK is not set
2049 # CONFIG_BASE_FULL is not set
2050 CONFIG_FUTEX=y
2051 CONFIG_ANON_INODES=y
2052 CONFIG_EPOLL=y
2053 CONFIG_SIGNALFD=y
2054 +CONFIG_TIMERFD=y
2055 CONFIG_EVENTFD=y
2056 CONFIG_SHMEM=y
2057 CONFIG_VM_EVENT_COUNTERS=y
2058 @@ -75,6 +75,14 @@
2059 # CONFIG_SLAB is not set
2060 CONFIG_SLUB=y
2061 # CONFIG_SLOB is not set
2062 +CONFIG_PROFILING=y
2063 +# CONFIG_MARKERS is not set
2064 +CONFIG_OPROFILE=m
2065 +CONFIG_HAVE_OPROFILE=y
2066 +CONFIG_KPROBES=y
2067 +CONFIG_HAVE_KPROBES=y
2068 +# CONFIG_HAVE_KRETPROBES is not set
2069 +CONFIG_PROC_PAGE_MONITOR=y
2070 CONFIG_SLABINFO=y
2071 CONFIG_RT_MUTEXES=y
2072 # CONFIG_TINY_SHMEM is not set
2073 @@ -103,10 +111,15 @@
2074 CONFIG_DEFAULT_CFQ=y
2075 # CONFIG_DEFAULT_NOOP is not set
2076 CONFIG_DEFAULT_IOSCHED="cfq"
2077 +CONFIG_CLASSIC_RCU=y
2078
2079 #
2080 # System Type and features
2081 #
2082 +CONFIG_TICK_ONESHOT=y
2083 +CONFIG_NO_HZ=y
2084 +CONFIG_HIGH_RES_TIMERS=y
2085 +CONFIG_GENERIC_CLOCKEVENTS_BUILD=y
2086 CONFIG_SUBARCH_AVR32B=y
2087 CONFIG_MMU=y
2088 CONFIG_PERFORMANCE_COUNTERS=y
2089 @@ -118,12 +131,16 @@
2090 # CONFIG_BOARD_ATSTK1002 is not set
2091 CONFIG_BOARD_ATSTK1003=y
2092 # CONFIG_BOARD_ATSTK1004 is not set
2093 +# CONFIG_BOARD_ATSTK1006 is not set
2094 # CONFIG_BOARD_ATSTK100X_CUSTOM is not set
2095 # CONFIG_BOARD_ATSTK100X_SPI1 is not set
2096 # CONFIG_BOARD_ATSTK1000_J2_LED is not set
2097 # CONFIG_BOARD_ATSTK1000_J2_LED8 is not set
2098 # CONFIG_BOARD_ATSTK1000_J2_RGB is not set
2099 CONFIG_BOARD_ATSTK1000_EXTDAC=y
2100 +# CONFIG_BOARD_ATSTK100X_ENABLE_AC97 is not set
2101 +# CONFIG_BOARD_ATSTK1000_CF_HACKS is not set
2102 +# CONFIG_BOARD_ATSTK100X_ENABLE_PSIF is not set
2103 CONFIG_LOADER_U_BOOT=y
2104
2105 #
2106 @@ -132,6 +149,7 @@
2107 # CONFIG_AP700X_32_BIT_SMC is not set
2108 CONFIG_AP700X_16_BIT_SMC=y
2109 # CONFIG_AP700X_8_BIT_SMC is not set
2110 +CONFIG_GPIO_DEV=y
2111 CONFIG_LOAD_ADDRESS=0x10000000
2112 CONFIG_ENTRY_ADDRESS=0x90000000
2113 CONFIG_PHYS_OFFSET=0x10000000
2114 @@ -157,16 +175,26 @@
2115 CONFIG_ZONE_DMA_FLAG=0
2116 CONFIG_VIRT_TO_BUS=y
2117 # CONFIG_OWNERSHIP_TRACE is not set
2118 +CONFIG_NMI_DEBUGGING=y
2119 +CONFIG_DW_DMAC=y
2120 # CONFIG_HZ_100 is not set
2121 CONFIG_HZ_250=y
2122 # CONFIG_HZ_300 is not set
2123 # CONFIG_HZ_1000 is not set
2124 CONFIG_HZ=250
2125 +# CONFIG_SCHED_HRTICK is not set
2126 CONFIG_CMDLINE=""
2127
2128 #
2129 # Power management options
2130 #
2131 +CONFIG_ARCH_SUSPEND_POSSIBLE=y
2132 +CONFIG_PM=y
2133 +# CONFIG_PM_LEGACY is not set
2134 +# CONFIG_PM_DEBUG is not set
2135 +CONFIG_PM_SLEEP=y
2136 +CONFIG_SUSPEND=y
2137 +CONFIG_SUSPEND_FREEZER=y
2138
2139 #
2140 # CPU Frequency scaling
2141 @@ -175,9 +203,9 @@
2142 CONFIG_CPU_FREQ_TABLE=y
2143 # CONFIG_CPU_FREQ_DEBUG is not set
2144 # CONFIG_CPU_FREQ_STAT is not set
2145 -CONFIG_CPU_FREQ_DEFAULT_GOV_PERFORMANCE=y
2146 +# CONFIG_CPU_FREQ_DEFAULT_GOV_PERFORMANCE is not set
2147 # CONFIG_CPU_FREQ_DEFAULT_GOV_USERSPACE is not set
2148 -# CONFIG_CPU_FREQ_DEFAULT_GOV_ONDEMAND is not set
2149 +CONFIG_CPU_FREQ_DEFAULT_GOV_ONDEMAND=y
2150 # CONFIG_CPU_FREQ_DEFAULT_GOV_CONSERVATIVE is not set
2151 CONFIG_CPU_FREQ_GOV_PERFORMANCE=y
2152 # CONFIG_CPU_FREQ_GOV_POWERSAVE is not set
2153 @@ -260,6 +288,7 @@
2154 # CONFIG_NET_PKTGEN is not set
2155 # CONFIG_NET_TCPPROBE is not set
2156 # CONFIG_HAMRADIO is not set
2157 +# CONFIG_CAN is not set
2158 # CONFIG_IRDA is not set
2159 # CONFIG_BT is not set
2160 # CONFIG_AF_RXRPC is not set
2161 @@ -376,13 +405,18 @@
2162 CONFIG_BLK_DEV_RAM=m
2163 CONFIG_BLK_DEV_RAM_COUNT=16
2164 CONFIG_BLK_DEV_RAM_SIZE=4096
2165 -CONFIG_BLK_DEV_RAM_BLOCKSIZE=1024
2166 +# CONFIG_BLK_DEV_XIP is not set
2167 # CONFIG_CDROM_PKTCDVD is not set
2168 # CONFIG_ATA_OVER_ETH is not set
2169 CONFIG_MISC_DEVICES=y
2170 +CONFIG_ATMEL_PWM=m
2171 +CONFIG_ATMEL_TCLIB=y
2172 +CONFIG_ATMEL_TCB_CLKSRC=y
2173 +CONFIG_ATMEL_TCB_CLKSRC_BLOCK=0
2174 # CONFIG_EEPROM_93CX6 is not set
2175 CONFIG_ATMEL_SSC=m
2176 -# CONFIG_IDE is not set
2177 +# CONFIG_ENCLOSURE_SERVICES is not set
2178 +# CONFIG_HAVE_IDE is not set
2179
2180 #
2181 # SCSI device support
2182 @@ -427,6 +461,7 @@
2183 # CONFIG_SCSI_DEBUG is not set
2184 CONFIG_ATA=m
2185 # CONFIG_ATA_NONSTANDARD is not set
2186 +# CONFIG_SATA_MV is not set
2187 CONFIG_PATA_AT32=m
2188 # CONFIG_PATA_PLATFORM is not set
2189 # CONFIG_MD is not set
2190 @@ -460,7 +495,6 @@
2191 # CONFIG_PPPOL2TP is not set
2192 # CONFIG_SLIP is not set
2193 CONFIG_SLHC=m
2194 -# CONFIG_SHAPER is not set
2195 # CONFIG_NETCONSOLE is not set
2196 # CONFIG_NETPOLL is not set
2197 # CONFIG_NET_POLL_CONTROLLER is not set
2198 @@ -528,6 +562,7 @@
2199 #
2200 CONFIG_SERIAL_ATMEL=y
2201 CONFIG_SERIAL_ATMEL_CONSOLE=y
2202 +CONFIG_SERIAL_ATMEL_PDC=y
2203 # CONFIG_SERIAL_ATMEL_TTYAT is not set
2204 CONFIG_SERIAL_CORE=y
2205 CONFIG_SERIAL_CORE_CONSOLE=y
2206 @@ -535,8 +570,6 @@
2207 # CONFIG_LEGACY_PTYS is not set
2208 # CONFIG_IPMI_HANDLER is not set
2209 # CONFIG_HW_RANDOM is not set
2210 -# CONFIG_RTC is not set
2211 -# CONFIG_GEN_RTC is not set
2212 # CONFIG_R3964 is not set
2213 # CONFIG_RAW_DRIVER is not set
2214 # CONFIG_TCG_TPM is not set
2215 @@ -554,6 +587,7 @@
2216 #
2217 # I2C Hardware Bus support
2218 #
2219 +CONFIG_I2C_ATMELTWI=m
2220 CONFIG_I2C_GPIO=m
2221 # CONFIG_I2C_OCORES is not set
2222 # CONFIG_I2C_PARPORT_LIGHT is not set
2223 @@ -564,13 +598,12 @@
2224 #
2225 # Miscellaneous I2C Chip support
2226 #
2227 -# CONFIG_SENSORS_DS1337 is not set
2228 -# CONFIG_SENSORS_DS1374 is not set
2229 # CONFIG_DS1682 is not set
2230 # CONFIG_SENSORS_EEPROM is not set
2231 # CONFIG_SENSORS_PCF8574 is not set
2232 -# CONFIG_SENSORS_PCA9539 is not set
2233 +# CONFIG_PCF8575 is not set
2234 # CONFIG_SENSORS_PCF8591 is not set
2235 +# CONFIG_TPS65010 is not set
2236 # CONFIG_SENSORS_MAX6875 is not set
2237 # CONFIG_SENSORS_TSL2550 is not set
2238 # CONFIG_I2C_DEBUG_CORE is not set
2239 @@ -597,9 +630,27 @@
2240 # CONFIG_SPI_AT25 is not set
2241 CONFIG_SPI_SPIDEV=m
2242 # CONFIG_SPI_TLE62X0 is not set
2243 +CONFIG_HAVE_GPIO_LIB=y
2244 +
2245 +#
2246 +# GPIO Support
2247 +#
2248 +# CONFIG_DEBUG_GPIO is not set
2249 +
2250 +#
2251 +# I2C GPIO expanders:
2252 +#
2253 +# CONFIG_GPIO_PCA953X is not set
2254 +# CONFIG_GPIO_PCF857X is not set
2255 +
2256 +#
2257 +# SPI GPIO expanders:
2258 +#
2259 +# CONFIG_GPIO_MCP23S08 is not set
2260 # CONFIG_W1 is not set
2261 # CONFIG_POWER_SUPPLY is not set
2262 # CONFIG_HWMON is not set
2263 +# CONFIG_THERMAL is not set
2264 CONFIG_WATCHDOG=y
2265 # CONFIG_WATCHDOG_NOWAYOUT is not set
2266
2267 @@ -665,12 +716,18 @@
2268 #
2269 # Generic devices
2270 #
2271 +CONFIG_SND_AC97_CODEC=m
2272 # CONFIG_SND_DUMMY is not set
2273 # CONFIG_SND_MTPAV is not set
2274 # CONFIG_SND_SERIAL_U16550 is not set
2275 # CONFIG_SND_MPU401 is not set
2276
2277 #
2278 +# AVR32 devices
2279 +#
2280 +CONFIG_SND_ATMEL_AC97=m
2281 +
2282 +#
2283 # SPI devices
2284 #
2285 CONFIG_SND_AT73C213=m
2286 @@ -686,9 +743,14 @@
2287 #
2288
2289 #
2290 +# ALSA SoC audio for Freescale SOCs
2291 +#
2292 +
2293 +#
2294 # Open Sound System
2295 #
2296 # CONFIG_SOUND_PRIME is not set
2297 +CONFIG_AC97_BUS=m
2298 # CONFIG_HID_SUPPORT is not set
2299 CONFIG_USB_SUPPORT=y
2300 # CONFIG_USB_ARCH_HAS_HCD is not set
2301 @@ -698,10 +760,6 @@
2302 #
2303 # NOTE: USB_STORAGE enables SCSI, and 'SCSI disk support'
2304 #
2305 -
2306 -#
2307 -# USB Gadget Support
2308 -#
2309 CONFIG_USB_GADGET=y
2310 # CONFIG_USB_GADGET_DEBUG is not set
2311 # CONFIG_USB_GADGET_DEBUG_FILES is not set
2312 @@ -729,27 +787,31 @@
2313 # CONFIG_USB_FILE_STORAGE_TEST is not set
2314 CONFIG_USB_G_SERIAL=m
2315 # CONFIG_USB_MIDI_GADGET is not set
2316 -CONFIG_MMC=m
2317 +# CONFIG_USB_G_PRINTER is not set
2318 +CONFIG_MMC=y
2319 # CONFIG_MMC_DEBUG is not set
2320 # CONFIG_MMC_UNSAFE_RESUME is not set
2321
2322 #
2323 # MMC/SD Card Drivers
2324 #
2325 -CONFIG_MMC_BLOCK=m
2326 +CONFIG_MMC_BLOCK=y
2327 # CONFIG_MMC_BLOCK_BOUNCE is not set
2328 # CONFIG_SDIO_UART is not set
2329
2330 #
2331 # MMC/SD Host Controller Drivers
2332 #
2333 +CONFIG_MMC_ATMELMCI=y
2334 CONFIG_MMC_SPI=m
2335 +# CONFIG_MEMSTICK is not set
2336 CONFIG_NEW_LEDS=y
2337 CONFIG_LEDS_CLASS=y
2338
2339 #
2340 # LED drivers
2341 #
2342 +CONFIG_LEDS_ATMEL_PWM=m
2343 CONFIG_LEDS_GPIO=y
2344
2345 #
2346 @@ -786,19 +848,22 @@
2347 # CONFIG_RTC_DRV_PCF8563 is not set
2348 # CONFIG_RTC_DRV_PCF8583 is not set
2349 # CONFIG_RTC_DRV_M41T80 is not set
2350 +# CONFIG_RTC_DRV_S35390A is not set
2351
2352 #
2353 # SPI RTC drivers
2354 #
2355 -# CONFIG_RTC_DRV_RS5C348 is not set
2356 # CONFIG_RTC_DRV_MAX6902 is not set
2357 +# CONFIG_RTC_DRV_R9701 is not set
2358 +# CONFIG_RTC_DRV_RS5C348 is not set
2359
2360 #
2361 # Platform RTC drivers
2362 #
2363 +# CONFIG_RTC_DRV_DS1511 is not set
2364 # CONFIG_RTC_DRV_DS1553 is not set
2365 -# CONFIG_RTC_DRV_STK17TA8 is not set
2366 # CONFIG_RTC_DRV_DS1742 is not set
2367 +# CONFIG_RTC_DRV_STK17TA8 is not set
2368 # CONFIG_RTC_DRV_M48T86 is not set
2369 # CONFIG_RTC_DRV_M48T59 is not set
2370 # CONFIG_RTC_DRV_V3020 is not set
2371 @@ -816,13 +881,13 @@
2372 #
2373 # File systems
2374 #
2375 -CONFIG_EXT2_FS=m
2376 +CONFIG_EXT2_FS=y
2377 # CONFIG_EXT2_FS_XATTR is not set
2378 # CONFIG_EXT2_FS_XIP is not set
2379 -CONFIG_EXT3_FS=m
2380 +CONFIG_EXT3_FS=y
2381 # CONFIG_EXT3_FS_XATTR is not set
2382 # CONFIG_EXT4DEV_FS is not set
2383 -CONFIG_JBD=m
2384 +CONFIG_JBD=y
2385 # CONFIG_JBD_DEBUG is not set
2386 # CONFIG_REISERFS_FS is not set
2387 # CONFIG_JFS_FS is not set
2388 @@ -830,12 +895,10 @@
2389 # CONFIG_XFS_FS is not set
2390 # CONFIG_GFS2_FS is not set
2391 # CONFIG_OCFS2_FS is not set
2392 -# CONFIG_MINIX_FS is not set
2393 -# CONFIG_ROMFS_FS is not set
2394 +# CONFIG_DNOTIFY is not set
2395 CONFIG_INOTIFY=y
2396 CONFIG_INOTIFY_USER=y
2397 # CONFIG_QUOTA is not set
2398 -# CONFIG_DNOTIFY is not set
2399 # CONFIG_AUTOFS_FS is not set
2400 # CONFIG_AUTOFS4_FS is not set
2401 CONFIG_FUSE_FS=m
2402 @@ -866,7 +929,7 @@
2403 CONFIG_TMPFS=y
2404 # CONFIG_TMPFS_POSIX_ACL is not set
2405 # CONFIG_HUGETLB_PAGE is not set
2406 -CONFIG_CONFIGFS_FS=m
2407 +CONFIG_CONFIGFS_FS=y
2408
2409 #
2410 # Miscellaneous filesystems
2411 @@ -891,8 +954,10 @@
2412 # CONFIG_JFFS2_RUBIN is not set
2413 # CONFIG_CRAMFS is not set
2414 # CONFIG_VXFS_FS is not set
2415 +# CONFIG_MINIX_FS is not set
2416 # CONFIG_HPFS_FS is not set
2417 # CONFIG_QNX4FS_FS is not set
2418 +# CONFIG_ROMFS_FS is not set
2419 # CONFIG_SYSV_FS is not set
2420 # CONFIG_UFS_FS is not set
2421 # CONFIG_NETWORK_FILESYSTEMS is not set
2422 @@ -943,11 +1008,6 @@
2423 # CONFIG_NLS_KOI8_U is not set
2424 CONFIG_NLS_UTF8=m
2425 # CONFIG_DLM is not set
2426 -CONFIG_INSTRUMENTATION=y
2427 -CONFIG_PROFILING=y
2428 -CONFIG_OPROFILE=m
2429 -CONFIG_KPROBES=y
2430 -# CONFIG_MARKERS is not set
2431
2432 #
2433 # Kernel hacking
2434 @@ -965,6 +1025,7 @@
2435 CONFIG_SCHED_DEBUG=y
2436 # CONFIG_SCHEDSTATS is not set
2437 # CONFIG_TIMER_STATS is not set
2438 +# CONFIG_SLUB_STATS is not set
2439 # CONFIG_DEBUG_RT_MUTEXES is not set
2440 # CONFIG_RT_MUTEX_TESTER is not set
2441 # CONFIG_DEBUG_SPINLOCK is not set
2442 @@ -981,9 +1042,10 @@
2443 # CONFIG_DEBUG_LIST is not set
2444 # CONFIG_DEBUG_SG is not set
2445 CONFIG_FRAME_POINTER=y
2446 -CONFIG_FORCED_INLINING=y
2447 # CONFIG_BOOT_PRINTK_DELAY is not set
2448 # CONFIG_RCU_TORTURE_TEST is not set
2449 +# CONFIG_KPROBES_SANITY_TEST is not set
2450 +# CONFIG_BACKTRACE_SELF_TEST is not set
2451 # CONFIG_LKDTM is not set
2452 # CONFIG_FAULT_INJECTION is not set
2453 # CONFIG_SAMPLES is not set
2454 @@ -1009,6 +1071,7 @@
2455 CONFIG_AUDIT_GENERIC=y
2456 CONFIG_ZLIB_INFLATE=y
2457 CONFIG_ZLIB_DEFLATE=y
2458 +CONFIG_GENERIC_ALLOCATOR=y
2459 CONFIG_PLIST=y
2460 CONFIG_HAS_IOMEM=y
2461 CONFIG_HAS_IOPORT=y
2462 diff --exclude=.git -urN linux-2.6.25.6/arch/avr32/configs/atstk1004_defconfig avr32-2.6/arch/avr32/configs/atstk1004_defconfig
2463 --- linux-2.6.25.6/arch/avr32/configs/atstk1004_defconfig 2008-06-09 20:27:19.000000000 +0200
2464 +++ avr32-2.6/arch/avr32/configs/atstk1004_defconfig 2008-06-12 15:09:38.719815350 +0200
2465 @@ -1,7 +1,7 @@
2466 #
2467 # Automatically generated make config: don't edit
2468 -# Linux kernel version: 2.6.24-rc7
2469 -# Wed Jan 9 23:04:20 2008
2470 +# Linux kernel version: 2.6.25.4
2471 +# Wed Jun 11 15:37:49 2008
2472 #
2473 CONFIG_AVR32=y
2474 CONFIG_GENERIC_GPIO=y
2475 @@ -13,10 +13,10 @@
2476 CONFIG_GENERIC_IRQ_PROBE=y
2477 CONFIG_RWSEM_GENERIC_SPINLOCK=y
2478 CONFIG_GENERIC_TIME=y
2479 +CONFIG_GENERIC_CLOCKEVENTS=y
2480 # CONFIG_RWSEM_XCHGADD_ALGORITHM is not set
2481 # CONFIG_ARCH_HAS_ILOG2_U32 is not set
2482 # CONFIG_ARCH_HAS_ILOG2_U64 is not set
2483 -CONFIG_ARCH_SUPPORTS_OPROFILE=y
2484 CONFIG_GENERIC_HWEIGHT=y
2485 CONFIG_GENERIC_CALIBRATE_DELAY=y
2486 CONFIG_GENERIC_BUG=y
2487 @@ -34,15 +34,15 @@
2488 # CONFIG_POSIX_MQUEUE is not set
2489 # CONFIG_BSD_PROCESS_ACCT is not set
2490 # CONFIG_TASKSTATS is not set
2491 -# CONFIG_USER_NS is not set
2492 -# CONFIG_PID_NS is not set
2493 # CONFIG_AUDIT is not set
2494 # CONFIG_IKCONFIG is not set
2495 CONFIG_LOG_BUF_SHIFT=14
2496 # CONFIG_CGROUPS is not set
2497 -# CONFIG_FAIR_GROUP_SCHED is not set
2498 +# CONFIG_GROUP_SCHED is not set
2499 CONFIG_SYSFS_DEPRECATED=y
2500 +CONFIG_SYSFS_DEPRECATED_V2=y
2501 # CONFIG_RELAY is not set
2502 +# CONFIG_NAMESPACES is not set
2503 # CONFIG_BLK_DEV_INITRD is not set
2504 CONFIG_CC_OPTIMIZE_FOR_SIZE=y
2505 CONFIG_SYSCTL=y
2506 @@ -54,24 +54,37 @@
2507 CONFIG_PRINTK=y
2508 CONFIG_BUG=y
2509 CONFIG_ELF_CORE=y
2510 +# CONFIG_COMPAT_BRK is not set
2511 # CONFIG_BASE_FULL is not set
2512 # CONFIG_FUTEX is not set
2513 # CONFIG_EPOLL is not set
2514 # CONFIG_SIGNALFD is not set
2515 +# CONFIG_TIMERFD is not set
2516 # CONFIG_EVENTFD is not set
2517 CONFIG_SHMEM=y
2518 CONFIG_VM_EVENT_COUNTERS=y
2519 # CONFIG_SLAB is not set
2520 # CONFIG_SLUB is not set
2521 CONFIG_SLOB=y
2522 +# CONFIG_PROFILING is not set
2523 +# CONFIG_MARKERS is not set
2524 +CONFIG_HAVE_OPROFILE=y
2525 +CONFIG_HAVE_KPROBES=y
2526 +# CONFIG_HAVE_KRETPROBES is not set
2527 +# CONFIG_PROC_PAGE_MONITOR is not set
2528 # CONFIG_TINY_SHMEM is not set
2529 CONFIG_BASE_SMALL=1
2530 # CONFIG_MODULES is not set
2531 # CONFIG_BLOCK is not set
2532 +CONFIG_CLASSIC_RCU=y
2533
2534 #
2535 # System Type and features
2536 #
2537 +# CONFIG_TICK_ONESHOT is not set
2538 +# CONFIG_NO_HZ is not set
2539 +# CONFIG_HIGH_RES_TIMERS is not set
2540 +CONFIG_GENERIC_CLOCKEVENTS_BUILD=y
2541 CONFIG_SUBARCH_AVR32B=y
2542 CONFIG_MMU=y
2543 CONFIG_PERFORMANCE_COUNTERS=y
2544 @@ -83,10 +96,14 @@
2545 # CONFIG_BOARD_ATSTK1002 is not set
2546 # CONFIG_BOARD_ATSTK1003 is not set
2547 CONFIG_BOARD_ATSTK1004=y
2548 +# CONFIG_BOARD_ATSTK1006 is not set
2549 # CONFIG_BOARD_ATSTK100X_CUSTOM is not set
2550 # CONFIG_BOARD_ATSTK100X_SPI1 is not set
2551 # CONFIG_BOARD_ATSTK1000_J2_LED is not set
2552 CONFIG_BOARD_ATSTK1000_EXTDAC=y
2553 +# CONFIG_BOARD_ATSTK100X_ENABLE_AC97 is not set
2554 +# CONFIG_BOARD_ATSTK1000_CF_HACKS is not set
2555 +# CONFIG_BOARD_ATSTK100X_ENABLE_PSIF is not set
2556 CONFIG_LOADER_U_BOOT=y
2557
2558 #
2559 @@ -95,6 +112,7 @@
2560 # CONFIG_AP700X_32_BIT_SMC is not set
2561 CONFIG_AP700X_16_BIT_SMC=y
2562 # CONFIG_AP700X_8_BIT_SMC is not set
2563 +# CONFIG_GPIO_DEV is not set
2564 CONFIG_LOAD_ADDRESS=0x10000000
2565 CONFIG_ENTRY_ADDRESS=0x90000000
2566 CONFIG_PHYS_OFFSET=0x10000000
2567 @@ -120,34 +138,26 @@
2568 CONFIG_ZONE_DMA_FLAG=0
2569 CONFIG_VIRT_TO_BUS=y
2570 # CONFIG_OWNERSHIP_TRACE is not set
2571 +# CONFIG_NMI_DEBUGGING is not set
2572 +CONFIG_DW_DMAC=y
2573 # CONFIG_HZ_100 is not set
2574 CONFIG_HZ_250=y
2575 # CONFIG_HZ_300 is not set
2576 # CONFIG_HZ_1000 is not set
2577 CONFIG_HZ=250
2578 +# CONFIG_SCHED_HRTICK is not set
2579 CONFIG_CMDLINE=""
2580
2581 #
2582 # Power management options
2583 #
2584 +CONFIG_ARCH_SUSPEND_POSSIBLE=y
2585 +# CONFIG_PM is not set
2586
2587 #
2588 # CPU Frequency scaling
2589 #
2590 -CONFIG_CPU_FREQ=y
2591 -CONFIG_CPU_FREQ_TABLE=y
2592 -# CONFIG_CPU_FREQ_DEBUG is not set
2593 -# CONFIG_CPU_FREQ_STAT is not set
2594 -CONFIG_CPU_FREQ_DEFAULT_GOV_PERFORMANCE=y
2595 -# CONFIG_CPU_FREQ_DEFAULT_GOV_USERSPACE is not set
2596 -# CONFIG_CPU_FREQ_DEFAULT_GOV_ONDEMAND is not set
2597 -# CONFIG_CPU_FREQ_DEFAULT_GOV_CONSERVATIVE is not set
2598 -CONFIG_CPU_FREQ_GOV_PERFORMANCE=y
2599 -# CONFIG_CPU_FREQ_GOV_POWERSAVE is not set
2600 -CONFIG_CPU_FREQ_GOV_USERSPACE=y
2601 -CONFIG_CPU_FREQ_GOV_ONDEMAND=y
2602 -# CONFIG_CPU_FREQ_GOV_CONSERVATIVE is not set
2603 -CONFIG_CPU_FREQ_AT32AP=y
2604 +# CONFIG_CPU_FREQ is not set
2605
2606 #
2607 # Bus options
2608 @@ -222,6 +232,7 @@
2609 #
2610 # CONFIG_NET_PKTGEN is not set
2611 # CONFIG_HAMRADIO is not set
2612 +# CONFIG_CAN is not set
2613 # CONFIG_IRDA is not set
2614 # CONFIG_BT is not set
2615 # CONFIG_AF_RXRPC is not set
2616 @@ -321,6 +332,7 @@
2617 # CONFIG_MTD_UBI is not set
2618 # CONFIG_PARPORT is not set
2619 # CONFIG_MISC_DEVICES is not set
2620 +# CONFIG_HAVE_IDE is not set
2621
2622 #
2623 # SCSI device support
2624 @@ -358,6 +370,7 @@
2625 #
2626 CONFIG_SERIAL_ATMEL=y
2627 CONFIG_SERIAL_ATMEL_CONSOLE=y
2628 +# CONFIG_SERIAL_ATMEL_PDC is not set
2629 # CONFIG_SERIAL_ATMEL_TTYAT is not set
2630 CONFIG_SERIAL_CORE=y
2631 CONFIG_SERIAL_CORE_CONSOLE=y
2632 @@ -365,8 +378,6 @@
2633 # CONFIG_LEGACY_PTYS is not set
2634 # CONFIG_IPMI_HANDLER is not set
2635 # CONFIG_HW_RANDOM is not set
2636 -# CONFIG_RTC is not set
2637 -# CONFIG_GEN_RTC is not set
2638 # CONFIG_R3964 is not set
2639 # CONFIG_TCG_TPM is not set
2640 # CONFIG_I2C is not set
2641 @@ -389,9 +400,24 @@
2642 # CONFIG_SPI_AT25 is not set
2643 # CONFIG_SPI_SPIDEV is not set
2644 # CONFIG_SPI_TLE62X0 is not set
2645 +CONFIG_HAVE_GPIO_LIB=y
2646 +
2647 +#
2648 +# GPIO Support
2649 +#
2650 +
2651 +#
2652 +# I2C GPIO expanders:
2653 +#
2654 +
2655 +#
2656 +# SPI GPIO expanders:
2657 +#
2658 +# CONFIG_GPIO_MCP23S08 is not set
2659 # CONFIG_W1 is not set
2660 # CONFIG_POWER_SUPPLY is not set
2661 # CONFIG_HWMON is not set
2662 +# CONFIG_THERMAL is not set
2663 CONFIG_WATCHDOG=y
2664 # CONFIG_WATCHDOG_NOWAYOUT is not set
2665
2666 @@ -471,10 +497,6 @@
2667 #
2668 # NOTE: USB_STORAGE enables SCSI, and 'SCSI disk support'
2669 #
2670 -
2671 -#
2672 -# USB Gadget Support
2673 -#
2674 CONFIG_USB_GADGET=y
2675 # CONFIG_USB_GADGET_DEBUG_FILES is not set
2676 CONFIG_USB_GADGET_SELECTED=y
2677 @@ -499,7 +521,9 @@
2678 # CONFIG_USB_FILE_STORAGE is not set
2679 # CONFIG_USB_G_SERIAL is not set
2680 # CONFIG_USB_MIDI_GADGET is not set
2681 +# CONFIG_USB_G_PRINTER is not set
2682 # CONFIG_MMC is not set
2683 +# CONFIG_MEMSTICK is not set
2684 # CONFIG_NEW_LEDS is not set
2685 CONFIG_RTC_LIB=y
2686 CONFIG_RTC_CLASS=y
2687 @@ -519,15 +543,17 @@
2688 #
2689 # SPI RTC drivers
2690 #
2691 -# CONFIG_RTC_DRV_RS5C348 is not set
2692 # CONFIG_RTC_DRV_MAX6902 is not set
2693 +# CONFIG_RTC_DRV_R9701 is not set
2694 +# CONFIG_RTC_DRV_RS5C348 is not set
2695
2696 #
2697 # Platform RTC drivers
2698 #
2699 +# CONFIG_RTC_DRV_DS1511 is not set
2700 # CONFIG_RTC_DRV_DS1553 is not set
2701 -# CONFIG_RTC_DRV_STK17TA8 is not set
2702 # CONFIG_RTC_DRV_DS1742 is not set
2703 +# CONFIG_RTC_DRV_STK17TA8 is not set
2704 # CONFIG_RTC_DRV_M48T86 is not set
2705 # CONFIG_RTC_DRV_M48T59 is not set
2706 # CONFIG_RTC_DRV_V3020 is not set
2707 @@ -545,9 +571,9 @@
2708 #
2709 # File systems
2710 #
2711 +# CONFIG_DNOTIFY is not set
2712 # CONFIG_INOTIFY is not set
2713 # CONFIG_QUOTA is not set
2714 -# CONFIG_DNOTIFY is not set
2715 # CONFIG_AUTOFS_FS is not set
2716 # CONFIG_AUTOFS4_FS is not set
2717 # CONFIG_FUSE_FS is not set
2718 @@ -580,7 +606,6 @@
2719 # CONFIG_NETWORK_FILESYSTEMS is not set
2720 # CONFIG_NLS is not set
2721 # CONFIG_DLM is not set
2722 -# CONFIG_INSTRUMENTATION is not set
2723
2724 #
2725 # Kernel hacking
2726 @@ -616,6 +641,7 @@
2727 # CONFIG_LIBCRC32C is not set
2728 CONFIG_ZLIB_INFLATE=y
2729 CONFIG_ZLIB_DEFLATE=y
2730 +CONFIG_GENERIC_ALLOCATOR=y
2731 CONFIG_HAS_IOMEM=y
2732 CONFIG_HAS_IOPORT=y
2733 CONFIG_HAS_DMA=y
2734 diff --exclude=.git -urN linux-2.6.25.6/arch/avr32/configs/atstk1006_defconfig avr32-2.6/arch/avr32/configs/atstk1006_defconfig
2735 --- linux-2.6.25.6/arch/avr32/configs/atstk1006_defconfig 1970-01-01 01:00:00.000000000 +0100
2736 +++ avr32-2.6/arch/avr32/configs/atstk1006_defconfig 2008-06-12 15:09:38.719815350 +0200
2737 @@ -0,0 +1,1235 @@
2738 +#
2739 +# Automatically generated make config: don't edit
2740 +# Linux kernel version: 2.6.25.4
2741 +# Wed Jun 11 15:40:45 2008
2742 +#
2743 +CONFIG_AVR32=y
2744 +CONFIG_GENERIC_GPIO=y
2745 +CONFIG_GENERIC_HARDIRQS=y
2746 +CONFIG_STACKTRACE_SUPPORT=y
2747 +CONFIG_LOCKDEP_SUPPORT=y
2748 +CONFIG_TRACE_IRQFLAGS_SUPPORT=y
2749 +CONFIG_HARDIRQS_SW_RESEND=y
2750 +CONFIG_GENERIC_IRQ_PROBE=y
2751 +CONFIG_RWSEM_GENERIC_SPINLOCK=y
2752 +CONFIG_GENERIC_TIME=y
2753 +CONFIG_GENERIC_CLOCKEVENTS=y
2754 +# CONFIG_RWSEM_XCHGADD_ALGORITHM is not set
2755 +# CONFIG_ARCH_HAS_ILOG2_U32 is not set
2756 +# CONFIG_ARCH_HAS_ILOG2_U64 is not set
2757 +CONFIG_GENERIC_HWEIGHT=y
2758 +CONFIG_GENERIC_CALIBRATE_DELAY=y
2759 +CONFIG_GENERIC_BUG=y
2760 +CONFIG_DEFCONFIG_LIST="/lib/modules/$UNAME_RELEASE/.config"
2761 +
2762 +#
2763 +# General setup
2764 +#
2765 +CONFIG_EXPERIMENTAL=y
2766 +CONFIG_BROKEN_ON_SMP=y
2767 +CONFIG_INIT_ENV_ARG_LIMIT=32
2768 +CONFIG_LOCALVERSION=""
2769 +# CONFIG_LOCALVERSION_AUTO is not set
2770 +CONFIG_SWAP=y
2771 +CONFIG_SYSVIPC=y
2772 +CONFIG_SYSVIPC_SYSCTL=y
2773 +CONFIG_POSIX_MQUEUE=y
2774 +# CONFIG_BSD_PROCESS_ACCT is not set
2775 +# CONFIG_TASKSTATS is not set
2776 +# CONFIG_AUDIT is not set
2777 +# CONFIG_IKCONFIG is not set
2778 +CONFIG_LOG_BUF_SHIFT=14
2779 +# CONFIG_CGROUPS is not set
2780 +# CONFIG_GROUP_SCHED is not set
2781 +CONFIG_SYSFS_DEPRECATED=y
2782 +CONFIG_SYSFS_DEPRECATED_V2=y
2783 +CONFIG_RELAY=y
2784 +# CONFIG_NAMESPACES is not set
2785 +CONFIG_BLK_DEV_INITRD=y
2786 +CONFIG_INITRAMFS_SOURCE=""
2787 +CONFIG_CC_OPTIMIZE_FOR_SIZE=y
2788 +CONFIG_SYSCTL=y
2789 +CONFIG_EMBEDDED=y
2790 +# CONFIG_SYSCTL_SYSCALL is not set
2791 +CONFIG_KALLSYMS=y
2792 +# CONFIG_KALLSYMS_ALL is not set
2793 +# CONFIG_KALLSYMS_EXTRA_PASS is not set
2794 +CONFIG_HOTPLUG=y
2795 +CONFIG_PRINTK=y
2796 +CONFIG_BUG=y
2797 +CONFIG_ELF_CORE=y
2798 +# CONFIG_COMPAT_BRK is not set
2799 +# CONFIG_BASE_FULL is not set
2800 +CONFIG_FUTEX=y
2801 +CONFIG_ANON_INODES=y
2802 +CONFIG_EPOLL=y
2803 +CONFIG_SIGNALFD=y
2804 +CONFIG_TIMERFD=y
2805 +CONFIG_EVENTFD=y
2806 +CONFIG_SHMEM=y
2807 +CONFIG_VM_EVENT_COUNTERS=y
2808 +CONFIG_SLUB_DEBUG=y
2809 +# CONFIG_SLAB is not set
2810 +CONFIG_SLUB=y
2811 +# CONFIG_SLOB is not set
2812 +CONFIG_PROFILING=y
2813 +# CONFIG_MARKERS is not set
2814 +CONFIG_OPROFILE=m
2815 +CONFIG_HAVE_OPROFILE=y
2816 +CONFIG_KPROBES=y
2817 +CONFIG_HAVE_KPROBES=y
2818 +# CONFIG_HAVE_KRETPROBES is not set
2819 +CONFIG_PROC_PAGE_MONITOR=y
2820 +CONFIG_SLABINFO=y
2821 +CONFIG_RT_MUTEXES=y
2822 +# CONFIG_TINY_SHMEM is not set
2823 +CONFIG_BASE_SMALL=1
2824 +CONFIG_MODULES=y
2825 +CONFIG_MODULE_UNLOAD=y
2826 +# CONFIG_MODULE_FORCE_UNLOAD is not set
2827 +# CONFIG_MODVERSIONS is not set
2828 +# CONFIG_MODULE_SRCVERSION_ALL is not set
2829 +# CONFIG_KMOD is not set
2830 +CONFIG_BLOCK=y
2831 +# CONFIG_LBD is not set
2832 +# CONFIG_BLK_DEV_IO_TRACE is not set
2833 +# CONFIG_LSF is not set
2834 +# CONFIG_BLK_DEV_BSG is not set
2835 +
2836 +#
2837 +# IO Schedulers
2838 +#
2839 +CONFIG_IOSCHED_NOOP=y
2840 +# CONFIG_IOSCHED_AS is not set
2841 +# CONFIG_IOSCHED_DEADLINE is not set
2842 +CONFIG_IOSCHED_CFQ=y
2843 +# CONFIG_DEFAULT_AS is not set
2844 +# CONFIG_DEFAULT_DEADLINE is not set
2845 +CONFIG_DEFAULT_CFQ=y
2846 +# CONFIG_DEFAULT_NOOP is not set
2847 +CONFIG_DEFAULT_IOSCHED="cfq"
2848 +CONFIG_CLASSIC_RCU=y
2849 +
2850 +#
2851 +# System Type and features
2852 +#
2853 +CONFIG_TICK_ONESHOT=y
2854 +CONFIG_NO_HZ=y
2855 +CONFIG_HIGH_RES_TIMERS=y
2856 +CONFIG_GENERIC_CLOCKEVENTS_BUILD=y
2857 +CONFIG_SUBARCH_AVR32B=y
2858 +CONFIG_MMU=y
2859 +CONFIG_PERFORMANCE_COUNTERS=y
2860 +CONFIG_PLATFORM_AT32AP=y
2861 +CONFIG_CPU_AT32AP700X=y
2862 +CONFIG_CPU_AT32AP7000=y
2863 +CONFIG_BOARD_ATSTK1000=y
2864 +# CONFIG_BOARD_ATNGW100 is not set
2865 +# CONFIG_BOARD_ATSTK1002 is not set
2866 +# CONFIG_BOARD_ATSTK1003 is not set
2867 +# CONFIG_BOARD_ATSTK1004 is not set
2868 +CONFIG_BOARD_ATSTK1006=y
2869 +# CONFIG_BOARD_ATSTK100X_CUSTOM is not set
2870 +# CONFIG_BOARD_ATSTK100X_SPI1 is not set
2871 +# CONFIG_BOARD_ATSTK1000_J2_LED is not set
2872 +# CONFIG_BOARD_ATSTK1000_J2_LED8 is not set
2873 +# CONFIG_BOARD_ATSTK1000_J2_RGB is not set
2874 +CONFIG_BOARD_ATSTK1000_EXTDAC=y
2875 +# CONFIG_BOARD_ATSTK100X_ENABLE_AC97 is not set
2876 +# CONFIG_BOARD_ATSTK1000_CF_HACKS is not set
2877 +# CONFIG_BOARD_ATSTK100X_ENABLE_PSIF is not set
2878 +CONFIG_LOADER_U_BOOT=y
2879 +
2880 +#
2881 +# Atmel AVR32 AP options
2882 +#
2883 +# CONFIG_AP700X_32_BIT_SMC is not set
2884 +CONFIG_AP700X_16_BIT_SMC=y
2885 +# CONFIG_AP700X_8_BIT_SMC is not set
2886 +CONFIG_GPIO_DEV=y
2887 +CONFIG_LOAD_ADDRESS=0x10000000
2888 +CONFIG_ENTRY_ADDRESS=0x90000000
2889 +CONFIG_PHYS_OFFSET=0x10000000
2890 +CONFIG_PREEMPT_NONE=y
2891 +# CONFIG_PREEMPT_VOLUNTARY is not set
2892 +# CONFIG_PREEMPT is not set
2893 +# CONFIG_HAVE_ARCH_BOOTMEM_NODE is not set
2894 +# CONFIG_ARCH_HAVE_MEMORY_PRESENT is not set
2895 +# CONFIG_NEED_NODE_MEMMAP_SIZE is not set
2896 +CONFIG_ARCH_FLATMEM_ENABLE=y
2897 +# CONFIG_ARCH_DISCONTIGMEM_ENABLE is not set
2898 +# CONFIG_ARCH_SPARSEMEM_ENABLE is not set
2899 +CONFIG_SELECT_MEMORY_MODEL=y
2900 +CONFIG_FLATMEM_MANUAL=y
2901 +# CONFIG_DISCONTIGMEM_MANUAL is not set
2902 +# CONFIG_SPARSEMEM_MANUAL is not set
2903 +CONFIG_FLATMEM=y
2904 +CONFIG_FLAT_NODE_MEM_MAP=y
2905 +# CONFIG_SPARSEMEM_STATIC is not set
2906 +# CONFIG_SPARSEMEM_VMEMMAP_ENABLE is not set
2907 +CONFIG_SPLIT_PTLOCK_CPUS=4
2908 +# CONFIG_RESOURCES_64BIT is not set
2909 +CONFIG_ZONE_DMA_FLAG=0
2910 +CONFIG_VIRT_TO_BUS=y
2911 +# CONFIG_OWNERSHIP_TRACE is not set
2912 +CONFIG_NMI_DEBUGGING=y
2913 +CONFIG_DW_DMAC=y
2914 +# CONFIG_HZ_100 is not set
2915 +CONFIG_HZ_250=y
2916 +# CONFIG_HZ_300 is not set
2917 +# CONFIG_HZ_1000 is not set
2918 +CONFIG_HZ=250
2919 +# CONFIG_SCHED_HRTICK is not set
2920 +CONFIG_CMDLINE=""
2921 +
2922 +#
2923 +# Power management options
2924 +#
2925 +CONFIG_ARCH_SUSPEND_POSSIBLE=y
2926 +CONFIG_PM=y
2927 +# CONFIG_PM_LEGACY is not set
2928 +# CONFIG_PM_DEBUG is not set
2929 +CONFIG_PM_SLEEP=y
2930 +CONFIG_SUSPEND=y
2931 +CONFIG_SUSPEND_FREEZER=y
2932 +
2933 +#
2934 +# CPU Frequency scaling
2935 +#
2936 +CONFIG_CPU_FREQ=y
2937 +CONFIG_CPU_FREQ_TABLE=y
2938 +# CONFIG_CPU_FREQ_DEBUG is not set
2939 +# CONFIG_CPU_FREQ_STAT is not set
2940 +# CONFIG_CPU_FREQ_DEFAULT_GOV_PERFORMANCE is not set
2941 +# CONFIG_CPU_FREQ_DEFAULT_GOV_USERSPACE is not set
2942 +CONFIG_CPU_FREQ_DEFAULT_GOV_ONDEMAND=y
2943 +# CONFIG_CPU_FREQ_DEFAULT_GOV_CONSERVATIVE is not set
2944 +CONFIG_CPU_FREQ_GOV_PERFORMANCE=y
2945 +# CONFIG_CPU_FREQ_GOV_POWERSAVE is not set
2946 +CONFIG_CPU_FREQ_GOV_USERSPACE=y
2947 +CONFIG_CPU_FREQ_GOV_ONDEMAND=y
2948 +# CONFIG_CPU_FREQ_GOV_CONSERVATIVE is not set
2949 +CONFIG_CPU_FREQ_AT32AP=y
2950 +
2951 +#
2952 +# Bus options
2953 +#
2954 +# CONFIG_ARCH_SUPPORTS_MSI is not set
2955 +# CONFIG_PCCARD is not set
2956 +
2957 +#
2958 +# Executable file formats
2959 +#
2960 +CONFIG_BINFMT_ELF=y
2961 +# CONFIG_BINFMT_MISC is not set
2962 +
2963 +#
2964 +# Networking
2965 +#
2966 +CONFIG_NET=y
2967 +
2968 +#
2969 +# Networking options
2970 +#
2971 +CONFIG_PACKET=y
2972 +CONFIG_PACKET_MMAP=y
2973 +CONFIG_UNIX=y
2974 +CONFIG_XFRM=y
2975 +CONFIG_XFRM_USER=m
2976 +# CONFIG_XFRM_SUB_POLICY is not set
2977 +# CONFIG_XFRM_MIGRATE is not set
2978 +# CONFIG_XFRM_STATISTICS is not set
2979 +CONFIG_NET_KEY=m
2980 +# CONFIG_NET_KEY_MIGRATE is not set
2981 +CONFIG_INET=y
2982 +# CONFIG_IP_MULTICAST is not set
2983 +# CONFIG_IP_ADVANCED_ROUTER is not set
2984 +CONFIG_IP_FIB_HASH=y
2985 +CONFIG_IP_PNP=y
2986 +CONFIG_IP_PNP_DHCP=y
2987 +# CONFIG_IP_PNP_BOOTP is not set
2988 +# CONFIG_IP_PNP_RARP is not set
2989 +CONFIG_NET_IPIP=m
2990 +CONFIG_NET_IPGRE=m
2991 +# CONFIG_ARPD is not set
2992 +# CONFIG_SYN_COOKIES is not set
2993 +CONFIG_INET_AH=m
2994 +CONFIG_INET_ESP=m
2995 +# CONFIG_INET_IPCOMP is not set
2996 +# CONFIG_INET_XFRM_TUNNEL is not set
2997 +CONFIG_INET_TUNNEL=m
2998 +CONFIG_INET_XFRM_MODE_TRANSPORT=m
2999 +CONFIG_INET_XFRM_MODE_TUNNEL=m
3000 +CONFIG_INET_XFRM_MODE_BEET=m
3001 +# CONFIG_INET_LRO is not set
3002 +CONFIG_INET_DIAG=y
3003 +CONFIG_INET_TCP_DIAG=y
3004 +# CONFIG_TCP_CONG_ADVANCED is not set
3005 +CONFIG_TCP_CONG_CUBIC=y
3006 +CONFIG_DEFAULT_TCP_CONG="cubic"
3007 +# CONFIG_TCP_MD5SIG is not set
3008 +CONFIG_IPV6=m
3009 +# CONFIG_IPV6_PRIVACY is not set
3010 +# CONFIG_IPV6_ROUTER_PREF is not set
3011 +# CONFIG_IPV6_OPTIMISTIC_DAD is not set
3012 +CONFIG_INET6_AH=m
3013 +CONFIG_INET6_ESP=m
3014 +CONFIG_INET6_IPCOMP=m
3015 +# CONFIG_IPV6_MIP6 is not set
3016 +CONFIG_INET6_XFRM_TUNNEL=m
3017 +CONFIG_INET6_TUNNEL=m
3018 +CONFIG_INET6_XFRM_MODE_TRANSPORT=m
3019 +CONFIG_INET6_XFRM_MODE_TUNNEL=m
3020 +CONFIG_INET6_XFRM_MODE_BEET=m
3021 +# CONFIG_INET6_XFRM_MODE_ROUTEOPTIMIZATION is not set
3022 +CONFIG_IPV6_SIT=m
3023 +CONFIG_IPV6_TUNNEL=m
3024 +# CONFIG_IPV6_MULTIPLE_TABLES is not set
3025 +# CONFIG_NETWORK_SECMARK is not set
3026 +# CONFIG_NETFILTER is not set
3027 +# CONFIG_IP_DCCP is not set
3028 +# CONFIG_IP_SCTP is not set
3029 +# CONFIG_TIPC is not set
3030 +# CONFIG_ATM is not set
3031 +CONFIG_BRIDGE=m
3032 +# CONFIG_VLAN_8021Q is not set
3033 +# CONFIG_DECNET is not set
3034 +CONFIG_LLC=m
3035 +# CONFIG_LLC2 is not set
3036 +# CONFIG_IPX is not set
3037 +# CONFIG_ATALK is not set
3038 +# CONFIG_X25 is not set
3039 +# CONFIG_LAPB is not set
3040 +# CONFIG_ECONET is not set
3041 +# CONFIG_WAN_ROUTER is not set
3042 +# CONFIG_NET_SCHED is not set
3043 +
3044 +#
3045 +# Network testing
3046 +#
3047 +# CONFIG_NET_PKTGEN is not set
3048 +# CONFIG_NET_TCPPROBE is not set
3049 +# CONFIG_HAMRADIO is not set
3050 +# CONFIG_CAN is not set
3051 +# CONFIG_IRDA is not set
3052 +# CONFIG_BT is not set
3053 +# CONFIG_AF_RXRPC is not set
3054 +
3055 +#
3056 +# Wireless
3057 +#
3058 +# CONFIG_CFG80211 is not set
3059 +# CONFIG_WIRELESS_EXT is not set
3060 +# CONFIG_MAC80211 is not set
3061 +# CONFIG_IEEE80211 is not set
3062 +# CONFIG_RFKILL is not set
3063 +# CONFIG_NET_9P is not set
3064 +
3065 +#
3066 +# Device Drivers
3067 +#
3068 +
3069 +#
3070 +# Generic Driver Options
3071 +#
3072 +CONFIG_UEVENT_HELPER_PATH="/sbin/hotplug"
3073 +CONFIG_STANDALONE=y
3074 +# CONFIG_PREVENT_FIRMWARE_BUILD is not set
3075 +# CONFIG_FW_LOADER is not set
3076 +# CONFIG_DEBUG_DRIVER is not set
3077 +# CONFIG_DEBUG_DEVRES is not set
3078 +# CONFIG_SYS_HYPERVISOR is not set
3079 +# CONFIG_CONNECTOR is not set
3080 +CONFIG_MTD=y
3081 +# CONFIG_MTD_DEBUG is not set
3082 +# CONFIG_MTD_CONCAT is not set
3083 +CONFIG_MTD_PARTITIONS=y
3084 +# CONFIG_MTD_REDBOOT_PARTS is not set
3085 +CONFIG_MTD_CMDLINE_PARTS=y
3086 +
3087 +#
3088 +# User Modules And Translation Layers
3089 +#
3090 +CONFIG_MTD_CHAR=y
3091 +CONFIG_MTD_BLKDEVS=y
3092 +CONFIG_MTD_BLOCK=y
3093 +# CONFIG_FTL is not set
3094 +# CONFIG_NFTL is not set
3095 +# CONFIG_INFTL is not set
3096 +# CONFIG_RFD_FTL is not set
3097 +# CONFIG_SSFDC is not set
3098 +# CONFIG_MTD_OOPS is not set
3099 +
3100 +#
3101 +# RAM/ROM/Flash chip drivers
3102 +#
3103 +CONFIG_MTD_CFI=y
3104 +# CONFIG_MTD_JEDECPROBE is not set
3105 +CONFIG_MTD_GEN_PROBE=y
3106 +# CONFIG_MTD_CFI_ADV_OPTIONS is not set
3107 +CONFIG_MTD_MAP_BANK_WIDTH_1=y
3108 +CONFIG_MTD_MAP_BANK_WIDTH_2=y
3109 +CONFIG_MTD_MAP_BANK_WIDTH_4=y
3110 +# CONFIG_MTD_MAP_BANK_WIDTH_8 is not set
3111 +# CONFIG_MTD_MAP_BANK_WIDTH_16 is not set
3112 +# CONFIG_MTD_MAP_BANK_WIDTH_32 is not set
3113 +CONFIG_MTD_CFI_I1=y
3114 +CONFIG_MTD_CFI_I2=y
3115 +# CONFIG_MTD_CFI_I4 is not set
3116 +# CONFIG_MTD_CFI_I8 is not set
3117 +# CONFIG_MTD_CFI_INTELEXT is not set
3118 +CONFIG_MTD_CFI_AMDSTD=y
3119 +# CONFIG_MTD_CFI_STAA is not set
3120 +CONFIG_MTD_CFI_UTIL=y
3121 +# CONFIG_MTD_RAM is not set
3122 +# CONFIG_MTD_ROM is not set
3123 +# CONFIG_MTD_ABSENT is not set
3124 +
3125 +#
3126 +# Mapping drivers for chip access
3127 +#
3128 +# CONFIG_MTD_COMPLEX_MAPPINGS is not set
3129 +CONFIG_MTD_PHYSMAP=y
3130 +CONFIG_MTD_PHYSMAP_START=0x8000000
3131 +CONFIG_MTD_PHYSMAP_LEN=0x0
3132 +CONFIG_MTD_PHYSMAP_BANKWIDTH=2
3133 +# CONFIG_MTD_PLATRAM is not set
3134 +
3135 +#
3136 +# Self-contained MTD device drivers
3137 +#
3138 +CONFIG_MTD_DATAFLASH=m
3139 +CONFIG_MTD_M25P80=m
3140 +# CONFIG_MTD_SLRAM is not set
3141 +# CONFIG_MTD_PHRAM is not set
3142 +# CONFIG_MTD_MTDRAM is not set
3143 +# CONFIG_MTD_BLOCK2MTD is not set
3144 +
3145 +#
3146 +# Disk-On-Chip Device Drivers
3147 +#
3148 +# CONFIG_MTD_DOC2000 is not set
3149 +# CONFIG_MTD_DOC2001 is not set
3150 +# CONFIG_MTD_DOC2001PLUS is not set
3151 +CONFIG_MTD_NAND=y
3152 +# CONFIG_MTD_NAND_VERIFY_WRITE is not set
3153 +# CONFIG_MTD_NAND_ECC_SMC is not set
3154 +# CONFIG_MTD_NAND_MUSEUM_IDS is not set
3155 +CONFIG_MTD_NAND_IDS=y
3156 +# CONFIG_MTD_NAND_DISKONCHIP is not set
3157 +CONFIG_MTD_NAND_ATMEL=y
3158 +CONFIG_MTD_NAND_ATMEL_ECC_HW=y
3159 +# CONFIG_MTD_NAND_ATMEL_ECC_SOFT is not set
3160 +# CONFIG_MTD_NAND_ATMEL_ECC_NONE is not set
3161 +# CONFIG_MTD_NAND_NANDSIM is not set
3162 +# CONFIG_MTD_NAND_PLATFORM is not set
3163 +# CONFIG_MTD_ONENAND is not set
3164 +
3165 +#
3166 +# UBI - Unsorted block images
3167 +#
3168 +CONFIG_MTD_UBI=m
3169 +CONFIG_MTD_UBI_WL_THRESHOLD=4096
3170 +CONFIG_MTD_UBI_BEB_RESERVE=1
3171 +CONFIG_MTD_UBI_GLUEBI=y
3172 +
3173 +#
3174 +# UBI debugging options
3175 +#
3176 +# CONFIG_MTD_UBI_DEBUG is not set
3177 +# CONFIG_PARPORT is not set
3178 +CONFIG_BLK_DEV=y
3179 +# CONFIG_BLK_DEV_COW_COMMON is not set
3180 +CONFIG_BLK_DEV_LOOP=m
3181 +# CONFIG_BLK_DEV_CRYPTOLOOP is not set
3182 +CONFIG_BLK_DEV_NBD=m
3183 +CONFIG_BLK_DEV_RAM=m
3184 +CONFIG_BLK_DEV_RAM_COUNT=16
3185 +CONFIG_BLK_DEV_RAM_SIZE=4096
3186 +# CONFIG_BLK_DEV_XIP is not set
3187 +# CONFIG_CDROM_PKTCDVD is not set
3188 +# CONFIG_ATA_OVER_ETH is not set
3189 +CONFIG_MISC_DEVICES=y
3190 +CONFIG_ATMEL_PWM=m
3191 +CONFIG_ATMEL_TCLIB=y
3192 +CONFIG_ATMEL_TCB_CLKSRC=y
3193 +CONFIG_ATMEL_TCB_CLKSRC_BLOCK=0
3194 +# CONFIG_EEPROM_93CX6 is not set
3195 +CONFIG_ATMEL_SSC=m
3196 +# CONFIG_ENCLOSURE_SERVICES is not set
3197 +# CONFIG_HAVE_IDE is not set
3198 +
3199 +#
3200 +# SCSI device support
3201 +#
3202 +# CONFIG_RAID_ATTRS is not set
3203 +CONFIG_SCSI=m
3204 +CONFIG_SCSI_DMA=y
3205 +# CONFIG_SCSI_TGT is not set
3206 +# CONFIG_SCSI_NETLINK is not set
3207 +# CONFIG_SCSI_PROC_FS is not set
3208 +
3209 +#
3210 +# SCSI support type (disk, tape, CD-ROM)
3211 +#
3212 +CONFIG_BLK_DEV_SD=m
3213 +# CONFIG_CHR_DEV_ST is not set
3214 +# CONFIG_CHR_DEV_OSST is not set
3215 +CONFIG_BLK_DEV_SR=m
3216 +# CONFIG_BLK_DEV_SR_VENDOR is not set
3217 +# CONFIG_CHR_DEV_SG is not set
3218 +# CONFIG_CHR_DEV_SCH is not set
3219 +
3220 +#
3221 +# Some SCSI devices (e.g. CD jukebox) support multiple LUNs
3222 +#
3223 +# CONFIG_SCSI_MULTI_LUN is not set
3224 +# CONFIG_SCSI_CONSTANTS is not set
3225 +# CONFIG_SCSI_LOGGING is not set
3226 +# CONFIG_SCSI_SCAN_ASYNC is not set
3227 +CONFIG_SCSI_WAIT_SCAN=m
3228 +
3229 +#
3230 +# SCSI Transports
3231 +#
3232 +# CONFIG_SCSI_SPI_ATTRS is not set
3233 +# CONFIG_SCSI_FC_ATTRS is not set
3234 +# CONFIG_SCSI_ISCSI_ATTRS is not set
3235 +# CONFIG_SCSI_SAS_LIBSAS is not set
3236 +# CONFIG_SCSI_SRP_ATTRS is not set
3237 +# CONFIG_SCSI_LOWLEVEL is not set
3238 +CONFIG_ATA=m
3239 +# CONFIG_ATA_NONSTANDARD is not set
3240 +# CONFIG_SATA_MV is not set
3241 +CONFIG_PATA_AT32=m
3242 +# CONFIG_PATA_PLATFORM is not set
3243 +# CONFIG_MD is not set
3244 +CONFIG_NETDEVICES=y
3245 +# CONFIG_NETDEVICES_MULTIQUEUE is not set
3246 +# CONFIG_DUMMY is not set
3247 +# CONFIG_BONDING is not set
3248 +# CONFIG_MACVLAN is not set
3249 +# CONFIG_EQUALIZER is not set
3250 +CONFIG_TUN=m
3251 +# CONFIG_VETH is not set
3252 +CONFIG_PHYLIB=y
3253 +
3254 +#
3255 +# MII PHY device drivers
3256 +#
3257 +# CONFIG_MARVELL_PHY is not set
3258 +# CONFIG_DAVICOM_PHY is not set
3259 +# CONFIG_QSEMI_PHY is not set
3260 +# CONFIG_LXT_PHY is not set
3261 +# CONFIG_CICADA_PHY is not set
3262 +# CONFIG_VITESSE_PHY is not set
3263 +# CONFIG_SMSC_PHY is not set
3264 +# CONFIG_BROADCOM_PHY is not set
3265 +# CONFIG_ICPLUS_PHY is not set
3266 +# CONFIG_REALTEK_PHY is not set
3267 +# CONFIG_FIXED_PHY is not set
3268 +# CONFIG_MDIO_BITBANG is not set
3269 +CONFIG_NET_ETHERNET=y
3270 +# CONFIG_MII is not set
3271 +CONFIG_MACB=y
3272 +# CONFIG_ENC28J60 is not set
3273 +# CONFIG_IBM_NEW_EMAC_ZMII is not set
3274 +# CONFIG_IBM_NEW_EMAC_RGMII is not set
3275 +# CONFIG_IBM_NEW_EMAC_TAH is not set
3276 +# CONFIG_IBM_NEW_EMAC_EMAC4 is not set
3277 +# CONFIG_B44 is not set
3278 +# CONFIG_NETDEV_1000 is not set
3279 +# CONFIG_NETDEV_10000 is not set
3280 +
3281 +#
3282 +# Wireless LAN
3283 +#
3284 +# CONFIG_WLAN_PRE80211 is not set
3285 +# CONFIG_WLAN_80211 is not set
3286 +# CONFIG_WAN is not set
3287 +CONFIG_PPP=m
3288 +# CONFIG_PPP_MULTILINK is not set
3289 +# CONFIG_PPP_FILTER is not set
3290 +CONFIG_PPP_ASYNC=m
3291 +# CONFIG_PPP_SYNC_TTY is not set
3292 +CONFIG_PPP_DEFLATE=m
3293 +CONFIG_PPP_BSDCOMP=m
3294 +# CONFIG_PPP_MPPE is not set
3295 +# CONFIG_PPPOE is not set
3296 +# CONFIG_PPPOL2TP is not set
3297 +# CONFIG_SLIP is not set
3298 +CONFIG_SLHC=m
3299 +# CONFIG_NETCONSOLE is not set
3300 +# CONFIG_NETPOLL is not set
3301 +# CONFIG_NET_POLL_CONTROLLER is not set
3302 +# CONFIG_ISDN is not set
3303 +# CONFIG_PHONE is not set
3304 +
3305 +#
3306 +# Input device support
3307 +#
3308 +CONFIG_INPUT=m
3309 +# CONFIG_INPUT_FF_MEMLESS is not set
3310 +CONFIG_INPUT_POLLDEV=m
3311 +
3312 +#
3313 +# Userland interfaces
3314 +#
3315 +CONFIG_INPUT_MOUSEDEV=m
3316 +CONFIG_INPUT_MOUSEDEV_PSAUX=y
3317 +CONFIG_INPUT_MOUSEDEV_SCREEN_X=1024
3318 +CONFIG_INPUT_MOUSEDEV_SCREEN_Y=768
3319 +# CONFIG_INPUT_JOYDEV is not set
3320 +CONFIG_INPUT_EVDEV=m
3321 +# CONFIG_INPUT_EVBUG is not set
3322 +
3323 +#
3324 +# Input Device Drivers
3325 +#
3326 +CONFIG_INPUT_KEYBOARD=y
3327 +# CONFIG_KEYBOARD_ATKBD is not set
3328 +# CONFIG_KEYBOARD_SUNKBD is not set
3329 +# CONFIG_KEYBOARD_LKKBD is not set
3330 +# CONFIG_KEYBOARD_XTKBD is not set
3331 +# CONFIG_KEYBOARD_NEWTON is not set
3332 +# CONFIG_KEYBOARD_STOWAWAY is not set
3333 +CONFIG_KEYBOARD_GPIO=m
3334 +CONFIG_INPUT_MOUSE=y
3335 +# CONFIG_MOUSE_PS2 is not set
3336 +# CONFIG_MOUSE_SERIAL is not set
3337 +# CONFIG_MOUSE_VSXXXAA is not set
3338 +CONFIG_MOUSE_GPIO=m
3339 +# CONFIG_INPUT_JOYSTICK is not set
3340 +# CONFIG_INPUT_TABLET is not set
3341 +# CONFIG_INPUT_TOUCHSCREEN is not set
3342 +# CONFIG_INPUT_MISC is not set
3343 +
3344 +#
3345 +# Hardware I/O ports
3346 +#
3347 +# CONFIG_SERIO is not set
3348 +# CONFIG_GAMEPORT is not set
3349 +
3350 +#
3351 +# Character devices
3352 +#
3353 +# CONFIG_VT is not set
3354 +# CONFIG_SERIAL_NONSTANDARD is not set
3355 +
3356 +#
3357 +# Serial drivers
3358 +#
3359 +# CONFIG_SERIAL_8250 is not set
3360 +
3361 +#
3362 +# Non-8250 serial port support
3363 +#
3364 +CONFIG_SERIAL_ATMEL=y
3365 +CONFIG_SERIAL_ATMEL_CONSOLE=y
3366 +CONFIG_SERIAL_ATMEL_PDC=y
3367 +# CONFIG_SERIAL_ATMEL_TTYAT is not set
3368 +CONFIG_SERIAL_CORE=y
3369 +CONFIG_SERIAL_CORE_CONSOLE=y
3370 +CONFIG_UNIX98_PTYS=y
3371 +# CONFIG_LEGACY_PTYS is not set
3372 +# CONFIG_IPMI_HANDLER is not set
3373 +# CONFIG_HW_RANDOM is not set
3374 +# CONFIG_R3964 is not set
3375 +# CONFIG_RAW_DRIVER is not set
3376 +# CONFIG_TCG_TPM is not set
3377 +CONFIG_I2C=m
3378 +CONFIG_I2C_BOARDINFO=y
3379 +CONFIG_I2C_CHARDEV=m
3380 +
3381 +#
3382 +# I2C Algorithms
3383 +#
3384 +CONFIG_I2C_ALGOBIT=m
3385 +# CONFIG_I2C_ALGOPCF is not set
3386 +# CONFIG_I2C_ALGOPCA is not set
3387 +
3388 +#
3389 +# I2C Hardware Bus support
3390 +#
3391 +CONFIG_I2C_ATMELTWI=m
3392 +CONFIG_I2C_GPIO=m
3393 +# CONFIG_I2C_OCORES is not set
3394 +# CONFIG_I2C_PARPORT_LIGHT is not set
3395 +# CONFIG_I2C_SIMTEC is not set
3396 +# CONFIG_I2C_TAOS_EVM is not set
3397 +# CONFIG_I2C_STUB is not set
3398 +
3399 +#
3400 +# Miscellaneous I2C Chip support
3401 +#
3402 +# CONFIG_DS1682 is not set
3403 +# CONFIG_SENSORS_EEPROM is not set
3404 +# CONFIG_SENSORS_PCF8574 is not set
3405 +# CONFIG_PCF8575 is not set
3406 +# CONFIG_SENSORS_PCF8591 is not set
3407 +# CONFIG_TPS65010 is not set
3408 +# CONFIG_SENSORS_MAX6875 is not set
3409 +# CONFIG_SENSORS_TSL2550 is not set
3410 +# CONFIG_I2C_DEBUG_CORE is not set
3411 +# CONFIG_I2C_DEBUG_ALGO is not set
3412 +# CONFIG_I2C_DEBUG_BUS is not set
3413 +# CONFIG_I2C_DEBUG_CHIP is not set
3414 +
3415 +#
3416 +# SPI support
3417 +#
3418 +CONFIG_SPI=y
3419 +# CONFIG_SPI_DEBUG is not set
3420 +CONFIG_SPI_MASTER=y
3421 +
3422 +#
3423 +# SPI Master Controller Drivers
3424 +#
3425 +CONFIG_SPI_ATMEL=y
3426 +# CONFIG_SPI_BITBANG is not set
3427 +
3428 +#
3429 +# SPI Protocol Masters
3430 +#
3431 +# CONFIG_SPI_AT25 is not set
3432 +CONFIG_SPI_SPIDEV=m
3433 +# CONFIG_SPI_TLE62X0 is not set
3434 +CONFIG_HAVE_GPIO_LIB=y
3435 +
3436 +#
3437 +# GPIO Support
3438 +#
3439 +# CONFIG_DEBUG_GPIO is not set
3440 +
3441 +#
3442 +# I2C GPIO expanders:
3443 +#
3444 +# CONFIG_GPIO_PCA953X is not set
3445 +# CONFIG_GPIO_PCF857X is not set
3446 +
3447 +#
3448 +# SPI GPIO expanders:
3449 +#
3450 +# CONFIG_GPIO_MCP23S08 is not set
3451 +# CONFIG_W1 is not set
3452 +# CONFIG_POWER_SUPPLY is not set
3453 +# CONFIG_HWMON is not set
3454 +# CONFIG_THERMAL is not set
3455 +CONFIG_WATCHDOG=y
3456 +# CONFIG_WATCHDOG_NOWAYOUT is not set
3457 +
3458 +#
3459 +# Watchdog Device Drivers
3460 +#
3461 +# CONFIG_SOFT_WATCHDOG is not set
3462 +CONFIG_AT32AP700X_WDT=y
3463 +
3464 +#
3465 +# Sonics Silicon Backplane
3466 +#
3467 +CONFIG_SSB_POSSIBLE=y
3468 +# CONFIG_SSB is not set
3469 +
3470 +#
3471 +# Multifunction device drivers
3472 +#
3473 +# CONFIG_MFD_SM501 is not set
3474 +
3475 +#
3476 +# Multimedia devices
3477 +#
3478 +# CONFIG_VIDEO_DEV is not set
3479 +# CONFIG_DVB_CORE is not set
3480 +# CONFIG_DAB is not set
3481 +
3482 +#
3483 +# Graphics support
3484 +#
3485 +# CONFIG_VGASTATE is not set
3486 +# CONFIG_VIDEO_OUTPUT_CONTROL is not set
3487 +CONFIG_FB=y
3488 +# CONFIG_FIRMWARE_EDID is not set
3489 +# CONFIG_FB_DDC is not set
3490 +CONFIG_FB_CFB_FILLRECT=y
3491 +CONFIG_FB_CFB_COPYAREA=y
3492 +CONFIG_FB_CFB_IMAGEBLIT=y
3493 +# CONFIG_FB_CFB_REV_PIXELS_IN_BYTE is not set
3494 +# CONFIG_FB_SYS_FILLRECT is not set
3495 +# CONFIG_FB_SYS_COPYAREA is not set
3496 +# CONFIG_FB_SYS_IMAGEBLIT is not set
3497 +# CONFIG_FB_SYS_FOPS is not set
3498 +CONFIG_FB_DEFERRED_IO=y
3499 +# CONFIG_FB_SVGALIB is not set
3500 +# CONFIG_FB_MACMODES is not set
3501 +# CONFIG_FB_BACKLIGHT is not set
3502 +# CONFIG_FB_MODE_HELPERS is not set
3503 +# CONFIG_FB_TILEBLITTING is not set
3504 +
3505 +#
3506 +# Frame buffer hardware drivers
3507 +#
3508 +# CONFIG_FB_S1D13XXX is not set
3509 +CONFIG_FB_ATMEL=y
3510 +# CONFIG_FB_VIRTUAL is not set
3511 +CONFIG_BACKLIGHT_LCD_SUPPORT=y
3512 +CONFIG_LCD_CLASS_DEVICE=y
3513 +CONFIG_LCD_LTV350QV=y
3514 +# CONFIG_BACKLIGHT_CLASS_DEVICE is not set
3515 +
3516 +#
3517 +# Display device support
3518 +#
3519 +# CONFIG_DISPLAY_SUPPORT is not set
3520 +# CONFIG_LOGO is not set
3521 +
3522 +#
3523 +# Sound
3524 +#
3525 +CONFIG_SOUND=m
3526 +
3527 +#
3528 +# Advanced Linux Sound Architecture
3529 +#
3530 +CONFIG_SND=m
3531 +CONFIG_SND_TIMER=m
3532 +CONFIG_SND_PCM=m
3533 +# CONFIG_SND_SEQUENCER is not set
3534 +CONFIG_SND_OSSEMUL=y
3535 +CONFIG_SND_MIXER_OSS=m
3536 +CONFIG_SND_PCM_OSS=m
3537 +CONFIG_SND_PCM_OSS_PLUGINS=y
3538 +# CONFIG_SND_DYNAMIC_MINORS is not set
3539 +# CONFIG_SND_SUPPORT_OLD_API is not set
3540 +# CONFIG_SND_VERBOSE_PROCFS is not set
3541 +# CONFIG_SND_VERBOSE_PRINTK is not set
3542 +# CONFIG_SND_DEBUG is not set
3543 +
3544 +#
3545 +# Generic devices
3546 +#
3547 +CONFIG_SND_AC97_CODEC=m
3548 +# CONFIG_SND_DUMMY is not set
3549 +# CONFIG_SND_MTPAV is not set
3550 +# CONFIG_SND_SERIAL_U16550 is not set
3551 +# CONFIG_SND_MPU401 is not set
3552 +
3553 +#
3554 +# AVR32 devices
3555 +#
3556 +CONFIG_SND_ATMEL_AC97=m
3557 +
3558 +#
3559 +# SPI devices
3560 +#
3561 +CONFIG_SND_AT73C213=m
3562 +CONFIG_SND_AT73C213_TARGET_BITRATE=48000
3563 +
3564 +#
3565 +# System on Chip audio support
3566 +#
3567 +# CONFIG_SND_SOC is not set
3568 +
3569 +#
3570 +# SoC Audio support for SuperH
3571 +#
3572 +
3573 +#
3574 +# ALSA SoC audio for Freescale SOCs
3575 +#
3576 +
3577 +#
3578 +# Open Sound System
3579 +#
3580 +# CONFIG_SOUND_PRIME is not set
3581 +CONFIG_AC97_BUS=m
3582 +# CONFIG_HID_SUPPORT is not set
3583 +CONFIG_USB_SUPPORT=y
3584 +# CONFIG_USB_ARCH_HAS_HCD is not set
3585 +# CONFIG_USB_ARCH_HAS_OHCI is not set
3586 +# CONFIG_USB_ARCH_HAS_EHCI is not set
3587 +
3588 +#
3589 +# NOTE: USB_STORAGE enables SCSI, and 'SCSI disk support'
3590 +#
3591 +CONFIG_USB_GADGET=y
3592 +# CONFIG_USB_GADGET_DEBUG is not set
3593 +# CONFIG_USB_GADGET_DEBUG_FILES is not set
3594 +# CONFIG_USB_GADGET_DEBUG_FS is not set
3595 +CONFIG_USB_GADGET_SELECTED=y
3596 +# CONFIG_USB_GADGET_AMD5536UDC is not set
3597 +CONFIG_USB_GADGET_ATMEL_USBA=y
3598 +CONFIG_USB_ATMEL_USBA=y
3599 +# CONFIG_USB_GADGET_FSL_USB2 is not set
3600 +# CONFIG_USB_GADGET_NET2280 is not set
3601 +# CONFIG_USB_GADGET_PXA2XX is not set
3602 +# CONFIG_USB_GADGET_M66592 is not set
3603 +# CONFIG_USB_GADGET_GOKU is not set
3604 +# CONFIG_USB_GADGET_LH7A40X is not set
3605 +# CONFIG_USB_GADGET_OMAP is not set
3606 +# CONFIG_USB_GADGET_S3C2410 is not set
3607 +# CONFIG_USB_GADGET_AT91 is not set
3608 +# CONFIG_USB_GADGET_DUMMY_HCD is not set
3609 +CONFIG_USB_GADGET_DUALSPEED=y
3610 +CONFIG_USB_ZERO=m
3611 +CONFIG_USB_ETH=m
3612 +CONFIG_USB_ETH_RNDIS=y
3613 +CONFIG_USB_GADGETFS=m
3614 +CONFIG_USB_FILE_STORAGE=m
3615 +# CONFIG_USB_FILE_STORAGE_TEST is not set
3616 +CONFIG_USB_G_SERIAL=m
3617 +# CONFIG_USB_MIDI_GADGET is not set
3618 +# CONFIG_USB_G_PRINTER is not set
3619 +CONFIG_MMC=y
3620 +# CONFIG_MMC_DEBUG is not set
3621 +# CONFIG_MMC_UNSAFE_RESUME is not set
3622 +
3623 +#
3624 +# MMC/SD Card Drivers
3625 +#
3626 +CONFIG_MMC_BLOCK=y
3627 +CONFIG_MMC_BLOCK_BOUNCE=y
3628 +# CONFIG_SDIO_UART is not set
3629 +
3630 +#
3631 +# MMC/SD Host Controller Drivers
3632 +#
3633 +CONFIG_MMC_ATMELMCI=y
3634 +CONFIG_MMC_SPI=m
3635 +# CONFIG_MEMSTICK is not set
3636 +CONFIG_NEW_LEDS=y
3637 +CONFIG_LEDS_CLASS=m
3638 +
3639 +#
3640 +# LED drivers
3641 +#
3642 +CONFIG_LEDS_ATMEL_PWM=m
3643 +CONFIG_LEDS_GPIO=m
3644 +
3645 +#
3646 +# LED Triggers
3647 +#
3648 +CONFIG_LEDS_TRIGGERS=y
3649 +CONFIG_LEDS_TRIGGER_TIMER=m
3650 +CONFIG_LEDS_TRIGGER_HEARTBEAT=m
3651 +CONFIG_RTC_LIB=y
3652 +CONFIG_RTC_CLASS=y
3653 +CONFIG_RTC_HCTOSYS=y
3654 +CONFIG_RTC_HCTOSYS_DEVICE="rtc0"
3655 +# CONFIG_RTC_DEBUG is not set
3656 +
3657 +#
3658 +# RTC interfaces
3659 +#
3660 +CONFIG_RTC_INTF_SYSFS=y
3661 +CONFIG_RTC_INTF_PROC=y
3662 +CONFIG_RTC_INTF_DEV=y
3663 +# CONFIG_RTC_INTF_DEV_UIE_EMUL is not set
3664 +# CONFIG_RTC_DRV_TEST is not set
3665 +
3666 +#
3667 +# I2C RTC drivers
3668 +#
3669 +# CONFIG_RTC_DRV_DS1307 is not set
3670 +# CONFIG_RTC_DRV_DS1374 is not set
3671 +# CONFIG_RTC_DRV_DS1672 is not set
3672 +# CONFIG_RTC_DRV_MAX6900 is not set
3673 +# CONFIG_RTC_DRV_RS5C372 is not set
3674 +# CONFIG_RTC_DRV_ISL1208 is not set
3675 +# CONFIG_RTC_DRV_X1205 is not set
3676 +# CONFIG_RTC_DRV_PCF8563 is not set
3677 +# CONFIG_RTC_DRV_PCF8583 is not set
3678 +# CONFIG_RTC_DRV_M41T80 is not set
3679 +# CONFIG_RTC_DRV_S35390A is not set
3680 +
3681 +#
3682 +# SPI RTC drivers
3683 +#
3684 +# CONFIG_RTC_DRV_MAX6902 is not set
3685 +# CONFIG_RTC_DRV_R9701 is not set
3686 +# CONFIG_RTC_DRV_RS5C348 is not set
3687 +
3688 +#
3689 +# Platform RTC drivers
3690 +#
3691 +# CONFIG_RTC_DRV_DS1511 is not set
3692 +# CONFIG_RTC_DRV_DS1553 is not set
3693 +# CONFIG_RTC_DRV_DS1742 is not set
3694 +# CONFIG_RTC_DRV_STK17TA8 is not set
3695 +# CONFIG_RTC_DRV_M48T86 is not set
3696 +# CONFIG_RTC_DRV_M48T59 is not set
3697 +# CONFIG_RTC_DRV_V3020 is not set
3698 +
3699 +#
3700 +# on-CPU RTC drivers
3701 +#
3702 +CONFIG_RTC_DRV_AT32AP700X=y
3703 +
3704 +#
3705 +# Userspace I/O
3706 +#
3707 +# CONFIG_UIO is not set
3708 +
3709 +#
3710 +# File systems
3711 +#
3712 +CONFIG_EXT2_FS=y
3713 +# CONFIG_EXT2_FS_XATTR is not set
3714 +# CONFIG_EXT2_FS_XIP is not set
3715 +CONFIG_EXT3_FS=y
3716 +# CONFIG_EXT3_FS_XATTR is not set
3717 +# CONFIG_EXT4DEV_FS is not set
3718 +CONFIG_JBD=y
3719 +# CONFIG_JBD_DEBUG is not set
3720 +# CONFIG_REISERFS_FS is not set
3721 +# CONFIG_JFS_FS is not set
3722 +# CONFIG_FS_POSIX_ACL is not set
3723 +# CONFIG_XFS_FS is not set
3724 +# CONFIG_GFS2_FS is not set
3725 +# CONFIG_OCFS2_FS is not set
3726 +# CONFIG_DNOTIFY is not set
3727 +CONFIG_INOTIFY=y
3728 +CONFIG_INOTIFY_USER=y
3729 +# CONFIG_QUOTA is not set
3730 +# CONFIG_AUTOFS_FS is not set
3731 +# CONFIG_AUTOFS4_FS is not set
3732 +CONFIG_FUSE_FS=m
3733 +
3734 +#
3735 +# CD-ROM/DVD Filesystems
3736 +#
3737 +# CONFIG_ISO9660_FS is not set
3738 +# CONFIG_UDF_FS is not set
3739 +
3740 +#
3741 +# DOS/FAT/NT Filesystems
3742 +#
3743 +CONFIG_FAT_FS=m
3744 +CONFIG_MSDOS_FS=m
3745 +CONFIG_VFAT_FS=m
3746 +CONFIG_FAT_DEFAULT_CODEPAGE=437
3747 +CONFIG_FAT_DEFAULT_IOCHARSET="iso8859-1"
3748 +# CONFIG_NTFS_FS is not set
3749 +
3750 +#
3751 +# Pseudo filesystems
3752 +#
3753 +CONFIG_PROC_FS=y
3754 +CONFIG_PROC_KCORE=y
3755 +CONFIG_PROC_SYSCTL=y
3756 +CONFIG_SYSFS=y
3757 +CONFIG_TMPFS=y
3758 +# CONFIG_TMPFS_POSIX_ACL is not set
3759 +# CONFIG_HUGETLB_PAGE is not set
3760 +CONFIG_CONFIGFS_FS=y
3761 +
3762 +#
3763 +# Miscellaneous filesystems
3764 +#
3765 +# CONFIG_ADFS_FS is not set
3766 +# CONFIG_AFFS_FS is not set
3767 +# CONFIG_HFS_FS is not set
3768 +# CONFIG_HFSPLUS_FS is not set
3769 +# CONFIG_BEFS_FS is not set
3770 +# CONFIG_BFS_FS is not set
3771 +# CONFIG_EFS_FS is not set
3772 +CONFIG_JFFS2_FS=y
3773 +CONFIG_JFFS2_FS_DEBUG=0
3774 +# CONFIG_JFFS2_FS_WRITEBUFFER is not set
3775 +# CONFIG_JFFS2_SUMMARY is not set
3776 +# CONFIG_JFFS2_FS_XATTR is not set
3777 +# CONFIG_JFFS2_COMPRESSION_OPTIONS is not set
3778 +CONFIG_JFFS2_ZLIB=y
3779 +# CONFIG_JFFS2_LZO is not set
3780 +CONFIG_JFFS2_RTIME=y
3781 +# CONFIG_JFFS2_RUBIN is not set
3782 +# CONFIG_CRAMFS is not set
3783 +# CONFIG_VXFS_FS is not set
3784 +CONFIG_MINIX_FS=m
3785 +# CONFIG_HPFS_FS is not set
3786 +# CONFIG_QNX4FS_FS is not set
3787 +# CONFIG_ROMFS_FS is not set
3788 +# CONFIG_SYSV_FS is not set
3789 +# CONFIG_UFS_FS is not set
3790 +CONFIG_NETWORK_FILESYSTEMS=y
3791 +CONFIG_NFS_FS=y
3792 +CONFIG_NFS_V3=y
3793 +# CONFIG_NFS_V3_ACL is not set
3794 +# CONFIG_NFS_V4 is not set
3795 +# CONFIG_NFS_DIRECTIO is not set
3796 +# CONFIG_NFSD is not set
3797 +CONFIG_ROOT_NFS=y
3798 +CONFIG_LOCKD=y
3799 +CONFIG_LOCKD_V4=y
3800 +CONFIG_NFS_COMMON=y
3801 +CONFIG_SUNRPC=y
3802 +# CONFIG_SUNRPC_BIND34 is not set
3803 +# CONFIG_RPCSEC_GSS_KRB5 is not set
3804 +# CONFIG_RPCSEC_GSS_SPKM3 is not set
3805 +# CONFIG_SMB_FS is not set
3806 +# CONFIG_CIFS is not set
3807 +# CONFIG_NCP_FS is not set
3808 +# CONFIG_CODA_FS is not set
3809 +# CONFIG_AFS_FS is not set
3810 +
3811 +#
3812 +# Partition Types
3813 +#
3814 +# CONFIG_PARTITION_ADVANCED is not set
3815 +CONFIG_MSDOS_PARTITION=y
3816 +CONFIG_NLS=m
3817 +CONFIG_NLS_DEFAULT="iso8859-1"
3818 +CONFIG_NLS_CODEPAGE_437=m
3819 +# CONFIG_NLS_CODEPAGE_737 is not set
3820 +# CONFIG_NLS_CODEPAGE_775 is not set
3821 +# CONFIG_NLS_CODEPAGE_850 is not set
3822 +# CONFIG_NLS_CODEPAGE_852 is not set
3823 +# CONFIG_NLS_CODEPAGE_855 is not set
3824 +# CONFIG_NLS_CODEPAGE_857 is not set
3825 +# CONFIG_NLS_CODEPAGE_860 is not set
3826 +# CONFIG_NLS_CODEPAGE_861 is not set
3827 +# CONFIG_NLS_CODEPAGE_862 is not set
3828 +# CONFIG_NLS_CODEPAGE_863 is not set
3829 +# CONFIG_NLS_CODEPAGE_864 is not set
3830 +# CONFIG_NLS_CODEPAGE_865 is not set
3831 +# CONFIG_NLS_CODEPAGE_866 is not set
3832 +# CONFIG_NLS_CODEPAGE_869 is not set
3833 +# CONFIG_NLS_CODEPAGE_936 is not set
3834 +# CONFIG_NLS_CODEPAGE_950 is not set
3835 +# CONFIG_NLS_CODEPAGE_932 is not set
3836 +# CONFIG_NLS_CODEPAGE_949 is not set
3837 +# CONFIG_NLS_CODEPAGE_874 is not set
3838 +# CONFIG_NLS_ISO8859_8 is not set
3839 +# CONFIG_NLS_CODEPAGE_1250 is not set
3840 +# CONFIG_NLS_CODEPAGE_1251 is not set
3841 +# CONFIG_NLS_ASCII is not set
3842 +CONFIG_NLS_ISO8859_1=m
3843 +# CONFIG_NLS_ISO8859_2 is not set
3844 +# CONFIG_NLS_ISO8859_3 is not set
3845 +# CONFIG_NLS_ISO8859_4 is not set
3846 +# CONFIG_NLS_ISO8859_5 is not set
3847 +# CONFIG_NLS_ISO8859_6 is not set
3848 +# CONFIG_NLS_ISO8859_7 is not set
3849 +# CONFIG_NLS_ISO8859_9 is not set
3850 +# CONFIG_NLS_ISO8859_13 is not set
3851 +# CONFIG_NLS_ISO8859_14 is not set
3852 +# CONFIG_NLS_ISO8859_15 is not set
3853 +# CONFIG_NLS_KOI8_R is not set
3854 +# CONFIG_NLS_KOI8_U is not set
3855 +CONFIG_NLS_UTF8=m
3856 +# CONFIG_DLM is not set
3857 +
3858 +#
3859 +# Kernel hacking
3860 +#
3861 +# CONFIG_PRINTK_TIME is not set
3862 +CONFIG_ENABLE_WARN_DEPRECATED=y
3863 +CONFIG_ENABLE_MUST_CHECK=y
3864 +CONFIG_MAGIC_SYSRQ=y
3865 +# CONFIG_UNUSED_SYMBOLS is not set
3866 +CONFIG_DEBUG_FS=y
3867 +# CONFIG_HEADERS_CHECK is not set
3868 +CONFIG_DEBUG_KERNEL=y
3869 +# CONFIG_DEBUG_SHIRQ is not set
3870 +CONFIG_DETECT_SOFTLOCKUP=y
3871 +CONFIG_SCHED_DEBUG=y
3872 +# CONFIG_SCHEDSTATS is not set
3873 +# CONFIG_TIMER_STATS is not set
3874 +# CONFIG_SLUB_DEBUG_ON is not set
3875 +# CONFIG_SLUB_STATS is not set
3876 +# CONFIG_DEBUG_RT_MUTEXES is not set
3877 +# CONFIG_RT_MUTEX_TESTER is not set
3878 +# CONFIG_DEBUG_SPINLOCK is not set
3879 +# CONFIG_DEBUG_MUTEXES is not set
3880 +# CONFIG_DEBUG_LOCK_ALLOC is not set
3881 +# CONFIG_PROVE_LOCKING is not set
3882 +# CONFIG_LOCK_STAT is not set
3883 +# CONFIG_DEBUG_SPINLOCK_SLEEP is not set
3884 +# CONFIG_DEBUG_LOCKING_API_SELFTESTS is not set
3885 +# CONFIG_DEBUG_KOBJECT is not set
3886 +CONFIG_DEBUG_BUGVERBOSE=y
3887 +# CONFIG_DEBUG_INFO is not set
3888 +# CONFIG_DEBUG_VM is not set
3889 +# CONFIG_DEBUG_LIST is not set
3890 +# CONFIG_DEBUG_SG is not set
3891 +CONFIG_FRAME_POINTER=y
3892 +# CONFIG_BOOT_PRINTK_DELAY is not set
3893 +# CONFIG_RCU_TORTURE_TEST is not set
3894 +# CONFIG_KPROBES_SANITY_TEST is not set
3895 +# CONFIG_BACKTRACE_SELF_TEST is not set
3896 +# CONFIG_LKDTM is not set
3897 +# CONFIG_FAULT_INJECTION is not set
3898 +# CONFIG_SAMPLES is not set
3899 +
3900 +#
3901 +# Security options
3902 +#
3903 +# CONFIG_KEYS is not set
3904 +# CONFIG_SECURITY is not set
3905 +# CONFIG_SECURITY_FILE_CAPABILITIES is not set
3906 +CONFIG_CRYPTO=y
3907 +CONFIG_CRYPTO_ALGAPI=m
3908 +CONFIG_CRYPTO_AEAD=m
3909 +CONFIG_CRYPTO_BLKCIPHER=m
3910 +# CONFIG_CRYPTO_SEQIV is not set
3911 +CONFIG_CRYPTO_HASH=m
3912 +CONFIG_CRYPTO_MANAGER=m
3913 +CONFIG_CRYPTO_HMAC=m
3914 +# CONFIG_CRYPTO_XCBC is not set
3915 +# CONFIG_CRYPTO_NULL is not set
3916 +# CONFIG_CRYPTO_MD4 is not set
3917 +CONFIG_CRYPTO_MD5=m
3918 +CONFIG_CRYPTO_SHA1=m
3919 +# CONFIG_CRYPTO_SHA256 is not set
3920 +# CONFIG_CRYPTO_SHA512 is not set
3921 +# CONFIG_CRYPTO_WP512 is not set
3922 +# CONFIG_CRYPTO_TGR192 is not set
3923 +# CONFIG_CRYPTO_GF128MUL is not set
3924 +# CONFIG_CRYPTO_ECB is not set
3925 +CONFIG_CRYPTO_CBC=m
3926 +# CONFIG_CRYPTO_PCBC is not set
3927 +# CONFIG_CRYPTO_LRW is not set
3928 +# CONFIG_CRYPTO_XTS is not set
3929 +# CONFIG_CRYPTO_CTR is not set
3930 +# CONFIG_CRYPTO_GCM is not set
3931 +# CONFIG_CRYPTO_CCM is not set
3932 +# CONFIG_CRYPTO_CRYPTD is not set
3933 +CONFIG_CRYPTO_DES=m
3934 +# CONFIG_CRYPTO_FCRYPT is not set
3935 +# CONFIG_CRYPTO_BLOWFISH is not set
3936 +# CONFIG_CRYPTO_TWOFISH is not set
3937 +# CONFIG_CRYPTO_SERPENT is not set
3938 +# CONFIG_CRYPTO_AES is not set
3939 +# CONFIG_CRYPTO_CAST5 is not set
3940 +# CONFIG_CRYPTO_CAST6 is not set
3941 +# CONFIG_CRYPTO_TEA is not set
3942 +# CONFIG_CRYPTO_ARC4 is not set
3943 +# CONFIG_CRYPTO_KHAZAD is not set
3944 +# CONFIG_CRYPTO_ANUBIS is not set
3945 +# CONFIG_CRYPTO_SEED is not set
3946 +# CONFIG_CRYPTO_SALSA20 is not set
3947 +CONFIG_CRYPTO_DEFLATE=m
3948 +# CONFIG_CRYPTO_MICHAEL_MIC is not set
3949 +# CONFIG_CRYPTO_CRC32C is not set
3950 +# CONFIG_CRYPTO_CAMELLIA is not set
3951 +# CONFIG_CRYPTO_TEST is not set
3952 +CONFIG_CRYPTO_AUTHENC=m
3953 +# CONFIG_CRYPTO_LZO is not set
3954 +# CONFIG_CRYPTO_HW is not set
3955 +
3956 +#
3957 +# Library routines
3958 +#
3959 +CONFIG_BITREVERSE=y
3960 +CONFIG_CRC_CCITT=m
3961 +# CONFIG_CRC16 is not set
3962 +CONFIG_CRC_ITU_T=m
3963 +CONFIG_CRC32=y
3964 +CONFIG_CRC7=m
3965 +# CONFIG_LIBCRC32C is not set
3966 +CONFIG_ZLIB_INFLATE=y
3967 +CONFIG_ZLIB_DEFLATE=y
3968 +CONFIG_GENERIC_ALLOCATOR=y
3969 +CONFIG_PLIST=y
3970 +CONFIG_HAS_IOMEM=y
3971 +CONFIG_HAS_IOPORT=y
3972 +CONFIG_HAS_DMA=y
3973 diff --exclude=.git -urN linux-2.6.25.6/arch/avr32/drivers/dw-dmac.c avr32-2.6/arch/avr32/drivers/dw-dmac.c
3974 --- linux-2.6.25.6/arch/avr32/drivers/dw-dmac.c 1970-01-01 01:00:00.000000000 +0100
3975 +++ avr32-2.6/arch/avr32/drivers/dw-dmac.c 2008-06-12 15:09:38.719815350 +0200
3976 @@ -0,0 +1,761 @@
3977 +/*
3978 + * Driver for the Synopsys DesignWare DMA Controller
3979 + *
3980 + * Copyright (C) 2005-2006 Atmel Corporation
3981 + *
3982 + * This program is free software; you can redistribute it and/or modify
3983 + * it under the terms of the GNU General Public License version 2 as
3984 + * published by the Free Software Foundation.
3985 + */
3986 +#include <linux/clk.h>
3987 +#include <linux/device.h>
3988 +#include <linux/dma-mapping.h>
3989 +#include <linux/dmapool.h>
3990 +#include <linux/init.h>
3991 +#include <linux/interrupt.h>
3992 +#include <linux/module.h>
3993 +#include <linux/platform_device.h>
3994 +
3995 +#include <asm/dma-controller.h>
3996 +#include <asm/io.h>
3997 +
3998 +#include "dw-dmac.h"
3999 +
4000 +#define DMAC_NR_CHANNELS 3
4001 +#define DMAC_MAX_BLOCKSIZE 4095
4002 +
4003 +enum {
4004 + CH_STATE_FREE = 0,
4005 + CH_STATE_ALLOCATED,
4006 + CH_STATE_BUSY,
4007 +};
4008 +
4009 +struct dw_dma_lli {
4010 + dma_addr_t sar;
4011 + dma_addr_t dar;
4012 + dma_addr_t llp;
4013 + u32 ctllo;
4014 + u32 ctlhi;
4015 + u32 sstat;
4016 + u32 dstat;
4017 +};
4018 +
4019 +struct dw_dma_block {
4020 + struct dw_dma_lli *lli_vaddr;
4021 + dma_addr_t lli_dma_addr;
4022 +};
4023 +
4024 +struct dw_dma_channel {
4025 + unsigned int state;
4026 + int is_cyclic;
4027 + struct dma_request_sg *req_sg;
4028 + struct dma_request_cyclic *req_cyclic;
4029 + unsigned int nr_blocks;
4030 + int direction;
4031 + struct dw_dma_block *block;
4032 +};
4033 +
4034 +struct dw_dma_controller {
4035 + spinlock_t lock;
4036 + void * __iomem regs;
4037 + struct dma_pool *lli_pool;
4038 + struct clk *hclk;
4039 + struct dma_controller dma;
4040 + struct dw_dma_channel channel[DMAC_NR_CHANNELS];
4041 +};
4042 +#define to_dw_dmac(dmac) container_of(dmac, struct dw_dma_controller, dma)
4043 +
4044 +#define dmac_writel_hi(dmac, reg, value) \
4045 + __raw_writel((value), (dmac)->regs + DW_DMAC_##reg + 4)
4046 +#define dmac_readl_hi(dmac, reg) \
4047 + __raw_readl((dmac)->regs + DW_DMAC_##reg + 4)
4048 +#define dmac_writel_lo(dmac, reg, value) \
4049 + __raw_writel((value), (dmac)->regs + DW_DMAC_##reg)
4050 +#define dmac_readl_lo(dmac, reg) \
4051 + __raw_readl((dmac)->regs + DW_DMAC_##reg)
4052 +#define dmac_chan_writel_hi(dmac, chan, reg, value) \
4053 + __raw_writel((value), ((dmac)->regs + 0x58 * (chan) \
4054 + + DW_DMAC_CHAN_##reg + 4))
4055 +#define dmac_chan_readl_hi(dmac, chan, reg) \
4056 + __raw_readl((dmac)->regs + 0x58 * (chan) + DW_DMAC_CHAN_##reg + 4)
4057 +#define dmac_chan_writel_lo(dmac, chan, reg, value) \
4058 + __raw_writel((value), (dmac)->regs + 0x58 * (chan) + DW_DMAC_CHAN_##reg)
4059 +#define dmac_chan_readl_lo(dmac, chan, reg) \
4060 + __raw_readl((dmac)->regs + 0x58 * (chan) + DW_DMAC_CHAN_##reg)
4061 +#define set_channel_bit(dmac, reg, chan) \
4062 + dmac_writel_lo(dmac, reg, (1 << (chan)) | (1 << ((chan) + 8)))
4063 +#define clear_channel_bit(dmac, reg, chan) \
4064 + dmac_writel_lo(dmac, reg, (0 << (chan)) | (1 << ((chan) + 8)))
4065 +
4066 +static int dmac_alloc_channel(struct dma_controller *_dmac)
4067 +{
4068 + struct dw_dma_controller *dmac = to_dw_dmac(_dmac);
4069 + struct dw_dma_channel *chan;
4070 + unsigned long flags;
4071 + int i;
4072 +
4073 + spin_lock_irqsave(&dmac->lock, flags);
4074 + for (i = 0; i < DMAC_NR_CHANNELS; i++)
4075 + if (dmac->channel[i].state == CH_STATE_FREE)
4076 + break;
4077 +
4078 + if (i < DMAC_NR_CHANNELS) {
4079 + chan = &dmac->channel[i];
4080 + chan->state = CH_STATE_ALLOCATED;
4081 + } else {
4082 + i = -EBUSY;
4083 + }
4084 +
4085 + spin_unlock_irqrestore(&dmac->lock, flags);
4086 +
4087 + return i;
4088 +}
4089 +
4090 +static void dmac_release_channel(struct dma_controller *_dmac, int channel)
4091 +{
4092 + struct dw_dma_controller *dmac = to_dw_dmac(_dmac);
4093 +
4094 + BUG_ON(channel >= DMAC_NR_CHANNELS
4095 + || dmac->channel[channel].state != CH_STATE_ALLOCATED);
4096 +
4097 + dmac->channel[channel].state = CH_STATE_FREE;
4098 +}
4099 +
4100 +static struct dw_dma_block *allocate_blocks(struct dw_dma_controller *dmac,
4101 + unsigned int nr_blocks)
4102 +{
4103 + struct dw_dma_block *block;
4104 + void *p;
4105 + unsigned int i;
4106 +
4107 + block = kmalloc(nr_blocks * sizeof(*block),
4108 + GFP_KERNEL);
4109 + if (unlikely(!block))
4110 + return NULL;
4111 +
4112 + for (i = 0; i < nr_blocks; i++) {
4113 + p = dma_pool_alloc(dmac->lli_pool, GFP_KERNEL,
4114 + &block[i].lli_dma_addr);
4115 + block[i].lli_vaddr = p;
4116 + if (unlikely(!p))
4117 + goto fail;
4118 + }
4119 +
4120 + return block;
4121 +
4122 +fail:
4123 + for (i = 0; i < nr_blocks; i++) {
4124 + if (!block[i].lli_vaddr)
4125 + break;
4126 + dma_pool_free(dmac->lli_pool, block[i].lli_vaddr,
4127 + block[i].lli_dma_addr);
4128 + }
4129 + kfree(block);
4130 + return NULL;
4131 +}
4132 +
4133 +static void cleanup_channel(struct dw_dma_controller *dmac,
4134 + struct dw_dma_channel *chan)
4135 +{
4136 + unsigned int i;
4137 +
4138 + if (chan->nr_blocks > 1) {
4139 + for (i = 0; i < chan->nr_blocks; i++)
4140 + dma_pool_free(dmac->lli_pool, chan->block[i].lli_vaddr,
4141 + chan->block[i].lli_dma_addr);
4142 + kfree(chan->block);
4143 + }
4144 +
4145 + chan->state = CH_STATE_ALLOCATED;
4146 +}
4147 +
4148 +static int dmac_prepare_request_sg(struct dma_controller *_dmac,
4149 + struct dma_request_sg *req)
4150 +{
4151 + struct dw_dma_controller *dmac = to_dw_dmac(_dmac);
4152 + struct dw_dma_channel *chan;
4153 + unsigned long ctlhi, ctllo, cfghi, cfglo;
4154 + unsigned long block_size;
4155 + unsigned int nr_blocks;
4156 + int ret, i, direction;
4157 + unsigned long flags;
4158 +
4159 + spin_lock_irqsave(&dmac->lock, flags);
4160 +
4161 + ret = -EINVAL;
4162 + if (req->req.channel >= DMAC_NR_CHANNELS
4163 + || dmac->channel[req->req.channel].state != CH_STATE_ALLOCATED
4164 + || req->block_size > DMAC_MAX_BLOCKSIZE) {
4165 + spin_unlock_irqrestore(&dmac->lock, flags);
4166 + return -EINVAL;
4167 + }
4168 +
4169 + chan = &dmac->channel[req->req.channel];
4170 + chan->state = CH_STATE_BUSY;
4171 + chan->req_sg = req;
4172 + chan->is_cyclic = 0;
4173 +
4174 + /*
4175 + * We have marked the channel as busy, so no need to keep the
4176 + * lock as long as we only touch the channel-specific
4177 + * registers
4178 + */
4179 + spin_unlock_irqrestore(&dmac->lock, flags);
4180 +
4181 + /*
4182 + * There may be limitations in the driver and/or the DMA
4183 + * controller that prevents us from sending a whole
4184 + * scatterlist item in one go. Taking this into account,
4185 + * calculate the number of block transfers we need to set up.
4186 + *
4187 + * FIXME: Let the peripheral driver know about the maximum
4188 + * block size we support. We really don't want to use a
4189 + * different block size than what was suggested by the
4190 + * peripheral.
4191 + *
4192 + * Each block will get its own Linked List Item (LLI) below.
4193 + */
4194 + block_size = req->block_size;
4195 + nr_blocks = req->nr_blocks;
4196 + pr_debug("block_size %lu, nr_blocks %u nr_sg = %u\n",
4197 + block_size, nr_blocks, req->nr_sg);
4198 +
4199 + BUG_ON(nr_blocks == 0);
4200 + chan->nr_blocks = nr_blocks;
4201 +
4202 + ret = -EINVAL;
4203 + cfglo = cfghi = 0;
4204 + switch (req->direction) {
4205 + case DMA_DIR_MEM_TO_PERIPH:
4206 + direction = DMA_TO_DEVICE;
4207 + cfghi = req->periph_id << (43 - 32);
4208 + break;
4209 +
4210 + case DMA_DIR_PERIPH_TO_MEM:
4211 + direction = DMA_FROM_DEVICE;
4212 + cfghi = req->periph_id << (39 - 32);
4213 + break;
4214 + default:
4215 + goto out_unclaim_channel;
4216 + }
4217 +
4218 + chan->direction = direction;
4219 +
4220 + dmac_chan_writel_hi(dmac, req->req.channel, CFG, cfghi);
4221 + dmac_chan_writel_lo(dmac, req->req.channel, CFG, cfglo);
4222 +
4223 + ctlhi = block_size >> req->width;
4224 + ctllo = ((req->direction << 20)
4225 + // | (1 << 14) | (1 << 11) // source/dest burst trans len
4226 + | (req->width << 4) | (req->width << 1)
4227 + | (1 << 0)); // interrupt enable
4228 +
4229 + if (nr_blocks == 1) {
4230 + /* Only one block: No need to use block chaining */
4231 + if (direction == DMA_TO_DEVICE) {
4232 + dmac_chan_writel_lo(dmac, req->req.channel, SAR,
4233 + req->sg->dma_address);
4234 + dmac_chan_writel_lo(dmac, req->req.channel, DAR,
4235 + req->data_reg);
4236 + ctllo |= 2 << 7; // no dst increment
4237 + } else {
4238 + dmac_chan_writel_lo(dmac, req->req.channel, SAR,
4239 + req->data_reg);
4240 + dmac_chan_writel_lo(dmac, req->req.channel, DAR,
4241 + req->sg->dma_address);
4242 + ctllo |= 2 << 9; // no src increment
4243 + }
4244 + dmac_chan_writel_lo(dmac, req->req.channel, CTL, ctllo);
4245 + dmac_chan_writel_hi(dmac, req->req.channel, CTL, ctlhi);
4246 + pr_debug("ctl hi:lo 0x%lx:%lx\n", ctlhi, ctllo);
4247 + } else {
4248 + struct dw_dma_lli *lli, *lli_prev = NULL;
4249 + int j = 0, offset = 0;
4250 +
4251 + ret = -ENOMEM;
4252 + chan->block = allocate_blocks(dmac, nr_blocks);
4253 + if (!chan->block)
4254 + goto out_unclaim_channel;
4255 +
4256 + if (direction == DMA_TO_DEVICE)
4257 + ctllo |= 1 << 28 | 1 << 27 | 2 << 7;
4258 + else
4259 + ctllo |= 1 << 28 | 1 << 27 | 2 << 9;
4260 +
4261 + /*
4262 + * Map scatterlist items to blocks. One scatterlist
4263 + * item may need more than one block for the reasons
4264 + * mentioned above.
4265 + */
4266 + for (i = 0; i < nr_blocks; i++) {
4267 + lli = chan->block[i].lli_vaddr;
4268 + if (lli_prev) {
4269 + lli_prev->llp = chan->block[i].lli_dma_addr;
4270 + pr_debug("lli[%d] (0x%p/0x%x): 0x%x 0x%x 0x%x 0x%x 0x%x\n",
4271 + i - 1, chan->block[i - 1].lli_vaddr,
4272 + chan->block[i - 1].lli_dma_addr,
4273 + lli_prev->sar, lli_prev->dar, lli_prev->llp,
4274 + lli_prev->ctllo, lli_prev->ctlhi);
4275 + }
4276 + lli->llp = 0;
4277 + lli->ctllo = ctllo;
4278 + lli->ctlhi = ctlhi;
4279 + if (direction == DMA_TO_DEVICE) {
4280 + lli->sar = req->sg[j].dma_address + offset;
4281 + lli->dar = req->data_reg;
4282 + } else {
4283 + lli->sar = req->data_reg;
4284 + lli->dar = req->sg[j].dma_address + offset;
4285 + }
4286 + lli_prev = lli;
4287 +
4288 + offset += block_size;
4289 + if (offset > req->sg[j].length) {
4290 + j++;
4291 + offset = 0;
4292 + }
4293 + }
4294 +
4295 + pr_debug("lli[%d] (0x%p/0x%x): 0x%x 0x%x 0x%x 0x%x 0x%x\n",
4296 + i - 1, chan->block[i - 1].lli_vaddr,
4297 + chan->block[i - 1].lli_dma_addr, lli_prev->sar,
4298 + lli_prev->dar, lli_prev->llp,
4299 + lli_prev->ctllo, lli_prev->ctlhi);
4300 +
4301 + /*
4302 + * SAR, DAR and CTL are initialized from the LLI. We
4303 + * only have to enable the LLI bits in CTL.
4304 + */
4305 + dmac_chan_writel_hi(dmac, req->req.channel, CTL, 0);
4306 + dmac_chan_writel_lo(dmac, req->req.channel, LLP,
4307 + chan->block[0].lli_dma_addr);
4308 + dmac_chan_writel_lo(dmac, req->req.channel, CTL, 1 << 28 | 1 << 27);
4309 + }
4310 +
4311 + set_channel_bit(dmac, MASK_XFER, req->req.channel);
4312 + set_channel_bit(dmac, MASK_ERROR, req->req.channel);
4313 + if (req->req.block_complete)
4314 + set_channel_bit(dmac, MASK_BLOCK, req->req.channel);
4315 + else
4316 + clear_channel_bit(dmac, MASK_BLOCK, req->req.channel);
4317 +
4318 + return 0;
4319 +
4320 +out_unclaim_channel:
4321 + chan->state = CH_STATE_ALLOCATED;
4322 + return ret;
4323 +}
4324 +
4325 +static int dmac_prepare_request_cyclic(struct dma_controller *_dmac,
4326 + struct dma_request_cyclic *req)
4327 +{
4328 + struct dw_dma_controller *dmac = to_dw_dmac(_dmac);
4329 + struct dw_dma_channel *chan;
4330 + unsigned long ctlhi, ctllo, cfghi, cfglo;
4331 + unsigned long block_size;
4332 + int ret, i, direction;
4333 + unsigned long flags;
4334 +
4335 + spin_lock_irqsave(&dmac->lock, flags);
4336 +
4337 + block_size = (req->buffer_size/req->periods) >> req->width;
4338 +
4339 + ret = -EINVAL;
4340 + if (req->req.channel >= DMAC_NR_CHANNELS
4341 + || dmac->channel[req->req.channel].state != CH_STATE_ALLOCATED
4342 + || (req->periods == 0)
4343 + || block_size > DMAC_MAX_BLOCKSIZE) {
4344 + spin_unlock_irqrestore(&dmac->lock, flags);
4345 + return -EINVAL;
4346 + }
4347 +
4348 + chan = &dmac->channel[req->req.channel];
4349 + chan->state = CH_STATE_BUSY;
4350 + chan->is_cyclic = 1;
4351 + chan->req_cyclic = req;
4352 +
4353 + /*
4354 + * We have marked the channel as busy, so no need to keep the
4355 + * lock as long as we only touch the channel-specific
4356 + * registers
4357 + */
4358 + spin_unlock_irqrestore(&dmac->lock, flags);
4359 +
4360 + /*
4361 + Setup
4362 + */
4363 + BUG_ON(req->buffer_size % req->periods);
4364 + /* printk(KERN_INFO "block_size = %lu, periods = %u\n", block_size, req->periods); */
4365 +
4366 + chan->nr_blocks = req->periods;
4367 +
4368 + ret = -EINVAL;
4369 + cfglo = cfghi = 0;
4370 + switch (req->direction) {
4371 + case DMA_DIR_MEM_TO_PERIPH:
4372 + direction = DMA_TO_DEVICE;
4373 + cfghi = req->periph_id << (43 - 32);
4374 + break;
4375 +
4376 + case DMA_DIR_PERIPH_TO_MEM:
4377 + direction = DMA_FROM_DEVICE;
4378 + cfghi = req->periph_id << (39 - 32);
4379 + break;
4380 + default:
4381 + goto out_unclaim_channel;
4382 + }
4383 +
4384 + chan->direction = direction;
4385 +
4386 + dmac_chan_writel_hi(dmac, req->req.channel, CFG, cfghi);
4387 + dmac_chan_writel_lo(dmac, req->req.channel, CFG, cfglo);
4388 +
4389 + ctlhi = block_size;
4390 + ctllo = ((req->direction << 20)
4391 + | (req->width << 4) | (req->width << 1)
4392 + | (1 << 0)); // interrupt enable
4393 +
4394 + {
4395 + struct dw_dma_lli *lli = NULL, *lli_prev = NULL;
4396 +
4397 + ret = -ENOMEM;
4398 + chan->block = allocate_blocks(dmac, req->periods);
4399 + if (!chan->block)
4400 + goto out_unclaim_channel;
4401 +
4402 + if (direction == DMA_TO_DEVICE)
4403 + ctllo |= 1 << 28 | 1 << 27 | 2 << 7;
4404 + else
4405 + ctllo |= 1 << 28 | 1 << 27 | 2 << 9;
4406 +
4407 + /*
4408 + * Set up a linked list items where each period gets
4409 + * an item. The linked list item for the last period
4410 + * points back to the star of the buffer making a
4411 + * cyclic buffer.
4412 + */
4413 + for (i = 0; i < req->periods; i++) {
4414 + lli = chan->block[i].lli_vaddr;
4415 + if (lli_prev) {
4416 + lli_prev->llp = chan->block[i].lli_dma_addr;
4417 + /* printk(KERN_INFO "lli[%d] (0x%p/0x%x): 0x%x 0x%x 0x%x 0x%x 0x%x\n",
4418 + i - 1, chan->block[i - 1].lli_vaddr,
4419 + chan->block[i - 1].lli_dma_addr,
4420 + lli_prev->sar, lli_prev->dar, lli_prev->llp,
4421 + lli_prev->ctllo, lli_prev->ctlhi);*/
4422 + }
4423 + lli->llp = 0;
4424 + lli->ctllo = ctllo;
4425 + lli->ctlhi = ctlhi;
4426 + if (direction == DMA_TO_DEVICE) {
4427 + lli->sar = req->buffer_start + i*(block_size << req->width);
4428 + lli->dar = req->data_reg;
4429 + } else {
4430 + lli->sar = req->data_reg;
4431 + lli->dar = req->buffer_start + i*(block_size << req->width);
4432 + }
4433 + lli_prev = lli;
4434 + }
4435 + lli->llp = chan->block[0].lli_dma_addr;
4436 +
4437 + /*printk(KERN_INFO "lli[%d] (0x%p/0x%x): 0x%x 0x%x 0x%x 0x%x 0x%x\n",
4438 + i - 1, chan->block[i - 1].lli_vaddr,
4439 + chan->block[i - 1].lli_dma_addr, lli_prev->sar,
4440 + lli_prev->dar, lli_prev->llp,
4441 + lli_prev->ctllo, lli_prev->ctlhi); */
4442 +
4443 + /*
4444 + * SAR, DAR and CTL are initialized from the LLI. We
4445 + * only have to enable the LLI bits in CTL.
4446 + */
4447 + dmac_chan_writel_lo(dmac, req->req.channel, LLP,
4448 + chan->block[0].lli_dma_addr);
4449 + dmac_chan_writel_lo(dmac, req->req.channel, CTL, 1 << 28 | 1 << 27);
4450 + }
4451 +
4452 + clear_channel_bit(dmac, MASK_XFER, req->req.channel);
4453 + set_channel_bit(dmac, MASK_ERROR, req->req.channel);
4454 + if (req->req.block_complete)
4455 + set_channel_bit(dmac, MASK_BLOCK, req->req.channel);
4456 + else
4457 + clear_channel_bit(dmac, MASK_BLOCK, req->req.channel);
4458 +
4459 + return 0;
4460 +
4461 +out_unclaim_channel:
4462 + chan->state = CH_STATE_ALLOCATED;
4463 + return ret;
4464 +}
4465 +
4466 +static int dmac_start_request(struct dma_controller *_dmac,
4467 + unsigned int channel)
4468 +{
4469 + struct dw_dma_controller *dmac = to_dw_dmac(_dmac);
4470 +
4471 + BUG_ON(channel >= DMAC_NR_CHANNELS);
4472 +
4473 + set_channel_bit(dmac, CH_EN, channel);
4474 +
4475 + return 0;
4476 +}
4477 +
4478 +static dma_addr_t dmac_get_current_pos(struct dma_controller *_dmac,
4479 + unsigned int channel)
4480 +{
4481 + struct dw_dma_controller *dmac = to_dw_dmac(_dmac);
4482 + struct dw_dma_channel *chan;
4483 + dma_addr_t current_pos;
4484 +
4485 + BUG_ON(channel >= DMAC_NR_CHANNELS);
4486 +
4487 + chan = &dmac->channel[channel];
4488 +
4489 + switch (chan->direction) {
4490 + case DMA_TO_DEVICE:
4491 + current_pos = dmac_chan_readl_lo(dmac, channel, SAR);
4492 + break;
4493 + case DMA_FROM_DEVICE:
4494 + current_pos = dmac_chan_readl_lo(dmac, channel, DAR);
4495 + break;
4496 + default:
4497 + return 0;
4498 + }
4499 +
4500 +
4501 + if (!current_pos) {
4502 + if (chan->is_cyclic) {
4503 + current_pos = chan->req_cyclic->buffer_start;
4504 + } else {
4505 + current_pos = chan->req_sg->sg->dma_address;
4506 + }
4507 + }
4508 +
4509 + return current_pos;
4510 +}
4511 +
4512 +
4513 +static int dmac_stop_request(struct dma_controller *_dmac,
4514 + unsigned int channel)
4515 +{
4516 + struct dw_dma_controller *dmac = to_dw_dmac(_dmac);
4517 + struct dw_dma_channel *chan;
4518 +
4519 + BUG_ON(channel >= DMAC_NR_CHANNELS);
4520 +
4521 + chan = &dmac->channel[channel];
4522 + pr_debug("stop: st%u s%08x d%08x l%08x ctl0x%08x:0x%08x\n",
4523 + chan->state, dmac_chan_readl_lo(dmac, channel, SAR),
4524 + dmac_chan_readl_lo(dmac, channel, DAR),
4525 + dmac_chan_readl_lo(dmac, channel, LLP),
4526 + dmac_chan_readl_hi(dmac, channel, CTL),
4527 + dmac_chan_readl_lo(dmac, channel, CTL));
4528 +
4529 + if (chan->state == CH_STATE_BUSY) {
4530 + clear_channel_bit(dmac, CH_EN, channel);
4531 + cleanup_channel(dmac, &dmac->channel[channel]);
4532 + }
4533 +
4534 + return 0;
4535 +}
4536 +
4537 +
4538 +static void dmac_block_complete(struct dw_dma_controller *dmac)
4539 +{
4540 + struct dw_dma_channel *chan;
4541 + unsigned long status, chanid;
4542 +
4543 + status = dmac_readl_lo(dmac, STATUS_BLOCK);
4544 +
4545 + while (status) {
4546 + struct dma_request *req;
4547 + chanid = __ffs(status);
4548 + chan = &dmac->channel[chanid];
4549 +
4550 + if (chan->is_cyclic) {
4551 + BUG_ON(!chan->req_cyclic
4552 + || !chan->req_cyclic->req.block_complete);
4553 + req = &chan->req_cyclic->req;
4554 + } else {
4555 + BUG_ON(!chan->req_sg || !chan->req_sg->req.block_complete);
4556 + req = &chan->req_sg->req;
4557 + }
4558 + dmac_writel_lo(dmac, CLEAR_BLOCK, 1 << chanid);
4559 + req->block_complete(req);
4560 + status = dmac_readl_lo(dmac, STATUS_BLOCK);
4561 + }
4562 +}
4563 +
4564 +static void dmac_xfer_complete(struct dw_dma_controller *dmac)
4565 +{
4566 + struct dw_dma_channel *chan;
4567 + struct dma_request *req;
4568 + unsigned long status, chanid;
4569 +
4570 + status = dmac_readl_lo(dmac, STATUS_XFER);
4571 +
4572 + while (status) {
4573 + chanid = __ffs(status);
4574 + chan = &dmac->channel[chanid];
4575 +
4576 + dmac_writel_lo(dmac, CLEAR_XFER, 1 << chanid);
4577 +
4578 + req = &chan->req_sg->req;
4579 + BUG_ON(!req);
4580 + cleanup_channel(dmac, chan);
4581 + if (req->xfer_complete)
4582 + req->xfer_complete(req);
4583 +
4584 + status = dmac_readl_lo(dmac, STATUS_XFER);
4585 + }
4586 +}
4587 +
4588 +static void dmac_error(struct dw_dma_controller *dmac)
4589 +{
4590 + struct dw_dma_channel *chan;
4591 + unsigned long status, chanid;
4592 +
4593 + status = dmac_readl_lo(dmac, STATUS_ERROR);
4594 +
4595 + while (status) {
4596 + struct dma_request *req;
4597 +
4598 + chanid = __ffs(status);
4599 + chan = &dmac->channel[chanid];
4600 +
4601 + dmac_writel_lo(dmac, CLEAR_ERROR, 1 << chanid);
4602 + clear_channel_bit(dmac, CH_EN, chanid);
4603 +
4604 + if (chan->is_cyclic) {
4605 + BUG_ON(!chan->req_cyclic);
4606 + req = &chan->req_cyclic->req;
4607 + } else {
4608 + BUG_ON(!chan->req_sg);
4609 + req = &chan->req_sg->req;
4610 + }
4611 +
4612 + cleanup_channel(dmac, chan);
4613 + if (req->error)
4614 + req->error(req);
4615 +
4616 + status = dmac_readl_lo(dmac, STATUS_XFER);
4617 + }
4618 +}
4619 +
4620 +static irqreturn_t dmac_interrupt(int irq, void *dev_id)
4621 +{
4622 + struct dw_dma_controller *dmac = dev_id;
4623 + unsigned long status;
4624 + int ret = IRQ_NONE;
4625 +
4626 + spin_lock(&dmac->lock);
4627 +
4628 + status = dmac_readl_lo(dmac, STATUS_INT);
4629 +
4630 + while (status) {
4631 + ret = IRQ_HANDLED;
4632 + if (status & 0x10)
4633 + dmac_error(dmac);
4634 + if (status & 0x02)
4635 + dmac_block_complete(dmac);
4636 + if (status & 0x01)
4637 + dmac_xfer_complete(dmac);
4638 +
4639 + status = dmac_readl_lo(dmac, STATUS_INT);
4640 + }
4641 +
4642 + spin_unlock(&dmac->lock);
4643 + return ret;
4644 +}
4645 +
4646 +static int __devinit dmac_probe(struct platform_device *pdev)
4647 +{
4648 + struct dw_dma_controller *dmac;
4649 + struct resource *regs;
4650 + int ret;
4651 +
4652 + regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
4653 + if (!regs)
4654 + return -ENXIO;
4655 +
4656 + dmac = kmalloc(sizeof(*dmac), GFP_KERNEL);
4657 + if (!dmac)
4658 + return -ENOMEM;
4659 + memset(dmac, 0, sizeof(*dmac));
4660 +
4661 + dmac->hclk = clk_get(&pdev->dev, "hclk");
4662 + if (IS_ERR(dmac->hclk)) {
4663 + ret = PTR_ERR(dmac->hclk);
4664 + goto out_free_dmac;
4665 + }
4666 + clk_enable(dmac->hclk);
4667 +
4668 + ret = -ENOMEM;
4669 + dmac->lli_pool = dma_pool_create("dmac", &pdev->dev,
4670 + sizeof(struct dw_dma_lli), 4, 0);
4671 + if (!dmac->lli_pool)
4672 + goto out_disable_clk;
4673 +
4674 + spin_lock_init(&dmac->lock);
4675 + dmac->dma.dev = &pdev->dev;
4676 + dmac->dma.alloc_channel = dmac_alloc_channel;
4677 + dmac->dma.release_channel = dmac_release_channel;
4678 + dmac->dma.prepare_request_sg = dmac_prepare_request_sg;
4679 + dmac->dma.prepare_request_cyclic = dmac_prepare_request_cyclic;
4680 + dmac->dma.start_request = dmac_start_request;
4681 + dmac->dma.stop_request = dmac_stop_request;
4682 + dmac->dma.get_current_pos = dmac_get_current_pos;
4683 +
4684 + dmac->regs = ioremap(regs->start, regs->end - regs->start + 1);
4685 + if (!dmac->regs)
4686 + goto out_free_pool;
4687 +
4688 + ret = request_irq(platform_get_irq(pdev, 0), dmac_interrupt,
4689 + IRQF_SAMPLE_RANDOM, pdev->name, dmac);
4690 + if (ret)
4691 + goto out_unmap_regs;
4692 +
4693 + /* Enable the DMA controller */
4694 + dmac_writel_lo(dmac, CFG, 1);
4695 +
4696 + register_dma_controller(&dmac->dma);
4697 +
4698 + printk(KERN_INFO
4699 + "dmac%d: DesignWare DMA controller at 0x%p irq %d\n",
4700 + dmac->dma.id, dmac->regs, platform_get_irq(pdev, 0));
4701 +
4702 + return 0;
4703 +
4704 +out_unmap_regs:
4705 + iounmap(dmac->regs);
4706 +out_free_pool:
4707 + dma_pool_destroy(dmac->lli_pool);
4708 +out_disable_clk:
4709 + clk_disable(dmac->hclk);
4710 + clk_put(dmac->hclk);
4711 +out_free_dmac:
4712 + kfree(dmac);
4713 + return ret;
4714 +}
4715 +
4716 +static struct platform_driver dmac_driver = {
4717 + .probe = dmac_probe,
4718 + .driver = {
4719 + .name = "dmaca",
4720 + },
4721 +};
4722 +
4723 +static int __init dmac_init(void)
4724 +{
4725 + return platform_driver_register(&dmac_driver);
4726 +}
4727 +subsys_initcall(dmac_init);
4728 +
4729 +static void __exit dmac_exit(void)
4730 +{
4731 + platform_driver_unregister(&dmac_driver);
4732 +}
4733 +module_exit(dmac_exit);
4734 +
4735 +MODULE_DESCRIPTION("Synopsys DesignWare DMA Controller driver");
4736 +MODULE_AUTHOR("Haavard Skinnemoen <hskinnemoen@atmel.com>");
4737 +MODULE_LICENSE("GPL");
4738 diff --exclude=.git -urN linux-2.6.25.6/arch/avr32/drivers/dw-dmac.h avr32-2.6/arch/avr32/drivers/dw-dmac.h
4739 --- linux-2.6.25.6/arch/avr32/drivers/dw-dmac.h 1970-01-01 01:00:00.000000000 +0100
4740 +++ avr32-2.6/arch/avr32/drivers/dw-dmac.h 2008-06-12 15:09:38.719815350 +0200
4741 @@ -0,0 +1,42 @@
4742 +/*
4743 + * Driver for the Synopsys DesignWare DMA Controller
4744 + *
4745 + * Copyright (C) 2005-2006 Atmel Corporation
4746 + *
4747 + * This program is free software; you can redistribute it and/or modify
4748 + * it under the terms of the GNU General Public License version 2 as
4749 + * published by the Free Software Foundation.
4750 + */
4751 +#ifndef __AVR32_DW_DMAC_H__
4752 +#define __AVR32_DW_DMAC_H__
4753 +
4754 +#define DW_DMAC_CFG 0x398
4755 +#define DW_DMAC_CH_EN 0x3a0
4756 +
4757 +#define DW_DMAC_STATUS_XFER 0x2e8
4758 +#define DW_DMAC_STATUS_BLOCK 0x2f0
4759 +#define DW_DMAC_STATUS_ERROR 0x308
4760 +
4761 +#define DW_DMAC_MASK_XFER 0x310
4762 +#define DW_DMAC_MASK_BLOCK 0x318
4763 +#define DW_DMAC_MASK_ERROR 0x330
4764 +
4765 +#define DW_DMAC_CLEAR_XFER 0x338
4766 +#define DW_DMAC_CLEAR_BLOCK 0x340
4767 +#define DW_DMAC_CLEAR_ERROR 0x358
4768 +
4769 +#define DW_DMAC_STATUS_INT 0x360
4770 +
4771 +#define DW_DMAC_CHAN_SAR 0x000
4772 +#define DW_DMAC_CHAN_DAR 0x008
4773 +#define DW_DMAC_CHAN_LLP 0x010
4774 +#define DW_DMAC_CHAN_CTL 0x018
4775 +#define DW_DMAC_CHAN_SSTAT 0x020
4776 +#define DW_DMAC_CHAN_DSTAT 0x028
4777 +#define DW_DMAC_CHAN_SSTATAR 0x030
4778 +#define DW_DMAC_CHAN_DSTATAR 0x038
4779 +#define DW_DMAC_CHAN_CFG 0x040
4780 +#define DW_DMAC_CHAN_SGR 0x048
4781 +#define DW_DMAC_CHAN_DSR 0x050
4782 +
4783 +#endif /* __AVR32_DW_DMAC_H__ */
4784 diff --exclude=.git -urN linux-2.6.25.6/arch/avr32/drivers/Makefile avr32-2.6/arch/avr32/drivers/Makefile
4785 --- linux-2.6.25.6/arch/avr32/drivers/Makefile 1970-01-01 01:00:00.000000000 +0100
4786 +++ avr32-2.6/arch/avr32/drivers/Makefile 2008-06-12 15:09:38.719815350 +0200
4787 @@ -0,0 +1 @@
4788 +obj-$(CONFIG_DW_DMAC) += dw-dmac.o
4789 diff --exclude=.git -urN linux-2.6.25.6/arch/avr32/Kconfig avr32-2.6/arch/avr32/Kconfig
4790 --- linux-2.6.25.6/arch/avr32/Kconfig 2008-06-09 20:27:19.000000000 +0200
4791 +++ avr32-2.6/arch/avr32/Kconfig 2008-06-12 15:09:38.711815728 +0200
4792 @@ -47,6 +47,9 @@
4793 config GENERIC_TIME
4794 def_bool y
4795
4796 +config GENERIC_CLOCKEVENTS
4797 + def_bool y
4798 +
4799 config RWSEM_XCHGADD_ALGORITHM
4800 def_bool n
4801
4802 @@ -70,6 +73,8 @@
4803
4804 menu "System Type and features"
4805
4806 +source "kernel/time/Kconfig"
4807 +
4808 config SUBARCH_AVR32B
4809 bool
4810 config MMU
4811 @@ -83,6 +88,7 @@
4812 select MMU
4813 select PERFORMANCE_COUNTERS
4814 select HAVE_GPIO_LIB
4815 + select GENERIC_ALLOCATOR
4816
4817 #
4818 # CPU types
4819 @@ -117,6 +123,9 @@
4820 if BOARD_ATSTK1000
4821 source "arch/avr32/boards/atstk1000/Kconfig"
4822 endif
4823 +if BOARD_ATNGW100
4824 +source "arch/avr32/boards/atngw100/Kconfig"
4825 +endif
4826
4827 choice
4828 prompt "Boot loader type"
4829 @@ -180,6 +189,10 @@
4830 be dumped to the console when a Non-Maskable Interrupt
4831 happens.
4832
4833 +config DW_DMAC
4834 + tristate "Synopsys DesignWare DMA Controller support"
4835 + default y if CPU_AT32AP7000
4836 +
4837 # FPU emulation goes here
4838
4839 source "kernel/Kconfig.hz"
4840 @@ -196,6 +209,11 @@
4841
4842 menu "Power management options"
4843
4844 +config ARCH_SUSPEND_POSSIBLE
4845 + def_bool y
4846 +
4847 +source "kernel/power/Kconfig"
4848 +
4849 menu "CPU Frequency scaling"
4850
4851 source "drivers/cpufreq/Kconfig"
4852 diff --exclude=.git -urN linux-2.6.25.6/arch/avr32/kernel/avr32_ksyms.c avr32-2.6/arch/avr32/kernel/avr32_ksyms.c
4853 --- linux-2.6.25.6/arch/avr32/kernel/avr32_ksyms.c 2008-06-09 20:27:19.000000000 +0200
4854 +++ avr32-2.6/arch/avr32/kernel/avr32_ksyms.c 2008-06-12 15:03:55.883815569 +0200
4855 @@ -29,7 +29,9 @@
4856 */
4857 EXPORT_SYMBOL(memset);
4858 EXPORT_SYMBOL(memcpy);
4859 +
4860 EXPORT_SYMBOL(clear_page);
4861 +EXPORT_SYMBOL(copy_page);
4862
4863 /*
4864 * Userspace access stuff.
4865 @@ -41,6 +43,8 @@
4866 EXPORT_SYMBOL(__strncpy_from_user);
4867 EXPORT_SYMBOL(clear_user);
4868 EXPORT_SYMBOL(__clear_user);
4869 +EXPORT_SYMBOL(strnlen_user);
4870 +
4871 EXPORT_SYMBOL(csum_partial);
4872 EXPORT_SYMBOL(csum_partial_copy_generic);
4873
4874 diff --exclude=.git -urN linux-2.6.25.6/arch/avr32/kernel/dma-controller.c avr32-2.6/arch/avr32/kernel/dma-controller.c
4875 --- linux-2.6.25.6/arch/avr32/kernel/dma-controller.c 1970-01-01 01:00:00.000000000 +0100
4876 +++ avr32-2.6/arch/avr32/kernel/dma-controller.c 2008-06-12 15:09:38.719815350 +0200
4877 @@ -0,0 +1,34 @@
4878 +/*
4879 + * Preliminary DMA controller framework for AVR32
4880 + *
4881 + * Copyright (C) 2005-2006 Atmel Corporation
4882 + *
4883 + * This program is free software; you can redistribute it and/or modify
4884 + * it under the terms of the GNU General Public License version 2 as
4885 + * published by the Free Software Foundation.
4886 + */
4887 +#include <asm/dma-controller.h>
4888 +
4889 +static LIST_HEAD(controllers);
4890 +
4891 +int register_dma_controller(struct dma_controller *dmac)
4892 +{
4893 + static int next_id;
4894 +
4895 + dmac->id = next_id++;
4896 + list_add_tail(&dmac->list, &controllers);
4897 +
4898 + return 0;
4899 +}
4900 +EXPORT_SYMBOL(register_dma_controller);
4901 +
4902 +struct dma_controller *find_dma_controller(int id)
4903 +{
4904 + struct dma_controller *dmac;
4905 +
4906 + list_for_each_entry(dmac, &controllers, list)
4907 + if (dmac->id == id)
4908 + return dmac;
4909 + return NULL;
4910 +}
4911 +EXPORT_SYMBOL(find_dma_controller);
4912 diff --exclude=.git -urN linux-2.6.25.6/arch/avr32/kernel/entry-avr32b.S avr32-2.6/arch/avr32/kernel/entry-avr32b.S
4913 --- linux-2.6.25.6/arch/avr32/kernel/entry-avr32b.S 2008-06-09 20:27:19.000000000 +0200
4914 +++ avr32-2.6/arch/avr32/kernel/entry-avr32b.S 2008-06-12 15:03:55.883815569 +0200
4915 @@ -741,26 +741,6 @@
4916
4917 .section .irq.text,"ax",@progbits
4918
4919 -.global cpu_idle_sleep
4920 -cpu_idle_sleep:
4921 - mask_interrupts
4922 - get_thread_info r8
4923 - ld.w r9, r8[TI_flags]
4924 - bld r9, TIF_NEED_RESCHED
4925 - brcs cpu_idle_enable_int_and_exit
4926 - sbr r9, TIF_CPU_GOING_TO_SLEEP
4927 - st.w r8[TI_flags], r9
4928 - unmask_interrupts
4929 - sleep 0
4930 -cpu_idle_skip_sleep:
4931 - mask_interrupts
4932 - ld.w r9, r8[TI_flags]
4933 - cbr r9, TIF_CPU_GOING_TO_SLEEP
4934 - st.w r8[TI_flags], r9
4935 -cpu_idle_enable_int_and_exit:
4936 - unmask_interrupts
4937 - retal r12
4938 -
4939 .global irq_level0
4940 .global irq_level1
4941 .global irq_level2
4942 diff --exclude=.git -urN linux-2.6.25.6/arch/avr32/kernel/Makefile avr32-2.6/arch/avr32/kernel/Makefile
4943 --- linux-2.6.25.6/arch/avr32/kernel/Makefile 2008-06-09 20:27:19.000000000 +0200
4944 +++ avr32-2.6/arch/avr32/kernel/Makefile 2008-06-12 15:09:38.719815350 +0200
4945 @@ -9,6 +9,7 @@
4946 obj-y += setup.o traps.o semaphore.o ocd.o ptrace.o
4947 obj-y += signal.o sys_avr32.o process.o time.o
4948 obj-y += init_task.o switch_to.o cpu.o
4949 +obj-y += dma-controller.o
4950 obj-$(CONFIG_MODULES) += module.o avr32_ksyms.o
4951 obj-$(CONFIG_KPROBES) += kprobes.o
4952 obj-$(CONFIG_STACKTRACE) += stacktrace.o
4953 diff --exclude=.git -urN linux-2.6.25.6/arch/avr32/kernel/process.c avr32-2.6/arch/avr32/kernel/process.c
4954 --- linux-2.6.25.6/arch/avr32/kernel/process.c 2008-06-09 20:27:19.000000000 +0200
4955 +++ avr32-2.6/arch/avr32/kernel/process.c 2008-06-12 15:03:55.887814682 +0200
4956 @@ -18,11 +18,11 @@
4957 #include <asm/sysreg.h>
4958 #include <asm/ocd.h>
4959
4960 +#include <asm/arch/pm.h>
4961 +
4962 void (*pm_power_off)(void) = NULL;
4963 EXPORT_SYMBOL(pm_power_off);
4964
4965 -extern void cpu_idle_sleep(void);
4966 -
4967 /*
4968 * This file handles the architecture-dependent parts of process handling..
4969 */
4970 @@ -54,6 +54,8 @@
4971
4972 void machine_power_off(void)
4973 {
4974 + if (pm_power_off)
4975 + pm_power_off();
4976 }
4977
4978 void machine_restart(char *cmd)
4979 diff --exclude=.git -urN linux-2.6.25.6/arch/avr32/kernel/setup.c avr32-2.6/arch/avr32/kernel/setup.c
4980 --- linux-2.6.25.6/arch/avr32/kernel/setup.c 2008-06-09 20:27:19.000000000 +0200
4981 +++ avr32-2.6/arch/avr32/kernel/setup.c 2008-06-12 15:03:55.887814682 +0200
4982 @@ -274,6 +274,8 @@
4983 printk(KERN_WARNING
4984 "Failed to allocate framebuffer memory\n");
4985 fbmem_size = 0;
4986 + } else {
4987 + memset(__va(fbmem_start), 0, fbmem_size);
4988 }
4989 }
4990
4991 diff --exclude=.git -urN linux-2.6.25.6/arch/avr32/kernel/signal.c avr32-2.6/arch/avr32/kernel/signal.c
4992 --- linux-2.6.25.6/arch/avr32/kernel/signal.c 2008-06-09 20:27:19.000000000 +0200
4993 +++ avr32-2.6/arch/avr32/kernel/signal.c 2008-06-12 15:09:38.719815350 +0200
4994 @@ -93,6 +93,9 @@
4995 if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
4996 goto badframe;
4997
4998 + if (do_sigaltstack(&frame->uc.uc_stack, NULL, regs->sp) == -EFAULT)
4999 + goto badframe;
5000 +
5001 pr_debug("Context restored: pc = %08lx, lr = %08lx, sp = %08lx\n",
5002 regs->pc, regs->lr, regs->sp);
5003
5004 diff --exclude=.git -urN linux-2.6.25.6/arch/avr32/kernel/time.c avr32-2.6/arch/avr32/kernel/time.c
5005 --- linux-2.6.25.6/arch/avr32/kernel/time.c 2008-06-09 20:27:19.000000000 +0200
5006 +++ avr32-2.6/arch/avr32/kernel/time.c 2008-06-12 15:03:55.887814682 +0200
5007 @@ -1,16 +1,12 @@
5008 /*
5009 * Copyright (C) 2004-2007 Atmel Corporation
5010 *
5011 - * Based on MIPS implementation arch/mips/kernel/time.c
5012 - * Copyright 2001 MontaVista Software Inc.
5013 - *
5014 * This program is free software; you can redistribute it and/or modify
5015 * it under the terms of the GNU General Public License version 2 as
5016 * published by the Free Software Foundation.
5017 */
5018 -
5019 #include <linux/clk.h>
5020 -#include <linux/clocksource.h>
5021 +#include <linux/clockchips.h>
5022 #include <linux/time.h>
5023 #include <linux/module.h>
5024 #include <linux/interrupt.h>
5025 @@ -27,207 +23,133 @@
5026 #include <asm/io.h>
5027 #include <asm/sections.h>
5028
5029 -/* how many counter cycles in a jiffy? */
5030 -static u32 cycles_per_jiffy;
5031 +#include <asm/arch/pm.h>
5032
5033 -/* the count value for the next timer interrupt */
5034 -static u32 expirelo;
5035
5036 -cycle_t __weak read_cycle_count(void)
5037 +static cycle_t read_cycle_count(void)
5038 {
5039 return (cycle_t)sysreg_read(COUNT);
5040 }
5041
5042 -struct clocksource __weak clocksource_avr32 = {
5043 - .name = "avr32",
5044 - .rating = 350,
5045 +/*
5046 + * The architectural cycle count registers are a fine clocksource unless
5047 + * the system idle loop use sleep states like "idle": the CPU cycles
5048 + * measured by COUNT (and COMPARE) don't happen during sleep states.
5049 + * Their duration also changes if cpufreq changes the CPU clock rate.
5050 + * So we rate the clocksource using COUNT as very low quality.
5051 + */
5052 +static struct clocksource counter = {
5053 + .name = "avr32_counter",
5054 + .rating = 50,
5055 .read = read_cycle_count,
5056 .mask = CLOCKSOURCE_MASK(32),
5057 .shift = 16,
5058 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
5059 };
5060
5061 -irqreturn_t __weak timer_interrupt(int irq, void *dev_id);
5062 -
5063 -struct irqaction timer_irqaction = {
5064 - .handler = timer_interrupt,
5065 - .flags = IRQF_DISABLED,
5066 - .name = "timer",
5067 -};
5068 -
5069 -/*
5070 - * By default we provide the null RTC ops
5071 - */
5072 -static unsigned long null_rtc_get_time(void)
5073 +static irqreturn_t timer_interrupt(int irq, void *dev_id)
5074 {
5075 - return mktime(2007, 1, 1, 0, 0, 0);
5076 -}
5077 -
5078 -static int null_rtc_set_time(unsigned long sec)
5079 -{
5080 - return 0;
5081 -}
5082 + struct clock_event_device *evdev = dev_id;
5083
5084 -static unsigned long (*rtc_get_time)(void) = null_rtc_get_time;
5085 -static int (*rtc_set_time)(unsigned long) = null_rtc_set_time;
5086 -
5087 -static void avr32_timer_ack(void)
5088 -{
5089 - u32 count;
5090 -
5091 - /* Ack this timer interrupt and set the next one */
5092 - expirelo += cycles_per_jiffy;
5093 - /* setting COMPARE to 0 stops the COUNT-COMPARE */
5094 - if (expirelo == 0) {
5095 - sysreg_write(COMPARE, expirelo + 1);
5096 - } else {
5097 - sysreg_write(COMPARE, expirelo);
5098 - }
5099 + /*
5100 + * Disable the interrupt until the clockevent subsystem
5101 + * reprograms it.
5102 + */
5103 + sysreg_write(COMPARE, 0);
5104
5105 - /* Check to see if we have missed any timer interrupts */
5106 - count = sysreg_read(COUNT);
5107 - if ((count - expirelo) < 0x7fffffff) {
5108 - expirelo = count + cycles_per_jiffy;
5109 - sysreg_write(COMPARE, expirelo);
5110 - }
5111 + evdev->event_handler(evdev);
5112 + return IRQ_HANDLED;
5113 }
5114
5115 -int __weak avr32_hpt_init(void)
5116 -{
5117 - int ret;
5118 - unsigned long mult, shift, count_hz;
5119 -
5120 - count_hz = clk_get_rate(boot_cpu_data.clk);
5121 - shift = clocksource_avr32.shift;
5122 - mult = clocksource_hz2mult(count_hz, shift);
5123 - clocksource_avr32.mult = mult;
5124 -
5125 - {
5126 - u64 tmp;
5127 -
5128 - tmp = TICK_NSEC;
5129 - tmp <<= shift;
5130 - tmp += mult / 2;
5131 - do_div(tmp, mult);
5132 -
5133 - cycles_per_jiffy = tmp;
5134 - }
5135 +static struct irqaction timer_irqaction = {
5136 + .handler = timer_interrupt,
5137 + .flags = IRQF_TIMER | IRQF_DISABLED,
5138 + .name = "avr32_comparator",
5139 +};
5140
5141 - ret = setup_irq(0, &timer_irqaction);
5142 - if (ret) {
5143 - pr_debug("timer: could not request IRQ 0: %d\n", ret);
5144 - return -ENODEV;
5145 - }
5146 +static int comparator_next_event(unsigned long delta,
5147 + struct clock_event_device *evdev)
5148 +{
5149 + unsigned long flags;
5150
5151 - printk(KERN_INFO "timer: AT32AP COUNT-COMPARE at irq 0, "
5152 - "%lu.%03lu MHz\n",
5153 - ((count_hz + 500) / 1000) / 1000,
5154 - ((count_hz + 500) / 1000) % 1000);
5155 + raw_local_irq_save(flags);
5156
5157 - return 0;
5158 -}
5159 + /* The time to read COUNT then update COMPARE must be less
5160 + * than the min_delta_ns value for this clockevent source.
5161 + */
5162 + sysreg_write(COMPARE, (sysreg_read(COUNT) + delta) ? : 1);
5163
5164 -/*
5165 - * Taken from MIPS c0_hpt_timer_init().
5166 - *
5167 - * The reason COUNT is written twice is probably to make sure we don't get any
5168 - * timer interrupts while we are messing with the counter.
5169 - */
5170 -int __weak avr32_hpt_start(void)
5171 -{
5172 - u32 count = sysreg_read(COUNT);
5173 - expirelo = (count / cycles_per_jiffy + 1) * cycles_per_jiffy;
5174 - sysreg_write(COUNT, expirelo - cycles_per_jiffy);
5175 - sysreg_write(COMPARE, expirelo);
5176 - sysreg_write(COUNT, count);
5177 + raw_local_irq_restore(flags);
5178
5179 return 0;
5180 }
5181
5182 -/*
5183 - * local_timer_interrupt() does profiling and process accounting on a
5184 - * per-CPU basis.
5185 - *
5186 - * In UP mode, it is invoked from the (global) timer_interrupt.
5187 - */
5188 -void local_timer_interrupt(int irq, void *dev_id)
5189 +static void comparator_mode(enum clock_event_mode mode,
5190 + struct clock_event_device *evdev)
5191 {
5192 - if (current->pid)
5193 - profile_tick(CPU_PROFILING);
5194 - update_process_times(user_mode(get_irq_regs()));
5195 + switch (mode) {
5196 + case CLOCK_EVT_MODE_ONESHOT:
5197 + pr_debug("%s: start\n", evdev->name);
5198 + /* FALLTHROUGH */
5199 + case CLOCK_EVT_MODE_RESUME:
5200 + cpu_disable_idle_sleep();
5201 + break;
5202 + case CLOCK_EVT_MODE_UNUSED:
5203 + case CLOCK_EVT_MODE_SHUTDOWN:
5204 + sysreg_write(COMPARE, 0);
5205 + pr_debug("%s: stop\n", evdev->name);
5206 + cpu_enable_idle_sleep();
5207 + break;
5208 + default:
5209 + BUG();
5210 + }
5211 }
5212
5213 -irqreturn_t __weak timer_interrupt(int irq, void *dev_id)
5214 -{
5215 - /* ack timer interrupt and try to set next interrupt */
5216 - avr32_timer_ack();
5217 -
5218 - /*
5219 - * Call the generic timer interrupt handler
5220 - */
5221 - write_seqlock(&xtime_lock);
5222 - do_timer(1);
5223 - write_sequnlock(&xtime_lock);
5224 -
5225 - /*
5226 - * In UP mode, we call local_timer_interrupt() to do profiling
5227 - * and process accounting.
5228 - *
5229 - * SMP is not supported yet.
5230 - */
5231 - local_timer_interrupt(irq, dev_id);
5232 -
5233 - return IRQ_HANDLED;
5234 -}
5235 +static struct clock_event_device comparator = {
5236 + .name = "avr32_comparator",
5237 + .features = CLOCK_EVT_FEAT_ONESHOT,
5238 + .shift = 16,
5239 + .rating = 50,
5240 + .cpumask = CPU_MASK_CPU0,
5241 + .set_next_event = comparator_next_event,
5242 + .set_mode = comparator_mode,
5243 +};
5244
5245 void __init time_init(void)
5246 {
5247 + unsigned long counter_hz;
5248 int ret;
5249
5250 - /*
5251 - * Make sure we don't get any COMPARE interrupts before we can
5252 - * handle them.
5253 - */
5254 - sysreg_write(COMPARE, 0);
5255 -
5256 - xtime.tv_sec = rtc_get_time();
5257 + xtime.tv_sec = mktime(2007, 1, 1, 0, 0, 0);
5258 xtime.tv_nsec = 0;
5259
5260 set_normalized_timespec(&wall_to_monotonic,
5261 -xtime.tv_sec, -xtime.tv_nsec);
5262
5263 - ret = avr32_hpt_init();
5264 - if (ret) {
5265 - pr_debug("timer: failed setup: %d\n", ret);
5266 - return;
5267 - }
5268 + /* figure rate for counter */
5269 + counter_hz = clk_get_rate(boot_cpu_data.clk);
5270 + counter.mult = clocksource_hz2mult(counter_hz, counter.shift);
5271
5272 - ret = clocksource_register(&clocksource_avr32);
5273 + ret = clocksource_register(&counter);
5274 if (ret)
5275 pr_debug("timer: could not register clocksource: %d\n", ret);
5276
5277 - ret = avr32_hpt_start();
5278 - if (ret) {
5279 - pr_debug("timer: failed starting: %d\n", ret);
5280 - return;
5281 - }
5282 -}
5283 + /* setup COMPARE clockevent */
5284 + comparator.mult = div_sc(counter_hz, NSEC_PER_SEC, comparator.shift);
5285 + comparator.max_delta_ns = clockevent_delta2ns((u32)~0, &comparator);
5286 + comparator.min_delta_ns = clockevent_delta2ns(50, &comparator) + 1;
5287
5288 -static struct sysdev_class timer_class = {
5289 - .name = "timer",
5290 -};
5291 + sysreg_write(COMPARE, 0);
5292 + timer_irqaction.dev_id = &comparator;
5293
5294 -static struct sys_device timer_device = {
5295 - .id = 0,
5296 - .cls = &timer_class,
5297 -};
5298 + ret = setup_irq(0, &timer_irqaction);
5299 + if (ret)
5300 + pr_debug("timer: could not request IRQ 0: %d\n", ret);
5301 + else {
5302 + clockevents_register_device(&comparator);
5303
5304 -static int __init init_timer_sysfs(void)
5305 -{
5306 - int err = sysdev_class_register(&timer_class);
5307 - if (!err)
5308 - err = sysdev_register(&timer_device);
5309 - return err;
5310 + pr_info("%s: irq 0, %lu.%03lu MHz\n", comparator.name,
5311 + ((counter_hz + 500) / 1000) / 1000,
5312 + ((counter_hz + 500) / 1000) % 1000);
5313 + }
5314 }
5315 -
5316 -device_initcall(init_timer_sysfs);
5317 diff --exclude=.git -urN linux-2.6.25.6/arch/avr32/lib/io-readsb.S avr32-2.6/arch/avr32/lib/io-readsb.S
5318 --- linux-2.6.25.6/arch/avr32/lib/io-readsb.S 2008-06-09 20:27:19.000000000 +0200
5319 +++ avr32-2.6/arch/avr32/lib/io-readsb.S 2008-06-12 15:09:38.719815350 +0200
5320 @@ -41,7 +41,7 @@
5321 2: sub r10, -4
5322 reteq r12
5323
5324 -3: ld.uh r8, r12[0]
5325 +3: ld.ub r8, r12[0]
5326 sub r10, 1
5327 st.b r11++, r8
5328 brne 3b
5329 diff --exclude=.git -urN linux-2.6.25.6/arch/avr32/mach-at32ap/at32ap700x.c avr32-2.6/arch/avr32/mach-at32ap/at32ap700x.c
5330 --- linux-2.6.25.6/arch/avr32/mach-at32ap/at32ap700x.c 2008-06-09 20:27:19.000000000 +0200
5331 +++ avr32-2.6/arch/avr32/mach-at32ap/at32ap700x.c 2008-06-12 15:09:38.723815860 +0200
5332 @@ -6,11 +6,13 @@
5333 * published by the Free Software Foundation.
5334 */
5335 #include <linux/clk.h>
5336 +#include <linux/delay.h>
5337 #include <linux/fb.h>
5338 #include <linux/init.h>
5339 #include <linux/platform_device.h>
5340 #include <linux/dma-mapping.h>
5341 #include <linux/spi/spi.h>
5342 +#include <linux/usb/atmel_usba_udc.h>
5343
5344 #include <asm/io.h>
5345 #include <asm/irq.h>
5346 @@ -18,6 +20,7 @@
5347 #include <asm/arch/at32ap700x.h>
5348 #include <asm/arch/board.h>
5349 #include <asm/arch/portmux.h>
5350 +#include <asm/arch/sram.h>
5351
5352 #include <video/atmel_lcdc.h>
5353
5354 @@ -91,25 +94,18 @@
5355
5356 static DEFINE_SPINLOCK(pm_lock);
5357
5358 -unsigned long at32ap7000_osc_rates[3] = {
5359 - [0] = 32768,
5360 - /* FIXME: these are ATSTK1002-specific */
5361 - [1] = 20000000,
5362 - [2] = 12000000,
5363 -};
5364 +static struct clk osc0;
5365 +static struct clk osc1;
5366
5367 static unsigned long osc_get_rate(struct clk *clk)
5368 {
5369 - return at32ap7000_osc_rates[clk->index];
5370 + return at32_board_osc_rates[clk->index];
5371 }
5372
5373 static unsigned long pll_get_rate(struct clk *clk, unsigned long control)
5374 {
5375 unsigned long div, mul, rate;
5376
5377 - if (!(control & PM_BIT(PLLEN)))
5378 - return 0;
5379 -
5380 div = PM_BFEXT(PLLDIV, control) + 1;
5381 mul = PM_BFEXT(PLLMUL, control) + 1;
5382
5383 @@ -120,6 +116,71 @@
5384 return rate;
5385 }
5386
5387 +static long pll_set_rate(struct clk *clk, unsigned long rate,
5388 + u32 *pll_ctrl)
5389 +{
5390 + unsigned long mul;
5391 + unsigned long mul_best_fit = 0;
5392 + unsigned long div;
5393 + unsigned long div_min;
5394 + unsigned long div_max;
5395 + unsigned long div_best_fit = 0;
5396 + unsigned long base;
5397 + unsigned long pll_in;
5398 + unsigned long actual = 0;
5399 + unsigned long rate_error;
5400 + unsigned long rate_error_prev = ~0UL;
5401 + u32 ctrl;
5402 +
5403 + /* Rate must be between 80 MHz and 200 Mhz. */
5404 + if (rate < 80000000UL || rate > 200000000UL)
5405 + return -EINVAL;
5406 +
5407 + ctrl = PM_BF(PLLOPT, 4);
5408 + base = clk->parent->get_rate(clk->parent);
5409 +
5410 + /* PLL input frequency must be between 6 MHz and 32 MHz. */
5411 + div_min = DIV_ROUND_UP(base, 32000000UL);
5412 + div_max = base / 6000000UL;
5413 +
5414 + if (div_max < div_min)
5415 + return -EINVAL;
5416 +
5417 + for (div = div_min; div <= div_max; div++) {
5418 + pll_in = (base + div / 2) / div;
5419 + mul = (rate + pll_in / 2) / pll_in;
5420 +
5421 + if (mul == 0)
5422 + continue;
5423 +
5424 + actual = pll_in * mul;
5425 + rate_error = abs(actual - rate);
5426 +
5427 + if (rate_error < rate_error_prev) {
5428 + mul_best_fit = mul;
5429 + div_best_fit = div;
5430 + rate_error_prev = rate_error;
5431 + }
5432 +
5433 + if (rate_error == 0)
5434 + break;
5435 + }
5436 +
5437 + if (div_best_fit == 0)
5438 + return -EINVAL;
5439 +
5440 + ctrl |= PM_BF(PLLMUL, mul_best_fit - 1);
5441 + ctrl |= PM_BF(PLLDIV, div_best_fit - 1);
5442 + ctrl |= PM_BF(PLLCOUNT, 16);
5443 +
5444 + if (clk->parent == &osc1)
5445 + ctrl |= PM_BIT(PLLOSC);
5446 +
5447 + *pll_ctrl = ctrl;
5448 +
5449 + return actual;
5450 +}
5451 +
5452 static unsigned long pll0_get_rate(struct clk *clk)
5453 {
5454 u32 control;
5455 @@ -129,6 +190,41 @@
5456 return pll_get_rate(clk, control);
5457 }
5458
5459 +static void pll1_mode(struct clk *clk, int enabled)
5460 +{
5461 + unsigned long timeout;
5462 + u32 status;
5463 + u32 ctrl;
5464 +
5465 + ctrl = pm_readl(PLL1);
5466 +
5467 + if (enabled) {
5468 + if (!PM_BFEXT(PLLMUL, ctrl) && !PM_BFEXT(PLLDIV, ctrl)) {
5469 + pr_debug("clk %s: failed to enable, rate not set\n",
5470 + clk->name);
5471 + return;
5472 + }
5473 +
5474 + ctrl |= PM_BIT(PLLEN);
5475 + pm_writel(PLL1, ctrl);
5476 +
5477 + /* Wait for PLL lock. */
5478 + for (timeout = 10000; timeout; timeout--) {
5479 + status = pm_readl(ISR);
5480 + if (status & PM_BIT(LOCK1))
5481 + break;
5482 + udelay(10);
5483 + }
5484 +
5485 + if (!(status & PM_BIT(LOCK1)))
5486 + printk(KERN_ERR "clk %s: timeout waiting for lock\n",
5487 + clk->name);
5488 + } else {
5489 + ctrl &= ~PM_BIT(PLLEN);
5490 + pm_writel(PLL1, ctrl);
5491 + }
5492 +}
5493 +
5494 static unsigned long pll1_get_rate(struct clk *clk)
5495 {
5496 u32 control;
5497 @@ -138,6 +234,49 @@
5498 return pll_get_rate(clk, control);
5499 }
5500
5501 +static long pll1_set_rate(struct clk *clk, unsigned long rate, int apply)
5502 +{
5503 + u32 ctrl = 0;
5504 + unsigned long actual_rate;
5505 +
5506 + actual_rate = pll_set_rate(clk, rate, &ctrl);
5507 +
5508 + if (apply) {
5509 + if (actual_rate != rate)
5510 + return -EINVAL;
5511 + if (clk->users > 0)
5512 + return -EBUSY;
5513 + pr_debug(KERN_INFO "clk %s: new rate %lu (actual rate %lu)\n",
5514 + clk->name, rate, actual_rate);
5515 + pm_writel(PLL1, ctrl);
5516 + }
5517 +
5518 + return actual_rate;
5519 +}
5520 +
5521 +static int pll1_set_parent(struct clk *clk, struct clk *parent)
5522 +{
5523 + u32 ctrl;
5524 +
5525 + if (clk->users > 0)
5526 + return -EBUSY;
5527 +
5528 + ctrl = pm_readl(PLL1);
5529 + WARN_ON(ctrl & PM_BIT(PLLEN));
5530 +
5531 + if (parent == &osc0)
5532 + ctrl &= ~PM_BIT(PLLOSC);
5533 + else if (parent == &osc1)
5534 + ctrl |= PM_BIT(PLLOSC);
5535 + else
5536 + return -EINVAL;
5537 +
5538 + pm_writel(PLL1, ctrl);
5539 + clk->parent = parent;
5540 +
5541 + return 0;
5542 +}
5543 +
5544 /*
5545 * The AT32AP7000 has five primary clock sources: One 32kHz
5546 * oscillator, two crystal oscillators and two PLLs.
5547 @@ -166,7 +305,10 @@
5548 };
5549 static struct clk pll1 = {
5550 .name = "pll1",
5551 + .mode = pll1_mode,
5552 .get_rate = pll1_get_rate,
5553 + .set_rate = pll1_set_rate,
5554 + .set_parent = pll1_set_parent,
5555 .parent = &osc0,
5556 };
5557
5558 @@ -534,6 +676,14 @@
5559 .users = 1,
5560 .index = 3,
5561 };
5562 +static struct clk sdramc_clk = {
5563 + .name = "sdramc_clk",
5564 + .parent = &pbb_clk,
5565 + .mode = pbb_clk_mode,
5566 + .get_rate = pbb_clk_get_rate,
5567 + .users = 1,
5568 + .index = 14,
5569 +};
5570
5571 static struct resource smc0_resource[] = {
5572 PBMEM(0xfff03400),
5573 @@ -605,19 +755,32 @@
5574 }
5575
5576 /* --------------------------------------------------------------------
5577 - * System Timer/Counter (TC)
5578 + * Timer/Counter (TC)
5579 * -------------------------------------------------------------------- */
5580 -static struct resource at32_systc0_resource[] = {
5581 +
5582 +static struct resource at32_tcb0_resource[] = {
5583 PBMEM(0xfff00c00),
5584 IRQ(22),
5585 };
5586 -struct platform_device at32_systc0_device = {
5587 - .name = "systc",
5588 +static struct platform_device at32_tcb0_device = {
5589 + .name = "atmel_tcb",
5590 .id = 0,
5591 - .resource = at32_systc0_resource,
5592 - .num_resources = ARRAY_SIZE(at32_systc0_resource),
5593 + .resource = at32_tcb0_resource,
5594 + .num_resources = ARRAY_SIZE(at32_tcb0_resource),
5595 };
5596 -DEV_CLK(pclk, at32_systc0, pbb, 3);
5597 +DEV_CLK(t0_clk, at32_tcb0, pbb, 3);
5598 +
5599 +static struct resource at32_tcb1_resource[] = {
5600 + PBMEM(0xfff01000),
5601 + IRQ(23),
5602 +};
5603 +static struct platform_device at32_tcb1_device = {
5604 + .name = "atmel_tcb",
5605 + .id = 1,
5606 + .resource = at32_tcb1_resource,
5607 + .num_resources = ARRAY_SIZE(at32_tcb1_resource),
5608 +};
5609 +DEV_CLK(t0_clk, at32_tcb1, pbb, 4);
5610
5611 /* --------------------------------------------------------------------
5612 * PIO
5613 @@ -669,7 +832,8 @@
5614 platform_device_register(&pdc_device);
5615 platform_device_register(&dmaca0_device);
5616
5617 - platform_device_register(&at32_systc0_device);
5618 + platform_device_register(&at32_tcb0_device);
5619 + platform_device_register(&at32_tcb1_device);
5620
5621 platform_device_register(&pio0_device);
5622 platform_device_register(&pio1_device);
5623 @@ -679,6 +843,81 @@
5624 }
5625
5626 /* --------------------------------------------------------------------
5627 + * PSIF
5628 + * -------------------------------------------------------------------- */
5629 +static struct resource atmel_psif0_resource[] __initdata = {
5630 + {
5631 + .start = 0xffe03c00,
5632 + .end = 0xffe03cff,
5633 + .flags = IORESOURCE_MEM,
5634 + },
5635 + IRQ(18),
5636 +};
5637 +static struct clk atmel_psif0_pclk = {
5638 + .name = "pclk",
5639 + .parent = &pba_clk,
5640 + .mode = pba_clk_mode,
5641 + .get_rate = pba_clk_get_rate,
5642 + .index = 15,
5643 +};
5644 +
5645 +static struct resource atmel_psif1_resource[] __initdata = {
5646 + {
5647 + .start = 0xffe03d00,
5648 + .end = 0xffe03dff,
5649 + .flags = IORESOURCE_MEM,
5650 + },
5651 + IRQ(18),
5652 +};
5653 +static struct clk atmel_psif1_pclk = {
5654 + .name = "pclk",
5655 + .parent = &pba_clk,
5656 + .mode = pba_clk_mode,
5657 + .get_rate = pba_clk_get_rate,
5658 + .index = 15,
5659 +};
5660 +
5661 +struct platform_device *__init at32_add_device_psif(unsigned int id)
5662 +{
5663 + struct platform_device *pdev;
5664 +
5665 + if (!(id == 0 || id == 1))
5666 + return NULL;
5667 +
5668 + pdev = platform_device_alloc("atmel_psif", id);
5669 + if (!pdev)
5670 + return NULL;
5671 +
5672 + switch (id) {
5673 + case 0:
5674 + if (platform_device_add_resources(pdev, atmel_psif0_resource,
5675 + ARRAY_SIZE(atmel_psif0_resource)))
5676 + goto err_add_resources;
5677 + atmel_psif0_pclk.dev = &pdev->dev;
5678 + select_peripheral(PA(8), PERIPH_A, 0); /* CLOCK */
5679 + select_peripheral(PA(9), PERIPH_A, 0); /* DATA */
5680 + break;
5681 + case 1:
5682 + if (platform_device_add_resources(pdev, atmel_psif1_resource,
5683 + ARRAY_SIZE(atmel_psif1_resource)))
5684 + goto err_add_resources;
5685 + atmel_psif1_pclk.dev = &pdev->dev;
5686 + select_peripheral(PB(11), PERIPH_A, 0); /* CLOCK */
5687 + select_peripheral(PB(12), PERIPH_A, 0); /* DATA */
5688 + break;
5689 + default:
5690 + return NULL;
5691 + }
5692 +
5693 + platform_device_add(pdev);
5694 + return pdev;
5695 +
5696 +err_add_resources:
5697 + platform_device_put(pdev);
5698 + return NULL;
5699 +}
5700 +
5701 +/* --------------------------------------------------------------------
5702 * USART
5703 * -------------------------------------------------------------------- */
5704
5705 @@ -989,7 +1228,9 @@
5706 .index = 2,
5707 };
5708
5709 -struct platform_device *__init at32_add_device_twi(unsigned int id)
5710 +struct platform_device *__init at32_add_device_twi(unsigned int id,
5711 + struct i2c_board_info *b,
5712 + unsigned int n)
5713 {
5714 struct platform_device *pdev;
5715
5716 @@ -1009,6 +1250,9 @@
5717
5718 atmel_twi0_pclk.dev = &pdev->dev;
5719
5720 + if (b)
5721 + i2c_register_board_info(id, b, n);
5722 +
5723 platform_device_add(pdev);
5724 return pdev;
5725
5726 @@ -1032,7 +1276,8 @@
5727 .index = 9,
5728 };
5729
5730 -struct platform_device *__init at32_add_device_mci(unsigned int id)
5731 +struct platform_device *__init
5732 +at32_add_device_mci(unsigned int id, struct mci_platform_data *data)
5733 {
5734 struct platform_device *pdev;
5735
5736 @@ -1041,11 +1286,15 @@
5737
5738 pdev = platform_device_alloc("atmel_mci", id);
5739 if (!pdev)
5740 - return NULL;
5741 + goto fail;
5742
5743 if (platform_device_add_resources(pdev, atmel_mci0_resource,
5744 ARRAY_SIZE(atmel_mci0_resource)))
5745 - goto err_add_resources;
5746 + goto fail;
5747 +
5748 + if (data && platform_device_add_data(pdev, data,
5749 + sizeof(struct mci_platform_data)))
5750 + goto fail;
5751
5752 select_peripheral(PA(10), PERIPH_A, 0); /* CLK */
5753 select_peripheral(PA(11), PERIPH_A, 0); /* CMD */
5754 @@ -1054,12 +1303,19 @@
5755 select_peripheral(PA(14), PERIPH_A, 0); /* DATA2 */
5756 select_peripheral(PA(15), PERIPH_A, 0); /* DATA3 */
5757
5758 + if (data) {
5759 + if (data->detect_pin != GPIO_PIN_NONE)
5760 + at32_select_gpio(data->detect_pin, 0);
5761 + if (data->wp_pin != GPIO_PIN_NONE)
5762 + at32_select_gpio(data->wp_pin, 0);
5763 + }
5764 +
5765 atmel_mci0_pclk.dev = &pdev->dev;
5766
5767 platform_device_add(pdev);
5768 return pdev;
5769
5770 -err_add_resources:
5771 +fail:
5772 platform_device_put(pdev);
5773 return NULL;
5774 }
5775 @@ -1097,7 +1353,8 @@
5776
5777 struct platform_device *__init
5778 at32_add_device_lcdc(unsigned int id, struct atmel_lcdfb_info *data,
5779 - unsigned long fbmem_start, unsigned long fbmem_len)
5780 + unsigned long fbmem_start, unsigned long fbmem_len,
5781 + unsigned int pin_config)
5782 {
5783 struct platform_device *pdev;
5784 struct atmel_lcdfb_info *info;
5785 @@ -1124,37 +1381,77 @@
5786 switch (id) {
5787 case 0:
5788 pdev = &atmel_lcdfb0_device;
5789 - select_peripheral(PC(19), PERIPH_A, 0); /* CC */
5790 - select_peripheral(PC(20), PERIPH_A, 0); /* HSYNC */
5791 - select_peripheral(PC(21), PERIPH_A, 0); /* PCLK */
5792 - select_peripheral(PC(22), PERIPH_A, 0); /* VSYNC */
5793 - select_peripheral(PC(23), PERIPH_A, 0); /* DVAL */
5794 - select_peripheral(PC(24), PERIPH_A, 0); /* MODE */
5795 - select_peripheral(PC(25), PERIPH_A, 0); /* PWR */
5796 - select_peripheral(PC(26), PERIPH_A, 0); /* DATA0 */
5797 - select_peripheral(PC(27), PERIPH_A, 0); /* DATA1 */
5798 - select_peripheral(PC(28), PERIPH_A, 0); /* DATA2 */
5799 - select_peripheral(PC(29), PERIPH_A, 0); /* DATA3 */
5800 - select_peripheral(PC(30), PERIPH_A, 0); /* DATA4 */
5801 - select_peripheral(PC(31), PERIPH_A, 0); /* DATA5 */
5802 - select_peripheral(PD(0), PERIPH_A, 0); /* DATA6 */
5803 - select_peripheral(PD(1), PERIPH_A, 0); /* DATA7 */
5804 - select_peripheral(PD(2), PERIPH_A, 0); /* DATA8 */
5805 - select_peripheral(PD(3), PERIPH_A, 0); /* DATA9 */
5806 - select_peripheral(PD(4), PERIPH_A, 0); /* DATA10 */
5807 - select_peripheral(PD(5), PERIPH_A, 0); /* DATA11 */
5808 - select_peripheral(PD(6), PERIPH_A, 0); /* DATA12 */
5809 - select_peripheral(PD(7), PERIPH_A, 0); /* DATA13 */
5810 - select_peripheral(PD(8), PERIPH_A, 0); /* DATA14 */
5811 - select_peripheral(PD(9), PERIPH_A, 0); /* DATA15 */
5812 - select_peripheral(PD(10), PERIPH_A, 0); /* DATA16 */
5813 - select_peripheral(PD(11), PERIPH_A, 0); /* DATA17 */
5814 - select_peripheral(PD(12), PERIPH_A, 0); /* DATA18 */
5815 - select_peripheral(PD(13), PERIPH_A, 0); /* DATA19 */
5816 - select_peripheral(PD(14), PERIPH_A, 0); /* DATA20 */
5817 - select_peripheral(PD(15), PERIPH_A, 0); /* DATA21 */
5818 - select_peripheral(PD(16), PERIPH_A, 0); /* DATA22 */
5819 - select_peripheral(PD(17), PERIPH_A, 0); /* DATA23 */
5820 +
5821 + switch (pin_config) {
5822 + case 0:
5823 + select_peripheral(PC(19), PERIPH_A, 0); /* CC */
5824 + select_peripheral(PC(20), PERIPH_A, 0); /* HSYNC */
5825 + select_peripheral(PC(21), PERIPH_A, 0); /* PCLK */
5826 + select_peripheral(PC(22), PERIPH_A, 0); /* VSYNC */
5827 + select_peripheral(PC(23), PERIPH_A, 0); /* DVAL */
5828 + select_peripheral(PC(24), PERIPH_A, 0); /* MODE */
5829 + select_peripheral(PC(25), PERIPH_A, 0); /* PWR */
5830 + select_peripheral(PC(26), PERIPH_A, 0); /* DATA0 */
5831 + select_peripheral(PC(27), PERIPH_A, 0); /* DATA1 */
5832 + select_peripheral(PC(28), PERIPH_A, 0); /* DATA2 */
5833 + select_peripheral(PC(29), PERIPH_A, 0); /* DATA3 */
5834 + select_peripheral(PC(30), PERIPH_A, 0); /* DATA4 */
5835 + select_peripheral(PC(31), PERIPH_A, 0); /* DATA5 */
5836 + select_peripheral(PD(0), PERIPH_A, 0); /* DATA6 */
5837 + select_peripheral(PD(1), PERIPH_A, 0); /* DATA7 */
5838 + select_peripheral(PD(2), PERIPH_A, 0); /* DATA8 */
5839 + select_peripheral(PD(3), PERIPH_A, 0); /* DATA9 */
5840 + select_peripheral(PD(4), PERIPH_A, 0); /* DATA10 */
5841 + select_peripheral(PD(5), PERIPH_A, 0); /* DATA11 */
5842 + select_peripheral(PD(6), PERIPH_A, 0); /* DATA12 */
5843 + select_peripheral(PD(7), PERIPH_A, 0); /* DATA13 */
5844 + select_peripheral(PD(8), PERIPH_A, 0); /* DATA14 */
5845 + select_peripheral(PD(9), PERIPH_A, 0); /* DATA15 */
5846 + select_peripheral(PD(10), PERIPH_A, 0); /* DATA16 */
5847 + select_peripheral(PD(11), PERIPH_A, 0); /* DATA17 */
5848 + select_peripheral(PD(12), PERIPH_A, 0); /* DATA18 */
5849 + select_peripheral(PD(13), PERIPH_A, 0); /* DATA19 */
5850 + select_peripheral(PD(14), PERIPH_A, 0); /* DATA20 */
5851 + select_peripheral(PD(15), PERIPH_A, 0); /* DATA21 */
5852 + select_peripheral(PD(16), PERIPH_A, 0); /* DATA22 */
5853 + select_peripheral(PD(17), PERIPH_A, 0); /* DATA23 */
5854 + break;
5855 + case 1:
5856 + select_peripheral(PE(0), PERIPH_B, 0); /* CC */
5857 + select_peripheral(PC(20), PERIPH_A, 0); /* HSYNC */
5858 + select_peripheral(PC(21), PERIPH_A, 0); /* PCLK */
5859 + select_peripheral(PC(22), PERIPH_A, 0); /* VSYNC */
5860 + select_peripheral(PE(1), PERIPH_B, 0); /* DVAL */
5861 + select_peripheral(PE(2), PERIPH_B, 0); /* MODE */
5862 + select_peripheral(PC(25), PERIPH_A, 0); /* PWR */
5863 + select_peripheral(PE(3), PERIPH_B, 0); /* DATA0 */
5864 + select_peripheral(PE(4), PERIPH_B, 0); /* DATA1 */
5865 + select_peripheral(PE(5), PERIPH_B, 0); /* DATA2 */
5866 + select_peripheral(PE(6), PERIPH_B, 0); /* DATA3 */
5867 + select_peripheral(PE(7), PERIPH_B, 0); /* DATA4 */
5868 + select_peripheral(PC(31), PERIPH_A, 0); /* DATA5 */
5869 + select_peripheral(PD(0), PERIPH_A, 0); /* DATA6 */
5870 + select_peripheral(PD(1), PERIPH_A, 0); /* DATA7 */
5871 + select_peripheral(PE(8), PERIPH_B, 0); /* DATA8 */
5872 + select_peripheral(PE(9), PERIPH_B, 0); /* DATA9 */
5873 + select_peripheral(PE(10), PERIPH_B, 0); /* DATA10 */
5874 + select_peripheral(PE(11), PERIPH_B, 0); /* DATA11 */
5875 + select_peripheral(PE(12), PERIPH_B, 0); /* DATA12 */
5876 + select_peripheral(PD(7), PERIPH_A, 0); /* DATA13 */
5877 + select_peripheral(PD(8), PERIPH_A, 0); /* DATA14 */
5878 + select_peripheral(PD(9), PERIPH_A, 0); /* DATA15 */
5879 + select_peripheral(PE(13), PERIPH_B, 0); /* DATA16 */
5880 + select_peripheral(PE(14), PERIPH_B, 0); /* DATA17 */
5881 + select_peripheral(PE(15), PERIPH_B, 0); /* DATA18 */
5882 + select_peripheral(PE(16), PERIPH_B, 0); /* DATA19 */
5883 + select_peripheral(PE(17), PERIPH_B, 0); /* DATA20 */
5884 + select_peripheral(PE(18), PERIPH_B, 0); /* DATA21 */
5885 + select_peripheral(PD(16), PERIPH_A, 0); /* DATA22 */
5886 + select_peripheral(PD(17), PERIPH_A, 0); /* DATA23 */
5887 + break;
5888 + default:
5889 + goto err_invalid_id;
5890 + }
5891
5892 clk_set_parent(&atmel_lcdfb0_pixclk, &pll0);
5893 clk_set_rate(&atmel_lcdfb0_pixclk, clk_get_rate(&pll0));
5894 @@ -1351,9 +1648,39 @@
5895 .index = 6,
5896 };
5897
5898 +#define EP(nam, idx, maxpkt, maxbk, dma, isoc) \
5899 + [idx] = { \
5900 + .name = nam, \
5901 + .index = idx, \
5902 + .fifo_size = maxpkt, \
5903 + .nr_banks = maxbk, \
5904 + .can_dma = dma, \
5905 + .can_isoc = isoc, \
5906 + }
5907 +
5908 +static struct usba_ep_data at32_usba_ep[] __initdata = {
5909 + EP("ep0", 0, 64, 1, 0, 0),
5910 + EP("ep1", 1, 512, 2, 1, 1),
5911 + EP("ep2", 2, 512, 2, 1, 1),
5912 + EP("ep3-int", 3, 64, 3, 1, 0),
5913 + EP("ep4-int", 4, 64, 3, 1, 0),
5914 + EP("ep5", 5, 1024, 3, 1, 1),
5915 + EP("ep6", 6, 1024, 3, 1, 1),
5916 +};
5917 +
5918 +#undef EP
5919 +
5920 struct platform_device *__init
5921 at32_add_device_usba(unsigned int id, struct usba_platform_data *data)
5922 {
5923 + /*
5924 + * pdata doesn't have room for any endpoints, so we need to
5925 + * append room for the ones we need right after it.
5926 + */
5927 + struct {
5928 + struct usba_platform_data pdata;
5929 + struct usba_ep_data ep[7];
5930 + } usba_data;
5931 struct platform_device *pdev;
5932
5933 if (id != 0)
5934 @@ -1367,13 +1694,20 @@
5935 ARRAY_SIZE(usba0_resource)))
5936 goto out_free_pdev;
5937
5938 - if (data) {
5939 - if (platform_device_add_data(pdev, data, sizeof(*data)))
5940 - goto out_free_pdev;
5941 + if (data)
5942 + usba_data.pdata.vbus_pin = data->vbus_pin;
5943 + else
5944 + usba_data.pdata.vbus_pin = -EINVAL;
5945
5946 - if (data->vbus_pin != GPIO_PIN_NONE)
5947 - at32_select_gpio(data->vbus_pin, 0);
5948 - }
5949 + data = &usba_data.pdata;
5950 + data->num_ep = ARRAY_SIZE(at32_usba_ep);
5951 + memcpy(data->ep, at32_usba_ep, sizeof(at32_usba_ep));
5952 +
5953 + if (platform_device_add_data(pdev, data, sizeof(usba_data)))
5954 + goto out_free_pdev;
5955 +
5956 + if (data->vbus_pin >= 0)
5957 + at32_select_gpio(data->vbus_pin, 0);
5958
5959 usba0_pclk.dev = &pdev->dev;
5960 usba0_hclk.dev = &pdev->dev;
5961 @@ -1526,6 +1860,58 @@
5962 #endif
5963
5964 /* --------------------------------------------------------------------
5965 + * NAND Flash / SmartMedia
5966 + * -------------------------------------------------------------------- */
5967 +static struct resource smc_cs3_resource[] __initdata = {
5968 + {
5969 + .start = 0x0c000000,
5970 + .end = 0x0fffffff,
5971 + .flags = IORESOURCE_MEM,
5972 + }, {
5973 + .start = 0xfff03c00,
5974 + .end = 0xfff03fff,
5975 + .flags = IORESOURCE_MEM,
5976 + },
5977 +};
5978 +
5979 +struct platform_device *__init
5980 +at32_add_device_nand(unsigned int id, struct atmel_nand_data *data)
5981 +{
5982 + struct platform_device *pdev;
5983 +
5984 + if (id != 0 || !data)
5985 + return NULL;
5986 +
5987 + pdev = platform_device_alloc("atmel_nand", id);
5988 + if (!pdev)
5989 + goto fail;
5990 +
5991 + if (platform_device_add_resources(pdev, smc_cs3_resource,
5992 + ARRAY_SIZE(smc_cs3_resource)))
5993 + goto fail;
5994 +
5995 + if (platform_device_add_data(pdev, data,
5996 + sizeof(struct atmel_nand_data)))
5997 + goto fail;
5998 +
5999 + set_ebi_sfr_bits(HMATRIX_BIT(CS3A));
6000 + if (data->enable_pin)
6001 + at32_select_gpio(data->enable_pin,
6002 + AT32_GPIOF_OUTPUT | AT32_GPIOF_HIGH);
6003 + if (data->rdy_pin)
6004 + at32_select_gpio(data->rdy_pin, 0);
6005 + if (data->det_pin)
6006 + at32_select_gpio(data->det_pin, 0);
6007 +
6008 + platform_device_add(pdev);
6009 + return pdev;
6010 +
6011 +fail:
6012 + platform_device_put(pdev);
6013 + return NULL;
6014 +}
6015 +
6016 +/* --------------------------------------------------------------------
6017 * AC97C
6018 * -------------------------------------------------------------------- */
6019 static struct resource atmel_ac97c0_resource[] __initdata = {
6020 @@ -1683,6 +2069,7 @@
6021 &hmatrix_clk,
6022 &ebi_clk,
6023 &hramc_clk,
6024 + &sdramc_clk,
6025 &smc0_pclk,
6026 &smc0_mck,
6027 &pdc_hclk,
6028 @@ -1694,7 +2081,10 @@
6029 &pio2_mck,
6030 &pio3_mck,
6031 &pio4_mck,
6032 - &at32_systc0_pclk,
6033 + &at32_tcb0_t0_clk,
6034 + &at32_tcb1_t0_clk,
6035 + &atmel_psif0_pclk,
6036 + &atmel_psif1_pclk,
6037 &atmel_usart0_usart,
6038 &atmel_usart1_usart,
6039 &atmel_usart2_usart,
6040 @@ -1730,16 +2120,7 @@
6041 };
6042 unsigned int at32_nr_clocks = ARRAY_SIZE(at32_clock_list);
6043
6044 -void __init at32_portmux_init(void)
6045 -{
6046 - at32_init_pio(&pio0_device);
6047 - at32_init_pio(&pio1_device);
6048 - at32_init_pio(&pio2_device);
6049 - at32_init_pio(&pio3_device);
6050 - at32_init_pio(&pio4_device);
6051 -}
6052 -
6053 -void __init at32_clock_init(void)
6054 +void __init setup_platform(void)
6055 {
6056 u32 cpu_mask = 0, hsb_mask = 0, pba_mask = 0, pbb_mask = 0;
6057 int i;
6058 @@ -1794,4 +2175,36 @@
6059 pm_writel(HSB_MASK, hsb_mask);
6060 pm_writel(PBA_MASK, pba_mask);
6061 pm_writel(PBB_MASK, pbb_mask);
6062 +
6063 + /* Initialize the port muxes */
6064 + at32_init_pio(&pio0_device);
6065 + at32_init_pio(&pio1_device);
6066 + at32_init_pio(&pio2_device);
6067 + at32_init_pio(&pio3_device);
6068 + at32_init_pio(&pio4_device);
6069 +}
6070 +
6071 +struct gen_pool *sram_pool;
6072 +
6073 +static int __init sram_init(void)
6074 +{
6075 + struct gen_pool *pool;
6076 +
6077 + /* 1KiB granularity */
6078 + pool = gen_pool_create(10, -1);
6079 + if (!pool)
6080 + goto fail;
6081 +
6082 + if (gen_pool_add(pool, 0x24000000, 0x8000, -1))
6083 + goto err_pool_add;
6084 +
6085 + sram_pool = pool;
6086 + return 0;
6087 +
6088 +err_pool_add:
6089 + gen_pool_destroy(pool);
6090 +fail:
6091 + pr_err("Failed to create SRAM pool\n");
6092 + return -ENOMEM;
6093 }
6094 +core_initcall(sram_init);
6095 diff --exclude=.git -urN linux-2.6.25.6/arch/avr32/mach-at32ap/at32ap.c avr32-2.6/arch/avr32/mach-at32ap/at32ap.c
6096 --- linux-2.6.25.6/arch/avr32/mach-at32ap/at32ap.c 2008-06-09 20:27:19.000000000 +0200
6097 +++ avr32-2.6/arch/avr32/mach-at32ap/at32ap.c 1970-01-01 01:00:00.000000000 +0100
6098 @@ -1,56 +0,0 @@
6099 -/*
6100 - * Copyright (C) 2006 Atmel Corporation
6101 - *
6102 - * This program is free software; you can redistribute it and/or modify
6103 - * it under the terms of the GNU General Public License version 2 as
6104 - * published by the Free Software Foundation.
6105 - */
6106 -
6107 -#include <linux/clk.h>
6108 -#include <linux/err.h>
6109 -#include <linux/init.h>
6110 -#include <linux/platform_device.h>
6111 -
6112 -#include <asm/arch/init.h>
6113 -
6114 -void __init setup_platform(void)
6115 -{
6116 - at32_clock_init();
6117 - at32_portmux_init();
6118 -}
6119 -
6120 -static int __init pdc_probe(struct platform_device *pdev)
6121 -{
6122 - struct clk *pclk, *hclk;
6123 -
6124 - pclk = clk_get(&pdev->dev, "pclk");
6125 - if (IS_ERR(pclk)) {
6126 - dev_err(&pdev->dev, "no pclk defined\n");
6127 - return PTR_ERR(pclk);
6128 - }
6129 - hclk = clk_get(&pdev->dev, "hclk");
6130 - if (IS_ERR(hclk)) {
6131 - dev_err(&pdev->dev, "no hclk defined\n");
6132 - clk_put(pclk);
6133 - return PTR_ERR(hclk);
6134 - }
6135 -
6136 - clk_enable(pclk);
6137 - clk_enable(hclk);
6138 -
6139 - dev_info(&pdev->dev, "Atmel Peripheral DMA Controller enabled\n");
6140 - return 0;
6141 -}
6142 -
6143 -static struct platform_driver pdc_driver = {
6144 - .probe = pdc_probe,
6145 - .driver = {
6146 - .name = "pdc",
6147 - },
6148 -};
6149 -
6150 -static int __init pdc_init(void)
6151 -{
6152 - return platform_driver_register(&pdc_driver);
6153 -}
6154 -arch_initcall(pdc_init);
6155 diff --exclude=.git -urN linux-2.6.25.6/arch/avr32/mach-at32ap/cpufreq.c avr32-2.6/arch/avr32/mach-at32ap/cpufreq.c
6156 --- linux-2.6.25.6/arch/avr32/mach-at32ap/cpufreq.c 2008-06-09 20:27:19.000000000 +0200
6157 +++ avr32-2.6/arch/avr32/mach-at32ap/cpufreq.c 2008-06-12 15:03:55.891816030 +0200
6158 @@ -108,5 +108,4 @@
6159 {
6160 return cpufreq_register_driver(&at32_driver);
6161 }
6162 -
6163 -arch_initcall(at32_cpufreq_init);
6164 +late_initcall(at32_cpufreq_init);
6165 diff --exclude=.git -urN linux-2.6.25.6/arch/avr32/mach-at32ap/gpio-dev.c avr32-2.6/arch/avr32/mach-at32ap/gpio-dev.c
6166 --- linux-2.6.25.6/arch/avr32/mach-at32ap/gpio-dev.c 1970-01-01 01:00:00.000000000 +0100
6167 +++ avr32-2.6/arch/avr32/mach-at32ap/gpio-dev.c 2008-06-12 15:09:38.723815860 +0200
6168 @@ -0,0 +1,573 @@
6169 +/*
6170 + * GPIO /dev and configfs interface
6171 + *
6172 + * Copyright (C) 2006-2007 Atmel Corporation
6173 + *
6174 + * This program is free software; you can redistribute it and/or modify
6175 + * it under the terms of the GNU General Public License version 2 as
6176 + * published by the Free Software Foundation.
6177 + */
6178 +#include <linux/kernel.h>
6179 +#include <linux/configfs.h>
6180 +#include <linux/cdev.h>
6181 +#include <linux/device.h>
6182 +#include <linux/fs.h>
6183 +#include <linux/interrupt.h>
6184 +#include <linux/module.h>
6185 +#include <linux/poll.h>
6186 +#include <linux/uaccess.h>
6187 +#include <linux/wait.h>
6188 +
6189 +#include <asm/gpio.h>
6190 +#include <asm/arch/portmux.h>
6191 +
6192 +#define GPIO_DEV_MAX 8
6193 +
6194 +static struct class *gpio_dev_class;
6195 +static dev_t gpio_devt;
6196 +
6197 +struct gpio_item {
6198 + spinlock_t lock;
6199 +
6200 + int enabled;
6201 + int initialized;
6202 + int port;
6203 + u32 pin_mask;
6204 + u32 oe_mask;
6205 +
6206 + /* Pin state last time we read it (for blocking reads) */
6207 + u32 pin_state;
6208 + int changed;
6209 +
6210 + wait_queue_head_t change_wq;
6211 + struct fasync_struct *async_queue;
6212 +
6213 + int id;
6214 + struct class_device *gpio_dev;
6215 + struct cdev char_dev;
6216 + struct config_item item;
6217 +};
6218 +
6219 +struct gpio_attribute {
6220 + struct configfs_attribute attr;
6221 + ssize_t (*show)(struct gpio_item *, char *);
6222 + ssize_t (*store)(struct gpio_item *, const char *, size_t);
6223 +};
6224 +
6225 +static irqreturn_t gpio_dev_interrupt(int irq, void *dev_id)
6226 +{
6227 + struct gpio_item *gpio = dev_id;
6228 + u32 old_state, new_state;
6229 +
6230 + old_state = gpio->pin_state;
6231 + new_state = at32_gpio_get_value_multiple(gpio->port, gpio->pin_mask);
6232 + gpio->pin_state = new_state;
6233 +
6234 + if (new_state != old_state) {
6235 + gpio->changed = 1;
6236 + wake_up_interruptible(&gpio->change_wq);
6237 +
6238 + if (gpio->async_queue)
6239 + kill_fasync(&gpio->async_queue, SIGIO, POLL_IN);
6240 + }
6241 +
6242 + return IRQ_HANDLED;
6243 +}
6244 +
6245 +static int gpio_dev_open(struct inode *inode, struct file *file)
6246 +{
6247 + struct gpio_item *gpio = container_of(inode->i_cdev,
6248 + struct gpio_item,
6249 + char_dev);
6250 + unsigned int irq;
6251 + unsigned int i;
6252 + int ret;
6253 +
6254 + nonseekable_open(inode, file);
6255 + config_item_get(&gpio->item);
6256 + file->private_data = gpio;
6257 +
6258 + gpio->pin_state = at32_gpio_get_value_multiple(gpio->port,
6259 + gpio->pin_mask);
6260 + gpio->changed = 1;
6261 +
6262 + for (i = 0; i < 32; i++) {
6263 + if (gpio->pin_mask & (1 << i)) {
6264 + irq = gpio_to_irq(32 * gpio->port + i);
6265 + ret = request_irq(irq, gpio_dev_interrupt, 0,
6266 + "gpio-dev", gpio);
6267 + if (ret)
6268 + goto err_irq;
6269 + }
6270 + }
6271 +
6272 + return 0;
6273 +
6274 +err_irq:
6275 + while (i--) {
6276 + if (gpio->pin_mask & (1 << i)) {
6277 + irq = gpio_to_irq(32 * gpio->port + i);
6278 + free_irq(irq, gpio);
6279 + }
6280 + }
6281 +
6282 + config_item_put(&gpio->item);
6283 +
6284 + return ret;
6285 +}
6286 +
6287 +static int gpio_dev_fasync(int fd, struct file *file, int mode)
6288 +{
6289 + struct gpio_item *gpio = file->private_data;
6290 +
6291 + return fasync_helper(fd, file, mode, &gpio->async_queue);
6292 +}
6293 +
6294 +static int gpio_dev_release(struct inode *inode, struct file *file)
6295 +{
6296 + struct gpio_item *gpio = file->private_data;
6297 + unsigned int irq;
6298 + unsigned int i;
6299 +
6300 + gpio_dev_fasync(-1, file, 0);
6301 +
6302 + for (i = 0; i < 32; i++) {
6303 + if (gpio->pin_mask & (1 << i)) {
6304 + irq = gpio_to_irq(32 * gpio->port + i);
6305 + free_irq(irq, gpio);
6306 + }
6307 + }
6308 +
6309 + config_item_put(&gpio->item);
6310 +
6311 + return 0;
6312 +}
6313 +
6314 +static unsigned int gpio_dev_poll(struct file *file, poll_table *wait)
6315 +{
6316 + struct gpio_item *gpio = file->private_data;
6317 + unsigned int mask = 0;
6318 +
6319 + poll_wait(file, &gpio->change_wq, wait);
6320 + if (gpio->changed)
6321 + mask |= POLLIN | POLLRDNORM;
6322 +
6323 + return mask;
6324 +}
6325 +
6326 +static ssize_t gpio_dev_read(struct file *file, char __user *buf,
6327 + size_t count, loff_t *offset)
6328 +{
6329 + struct gpio_item *gpio = file->private_data;
6330 + u32 value;
6331 +
6332 + spin_lock_irq(&gpio->lock);
6333 + while (!gpio->changed) {
6334 + spin_unlock_irq(&gpio->lock);
6335 +
6336 + if (file->f_flags & O_NONBLOCK)
6337 + return -EAGAIN;
6338 +
6339 + if (wait_event_interruptible(gpio->change_wq, gpio->changed))
6340 + return -ERESTARTSYS;
6341 +
6342 + spin_lock_irq(&gpio->lock);
6343 + }
6344 +
6345 + gpio->changed = 0;
6346 + value = at32_gpio_get_value_multiple(gpio->port, gpio->pin_mask);
6347 +
6348 + spin_unlock_irq(&gpio->lock);
6349 +
6350 + count = min(count, (size_t)4);
6351 + if (copy_to_user(buf, &value, count))
6352 + return -EFAULT;
6353 +
6354 + return count;
6355 +}
6356 +
6357 +static ssize_t gpio_dev_write(struct file *file, const char __user *buf,
6358 + size_t count, loff_t *offset)
6359 +{
6360 + struct gpio_item *gpio = file->private_data;
6361 + u32 value = 0;
6362 + u32 mask = ~0UL;
6363 +
6364 + count = min(count, (size_t)4);
6365 + if (copy_from_user(&value, buf, count))
6366 + return -EFAULT;
6367 +
6368 + /* Assuming big endian */
6369 + mask <<= (4 - count) * 8;
6370 + mask &= gpio->pin_mask;
6371 +
6372 + at32_gpio_set_value_multiple(gpio->port, value, mask);
6373 +
6374 + return count;
6375 +}
6376 +
6377 +static struct file_operations gpio_dev_fops = {
6378 + .owner = THIS_MODULE,
6379 + .llseek = no_llseek,
6380 + .open = gpio_dev_open,
6381 + .release = gpio_dev_release,
6382 + .fasync = gpio_dev_fasync,
6383 + .poll = gpio_dev_poll,
6384 + .read = gpio_dev_read,
6385 + .write = gpio_dev_write,
6386 +};
6387 +
6388 +static struct gpio_item *to_gpio_item(struct config_item *item)
6389 +{
6390 + return item ? container_of(item, struct gpio_item, item) : NULL;
6391 +}
6392 +
6393 +static ssize_t gpio_show_gpio_id(struct gpio_item *gpio, char *page)
6394 +{
6395 + return sprintf(page, "%d\n", gpio->port);
6396 +}
6397 +
6398 +static ssize_t gpio_store_gpio_id(struct gpio_item *gpio,
6399 + const char *page, size_t count)
6400 +{
6401 + unsigned long id;
6402 + char *p = (char *)page;
6403 + ssize_t ret = -EINVAL;
6404 +
6405 + id = simple_strtoul(p, &p, 0);
6406 + if (!p || (*p && (*p != '\n')))
6407 + return -EINVAL;
6408 +
6409 + /* Switching PIO is not allowed when live... */
6410 + spin_lock(&gpio->lock);
6411 + if (!gpio->enabled) {
6412 + ret = -ENXIO;
6413 + if (at32_gpio_port_is_valid(id)) {
6414 + gpio->port = id;
6415 + ret = count;
6416 + }
6417 + }
6418 + spin_unlock(&gpio->lock);
6419 +
6420 + return ret;
6421 +}
6422 +
6423 +static ssize_t gpio_show_pin_mask(struct gpio_item *gpio, char *page)
6424 +{
6425 + return sprintf(page, "0x%08x\n", gpio->pin_mask);
6426 +}
6427 +
6428 +static ssize_t gpio_store_pin_mask(struct gpio_item *gpio,
6429 + const char *page, size_t count)
6430 +{
6431 + u32 new_mask;
6432 + char *p = (char *)page;
6433 + ssize_t ret = -EINVAL;
6434 +
6435 + new_mask = simple_strtoul(p, &p, 0);
6436 + if (!p || (*p && (*p != '\n')))
6437 + return -EINVAL;
6438 +
6439 + /* Can't update the pin mask while live. */
6440 + spin_lock(&gpio->lock);
6441 + if (!gpio->enabled) {
6442 + gpio->oe_mask &= new_mask;
6443 + gpio->pin_mask = new_mask;
6444 + ret = count;
6445 + }
6446 + spin_unlock(&gpio->lock);
6447 +
6448 + return ret;
6449 +}
6450 +
6451 +static ssize_t gpio_show_oe_mask(struct gpio_item *gpio, char *page)
6452 +{
6453 + return sprintf(page, "0x%08x\n", gpio->oe_mask);
6454 +}
6455 +
6456 +static ssize_t gpio_store_oe_mask(struct gpio_item *gpio,
6457 + const char *page, size_t count)
6458 +{
6459 + u32 mask;
6460 + char *p = (char *)page;
6461 + ssize_t ret = -EINVAL;
6462 +
6463 + mask = simple_strtoul(p, &p, 0);
6464 + if (!p || (*p && (*p != '\n')))
6465 + return -EINVAL;
6466 +
6467 + spin_lock(&gpio->lock);
6468 + if (!gpio->enabled) {
6469 + gpio->oe_mask = mask & gpio->pin_mask;
6470 + ret = count;
6471 + }
6472 + spin_unlock(&gpio->lock);
6473 +
6474 + return ret;
6475 +}
6476 +
6477 +static ssize_t gpio_show_enabled(struct gpio_item *gpio, char *page)
6478 +{
6479 + return sprintf(page, "%d\n", gpio->enabled);
6480 +}
6481 +
6482 +static ssize_t gpio_store_enabled(struct gpio_item *gpio,
6483 + const char *page, size_t count)
6484 +{
6485 + char *p = (char *)page;
6486 + int enabled;
6487 + int ret;
6488 +
6489 + enabled = simple_strtoul(p, &p, 0);
6490 + if (!p || (*p && (*p != '\n')))
6491 + return -EINVAL;
6492 +
6493 + /* make it a boolean value */
6494 + enabled = !!enabled;
6495 +
6496 + if (gpio->enabled == enabled)
6497 + /* No change; do nothing. */
6498 + return count;
6499 +
6500 + BUG_ON(gpio->id >= GPIO_DEV_MAX);
6501 +
6502 + if (!enabled) {
6503 + class_device_unregister(gpio->gpio_dev);
6504 + cdev_del(&gpio->char_dev);
6505 + at32_deselect_pins(gpio->port, gpio->pin_mask);
6506 + gpio->initialized = 0;
6507 + } else {
6508 + if (gpio->port < 0 || !gpio->pin_mask)
6509 + return -ENODEV;
6510 + }
6511 +
6512 + /* Disallow any updates to gpio_id or pin_mask */
6513 + spin_lock(&gpio->lock);
6514 + gpio->enabled = enabled;
6515 + spin_unlock(&gpio->lock);
6516 +
6517 + if (!enabled)
6518 + return count;
6519 +
6520 + /* Now, try to allocate the pins */
6521 + ret = at32_select_gpio_pins(gpio->port, gpio->pin_mask, gpio->oe_mask);
6522 + if (ret)
6523 + goto err_alloc_pins;
6524 +
6525 + gpio->initialized = 1;
6526 +
6527 + cdev_init(&gpio->char_dev, &gpio_dev_fops);
6528 + gpio->char_dev.owner = THIS_MODULE;
6529 + ret = cdev_add(&gpio->char_dev, MKDEV(MAJOR(gpio_devt), gpio->id), 1);
6530 + if (ret < 0)
6531 + goto err_cdev_add;
6532 + gpio->gpio_dev = class_device_create(gpio_dev_class, NULL,
6533 + MKDEV(MAJOR(gpio_devt), gpio->id),
6534 + NULL,
6535 + "gpio%d", gpio->id);
6536 + if (IS_ERR(gpio->gpio_dev)) {
6537 + printk(KERN_ERR "failed to create gpio%d\n", gpio->id);
6538 + ret = PTR_ERR(gpio->gpio_dev);
6539 + goto err_class_dev;
6540 + }
6541 +
6542 + printk(KERN_INFO "created gpio%d (port%d/0x%08x) as (%d:%d)\n",
6543 + gpio->id, gpio->port, gpio->pin_mask,
6544 + MAJOR(gpio->gpio_dev->devt), MINOR(gpio->gpio_dev->devt));
6545 +
6546 + return 0;
6547 +
6548 +err_class_dev:
6549 + cdev_del(&gpio->char_dev);
6550 +err_cdev_add:
6551 + at32_deselect_pins(gpio->port, gpio->pin_mask);
6552 + gpio->initialized = 0;
6553 +err_alloc_pins:
6554 + spin_lock(&gpio->lock);
6555 + gpio->enabled = 0;
6556 + spin_unlock(&gpio->lock);
6557 +
6558 + return ret;
6559 +}
6560 +
6561 +static struct gpio_attribute gpio_item_attr_gpio_id = {
6562 + .attr = {
6563 + .ca_owner = THIS_MODULE,
6564 + .ca_name = "gpio_id",
6565 + .ca_mode = S_IRUGO | S_IWUSR,
6566 + },
6567 + .show = gpio_show_gpio_id,
6568 + .store = gpio_store_gpio_id,
6569 +};
6570 +static struct gpio_attribute gpio_item_attr_pin_mask = {
6571 + .attr = {
6572 + .ca_owner = THIS_MODULE,
6573 + .ca_name = "pin_mask",
6574 + .ca_mode = S_IRUGO | S_IWUSR,
6575 + },
6576 + .show = gpio_show_pin_mask,
6577 + .store = gpio_store_pin_mask,
6578 +};
6579 +static struct gpio_attribute gpio_item_attr_oe_mask = {
6580 + .attr = {
6581 + .ca_owner = THIS_MODULE,
6582 + .ca_name = "oe_mask",
6583 + .ca_mode = S_IRUGO | S_IWUSR,
6584 + },
6585 + .show = gpio_show_oe_mask,
6586 + .store = gpio_store_oe_mask,
6587 +};
6588 +static struct gpio_attribute gpio_item_attr_enabled = {
6589 + .attr = {
6590 + .ca_owner = THIS_MODULE,
6591 + .ca_name = "enabled",
6592 + .ca_mode = S_IRUGO | S_IWUSR,
6593 + },
6594 + .show = gpio_show_enabled,
6595 + .store = gpio_store_enabled,
6596 +};
6597 +
6598 +static struct configfs_attribute *gpio_item_attrs[] = {
6599 + &gpio_item_attr_gpio_id.attr,
6600 + &gpio_item_attr_pin_mask.attr,
6601 + &gpio_item_attr_oe_mask.attr,
6602 + &gpio_item_attr_enabled.attr,
6603 + NULL,
6604 +};
6605 +
6606 +static ssize_t gpio_show_attr(struct config_item *item,
6607 + struct configfs_attribute *attr,
6608 + char *page)
6609 +{
6610 + struct gpio_item *gpio_item = to_gpio_item(item);
6611 + struct gpio_attribute *gpio_attr
6612 + = container_of(attr, struct gpio_attribute, attr);
6613 + ssize_t ret = 0;
6614 +
6615 + if (gpio_attr->show)
6616 + ret = gpio_attr->show(gpio_item, page);
6617 + return ret;
6618 +}
6619 +
6620 +static ssize_t gpio_store_attr(struct config_item *item,
6621 + struct configfs_attribute *attr,
6622 + const char *page, size_t count)
6623 +{
6624 + struct gpio_item *gpio_item = to_gpio_item(item);
6625 + struct gpio_attribute *gpio_attr
6626 + = container_of(attr, struct gpio_attribute, attr);
6627 + ssize_t ret = -EINVAL;
6628 +
6629 + if (gpio_attr->store)
6630 + ret = gpio_attr->store(gpio_item, page, count);
6631 + return ret;
6632 +}
6633 +
6634 +static void gpio_release(struct config_item *item)
6635 +{
6636 + kfree(to_gpio_item(item));
6637 +}
6638 +
6639 +static struct configfs_item_operations gpio_item_ops = {
6640 + .release = gpio_release,
6641 + .show_attribute = gpio_show_attr,
6642 + .store_attribute = gpio_store_attr,
6643 +};
6644 +
6645 +static struct config_item_type gpio_item_type = {
6646 + .ct_item_ops = &gpio_item_ops,
6647 + .ct_attrs = gpio_item_attrs,
6648 + .ct_owner = THIS_MODULE,
6649 +};
6650 +
6651 +static struct config_item *gpio_make_item(struct config_group *group,
6652 + const char *name)
6653 +{
6654 + static int next_id;
6655 + struct gpio_item *gpio;
6656 +
6657 + if (next_id >= GPIO_DEV_MAX)
6658 + return NULL;
6659 +
6660 + gpio = kzalloc(sizeof(struct gpio_item), GFP_KERNEL);
6661 + if (!gpio)
6662 + return NULL;
6663 +
6664 + gpio->id = next_id++;
6665 + config_item_init_type_name(&gpio->item, name, &gpio_item_type);
6666 + spin_lock_init(&gpio->lock);
6667 + init_waitqueue_head(&gpio->change_wq);
6668 +
6669 + return &gpio->item;
6670 +}
6671 +
6672 +static void gpio_drop_item(struct config_group *group,
6673 + struct config_item *item)
6674 +{
6675 + struct gpio_item *gpio = to_gpio_item(item);
6676 +
6677 + spin_lock(&gpio->lock);
6678 + if (gpio->enabled) {
6679 + class_device_unregister(gpio->gpio_dev);
6680 + cdev_del(&gpio->char_dev);
6681 + }
6682 +
6683 + if (gpio->initialized) {
6684 + at32_deselect_pins(gpio->port, gpio->pin_mask);
6685 + gpio->initialized = 0;
6686 + gpio->enabled = 0;
6687 + }
6688 + spin_unlock(&gpio->lock);
6689 +}
6690 +
6691 +static struct configfs_group_operations gpio_group_ops = {
6692 + .make_item = gpio_make_item,
6693 + .drop_item = gpio_drop_item,
6694 +};
6695 +
6696 +static struct config_item_type gpio_group_type = {
6697 + .ct_group_ops = &gpio_group_ops,
6698 + .ct_owner = THIS_MODULE,
6699 +};
6700 +
6701 +static struct configfs_subsystem gpio_subsys = {
6702 + .su_group = {
6703 + .cg_item = {
6704 + .ci_namebuf = "gpio",
6705 + .ci_type = &gpio_group_type,
6706 + },
6707 + },
6708 +};
6709 +
6710 +static int __init gpio_dev_init(void)
6711 +{
6712 + int err;
6713 +
6714 + gpio_dev_class = class_create(THIS_MODULE, "gpio-dev");
6715 + if (IS_ERR(gpio_dev_class)) {
6716 + err = PTR_ERR(gpio_dev_class);
6717 + goto err_class_create;
6718 + }
6719 +
6720 + err = alloc_chrdev_region(&gpio_devt, 0, GPIO_DEV_MAX, "gpio");
6721 + if (err < 0)
6722 + goto err_alloc_chrdev;
6723 +
6724 + /* Configfs initialization */
6725 + config_group_init(&gpio_subsys.su_group);
6726 + mutex_init(&gpio_subsys.su_mutex);
6727 + err = configfs_register_subsystem(&gpio_subsys);
6728 + if (err)
6729 + goto err_register_subsys;
6730 +
6731 + return 0;
6732 +
6733 +err_register_subsys:
6734 + unregister_chrdev_region(gpio_devt, GPIO_DEV_MAX);
6735 +err_alloc_chrdev:
6736 + class_destroy(gpio_dev_class);
6737 +err_class_create:
6738 + printk(KERN_WARNING "Failed to initialize gpio /dev interface\n");
6739 + return err;
6740 +}
6741 +late_initcall(gpio_dev_init);
6742 diff --exclude=.git -urN linux-2.6.25.6/arch/avr32/mach-at32ap/hsmc.c avr32-2.6/arch/avr32/mach-at32ap/hsmc.c
6743 --- linux-2.6.25.6/arch/avr32/mach-at32ap/hsmc.c 2008-06-09 20:27:19.000000000 +0200
6744 +++ avr32-2.6/arch/avr32/mach-at32ap/hsmc.c 2008-06-12 15:09:38.723815860 +0200
6745 @@ -278,4 +278,4 @@
6746 {
6747 return platform_driver_register(&hsmc_driver);
6748 }
6749 -arch_initcall(hsmc_init);
6750 +core_initcall(hsmc_init);
6751 diff --exclude=.git -urN linux-2.6.25.6/arch/avr32/mach-at32ap/intc.c avr32-2.6/arch/avr32/mach-at32ap/intc.c
6752 --- linux-2.6.25.6/arch/avr32/mach-at32ap/intc.c 2008-06-09 20:27:19.000000000 +0200
6753 +++ avr32-2.6/arch/avr32/mach-at32ap/intc.c 2008-06-12 15:09:38.723815860 +0200
6754 @@ -1,5 +1,5 @@
6755 /*
6756 - * Copyright (C) 2006 Atmel Corporation
6757 + * Copyright (C) 2006, 2008 Atmel Corporation
6758 *
6759 * This program is free software; you can redistribute it and/or modify
6760 * it under the terms of the GNU General Public License version 2 as
6761 @@ -12,15 +12,20 @@
6762 #include <linux/interrupt.h>
6763 #include <linux/irq.h>
6764 #include <linux/platform_device.h>
6765 +#include <linux/sysdev.h>
6766
6767 -#include <asm/intc.h>
6768 #include <asm/io.h>
6769
6770 #include "intc.h"
6771
6772 struct intc {
6773 - void __iomem *regs;
6774 - struct irq_chip chip;
6775 + void __iomem *regs;
6776 + struct irq_chip chip;
6777 + struct sys_device sysdev;
6778 +#ifdef CONFIG_PM
6779 + unsigned long suspend_ipr;
6780 + unsigned long saved_ipr[64];
6781 +#endif
6782 };
6783
6784 extern struct platform_device at32_intc0_device;
6785 @@ -137,6 +142,74 @@
6786 panic("Interrupt controller initialization failed!\n");
6787 }
6788
6789 +#ifdef CONFIG_PM
6790 +void intc_set_suspend_handler(unsigned long offset)
6791 +{
6792 + intc0.suspend_ipr = offset;
6793 +}
6794 +
6795 +static int intc_suspend(struct sys_device *sdev, pm_message_t state)
6796 +{
6797 + struct intc *intc = container_of(sdev, struct intc, sysdev);
6798 + int i;
6799 +
6800 + if (unlikely(!irqs_disabled())) {
6801 + pr_err("intc_suspend: called with interrupts enabled\n");
6802 + return -EINVAL;
6803 + }
6804 +
6805 + if (unlikely(!intc->suspend_ipr)) {
6806 + pr_err("intc_suspend: suspend_ipr not initialized\n");
6807 + return -EINVAL;
6808 + }
6809 +
6810 + for (i = 0; i < 64; i++) {
6811 + intc->saved_ipr[i] = intc_readl(intc, INTPR0 + 4 * i);
6812 + intc_writel(intc, INTPR0 + 4 * i, intc->suspend_ipr);
6813 + }
6814 +
6815 + return 0;
6816 +}
6817 +
6818 +static int intc_resume(struct sys_device *sdev)
6819 +{
6820 + struct intc *intc = container_of(sdev, struct intc, sysdev);
6821 + int i;
6822 +
6823 + WARN_ON(!irqs_disabled());
6824 +
6825 + for (i = 0; i < 64; i++)
6826 + intc_writel(intc, INTPR0 + 4 * i, intc->saved_ipr[i]);
6827 +
6828 + return 0;
6829 +}
6830 +#else
6831 +#define intc_suspend NULL
6832 +#define intc_resume NULL
6833 +#endif
6834 +
6835 +static struct sysdev_class intc_class = {
6836 + .name = "intc",
6837 + .suspend = intc_suspend,
6838 + .resume = intc_resume,
6839 +};
6840 +
6841 +static int __init intc_init_sysdev(void)
6842 +{
6843 + int ret;
6844 +
6845 + ret = sysdev_class_register(&intc_class);
6846 + if (ret)
6847 + return ret;
6848 +
6849 + intc0.sysdev.id = 0;
6850 + intc0.sysdev.cls = &intc_class;
6851 + ret = sysdev_register(&intc0.sysdev);
6852 +
6853 + return ret;
6854 +}
6855 +device_initcall(intc_init_sysdev);
6856 +
6857 unsigned long intc_get_pending(unsigned int group)
6858 {
6859 return intc_readl(&intc0, INTREQ0 + 4 * group);
6860 diff --exclude=.git -urN linux-2.6.25.6/arch/avr32/mach-at32ap/Kconfig avr32-2.6/arch/avr32/mach-at32ap/Kconfig
6861 --- linux-2.6.25.6/arch/avr32/mach-at32ap/Kconfig 2008-06-09 20:27:19.000000000 +0200
6862 +++ avr32-2.6/arch/avr32/mach-at32ap/Kconfig 2008-06-12 15:09:38.719815350 +0200
6863 @@ -26,6 +26,13 @@
6864
6865 endchoice
6866
6867 +config GPIO_DEV
6868 + bool "GPIO /dev interface"
6869 + select CONFIGFS_FS
6870 + default n
6871 + help
6872 + Say `Y' to enable a /dev interface to the GPIO pins.
6873 +
6874 endmenu
6875
6876 endif # PLATFORM_AT32AP
6877 diff --exclude=.git -urN linux-2.6.25.6/arch/avr32/mach-at32ap/Makefile avr32-2.6/arch/avr32/mach-at32ap/Makefile
6878 --- linux-2.6.25.6/arch/avr32/mach-at32ap/Makefile 2008-06-09 20:27:19.000000000 +0200
6879 +++ avr32-2.6/arch/avr32/mach-at32ap/Makefile 2008-06-12 15:09:38.719815350 +0200
6880 @@ -1,4 +1,9 @@
6881 -obj-y += at32ap.o clock.o intc.o extint.o pio.o hsmc.o
6882 -obj-$(CONFIG_CPU_AT32AP700X) += at32ap700x.o
6883 -obj-$(CONFIG_CPU_AT32AP700X) += time-tc.o
6884 +obj-y += pdc.o clock.o intc.o extint.o pio.o hsmc.o
6885 +obj-$(CONFIG_CPU_AT32AP700X) += at32ap700x.o pm-at32ap700x.o
6886 obj-$(CONFIG_CPU_FREQ_AT32AP) += cpufreq.o
6887 +obj-$(CONFIG_GPIO_DEV) += gpio-dev.o
6888 +obj-$(CONFIG_PM) += pm.o
6889 +
6890 +ifeq ($(CONFIG_PM_DEBUG),y)
6891 +CFLAGS_pm.o += -DDEBUG
6892 +endif
6893 diff --exclude=.git -urN linux-2.6.25.6/arch/avr32/mach-at32ap/pdc.c avr32-2.6/arch/avr32/mach-at32ap/pdc.c
6894 --- linux-2.6.25.6/arch/avr32/mach-at32ap/pdc.c 1970-01-01 01:00:00.000000000 +0100
6895 +++ avr32-2.6/arch/avr32/mach-at32ap/pdc.c 2008-06-12 15:09:38.723815860 +0200
6896 @@ -0,0 +1,48 @@
6897 +/*
6898 + * Copyright (C) 2006 Atmel Corporation
6899 + *
6900 + * This program is free software; you can redistribute it and/or modify
6901 + * it under the terms of the GNU General Public License version 2 as
6902 + * published by the Free Software Foundation.
6903 + */
6904 +
6905 +#include <linux/clk.h>
6906 +#include <linux/err.h>
6907 +#include <linux/init.h>
6908 +#include <linux/platform_device.h>
6909 +
6910 +static int __init pdc_probe(struct platform_device *pdev)
6911 +{
6912 + struct clk *pclk, *hclk;
6913 +
6914 + pclk = clk_get(&pdev->dev, "pclk");
6915 + if (IS_ERR(pclk)) {
6916 + dev_err(&pdev->dev, "no pclk defined\n");
6917 + return PTR_ERR(pclk);
6918 + }
6919 + hclk = clk_get(&pdev->dev, "hclk");
6920 + if (IS_ERR(hclk)) {
6921 + dev_err(&pdev->dev, "no hclk defined\n");
6922 + clk_put(pclk);
6923 + return PTR_ERR(hclk);
6924 + }
6925 +
6926 + clk_enable(pclk);
6927 + clk_enable(hclk);
6928 +
6929 + dev_info(&pdev->dev, "Atmel Peripheral DMA Controller enabled\n");
6930 + return 0;
6931 +}
6932 +
6933 +static struct platform_driver pdc_driver = {
6934 + .probe = pdc_probe,
6935 + .driver = {
6936 + .name = "pdc",
6937 + },
6938 +};
6939 +
6940 +static int __init pdc_init(void)
6941 +{
6942 + return platform_driver_register(&pdc_driver);
6943 +}
6944 +arch_initcall(pdc_init);
6945 diff --exclude=.git -urN linux-2.6.25.6/arch/avr32/mach-at32ap/pio.c avr32-2.6/arch/avr32/mach-at32ap/pio.c
6946 --- linux-2.6.25.6/arch/avr32/mach-at32ap/pio.c 2008-06-09 20:27:19.000000000 +0200
6947 +++ avr32-2.6/arch/avr32/mach-at32ap/pio.c 2008-06-12 15:09:38.723815860 +0200
6948 @@ -157,6 +157,82 @@
6949 dump_stack();
6950 }
6951
6952 +#ifdef CONFIG_GPIO_DEV
6953 +
6954 +/* Gang allocators and accessors; used by the GPIO /dev driver */
6955 +int at32_gpio_port_is_valid(unsigned int port)
6956 +{
6957 + return port < MAX_NR_PIO_DEVICES && pio_dev[port].regs != NULL;
6958 +}
6959 +
6960 +int at32_select_gpio_pins(unsigned int port, u32 pins, u32 oe_mask)
6961 +{
6962 + struct pio_device *pio;
6963 + u32 old, new;
6964 +
6965 + pio = &pio_dev[port];
6966 + BUG_ON(port > ARRAY_SIZE(pio_dev) || !pio->regs || (oe_mask & ~pins));
6967 +
6968 + /* Try to allocate the pins */
6969 + do {
6970 + old = pio->pinmux_mask;
6971 + if (old & pins)
6972 + return -EBUSY;
6973 +
6974 + new = old | pins;
6975 + } while (cmpxchg(&pio->pinmux_mask, old, new) != old);
6976 +
6977 + /* That went well, now configure the port */
6978 + pio_writel(pio, OER, oe_mask);
6979 + pio_writel(pio, PER, pins);
6980 +
6981 + return 0;
6982 +}
6983 +
6984 +void at32_deselect_pins(unsigned int port, u32 pins)
6985 +{
6986 + struct pio_device *pio;
6987 + u32 old, new;
6988 +
6989 + pio = &pio_dev[port];
6990 + BUG_ON(port > ARRAY_SIZE(pio_dev) || !pio->regs);
6991 +
6992 + /* Return to a "safe" mux configuration */
6993 + pio_writel(pio, PUER, pins);
6994 + pio_writel(pio, ODR, pins);
6995 +
6996 + /* Deallocate the pins */
6997 + do {
6998 + old = pio->pinmux_mask;
6999 + new = old & ~pins;
7000 + } while (cmpxchg(&pio->pinmux_mask, old, new) != old);
7001 +}
7002 +
7003 +u32 at32_gpio_get_value_multiple(unsigned int port, u32 pins)
7004 +{
7005 + struct pio_device *pio;
7006 +
7007 + pio = &pio_dev[port];
7008 + BUG_ON(port > ARRAY_SIZE(pio_dev) || !pio->regs);
7009 +
7010 + return pio_readl(pio, PDSR) & pins;
7011 +}
7012 +
7013 +void at32_gpio_set_value_multiple(unsigned int port, u32 value, u32 mask)
7014 +{
7015 + struct pio_device *pio;
7016 +
7017 + pio = &pio_dev[port];
7018 + BUG_ON(port > ARRAY_SIZE(pio_dev) || !pio->regs);
7019 +
7020 + /* No atomic updates for now... */
7021 + pio_writel(pio, CODR, ~value & mask);
7022 + pio_writel(pio, SODR, value & mask);
7023 +}
7024 +
7025 +#endif /* CONFIG_GPIO_DEV */
7026 +
7027 +
7028 /*--------------------------------------------------------------------------*/
7029
7030 /* GPIO API */
7031 @@ -318,6 +394,8 @@
7032 const char *label;
7033
7034 label = gpiochip_is_requested(chip, i);
7035 + if (!label && (imr & mask))
7036 + label = "[irq]";
7037 if (!label)
7038 continue;
7039
7040 diff --exclude=.git -urN linux-2.6.25.6/arch/avr32/mach-at32ap/pm-at32ap700x.S avr32-2.6/arch/avr32/mach-at32ap/pm-at32ap700x.S
7041 --- linux-2.6.25.6/arch/avr32/mach-at32ap/pm-at32ap700x.S 1970-01-01 01:00:00.000000000 +0100
7042 +++ avr32-2.6/arch/avr32/mach-at32ap/pm-at32ap700x.S 2008-06-12 15:09:38.723815860 +0200
7043 @@ -0,0 +1,174 @@
7044 +/*
7045 + * Low-level Power Management code.
7046 + *
7047 + * Copyright (C) 2008 Atmel Corporation
7048 + *
7049 + * This program is free software; you can redistribute it and/or modify
7050 + * it under the terms of the GNU General Public License version 2 as
7051 + * published by the Free Software Foundation.
7052 + */
7053 +#include <asm/asm.h>
7054 +#include <asm/asm-offsets.h>
7055 +#include <asm/thread_info.h>
7056 +#include <asm/arch/pm.h>
7057 +
7058 +#include "pm.h"
7059 +#include "sdramc.h"
7060 +
7061 +/* Same as 0xfff00000 but fits in a 21 bit signed immediate */
7062 +#define PM_BASE -0x100000
7063 +
7064 + .section .bss, "wa", @nobits
7065 + .global disable_idle_sleep
7066 + .type disable_idle_sleep, @object
7067 +disable_idle_sleep:
7068 + .int 4
7069 + .size disable_idle_sleep, . - disable_idle_sleep
7070 +
7071 + /* Keep this close to the irq handlers */
7072 + .section .irq.text, "ax", @progbits
7073 +
7074 + /*
7075 + * void cpu_enter_idle(void)
7076 + *
7077 + * Put the CPU into "idle" mode, in which it will consume
7078 + * significantly less power.
7079 + *
7080 + * If an interrupt comes along in the window between
7081 + * unmask_interrupts and the sleep instruction below, the
7082 + * interrupt code will adjust the return address so that we
7083 + * never execute the sleep instruction. This is required
7084 + * because the AP7000 doesn't unmask interrupts when entering
7085 + * sleep modes; later CPUs may not need this workaround.
7086 + */
7087 + .global cpu_enter_idle
7088 + .type cpu_enter_idle, @function
7089 +cpu_enter_idle:
7090 + mask_interrupts
7091 + get_thread_info r8
7092 + ld.w r9, r8[TI_flags]
7093 + bld r9, TIF_NEED_RESCHED
7094 + brcs .Lret_from_sleep
7095 + sbr r9, TIF_CPU_GOING_TO_SLEEP
7096 + st.w r8[TI_flags], r9
7097 + unmask_interrupts
7098 + sleep CPU_SLEEP_IDLE
7099 + .size cpu_idle_sleep, . - cpu_idle_sleep
7100 +
7101 + /*
7102 + * Common return path for PM functions that don't run from
7103 + * SRAM.
7104 + */
7105 + .global cpu_idle_skip_sleep
7106 + .type cpu_idle_skip_sleep, @function
7107 +cpu_idle_skip_sleep:
7108 + mask_interrupts
7109 + ld.w r9, r8[TI_flags]
7110 + cbr r9, TIF_CPU_GOING_TO_SLEEP
7111 + st.w r8[TI_flags], r9
7112 +.Lret_from_sleep:
7113 + unmask_interrupts
7114 + retal r12
7115 + .size cpu_idle_skip_sleep, . - cpu_idle_skip_sleep
7116 +
7117 +#ifdef CONFIG_PM
7118 + .section .init.text, "ax", @progbits
7119 +
7120 + .global pm_exception
7121 + .type pm_exception, @function
7122 +pm_exception:
7123 + /*
7124 + * Exceptions are masked when we switch to this handler, so
7125 + * we'll only get "unrecoverable" exceptions (offset 0.)
7126 + */
7127 + sub r12, pc, . - .Lpanic_msg
7128 + lddpc pc, .Lpanic_addr
7129 +
7130 + .align 2
7131 +.Lpanic_addr:
7132 + .long panic
7133 +.Lpanic_msg:
7134 + .asciz "Unrecoverable exception during suspend\n"
7135 + .size pm_exception, . - pm_exception
7136 +
7137 + .global pm_irq0
7138 + .type pm_irq0, @function
7139 +pm_irq0:
7140 + /* Disable interrupts and return after the sleep instruction */
7141 + mfsr r9, SYSREG_RSR_INT0
7142 + mtsr SYSREG_RAR_INT0, r8
7143 + sbr r9, SYSREG_GM_OFFSET
7144 + mtsr SYSREG_RSR_INT0, r9
7145 + rete
7146 +
7147 + /*
7148 + * void cpu_enter_standby(unsigned long sdramc_base)
7149 + *
7150 + * Enter PM_SUSPEND_STANDBY mode. At this point, all drivers
7151 + * are suspended and interrupts are disabled. Interrupts
7152 + * marked as 'wakeup' event sources may still come along and
7153 + * get us out of here.
7154 + *
7155 + * The SDRAM will be put into self-refresh mode (which does
7156 + * not require a clock from the CPU), and the CPU will be put
7157 + * into "frozen" mode (HSB bus stopped). The SDRAM controller
7158 + * will automatically bring the SDRAM into normal mode on the
7159 + * first access, and the power manager will automatically
7160 + * start the HSB and CPU clocks upon a wakeup event.
7161 + *
7162 + * This code uses the same "skip sleep" technique as above.
7163 + * It is very important that we jump directly to
7164 + * cpu_after_sleep after the sleep instruction since that's
7165 + * where we'll end up if the interrupt handler decides that we
7166 + * need to skip the sleep instruction.
7167 + */
7168 + .global pm_standby
7169 + .type pm_standby, @function
7170 +pm_standby:
7171 + /*
7172 + * interrupts are already masked at this point, and EVBA
7173 + * points to pm_exception above.
7174 + */
7175 + ld.w r10, r12[SDRAMC_LPR]
7176 + sub r8, pc, . - 1f /* return address for irq handler */
7177 + mov r11, SDRAMC_LPR_LPCB_SELF_RFR
7178 + bfins r10, r11, 0, 2 /* LPCB <- self Refresh */
7179 + sync 0 /* flush write buffer */
7180 + st.w r12[SDRAMC_LPR], r11 /* put SDRAM in self-refresh mode */
7181 + ld.w r11, r12[SDRAMC_LPR]
7182 + unmask_interrupts
7183 + sleep CPU_SLEEP_FROZEN
7184 +1: mask_interrupts
7185 + retal r12
7186 + .size pm_standby, . - pm_standby
7187 +
7188 + .global pm_suspend_to_ram
7189 + .type pm_suspend_to_ram, @function
7190 +pm_suspend_to_ram:
7191 + /*
7192 + * interrupts are already masked at this point, and EVBA
7193 + * points to pm_exception above.
7194 + */
7195 + mov r11, 0
7196 + cache r11[2], 8 /* clean all dcache lines */
7197 + sync 0 /* flush write buffer */
7198 + ld.w r10, r12[SDRAMC_LPR]
7199 + sub r8, pc, . - 1f /* return address for irq handler */
7200 + mov r11, SDRAMC_LPR_LPCB_SELF_RFR
7201 + bfins r10, r11, 0, 2 /* LPCB <- self refresh */
7202 + st.w r12[SDRAMC_LPR], r10 /* put SDRAM in self-refresh mode */
7203 + ld.w r11, r12[SDRAMC_LPR]
7204 +
7205 + unmask_interrupts
7206 + sleep CPU_SLEEP_STOP
7207 +1: mask_interrupts
7208 +
7209 + retal r12
7210 + .size pm_suspend_to_ram, . - pm_suspend_to_ram
7211 +
7212 + .global pm_sram_end
7213 + .type pm_sram_end, @function
7214 +pm_sram_end:
7215 + .size pm_sram_end, 0
7216 +
7217 +#endif /* CONFIG_PM */
7218 diff --exclude=.git -urN linux-2.6.25.6/arch/avr32/mach-at32ap/pm.c avr32-2.6/arch/avr32/mach-at32ap/pm.c
7219 --- linux-2.6.25.6/arch/avr32/mach-at32ap/pm.c 1970-01-01 01:00:00.000000000 +0100
7220 +++ avr32-2.6/arch/avr32/mach-at32ap/pm.c 2008-06-12 15:09:38.723815860 +0200
7221 @@ -0,0 +1,245 @@
7222 +/*
7223 + * AVR32 AP Power Management
7224 + *
7225 + * Copyright (C) 2008 Atmel Corporation
7226 + *
7227 + * This program is free software; you can redistribute it and/or
7228 + * modify it under the terms of the GNU General Public License
7229 + * version 2 as published by the Free Software Foundation.
7230 + */
7231 +#include <linux/io.h>
7232 +#include <linux/suspend.h>
7233 +#include <linux/vmalloc.h>
7234 +
7235 +#include <asm/cacheflush.h>
7236 +#include <asm/sysreg.h>
7237 +
7238 +#include <asm/arch/pm.h>
7239 +#include <asm/arch/sram.h>
7240 +
7241 +/* FIXME: This is only valid for AP7000 */
7242 +#define SDRAMC_BASE 0xfff03800
7243 +
7244 +#include "sdramc.h"
7245 +
7246 +#define SRAM_PAGE_FLAGS (SYSREG_BIT(TLBELO_D) | SYSREG_BF(SZ, 1) \
7247 + | SYSREG_BF(AP, 3) | SYSREG_BIT(G))
7248 +
7249 +
7250 +static unsigned long pm_sram_start;
7251 +static size_t pm_sram_size;
7252 +static struct vm_struct *pm_sram_area;
7253 +
7254 +static void (*avr32_pm_enter_standby)(unsigned long sdramc_base);
7255 +static void (*avr32_pm_enter_str)(unsigned long sdramc_base);
7256 +
7257 +/*
7258 + * Must be called with interrupts disabled. Exceptions will be masked
7259 + * on return (i.e. all exceptions will be "unrecoverable".)
7260 + */
7261 +static void *avr32_pm_map_sram(void)
7262 +{
7263 + unsigned long vaddr;
7264 + unsigned long page_addr;
7265 + u32 tlbehi;
7266 + u32 mmucr;
7267 +
7268 + vaddr = (unsigned long)pm_sram_area->addr;
7269 + page_addr = pm_sram_start & PAGE_MASK;
7270 +
7271 + /*
7272 + * Mask exceptions and grab the first TLB entry. We won't be
7273 + * needing it while sleeping.
7274 + */
7275 + asm volatile("ssrf %0" : : "i"(SYSREG_EM_OFFSET) : "memory");
7276 +
7277 + mmucr = sysreg_read(MMUCR);
7278 + tlbehi = sysreg_read(TLBEHI);
7279 + sysreg_write(MMUCR, SYSREG_BFINS(DRP, 0, mmucr));
7280 +
7281 + tlbehi = SYSREG_BF(ASID, SYSREG_BFEXT(ASID, tlbehi));
7282 + tlbehi |= vaddr & PAGE_MASK;
7283 + tlbehi |= SYSREG_BIT(TLBEHI_V);
7284 +
7285 + sysreg_write(TLBELO, page_addr | SRAM_PAGE_FLAGS);
7286 + sysreg_write(TLBEHI, tlbehi);
7287 + __builtin_tlbw();
7288 +
7289 + return (void *)(vaddr + pm_sram_start - page_addr);
7290 +}
7291 +
7292 +/*
7293 + * Must be called with interrupts disabled. Exceptions will be
7294 + * unmasked on return.
7295 + */
7296 +static void avr32_pm_unmap_sram(void)
7297 +{
7298 + u32 mmucr;
7299 + u32 tlbehi;
7300 + u32 tlbarlo;
7301 +
7302 + /* Going to update TLB entry at index 0 */
7303 + mmucr = sysreg_read(MMUCR);
7304 + tlbehi = sysreg_read(TLBEHI);
7305 + sysreg_write(MMUCR, SYSREG_BFINS(DRP, 0, mmucr));
7306 +
7307 + /* Clear the "valid" bit */
7308 + tlbehi = SYSREG_BF(ASID, SYSREG_BFEXT(ASID, tlbehi));
7309 + sysreg_write(TLBEHI, tlbehi);
7310 +
7311 + /* Mark it as "not accessed" */
7312 + tlbarlo = sysreg_read(TLBARLO);
7313 + sysreg_write(TLBARLO, tlbarlo | 0x80000000U);
7314 +
7315 + /* Update the TLB */
7316 + __builtin_tlbw();
7317 +
7318 + /* Unmask exceptions */
7319 + asm volatile("csrf %0" : : "i"(SYSREG_EM_OFFSET) : "memory");
7320 +}
7321 +
7322 +static int avr32_pm_valid_state(suspend_state_t state)
7323 +{
7324 + switch (state) {
7325 + case PM_SUSPEND_ON:
7326 + case PM_SUSPEND_STANDBY:
7327 + case PM_SUSPEND_MEM:
7328 + return 1;
7329 +
7330 + default:
7331 + return 0;
7332 + }
7333 +}
7334 +
7335 +static int avr32_pm_enter(suspend_state_t state)
7336 +{
7337 + u32 lpr_saved;
7338 + u32 evba_saved;
7339 + void *sram;
7340 +
7341 + switch (state) {
7342 + case PM_SUSPEND_STANDBY:
7343 + sram = avr32_pm_map_sram();
7344 +
7345 + /* Switch to in-sram exception handlers */
7346 + evba_saved = sysreg_read(EVBA);
7347 + sysreg_write(EVBA, (unsigned long)sram);
7348 +
7349 + /*
7350 + * Save the LPR register so that we can re-enable
7351 + * SDRAM Low Power mode on resume.
7352 + */
7353 + lpr_saved = sdramc_readl(LPR);
7354 + pr_debug("%s: Entering standby...\n", __func__);
7355 + avr32_pm_enter_standby(SDRAMC_BASE);
7356 + sdramc_writel(LPR, lpr_saved);
7357 +
7358 + /* Switch back to regular exception handlers */
7359 + sysreg_write(EVBA, evba_saved);
7360 +
7361 + avr32_pm_unmap_sram();
7362 + break;
7363 +
7364 + case PM_SUSPEND_MEM:
7365 + sram = avr32_pm_map_sram();
7366 +
7367 + /* Switch to in-sram exception handlers */
7368 + evba_saved = sysreg_read(EVBA);
7369 + sysreg_write(EVBA, (unsigned long)sram);
7370 +
7371 + /*
7372 + * Save the LPR register so that we can re-enable
7373 + * SDRAM Low Power mode on resume.
7374 + */
7375 + lpr_saved = sdramc_readl(LPR);
7376 + pr_debug("%s: Entering suspend-to-ram...\n", __func__);
7377 + avr32_pm_enter_str(SDRAMC_BASE);
7378 + sdramc_writel(LPR, lpr_saved);
7379 +
7380 + /* Switch back to regular exception handlers */
7381 + sysreg_write(EVBA, evba_saved);
7382 +
7383 + avr32_pm_unmap_sram();
7384 + break;
7385 +
7386 + case PM_SUSPEND_ON:
7387 + pr_debug("%s: Entering idle...\n", __func__);
7388 + cpu_enter_idle();
7389 + break;
7390 +
7391 + default:
7392 + pr_debug("%s: Invalid suspend state %d\n", __func__, state);
7393 + goto out;
7394 + }
7395 +
7396 + pr_debug("%s: wakeup\n", __func__);
7397 +
7398 +out:
7399 + return 0;
7400 +}
7401 +
7402 +static struct platform_suspend_ops avr32_pm_ops = {
7403 + .valid = avr32_pm_valid_state,
7404 + .enter = avr32_pm_enter,
7405 +};
7406 +
7407 +static unsigned long avr32_pm_offset(void *symbol)
7408 +{
7409 + extern u8 pm_exception[];
7410 +
7411 + return (unsigned long)symbol - (unsigned long)pm_exception;
7412 +}
7413 +
7414 +static int __init avr32_pm_init(void)
7415 +{
7416 + extern u8 pm_exception[];
7417 + extern u8 pm_irq0[];
7418 + extern u8 pm_standby[];
7419 + extern u8 pm_suspend_to_ram[];
7420 + extern u8 pm_sram_end[];
7421 + void *dst;
7422 +
7423 + /*
7424 + * To keep things simple, we depend on not needing more than a
7425 + * single page.
7426 + */
7427 + pm_sram_size = avr32_pm_offset(pm_sram_end);
7428 + if (pm_sram_size > PAGE_SIZE)
7429 + goto err;
7430 +
7431 + pm_sram_start = sram_alloc(pm_sram_size);
7432 + if (!pm_sram_start)
7433 + goto err_alloc_sram;
7434 +
7435 + /* Grab a virtual area we can use later on. */
7436 + pm_sram_area = get_vm_area(pm_sram_size, VM_IOREMAP);
7437 + if (!pm_sram_area)
7438 + goto err_vm_area;
7439 + pm_sram_area->phys_addr = pm_sram_start;
7440 +
7441 + local_irq_disable();
7442 + dst = avr32_pm_map_sram();
7443 + memcpy(dst, pm_exception, pm_sram_size);
7444 + flush_dcache_region(dst, pm_sram_size);
7445 + invalidate_icache_region(dst, pm_sram_size);
7446 + avr32_pm_unmap_sram();
7447 + local_irq_enable();
7448 +
7449 + avr32_pm_enter_standby = dst + avr32_pm_offset(pm_standby);
7450 + avr32_pm_enter_str = dst + avr32_pm_offset(pm_suspend_to_ram);
7451 + intc_set_suspend_handler(avr32_pm_offset(pm_irq0));
7452 +
7453 + suspend_set_ops(&avr32_pm_ops);
7454 +
7455 + printk("AVR32 AP Power Management enabled\n");
7456 +
7457 + return 0;
7458 +
7459 +err_vm_area:
7460 + sram_free(pm_sram_start, pm_sram_size);
7461 +err_alloc_sram:
7462 +err:
7463 + pr_err("AVR32 Power Management initialization failed\n");
7464 + return -ENOMEM;
7465 +}
7466 +arch_initcall(avr32_pm_init);
7467 diff --exclude=.git -urN linux-2.6.25.6/arch/avr32/mach-at32ap/sdramc.h avr32-2.6/arch/avr32/mach-at32ap/sdramc.h
7468 --- linux-2.6.25.6/arch/avr32/mach-at32ap/sdramc.h 1970-01-01 01:00:00.000000000 +0100
7469 +++ avr32-2.6/arch/avr32/mach-at32ap/sdramc.h 2008-06-12 15:09:38.723815860 +0200
7470 @@ -0,0 +1,76 @@
7471 +/*
7472 + * Register definitions for the AT32AP SDRAM Controller
7473 + *
7474 + * Copyright (C) 2008 Atmel Corporation
7475 + *
7476 + * This program is free software; you can redistribute it and/or
7477 + * modify it under the terms of the GNU General Public License
7478 + * version 2 as published by the Free Software Foundation.
7479 + */
7480 +
7481 +/* Register offsets */
7482 +#define SDRAMC_MR 0x0000
7483 +#define SDRAMC_TR 0x0004
7484 +#define SDRAMC_CR 0x0008
7485 +#define SDRAMC_HSR 0x000c
7486 +#define SDRAMC_LPR 0x0010
7487 +#define SDRAMC_IER 0x0014
7488 +#define SDRAMC_IDR 0x0018
7489 +#define SDRAMC_IMR 0x001c
7490 +#define SDRAMC_ISR 0x0020
7491 +#define SDRAMC_MDR 0x0024
7492 +
7493 +/* MR - Mode Register */
7494 +#define SDRAMC_MR_MODE_NORMAL ( 0 << 0)
7495 +#define SDRAMC_MR_MODE_NOP ( 1 << 0)
7496 +#define SDRAMC_MR_MODE_BANKS_PRECHARGE ( 2 << 0)
7497 +#define SDRAMC_MR_MODE_LOAD_MODE ( 3 << 0)
7498 +#define SDRAMC_MR_MODE_AUTO_REFRESH ( 4 << 0)
7499 +#define SDRAMC_MR_MODE_EXT_LOAD_MODE ( 5 << 0)
7500 +#define SDRAMC_MR_MODE_POWER_DOWN ( 6 << 0)
7501 +
7502 +/* CR - Configuration Register */
7503 +#define SDRAMC_CR_NC_8_BITS ( 0 << 0)
7504 +#define SDRAMC_CR_NC_9_BITS ( 1 << 0)
7505 +#define SDRAMC_CR_NC_10_BITS ( 2 << 0)
7506 +#define SDRAMC_CR_NC_11_BITS ( 3 << 0)
7507 +#define SDRAMC_CR_NR_11_BITS ( 0 << 2)
7508 +#define SDRAMC_CR_NR_12_BITS ( 1 << 2)
7509 +#define SDRAMC_CR_NR_13_BITS ( 2 << 2)
7510 +#define SDRAMC_CR_NB_2_BANKS ( 0 << 4)
7511 +#define SDRAMC_CR_NB_4_BANKS ( 1 << 4)
7512 +#define SDRAMC_CR_CAS(x) ((x) << 5)
7513 +#define SDRAMC_CR_DBW_32_BITS ( 0 << 7)
7514 +#define SDRAMC_CR_DBW_16_BITS ( 1 << 7)
7515 +#define SDRAMC_CR_TWR(x) ((x) << 8)
7516 +#define SDRAMC_CR_TRC(x) ((x) << 12)
7517 +#define SDRAMC_CR_TRP(x) ((x) << 16)
7518 +#define SDRAMC_CR_TRCD(x) ((x) << 20)
7519 +#define SDRAMC_CR_TRAS(x) ((x) << 24)
7520 +#define SDRAMC_CR_TXSR(x) ((x) << 28)
7521 +
7522 +/* HSR - High Speed Register */
7523 +#define SDRAMC_HSR_DA ( 1 << 0)
7524 +
7525 +/* LPR - Low Power Register */
7526 +#define SDRAMC_LPR_LPCB_INHIBIT ( 0 << 0)
7527 +#define SDRAMC_LPR_LPCB_SELF_RFR ( 1 << 0)
7528 +#define SDRAMC_LPR_LPCB_PDOWN ( 2 << 0)
7529 +#define SDRAMC_LPR_LPCB_DEEP_PDOWN ( 3 << 0)
7530 +#define SDRAMC_LPR_PASR(x) ((x) << 4)
7531 +#define SDRAMC_LPR_TCSR(x) ((x) << 8)
7532 +#define SDRAMC_LPR_DS(x) ((x) << 10)
7533 +#define SDRAMC_LPR_TIMEOUT(x) ((x) << 12)
7534 +
7535 +/* IER/IDR/IMR/ISR - Interrupt Enable/Disable/Mask/Status Register */
7536 +#define SDRAMC_ISR_RES ( 1 << 0)
7537 +
7538 +/* MDR - Memory Device Register */
7539 +#define SDRAMC_MDR_MD_SDRAM ( 0 << 0)
7540 +#define SDRAMC_MDR_MD_LOW_PWR_SDRAM ( 1 << 0)
7541 +
7542 +/* Register access macros */
7543 +#define sdramc_readl(reg) \
7544 + __raw_readl((void __iomem __force *)SDRAMC_BASE + SDRAMC_##reg)
7545 +#define sdramc_writel(reg, value) \
7546 + __raw_writel(value, (void __iomem __force *)SDRAMC_BASE + SDRAMC_##reg)
7547 diff --exclude=.git -urN linux-2.6.25.6/arch/avr32/mach-at32ap/time-tc.c avr32-2.6/arch/avr32/mach-at32ap/time-tc.c
7548 --- linux-2.6.25.6/arch/avr32/mach-at32ap/time-tc.c 2008-06-09 20:27:19.000000000 +0200
7549 +++ avr32-2.6/arch/avr32/mach-at32ap/time-tc.c 1970-01-01 01:00:00.000000000 +0100
7550 @@ -1,218 +0,0 @@
7551 -/*
7552 - * Copyright (C) 2004-2007 Atmel Corporation
7553 - *
7554 - * Based on MIPS implementation arch/mips/kernel/time.c
7555 - * Copyright 2001 MontaVista Software Inc.
7556 - *
7557 - * This program is free software; you can redistribute it and/or modify
7558 - * it under the terms of the GNU General Public License version 2 as
7559 - * published by the Free Software Foundation.
7560 - */
7561 -
7562 -#include <linux/clk.h>
7563 -#include <linux/clocksource.h>
7564 -#include <linux/time.h>
7565 -#include <linux/module.h>
7566 -#include <linux/interrupt.h>
7567 -#include <linux/irq.h>
7568 -#include <linux/kernel_stat.h>
7569 -#include <linux/errno.h>
7570 -#include <linux/init.h>
7571 -#include <linux/profile.h>
7572 -#include <linux/sysdev.h>
7573 -#include <linux/err.h>
7574 -
7575 -#include <asm/div64.h>
7576 -#include <asm/sysreg.h>
7577 -#include <asm/io.h>
7578 -#include <asm/sections.h>
7579 -
7580 -#include <asm/arch/time.h>
7581 -
7582 -/* how many counter cycles in a jiffy? */
7583 -static u32 cycles_per_jiffy;
7584 -
7585 -/* the count value for the next timer interrupt */
7586 -static u32 expirelo;
7587 -
7588 -/* the I/O registers of the TC module */
7589 -static void __iomem *ioregs;
7590 -
7591 -cycle_t read_cycle_count(void)
7592 -{
7593 - return (cycle_t)timer_read(ioregs, 0, CV);
7594 -}
7595 -
7596 -struct clocksource clocksource_avr32 = {
7597 - .name = "avr32",
7598 - .rating = 342,
7599 - .read = read_cycle_count,
7600 - .mask = CLOCKSOURCE_MASK(16),
7601 - .shift = 16,
7602 - .flags = CLOCK_SOURCE_IS_CONTINUOUS,
7603 -};
7604 -
7605 -static void avr32_timer_ack(void)
7606 -{
7607 - u16 count = expirelo;
7608 -
7609 - /* Ack this timer interrupt and set the next one, use a u16
7610 - * variable so it will wrap around correctly */
7611 - count += cycles_per_jiffy;
7612 - expirelo = count;
7613 - timer_write(ioregs, 0, RC, expirelo);
7614 -
7615 - /* Check to see if we have missed any timer interrupts */
7616 - count = timer_read(ioregs, 0, CV);
7617 - if ((count - expirelo) < 0x7fff) {
7618 - expirelo = count + cycles_per_jiffy;
7619 - timer_write(ioregs, 0, RC, expirelo);
7620 - }
7621 -}
7622 -
7623 -u32 avr32_hpt_read(void)
7624 -{
7625 - return timer_read(ioregs, 0, CV);
7626 -}
7627 -
7628 -static int avr32_timer_calc_div_and_set_jiffies(struct clk *pclk)
7629 -{
7630 - unsigned int cycles_max = (clocksource_avr32.mask + 1) / 2;
7631 - unsigned int divs[] = { 4, 8, 16, 32 };
7632 - int divs_size = ARRAY_SIZE(divs);
7633 - int i = 0;
7634 - unsigned long count_hz;
7635 - unsigned long shift;
7636 - unsigned long mult;
7637 - int clock_div = -1;
7638 - u64 tmp;
7639 -
7640 - shift = clocksource_avr32.shift;
7641 -
7642 - do {
7643 - count_hz = clk_get_rate(pclk) / divs[i];
7644 - mult = clocksource_hz2mult(count_hz, shift);
7645 - clocksource_avr32.mult = mult;
7646 -
7647 - tmp = TICK_NSEC;
7648 - tmp <<= shift;
7649 - tmp += mult / 2;
7650 - do_div(tmp, mult);
7651 -
7652 - cycles_per_jiffy = tmp;
7653 - } while (cycles_per_jiffy > cycles_max && ++i < divs_size);
7654 -
7655 - clock_div = i + 1;
7656 -
7657 - if (clock_div > divs_size) {
7658 - pr_debug("timer: could not calculate clock divider\n");
7659 - return -EFAULT;
7660 - }
7661 -
7662 - /* Set the clock divider */
7663 - timer_write(ioregs, 0, CMR, TIMER_BF(CMR_TCCLKS, clock_div));
7664 -
7665 - return 0;
7666 -}
7667 -
7668 -int avr32_hpt_init(unsigned int count)
7669 -{
7670 - struct resource *regs;
7671 - struct clk *pclk;
7672 - int irq = -1;
7673 - int ret = 0;
7674 -
7675 - ret = -ENXIO;
7676 -
7677 - irq = platform_get_irq(&at32_systc0_device, 0);
7678 - if (irq < 0) {
7679 - pr_debug("timer: could not get irq\n");
7680 - goto out_error;
7681 - }
7682 -
7683 - pclk = clk_get(&at32_systc0_device.dev, "pclk");
7684 - if (IS_ERR(pclk)) {
7685 - pr_debug("timer: could not get clk: %ld\n", PTR_ERR(pclk));
7686 - goto out_error;
7687 - }
7688 - clk_enable(pclk);
7689 -
7690 - regs = platform_get_resource(&at32_systc0_device, IORESOURCE_MEM, 0);
7691 - if (!regs) {
7692 - pr_debug("timer: could not get resource\n");
7693 - goto out_error_clk;
7694 - }
7695 -
7696 - ioregs = ioremap(regs->start, regs->end - regs->start + 1);
7697 - if (!ioregs) {
7698 - pr_debug("timer: could not get ioregs\n");
7699 - goto out_error_clk;
7700 - }
7701 -
7702 - ret = avr32_timer_calc_div_and_set_jiffies(pclk);
7703 - if (ret)
7704 - goto out_error_io;
7705 -
7706 - ret = setup_irq(irq, &timer_irqaction);
7707 - if (ret) {
7708 - pr_debug("timer: could not request irq %d: %d\n",
7709 - irq, ret);
7710 - goto out_error_io;
7711 - }
7712 -
7713 - expirelo = (timer_read(ioregs, 0, CV) / cycles_per_jiffy + 1)
7714 - * cycles_per_jiffy;
7715 -
7716 - /* Enable clock and interrupts on RC compare */
7717 - timer_write(ioregs, 0, CCR, TIMER_BIT(CCR_CLKEN));
7718 - timer_write(ioregs, 0, IER, TIMER_BIT(IER_CPCS));
7719 - /* Set cycles to first interrupt */
7720 - timer_write(ioregs, 0, RC, expirelo);
7721 -
7722 - printk(KERN_INFO "timer: AT32AP system timer/counter at 0x%p irq %d\n",
7723 - ioregs, irq);
7724 -
7725 - return 0;
7726 -
7727 -out_error_io:
7728 - iounmap(ioregs);
7729 -out_error_clk:
7730 - clk_put(pclk);
7731 -out_error:
7732 - return ret;
7733 -}
7734 -
7735 -int avr32_hpt_start(void)
7736 -{
7737 - timer_write(ioregs, 0, CCR, TIMER_BIT(CCR_SWTRG));
7738 - return 0;
7739 -}
7740 -
7741 -irqreturn_t timer_interrupt(int irq, void *dev_id)
7742 -{
7743 - unsigned int sr = timer_read(ioregs, 0, SR);
7744 -
7745 - if (sr & TIMER_BIT(SR_CPCS)) {
7746 - /* ack timer interrupt and try to set next interrupt */
7747 - avr32_timer_ack();
7748 -
7749 - /*
7750 - * Call the generic timer interrupt handler
7751 - */
7752 - write_seqlock(&xtime_lock);
7753 - do_timer(1);
7754 - write_sequnlock(&xtime_lock);
7755 -
7756 - /*
7757 - * In UP mode, we call local_timer_interrupt() to do profiling
7758 - * and process accounting.
7759 - *
7760 - * SMP is not supported yet.
7761 - */
7762 - local_timer_interrupt(irq, dev_id);
7763 -
7764 - return IRQ_HANDLED;
7765 - }
7766 -
7767 - return IRQ_NONE;
7768 -}
7769 diff --exclude=.git -urN linux-2.6.25.6/arch/avr32/Makefile avr32-2.6/arch/avr32/Makefile
7770 --- linux-2.6.25.6/arch/avr32/Makefile 2008-06-09 20:27:19.000000000 +0200
7771 +++ avr32-2.6/arch/avr32/Makefile 2008-06-12 15:09:38.711815728 +0200
7772 @@ -32,6 +32,7 @@
7773 core-y += arch/avr32/kernel/
7774 core-y += arch/avr32/mm/
7775 drivers-$(CONFIG_OPROFILE) += arch/avr32/oprofile/
7776 +drivers-y += arch/avr32/drivers/
7777 libs-y += arch/avr32/lib/
7778
7779 archincdir-$(CONFIG_PLATFORM_AT32AP) := arch-at32ap
7780 diff --exclude=.git -urN linux-2.6.25.6/arch/avr32/mm/init.c avr32-2.6/arch/avr32/mm/init.c
7781 --- linux-2.6.25.6/arch/avr32/mm/init.c 2008-06-09 20:27:19.000000000 +0200
7782 +++ avr32-2.6/arch/avr32/mm/init.c 2008-06-12 15:09:38.727815811 +0200
7783 @@ -11,6 +11,7 @@
7784 #include <linux/swap.h>
7785 #include <linux/init.h>
7786 #include <linux/mmzone.h>
7787 +#include <linux/module.h>
7788 #include <linux/bootmem.h>
7789 #include <linux/pagemap.h>
7790 #include <linux/nodemask.h>
7791 @@ -28,15 +29,13 @@
7792 pgd_t swapper_pg_dir[PTRS_PER_PGD];
7793
7794 struct page *empty_zero_page;
7795 +EXPORT_SYMBOL(empty_zero_page);
7796
7797 /*
7798 * Cache of MMU context last used.
7799 */
7800 unsigned long mmu_context_cache = NO_CONTEXT;
7801
7802 -#define START_PFN (NODE_DATA(0)->bdata->node_boot_start >> PAGE_SHIFT)
7803 -#define MAX_LOW_PFN (NODE_DATA(0)->bdata->node_low_pfn)
7804 -
7805 void show_mem(void)
7806 {
7807 int total = 0, reserved = 0, cached = 0;
7808 diff --exclude=.git -urN linux-2.6.25.6/arch/avr32/oprofile/op_model_avr32.c avr32-2.6/arch/avr32/oprofile/op_model_avr32.c
7809 --- linux-2.6.25.6/arch/avr32/oprofile/op_model_avr32.c 2008-06-09 20:27:19.000000000 +0200
7810 +++ avr32-2.6/arch/avr32/oprofile/op_model_avr32.c 2008-06-12 15:03:55.895816260 +0200
7811 @@ -16,7 +16,6 @@
7812 #include <linux/sched.h>
7813 #include <linux/types.h>
7814
7815 -#include <asm/intc.h>
7816 #include <asm/sysreg.h>
7817 #include <asm/system.h>
7818
7819 diff --exclude=.git -urN linux-2.6.25.6/Documentation/ABI/stable/sysfs-class-ubi avr32-2.6/Documentation/ABI/stable/sysfs-class-ubi
7820 --- linux-2.6.25.6/Documentation/ABI/stable/sysfs-class-ubi 1970-01-01 01:00:00.000000000 +0100
7821 +++ avr32-2.6/Documentation/ABI/stable/sysfs-class-ubi 2008-06-12 15:03:55.427814470 +0200
7822 @@ -0,0 +1,212 @@
7823 +What: /sys/class/ubi/
7824 +Date: July 2006
7825 +KernelVersion: 2.6.22
7826 +Contact: Artem Bityutskiy <dedekind@infradead.org>
7827 +Description:
7828 + The ubi/ class sub-directory belongs to the UBI subsystem and
7829 + provides general UBI information, per-UBI device information
7830 + and per-UBI volume information.
7831 +
7832 +What: /sys/class/ubi/version
7833 +Date: July 2006
7834 +KernelVersion: 2.6.22
7835 +Contact: Artem Bityutskiy <dedekind@infradead.org>
7836 +Description:
7837 + This file contains version of the latest supported UBI on-media
7838 + format. Currently it is 1, and there is no plan to change this.
7839 + However, if in the future UBI needs on-flash format changes
7840 + which cannot be done in a compatible manner, a new format
7841 + version will be added. So this is a mechanism for possible
7842 + future backward-compatible (but forward-incompatible)
7843 + improvements.
7844 +
7845 +What: /sys/class/ubiX/
7846 +Date: July 2006
7847 +KernelVersion: 2.6.22
7848 +Contact: Artem Bityutskiy <dedekind@infradead.org>
7849 +Description:
7850 + The /sys/class/ubi0, /sys/class/ubi1, etc directories describe
7851 + UBI devices (UBI device 0, 1, etc). They contain general UBI
7852 + device information and per UBI volume information (each UBI
7853 + device may have many UBI volumes)
7854 +
7855 +What: /sys/class/ubi/ubiX/avail_eraseblocks
7856 +Date: July 2006
7857 +KernelVersion: 2.6.22
7858 +Contact: Artem Bityutskiy <dedekind@infradead.org>
7859 +Description:
7860 + Amount of available logical eraseblock. For example, one may
7861 + create a new UBI volume which has this amount of logical
7862 + eraseblocks.
7863 +
7864 +What: /sys/class/ubi/ubiX/bad_peb_count
7865 +Date: July 2006
7866 +KernelVersion: 2.6.22
7867 +Contact: Artem Bityutskiy <dedekind@infradead.org>
7868 +Description:
7869 + Count of bad physical eraseblocks on the underlying MTD device.
7870 +
7871 +What: /sys/class/ubi/ubiX/bgt_enabled
7872 +Date: July 2006
7873 +KernelVersion: 2.6.22
7874 +Contact: Artem Bityutskiy <dedekind@infradead.org>
7875 +Description:
7876 + Contains ASCII "0\n" if the UBI background thread is disabled,
7877 + and ASCII "1\n" if it is enabled.
7878 +
7879 +What: /sys/class/ubi/ubiX/dev
7880 +Date: July 2006
7881 +KernelVersion: 2.6.22
7882 +Contact: Artem Bityutskiy <dedekind@infradead.org>
7883 +Description:
7884 + Major and minor numbers of the character device corresponding
7885 + to this UBI device (in <major>:<minor> format).
7886 +
7887 +What: /sys/class/ubi/ubiX/eraseblock_size
7888 +Date: July 2006
7889 +KernelVersion: 2.6.22
7890 +Contact: Artem Bityutskiy <dedekind@infradead.org>
7891 +Description:
7892 + Maximum logical eraseblock size this UBI device may provide. UBI
7893 + volumes may have smaller logical eraseblock size because of their
7894 + alignment.
7895 +
7896 +What: /sys/class/ubi/ubiX/max_ec
7897 +Date: July 2006
7898 +KernelVersion: 2.6.22
7899 +Contact: Artem Bityutskiy <dedekind@infradead.org>
7900 +Description:
7901 + Maximum physical eraseblock erase counter value.
7902 +
7903 +What: /sys/class/ubi/ubiX/max_vol_count
7904 +Date: July 2006
7905 +KernelVersion: 2.6.22
7906 +Contact: Artem Bityutskiy <dedekind@infradead.org>
7907 +Description:
7908 + Maximum number of volumes which this UBI device may have.
7909 +
7910 +What: /sys/class/ubi/ubiX/min_io_size
7911 +Date: July 2006
7912 +KernelVersion: 2.6.22
7913 +Contact: Artem Bityutskiy <dedekind@infradead.org>
7914 +Description:
7915 + Minimum input/output unit size. All the I/O may only be done
7916 + in fractions of the contained number.
7917 +
7918 +What: /sys/class/ubi/ubiX/mtd_num
7919 +Date: January 2008
7920 +KernelVersion: 2.6.25
7921 +Contact: Artem Bityutskiy <dedekind@infradead.org>
7922 +Description:
7923 + Number of the underlying MTD device.
7924 +
7925 +What: /sys/class/ubi/ubiX/reserved_for_bad
7926 +Date: July 2006
7927 +KernelVersion: 2.6.22
7928 +Contact: Artem Bityutskiy <dedekind@infradead.org>
7929 +Description:
7930 + Number of physical eraseblocks reserved for bad block handling.
7931 +
7932 +What: /sys/class/ubi/ubiX/total_eraseblocks
7933 +Date: July 2006
7934 +KernelVersion: 2.6.22
7935 +Contact: Artem Bityutskiy <dedekind@infradead.org>
7936 +Description:
7937 + Total number of good (not marked as bad) physical eraseblocks on
7938 + the underlying MTD device.
7939 +
7940 +What: /sys/class/ubi/ubiX/volumes_count
7941 +Date: July 2006
7942 +KernelVersion: 2.6.22
7943 +Contact: Artem Bityutskiy <dedekind@infradead.org>
7944 +Description:
7945 + Count of volumes on this UBI device.
7946 +
7947 +What: /sys/class/ubi/ubiX/ubiX_Y/
7948 +Date: July 2006
7949 +KernelVersion: 2.6.22
7950 +Contact: Artem Bityutskiy <dedekind@infradead.org>
7951 +Description:
7952 + The /sys/class/ubi/ubiX/ubiX_0/, /sys/class/ubi/ubiX/ubiX_1/,
7953 + etc directories describe UBI volumes on UBI device X (volumes
7954 + 0, 1, etc).
7955 +
7956 +What: /sys/class/ubi/ubiX/ubiX_Y/alignment
7957 +Date: July 2006
7958 +KernelVersion: 2.6.22
7959 +Contact: Artem Bityutskiy <dedekind@infradead.org>
7960 +Description:
7961 + Volume alignment - the value the logical eraseblock size of
7962 + this volume has to be aligned on. For example, 2048 means that
7963 + logical eraseblock size is multiple of 2048. In other words,
7964 + volume logical eraseblock size is UBI device logical eraseblock
7965 + size aligned to the alignment value.
7966 +
7967 +What: /sys/class/ubi/ubiX/ubiX_Y/corrupted
7968 +Date: July 2006
7969 +KernelVersion: 2.6.22
7970 +Contact: Artem Bityutskiy <dedekind@infradead.org>
7971 +Description:
7972 + Contains ASCII "0\n" if the UBI volume is OK, and ASCII "1\n"
7973 + if it is corrupted (e.g., due to an interrupted volume update).
7974 +
7975 +What: /sys/class/ubi/ubiX/ubiX_Y/data_bytes
7976 +Date: July 2006
7977 +KernelVersion: 2.6.22
7978 +Contact: Artem Bityutskiy <dedekind@infradead.org>
7979 +Description:
7980 + The amount of data this volume contains. This value makes sense
7981 + only for static volumes, and for dynamic volume it equivalent
7982 + to the total volume size in bytes.
7983 +
7984 +What: /sys/class/ubi/ubiX/ubiX_Y/dev
7985 +Date: July 2006
7986 +KernelVersion: 2.6.22
7987 +Contact: Artem Bityutskiy <dedekind@infradead.org>
7988 +Description:
7989 + Major and minor numbers of the character device corresponding
7990 + to this UBI volume (in <major>:<minor> format).
7991 +
7992 +What: /sys/class/ubi/ubiX/ubiX_Y/name
7993 +Date: July 2006
7994 +KernelVersion: 2.6.22
7995 +Contact: Artem Bityutskiy <dedekind@infradead.org>
7996 +Description:
7997 + Volume name.
7998 +
7999 +What: /sys/class/ubi/ubiX/ubiX_Y/reserved_ebs
8000 +Date: July 2006
8001 +KernelVersion: 2.6.22
8002 +Contact: Artem Bityutskiy <dedekind@infradead.org>
8003 +Description:
8004 + Count of physical eraseblock reserved for this volume.
8005 + Equivalent to the volume size in logical eraseblocks.
8006 +
8007 +What: /sys/class/ubi/ubiX/ubiX_Y/type
8008 +Date: July 2006
8009 +KernelVersion: 2.6.22
8010 +Contact: Artem Bityutskiy <dedekind@infradead.org>
8011 +Description:
8012 + Volume type. Contains ASCII "dynamic\n" for dynamic volumes and
8013 + "static\n" for static volumes.
8014 +
8015 +What: /sys/class/ubi/ubiX/ubiX_Y/upd_marker
8016 +Date: July 2006
8017 +KernelVersion: 2.6.22
8018 +Contact: Artem Bityutskiy <dedekind@infradead.org>
8019 +Description:
8020 + Contains ASCII "0\n" if the update marker is not set for this
8021 + volume, and "1\n" if it is set. The update marker is set when
8022 + volume update starts, and cleaned when it ends. So the presence
8023 + of the update marker indicates that the volume is being updated
8024 + at the moment of the update was interrupted. The later may be
8025 + checked using the "corrupted" sysfs file.
8026 +
8027 +What: /sys/class/ubi/ubiX/ubiX_Y/usable_eb_size
8028 +Date: July 2006
8029 +KernelVersion: 2.6.22
8030 +Contact: Artem Bityutskiy <dedekind@infradead.org>
8031 +Description:
8032 + Logical eraseblock size of this volume. Equivalent to logical
8033 + eraseblock size of the device aligned on the volume alignment
8034 + value.
8035 diff --exclude=.git -urN linux-2.6.25.6/Documentation/filesystems/ubifs.txt avr32-2.6/Documentation/filesystems/ubifs.txt
8036 --- linux-2.6.25.6/Documentation/filesystems/ubifs.txt 1970-01-01 01:00:00.000000000 +0100
8037 +++ avr32-2.6/Documentation/filesystems/ubifs.txt 2008-06-12 15:09:38.451815572 +0200
8038 @@ -0,0 +1,163 @@
8039 +Introduction
8040 +=============
8041 +
8042 +UBIFS file-system stands for UBI File System. UBI stands for "Unsorted
8043 +Block Images". UBIFS is a flash file system, which means it is designed
8044 +to work with flash devices. It is important to understand, that UBIFS
8045 +is completely different to any traditional file-system in Linux, like
8046 +Ext2, XFS, JFS, etc. UBIFS represents a separate class of file-systems
8047 +which work with MTD devices, not block devices. The other Linux
8048 +file-system of this class is JFFS2.
8049 +
8050 +To make it more clear, here is a small comparison of MTD devices and
8051 +block devices.
8052 +
8053 +1 MTD devices represent flash devices and they consist of eraseblocks of
8054 + rather large size, typically about 128KiB. Block devices consist of
8055 + small blocks, typically 512 bytes.
8056 +2 MTD devices support 3 main operations - read from some offset within an
8057 + eraseblock, write to some offset within an eraseblock, and erase a whole
8058 + eraseblock. Block devices support 2 main operations - read a whole
8059 + block and write a whole block.
8060 +3 The whole eraseblock has to be erased before it becomes possible to
8061 + re-write its contents. Blocks may be just re-written.
8062 +4 Eraseblocks become worn out after some number of erase cycles -
8063 + typically 100K-1G for SLC NAND and NOR flashes, and 1K-10K for MLC
8064 + NAND flashes. Blocks do not have the wear-out property.
8065 +5 Eraseblocks may become bad (only on NAND flashes) and software should
8066 + deal with this. Blocks on hard drives typically do not become bad,
8067 + because hardware has mechanisms to substitute bad blocks, at least in
8068 + modern LBA disks.
8069 +
8070 +It should be quite obvious why UBIFS is very different to traditional
8071 +file-systems.
8072 +
8073 +UBIFS works on top of UBI. UBI is a separate software layer which may be
8074 +found in drivers/mtd/ubi. UBI is basically a volume management and
8075 +wear-leveling layer. It provides so called UBI volumes which is a higher
8076 +level abstraction than a MTD device. The programming model of UBI devices
8077 +is very similar to MTD devices - they still consist of large eraseblocks,
8078 +they have read/write/erase operations, but UBI devices are devoid of
8079 +limitations like wear and bad blocks (items 4 and 5 in the above list).
8080 +
8081 +In a sense, UBIFS is a next generation of JFFS2 file-system, but it is
8082 +very different and incompatible to JFFS2. The following are the main
8083 +differences.
8084 +
8085 +* JFFS2 works on top of MTD devices, UBIFS depends on UBI and works on
8086 + top of UBI volumes.
8087 +* JFFS2 does not have on-media index and has to build it while mounting,
8088 + which requires full media scan. UBIFS maintains the FS indexing
8089 + information on the flash media and does not require full media scan,
8090 + so it mounts many times faster than JFFS2.
8091 +* JFFS2 is a write-through file-system, while UBIFS supports write-back,
8092 + which makes UBIFS much faster on writes.
8093 +
8094 +Similarly to JFFS2, UBIFS supports on-the-flight compression which makes
8095 +it possible to fit quite a lot of data to the flash.
8096 +
8097 +Similarly to JFFS2, UBIFS is tolerant of unclean reboots and power-cuts.
8098 +It does not need stuff like ckfs.ext2. UBIFS automatically replays its
8099 +journal and recovers from crashes, ensuring that the on-flash data
8100 +structures are consistent.
8101 +
8102 +UBIFS scales logarithmically (most of the data structures it uses are
8103 +trees), so the mount time and memory consumption do not linearly depend
8104 +on the flash size, like in case of JFFS2. This is because UBIFS
8105 +maintains the FS index on the flash media. However, UBIFS depends on
8106 +UBI, which scales linearly. So overall UBI/UBIFS stack scales linearly.
8107 +Nevertheless, UBI/UBIFS scales considerably better than JFFS2.
8108 +
8109 +The authors of UBIFS believe, that it is possible to develop UBI2 which
8110 +would scale logarithmically as well. UBI2 would support the same API as UBI,
8111 +but it would be binary incompatible to UBI. So UBIFS would not need to be
8112 +changed to use UBI2
8113 +
8114 +
8115 +Mount options
8116 +=============
8117 +
8118 +(*) == default.
8119 +
8120 +norm_unmount (*) commit on unmount; the journal is committed
8121 + when the file-system is unmounted so that the
8122 + next mount does not have to replay the journal
8123 + and it becomes very fast;
8124 +fast_unmount do not commit on unmount; this option makes
8125 + unmount faster, but the next mount slower
8126 + because of the need to replay the journal.
8127 +
8128 +
8129 +Quick usage instructions
8130 +========================
8131 +
8132 +The UBI volume to mount is specified using "ubiX_Y" or "ubiX:NAME" syntax,
8133 +where "X" is UBI device number, "Y" is UBI volume number, and "NAME" is
8134 +UBI volume name.
8135 +
8136 +Mount volume 0 on UBI device 0 to /mnt/ubifs:
8137 +$ mount -t ubifs ubi0_0 /mnt/ubifs
8138 +
8139 +Mount "rootfs" volume of UBI device 0 to /mnt/ubifs ("rootfs" is volume
8140 +name):
8141 +$ mount -t ubifs ubi0:rootfs /mnt/ubifs
8142 +
8143 +The following is an example of the kernel boot arguments to attach mtd0
8144 +to UBI and mount volume "rootfs":
8145 +ubi.mtd=0 root=ubi0:rootfs rootfstype=ubifs
8146 +
8147 +
8148 +Module Parameters for Debugging
8149 +===============================
8150 +
8151 +When UBIFS has been compiled with debugging enabled, there are 3 module
8152 +parameters that are available to control aspects of testing and debugging.
8153 +The parameters are unsigned integers where each bit controls an option.
8154 +The parameters are:
8155 +
8156 +debug_msgs Selects which debug messages to display, as follows:
8157 +
8158 + Message Type Flag value
8159 +
8160 + General messages 1
8161 + Journal messages 2
8162 + Mount messages 4
8163 + Commit messages 8
8164 + LEB search messages 16
8165 + Budgeting messages 32
8166 + Garbage collection messages 64
8167 + Tree Node Cache (TNC) messages 128
8168 + LEB properties (lprops) messages 256
8169 + Input/output messages 512
8170 + Log messages 1024
8171 + Scan messages 2048
8172 + Recovery messages 4096
8173 +
8174 +debug_chks Selects extra checks that UBIFS can do while running:
8175 +
8176 + Check Flag value
8177 +
8178 + General checks 1
8179 + Check Tree Node Cache (TNC) 2
8180 + Check indexing tree size 4
8181 + Check orphan area 8
8182 + Check old indexing tree 16
8183 + Check LEB properties (lprops) 32
8184 +
8185 +debug_tsts Selects a mode of testing, as follows:
8186 +
8187 + Test mode Flag value
8188 +
8189 + Force in-the-gaps method 2
8190 + Failure mode for recovery testing 4
8191 +
8192 +For example, set debug_msgs to 5 to display General messages and Mount
8193 +messages.
8194 +
8195 +
8196 +References
8197 +==========
8198 +
8199 +UBIFS documentation and FAQ/HOWTO at the MTD web site:
8200 +http://www.linux-mtd.infradead.org/doc/ubifs.html
8201 +http://www.linux-mtd.infradead.org/faq/ubifs.html
8202 diff --exclude=.git -urN linux-2.6.25.6/drivers/char/Kconfig avr32-2.6/drivers/char/Kconfig
8203 --- linux-2.6.25.6/drivers/char/Kconfig 2008-06-09 20:27:19.000000000 +0200
8204 +++ avr32-2.6/drivers/char/Kconfig 2008-06-12 15:09:40.071816052 +0200
8205 @@ -706,7 +706,7 @@
8206
8207 config RTC
8208 tristate "Enhanced Real Time Clock Support"
8209 - depends on !PPC && !PARISC && !IA64 && !M68K && !SPARC && !FRV && !ARM && !SUPERH && !S390
8210 + depends on !PPC && !PARISC && !IA64 && !M68K && !SPARC && !FRV && !ARM && !SUPERH && !S390 && !AVR32
8211 ---help---
8212 If you say Y here and create a character special file /dev/rtc with
8213 major number 10 and minor number 135 using mknod ("man mknod"), you
8214 @@ -776,7 +776,7 @@
8215
8216 config GEN_RTC
8217 tristate "Generic /dev/rtc emulation"
8218 - depends on RTC!=y && !IA64 && !ARM && !M32R && !MIPS && !SPARC && !FRV && !S390 && !SUPERH
8219 + depends on RTC!=y && !IA64 && !ARM && !M32R && !MIPS && !SPARC && !FRV && !S390 && !SUPERH && !AVR32
8220 ---help---
8221 If you say Y here and create a character special file /dev/rtc with
8222 major number 10 and minor number 135 using mknod ("man mknod"), you
8223 diff --exclude=.git -urN linux-2.6.25.6/drivers/char/keyboard.c avr32-2.6/drivers/char/keyboard.c
8224 --- linux-2.6.25.6/drivers/char/keyboard.c 2008-06-09 20:27:19.000000000 +0200
8225 +++ avr32-2.6/drivers/char/keyboard.c 2008-06-12 15:09:40.243816452 +0200
8226 @@ -1033,7 +1033,8 @@
8227 #if defined(CONFIG_X86) || defined(CONFIG_IA64) || defined(CONFIG_ALPHA) ||\
8228 defined(CONFIG_MIPS) || defined(CONFIG_PPC) || defined(CONFIG_SPARC) ||\
8229 defined(CONFIG_PARISC) || defined(CONFIG_SUPERH) ||\
8230 - (defined(CONFIG_ARM) && defined(CONFIG_KEYBOARD_ATKBD) && !defined(CONFIG_ARCH_RPC))
8231 + (defined(CONFIG_ARM) && defined(CONFIG_KEYBOARD_ATKBD) && !defined(CONFIG_ARCH_RPC)) ||\
8232 + defined(CONFIG_AVR32)
8233
8234 #define HW_RAW(dev) (test_bit(EV_MSC, dev->evbit) && test_bit(MSC_RAW, dev->mscbit) &&\
8235 ((dev)->id.bustype == BUS_I8042) && ((dev)->id.vendor == 0x0001) && ((dev)->id.product == 0x0001))
8236 diff --exclude=.git -urN linux-2.6.25.6/drivers/clocksource/Makefile avr32-2.6/drivers/clocksource/Makefile
8237 --- linux-2.6.25.6/drivers/clocksource/Makefile 2008-06-09 20:27:19.000000000 +0200
8238 +++ avr32-2.6/drivers/clocksource/Makefile 2008-06-12 15:03:58.475816394 +0200
8239 @@ -1,3 +1,4 @@
8240 +obj-$(CONFIG_ATMEL_TCB_CLKSRC) += tcb_clksrc.o
8241 obj-$(CONFIG_X86_CYCLONE_TIMER) += cyclone.o
8242 obj-$(CONFIG_X86_PM_TIMER) += acpi_pm.o
8243 obj-$(CONFIG_SCx200HR_TIMER) += scx200_hrt.o
8244 diff --exclude=.git -urN linux-2.6.25.6/drivers/clocksource/tcb_clksrc.c avr32-2.6/drivers/clocksource/tcb_clksrc.c
8245 --- linux-2.6.25.6/drivers/clocksource/tcb_clksrc.c 1970-01-01 01:00:00.000000000 +0100
8246 +++ avr32-2.6/drivers/clocksource/tcb_clksrc.c 2008-06-12 15:03:58.479814669 +0200
8247 @@ -0,0 +1,302 @@
8248 +#include <linux/init.h>
8249 +#include <linux/clocksource.h>
8250 +#include <linux/clockchips.h>
8251 +#include <linux/interrupt.h>
8252 +#include <linux/irq.h>
8253 +
8254 +#include <linux/clk.h>
8255 +#include <linux/err.h>
8256 +#include <linux/ioport.h>
8257 +#include <linux/io.h>
8258 +#include <linux/platform_device.h>
8259 +#include <linux/atmel_tc.h>
8260 +
8261 +
8262 +/*
8263 + * We're configured to use a specific TC block, one that's not hooked
8264 + * up to external hardware, to provide a time solution:
8265 + *
8266 + * - Two channels combine to create a free-running 32 bit counter
8267 + * with a base rate of 5+ MHz, packaged as a clocksource (with
8268 + * resolution better than 200 nsec).
8269 + *
8270 + * - The third channel may be used to provide a 16-bit clockevent
8271 + * source, used in either periodic or oneshot mode. This runs
8272 + * at 32 KiHZ, and can handle delays of up to two seconds.
8273 + *
8274 + * A boot clocksource and clockevent source are also currently needed,
8275 + * unless the relevant platforms (ARM/AT91, AVR32/AT32) are changed so
8276 + * this code can be used when init_timers() is called, well before most
8277 + * devices are set up. (Some low end AT91 parts, which can run uClinux,
8278 + * have only the timers in one TC block... they currently don't support
8279 + * the tclib code, because of that initialization issue.)
8280 + *
8281 + * REVISIT behavior during system suspend states... we should disable
8282 + * all clocks and save the power. Easily done for clockevent devices,
8283 + * but clocksources won't necessarily get the needed notifications.
8284 + * For deeper system sleep states, this will be mandatory...
8285 + */
8286 +
8287 +static void __iomem *tcaddr;
8288 +
8289 +static cycle_t tc_get_cycles(void)
8290 +{
8291 + unsigned long flags;
8292 + u32 lower, upper;
8293 +
8294 + raw_local_irq_save(flags);
8295 + do {
8296 + upper = __raw_readl(tcaddr + ATMEL_TC_REG(1, CV));
8297 + lower = __raw_readl(tcaddr + ATMEL_TC_REG(0, CV));
8298 + } while (upper != __raw_readl(tcaddr + ATMEL_TC_REG(1, CV)));
8299 +
8300 + raw_local_irq_restore(flags);
8301 + return (upper << 16) | lower;
8302 +}
8303 +
8304 +static struct clocksource clksrc = {
8305 + .name = "tcb_clksrc",
8306 + .rating = 200,
8307 + .read = tc_get_cycles,
8308 + .mask = CLOCKSOURCE_MASK(32),
8309 + .shift = 18,
8310 + .flags = CLOCK_SOURCE_IS_CONTINUOUS,
8311 +};
8312 +
8313 +#ifdef CONFIG_GENERIC_CLOCKEVENTS
8314 +
8315 +struct tc_clkevt_device {
8316 + struct clock_event_device clkevt;
8317 + struct clk *clk;
8318 + void __iomem *regs;
8319 +};
8320 +
8321 +static struct tc_clkevt_device *to_tc_clkevt(struct clock_event_device *clkevt)
8322 +{
8323 + return container_of(clkevt, struct tc_clkevt_device, clkevt);
8324 +}
8325 +
8326 +/* For now, we always use the 32K clock ... this optimizes for NO_HZ,
8327 + * because using one of the divided clocks would usually mean the
8328 + * tick rate can never be less than several dozen Hz (vs 0.5 Hz).
8329 + *
8330 + * A divided clock could be good for high resolution timers, since
8331 + * 30.5 usec resolution can seem "low".
8332 + */
8333 +static u32 timer_clock;
8334 +
8335 +static void tc_mode(enum clock_event_mode m, struct clock_event_device *d)
8336 +{
8337 + struct tc_clkevt_device *tcd = to_tc_clkevt(d);
8338 + void __iomem *regs = tcd->regs;
8339 +
8340 + if (tcd->clkevt.mode == CLOCK_EVT_MODE_PERIODIC
8341 + || tcd->clkevt.mode == CLOCK_EVT_MODE_ONESHOT) {
8342 + __raw_writel(0xff, regs + ATMEL_TC_REG(2, IDR));
8343 + __raw_writel(ATMEL_TC_CLKDIS, regs + ATMEL_TC_REG(2, CCR));
8344 + clk_disable(tcd->clk);
8345 + }
8346 +
8347 + switch (m) {
8348 +
8349 + /* By not making the gentime core emulate periodic mode on top
8350 + * of oneshot, we get lower overhead and improved accuracy.
8351 + */
8352 + case CLOCK_EVT_MODE_PERIODIC:
8353 + clk_enable(tcd->clk);
8354 +
8355 + /* slow clock, count up to RC, then irq and restart */
8356 + __raw_writel(timer_clock
8357 + | ATMEL_TC_WAVE | ATMEL_TC_WAVESEL_UP_AUTO,
8358 + regs + ATMEL_TC_REG(2, CMR));
8359 + __raw_writel((32768 + HZ/2) / HZ, tcaddr + ATMEL_TC_REG(2, RC));
8360 +
8361 + /* Enable clock and interrupts on RC compare */
8362 + __raw_writel(ATMEL_TC_CPCS, regs + ATMEL_TC_REG(2, IER));
8363 +
8364 + /* go go gadget! */
8365 + __raw_writel(ATMEL_TC_CLKEN | ATMEL_TC_SWTRG,
8366 + regs + ATMEL_TC_REG(2, CCR));
8367 + break;
8368 +
8369 + case CLOCK_EVT_MODE_ONESHOT:
8370 + clk_enable(tcd->clk);
8371 +
8372 + /* slow clock, count up to RC, then irq and stop */
8373 + __raw_writel(timer_clock | ATMEL_TC_CPCSTOP
8374 + | ATMEL_TC_WAVE | ATMEL_TC_WAVESEL_UP_AUTO,
8375 + regs + ATMEL_TC_REG(2, CMR));
8376 + __raw_writel(ATMEL_TC_CPCS, regs + ATMEL_TC_REG(2, IER));
8377 +
8378 + /* set_next_event() configures and starts the timer */
8379 + break;
8380 +
8381 + default:
8382 + break;
8383 + }
8384 +}
8385 +
8386 +static int tc_next_event(unsigned long delta, struct clock_event_device *d)
8387 +{
8388 + __raw_writel(delta, tcaddr + ATMEL_TC_REG(2, RC));
8389 +
8390 + /* go go gadget! */
8391 + __raw_writel(ATMEL_TC_CLKEN | ATMEL_TC_SWTRG,
8392 + tcaddr + ATMEL_TC_REG(2, CCR));
8393 + return 0;
8394 +}
8395 +
8396 +static struct tc_clkevt_device clkevt = {
8397 + .clkevt = {
8398 + .name = "tc_clkevt",
8399 + .features = CLOCK_EVT_FEAT_PERIODIC
8400 + | CLOCK_EVT_FEAT_ONESHOT,
8401 + .shift = 32,
8402 + /* Should be lower than at91rm9200's system timer */
8403 + .rating = 125,
8404 + .cpumask = CPU_MASK_CPU0,
8405 + .set_next_event = tc_next_event,
8406 + .set_mode = tc_mode,
8407 + },
8408 +};
8409 +
8410 +static irqreturn_t ch2_irq(int irq, void *handle)
8411 +{
8412 + struct tc_clkevt_device *dev = handle;
8413 + unsigned int sr;
8414 +
8415 + sr = __raw_readl(dev->regs + ATMEL_TC_REG(2, SR));
8416 + if (sr & ATMEL_TC_CPCS) {
8417 + dev->clkevt.event_handler(&dev->clkevt);
8418 + return IRQ_HANDLED;
8419 + }
8420 +
8421 + return IRQ_NONE;
8422 +}
8423 +
8424 +static struct irqaction tc_irqaction = {
8425 + .name = "tc_clkevt",
8426 + .flags = IRQF_TIMER | IRQF_DISABLED,
8427 + .handler = ch2_irq,
8428 +};
8429 +
8430 +static void __init setup_clkevents(struct atmel_tc *tc, int clk32k_divisor_idx)
8431 +{
8432 + struct clk *t2_clk = tc->clk[2];
8433 + int irq = tc->irq[2];
8434 +
8435 + clkevt.regs = tc->regs;
8436 + clkevt.clk = t2_clk;
8437 + tc_irqaction.dev_id = &clkevt;
8438 +
8439 + timer_clock = clk32k_divisor_idx;
8440 +
8441 + clkevt.clkevt.mult = div_sc(32768, NSEC_PER_SEC, clkevt.clkevt.shift);
8442 + clkevt.clkevt.max_delta_ns
8443 + = clockevent_delta2ns(0xffff, &clkevt.clkevt);
8444 + clkevt.clkevt.min_delta_ns = clockevent_delta2ns(1, &clkevt.clkevt) + 1;
8445 +
8446 + setup_irq(irq, &tc_irqaction);
8447 +
8448 + clockevents_register_device(&clkevt.clkevt);
8449 +}
8450 +
8451 +#else /* !CONFIG_GENERIC_CLOCKEVENTS */
8452 +
8453 +static void __init setup_clkevents(struct atmel_tc *tc, int clk32k_divisor_idx)
8454 +{
8455 + /* NOTHING */
8456 +}
8457 +
8458 +#endif
8459 +
8460 +static int __init tcb_clksrc_init(void)
8461 +{
8462 + static char bootinfo[] __initdata
8463 + = KERN_DEBUG "%s: tc%d at %d.%03d MHz\n";
8464 +
8465 + struct platform_device *pdev;
8466 + struct atmel_tc *tc;
8467 + struct clk *t0_clk;
8468 + u32 rate, divided_rate = 0;
8469 + int best_divisor_idx = -1;
8470 + int clk32k_divisor_idx = -1;
8471 + int i;
8472 +
8473 + tc = atmel_tc_alloc(CONFIG_ATMEL_TCB_CLKSRC_BLOCK, clksrc.name);
8474 + if (!tc) {
8475 + pr_debug("can't alloc TC for clocksource\n");
8476 + return -ENODEV;
8477 + }
8478 + tcaddr = tc->regs;
8479 + pdev = tc->pdev;
8480 +
8481 + t0_clk = tc->clk[0];
8482 + clk_enable(t0_clk);
8483 +
8484 + /* How fast will we be counting? Pick something over 5 MHz. */
8485 + rate = (u32) clk_get_rate(t0_clk);
8486 + for (i = 0; i < 5; i++) {
8487 + unsigned divisor = atmel_tc_divisors[i];
8488 + unsigned tmp;
8489 +
8490 + /* remember 32 KiHz clock for later */
8491 + if (!divisor) {
8492 + clk32k_divisor_idx = i;
8493 + continue;
8494 + }
8495 +
8496 + tmp = rate / divisor;
8497 + pr_debug("TC: %u / %-3u [%d] --> %u\n", rate, divisor, i, tmp);
8498 + if (best_divisor_idx > 0) {
8499 + if (tmp < 5 * 1000 * 1000)
8500 + continue;
8501 + }
8502 + divided_rate = tmp;
8503 + best_divisor_idx = i;
8504 + }
8505 +
8506 + clksrc.mult = clocksource_hz2mult(divided_rate, clksrc.shift);
8507 +
8508 + printk(bootinfo, clksrc.name, CONFIG_ATMEL_TCB_CLKSRC_BLOCK,
8509 + divided_rate / 1000000,
8510 + ((divided_rate + 500000) % 1000000) / 1000);
8511 +
8512 + /* tclib will give us three clocks no matter what the
8513 + * underlying platform supports.
8514 + */
8515 + clk_enable(tc->clk[1]);
8516 +
8517 + /* channel 0: waveform mode, input mclk/8, clock TIOA0 on overflow */
8518 + __raw_writel(best_divisor_idx /* likely divide-by-8 */
8519 + | ATMEL_TC_WAVE
8520 + | ATMEL_TC_WAVESEL_UP /* free-run */
8521 + | ATMEL_TC_ACPA_SET /* TIOA0 rises at 0 */
8522 + | ATMEL_TC_ACPC_CLEAR, /* (duty cycle 50%) */
8523 + tcaddr + ATMEL_TC_REG(0, CMR));
8524 + __raw_writel(0x0000, tcaddr + ATMEL_TC_REG(0, RA));
8525 + __raw_writel(0x8000, tcaddr + ATMEL_TC_REG(0, RC));
8526 + __raw_writel(0xff, tcaddr + ATMEL_TC_REG(0, IDR)); /* no irqs */
8527 + __raw_writel(ATMEL_TC_CLKEN, tcaddr + ATMEL_TC_REG(0, CCR));
8528 +
8529 + /* channel 1: waveform mode, input TIOA0 */
8530 + __raw_writel(ATMEL_TC_XC1 /* input: TIOA0 */
8531 + | ATMEL_TC_WAVE
8532 + | ATMEL_TC_WAVESEL_UP, /* free-run */
8533 + tcaddr + ATMEL_TC_REG(1, CMR));
8534 + __raw_writel(0xff, tcaddr + ATMEL_TC_REG(1, IDR)); /* no irqs */
8535 + __raw_writel(ATMEL_TC_CLKEN, tcaddr + ATMEL_TC_REG(1, CCR));
8536 +
8537 + /* chain channel 0 to channel 1, then reset all the timers */
8538 + __raw_writel(ATMEL_TC_TC1XC1S_TIOA0, tcaddr + ATMEL_TC_BMR);
8539 + __raw_writel(ATMEL_TC_SYNC, tcaddr + ATMEL_TC_BCR);
8540 +
8541 + /* and away we go! */
8542 + clocksource_register(&clksrc);
8543 +
8544 + /* channel 2: periodic and oneshot timer support */
8545 + setup_clkevents(tc, clk32k_divisor_idx);
8546 +
8547 + return 0;
8548 +}
8549 +arch_initcall(tcb_clksrc_init);
8550 diff --exclude=.git -urN linux-2.6.25.6/drivers/i2c/busses/i2c-atmeltwi.c avr32-2.6/drivers/i2c/busses/i2c-atmeltwi.c
8551 --- linux-2.6.25.6/drivers/i2c/busses/i2c-atmeltwi.c 1970-01-01 01:00:00.000000000 +0100
8552 +++ avr32-2.6/drivers/i2c/busses/i2c-atmeltwi.c 2008-06-12 15:09:40.391816588 +0200
8553 @@ -0,0 +1,436 @@
8554 +/*
8555 + * i2c Support for Atmel's Two-Wire Interface (TWI)
8556 + *
8557 + * Based on the work of Copyright (C) 2004 Rick Bronson
8558 + * Converted to 2.6 by Andrew Victor <andrew at sanpeople.com>
8559 + * Ported to AVR32 and heavily modified by Espen Krangnes
8560 + * <ekrangnes at atmel.com>
8561 + *
8562 + * Copyright (C) 2006 Atmel Corporation
8563 + *
8564 + * Borrowed heavily from the original work by:
8565 + * Copyright (C) 2000 Philip Edelbrock <phil at stimpy.netroedge.com>
8566 + *
8567 + * Partialy rewriten by Karel Hojdar <cmkaho at seznam.cz>
8568 + * bugs removed, interrupt routine markedly rewritten
8569 + *
8570 + * This program is free software; you can redistribute it and/or modify
8571 + * it under the terms of the GNU General Public License as published by
8572 + * the Free Software Foundation; either version 2 of the License, or
8573 + * (at your option) any later version.
8574 + */
8575 +#undef VERBOSE_DEBUG
8576 +
8577 +#include <linux/module.h>
8578 +#include <linux/slab.h>
8579 +#include <linux/i2c.h>
8580 +#include <linux/init.h>
8581 +#include <linux/clk.h>
8582 +#include <linux/err.h>
8583 +#include <linux/interrupt.h>
8584 +#include <linux/platform_device.h>
8585 +#include <linux/completion.h>
8586 +#include <linux/io.h>
8587 +
8588 +#include "i2c-atmeltwi.h"
8589 +
8590 +static unsigned int baudrate = 100 * 1000;
8591 +module_param(baudrate, uint, S_IRUGO);
8592 +MODULE_PARM_DESC(baudrate, "The TWI baudrate");
8593 +
8594 +
8595 +struct atmel_twi {
8596 + void __iomem *regs;
8597 + struct i2c_adapter adapter;
8598 + struct clk *pclk;
8599 + struct completion comp;
8600 + u32 mask;
8601 + u8 *buf;
8602 + u16 len;
8603 + u16 acks_left;
8604 + int status;
8605 + unsigned int irq;
8606 +
8607 +};
8608 +#define to_atmel_twi(adap) container_of(adap, struct atmel_twi, adapter)
8609 +
8610 +/*
8611 + * (Re)Initialize the TWI hardware registers.
8612 + */
8613 +static int twi_hwinit(struct atmel_twi *twi)
8614 +{
8615 + unsigned long cdiv, ckdiv = 0;
8616 +
8617 + /* REVISIT: wait till SCL is high before resetting; otherwise,
8618 + * some versions will wedge forever.
8619 + */
8620 +
8621 + twi_writel(twi, IDR, ~0UL);
8622 + twi_writel(twi, CR, TWI_BIT(SWRST)); /*Reset peripheral*/
8623 + twi_readl(twi, SR);
8624 +
8625 + cdiv = (clk_get_rate(twi->pclk) / (2 * baudrate)) - 4;
8626 +
8627 + while (cdiv > 255) {
8628 + ckdiv++;
8629 + cdiv = cdiv >> 1;
8630 + }
8631 +
8632 + /* REVISIT: there are various errata to consider re CDIV and CHDIV
8633 + * here, at least on at91 parts.
8634 + */
8635 +
8636 + if (ckdiv > 7)
8637 + return -EINVAL;
8638 + else
8639 + twi_writel(twi, CWGR, TWI_BF(CKDIV, ckdiv)
8640 + | TWI_BF(CHDIV, cdiv)
8641 + | TWI_BF(CLDIV, cdiv));
8642 + return 0;
8643 +}
8644 +
8645 +/*
8646 + * Waits for the i2c status register to set the specified bitmask
8647 + * Returns 0 if timed out ... ~100ms is much longer than the SMBus
8648 + * limit, but I2C has no limit at all.
8649 + */
8650 +static int twi_complete(struct atmel_twi *twi, u32 mask)
8651 +{
8652 + int timeout = msecs_to_jiffies(100);
8653 +
8654 + mask |= TWI_BIT(TXCOMP);
8655 + twi->mask = mask | TWI_BIT(NACK) | TWI_BIT(OVRE);
8656 + init_completion(&twi->comp);
8657 +
8658 + twi_writel(twi, IER, mask);
8659 +
8660 + if (!wait_for_completion_timeout(&twi->comp, timeout)) {
8661 + /* RESET TWI interface */
8662 + twi_writel(twi, CR, TWI_BIT(SWRST));
8663 +
8664 + /* Reinitialize TWI */
8665 + twi_hwinit(twi);
8666 +
8667 + return -ETIMEDOUT;
8668 + }
8669 + return 0;
8670 +}
8671 +
8672 +/*
8673 + * Generic i2c master transfer entrypoint.
8674 + */
8675 +static int twi_xfer(struct i2c_adapter *adap, struct i2c_msg *pmsg, int num)
8676 +{
8677 + struct atmel_twi *twi = to_atmel_twi(adap);
8678 + int i;
8679 +
8680 + dev_dbg(&adap->dev, "twi_xfer: processing %d messages:\n", num);
8681 +
8682 + twi->status = 0;
8683 + for (i = 0; i < num; i++, pmsg++) {
8684 + twi->len = pmsg->len;
8685 + twi->buf = pmsg->buf;
8686 + twi->acks_left = pmsg->len;
8687 + twi_writel(twi, MMR, TWI_BF(DADR, pmsg->addr) |
8688 + (pmsg->flags & I2C_M_RD ? TWI_BIT(MREAD) : 0));
8689 + twi_writel(twi, IADR, TWI_BF(IADR, pmsg->addr));
8690 +
8691 + dev_dbg(&adap->dev,
8692 + "#%d: %s %d byte%s %s dev 0x%02x\n",
8693 + i,
8694 + pmsg->flags & I2C_M_RD ? "reading" : "writing",
8695 + pmsg->len,
8696 + pmsg->len > 1 ? "s" : "",
8697 + pmsg->flags & I2C_M_RD ? "from" : "to", pmsg->addr);
8698 +
8699 + /* enable */
8700 + twi_writel(twi, CR, TWI_BIT(MSEN));
8701 +
8702 + if (pmsg->flags & I2C_M_RD) {
8703 + /* cleanup after previous RX overruns */
8704 + while (twi_readl(twi, SR) & TWI_BIT(RXRDY))
8705 + twi_readl(twi, RHR);
8706 +
8707 + if (twi->len == 1)
8708 + twi_writel(twi, CR,
8709 + TWI_BIT(START) | TWI_BIT(STOP));
8710 + else
8711 + twi_writel(twi, CR, TWI_BIT(START));
8712 +
8713 + if (twi_complete(twi, TWI_BIT(RXRDY)) == -ETIMEDOUT) {
8714 + dev_dbg(&adap->dev, "RX[%d] timeout. "
8715 + "Stopped with %d bytes left\n",
8716 + i, twi->acks_left);
8717 + return -ETIMEDOUT;
8718 + }
8719 + } else {
8720 + twi_writel(twi, THR, twi->buf[0]);
8721 + twi->acks_left--;
8722 + /* REVISIT: some chips don't start automagically:
8723 + * twi_writel(twi, CR, TWI_BIT(START));
8724 + */
8725 + if (twi_complete(twi, TWI_BIT(TXRDY)) == -ETIMEDOUT) {
8726 + dev_dbg(&adap->dev, "TX[%d] timeout. "
8727 + "Stopped with %d bytes left\n",
8728 + i, twi->acks_left);
8729 + return -ETIMEDOUT;
8730 + }
8731 + /* REVISIT: an erratum workaround may be needed here;
8732 + * see sam9261 "STOP not generated" (START either).
8733 + */
8734 + }
8735 +
8736 + /* Disable TWI interface */
8737 + twi_writel(twi, CR, TWI_BIT(MSDIS));
8738 +
8739 + if (twi->status)
8740 + return twi->status;
8741 +
8742 + /* WARNING: This driver lies about properly supporting
8743 + * repeated start, or it would *ALWAYS* return here. It
8744 + * has issued a STOP. Continuing is a false claim -- that
8745 + * a second (or third, etc.) message is part of the same
8746 + * "combined" (no STOPs between parts) message.
8747 + */
8748 +
8749 + } /* end cur msg */
8750 +
8751 + return i;
8752 +}
8753 +
8754 +
8755 +static irqreturn_t twi_interrupt(int irq, void *dev_id)
8756 +{
8757 + struct atmel_twi *twi = dev_id;
8758 + int status = twi_readl(twi, SR);
8759 +
8760 + /* Save state for later debug prints */
8761 + int old_status = status;
8762 +
8763 + if (twi->mask & status) {
8764 +
8765 + status &= twi->mask;
8766 +
8767 + if (status & TWI_BIT(RXRDY)) {
8768 + if ((status & TWI_BIT(OVRE)) && twi->acks_left) {
8769 + /* Note weakness in fault reporting model:
8770 + * we can't say "the first N of these data
8771 + * bytes are valid".
8772 + */
8773 + dev_err(&twi->adapter.dev,
8774 + "OVERRUN RX! %04x, lost %d\n",
8775 + old_status, twi->acks_left);
8776 + twi->acks_left = 0;
8777 + twi_writel(twi, CR, TWI_BIT(STOP));
8778 + twi->status = -EOVERFLOW;
8779 + } else if (twi->acks_left > 0) {
8780 + twi->buf[twi->len - twi->acks_left] =
8781 + twi_readl(twi, RHR);
8782 + twi->acks_left--;
8783 + }
8784 + if (status & TWI_BIT(TXCOMP))
8785 + goto done;
8786 + if (twi->acks_left == 1)
8787 + twi_writel(twi, CR, TWI_BIT(STOP));
8788 +
8789 + } else if (status & (TWI_BIT(NACK) | TWI_BIT(TXCOMP))) {
8790 + goto done;
8791 +
8792 + } else if (status & TWI_BIT(TXRDY)) {
8793 + if (twi->acks_left > 0) {
8794 + twi_writel(twi, THR,
8795 + twi->buf[twi->len - twi->acks_left]);
8796 + twi->acks_left--;
8797 + } else
8798 + twi_writel(twi, CR, TWI_BIT(STOP));
8799 + }
8800 +
8801 + if (twi->acks_left == 0)
8802 + twi_writel(twi, IDR, ~TWI_BIT(TXCOMP));
8803 + }
8804 +
8805 + /* enabling this message helps trigger overruns/underruns ... */
8806 + dev_vdbg(&twi->adapter.dev,
8807 + "ISR: SR 0x%04X, mask 0x%04X, acks %i\n",
8808 + old_status,
8809 + twi->acks_left ? twi->mask : TWI_BIT(TXCOMP),
8810 + twi->acks_left);
8811 +
8812 + return IRQ_HANDLED;
8813 +
8814 +done:
8815 + /* Note weak fault reporting model: we can't report how many
8816 + * bytes we sent before the NAK, or let upper layers choose
8817 + * whether to continue. The I2C stack doesn't allow that...
8818 + */
8819 + if (status & TWI_BIT(NACK)) {
8820 + dev_dbg(&twi->adapter.dev, "NACK received! %d to go\n",
8821 + twi->acks_left);
8822 + twi->status = -EPIPE;
8823 +
8824 + /* TX underrun morphs automagically into a premature STOP;
8825 + * we'll probably observe UVRE even when it's not documented.
8826 + */
8827 + } else if (twi->acks_left && (twi->mask & TWI_BIT(TXRDY))) {
8828 + dev_err(&twi->adapter.dev, "UNDERRUN TX! %04x, %d to go\n",
8829 + old_status, twi->acks_left);
8830 + twi->status = -ENOSR;
8831 + }
8832 +
8833 + twi_writel(twi, IDR, ~0UL);
8834 + complete(&twi->comp);
8835 +
8836 + dev_dbg(&twi->adapter.dev, "ISR: SR 0x%04X, acks %i --> %d\n",
8837 + old_status, twi->acks_left, twi->status);
8838 +
8839 + return IRQ_HANDLED;
8840 +}
8841 +
8842 +
8843 +/*
8844 + * Return list of supported functionality.
8845 + *
8846 + * NOTE: see warning above about repeated starts; this driver is falsely
8847 + * claiming to support "combined" transfers. The mid-message STOPs mean
8848 + * some slaves will never work with this driver. (Use i2c-gpio...)
8849 + */
8850 +static u32 twi_func(struct i2c_adapter *adapter)
8851 +{
8852 + return (I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL)
8853 + & ~I2C_FUNC_SMBUS_QUICK;
8854 +}
8855 +
8856 +static struct i2c_algorithm twi_algorithm = {
8857 + .master_xfer = twi_xfer,
8858 + .functionality = twi_func,
8859 +};
8860 +
8861 +/*
8862 + * Main initialization routine.
8863 + */
8864 +static int __init twi_probe(struct platform_device *pdev)
8865 +{
8866 + struct atmel_twi *twi;
8867 + struct resource *regs;
8868 + struct clk *pclk;
8869 + struct i2c_adapter *adapter;
8870 + int rc, irq;
8871 +
8872 + regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
8873 + if (!regs)
8874 + return -ENXIO;
8875 +
8876 + pclk = clk_get(&pdev->dev, "twi_pclk");
8877 + if (IS_ERR(pclk))
8878 + return PTR_ERR(pclk);
8879 + clk_enable(pclk);
8880 +
8881 + rc = -ENOMEM;
8882 + twi = kzalloc(sizeof(struct atmel_twi), GFP_KERNEL);
8883 + if (!twi) {
8884 + dev_dbg(&pdev->dev, "can't allocate interface!\n");
8885 + goto err_alloc_twi;
8886 + }
8887 +
8888 + twi->pclk = pclk;
8889 + twi->regs = ioremap(regs->start, regs->end - regs->start + 1);
8890 + if (!twi->regs)
8891 + goto err_ioremap;
8892 +
8893 + irq = platform_get_irq(pdev, 0);
8894 + rc = request_irq(irq, twi_interrupt, 0, "twi", twi);
8895 + if (rc) {
8896 + dev_dbg(&pdev->dev, "can't bind irq!\n");
8897 + goto err_irq;
8898 + }
8899 + twi->irq = irq;
8900 +
8901 + rc = twi_hwinit(twi);
8902 + if (rc) {
8903 + dev_err(&pdev->dev, "Unable to set baudrate\n");
8904 + goto err_hw_init;
8905 + }
8906 +
8907 + adapter = &twi->adapter;
8908 + sprintf(adapter->name, "TWI");
8909 + adapter->algo = &twi_algorithm;
8910 + adapter->class = I2C_CLASS_ALL;
8911 + adapter->nr = pdev->id;
8912 + adapter->dev.parent = &pdev->dev;
8913 +
8914 + platform_set_drvdata(pdev, twi);
8915 +
8916 + rc = i2c_add_numbered_adapter(adapter);
8917 + if (rc) {
8918 + dev_dbg(&pdev->dev, "Adapter %s registration failed\n",
8919 + adapter->name);
8920 + goto err_register;
8921 + }
8922 +
8923 + dev_info(&pdev->dev,
8924 + "Atmel TWI/I2C adapter (baudrate %dk) at 0x%08lx.\n",
8925 + baudrate/1000, (unsigned long)regs->start);
8926 +
8927 + return 0;
8928 +
8929 +
8930 +err_register:
8931 + platform_set_drvdata(pdev, NULL);
8932 +
8933 +err_hw_init:
8934 + free_irq(irq, twi);
8935 +
8936 +err_irq:
8937 + iounmap(twi->regs);
8938 +
8939 +err_ioremap:
8940 + kfree(twi);
8941 +
8942 +err_alloc_twi:
8943 + clk_disable(pclk);
8944 + clk_put(pclk);
8945 +
8946 + return rc;
8947 +}
8948 +
8949 +static int __exit twi_remove(struct platform_device *pdev)
8950 +{
8951 + struct atmel_twi *twi = platform_get_drvdata(pdev);
8952 + int res;
8953 +
8954 + platform_set_drvdata(pdev, NULL);
8955 + res = i2c_del_adapter(&twi->adapter);
8956 + twi_writel(twi, CR, TWI_BIT(MSDIS));
8957 + iounmap(twi->regs);
8958 + clk_disable(twi->pclk);
8959 + clk_put(twi->pclk);
8960 + free_irq(twi->irq, twi);
8961 + kfree(twi);
8962 +
8963 + return res;
8964 +}
8965 +
8966 +static struct platform_driver twi_driver = {
8967 + .remove = __exit_p(twi_remove),
8968 + .driver = {
8969 + .name = "atmel_twi",
8970 + .owner = THIS_MODULE,
8971 + },
8972 +};
8973 +
8974 +static int __init atmel_twi_init(void)
8975 +{
8976 + return platform_driver_probe(&twi_driver, twi_probe);
8977 +}
8978 +
8979 +static void __exit atmel_twi_exit(void)
8980 +{
8981 + platform_driver_unregister(&twi_driver);
8982 +}
8983 +
8984 +module_init(atmel_twi_init);
8985 +module_exit(atmel_twi_exit);
8986 +
8987 +MODULE_AUTHOR("Espen Krangnes");
8988 +MODULE_DESCRIPTION("I2C driver for Atmel TWI");
8989 +MODULE_LICENSE("GPL");
8990 diff --exclude=.git -urN linux-2.6.25.6/drivers/i2c/busses/i2c-atmeltwi.h avr32-2.6/drivers/i2c/busses/i2c-atmeltwi.h
8991 --- linux-2.6.25.6/drivers/i2c/busses/i2c-atmeltwi.h 1970-01-01 01:00:00.000000000 +0100
8992 +++ avr32-2.6/drivers/i2c/busses/i2c-atmeltwi.h 2008-06-12 15:09:40.391816588 +0200
8993 @@ -0,0 +1,117 @@
8994 +/*
8995 + * Register definitions for the Atmel Two-Wire Interface
8996 + */
8997 +
8998 +#ifndef __ATMELTWI_H__
8999 +#define __ATMELTWI_H__
9000 +
9001 +/* TWI register offsets */
9002 +#define TWI_CR 0x0000
9003 +#define TWI_MMR 0x0004
9004 +#define TWI_SMR 0x0008
9005 +#define TWI_IADR 0x000c
9006 +#define TWI_CWGR 0x0010
9007 +#define TWI_SR 0x0020
9008 +#define TWI_IER 0x0024
9009 +#define TWI_IDR 0x0028
9010 +#define TWI_IMR 0x002c
9011 +#define TWI_RHR 0x0030
9012 +#define TWI_THR 0x0034
9013 +
9014 +/* Bitfields in CR */
9015 +#define TWI_START_OFFSET 0
9016 +#define TWI_START_SIZE 1
9017 +#define TWI_STOP_OFFSET 1
9018 +#define TWI_STOP_SIZE 1
9019 +#define TWI_MSEN_OFFSET 2
9020 +#define TWI_MSEN_SIZE 1
9021 +#define TWI_MSDIS_OFFSET 3
9022 +#define TWI_MSDIS_SIZE 1
9023 +#define TWI_SVEN_OFFSET 4
9024 +#define TWI_SVEN_SIZE 1
9025 +#define TWI_SVDIS_OFFSET 5
9026 +#define TWI_SVDIS_SIZE 1
9027 +#define TWI_SWRST_OFFSET 7
9028 +#define TWI_SWRST_SIZE 1
9029 +
9030 +/* Bitfields in MMR */
9031 +#define TWI_IADRSZ_OFFSET 8
9032 +#define TWI_IADRSZ_SIZE 2
9033 +#define TWI_MREAD_OFFSET 12
9034 +#define TWI_MREAD_SIZE 1
9035 +#define TWI_DADR_OFFSET 16
9036 +#define TWI_DADR_SIZE 7
9037 +
9038 +/* Bitfields in SMR */
9039 +#define TWI_SADR_OFFSET 16
9040 +#define TWI_SADR_SIZE 7
9041 +
9042 +/* Bitfields in IADR */
9043 +#define TWI_IADR_OFFSET 0
9044 +#define TWI_IADR_SIZE 24
9045 +
9046 +/* Bitfields in CWGR */
9047 +#define TWI_CLDIV_OFFSET 0
9048 +#define TWI_CLDIV_SIZE 8
9049 +#define TWI_CHDIV_OFFSET 8
9050 +#define TWI_CHDIV_SIZE 8
9051 +#define TWI_CKDIV_OFFSET 16
9052 +#define TWI_CKDIV_SIZE 3
9053 +
9054 +/* Bitfields in SR */
9055 +#define TWI_TXCOMP_OFFSET 0
9056 +#define TWI_TXCOMP_SIZE 1
9057 +#define TWI_RXRDY_OFFSET 1
9058 +#define TWI_RXRDY_SIZE 1
9059 +#define TWI_TXRDY_OFFSET 2
9060 +#define TWI_TXRDY_SIZE 1
9061 +#define TWI_SVDIR_OFFSET 3
9062 +#define TWI_SVDIR_SIZE 1
9063 +#define TWI_SVACC_OFFSET 4
9064 +#define TWI_SVACC_SIZE 1
9065 +#define TWI_GCACC_OFFSET 5
9066 +#define TWI_GCACC_SIZE 1
9067 +#define TWI_OVRE_OFFSET 6
9068 +#define TWI_OVRE_SIZE 1
9069 +#define TWI_UNRE_OFFSET 7
9070 +#define TWI_UNRE_SIZE 1
9071 +#define TWI_NACK_OFFSET 8
9072 +#define TWI_NACK_SIZE 1
9073 +#define TWI_ARBLST_OFFSET 9
9074 +#define TWI_ARBLST_SIZE 1
9075 +
9076 +/* Bitfields in RHR */
9077 +#define TWI_RXDATA_OFFSET 0
9078 +#define TWI_RXDATA_SIZE 8
9079 +
9080 +/* Bitfields in THR */
9081 +#define TWI_TXDATA_OFFSET 0
9082 +#define TWI_TXDATA_SIZE 8
9083 +
9084 +/* Constants for IADRSZ */
9085 +#define TWI_IADRSZ_NO_ADDR 0
9086 +#define TWI_IADRSZ_ONE_BYTE 1
9087 +#define TWI_IADRSZ_TWO_BYTES 2
9088 +#define TWI_IADRSZ_THREE_BYTES 3
9089 +
9090 +/* Bit manipulation macros */
9091 +#define TWI_BIT(name) \
9092 + (1 << TWI_##name##_OFFSET)
9093 +#define TWI_BF(name, value) \
9094 + (((value) & ((1 << TWI_##name##_SIZE) - 1)) \
9095 + << TWI_##name##_OFFSET)
9096 +#define TWI_BFEXT(name, value) \
9097 + (((value) >> TWI_##name##_OFFSET) \
9098 + & ((1 << TWI_##name##_SIZE) - 1))
9099 +#define TWI_BFINS(name, value, old) \
9100 + (((old) & ~(((1 << TWI_##name##_SIZE) - 1) \
9101 + << TWI_##name##_OFFSET)) \
9102 + | TWI_BF(name, (value)))
9103 +
9104 +/* Register access macros */
9105 +#define twi_readl(port, reg) \
9106 + __raw_readl((port)->regs + TWI_##reg)
9107 +#define twi_writel(port, reg, value) \
9108 + __raw_writel((value), (port)->regs + TWI_##reg)
9109 +
9110 +#endif /* __ATMELTWI_H__ */
9111 diff --exclude=.git -urN linux-2.6.25.6/drivers/i2c/busses/Kconfig avr32-2.6/drivers/i2c/busses/Kconfig
9112 --- linux-2.6.25.6/drivers/i2c/busses/Kconfig 2008-06-09 20:27:19.000000000 +0200
9113 +++ avr32-2.6/drivers/i2c/busses/Kconfig 2008-06-12 15:09:40.383816128 +0200
9114 @@ -88,6 +88,14 @@
9115 to support combined I2C messages. Use the i2c-gpio driver
9116 unless your system can cope with those limitations.
9117
9118 +config I2C_ATMELTWI
9119 + tristate "Atmel Two-Wire Interface (TWI)"
9120 + depends on I2C && (ARCH_AT91 || PLATFORM_AT32AP)
9121 + help
9122 + Atmel on-chip TWI controller. Say Y if you have an AT32 or
9123 + AT91-based device and want to use its built-in TWI
9124 + functionality.
9125 +
9126 config I2C_AU1550
9127 tristate "Au1550/Au1200 SMBus interface"
9128 depends on SOC_AU1550 || SOC_AU1200
9129 diff --exclude=.git -urN linux-2.6.25.6/drivers/i2c/busses/Makefile avr32-2.6/drivers/i2c/busses/Makefile
9130 --- linux-2.6.25.6/drivers/i2c/busses/Makefile 2008-06-09 20:27:19.000000000 +0200
9131 +++ avr32-2.6/drivers/i2c/busses/Makefile 2008-06-12 15:09:40.383816128 +0200
9132 @@ -52,6 +52,7 @@
9133 obj-$(CONFIG_I2C_VOODOO3) += i2c-voodoo3.o
9134 obj-$(CONFIG_SCx200_ACB) += scx200_acb.o
9135 obj-$(CONFIG_SCx200_I2C) += scx200_i2c.o
9136 +obj-$(CONFIG_I2C_ATMELTWI) += i2c-atmeltwi.o
9137
9138 ifeq ($(CONFIG_I2C_DEBUG_BUS),y)
9139 EXTRA_CFLAGS += -DDEBUG
9140 diff --exclude=.git -urN linux-2.6.25.6/drivers/input/serio/at32psif.c avr32-2.6/drivers/input/serio/at32psif.c
9141 --- linux-2.6.25.6/drivers/input/serio/at32psif.c 1970-01-01 01:00:00.000000000 +0100
9142 +++ avr32-2.6/drivers/input/serio/at32psif.c 2008-06-12 15:09:40.619816021 +0200
9143 @@ -0,0 +1,351 @@
9144 +/*
9145 + * Copyright (C) 2007 Atmel Corporation
9146 + *
9147 + * Driver for the AT32AP700X PS/2 controller (PSIF).
9148 + *
9149 + * This program is free software; you can redistribute it and/or modify it
9150 + * under the terms of the GNU General Public License version 2 as published
9151 + * by the Free Software Foundation.
9152 + */
9153 +#include <linux/kernel.h>
9154 +#include <linux/module.h>
9155 +#include <linux/device.h>
9156 +#include <linux/init.h>
9157 +#include <linux/serio.h>
9158 +#include <linux/timer.h>
9159 +#include <linux/interrupt.h>
9160 +#include <linux/err.h>
9161 +#include <linux/io.h>
9162 +#include <linux/clk.h>
9163 +#include <linux/platform_device.h>
9164 +
9165 +#include "at32psif.h"
9166 +
9167 +#define PSIF_BUF_SIZE 16
9168 +
9169 +#define ring_is_empty(_psif) (_psif->head == _psif->tail)
9170 +#define ring_next_head(_psif) ((_psif->head + 1) & (PSIF_BUF_SIZE - 1))
9171 +#define ring_next_tail(_psif) ((_psif->tail + 1) & (PSIF_BUF_SIZE - 1))
9172 +
9173 +struct psif {
9174 + struct platform_device *pdev;
9175 + struct clk *pclk;
9176 + struct serio *io;
9177 + struct timer_list tx_timer;
9178 + void __iomem *regs;
9179 + unsigned int irq;
9180 + unsigned int open;
9181 + /* Prevent concurrent writes to circular buffer. */
9182 + spinlock_t lock;
9183 + unsigned int head;
9184 + unsigned int tail;
9185 + unsigned char buffer[PSIF_BUF_SIZE];
9186 +};
9187 +
9188 +static irqreturn_t psif_interrupt(int irq, void *_ptr)
9189 +{
9190 + struct psif *psif = _ptr;
9191 + int retval = IRQ_NONE;
9192 + unsigned int io_flags = 0;
9193 + unsigned long status;
9194 +
9195 + status = psif_readl(psif, SR);
9196 +
9197 + if (status & PSIF_BIT(RXRDY)) {
9198 + unsigned char val = (unsigned char) psif_readl(psif, RHR);
9199 +
9200 + if (status & PSIF_BIT(PARITY))
9201 + io_flags |= SERIO_PARITY;
9202 + if (status & PSIF_BIT(OVRUN))
9203 + dev_err(&psif->pdev->dev, "overrun read error\n");
9204 +
9205 + serio_interrupt(psif->io, val, io_flags);
9206 +
9207 + retval = IRQ_HANDLED;
9208 + }
9209 +
9210 + spin_lock(&psif->lock);
9211 +
9212 + if (status & PSIF_BIT(TXEMPTY)) {
9213 + if (status & PSIF_BIT(NACK))
9214 + dev_err(&psif->pdev->dev, "NACK error\n");
9215 +
9216 + psif_writel(psif, IDR, PSIF_BIT(TXEMPTY));
9217 +
9218 + if (!ring_is_empty(psif))
9219 + mod_timer(&psif->tx_timer,
9220 + jiffies + msecs_to_jiffies(1));
9221 +
9222 + retval = IRQ_HANDLED;
9223 + }
9224 +
9225 + spin_unlock(&psif->lock);
9226 +
9227 + return retval;
9228 +}
9229 +
9230 +static void psif_transmit_data(unsigned long data)
9231 +{
9232 + struct psif *psif = (struct psif *)data;
9233 + unsigned long flags;
9234 +
9235 + spin_lock_irqsave(&psif->lock, flags);
9236 +
9237 + psif_writel(psif, THR, psif->buffer[psif->tail]);
9238 + psif->tail = ring_next_tail(psif);
9239 +
9240 + if (!ring_is_empty(psif))
9241 + psif_writel(psif, IER, PSIF_BIT(TXEMPTY));
9242 +
9243 + spin_unlock_irqrestore(&psif->lock, flags);
9244 +}
9245 +
9246 +static int psif_write(struct serio *io, unsigned char val)
9247 +{
9248 + struct psif *psif = io->port_data;
9249 + unsigned long flags;
9250 + unsigned int head;
9251 +
9252 + spin_lock_irqsave(&psif->lock, flags);
9253 +
9254 + head = ring_next_head(psif);
9255 +
9256 + if (head != psif->tail) {
9257 + psif->buffer[psif->head] = val;
9258 + psif->head = head;
9259 + } else {
9260 + dev_err(&psif->pdev->dev, "underrun write error\n");
9261 + }
9262 +
9263 + spin_unlock_irqrestore(&psif->lock, flags);
9264 +
9265 + /* Make sure TXEMPTY interrupt is enabled. */
9266 + psif_writel(psif, IER, PSIF_BIT(TXEMPTY));
9267 +
9268 + return 0;
9269 +}
9270 +
9271 +static int psif_open(struct serio *io)
9272 +{
9273 + struct psif *psif = io->port_data;
9274 + int retval;
9275 +
9276 + retval = clk_enable(psif->pclk);
9277 + if (retval)
9278 + goto out;
9279 +
9280 + psif_writel(psif, CR, PSIF_BIT(CR_TXEN) | PSIF_BIT(CR_RXEN));
9281 + psif_writel(psif, IER, PSIF_BIT(RXRDY));
9282 +
9283 + psif->open = 1;
9284 +out:
9285 + return retval;
9286 +}
9287 +
9288 +static void psif_close(struct serio *io)
9289 +{
9290 + struct psif *psif = io->port_data;
9291 +
9292 + psif->open = 0;
9293 +
9294 + psif_writel(psif, IDR, ~0UL);
9295 + psif_writel(psif, CR, PSIF_BIT(CR_TXDIS) | PSIF_BIT(CR_RXDIS));
9296 +
9297 + clk_disable(psif->pclk);
9298 +}
9299 +
9300 +static void psif_set_prescaler(struct psif *psif)
9301 +{
9302 + unsigned long prscv;
9303 + unsigned long rate = clk_get_rate(psif->pclk);
9304 +
9305 + /* PRSCV = Pulse length (100 us) * PSIF module frequency. */
9306 + prscv = 100 * (rate / 1000000UL);
9307 +
9308 + if (prscv > ((1<<PSIF_PSR_PRSCV_SIZE) - 1)) {
9309 + prscv = (1<<PSIF_PSR_PRSCV_SIZE) - 1;
9310 + dev_dbg(&psif->pdev->dev, "pclk too fast, "
9311 + "prescaler set to max\n");
9312 + }
9313 +
9314 + clk_enable(psif->pclk);
9315 + psif_writel(psif, PSR, prscv);
9316 + clk_disable(psif->pclk);
9317 +}
9318 +
9319 +static int __init psif_probe(struct platform_device *pdev)
9320 +{
9321 + struct resource *regs;
9322 + struct psif *psif;
9323 + struct serio *io;
9324 + struct clk *pclk;
9325 + int irq;
9326 + int ret;
9327 +
9328 + psif = kzalloc(sizeof(struct psif), GFP_KERNEL);
9329 + if (!psif) {
9330 + dev_dbg(&pdev->dev, "out of memory\n");
9331 + ret = -ENOMEM;
9332 + goto out;
9333 + }
9334 + psif->pdev = pdev;
9335 +
9336 + io = kzalloc(sizeof(struct serio), GFP_KERNEL);
9337 + if (!io) {
9338 + dev_dbg(&pdev->dev, "out of memory\n");
9339 + ret = -ENOMEM;
9340 + goto out_free_psif;
9341 + }
9342 + psif->io = io;
9343 +
9344 + regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
9345 + if (!regs) {
9346 + dev_dbg(&pdev->dev, "no mmio resources defined\n");
9347 + ret = -ENOMEM;
9348 + goto out_free_io;
9349 + }
9350 +
9351 + psif->regs = ioremap(regs->start, regs->end - regs->start + 1);
9352 + if (!psif->regs) {
9353 + ret = -ENOMEM;
9354 + dev_dbg(&pdev->dev, "could not map I/O memory\n");
9355 + goto out_free_io;
9356 + }
9357 +
9358 + pclk = clk_get(&pdev->dev, "pclk");
9359 + if (IS_ERR(pclk)) {
9360 + dev_dbg(&pdev->dev, "could not get peripheral clock\n");
9361 + ret = PTR_ERR(pclk);
9362 + goto out_iounmap;
9363 + }
9364 + psif->pclk = pclk;
9365 +
9366 + /* Reset the PSIF to enter at a known state. */
9367 + ret = clk_enable(pclk);
9368 + if (ret) {
9369 + dev_dbg(&pdev->dev, "could not enable pclk\n");
9370 + goto out_put_clk;
9371 + }
9372 + psif_writel(psif, CR, PSIF_BIT(CR_SWRST));
9373 + clk_disable(pclk);
9374 +
9375 + setup_timer(&psif->tx_timer, psif_transmit_data, (unsigned long)psif);
9376 +
9377 + irq = platform_get_irq(pdev, 0);
9378 + if (irq < 0) {
9379 + dev_dbg(&pdev->dev, "could not get irq\n");
9380 + ret = -ENXIO;
9381 + goto out_put_clk;
9382 + }
9383 + ret = request_irq(irq, psif_interrupt, IRQF_SHARED, "at32psif", psif);
9384 + if (ret) {
9385 + dev_dbg(&pdev->dev, "could not request irq %d\n", irq);
9386 + goto out_put_clk;
9387 + }
9388 + psif->irq = irq;
9389 +
9390 + io->id.type = SERIO_8042;
9391 + io->write = psif_write;
9392 + io->open = psif_open;
9393 + io->close = psif_close;
9394 + strlcpy(io->name, pdev->dev.bus_id, sizeof(io->name));
9395 + strlcpy(io->phys, pdev->dev.bus_id, sizeof(io->phys));
9396 + io->port_data = psif;
9397 + io->dev.parent = &pdev->dev;
9398 +
9399 + psif_set_prescaler(psif);
9400 +
9401 + spin_lock_init(&psif->lock);
9402 + serio_register_port(psif->io);
9403 + platform_set_drvdata(pdev, psif);
9404 +
9405 + dev_info(&pdev->dev, "Atmel AVR32 PSIF PS/2 driver on 0x%08x irq %d\n",
9406 + (int)psif->regs, psif->irq);
9407 +
9408 + return 0;
9409 +
9410 +out_put_clk:
9411 + clk_put(psif->pclk);
9412 +out_iounmap:
9413 + iounmap(psif->regs);
9414 +out_free_io:
9415 + kfree(io);
9416 +out_free_psif:
9417 + kfree(psif);
9418 +out:
9419 + return ret;
9420 +}
9421 +
9422 +static int __exit psif_remove(struct platform_device *pdev)
9423 +{
9424 + struct psif *psif = platform_get_drvdata(pdev);
9425 +
9426 + psif_writel(psif, IDR, ~0UL);
9427 + psif_writel(psif, CR, PSIF_BIT(CR_TXDIS) | PSIF_BIT(CR_RXDIS));
9428 +
9429 + serio_unregister_port(psif->io);
9430 + iounmap(psif->regs);
9431 + free_irq(psif->irq, psif);
9432 + clk_put(psif->pclk);
9433 + kfree(psif);
9434 +
9435 + platform_set_drvdata(pdev, NULL);
9436 +
9437 + return 0;
9438 +}
9439 +
9440 +#ifdef CONFIG_PM
9441 +static int psif_suspend(struct platform_device *pdev, pm_message_t state)
9442 +{
9443 + struct psif *psif = platform_get_drvdata(pdev);
9444 +
9445 + if (psif->open) {
9446 + psif_writel(psif, CR, PSIF_BIT(CR_RXDIS) | PSIF_BIT(CR_TXDIS));
9447 + clk_disable(psif->pclk);
9448 + }
9449 +
9450 + return 0;
9451 +}
9452 +
9453 +static int psif_resume(struct platform_device *pdev)
9454 +{
9455 + struct psif *psif = platform_get_drvdata(pdev);
9456 +
9457 + if (psif->open) {
9458 + clk_enable(psif->pclk);
9459 + psif_set_prescaler(psif);
9460 + psif_writel(psif, CR, PSIF_BIT(CR_RXEN) | PSIF_BIT(CR_TXEN));
9461 + }
9462 +
9463 + return 0;
9464 +}
9465 +#else
9466 +#define psif_suspend NULL
9467 +#define psif_resume NULL
9468 +#endif
9469 +
9470 +static struct platform_driver psif_driver = {
9471 + .remove = __exit_p(psif_remove),
9472 + .driver = {
9473 + .name = "atmel_psif",
9474 + },
9475 + .suspend = psif_suspend,
9476 + .resume = psif_resume,
9477 +};
9478 +
9479 +static int __init psif_init(void)
9480 +{
9481 + return platform_driver_probe(&psif_driver, psif_probe);
9482 +}
9483 +
9484 +static void __exit psif_exit(void)
9485 +{
9486 + platform_driver_unregister(&psif_driver);
9487 +}
9488 +
9489 +module_init(psif_init);
9490 +module_exit(psif_exit);
9491 +
9492 +MODULE_AUTHOR("Hans-Christian Egtvedt <hcegtvedt@atmel.com>");
9493 +MODULE_DESCRIPTION("Atmel AVR32 PSIF PS/2 driver");
9494 +MODULE_LICENSE("GPL");
9495 diff --exclude=.git -urN linux-2.6.25.6/drivers/input/serio/at32psif.h avr32-2.6/drivers/input/serio/at32psif.h
9496 --- linux-2.6.25.6/drivers/input/serio/at32psif.h 1970-01-01 01:00:00.000000000 +0100
9497 +++ avr32-2.6/drivers/input/serio/at32psif.h 2008-06-12 15:09:40.619816021 +0200
9498 @@ -0,0 +1,82 @@
9499 +/*
9500 + * Copyright (C) 2007 Atmel Corporation
9501 + *
9502 + * Driver for the AT32AP700X PS/2 controller (PSIF).
9503 + *
9504 + * This program is free software; you can redistribute it and/or modify it
9505 + * under the terms of the GNU General Public License version 2 as published
9506 + * by the Free Software Foundation.
9507 + */
9508 +
9509 +#ifndef _AT32PSIF_H
9510 +#define _AT32PSIF_H
9511 +
9512 +/* PSIF register offsets */
9513 +#define PSIF_CR 0x00
9514 +#define PSIF_RHR 0x04
9515 +#define PSIF_THR 0x08
9516 +#define PSIF_SR 0x10
9517 +#define PSIF_IER 0x14
9518 +#define PSIF_IDR 0x18
9519 +#define PSIF_IMR 0x1c
9520 +#define PSIF_PSR 0x24
9521 +
9522 +/* Bitfields in control register. */
9523 +#define PSIF_CR_RXDIS_OFFSET 1
9524 +#define PSIF_CR_RXDIS_SIZE 1
9525 +#define PSIF_CR_RXEN_OFFSET 0
9526 +#define PSIF_CR_RXEN_SIZE 1
9527 +#define PSIF_CR_SWRST_OFFSET 15
9528 +#define PSIF_CR_SWRST_SIZE 1
9529 +#define PSIF_CR_TXDIS_OFFSET 9
9530 +#define PSIF_CR_TXDIS_SIZE 1
9531 +#define PSIF_CR_TXEN_OFFSET 8
9532 +#define PSIF_CR_TXEN_SIZE 1
9533 +
9534 +/* Bitfields in interrupt disable, enable, mask and status register. */
9535 +#define PSIF_NACK_OFFSET 8
9536 +#define PSIF_NACK_SIZE 1
9537 +#define PSIF_OVRUN_OFFSET 5
9538 +#define PSIF_OVRUN_SIZE 1
9539 +#define PSIF_PARITY_OFFSET 9
9540 +#define PSIF_PARITY_SIZE 1
9541 +#define PSIF_RXRDY_OFFSET 4
9542 +#define PSIF_RXRDY_SIZE 1
9543 +#define PSIF_TXEMPTY_OFFSET 1
9544 +#define PSIF_TXEMPTY_SIZE 1
9545 +#define PSIF_TXRDY_OFFSET 0
9546 +#define PSIF_TXRDY_SIZE 1
9547 +
9548 +/* Bitfields in prescale register. */
9549 +#define PSIF_PSR_PRSCV_OFFSET 0
9550 +#define PSIF_PSR_PRSCV_SIZE 12
9551 +
9552 +/* Bitfields in receive hold register. */
9553 +#define PSIF_RHR_RXDATA_OFFSET 0
9554 +#define PSIF_RHR_RXDATA_SIZE 8
9555 +
9556 +/* Bitfields in transmit hold register. */
9557 +#define PSIF_THR_TXDATA_OFFSET 0
9558 +#define PSIF_THR_TXDATA_SIZE 8
9559 +
9560 +/* Bit manipulation macros */
9561 +#define PSIF_BIT(name) \
9562 + (1 << PSIF_##name##_OFFSET)
9563 +#define PSIF_BF(name, value) \
9564 + (((value) & ((1 << PSIF_##name##_SIZE) - 1)) \
9565 + << PSIF_##name##_OFFSET)
9566 +#define PSIF_BFEXT(name, value)\
9567 + (((value) >> PSIF_##name##_OFFSET) \
9568 + & ((1 << PSIF_##name##_SIZE) - 1))
9569 +#define PSIF_BFINS(name, value, old) \
9570 + (((old) & ~(((1 << PSIF_##name##_SIZE) - 1) \
9571 + << PSIF_##name##_OFFSET)) \
9572 + | PSIF_BF(name, value))
9573 +
9574 +/* Register access macros */
9575 +#define psif_readl(port, reg) \
9576 + __raw_readl((port)->regs + PSIF_##reg)
9577 +#define psif_writel(port, reg, value) \
9578 + __raw_writel((value), (port)->regs + PSIF_##reg)
9579 +
9580 +#endif /* _AT32PSIF_H */
9581 diff --exclude=.git -urN linux-2.6.25.6/drivers/input/serio/Kconfig avr32-2.6/drivers/input/serio/Kconfig
9582 --- linux-2.6.25.6/drivers/input/serio/Kconfig 2008-06-09 20:27:19.000000000 +0200
9583 +++ avr32-2.6/drivers/input/serio/Kconfig 2008-06-12 15:09:40.615815791 +0200
9584 @@ -88,6 +88,17 @@
9585 To compile this driver as a module, choose M here: the
9586 module will be called rpckbd.
9587
9588 +config SERIO_AT32PSIF
9589 + tristate "AVR32 PSIF PS/2 keyboard and mouse controller"
9590 + depends on AVR32
9591 + default n
9592 + help
9593 + Say Y here if you want to use the PSIF peripheral on AVR32 devices
9594 + and connect a PS/2 keyboard and/or mouse to it.
9595 +
9596 + To compile this driver as a module, choose M here: the module will
9597 + be called at32psif.
9598 +
9599 config SERIO_AMBAKMI
9600 tristate "AMBA KMI keyboard controller"
9601 depends on ARM_AMBA
9602 diff --exclude=.git -urN linux-2.6.25.6/drivers/input/serio/Makefile avr32-2.6/drivers/input/serio/Makefile
9603 --- linux-2.6.25.6/drivers/input/serio/Makefile 2008-06-09 20:27:19.000000000 +0200
9604 +++ avr32-2.6/drivers/input/serio/Makefile 2008-06-12 15:03:58.919815686 +0200
9605 @@ -12,6 +12,7 @@
9606 obj-$(CONFIG_SERIO_RPCKBD) += rpckbd.o
9607 obj-$(CONFIG_SERIO_SA1111) += sa1111ps2.o
9608 obj-$(CONFIG_SERIO_AMBAKMI) += ambakmi.o
9609 +obj-$(CONFIG_SERIO_AT32PSIF) += at32psif.o
9610 obj-$(CONFIG_SERIO_Q40KBD) += q40kbd.o
9611 obj-$(CONFIG_SERIO_GSCPS2) += gscps2.o
9612 obj-$(CONFIG_HP_SDC) += hp_sdc.o
9613 diff --exclude=.git -urN linux-2.6.25.6/drivers/misc/atmel_tclib.c avr32-2.6/drivers/misc/atmel_tclib.c
9614 --- linux-2.6.25.6/drivers/misc/atmel_tclib.c 1970-01-01 01:00:00.000000000 +0100
9615 +++ avr32-2.6/drivers/misc/atmel_tclib.c 2008-06-12 15:03:59.515815344 +0200
9616 @@ -0,0 +1,161 @@
9617 +#include <linux/atmel_tc.h>
9618 +#include <linux/clk.h>
9619 +#include <linux/err.h>
9620 +#include <linux/init.h>
9621 +#include <linux/io.h>
9622 +#include <linux/ioport.h>
9623 +#include <linux/kernel.h>
9624 +#include <linux/platform_device.h>
9625 +
9626 +/* Number of bytes to reserve for the iomem resource */
9627 +#define ATMEL_TC_IOMEM_SIZE 256
9628 +
9629 +
9630 +/*
9631 + * This is a thin library to solve the problem of how to portably allocate
9632 + * one of the TC blocks. For simplicity, it doesn't currently expect to
9633 + * share individual timers between different drivers.
9634 + */
9635 +
9636 +#if defined(CONFIG_AVR32)
9637 +/* AVR32 has these divide PBB */
9638 +const u8 atmel_tc_divisors[5] = { 0, 4, 8, 16, 32, };
9639 +EXPORT_SYMBOL(atmel_tc_divisors);
9640 +
9641 +#elif defined(CONFIG_ARCH_AT91)
9642 +/* AT91 has these divide MCK */
9643 +const u8 atmel_tc_divisors[5] = { 2, 8, 32, 128, 0, };
9644 +EXPORT_SYMBOL(atmel_tc_divisors);
9645 +
9646 +#endif
9647 +
9648 +static DEFINE_SPINLOCK(tc_list_lock);
9649 +static LIST_HEAD(tc_list);
9650 +
9651 +/**
9652 + * atmel_tc_alloc - allocate a specified TC block
9653 + * @block: which block to allocate
9654 + * @name: name to be associated with the iomem resource
9655 + *
9656 + * Caller allocates a block. If it is available, a pointer to a
9657 + * pre-initialized struct atmel_tc is returned. The caller can access
9658 + * the registers directly through the "regs" field.
9659 + */
9660 +struct atmel_tc *atmel_tc_alloc(unsigned block, const char *name)
9661 +{
9662 + struct atmel_tc *tc;
9663 + struct platform_device *pdev = NULL;
9664 + struct resource *r;
9665 +
9666 + spin_lock(&tc_list_lock);
9667 + list_for_each_entry(tc, &tc_list, node) {
9668 + if (tc->pdev->id == block) {
9669 + pdev = tc->pdev;
9670 + break;
9671 + }
9672 + }
9673 +
9674 + if (!pdev || tc->iomem)
9675 + goto fail;
9676 +
9677 + r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
9678 + r = request_mem_region(r->start, ATMEL_TC_IOMEM_SIZE, name);
9679 + if (!r)
9680 + goto fail;
9681 +
9682 + tc->regs = ioremap(r->start, ATMEL_TC_IOMEM_SIZE);
9683 + if (!tc->regs)
9684 + goto fail_ioremap;
9685 +
9686 + tc->iomem = r;
9687 +
9688 +out:
9689 + spin_unlock(&tc_list_lock);
9690 + return tc;
9691 +
9692 +fail_ioremap:
9693 + release_resource(r);
9694 +fail:
9695 + tc = NULL;
9696 + goto out;
9697 +}
9698 +EXPORT_SYMBOL_GPL(atmel_tc_alloc);
9699 +
9700 +/**
9701 + * atmel_tc_free - release a specified TC block
9702 + * @tc: Timer/counter block that was returned by atmel_tc_alloc()
9703 + *
9704 + * This reverses the effect of atmel_tc_alloc(), unmapping the I/O
9705 + * registers, invalidating the resource returned by that routine and
9706 + * making the TC available to other drivers.
9707 + */
9708 +void atmel_tc_free(struct atmel_tc *tc)
9709 +{
9710 + spin_lock(&tc_list_lock);
9711 + if (tc->regs) {
9712 + iounmap(tc->regs);
9713 + release_resource(tc->iomem);
9714 + tc->regs = NULL;
9715 + tc->iomem = NULL;
9716 + }
9717 + spin_unlock(&tc_list_lock);
9718 +}
9719 +EXPORT_SYMBOL_GPL(atmel_tc_free);
9720 +
9721 +static int __init tc_probe(struct platform_device *pdev)
9722 +{
9723 + struct atmel_tc *tc;
9724 + struct clk *clk;
9725 + int irq;
9726 +
9727 + if (!platform_get_resource(pdev, IORESOURCE_MEM, 0))
9728 + return -EINVAL;
9729 +
9730 + irq = platform_get_irq(pdev, 0);
9731 + if (irq < 0)
9732 + return -EINVAL;
9733 +
9734 + tc = kzalloc(sizeof(struct atmel_tc), GFP_KERNEL);
9735 + if (!tc)
9736 + return -ENOMEM;
9737 +
9738 + tc->pdev = pdev;
9739 +
9740 + clk = clk_get(&pdev->dev, "t0_clk");
9741 + if (IS_ERR(clk)) {
9742 + kfree(tc);
9743 + return -EINVAL;
9744 + }
9745 +
9746 + tc->clk[0] = clk;
9747 + tc->clk[1] = clk_get(&pdev->dev, "t1_clk");
9748 + if (IS_ERR(tc->clk[1]))
9749 + tc->clk[1] = clk;
9750 + tc->clk[2] = clk_get(&pdev->dev, "t2_clk");
9751 + if (IS_ERR(tc->clk[2]))
9752 + tc->clk[2] = clk;
9753 +
9754 + tc->irq[0] = irq;
9755 + tc->irq[1] = platform_get_irq(pdev, 1);
9756 + if (tc->irq[1] < 0)
9757 + tc->irq[1] = irq;
9758 + tc->irq[2] = platform_get_irq(pdev, 2);
9759 + if (tc->irq[2] < 0)
9760 + tc->irq[2] = irq;
9761 +
9762 + spin_lock(&tc_list_lock);
9763 + list_add_tail(&tc->node, &tc_list);
9764 + spin_unlock(&tc_list_lock);
9765 +
9766 + return 0;
9767 +}
9768 +
9769 +static struct platform_driver tc_driver = {
9770 + .driver.name = "atmel_tcb",
9771 +};
9772 +
9773 +static int __init tc_init(void)
9774 +{
9775 + return platform_driver_probe(&tc_driver, tc_probe);
9776 +}
9777 +arch_initcall(tc_init);
9778 diff --exclude=.git -urN linux-2.6.25.6/drivers/misc/Kconfig avr32-2.6/drivers/misc/Kconfig
9779 --- linux-2.6.25.6/drivers/misc/Kconfig 2008-06-09 20:27:19.000000000 +0200
9780 +++ avr32-2.6/drivers/misc/Kconfig 2008-06-12 15:09:41.067816939 +0200
9781 @@ -22,6 +22,39 @@
9782 purposes including software controlled power-efficent backlights
9783 on LCD displays, motor control, and waveform generation.
9784
9785 +config ATMEL_TCLIB
9786 + bool "Atmel AT32/AT91 Timer/Counter Library"
9787 + depends on (AVR32 || ARCH_AT91)
9788 + help
9789 + Select this if you want a library to allocate the Timer/Counter
9790 + blocks found on many Atmel processors. This facilitates using
9791 + these blocks by different drivers despite processor differences.
9792 +
9793 +config ATMEL_TCB_CLKSRC
9794 + bool "TC Block Clocksource"
9795 + depends on ATMEL_TCLIB && GENERIC_TIME
9796 + default y
9797 + help
9798 + Select this to get a high precision clocksource based on a
9799 + TC block with a 5+ MHz base clock rate. Two timer channels
9800 + are combined to make a single 32-bit timer.
9801 +
9802 + When GENERIC_CLOCKEVENTS is defined, the third timer channel
9803 + may be used as a clock event device supporting oneshot mode
9804 + (delays of up to two seconds) based on the 32 KiHz clock.
9805 +
9806 +config ATMEL_TCB_CLKSRC_BLOCK
9807 + int
9808 + depends on ATMEL_TCB_CLKSRC
9809 + prompt "TC Block" if ARCH_AT91RM9200 || ARCH_AT91SAM9260 || CPU_AT32AP700X
9810 + default 0
9811 + range 0 1
9812 + help
9813 + Some chips provide more than one TC block, so you have the
9814 + choice of which one to use for the clock framework. The other
9815 + TC can be used for other purposes, such as PWM generation and
9816 + interval timing.
9817 +
9818 config IBM_ASM
9819 tristate "Device driver for IBM RSA service processor"
9820 depends on X86 && PCI && INPUT && EXPERIMENTAL
9821 diff --exclude=.git -urN linux-2.6.25.6/drivers/misc/Makefile avr32-2.6/drivers/misc/Makefile
9822 --- linux-2.6.25.6/drivers/misc/Makefile 2008-06-09 20:27:19.000000000 +0200
9823 +++ avr32-2.6/drivers/misc/Makefile 2008-06-12 15:09:41.067816939 +0200
9824 @@ -10,6 +10,7 @@
9825 obj-$(CONFIG_ASUS_LAPTOP) += asus-laptop.o
9826 obj-$(CONFIG_ATMEL_PWM) += atmel_pwm.o
9827 obj-$(CONFIG_ATMEL_SSC) += atmel-ssc.o
9828 +obj-$(CONFIG_ATMEL_TCLIB) += atmel_tclib.o
9829 obj-$(CONFIG_TC1100_WMI) += tc1100-wmi.o
9830 obj-$(CONFIG_LKDTM) += lkdtm.o
9831 obj-$(CONFIG_TIFM_CORE) += tifm_core.o
9832 diff --exclude=.git -urN linux-2.6.25.6/drivers/mmc/host/atmel-mci.c avr32-2.6/drivers/mmc/host/atmel-mci.c
9833 --- linux-2.6.25.6/drivers/mmc/host/atmel-mci.c 1970-01-01 01:00:00.000000000 +0100
9834 +++ avr32-2.6/drivers/mmc/host/atmel-mci.c 2008-06-12 15:09:41.083816184 +0200
9835 @@ -0,0 +1,1220 @@
9836 +/*
9837 + * Atmel MultiMedia Card Interface driver
9838 + *
9839 + * Copyright (C) 2004-2006 Atmel Corporation
9840 + *
9841 + * This program is free software; you can redistribute it and/or modify
9842 + * it under the terms of the GNU General Public License version 2 as
9843 + * published by the Free Software Foundation.
9844 + */
9845 +#include <linux/blkdev.h>
9846 +#include <linux/clk.h>
9847 +#include <linux/device.h>
9848 +#include <linux/dma-mapping.h>
9849 +#include <linux/init.h>
9850 +#include <linux/interrupt.h>
9851 +#include <linux/ioport.h>
9852 +#include <linux/module.h>
9853 +#include <linux/platform_device.h>
9854 +
9855 +#include <linux/mmc/host.h>
9856 +
9857 +#include <asm/dma-controller.h>
9858 +#include <asm/io.h>
9859 +#include <asm/arch/board.h>
9860 +#include <asm/arch/gpio.h>
9861 +
9862 +#include "atmel-mci.h"
9863 +
9864 +#define DRIVER_NAME "atmel_mci"
9865 +
9866 +#define MCI_DATA_ERROR_FLAGS (MCI_BIT(DCRCE) | MCI_BIT(DTOE) | \
9867 + MCI_BIT(OVRE) | MCI_BIT(UNRE))
9868 +
9869 +enum {
9870 + EVENT_CMD_COMPLETE = 0,
9871 + EVENT_DATA_COMPLETE,
9872 + EVENT_DATA_ERROR,
9873 + EVENT_STOP_SENT,
9874 + EVENT_STOP_COMPLETE,
9875 + EVENT_DMA_COMPLETE,
9876 + EVENT_DMA_ERROR,
9877 +};
9878 +
9879 +struct atmel_mci_dma {
9880 + struct dma_request_sg req;
9881 + unsigned short rx_periph_id;
9882 + unsigned short tx_periph_id;
9883 +};
9884 +
9885 +struct atmel_mci {
9886 + struct mmc_host *mmc;
9887 + void __iomem *regs;
9888 + struct atmel_mci_dma dma;
9889 +
9890 + struct mmc_request *mrq;
9891 + struct mmc_command *cmd;
9892 + struct mmc_data *data;
9893 +
9894 + u32 cmd_status;
9895 + u32 data_status;
9896 + u32 stop_status;
9897 + u32 stop_cmdr;
9898 +
9899 + struct tasklet_struct tasklet;
9900 + unsigned long pending_events;
9901 + unsigned long completed_events;
9902 +
9903 + int present;
9904 + int detect_pin;
9905 + int wp_pin;
9906 +
9907 + /* For detect pin debouncing */
9908 + struct timer_list detect_timer;
9909 +
9910 + unsigned long bus_hz;
9911 + unsigned long mapbase;
9912 + struct clk *mck;
9913 + struct platform_device *pdev;
9914 +
9915 +#ifdef CONFIG_DEBUG_FS
9916 + struct dentry *debugfs_root;
9917 + struct dentry *debugfs_regs;
9918 + struct dentry *debugfs_req;
9919 + struct dentry *debugfs_pending_events;
9920 + struct dentry *debugfs_completed_events;
9921 +#endif
9922 +};
9923 +
9924 +/* Those printks take an awful lot of time... */
9925 +#ifndef DEBUG
9926 +static unsigned int fmax = 15000000U;
9927 +#else
9928 +static unsigned int fmax = 1000000U;
9929 +#endif
9930 +module_param(fmax, uint, 0444);
9931 +MODULE_PARM_DESC(fmax, "Max frequency in Hz of the MMC bus clock");
9932 +
9933 +/* Test bit macros for completed events */
9934 +#define mci_cmd_is_complete(host) \
9935 + test_bit(EVENT_CMD_COMPLETE, &host->completed_events)
9936 +#define mci_data_is_complete(host) \
9937 + test_bit(EVENT_DATA_COMPLETE, &host->completed_events)
9938 +#define mci_data_error_is_complete(host) \
9939 + test_bit(EVENT_DATA_ERROR, &host->completed_events)
9940 +#define mci_stop_sent_is_complete(host) \
9941 + test_bit(EVENT_STOP_SENT, &host->completed_events)
9942 +#define mci_stop_is_complete(host) \
9943 + test_bit(EVENT_STOP_COMPLETE, &host->completed_events)
9944 +#define mci_dma_is_complete(host) \
9945 + test_bit(EVENT_DMA_COMPLETE, &host->completed_events)
9946 +#define mci_dma_error_is_complete(host) \
9947 + test_bit(EVENT_DMA_ERROR, &host->completed_events)
9948 +
9949 +/* Test and clear bit macros for pending events */
9950 +#define mci_clear_cmd_is_pending(host) \
9951 + test_and_clear_bit(EVENT_CMD_COMPLETE, &host->pending_events)
9952 +#define mci_clear_data_is_pending(host) \
9953 + test_and_clear_bit(EVENT_DATA_COMPLETE, &host->pending_events)
9954 +#define mci_clear_data_error_is_pending(host) \
9955 + test_and_clear_bit(EVENT_DATA_ERROR, &host->pending_events)
9956 +#define mci_clear_stop_sent_is_pending(host) \
9957 + test_and_clear_bit(EVENT_STOP_SENT, &host->pending_events)
9958 +#define mci_clear_stop_is_pending(host) \
9959 + test_and_clear_bit(EVENT_STOP_COMPLETE, &host->pending_events)
9960 +#define mci_clear_dma_error_is_pending(host) \
9961 + test_and_clear_bit(EVENT_DMA_ERROR, &host->pending_events)
9962 +
9963 +/* Test and set bit macros for completed events */
9964 +#define mci_set_cmd_is_completed(host) \
9965 + test_and_set_bit(EVENT_CMD_COMPLETE, &host->completed_events)
9966 +#define mci_set_data_is_completed(host) \
9967 + test_and_set_bit(EVENT_DATA_COMPLETE, &host->completed_events)
9968 +#define mci_set_data_error_is_completed(host) \
9969 + test_and_set_bit(EVENT_DATA_ERROR, &host->completed_events)
9970 +#define mci_set_stop_sent_is_completed(host) \
9971 + test_and_set_bit(EVENT_STOP_SENT, &host->completed_events)
9972 +#define mci_set_stop_is_completed(host) \
9973 + test_and_set_bit(EVENT_STOP_COMPLETE, &host->completed_events)
9974 +#define mci_set_dma_error_is_completed(host) \
9975 + test_and_set_bit(EVENT_DMA_ERROR, &host->completed_events)
9976 +
9977 +/* Set bit macros for completed events */
9978 +#define mci_set_cmd_complete(host) \
9979 + set_bit(EVENT_CMD_COMPLETE, &host->completed_events)
9980 +#define mci_set_data_complete(host) \
9981 + set_bit(EVENT_DATA_COMPLETE, &host->completed_events)
9982 +#define mci_set_data_error_complete(host) \
9983 + set_bit(EVENT_DATA_ERROR, &host->completed_events)
9984 +#define mci_set_stop_sent_complete(host) \
9985 + set_bit(EVENT_STOP_SENT, &host->completed_events)
9986 +#define mci_set_stop_complete(host) \
9987 + set_bit(EVENT_STOP_COMPLETE, &host->completed_events)
9988 +#define mci_set_dma_complete(host) \
9989 + set_bit(EVENT_DMA_COMPLETE, &host->completed_events)
9990 +#define mci_set_dma_error_complete(host) \
9991 + set_bit(EVENT_DMA_ERROR, &host->completed_events)
9992 +
9993 +/* Set bit macros for pending events */
9994 +#define mci_set_cmd_pending(host) \
9995 + set_bit(EVENT_CMD_COMPLETE, &host->pending_events)
9996 +#define mci_set_data_pending(host) \
9997 + set_bit(EVENT_DATA_COMPLETE, &host->pending_events)
9998 +#define mci_set_data_error_pending(host) \
9999 + set_bit(EVENT_DATA_ERROR, &host->pending_events)
10000 +#define mci_set_stop_sent_pending(host) \
10001 + set_bit(EVENT_STOP_SENT, &host->pending_events)
10002 +#define mci_set_stop_pending(host) \
10003 + set_bit(EVENT_STOP_COMPLETE, &host->pending_events)
10004 +#define mci_set_dma_error_pending(host) \
10005 + set_bit(EVENT_DMA_ERROR, &host->pending_events)
10006 +
10007 +/* Clear bit macros for pending events */
10008 +#define mci_clear_cmd_pending(host) \
10009 + clear_bit(EVENT_CMD_COMPLETE, &host->pending_events)
10010 +#define mci_clear_data_pending(host) \
10011 + clear_bit(EVENT_DATA_COMPLETE, &host->pending_events)
10012 +#define mci_clear_data_error_pending(host) \
10013 + clear_bit(EVENT_DATA_ERROR, &host->pending_events)
10014 +#define mci_clear_stop_sent_pending(host) \
10015 + clear_bit(EVENT_STOP_SENT, &host->pending_events)
10016 +#define mci_clear_stop_pending(host) \
10017 + clear_bit(EVENT_STOP_COMPLETE, &host->pending_events)
10018 +#define mci_clear_dma_error_pending(host) \
10019 + clear_bit(EVENT_DMA_ERROR, &host->pending_events)
10020 +
10021 +
10022 +#ifdef CONFIG_DEBUG_FS
10023 +#include <linux/debugfs.h>
10024 +
10025 +#define DBG_REQ_BUF_SIZE (4096 - sizeof(unsigned int))
10026 +
10027 +struct req_dbg_data {
10028 + unsigned int nbytes;
10029 + char str[DBG_REQ_BUF_SIZE];
10030 +};
10031 +
10032 +static int req_dbg_open(struct inode *inode, struct file *file)
10033 +{
10034 + struct atmel_mci *host;
10035 + struct mmc_request *mrq;
10036 + struct mmc_command *cmd, *stop;
10037 + struct mmc_data *data;
10038 + struct req_dbg_data *priv;
10039 + char *str;
10040 + unsigned long n = 0;
10041 +
10042 + priv = kzalloc(DBG_REQ_BUF_SIZE, GFP_KERNEL);
10043 + if (!priv)
10044 + return -ENOMEM;
10045 + str = priv->str;
10046 +
10047 + mutex_lock(&inode->i_mutex);
10048 + host = inode->i_private;
10049 +
10050 + spin_lock_irq(&host->mmc->lock);
10051 + mrq = host->mrq;
10052 + if (mrq) {
10053 + cmd = mrq->cmd;
10054 + data = mrq->data;
10055 + stop = mrq->stop;
10056 + n = snprintf(str, DBG_REQ_BUF_SIZE,
10057 + "CMD%u(0x%x) %x %x %x %x %x (err %u)\n",
10058 + cmd->opcode, cmd->arg, cmd->flags,
10059 + cmd->resp[0], cmd->resp[1], cmd->resp[2],
10060 + cmd->resp[3], cmd->error);
10061 + if (n < DBG_REQ_BUF_SIZE && data)
10062 + n += snprintf(str + n, DBG_REQ_BUF_SIZE - n,
10063 + "DATA %u * %u (%u) %x (err %u)\n",
10064 + data->blocks, data->blksz,
10065 + data->bytes_xfered, data->flags,
10066 + data->error);
10067 + if (n < DBG_REQ_BUF_SIZE && stop)
10068 + n += snprintf(str + n, DBG_REQ_BUF_SIZE - n,
10069 + "CMD%u(0x%x) %x %x %x %x %x (err %u)\n",
10070 + stop->opcode, stop->arg, stop->flags,
10071 + stop->resp[0], stop->resp[1],
10072 + stop->resp[2], stop->resp[3],
10073 + stop->error);
10074 + }
10075 + spin_unlock_irq(&host->mmc->lock);
10076 + mutex_unlock(&inode->i_mutex);
10077 +
10078 + priv->nbytes = min(n, DBG_REQ_BUF_SIZE);
10079 + file->private_data = priv;
10080 +
10081 + return 0;
10082 +}
10083 +
10084 +static ssize_t req_dbg_read(struct file *file, char __user *buf,
10085 + size_t nbytes, loff_t *ppos)
10086 +{
10087 + struct req_dbg_data *priv = file->private_data;
10088 +
10089 + return simple_read_from_buffer(buf, nbytes, ppos,
10090 + priv->str, priv->nbytes);
10091 +}
10092 +
10093 +static int req_dbg_release(struct inode *inode, struct file *file)
10094 +{
10095 + kfree(file->private_data);
10096 + return 0;
10097 +}
10098 +
10099 +static const struct file_operations req_dbg_fops = {
10100 + .owner = THIS_MODULE,
10101 + .open = req_dbg_open,
10102 + .llseek = no_llseek,
10103 + .read = req_dbg_read,
10104 + .release = req_dbg_release,
10105 +};
10106 +
10107 +static int regs_dbg_open(struct inode *inode, struct file *file)
10108 +{
10109 + struct atmel_mci *host;
10110 + unsigned int i;
10111 + u32 *data;
10112 + int ret = -ENOMEM;
10113 +
10114 + mutex_lock(&inode->i_mutex);
10115 + host = inode->i_private;
10116 + data = kmalloc(inode->i_size, GFP_KERNEL);
10117 + if (!data)
10118 + goto out;
10119 +
10120 + spin_lock_irq(&host->mmc->lock);
10121 + for (i = 0; i < inode->i_size / 4; i++)
10122 + data[i] = __raw_readl(host->regs + i * 4);
10123 + spin_unlock_irq(&host->mmc->lock);
10124 +
10125 + file->private_data = data;
10126 + ret = 0;
10127 +
10128 +out:
10129 + mutex_unlock(&inode->i_mutex);
10130 +
10131 + return ret;
10132 +}
10133 +
10134 +static ssize_t regs_dbg_read(struct file *file, char __user *buf,
10135 + size_t nbytes, loff_t *ppos)
10136 +{
10137 + struct inode *inode = file->f_dentry->d_inode;
10138 + int ret;
10139 +
10140 + mutex_lock(&inode->i_mutex);
10141 + ret = simple_read_from_buffer(buf, nbytes, ppos,
10142 + file->private_data,
10143 + file->f_dentry->d_inode->i_size);
10144 + mutex_unlock(&inode->i_mutex);
10145 +
10146 + return ret;
10147 +}
10148 +
10149 +static int regs_dbg_release(struct inode *inode, struct file *file)
10150 +{
10151 + kfree(file->private_data);
10152 + return 0;
10153 +}
10154 +
10155 +static const struct file_operations regs_dbg_fops = {
10156 + .owner = THIS_MODULE,
10157 + .open = regs_dbg_open,
10158 + .llseek = generic_file_llseek,
10159 + .read = regs_dbg_read,
10160 + .release = regs_dbg_release,
10161 +};
10162 +
10163 +static void atmci_init_debugfs(struct atmel_mci *host)
10164 +{
10165 + struct mmc_host *mmc;
10166 + struct dentry *root, *regs;
10167 + struct resource *res;
10168 +
10169 + mmc = host->mmc;
10170 + root = debugfs_create_dir(mmc_hostname(mmc), NULL);
10171 + if (IS_ERR(root) || !root)
10172 + goto err_root;
10173 + host->debugfs_root = root;
10174 +
10175 + regs = debugfs_create_file("regs", 0400, root, host, &regs_dbg_fops);
10176 + if (!regs)
10177 + goto err_regs;
10178 +
10179 + res = platform_get_resource(host->pdev, IORESOURCE_MEM, 0);
10180 + regs->d_inode->i_size = res->end - res->start + 1;
10181 + host->debugfs_regs = regs;
10182 +
10183 + host->debugfs_req = debugfs_create_file("req", 0400, root,
10184 + host, &req_dbg_fops);
10185 + if (!host->debugfs_req)
10186 + goto err_req;
10187 +
10188 + host->debugfs_pending_events
10189 + = debugfs_create_u32("pending_events", 0400, root,
10190 + (u32 *)&host->pending_events);
10191 + if (!host->debugfs_pending_events)
10192 + goto err_pending_events;
10193 +
10194 + host->debugfs_completed_events
10195 + = debugfs_create_u32("completed_events", 0400, root,
10196 + (u32 *)&host->completed_events);
10197 + if (!host->debugfs_completed_events)
10198 + goto err_completed_events;
10199 +
10200 + return;
10201 +
10202 +err_completed_events:
10203 + debugfs_remove(host->debugfs_pending_events);
10204 +err_pending_events:
10205 + debugfs_remove(host->debugfs_req);
10206 +err_req:
10207 + debugfs_remove(host->debugfs_regs);
10208 +err_regs:
10209 + debugfs_remove(host->debugfs_root);
10210 +err_root:
10211 + host->debugfs_root = NULL;
10212 + dev_err(&host->pdev->dev,
10213 + "failed to initialize debugfs for %s\n",
10214 + mmc_hostname(mmc));
10215 +}
10216 +
10217 +static void atmci_cleanup_debugfs(struct atmel_mci *host)
10218 +{
10219 + if (host->debugfs_root) {
10220 + debugfs_remove(host->debugfs_completed_events);
10221 + debugfs_remove(host->debugfs_pending_events);
10222 + debugfs_remove(host->debugfs_req);
10223 + debugfs_remove(host->debugfs_regs);
10224 + debugfs_remove(host->debugfs_root);
10225 + host->debugfs_root = NULL;
10226 + }
10227 +}
10228 +#else
10229 +static inline void atmci_init_debugfs(struct atmel_mci *host)
10230 +{
10231 +
10232 +}
10233 +
10234 +static inline void atmci_cleanup_debugfs(struct atmel_mci *host)
10235 +{
10236 +
10237 +}
10238 +#endif /* CONFIG_DEBUG_FS */
10239 +
10240 +static inline unsigned int ns_to_clocks(struct atmel_mci *host,
10241 + unsigned int ns)
10242 +{
10243 + return (ns * (host->bus_hz / 1000000) + 999) / 1000;
10244 +}
10245 +
10246 +static void atmci_set_timeout(struct atmel_mci *host,
10247 + struct mmc_data *data)
10248 +{
10249 + static unsigned dtomul_to_shift[] = {
10250 + 0, 4, 7, 8, 10, 12, 16, 20
10251 + };
10252 + unsigned timeout;
10253 + unsigned dtocyc, dtomul;
10254 +
10255 + timeout = ns_to_clocks(host, data->timeout_ns) + data->timeout_clks;
10256 +
10257 + for (dtomul = 0; dtomul < 8; dtomul++) {
10258 + unsigned shift = dtomul_to_shift[dtomul];
10259 + dtocyc = (timeout + (1 << shift) - 1) >> shift;
10260 + if (dtocyc < 15)
10261 + break;
10262 + }
10263 +
10264 + if (dtomul >= 8) {
10265 + dtomul = 7;
10266 + dtocyc = 15;
10267 + }
10268 +
10269 + dev_dbg(&host->mmc->class_dev, "setting timeout to %u cycles\n",
10270 + dtocyc << dtomul_to_shift[dtomul]);
10271 + mci_writel(host, DTOR, (MCI_BF(DTOMUL, dtomul)
10272 + | MCI_BF(DTOCYC, dtocyc)));
10273 +}
10274 +
10275 +/*
10276 + * Return mask with command flags to be enabled for this command.
10277 + */
10278 +static u32 atmci_prepare_command(struct mmc_host *mmc,
10279 + struct mmc_command *cmd)
10280 +{
10281 + u32 cmdr;
10282 +
10283 + cmd->error = 0;
10284 +
10285 + cmdr = MCI_BF(CMDNB, cmd->opcode);
10286 +
10287 + if (cmd->flags & MMC_RSP_PRESENT) {
10288 + if (cmd->flags & MMC_RSP_136)
10289 + cmdr |= MCI_BF(RSPTYP, MCI_RSPTYP_136_BIT);
10290 + else
10291 + cmdr |= MCI_BF(RSPTYP, MCI_RSPTYP_48_BIT);
10292 + }
10293 +
10294 + /*
10295 + * This should really be MAXLAT_5 for CMD2 and ACMD41, but
10296 + * it's too difficult to determine whether this is an ACMD or
10297 + * not. Better make it 64.
10298 + */
10299 + cmdr |= MCI_BIT(MAXLAT);
10300 +
10301 + if (mmc->ios.bus_mode == MMC_BUSMODE_OPENDRAIN)
10302 + cmdr |= MCI_BIT(OPDCMD);
10303 +
10304 + dev_dbg(&mmc->class_dev,
10305 + "cmd: op %02x arg %08x flags %08x, cmdflags %08lx\n",
10306 + cmd->opcode, cmd->arg, cmd->flags, (unsigned long)cmdr);
10307 +
10308 + return cmdr;
10309 +}
10310 +
10311 +static void atmci_start_command(struct atmel_mci *host,
10312 + struct mmc_command *cmd,
10313 + u32 cmd_flags)
10314 +{
10315 + WARN_ON(host->cmd);
10316 + host->cmd = cmd;
10317 +
10318 + mci_writel(host, ARGR, cmd->arg);
10319 + mci_writel(host, CMDR, cmd_flags);
10320 +
10321 + if (cmd->data)
10322 + dma_start_request(host->dma.req.req.dmac,
10323 + host->dma.req.req.channel);
10324 +}
10325 +
10326 +/*
10327 + * Returns a mask of flags to be set in the command register when the
10328 + * command to start the transfer is to be sent.
10329 + */
10330 +static u32 atmci_prepare_data(struct mmc_host *mmc, struct mmc_data *data)
10331 +{
10332 + struct atmel_mci *host = mmc_priv(mmc);
10333 + u32 cmd_flags;
10334 +
10335 + WARN_ON(host->data);
10336 + host->data = data;
10337 +
10338 + atmci_set_timeout(host, data);
10339 + mci_writel(host, BLKR, (MCI_BF(BCNT, data->blocks)
10340 + | MCI_BF(BLKLEN, data->blksz)));
10341 + host->dma.req.block_size = data->blksz;
10342 + host->dma.req.nr_blocks = data->blocks;
10343 +
10344 + cmd_flags = MCI_BF(TRCMD, MCI_TRCMD_START_TRANS);
10345 + if (data->flags & MMC_DATA_STREAM)
10346 + cmd_flags |= MCI_BF(TRTYP, MCI_TRTYP_STREAM);
10347 + else if (data->blocks > 1)
10348 + cmd_flags |= MCI_BF(TRTYP, MCI_TRTYP_MULTI_BLOCK);
10349 + else
10350 + cmd_flags |= MCI_BF(TRTYP, MCI_TRTYP_BLOCK);
10351 +
10352 + if (data->flags & MMC_DATA_READ) {
10353 + cmd_flags |= MCI_BIT(TRDIR);
10354 + host->dma.req.nr_sg
10355 + = dma_map_sg(&host->pdev->dev, data->sg,
10356 + data->sg_len, DMA_FROM_DEVICE);
10357 + host->dma.req.periph_id = host->dma.rx_periph_id;
10358 + host->dma.req.direction = DMA_DIR_PERIPH_TO_MEM;
10359 + host->dma.req.data_reg = host->mapbase + MCI_RDR;
10360 + } else {
10361 + host->dma.req.nr_sg
10362 + = dma_map_sg(&host->pdev->dev, data->sg,
10363 + data->sg_len, DMA_TO_DEVICE);
10364 + host->dma.req.periph_id = host->dma.tx_periph_id;
10365 + host->dma.req.direction = DMA_DIR_MEM_TO_PERIPH;
10366 + host->dma.req.data_reg = host->mapbase + MCI_TDR;
10367 + }
10368 + host->dma.req.sg = data->sg;
10369 +
10370 + dma_prepare_request_sg(host->dma.req.req.dmac, &host->dma.req);
10371 +
10372 + return cmd_flags;
10373 +}
10374 +
10375 +static void atmci_request(struct mmc_host *mmc, struct mmc_request *mrq)
10376 +{
10377 + struct atmel_mci *host = mmc_priv(mmc);
10378 + struct mmc_data *data = mrq->data;
10379 + u32 iflags;
10380 + u32 cmdflags = 0;
10381 +
10382 + iflags = mci_readl(host, IMR);
10383 + if (iflags)
10384 + dev_warn(&mmc->class_dev, "WARNING: IMR=0x%08x\n",
10385 + mci_readl(host, IMR));
10386 +
10387 + WARN_ON(host->mrq != NULL);
10388 +
10389 + /*
10390 + * We may "know" the card is gone even though there's still an
10391 + * electrical connection. If so, we really need to communicate
10392 + * this to the MMC core since there won't be any more
10393 + * interrupts as the card is completely removed. Otherwise,
10394 + * the MMC core might believe the card is still there even
10395 + * though the card was just removed very slowly.
10396 + */
10397 + if (!host->present) {
10398 + mrq->cmd->error = -ENOMEDIUM;
10399 + mmc_request_done(mmc, mrq);
10400 + return;
10401 + }
10402 +
10403 + host->mrq = mrq;
10404 + host->pending_events = 0;
10405 + host->completed_events = 0;
10406 +
10407 + iflags = MCI_BIT(CMDRDY);
10408 + cmdflags = atmci_prepare_command(mmc, mrq->cmd);
10409 +
10410 + if (mrq->stop) {
10411 + WARN_ON(!data);
10412 +
10413 + host->stop_cmdr = atmci_prepare_command(mmc, mrq->stop);
10414 + host->stop_cmdr |= MCI_BF(TRCMD, MCI_TRCMD_STOP_TRANS);
10415 + if (!(data->flags & MMC_DATA_WRITE))
10416 + host->stop_cmdr |= MCI_BIT(TRDIR);
10417 + if (data->flags & MMC_DATA_STREAM)
10418 + host->stop_cmdr |= MCI_BF(TRTYP, MCI_TRTYP_STREAM);
10419 + else
10420 + host->stop_cmdr |= MCI_BF(TRTYP, MCI_TRTYP_MULTI_BLOCK);
10421 + }
10422 + if (data) {
10423 + cmdflags |= atmci_prepare_data(mmc, data);
10424 + iflags |= MCI_DATA_ERROR_FLAGS;
10425 + }
10426 +
10427 + atmci_start_command(host, mrq->cmd, cmdflags);
10428 + mci_writel(host, IER, iflags);
10429 +}
10430 +
10431 +static void atmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
10432 +{
10433 + struct atmel_mci *host = mmc_priv(mmc);
10434 + u32 mr;
10435 +
10436 + if (ios->clock) {
10437 + u32 clkdiv;
10438 +
10439 + /* Set clock rate */
10440 + clkdiv = DIV_ROUND_UP(host->bus_hz, 2 * ios->clock) - 1;
10441 + if (clkdiv > 255) {
10442 + dev_warn(&mmc->class_dev,
10443 + "clock %u too slow; using %lu\n",
10444 + ios->clock, host->bus_hz / (2 * 256));
10445 + clkdiv = 255;
10446 + }
10447 +
10448 + mr = mci_readl(host, MR);
10449 + mr = MCI_BFINS(CLKDIV, clkdiv, mr)
10450 + | MCI_BIT(WRPROOF) | MCI_BIT(RDPROOF);
10451 + mci_writel(host, MR, mr);
10452 +
10453 + /* Enable the MCI controller */
10454 + mci_writel(host, CR, MCI_BIT(MCIEN));
10455 + } else {
10456 + /* Disable the MCI controller */
10457 + mci_writel(host, CR, MCI_BIT(MCIDIS));
10458 + }
10459 +
10460 + switch (ios->bus_width) {
10461 + case MMC_BUS_WIDTH_1:
10462 + mci_writel(host, SDCR, 0);
10463 + break;
10464 + case MMC_BUS_WIDTH_4:
10465 + mci_writel(host, SDCR, MCI_BIT(SDCBUS));
10466 + break;
10467 + }
10468 +
10469 + switch (ios->power_mode) {
10470 + case MMC_POWER_ON:
10471 + /* Send init sequence (74 clock cycles) */
10472 + mci_writel(host, IDR, ~0UL);
10473 + mci_writel(host, CMDR, MCI_BF(SPCMD, MCI_SPCMD_INIT_CMD));
10474 + while (!(mci_readl(host, SR) & MCI_BIT(CMDRDY)))
10475 + cpu_relax();
10476 + break;
10477 + default:
10478 + /*
10479 + * TODO: None of the currently available AVR32-based
10480 + * boards allow MMC power to be turned off. Implement
10481 + * power control when this can be tested properly.
10482 + */
10483 + break;
10484 + }
10485 +}
10486 +
10487 +static int atmci_get_ro(struct mmc_host *mmc)
10488 +{
10489 + int read_only = 0;
10490 + struct atmel_mci *host = mmc_priv(mmc);
10491 +
10492 + if (host->wp_pin >= 0) {
10493 + read_only = gpio_get_value(host->wp_pin);
10494 + dev_dbg(&mmc->class_dev, "card is %s\n",
10495 + read_only ? "read-only" : "read-write");
10496 + } else {
10497 + dev_dbg(&mmc->class_dev,
10498 + "no pin for checking read-only switch."
10499 + " Assuming write-enable.\n");
10500 + }
10501 +
10502 + return read_only;
10503 +}
10504 +
10505 +static struct mmc_host_ops atmci_ops = {
10506 + .request = atmci_request,
10507 + .set_ios = atmci_set_ios,
10508 + .get_ro = atmci_get_ro,
10509 +};
10510 +
10511 +static void atmci_request_end(struct mmc_host *mmc, struct mmc_request *mrq)
10512 +{
10513 + struct atmel_mci *host = mmc_priv(mmc);
10514 +
10515 + WARN_ON(host->cmd || host->data);
10516 + host->mrq = NULL;
10517 +
10518 + mmc_request_done(mmc, mrq);
10519 +}
10520 +
10521 +static void send_stop_cmd(struct mmc_host *mmc, struct mmc_data *data,
10522 + u32 flags)
10523 +{
10524 + struct atmel_mci *host = mmc_priv(mmc);
10525 +
10526 + atmci_start_command(host, data->stop, host->stop_cmdr | flags);
10527 + mci_writel(host, IER, MCI_BIT(CMDRDY));
10528 +}
10529 +
10530 +static void atmci_data_complete(struct atmel_mci *host, struct mmc_data *data)
10531 +{
10532 + host->data = NULL;
10533 + dma_unmap_sg(&host->pdev->dev, data->sg, host->dma.req.nr_sg,
10534 + ((data->flags & MMC_DATA_WRITE)
10535 + ? DMA_TO_DEVICE : DMA_FROM_DEVICE));
10536 +
10537 + /*
10538 + * Data might complete before command for very short transfers
10539 + * (like READ_SCR)
10540 + */
10541 + if (mci_cmd_is_complete(host)
10542 + && (!data->stop || mci_stop_is_complete(host)))
10543 + atmci_request_end(host->mmc, data->mrq);
10544 +}
10545 +
10546 +static void atmci_command_complete(struct atmel_mci *host,
10547 + struct mmc_command *cmd, u32 status)
10548 +{
10549 + if (status & MCI_BIT(RTOE))
10550 + cmd->error = -ETIMEDOUT;
10551 + else if ((cmd->flags & MMC_RSP_CRC)
10552 + && (status & MCI_BIT(RCRCE)))
10553 + cmd->error = -EILSEQ;
10554 + else if (status & (MCI_BIT(RINDE) | MCI_BIT(RDIRE) | MCI_BIT(RENDE)))
10555 + cmd->error = -EIO;
10556 +
10557 + if (cmd->error) {
10558 + dev_dbg(&host->mmc->class_dev,
10559 + "command error: op=0x%x status=0x%08x\n",
10560 + cmd->opcode, status);
10561 +
10562 + if (cmd->data) {
10563 + dma_stop_request(host->dma.req.req.dmac,
10564 + host->dma.req.req.channel);
10565 + mci_writel(host, IDR, MCI_BIT(NOTBUSY)
10566 + | MCI_DATA_ERROR_FLAGS);
10567 + host->data = NULL;
10568 + }
10569 + }
10570 +}
10571 +
10572 +static void atmci_detect_change(unsigned long data)
10573 +{
10574 + struct atmel_mci *host = (struct atmel_mci *)data;
10575 + struct mmc_request *mrq = host->mrq;
10576 + int present;
10577 +
10578 + /*
10579 + * atmci_remove() sets detect_pin to -1 before freeing the
10580 + * interrupt. We must not re-enable the interrupt if it has
10581 + * been freed.
10582 + */
10583 + smp_rmb();
10584 + if (host->detect_pin < 0)
10585 + return;
10586 +
10587 + enable_irq(gpio_to_irq(host->detect_pin));
10588 + present = !gpio_get_value(host->detect_pin);
10589 +
10590 + dev_vdbg(&host->pdev->dev, "detect change: %d (was %d)\n",
10591 + present, host->present);
10592 +
10593 + if (present != host->present) {
10594 + dev_dbg(&host->mmc->class_dev, "card %s\n",
10595 + present ? "inserted" : "removed");
10596 + host->present = present;
10597 +
10598 + /* Reset controller if card is gone */
10599 + if (!present) {
10600 + mci_writel(host, CR, MCI_BIT(SWRST));
10601 + mci_writel(host, IDR, ~0UL);
10602 + mci_writel(host, CR, MCI_BIT(MCIEN));
10603 + }
10604 +
10605 + /* Clean up queue if present */
10606 + if (mrq) {
10607 + if (!mci_cmd_is_complete(host))
10608 + mrq->cmd->error = -ENOMEDIUM;
10609 + if (mrq->data && !mci_data_is_complete(host)
10610 + && !mci_data_error_is_complete(host)) {
10611 + dma_stop_request(host->dma.req.req.dmac,
10612 + host->dma.req.req.channel);
10613 + host->data->error = -ENOMEDIUM;
10614 + atmci_data_complete(host, host->data);
10615 + }
10616 + if (mrq->stop && !mci_stop_is_complete(host))
10617 + mrq->stop->error = -ENOMEDIUM;
10618 +
10619 + host->cmd = NULL;
10620 + atmci_request_end(host->mmc, mrq);
10621 + }
10622 +
10623 + mmc_detect_change(host->mmc, 0);
10624 + }
10625 +}
10626 +
10627 +static void atmci_tasklet_func(unsigned long priv)
10628 +{
10629 + struct mmc_host *mmc = (struct mmc_host *)priv;
10630 + struct atmel_mci *host = mmc_priv(mmc);
10631 + struct mmc_request *mrq = host->mrq;
10632 + struct mmc_data *data = host->data;
10633 +
10634 + dev_vdbg(&mmc->class_dev,
10635 + "tasklet: pending/completed/mask %lx/%lx/%x\n",
10636 + host->pending_events, host->completed_events,
10637 + mci_readl(host, IMR));
10638 +
10639 + if (mci_clear_cmd_is_pending(host)) {
10640 + mci_set_cmd_complete(host);
10641 + atmci_command_complete(host, mrq->cmd, host->cmd_status);
10642 + if (!host->data || mci_data_is_complete(host)
10643 + || mci_data_error_is_complete(host))
10644 + atmci_request_end(mmc, mrq);
10645 + }
10646 + if (mci_clear_stop_is_pending(host)) {
10647 + mci_set_stop_complete(host);
10648 + atmci_command_complete(host, mrq->stop, host->stop_status);
10649 + if (mci_data_is_complete(host)
10650 + || mci_data_error_is_complete(host))
10651 + atmci_request_end(mmc, mrq);
10652 + }
10653 + if (mci_clear_dma_error_is_pending(host)) {
10654 + mci_set_dma_error_complete(host);
10655 + mci_clear_data_pending(host);
10656 +
10657 + /* DMA controller got bus error => invalid address */
10658 + data->error = -EIO;
10659 +
10660 + dev_dbg(&mmc->class_dev, "dma error after %u bytes xfered\n",
10661 + host->data->bytes_xfered);
10662 +
10663 + if (data->stop
10664 + && !mci_set_stop_sent_is_completed(host))
10665 + /* TODO: Check if card is still present */
10666 + send_stop_cmd(host->mmc, data, 0);
10667 +
10668 + atmci_data_complete(host, data);
10669 + }
10670 + if (mci_clear_data_error_is_pending(host)) {
10671 + u32 status = host->data_status;
10672 +
10673 + mci_set_data_error_complete(host);
10674 + mci_clear_data_pending(host);
10675 +
10676 + dma_stop_request(host->dma.req.req.dmac,
10677 + host->dma.req.req.channel);
10678 +
10679 + if (status & MCI_BIT(DCRCE)) {
10680 + dev_dbg(&mmc->class_dev, "data CRC error\n");
10681 + data->error = -EILSEQ;
10682 + } else if (status & MCI_BIT(DTOE)) {
10683 + dev_dbg(&mmc->class_dev, "data timeout error\n");
10684 + data->error = -ETIMEDOUT;
10685 + } else {
10686 + dev_dbg(&mmc->class_dev, "data FIFO error\n");
10687 + data->error = -EIO;
10688 + }
10689 + dev_dbg(&mmc->class_dev, "bytes xfered: %u\n",
10690 + data->bytes_xfered);
10691 +
10692 + if (data->stop
10693 + && !mci_set_stop_sent_is_completed(host))
10694 + /* TODO: Check if card is still present */
10695 + send_stop_cmd(host->mmc, data, 0);
10696 +
10697 + atmci_data_complete(host, data);
10698 + }
10699 + if (mci_clear_data_is_pending(host)) {
10700 + mci_set_data_complete(host);
10701 + data->bytes_xfered = data->blocks * data->blksz;
10702 + atmci_data_complete(host, data);
10703 + }
10704 +}
10705 +
10706 +static void atmci_cmd_interrupt(struct mmc_host *mmc, u32 status)
10707 +{
10708 + struct atmel_mci *host = mmc_priv(mmc);
10709 + struct mmc_command *cmd = host->cmd;
10710 +
10711 + /*
10712 + * Read the response now so that we're free to send a new
10713 + * command immediately.
10714 + */
10715 + cmd->resp[0] = mci_readl(host, RSPR);
10716 + cmd->resp[1] = mci_readl(host, RSPR);
10717 + cmd->resp[2] = mci_readl(host, RSPR);
10718 + cmd->resp[3] = mci_readl(host, RSPR);
10719 +
10720 + mci_writel(host, IDR, MCI_BIT(CMDRDY));
10721 + host->cmd = NULL;
10722 +
10723 + if (mci_stop_sent_is_complete(host)) {
10724 + host->stop_status = status;
10725 + mci_set_stop_pending(host);
10726 + } else {
10727 + struct mmc_request *mrq = host->mrq;
10728 +
10729 + if (mrq->stop && mci_dma_is_complete(host)
10730 + && !mci_set_stop_sent_is_completed(host))
10731 + send_stop_cmd(host->mmc, mrq->data, 0);
10732 + host->cmd_status = status;
10733 + mci_set_cmd_pending(host);
10734 + }
10735 +
10736 + tasklet_schedule(&host->tasklet);
10737 +}
10738 +
10739 +static void atmci_xfer_complete(struct dma_request *_req)
10740 +{
10741 + struct dma_request_sg *req = to_dma_request_sg(_req);
10742 + struct atmel_mci_dma *dma;
10743 + struct atmel_mci *host;
10744 + struct mmc_data *data;
10745 +
10746 + dma = container_of(req, struct atmel_mci_dma, req);
10747 + host = container_of(dma, struct atmel_mci, dma);
10748 + data = host->data;
10749 +
10750 + /*
10751 + * This callback may be called before we see the CMDRDY
10752 + * interrupt under heavy irq load (possibly caused by other
10753 + * drivers) or when interrupts are disabled for a long time.
10754 + */
10755 + mci_set_dma_complete(host);
10756 + if (data->stop && mci_cmd_is_complete(host)
10757 + && !mci_set_stop_sent_is_completed(host))
10758 + send_stop_cmd(host->mmc, data, 0);
10759 +
10760 + /*
10761 + * Regardless of what the documentation says, we have to wait
10762 + * for NOTBUSY even after block read operations.
10763 + *
10764 + * When the DMA transfer is complete, the controller may still
10765 + * be reading the CRC from the card, i.e. the data transfer is
10766 + * still in progress and we haven't seen all the potential
10767 + * error bits yet.
10768 + */
10769 + mci_writel(host, IER, MCI_BIT(NOTBUSY));
10770 +}
10771 +
10772 +static void atmci_dma_error(struct dma_request *_req)
10773 +{
10774 + struct dma_request_sg *req = to_dma_request_sg(_req);
10775 + struct atmel_mci_dma *dma;
10776 + struct atmel_mci *host;
10777 +
10778 + dma = container_of(req, struct atmel_mci_dma, req);
10779 + host = container_of(dma, struct atmel_mci, dma);
10780 +
10781 + mci_writel(host, IDR, (MCI_BIT(NOTBUSY)
10782 + | MCI_DATA_ERROR_FLAGS));
10783 +
10784 + mci_set_dma_error_pending(host);
10785 + tasklet_schedule(&host->tasklet);
10786 +}
10787 +
10788 +static irqreturn_t atmci_interrupt(int irq, void *dev_id)
10789 +{
10790 + struct mmc_host *mmc = dev_id;
10791 + struct atmel_mci *host = mmc_priv(mmc);
10792 + u32 status, mask, pending;
10793 +
10794 + spin_lock(&mmc->lock);
10795 +
10796 + status = mci_readl(host, SR);
10797 + mask = mci_readl(host, IMR);
10798 + pending = status & mask;
10799 +
10800 + do {
10801 + if (pending & MCI_DATA_ERROR_FLAGS) {
10802 + mci_writel(host, IDR, (MCI_BIT(NOTBUSY)
10803 + | MCI_DATA_ERROR_FLAGS));
10804 + host->data_status = status;
10805 + mci_set_data_error_pending(host);
10806 + tasklet_schedule(&host->tasklet);
10807 + break;
10808 + }
10809 + if (pending & MCI_BIT(CMDRDY))
10810 + atmci_cmd_interrupt(mmc, status);
10811 + if (pending & MCI_BIT(NOTBUSY)) {
10812 + mci_writel(host, IDR, (MCI_BIT(NOTBUSY)
10813 + | MCI_DATA_ERROR_FLAGS));
10814 + mci_set_data_pending(host);
10815 + tasklet_schedule(&host->tasklet);
10816 + }
10817 +
10818 + status = mci_readl(host, SR);
10819 + mask = mci_readl(host, IMR);
10820 + pending = status & mask;
10821 + } while (pending);
10822 +
10823 + spin_unlock(&mmc->lock);
10824 +
10825 + return IRQ_HANDLED;
10826 +}
10827 +
10828 +static irqreturn_t atmci_detect_interrupt(int irq, void *dev_id)
10829 +{
10830 + struct mmc_host *mmc = dev_id;
10831 + struct atmel_mci *host = mmc_priv(mmc);
10832 +
10833 + /*
10834 + * Disable interrupts until the pin has stabilized and check
10835 + * the state then. Use mod_timer() since we may be in the
10836 + * middle of the timer routine when this interrupt triggers.
10837 + */
10838 + disable_irq_nosync(irq);
10839 + mod_timer(&host->detect_timer, jiffies + msecs_to_jiffies(20));
10840 +
10841 + return IRQ_HANDLED;
10842 +}
10843 +
10844 +static int __devinit atmci_probe(struct platform_device *pdev)
10845 +{
10846 + struct mci_platform_data *board;
10847 + struct atmel_mci *host;
10848 + struct mmc_host *mmc;
10849 + struct resource *regs;
10850 + int irq;
10851 + int ret;
10852 +
10853 + regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
10854 + if (!regs)
10855 + return -ENXIO;
10856 + irq = platform_get_irq(pdev, 0);
10857 + if (irq < 0)
10858 + return irq;
10859 +
10860 + board = pdev->dev.platform_data;
10861 +
10862 + mmc = mmc_alloc_host(sizeof(struct atmel_mci), &pdev->dev);
10863 + if (!mmc)
10864 + return -ENOMEM;
10865 +
10866 + host = mmc_priv(mmc);
10867 + host->pdev = pdev;
10868 + host->mmc = mmc;
10869 + if (board) {
10870 + host->detect_pin = board->detect_pin;
10871 + host->wp_pin = board->wp_pin;
10872 + } else {
10873 + host->detect_pin = -1;
10874 + host->wp_pin = -1;
10875 + }
10876 +
10877 + host->mck = clk_get(&pdev->dev, "mci_clk");
10878 + if (IS_ERR(host->mck)) {
10879 + ret = PTR_ERR(host->mck);
10880 + goto out_free_host;
10881 + }
10882 + clk_enable(host->mck);
10883 +
10884 + ret = -ENOMEM;
10885 + host->regs = ioremap(regs->start, regs->end - regs->start + 1);
10886 + if (!host->regs)
10887 + goto out_disable_clk;
10888 +
10889 + host->bus_hz = clk_get_rate(host->mck);
10890 + host->mapbase = regs->start;
10891 +
10892 + mmc->ops = &atmci_ops;
10893 + mmc->f_min = (host->bus_hz + 511) / 512;
10894 + mmc->f_max = min((unsigned int)(host->bus_hz / 2), fmax);
10895 + mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
10896 + mmc->caps |= MMC_CAP_4_BIT_DATA;
10897 +
10898 + tasklet_init(&host->tasklet, atmci_tasklet_func, (unsigned long)mmc);
10899 +
10900 + ret = request_irq(irq, atmci_interrupt, 0, "mmci", mmc);
10901 + if (ret)
10902 + goto out_unmap;
10903 +
10904 + /* Assume card is present if we don't have a detect pin */
10905 + host->present = 1;
10906 + if (host->detect_pin >= 0) {
10907 + if (gpio_request(host->detect_pin, "mmc_detect")) {
10908 + dev_dbg(&mmc->class_dev, "no detect pin available\n");
10909 + host->detect_pin = -1;
10910 + } else {
10911 + host->present = !gpio_get_value(host->detect_pin);
10912 + }
10913 + }
10914 + if (host->wp_pin >= 0) {
10915 + if (gpio_request(host->wp_pin, "mmc_wp")) {
10916 + dev_dbg(&mmc->class_dev, "no WP pin available\n");
10917 + host->wp_pin = -1;
10918 + }
10919 + }
10920 +
10921 + /* TODO: Get this information from platform data */
10922 + ret = -ENOMEM;
10923 + host->dma.req.req.dmac = find_dma_controller(0);
10924 + if (!host->dma.req.req.dmac) {
10925 + dev_dbg(&mmc->class_dev, "no DMA controller available\n");
10926 + goto out_free_irq;
10927 + }
10928 + ret = dma_alloc_channel(host->dma.req.req.dmac);
10929 + if (ret < 0) {
10930 + dev_dbg(&mmc->class_dev, "unable to allocate DMA channel\n");
10931 + goto out_free_irq;
10932 + }
10933 + host->dma.req.req.channel = ret;
10934 + host->dma.req.width = DMA_WIDTH_32BIT;
10935 + host->dma.req.req.xfer_complete = atmci_xfer_complete;
10936 + host->dma.req.req.block_complete = NULL; // atmci_block_complete;
10937 + host->dma.req.req.error = atmci_dma_error;
10938 + host->dma.rx_periph_id = 0;
10939 + host->dma.tx_periph_id = 1;
10940 +
10941 + mci_writel(host, CR, MCI_BIT(SWRST));
10942 + mci_writel(host, IDR, ~0UL);
10943 +
10944 + platform_set_drvdata(pdev, host);
10945 +
10946 + mmc_add_host(mmc);
10947 +
10948 + if (host->detect_pin >= 0) {
10949 + setup_timer(&host->detect_timer, atmci_detect_change,
10950 + (unsigned long)host);
10951 +
10952 + ret = request_irq(gpio_to_irq(host->detect_pin),
10953 + atmci_detect_interrupt,
10954 + IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
10955 + DRIVER_NAME, mmc);
10956 + if (ret) {
10957 + dev_dbg(&mmc->class_dev,
10958 + "could not request IRQ %d for detect pin\n",
10959 + gpio_to_irq(host->detect_pin));
10960 + gpio_free(host->detect_pin);
10961 + host->detect_pin = -1;
10962 + }
10963 + }
10964 +
10965 + dev_info(&mmc->class_dev, "Atmel MCI controller at 0x%08lx irq %d\n",
10966 + host->mapbase, irq);
10967 +
10968 + atmci_init_debugfs(host);
10969 +
10970 + return 0;
10971 +
10972 +out_free_irq:
10973 + if (host->detect_pin >= 0)
10974 + gpio_free(host->detect_pin);
10975 + if (host->wp_pin >= 0)
10976 + gpio_free(host->wp_pin);
10977 + free_irq(irq, mmc);
10978 +out_unmap:
10979 + iounmap(host->regs);
10980 +out_disable_clk:
10981 + clk_disable(host->mck);
10982 + clk_put(host->mck);
10983 +out_free_host:
10984 + mmc_free_host(mmc);
10985 + return ret;
10986 +}
10987 +
10988 +static int __devexit atmci_remove(struct platform_device *pdev)
10989 +{
10990 + struct atmel_mci *host = platform_get_drvdata(pdev);
10991 +
10992 + platform_set_drvdata(pdev, NULL);
10993 +
10994 + if (host) {
10995 + atmci_cleanup_debugfs(host);
10996 +
10997 + if (host->detect_pin >= 0) {
10998 + int pin = host->detect_pin;
10999 +
11000 + /* Make sure our timer doesn't enable the interrupt */
11001 + host->detect_pin = -1;
11002 + smp_wmb();
11003 +
11004 + free_irq(gpio_to_irq(pin), host->mmc);
11005 + del_timer_sync(&host->detect_timer);
11006 + cancel_delayed_work(&host->mmc->detect);
11007 + gpio_free(pin);
11008 + }
11009 +
11010 + mmc_remove_host(host->mmc);
11011 +
11012 + mci_writel(host, IDR, ~0UL);
11013 + mci_writel(host, CR, MCI_BIT(MCIDIS));
11014 + mci_readl(host, SR);
11015 +
11016 + dma_release_channel(host->dma.req.req.dmac,
11017 + host->dma.req.req.channel);
11018 +
11019 + if (host->wp_pin >= 0)
11020 + gpio_free(host->wp_pin);
11021 +
11022 + free_irq(platform_get_irq(pdev, 0), host->mmc);
11023 + iounmap(host->regs);
11024 +
11025 + clk_disable(host->mck);
11026 + clk_put(host->mck);
11027 +
11028 + mmc_free_host(host->mmc);
11029 + }
11030 + return 0;
11031 +}
11032 +
11033 +static struct platform_driver atmci_driver = {
11034 + .probe = atmci_probe,
11035 + .remove = __devexit_p(atmci_remove),
11036 + .driver = {
11037 + .name = DRIVER_NAME,
11038 + },
11039 +};
11040 +
11041 +static int __init atmci_init(void)
11042 +{
11043 + return platform_driver_register(&atmci_driver);
11044 +}
11045 +
11046 +static void __exit atmci_exit(void)
11047 +{
11048 + platform_driver_unregister(&atmci_driver);
11049 +}
11050 +
11051 +module_init(atmci_init);
11052 +module_exit(atmci_exit);
11053 +
11054 +MODULE_DESCRIPTION("Atmel Multimedia Card Interface driver");
11055 +MODULE_LICENSE("GPL");
11056 diff --exclude=.git -urN linux-2.6.25.6/drivers/mmc/host/atmel-mci.h avr32-2.6/drivers/mmc/host/atmel-mci.h
11057 --- linux-2.6.25.6/drivers/mmc/host/atmel-mci.h 1970-01-01 01:00:00.000000000 +0100
11058 +++ avr32-2.6/drivers/mmc/host/atmel-mci.h 2008-06-12 15:09:41.083816184 +0200
11059 @@ -0,0 +1,192 @@
11060 +/*
11061 + * Atmel MultiMedia Card Interface driver
11062 + *
11063 + * Copyright (C) 2004-2006 Atmel Corporation
11064 + *
11065 + * This program is free software; you can redistribute it and/or modify
11066 + * it under the terms of the GNU General Public License version 2 as
11067 + * published by the Free Software Foundation.
11068 + */
11069 +#ifndef __DRIVERS_MMC_ATMEL_MCI_H__
11070 +#define __DRIVERS_MMC_ATMEL_MCI_H__
11071 +
11072 +/* MCI register offsets */
11073 +#define MCI_CR 0x0000
11074 +#define MCI_MR 0x0004
11075 +#define MCI_DTOR 0x0008
11076 +#define MCI_SDCR 0x000c
11077 +#define MCI_ARGR 0x0010
11078 +#define MCI_CMDR 0x0014
11079 +#define MCI_BLKR 0x0018
11080 +#define MCI_RSPR 0x0020
11081 +#define MCI_RSPR1 0x0024
11082 +#define MCI_RSPR2 0x0028
11083 +#define MCI_RSPR3 0x002c
11084 +#define MCI_RDR 0x0030
11085 +#define MCI_TDR 0x0034
11086 +#define MCI_SR 0x0040
11087 +#define MCI_IER 0x0044
11088 +#define MCI_IDR 0x0048
11089 +#define MCI_IMR 0x004c
11090 +
11091 +/* Bitfields in CR */
11092 +#define MCI_MCIEN_OFFSET 0
11093 +#define MCI_MCIEN_SIZE 1
11094 +#define MCI_MCIDIS_OFFSET 1
11095 +#define MCI_MCIDIS_SIZE 1
11096 +#define MCI_PWSEN_OFFSET 2
11097 +#define MCI_PWSEN_SIZE 1
11098 +#define MCI_PWSDIS_OFFSET 3
11099 +#define MCI_PWSDIS_SIZE 1
11100 +#define MCI_SWRST_OFFSET 7
11101 +#define MCI_SWRST_SIZE 1
11102 +
11103 +/* Bitfields in MR */
11104 +#define MCI_CLKDIV_OFFSET 0
11105 +#define MCI_CLKDIV_SIZE 8
11106 +#define MCI_PWSDIV_OFFSET 8
11107 +#define MCI_PWSDIV_SIZE 3
11108 +#define MCI_RDPROOF_OFFSET 11
11109 +#define MCI_RDPROOF_SIZE 1
11110 +#define MCI_WRPROOF_OFFSET 12
11111 +#define MCI_WRPROOF_SIZE 1
11112 +#define MCI_DMAPADV_OFFSET 14
11113 +#define MCI_DMAPADV_SIZE 1
11114 +#define MCI_BLKLEN_OFFSET 16
11115 +#define MCI_BLKLEN_SIZE 16
11116 +
11117 +/* Bitfields in DTOR */
11118 +#define MCI_DTOCYC_OFFSET 0
11119 +#define MCI_DTOCYC_SIZE 4
11120 +#define MCI_DTOMUL_OFFSET 4
11121 +#define MCI_DTOMUL_SIZE 3
11122 +
11123 +/* Bitfields in SDCR */
11124 +#define MCI_SDCSEL_OFFSET 0
11125 +#define MCI_SDCSEL_SIZE 4
11126 +#define MCI_SDCBUS_OFFSET 7
11127 +#define MCI_SDCBUS_SIZE 1
11128 +
11129 +/* Bitfields in ARGR */
11130 +#define MCI_ARG_OFFSET 0
11131 +#define MCI_ARG_SIZE 32
11132 +
11133 +/* Bitfields in CMDR */
11134 +#define MCI_CMDNB_OFFSET 0
11135 +#define MCI_CMDNB_SIZE 6
11136 +#define MCI_RSPTYP_OFFSET 6
11137 +#define MCI_RSPTYP_SIZE 2
11138 +#define MCI_SPCMD_OFFSET 8
11139 +#define MCI_SPCMD_SIZE 3
11140 +#define MCI_OPDCMD_OFFSET 11
11141 +#define MCI_OPDCMD_SIZE 1
11142 +#define MCI_MAXLAT_OFFSET 12
11143 +#define MCI_MAXLAT_SIZE 1
11144 +#define MCI_TRCMD_OFFSET 16
11145 +#define MCI_TRCMD_SIZE 2
11146 +#define MCI_TRDIR_OFFSET 18
11147 +#define MCI_TRDIR_SIZE 1
11148 +#define MCI_TRTYP_OFFSET 19
11149 +#define MCI_TRTYP_SIZE 2
11150 +
11151 +/* Bitfields in BLKR */
11152 +#define MCI_BCNT_OFFSET 0
11153 +#define MCI_BCNT_SIZE 16
11154 +
11155 +/* Bitfields in RSPRn */
11156 +#define MCI_RSP_OFFSET 0
11157 +#define MCI_RSP_SIZE 32
11158 +
11159 +/* Bitfields in SR/IER/IDR/IMR */
11160 +#define MCI_CMDRDY_OFFSET 0
11161 +#define MCI_CMDRDY_SIZE 1
11162 +#define MCI_RXRDY_OFFSET 1
11163 +#define MCI_RXRDY_SIZE 1
11164 +#define MCI_TXRDY_OFFSET 2
11165 +#define MCI_TXRDY_SIZE 1
11166 +#define MCI_BLKE_OFFSET 3
11167 +#define MCI_BLKE_SIZE 1
11168 +#define MCI_DTIP_OFFSET 4
11169 +#define MCI_DTIP_SIZE 1
11170 +#define MCI_NOTBUSY_OFFSET 5
11171 +#define MCI_NOTBUSY_SIZE 1
11172 +#define MCI_ENDRX_OFFSET 6
11173 +#define MCI_ENDRX_SIZE 1
11174 +#define MCI_ENDTX_OFFSET 7
11175 +#define MCI_ENDTX_SIZE 1
11176 +#define MCI_RXBUFF_OFFSET 14
11177 +#define MCI_RXBUFF_SIZE 1
11178 +#define MCI_TXBUFE_OFFSET 15
11179 +#define MCI_TXBUFE_SIZE 1
11180 +#define MCI_RINDE_OFFSET 16
11181 +#define MCI_RINDE_SIZE 1
11182 +#define MCI_RDIRE_OFFSET 17
11183 +#define MCI_RDIRE_SIZE 1
11184 +#define MCI_RCRCE_OFFSET 18
11185 +#define MCI_RCRCE_SIZE 1
11186 +#define MCI_RENDE_OFFSET 19
11187 +#define MCI_RENDE_SIZE 1
11188 +#define MCI_RTOE_OFFSET 20
11189 +#define MCI_RTOE_SIZE 1
11190 +#define MCI_DCRCE_OFFSET 21
11191 +#define MCI_DCRCE_SIZE 1
11192 +#define MCI_DTOE_OFFSET 22
11193 +#define MCI_DTOE_SIZE 1
11194 +#define MCI_OVRE_OFFSET 30
11195 +#define MCI_OVRE_SIZE 1
11196 +#define MCI_UNRE_OFFSET 31
11197 +#define MCI_UNRE_SIZE 1
11198 +
11199 +/* Constants for DTOMUL */
11200 +#define MCI_DTOMUL_1_CYCLE 0
11201 +#define MCI_DTOMUL_16_CYCLES 1
11202 +#define MCI_DTOMUL_128_CYCLES 2
11203 +#define MCI_DTOMUL_256_CYCLES 3
11204 +#define MCI_DTOMUL_1024_CYCLES 4
11205 +#define MCI_DTOMUL_4096_CYCLES 5
11206 +#define MCI_DTOMUL_65536_CYCLES 6
11207 +#define MCI_DTOMUL_1048576_CYCLES 7
11208 +
11209 +/* Constants for RSPTYP */
11210 +#define MCI_RSPTYP_NO_RESP 0
11211 +#define MCI_RSPTYP_48_BIT 1
11212 +#define MCI_RSPTYP_136_BIT 2
11213 +
11214 +/* Constants for SPCMD */
11215 +#define MCI_SPCMD_NO_SPEC_CMD 0
11216 +#define MCI_SPCMD_INIT_CMD 1
11217 +#define MCI_SPCMD_SYNC_CMD 2
11218 +#define MCI_SPCMD_INT_CMD 4
11219 +#define MCI_SPCMD_INT_RESP 5
11220 +
11221 +/* Constants for TRCMD */
11222 +#define MCI_TRCMD_NO_TRANS 0
11223 +#define MCI_TRCMD_START_TRANS 1
11224 +#define MCI_TRCMD_STOP_TRANS 2
11225 +
11226 +/* Constants for TRTYP */
11227 +#define MCI_TRTYP_BLOCK 0
11228 +#define MCI_TRTYP_MULTI_BLOCK 1
11229 +#define MCI_TRTYP_STREAM 2
11230 +
11231 +/* Bit manipulation macros */
11232 +#define MCI_BIT(name) \
11233 + (1 << MCI_##name##_OFFSET)
11234 +#define MCI_BF(name,value) \
11235 + (((value) & ((1 << MCI_##name##_SIZE) - 1)) \
11236 + << MCI_##name##_OFFSET)
11237 +#define MCI_BFEXT(name,value) \
11238 + (((value) >> MCI_##name##_OFFSET) \
11239 + & ((1 << MCI_##name##_SIZE) - 1))
11240 +#define MCI_BFINS(name,value,old) \
11241 + (((old) & ~(((1 << MCI_##name##_SIZE) - 1) \
11242 + << MCI_##name##_OFFSET)) \
11243 + | MCI_BF(name,value))
11244 +
11245 +/* Register access macros */
11246 +#define mci_readl(port,reg) \
11247 + __raw_readl((port)->regs + MCI_##reg)
11248 +#define mci_writel(port,reg,value) \
11249 + __raw_writel((value), (port)->regs + MCI_##reg)
11250 +
11251 +#endif /* __DRIVERS_MMC_ATMEL_MCI_H__ */
11252 diff --exclude=.git -urN linux-2.6.25.6/drivers/mmc/host/Kconfig avr32-2.6/drivers/mmc/host/Kconfig
11253 --- linux-2.6.25.6/drivers/mmc/host/Kconfig 2008-06-09 20:27:19.000000000 +0200
11254 +++ avr32-2.6/drivers/mmc/host/Kconfig 2008-06-12 15:09:41.083816184 +0200
11255 @@ -91,6 +91,16 @@
11256
11257 If unsure, say N.
11258
11259 +config MMC_ATMELMCI
11260 + tristate "Atmel Multimedia Card Interface support"
11261 + depends on AVR32 && MMC
11262 + help
11263 + This selects the Atmel Multimedia Card Interface. If you have
11264 + a AT91 (ARM) or AT32 (AVR32) platform with a Multimedia Card
11265 + slot, say Y or M here.
11266 +
11267 + If unsure, say N.
11268 +
11269 config MMC_IMX
11270 tristate "Motorola i.MX Multimedia Card Interface support"
11271 depends on ARCH_IMX
11272 diff --exclude=.git -urN linux-2.6.25.6/drivers/mmc/host/Makefile avr32-2.6/drivers/mmc/host/Makefile
11273 --- linux-2.6.25.6/drivers/mmc/host/Makefile 2008-06-09 20:27:19.000000000 +0200
11274 +++ avr32-2.6/drivers/mmc/host/Makefile 2008-06-12 15:09:41.083816184 +0200
11275 @@ -15,6 +15,7 @@
11276 obj-$(CONFIG_MMC_AU1X) += au1xmmc.o
11277 obj-$(CONFIG_MMC_OMAP) += omap.o
11278 obj-$(CONFIG_MMC_AT91) += at91_mci.o
11279 +obj-$(CONFIG_MMC_ATMELMCI) += atmel-mci.o
11280 obj-$(CONFIG_MMC_TIFM_SD) += tifm_sd.o
11281 obj-$(CONFIG_MMC_SPI) += mmc_spi.o
11282
11283 diff --exclude=.git -urN linux-2.6.25.6/drivers/mtd/nand/at91_nand.c avr32-2.6/drivers/mtd/nand/at91_nand.c
11284 --- linux-2.6.25.6/drivers/mtd/nand/at91_nand.c 2008-06-09 20:27:19.000000000 +0200
11285 +++ avr32-2.6/drivers/mtd/nand/at91_nand.c 1970-01-01 01:00:00.000000000 +0100
11286 @@ -1,236 +0,0 @@
11287 -/*
11288 - * drivers/mtd/nand/at91_nand.c
11289 - *
11290 - * Copyright (C) 2003 Rick Bronson
11291 - *
11292 - * Derived from drivers/mtd/nand/autcpu12.c
11293 - * Copyright (c) 2001 Thomas Gleixner (gleixner@autronix.de)
11294 - *
11295 - * Derived from drivers/mtd/spia.c
11296 - * Copyright (C) 2000 Steven J. Hill (sjhill@cotw.com)
11297 - *
11298 - * This program is free software; you can redistribute it and/or modify
11299 - * it under the terms of the GNU General Public License version 2 as
11300 - * published by the Free Software Foundation.
11301 - *
11302 - */
11303 -
11304 -#include <linux/slab.h>
11305 -#include <linux/module.h>
11306 -#include <linux/platform_device.h>
11307 -#include <linux/mtd/mtd.h>
11308 -#include <linux/mtd/nand.h>
11309 -#include <linux/mtd/partitions.h>
11310 -
11311 -#include <asm/io.h>
11312 -#include <asm/sizes.h>
11313 -
11314 -#include <asm/hardware.h>
11315 -#include <asm/arch/board.h>
11316 -#include <asm/arch/gpio.h>
11317 -
11318 -struct at91_nand_host {
11319 - struct nand_chip nand_chip;
11320 - struct mtd_info mtd;
11321 - void __iomem *io_base;
11322 - struct at91_nand_data *board;
11323 -};
11324 -
11325 -/*
11326 - * Hardware specific access to control-lines
11327 - */
11328 -static void at91_nand_cmd_ctrl(struct mtd_info *mtd, int cmd, unsigned int ctrl)
11329 -{
11330 - struct nand_chip *nand_chip = mtd->priv;
11331 - struct at91_nand_host *host = nand_chip->priv;
11332 -
11333 - if (cmd == NAND_CMD_NONE)
11334 - return;
11335 -
11336 - if (ctrl & NAND_CLE)
11337 - writeb(cmd, host->io_base + (1 << host->board->cle));
11338 - else
11339 - writeb(cmd, host->io_base + (1 << host->board->ale));
11340 -}
11341 -
11342 -/*
11343 - * Read the Device Ready pin.
11344 - */
11345 -static int at91_nand_device_ready(struct mtd_info *mtd)
11346 -{
11347 - struct nand_chip *nand_chip = mtd->priv;
11348 - struct at91_nand_host *host = nand_chip->priv;
11349 -
11350 - return at91_get_gpio_value(host->board->rdy_pin);
11351 -}
11352 -
11353 -/*
11354 - * Enable NAND.
11355 - */
11356 -static void at91_nand_enable(struct at91_nand_host *host)
11357 -{
11358 - if (host->board->enable_pin)
11359 - at91_set_gpio_value(host->board->enable_pin, 0);
11360 -}
11361 -
11362 -/*
11363 - * Disable NAND.
11364 - */
11365 -static void at91_nand_disable(struct at91_nand_host *host)
11366 -{
11367 - if (host->board->enable_pin)
11368 - at91_set_gpio_value(host->board->enable_pin, 1);
11369 -}
11370 -
11371 -#ifdef CONFIG_MTD_PARTITIONS
11372 -const char *part_probes[] = { "cmdlinepart", NULL };
11373 -#endif
11374 -
11375 -/*
11376 - * Probe for the NAND device.
11377 - */
11378 -static int __init at91_nand_probe(struct platform_device *pdev)
11379 -{
11380 - struct at91_nand_host *host;
11381 - struct mtd_info *mtd;
11382 - struct nand_chip *nand_chip;
11383 - int res;
11384 -
11385 -#ifdef CONFIG_MTD_PARTITIONS
11386 - struct mtd_partition *partitions = NULL;
11387 - int num_partitions = 0;
11388 -#endif
11389 -
11390 - /* Allocate memory for the device structure (and zero it) */
11391 - host = kzalloc(sizeof(struct at91_nand_host), GFP_KERNEL);
11392 - if (!host) {
11393 - printk(KERN_ERR "at91_nand: failed to allocate device structure.\n");
11394 - return -ENOMEM;
11395 - }
11396 -
11397 - host->io_base = ioremap(pdev->resource[0].start,
11398 - pdev->resource[0].end - pdev->resource[0].start + 1);
11399 - if (host->io_base == NULL) {
11400 - printk(KERN_ERR "at91_nand: ioremap failed\n");
11401 - kfree(host);
11402 - return -EIO;
11403 - }
11404 -
11405 - mtd = &host->mtd;
11406 - nand_chip = &host->nand_chip;
11407 - host->board = pdev->dev.platform_data;
11408 -
11409 - nand_chip->priv = host; /* link the private data structures */
11410 - mtd->priv = nand_chip;
11411 - mtd->owner = THIS_MODULE;
11412 -
11413 - /* Set address of NAND IO lines */
11414 - nand_chip->IO_ADDR_R = host->io_base;
11415 - nand_chip->IO_ADDR_W = host->io_base;
11416 - nand_chip->cmd_ctrl = at91_nand_cmd_ctrl;
11417 -
11418 - if (host->board->rdy_pin)
11419 - nand_chip->dev_ready = at91_nand_device_ready;
11420 -
11421 - nand_chip->ecc.mode = NAND_ECC_SOFT; /* enable ECC */
11422 - nand_chip->chip_delay = 20; /* 20us command delay time */
11423 -
11424 - if (host->board->bus_width_16) /* 16-bit bus width */
11425 - nand_chip->options |= NAND_BUSWIDTH_16;
11426 -
11427 - platform_set_drvdata(pdev, host);
11428 - at91_nand_enable(host);
11429 -
11430 - if (host->board->det_pin) {
11431 - if (at91_get_gpio_value(host->board->det_pin)) {
11432 - printk ("No SmartMedia card inserted.\n");
11433 - res = ENXIO;
11434 - goto out;
11435 - }
11436 - }
11437 -
11438 - /* Scan to find existance of the device */
11439 - if (nand_scan(mtd, 1)) {
11440 - res = -ENXIO;
11441 - goto out;
11442 - }
11443 -
11444 -#ifdef CONFIG_MTD_PARTITIONS
11445 -#ifdef CONFIG_MTD_CMDLINE_PARTS
11446 - mtd->name = "at91_nand";
11447 - num_partitions = parse_mtd_partitions(mtd, part_probes,
11448 - &partitions, 0);
11449 -#endif
11450 - if (num_partitions <= 0 && host->board->partition_info)
11451 - partitions = host->board->partition_info(mtd->size,
11452 - &num_partitions);
11453 -
11454 - if ((!partitions) || (num_partitions == 0)) {
11455 - printk(KERN_ERR "at91_nand: No parititions defined, or unsupported device.\n");
11456 - res = ENXIO;
11457 - goto release;
11458 - }
11459 -
11460 - res = add_mtd_partitions(mtd, partitions, num_partitions);
11461 -#else
11462 - res = add_mtd_device(mtd);
11463 -#endif
11464 -
11465 - if (!res)
11466 - return res;
11467 -
11468 -release:
11469 - nand_release(mtd);
11470 -out:
11471 - at91_nand_disable(host);
11472 - platform_set_drvdata(pdev, NULL);
11473 - iounmap(host->io_base);
11474 - kfree(host);
11475 - return res;
11476 -}
11477 -
11478 -/*
11479 - * Remove a NAND device.
11480 - */
11481 -static int __devexit at91_nand_remove(struct platform_device *pdev)
11482 -{
11483 - struct at91_nand_host *host = platform_get_drvdata(pdev);
11484 - struct mtd_info *mtd = &host->mtd;
11485 -
11486 - nand_release(mtd);
11487 -
11488 - at91_nand_disable(host);
11489 -
11490 - iounmap(host->io_base);
11491 - kfree(host);
11492 -
11493 - return 0;
11494 -}
11495 -
11496 -static struct platform_driver at91_nand_driver = {
11497 - .probe = at91_nand_probe,
11498 - .remove = at91_nand_remove,
11499 - .driver = {
11500 - .name = "at91_nand",
11501 - .owner = THIS_MODULE,
11502 - },
11503 -};
11504 -
11505 -static int __init at91_nand_init(void)
11506 -{
11507 - return platform_driver_register(&at91_nand_driver);
11508 -}
11509 -
11510 -
11511 -static void __exit at91_nand_exit(void)
11512 -{
11513 - platform_driver_unregister(&at91_nand_driver);
11514 -}
11515 -
11516 -
11517 -module_init(at91_nand_init);
11518 -module_exit(at91_nand_exit);
11519 -
11520 -MODULE_LICENSE("GPL");
11521 -MODULE_AUTHOR("Rick Bronson");
11522 -MODULE_DESCRIPTION("NAND/SmartMedia driver for AT91RM9200");
11523 diff --exclude=.git -urN linux-2.6.25.6/drivers/mtd/nand/atmel_nand.c avr32-2.6/drivers/mtd/nand/atmel_nand.c
11524 --- linux-2.6.25.6/drivers/mtd/nand/atmel_nand.c 1970-01-01 01:00:00.000000000 +0100
11525 +++ avr32-2.6/drivers/mtd/nand/atmel_nand.c 2008-06-12 15:09:41.107815889 +0200
11526 @@ -0,0 +1,650 @@
11527 +/*
11528 + * Copyright (C) 2003 Rick Bronson
11529 + *
11530 + * Derived from drivers/mtd/nand/autcpu12.c
11531 + * Copyright (c) 2001 Thomas Gleixner (gleixner@autronix.de)
11532 + *
11533 + * Derived from drivers/mtd/spia.c
11534 + * Copyright (C) 2000 Steven J. Hill (sjhill@cotw.com)
11535 + *
11536 + *
11537 + * Add Hardware ECC support for AT91SAM9260 / AT91SAM9263
11538 + * Richard Genoud (richard.genoud@gmail.com), Adeneo Copyright (C) 2007
11539 + *
11540 + * Derived from Das U-Boot source code
11541 + * (u-boot-1.1.5/board/atmel/at91sam9263ek/nand.c)
11542 + * (C) Copyright 2006 ATMEL Rousset, Lacressonniere Nicolas
11543 + *
11544 + *
11545 + * This program is free software; you can redistribute it and/or modify
11546 + * it under the terms of the GNU General Public License version 2 as
11547 + * published by the Free Software Foundation.
11548 + *
11549 + */
11550 +
11551 +#include <linux/slab.h>
11552 +#include <linux/module.h>
11553 +#include <linux/platform_device.h>
11554 +#include <linux/mtd/mtd.h>
11555 +#include <linux/mtd/nand.h>
11556 +#include <linux/mtd/partitions.h>
11557 +
11558 +#include <linux/gpio.h>
11559 +#include <linux/io.h>
11560 +
11561 +#include <asm/arch/board.h>
11562 +#include <asm/arch/cpu.h>
11563 +
11564 +#ifdef CONFIG_MTD_NAND_ATMEL_ECC_HW
11565 +#define hard_ecc 1
11566 +#else
11567 +#define hard_ecc 0
11568 +#endif
11569 +
11570 +#ifdef CONFIG_MTD_NAND_ATMEL_ECC_NONE
11571 +#define no_ecc 1
11572 +#else
11573 +#define no_ecc 0
11574 +#endif
11575 +
11576 +/* Register access macros */
11577 +#define ecc_readl(add, reg) \
11578 + __raw_readl(add + ATMEL_ECC_##reg)
11579 +#define ecc_writel(add, reg, value) \
11580 + __raw_writel((value), add + ATMEL_ECC_##reg)
11581 +
11582 +#include "atmel_nand_ecc.h" /* Hardware ECC registers */
11583 +
11584 +/* oob layout for large page size
11585 + * bad block info is on bytes 0 and 1
11586 + * the bytes have to be consecutives to avoid
11587 + * several NAND_CMD_RNDOUT during read
11588 + */
11589 +static struct nand_ecclayout atmel_oobinfo_large = {
11590 + .eccbytes = 4,
11591 + .eccpos = {60, 61, 62, 63},
11592 + .oobfree = {
11593 + {2, 58}
11594 + },
11595 +};
11596 +
11597 +/* oob layout for small page size
11598 + * bad block info is on bytes 4 and 5
11599 + * the bytes have to be consecutives to avoid
11600 + * several NAND_CMD_RNDOUT during read
11601 + */
11602 +static struct nand_ecclayout atmel_oobinfo_small = {
11603 + .eccbytes = 4,
11604 + .eccpos = {0, 1, 2, 3},
11605 + .oobfree = {
11606 + {6, 10}
11607 + },
11608 +};
11609 +
11610 +struct atmel_nand_host {
11611 + struct nand_chip nand_chip;
11612 + struct mtd_info mtd;
11613 + void __iomem *io_base;
11614 + struct atmel_nand_data *board;
11615 + struct device *dev;
11616 + void __iomem *ecc;
11617 +};
11618 +
11619 +/*
11620 + * Enable NAND.
11621 + */
11622 +static void atmel_nand_enable(struct atmel_nand_host *host)
11623 +{
11624 + if (host->board->enable_pin)
11625 + gpio_set_value(host->board->enable_pin, 0);
11626 +}
11627 +
11628 +/*
11629 + * Disable NAND.
11630 + */
11631 +static void atmel_nand_disable(struct atmel_nand_host *host)
11632 +{
11633 + if (host->board->enable_pin)
11634 + gpio_set_value(host->board->enable_pin, 1);
11635 +}
11636 +
11637 +/*
11638 + * Hardware specific access to control-lines
11639 + */
11640 +static void atmel_nand_cmd_ctrl(struct mtd_info *mtd, int cmd, unsigned int ctrl)
11641 +{
11642 + struct nand_chip *nand_chip = mtd->priv;
11643 + struct atmel_nand_host *host = nand_chip->priv;
11644 +
11645 + if (ctrl & NAND_CTRL_CHANGE) {
11646 + if (ctrl & NAND_NCE)
11647 + atmel_nand_enable(host);
11648 + else
11649 + atmel_nand_disable(host);
11650 + }
11651 + if (cmd == NAND_CMD_NONE)
11652 + return;
11653 +
11654 + if (ctrl & NAND_CLE)
11655 + writeb(cmd, host->io_base + (1 << host->board->cle));
11656 + else
11657 + writeb(cmd, host->io_base + (1 << host->board->ale));
11658 +}
11659 +
11660 +/*
11661 + * Read the Device Ready pin.
11662 + */
11663 +static int atmel_nand_device_ready(struct mtd_info *mtd)
11664 +{
11665 + struct nand_chip *nand_chip = mtd->priv;
11666 + struct atmel_nand_host *host = nand_chip->priv;
11667 +
11668 + return gpio_get_value(host->board->rdy_pin);
11669 +}
11670 +
11671 +/*
11672 + * Minimal-overhead PIO for data access.
11673 + */
11674 +static void atmel_read_buf(struct mtd_info *mtd, u8 *buf, int len)
11675 +{
11676 + struct nand_chip *nand_chip = mtd->priv;
11677 +
11678 + __raw_readsb(nand_chip->IO_ADDR_R, buf, len);
11679 +}
11680 +
11681 +static void atmel_read_buf16(struct mtd_info *mtd, u8 *buf, int len)
11682 +{
11683 + struct nand_chip *nand_chip = mtd->priv;
11684 +
11685 + __raw_readsw(nand_chip->IO_ADDR_R, buf, len / 2);
11686 +}
11687 +
11688 +static void atmel_write_buf(struct mtd_info *mtd, const u8 *buf, int len)
11689 +{
11690 + struct nand_chip *nand_chip = mtd->priv;
11691 +
11692 + __raw_writesb(nand_chip->IO_ADDR_W, buf, len);
11693 +}
11694 +
11695 +static void atmel_write_buf16(struct mtd_info *mtd, const u8 *buf, int len)
11696 +{
11697 + struct nand_chip *nand_chip = mtd->priv;
11698 +
11699 + __raw_writesw(nand_chip->IO_ADDR_W, buf, len / 2);
11700 +}
11701 +
11702 +/*
11703 + * write oob for small pages
11704 + */
11705 +static int atmel_nand_write_oob_512(struct mtd_info *mtd,
11706 + struct nand_chip *chip, int page)
11707 +{
11708 + int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
11709 + int eccsize = chip->ecc.size, length = mtd->oobsize;
11710 + int len, pos, status = 0;
11711 + const uint8_t *bufpoi = chip->oob_poi;
11712 +
11713 + pos = eccsize + chunk;
11714 +
11715 + chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
11716 + len = min_t(int, length, chunk);
11717 + chip->write_buf(mtd, bufpoi, len);
11718 + bufpoi += len;
11719 + length -= len;
11720 + if (length > 0)
11721 + chip->write_buf(mtd, bufpoi, length);
11722 +
11723 + chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
11724 + status = chip->waitfunc(mtd, chip);
11725 +
11726 + return status & NAND_STATUS_FAIL ? -EIO : 0;
11727 +
11728 +}
11729 +
11730 +/*
11731 + * read oob for small pages
11732 + */
11733 +static int atmel_nand_read_oob_512(struct mtd_info *mtd,
11734 + struct nand_chip *chip, int page, int sndcmd)
11735 +{
11736 + if (sndcmd) {
11737 + chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
11738 + sndcmd = 0;
11739 + }
11740 + chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
11741 + return sndcmd;
11742 +}
11743 +
11744 +/*
11745 + * Calculate HW ECC
11746 + *
11747 + * function called after a write
11748 + *
11749 + * mtd: MTD block structure
11750 + * dat: raw data (unused)
11751 + * ecc_code: buffer for ECC
11752 + */
11753 +static int atmel_nand_calculate(struct mtd_info *mtd,
11754 + const u_char *dat, unsigned char *ecc_code)
11755 +{
11756 + struct nand_chip *nand_chip = mtd->priv;
11757 + struct atmel_nand_host *host = nand_chip->priv;
11758 + uint32_t *eccpos = nand_chip->ecc.layout->eccpos;
11759 + unsigned int ecc_value;
11760 +
11761 + /* get the first 2 ECC bytes */
11762 + ecc_value = ecc_readl(host->ecc, PR);
11763 +
11764 + ecc_code[eccpos[0]] = ecc_value & 0xFF;
11765 + ecc_code[eccpos[1]] = (ecc_value >> 8) & 0xFF;
11766 +
11767 + /* get the last 2 ECC bytes */
11768 + ecc_value = ecc_readl(host->ecc, NPR) & ATMEL_ECC_NPARITY;
11769 +
11770 + ecc_code[eccpos[2]] = ecc_value & 0xFF;
11771 + ecc_code[eccpos[3]] = (ecc_value >> 8) & 0xFF;
11772 +
11773 + return 0;
11774 +}
11775 +
11776 +/*
11777 + * HW ECC read page function
11778 + *
11779 + * mtd: mtd info structure
11780 + * chip: nand chip info structure
11781 + * buf: buffer to store read data
11782 + */
11783 +static int atmel_nand_read_page(struct mtd_info *mtd,
11784 + struct nand_chip *chip, uint8_t *buf)
11785 +{
11786 + int eccsize = chip->ecc.size;
11787 + int eccbytes = chip->ecc.bytes;
11788 + uint32_t *eccpos = chip->ecc.layout->eccpos;
11789 + uint8_t *p = buf;
11790 + uint8_t *oob = chip->oob_poi;
11791 + uint8_t *ecc_pos;
11792 + int stat;
11793 +
11794 + /*
11795 + * Errata: ALE is incorrectly wired up to the ECC controller
11796 + * on the AP7000, so it will include the address cycles in the
11797 + * ECC calculation.
11798 + *
11799 + * Workaround: Reset the parity registers before reading the
11800 + * actual data.
11801 + */
11802 + if (cpu_is_at32ap7000()) {
11803 + struct atmel_nand_host *host = chip->priv;
11804 + ecc_writel(host->ecc, CR, ATMEL_ECC_RST);
11805 + }
11806 +
11807 + /* read the page */
11808 + chip->read_buf(mtd, p, eccsize);
11809 +
11810 + /* move to ECC position if needed */
11811 + if (eccpos[0] != 0) {
11812 + /* This only works on large pages
11813 + * because the ECC controller waits for
11814 + * NAND_CMD_RNDOUTSTART after the
11815 + * NAND_CMD_RNDOUT.
11816 + * anyway, for small pages, the eccpos[0] == 0
11817 + */
11818 + chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
11819 + mtd->writesize + eccpos[0], -1);
11820 + }
11821 +
11822 + /* the ECC controller needs to read the ECC just after the data */
11823 + ecc_pos = oob + eccpos[0];
11824 + chip->read_buf(mtd, ecc_pos, eccbytes);
11825 +
11826 + /* check if there's an error */
11827 + stat = chip->ecc.correct(mtd, p, oob, NULL);
11828 +
11829 + if (stat < 0)
11830 + mtd->ecc_stats.failed++;
11831 + else
11832 + mtd->ecc_stats.corrected += stat;
11833 +
11834 + /* get back to oob start (end of page) */
11835 + chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
11836 +
11837 + /* read the oob */
11838 + chip->read_buf(mtd, oob, mtd->oobsize);
11839 +
11840 + return 0;
11841 +}
11842 +
11843 +/*
11844 + * HW ECC Correction
11845 + *
11846 + * function called after a read
11847 + *
11848 + * mtd: MTD block structure
11849 + * dat: raw data read from the chip
11850 + * read_ecc: ECC from the chip (unused)
11851 + * isnull: unused
11852 + *
11853 + * Detect and correct a 1 bit error for a page
11854 + */
11855 +static int atmel_nand_correct(struct mtd_info *mtd, u_char *dat,
11856 + u_char *read_ecc, u_char *isnull)
11857 +{
11858 + struct nand_chip *nand_chip = mtd->priv;
11859 + struct atmel_nand_host *host = nand_chip->priv;
11860 + unsigned int ecc_status;
11861 + unsigned int ecc_word, ecc_bit;
11862 +
11863 + /* get the status from the Status Register */
11864 + ecc_status = ecc_readl(host->ecc, SR);
11865 +
11866 + /* if there's no error */
11867 + if (likely(!(ecc_status & ATMEL_ECC_RECERR)))
11868 + return 0;
11869 +
11870 + /* get error bit offset (4 bits) */
11871 + ecc_bit = ecc_readl(host->ecc, PR) & ATMEL_ECC_BITADDR;
11872 + /* get word address (12 bits) */
11873 + ecc_word = ecc_readl(host->ecc, PR) & ATMEL_ECC_WORDADDR;
11874 + ecc_word >>= 4;
11875 +
11876 + /* if there are multiple errors */
11877 + if (ecc_status & ATMEL_ECC_MULERR) {
11878 + /* check if it is a freshly erased block
11879 + * (filled with 0xff) */
11880 + if ((ecc_bit == ATMEL_ECC_BITADDR)
11881 + && (ecc_word == (ATMEL_ECC_WORDADDR >> 4))) {
11882 + /* the block has just been erased, return OK */
11883 + return 0;
11884 + }
11885 + /* it doesn't seems to be a freshly
11886 + * erased block.
11887 + * We can't correct so many errors */
11888 + dev_dbg(host->dev, "atmel_nand : multiple errors detected."
11889 + " Unable to correct.\n");
11890 + return -EIO;
11891 + }
11892 +
11893 + /* if there's a single bit error : we can correct it */
11894 + if (ecc_status & ATMEL_ECC_ECCERR) {
11895 + /* there's nothing much to do here.
11896 + * the bit error is on the ECC itself.
11897 + */
11898 + dev_dbg(host->dev, "atmel_nand : one bit error on ECC code."
11899 + " Nothing to correct\n");
11900 + return 0;
11901 + }
11902 +
11903 + dev_dbg(host->dev, "atmel_nand : one bit error on data."
11904 + " (word offset in the page :"
11905 + " 0x%x bit offset : 0x%x)\n",
11906 + ecc_word, ecc_bit);
11907 + /* correct the error */
11908 + if (nand_chip->options & NAND_BUSWIDTH_16) {
11909 + /* 16 bits words */
11910 + ((unsigned short *) dat)[ecc_word] ^= (1 << ecc_bit);
11911 + } else {
11912 + /* 8 bits words */
11913 + dat[ecc_word] ^= (1 << ecc_bit);
11914 + }
11915 + dev_dbg(host->dev, "atmel_nand : error corrected\n");
11916 + return 1;
11917 +}
11918 +
11919 +/*
11920 + * Enable HW ECC : unused on most chips
11921 + */
11922 +static void atmel_nand_hwctl(struct mtd_info *mtd, int mode)
11923 +{
11924 + if (cpu_is_at32ap7000()) {
11925 + struct nand_chip *nand_chip = mtd->priv;
11926 + struct atmel_nand_host *host = nand_chip->priv;
11927 + ecc_writel(host->ecc, CR, ATMEL_ECC_RST);
11928 + }
11929 +}
11930 +
11931 +#ifdef CONFIG_MTD_PARTITIONS
11932 +static const char *part_probes[] = { "cmdlinepart", NULL };
11933 +#endif
11934 +
11935 +/*
11936 + * Probe for the NAND device.
11937 + */
11938 +static int __init atmel_nand_probe(struct platform_device *pdev)
11939 +{
11940 + struct atmel_nand_host *host;
11941 + struct mtd_info *mtd;
11942 + struct nand_chip *nand_chip;
11943 + struct resource *regs;
11944 + struct resource *mem;
11945 + int res;
11946 +
11947 +#ifdef CONFIG_MTD_PARTITIONS
11948 + struct mtd_partition *partitions = NULL;
11949 + int num_partitions = 0;
11950 +#endif
11951 +
11952 + mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
11953 + if (!mem) {
11954 + printk(KERN_ERR "atmel_nand: can't get I/O resource mem\n");
11955 + return -ENXIO;
11956 + }
11957 +
11958 + /* Allocate memory for the device structure (and zero it) */
11959 + host = kzalloc(sizeof(struct atmel_nand_host), GFP_KERNEL);
11960 + if (!host) {
11961 + printk(KERN_ERR "atmel_nand: failed to allocate device structure.\n");
11962 + return -ENOMEM;
11963 + }
11964 +
11965 + host->io_base = ioremap(mem->start, mem->end - mem->start + 1);
11966 + if (host->io_base == NULL) {
11967 + printk(KERN_ERR "atmel_nand: ioremap failed\n");
11968 + res = -EIO;
11969 + goto err_nand_ioremap;
11970 + }
11971 +
11972 + mtd = &host->mtd;
11973 + nand_chip = &host->nand_chip;
11974 + host->board = pdev->dev.platform_data;
11975 + host->dev = &pdev->dev;
11976 +
11977 + nand_chip->priv = host; /* link the private data structures */
11978 + mtd->priv = nand_chip;
11979 + mtd->owner = THIS_MODULE;
11980 +
11981 + /* Set address of NAND IO lines */
11982 + nand_chip->IO_ADDR_R = host->io_base;
11983 + nand_chip->IO_ADDR_W = host->io_base;
11984 + nand_chip->cmd_ctrl = atmel_nand_cmd_ctrl;
11985 +
11986 + if (host->board->rdy_pin)
11987 + nand_chip->dev_ready = atmel_nand_device_ready;
11988 +
11989 + regs = platform_get_resource(pdev, IORESOURCE_MEM, 1);
11990 + if (!regs && hard_ecc) {
11991 + printk(KERN_ERR "atmel_nand: can't get I/O resource "
11992 + "regs\nFalling back on software ECC\n");
11993 + }
11994 +
11995 + nand_chip->ecc.mode = NAND_ECC_SOFT; /* enable ECC */
11996 + if (no_ecc)
11997 + nand_chip->ecc.mode = NAND_ECC_NONE;
11998 + if (hard_ecc && regs) {
11999 + host->ecc = ioremap(regs->start, regs->end - regs->start + 1);
12000 + if (host->ecc == NULL) {
12001 + printk(KERN_ERR "atmel_nand: ioremap failed\n");
12002 + res = -EIO;
12003 + goto err_ecc_ioremap;
12004 + }
12005 + nand_chip->ecc.mode = NAND_ECC_HW_SYNDROME;
12006 + nand_chip->ecc.calculate = atmel_nand_calculate;
12007 + nand_chip->ecc.correct = atmel_nand_correct;
12008 + nand_chip->ecc.hwctl = atmel_nand_hwctl;
12009 + nand_chip->ecc.read_page = atmel_nand_read_page;
12010 + nand_chip->ecc.bytes = 4;
12011 + nand_chip->ecc.prepad = 0;
12012 + nand_chip->ecc.postpad = 0;
12013 + }
12014 +
12015 + nand_chip->chip_delay = 20; /* 20us command delay time */
12016 +
12017 + if (host->board->bus_width_16) { /* 16-bit bus width */
12018 + nand_chip->options |= NAND_BUSWIDTH_16;
12019 + nand_chip->read_buf = atmel_read_buf16;
12020 + nand_chip->write_buf = atmel_write_buf16;
12021 + } else {
12022 + nand_chip->read_buf = atmel_read_buf;
12023 + nand_chip->write_buf = atmel_write_buf;
12024 + }
12025 +
12026 + platform_set_drvdata(pdev, host);
12027 + atmel_nand_enable(host);
12028 +
12029 + if (host->board->det_pin) {
12030 + if (gpio_get_value(host->board->det_pin)) {
12031 + printk("No SmartMedia card inserted.\n");
12032 + res = ENXIO;
12033 + goto err_no_card;
12034 + }
12035 + }
12036 +
12037 + /* first scan to find the device and get the page size */
12038 + if (nand_scan_ident(mtd, 1)) {
12039 + res = -ENXIO;
12040 + goto err_scan_ident;
12041 + }
12042 +
12043 + if (nand_chip->ecc.mode == NAND_ECC_HW_SYNDROME) {
12044 + /* ECC is calculated for the whole page (1 step) */
12045 + nand_chip->ecc.size = mtd->writesize;
12046 +
12047 + /* set ECC page size and oob layout */
12048 + switch (mtd->writesize) {
12049 + case 512:
12050 + nand_chip->ecc.layout = &atmel_oobinfo_small;
12051 + nand_chip->ecc.read_oob = atmel_nand_read_oob_512;
12052 + nand_chip->ecc.write_oob = atmel_nand_write_oob_512;
12053 + ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_528);
12054 + break;
12055 + case 1024:
12056 + nand_chip->ecc.layout = &atmel_oobinfo_large;
12057 + ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_1056);
12058 + break;
12059 + case 2048:
12060 + nand_chip->ecc.layout = &atmel_oobinfo_large;
12061 + ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_2112);
12062 + break;
12063 + case 4096:
12064 + nand_chip->ecc.layout = &atmel_oobinfo_large;
12065 + ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_4224);
12066 + break;
12067 + default:
12068 + /* page size not handled by HW ECC */
12069 + /* switching back to soft ECC */
12070 + nand_chip->ecc.mode = NAND_ECC_SOFT;
12071 + nand_chip->ecc.calculate = NULL;
12072 + nand_chip->ecc.correct = NULL;
12073 + nand_chip->ecc.hwctl = NULL;
12074 + nand_chip->ecc.read_page = NULL;
12075 + nand_chip->ecc.postpad = 0;
12076 + nand_chip->ecc.prepad = 0;
12077 + nand_chip->ecc.bytes = 0;
12078 + break;
12079 + }
12080 + }
12081 +
12082 + /* second phase scan */
12083 + if (nand_scan_tail(mtd)) {
12084 + res = -ENXIO;
12085 + goto err_scan_tail;
12086 + }
12087 +
12088 +#ifdef CONFIG_MTD_PARTITIONS
12089 +#ifdef CONFIG_MTD_CMDLINE_PARTS
12090 + mtd->name = "atmel_nand";
12091 + num_partitions = parse_mtd_partitions(mtd, part_probes,
12092 + &partitions, 0);
12093 +#endif
12094 + if (num_partitions <= 0 && host->board->partition_info)
12095 + partitions = host->board->partition_info(mtd->size,
12096 + &num_partitions);
12097 +
12098 + if ((!partitions) || (num_partitions == 0)) {
12099 + printk(KERN_ERR "atmel_nand: No parititions defined, or unsupported device.\n");
12100 + res = ENXIO;
12101 + goto err_no_partitions;
12102 + }
12103 +
12104 + res = add_mtd_partitions(mtd, partitions, num_partitions);
12105 +#else
12106 + res = add_mtd_device(mtd);
12107 +#endif
12108 +
12109 + if (!res)
12110 + return res;
12111 +
12112 +#ifdef CONFIG_MTD_PARTITIONS
12113 +err_no_partitions:
12114 +#endif
12115 + nand_release(mtd);
12116 +err_scan_tail:
12117 +err_scan_ident:
12118 +err_no_card:
12119 + atmel_nand_disable(host);
12120 + platform_set_drvdata(pdev, NULL);
12121 + if (host->ecc)
12122 + iounmap(host->ecc);
12123 +err_ecc_ioremap:
12124 + iounmap(host->io_base);
12125 +err_nand_ioremap:
12126 + kfree(host);
12127 + return res;
12128 +}
12129 +
12130 +/*
12131 + * Remove a NAND device.
12132 + */
12133 +static int __exit atmel_nand_remove(struct platform_device *pdev)
12134 +{
12135 + struct atmel_nand_host *host = platform_get_drvdata(pdev);
12136 + struct mtd_info *mtd = &host->mtd;
12137 +
12138 + nand_release(mtd);
12139 +
12140 + atmel_nand_disable(host);
12141 +
12142 + if (host->ecc)
12143 + iounmap(host->ecc);
12144 + iounmap(host->io_base);
12145 + kfree(host);
12146 +
12147 + return 0;
12148 +}
12149 +
12150 +static struct platform_driver atmel_nand_driver = {
12151 + .remove = __exit_p(atmel_nand_remove),
12152 + .driver = {
12153 + .name = "atmel_nand",
12154 + .owner = THIS_MODULE,
12155 + },
12156 +};
12157 +
12158 +static int __init atmel_nand_init(void)
12159 +{
12160 + return platform_driver_probe(&atmel_nand_driver, atmel_nand_probe);
12161 +}
12162 +
12163 +
12164 +static void __exit atmel_nand_exit(void)
12165 +{
12166 + platform_driver_unregister(&atmel_nand_driver);
12167 +}
12168 +
12169 +
12170 +module_init(atmel_nand_init);
12171 +module_exit(atmel_nand_exit);
12172 +
12173 +MODULE_LICENSE("GPL");
12174 +MODULE_AUTHOR("Rick Bronson");
12175 +MODULE_DESCRIPTION("NAND/SmartMedia driver for AT91 / AVR32");
12176 +MODULE_ALIAS("platform:atmel_nand");
12177 diff --exclude=.git -urN linux-2.6.25.6/drivers/mtd/nand/atmel_nand_ecc.h avr32-2.6/drivers/mtd/nand/atmel_nand_ecc.h
12178 --- linux-2.6.25.6/drivers/mtd/nand/atmel_nand_ecc.h 1970-01-01 01:00:00.000000000 +0100
12179 +++ avr32-2.6/drivers/mtd/nand/atmel_nand_ecc.h 2008-06-12 15:09:41.111815840 +0200
12180 @@ -0,0 +1,36 @@
12181 +/*
12182 + * Error Corrected Code Controller (ECC) - System peripherals regsters.
12183 + * Based on AT91SAM9260 datasheet revision B.
12184 + *
12185 + * This program is free software; you can redistribute it and/or modify it
12186 + * under the terms of the GNU General Public License as published by the
12187 + * Free Software Foundation; either version 2 of the License, or (at your
12188 + * option) any later version.
12189 + */
12190 +
12191 +#ifndef ATMEL_NAND_ECC_H
12192 +#define ATMEL_NAND_ECC_H
12193 +
12194 +#define ATMEL_ECC_CR 0x00 /* Control register */
12195 +#define ATMEL_ECC_RST (1 << 0) /* Reset parity */
12196 +
12197 +#define ATMEL_ECC_MR 0x04 /* Mode register */
12198 +#define ATMEL_ECC_PAGESIZE (3 << 0) /* Page Size */
12199 +#define ATMEL_ECC_PAGESIZE_528 (0)
12200 +#define ATMEL_ECC_PAGESIZE_1056 (1)
12201 +#define ATMEL_ECC_PAGESIZE_2112 (2)
12202 +#define ATMEL_ECC_PAGESIZE_4224 (3)
12203 +
12204 +#define ATMEL_ECC_SR 0x08 /* Status register */
12205 +#define ATMEL_ECC_RECERR (1 << 0) /* Recoverable Error */
12206 +#define ATMEL_ECC_ECCERR (1 << 1) /* ECC Single Bit Error */
12207 +#define ATMEL_ECC_MULERR (1 << 2) /* Multiple Errors */
12208 +
12209 +#define ATMEL_ECC_PR 0x0c /* Parity register */
12210 +#define ATMEL_ECC_BITADDR (0xf << 0) /* Bit Error Address */
12211 +#define ATMEL_ECC_WORDADDR (0xfff << 4) /* Word Error Address */
12212 +
12213 +#define ATMEL_ECC_NPR 0x10 /* NParity register */
12214 +#define ATMEL_ECC_NPARITY (0xffff << 0) /* NParity */
12215 +
12216 +#endif
12217 diff --exclude=.git -urN linux-2.6.25.6/drivers/mtd/nand/bf5xx_nand.c avr32-2.6/drivers/mtd/nand/bf5xx_nand.c
12218 --- linux-2.6.25.6/drivers/mtd/nand/bf5xx_nand.c 2008-06-09 20:27:19.000000000 +0200
12219 +++ avr32-2.6/drivers/mtd/nand/bf5xx_nand.c 2008-06-12 15:09:41.111815840 +0200
12220 @@ -803,3 +803,4 @@
12221 MODULE_LICENSE("GPL");
12222 MODULE_AUTHOR(DRV_AUTHOR);
12223 MODULE_DESCRIPTION(DRV_DESC);
12224 +MODULE_ALIAS("platform:" DRV_NAME);
12225 diff --exclude=.git -urN linux-2.6.25.6/drivers/mtd/nand/Kconfig avr32-2.6/drivers/mtd/nand/Kconfig
12226 --- linux-2.6.25.6/drivers/mtd/nand/Kconfig 2008-06-09 20:27:19.000000000 +0200
12227 +++ avr32-2.6/drivers/mtd/nand/Kconfig 2008-06-12 15:09:41.107815889 +0200
12228 @@ -272,12 +272,54 @@
12229
12230 If you say "m", the module will be called "cs553x_nand.ko".
12231
12232 -config MTD_NAND_AT91
12233 - bool "Support for NAND Flash / SmartMedia on AT91"
12234 - depends on ARCH_AT91
12235 +config MTD_NAND_ATMEL
12236 + bool "Support for NAND Flash / SmartMedia on AT91 and AVR32"
12237 + depends on ARCH_AT91 || AVR32
12238 help
12239 Enables support for NAND Flash / Smart Media Card interface
12240 - on Atmel AT91 processors.
12241 + on Atmel AT91 and AVR32 processors.
12242 +choice
12243 + prompt "ECC management for NAND Flash / SmartMedia on AT91 / AVR32"
12244 + depends on MTD_NAND_ATMEL
12245 +
12246 +config MTD_NAND_ATMEL_ECC_HW
12247 + bool "Hardware ECC"
12248 + depends on ARCH_AT91SAM9263 || ARCH_AT91SAM9260 || AVR32
12249 + help
12250 + Use hardware ECC instead of software ECC when the chip
12251 + supports it.
12252 +
12253 + The hardware ECC controller is capable of single bit error
12254 + correction and 2-bit random detection per page.
12255 +
12256 + NB : hardware and software ECC schemes are incompatible.
12257 + If you switch from one to another, you'll have to erase your
12258 + mtd partition.
12259 +
12260 + If unsure, say Y
12261 +
12262 +config MTD_NAND_ATMEL_ECC_SOFT
12263 + bool "Software ECC"
12264 + help
12265 + Use software ECC.
12266 +
12267 + NB : hardware and software ECC schemes are incompatible.
12268 + If you switch from one to another, you'll have to erase your
12269 + mtd partition.
12270 +
12271 +config MTD_NAND_ATMEL_ECC_NONE
12272 + bool "No ECC (testing only, DANGEROUS)"
12273 + depends on DEBUG_KERNEL
12274 + help
12275 + No ECC will be used.
12276 + It's not a good idea and it should be reserved for testing
12277 + purpose only.
12278 +
12279 + If unsure, say N
12280 +
12281 + endchoice
12282 +
12283 +endchoice
12284
12285 config MTD_NAND_CM_X270
12286 tristate "Support for NAND Flash on CM-X270 modules"
12287 diff --exclude=.git -urN linux-2.6.25.6/drivers/mtd/nand/Makefile avr32-2.6/drivers/mtd/nand/Makefile
12288 --- linux-2.6.25.6/drivers/mtd/nand/Makefile 2008-06-09 20:27:19.000000000 +0200
12289 +++ avr32-2.6/drivers/mtd/nand/Makefile 2008-06-12 15:09:41.107815889 +0200
12290 @@ -24,7 +24,7 @@
12291 obj-$(CONFIG_MTD_NAND_NANDSIM) += nandsim.o
12292 obj-$(CONFIG_MTD_NAND_CS553X) += cs553x_nand.o
12293 obj-$(CONFIG_MTD_NAND_NDFC) += ndfc.o
12294 -obj-$(CONFIG_MTD_NAND_AT91) += at91_nand.o
12295 +obj-$(CONFIG_MTD_NAND_ATMEL) += atmel_nand.o
12296 obj-$(CONFIG_MTD_NAND_CM_X270) += cmx270_nand.o
12297 obj-$(CONFIG_MTD_NAND_BASLER_EXCITE) += excite_nandflash.o
12298 obj-$(CONFIG_MTD_NAND_PLATFORM) += plat_nand.o
12299 diff --exclude=.git -urN linux-2.6.25.6/drivers/mtd/nand/ndfc.c avr32-2.6/drivers/mtd/nand/ndfc.c
12300 --- linux-2.6.25.6/drivers/mtd/nand/ndfc.c 2008-06-09 20:27:19.000000000 +0200
12301 +++ avr32-2.6/drivers/mtd/nand/ndfc.c 2008-06-12 15:03:59.579815954 +0200
12302 @@ -317,3 +317,5 @@
12303 MODULE_LICENSE("GPL");
12304 MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
12305 MODULE_DESCRIPTION("Platform driver for NDFC");
12306 +MODULE_ALIAS("platform:ndfc-chip");
12307 +MODULE_ALIAS("platform:ndfc-nand");
12308 diff --exclude=.git -urN linux-2.6.25.6/drivers/mtd/nand/orion_nand.c avr32-2.6/drivers/mtd/nand/orion_nand.c
12309 --- linux-2.6.25.6/drivers/mtd/nand/orion_nand.c 2008-06-09 20:27:19.000000000 +0200
12310 +++ avr32-2.6/drivers/mtd/nand/orion_nand.c 2008-06-12 15:09:41.115816070 +0200
12311 @@ -169,3 +169,4 @@
12312 MODULE_LICENSE("GPL");
12313 MODULE_AUTHOR("Tzachi Perelstein");
12314 MODULE_DESCRIPTION("NAND glue for Orion platforms");
12315 +MODULE_ALIAS("platform:orion_nand");
12316 diff --exclude=.git -urN linux-2.6.25.6/drivers/mtd/nand/plat_nand.c avr32-2.6/drivers/mtd/nand/plat_nand.c
12317 --- linux-2.6.25.6/drivers/mtd/nand/plat_nand.c 2008-06-09 20:27:19.000000000 +0200
12318 +++ avr32-2.6/drivers/mtd/nand/plat_nand.c 2008-06-12 15:09:41.115816070 +0200
12319 @@ -150,3 +150,4 @@
12320 MODULE_LICENSE("GPL");
12321 MODULE_AUTHOR("Vitaly Wool");
12322 MODULE_DESCRIPTION("Simple generic NAND driver");
12323 +MODULE_ALIAS("platform:gen_nand");
12324 diff --exclude=.git -urN linux-2.6.25.6/drivers/mtd/nand/s3c2410.c avr32-2.6/drivers/mtd/nand/s3c2410.c
12325 --- linux-2.6.25.6/drivers/mtd/nand/s3c2410.c 2008-06-09 20:27:19.000000000 +0200
12326 +++ avr32-2.6/drivers/mtd/nand/s3c2410.c 2008-06-12 15:09:41.115816070 +0200
12327 @@ -927,3 +927,6 @@
12328 MODULE_LICENSE("GPL");
12329 MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
12330 MODULE_DESCRIPTION("S3C24XX MTD NAND driver");
12331 +MODULE_ALIAS("platform:s3c2410-nand");
12332 +MODULE_ALIAS("platform:s3c2412-nand");
12333 +MODULE_ALIAS("platform:s3c2440-nand");
12334 diff --exclude=.git -urN linux-2.6.25.6/drivers/mtd/ubi/build.c avr32-2.6/drivers/mtd/ubi/build.c
12335 --- linux-2.6.25.6/drivers/mtd/ubi/build.c 2008-06-09 20:27:19.000000000 +0200
12336 +++ avr32-2.6/drivers/mtd/ubi/build.c 2008-06-12 15:09:41.119815462 +0200
12337 @@ -355,15 +355,34 @@
12338 }
12339
12340 /**
12341 + * free_user_volumes - free all user volumes.
12342 + * @ubi: UBI device description object
12343 + *
12344 + * Normally the volumes are freed at the release function of the volume device
12345 + * objects. However, on error paths the volumes have to be freed before the
12346 + * device objects have been initialized.
12347 + */
12348 +static void free_user_volumes(struct ubi_device *ubi)
12349 +{
12350 + int i;
12351 +
12352 + for (i = 0; i < ubi->vtbl_slots; i++)
12353 + if (ubi->volumes[i]) {
12354 + kfree(ubi->volumes[i]->eba_tbl);
12355 + kfree(ubi->volumes[i]);
12356 + }
12357 +}
12358 +
12359 +/**
12360 * uif_init - initialize user interfaces for an UBI device.
12361 * @ubi: UBI device description object
12362 *
12363 * This function returns zero in case of success and a negative error code in
12364 - * case of failure.
12365 + * case of failure. Note, this function destroys all volumes if it failes.
12366 */
12367 static int uif_init(struct ubi_device *ubi)
12368 {
12369 - int i, err;
12370 + int i, err, do_free = 0;
12371 dev_t dev;
12372
12373 sprintf(ubi->ubi_name, UBI_NAME_STR "%d", ubi->ubi_num);
12374 @@ -410,10 +429,13 @@
12375
12376 out_volumes:
12377 kill_volumes(ubi);
12378 + do_free = 0;
12379 out_sysfs:
12380 ubi_sysfs_close(ubi);
12381 cdev_del(&ubi->cdev);
12382 out_unreg:
12383 + if (do_free)
12384 + free_user_volumes(ubi);
12385 unregister_chrdev_region(ubi->cdev.dev, ubi->vtbl_slots + 1);
12386 ubi_err("cannot initialize UBI %s, error %d", ubi->ubi_name, err);
12387 return err;
12388 @@ -422,6 +444,10 @@
12389 /**
12390 * uif_close - close user interfaces for an UBI device.
12391 * @ubi: UBI device description object
12392 + *
12393 + * Note, since this function un-registers UBI volume device objects (@vol->dev),
12394 + * the memory allocated voe the volumes is freed as well (in the release
12395 + * function).
12396 */
12397 static void uif_close(struct ubi_device *ubi)
12398 {
12399 @@ -432,6 +458,21 @@
12400 }
12401
12402 /**
12403 + * free_internal_volumes - free internal volumes.
12404 + * @ubi: UBI device description object
12405 + */
12406 +static void free_internal_volumes(struct ubi_device *ubi)
12407 +{
12408 + int i;
12409 +
12410 + for (i = ubi->vtbl_slots;
12411 + i < ubi->vtbl_slots + UBI_INT_VOL_COUNT; i++) {
12412 + kfree(ubi->volumes[i]->eba_tbl);
12413 + kfree(ubi->volumes[i]);
12414 + }
12415 +}
12416 +
12417 +/**
12418 * attach_by_scanning - attach an MTD device using scanning method.
12419 * @ubi: UBI device descriptor
12420 *
12421 @@ -475,6 +516,7 @@
12422 out_wl:
12423 ubi_wl_close(ubi);
12424 out_vtbl:
12425 + free_internal_volumes(ubi);
12426 vfree(ubi->vtbl);
12427 out_si:
12428 ubi_scan_destroy_si(si);
12429 @@ -530,7 +572,11 @@
12430 ubi->min_io_size = ubi->mtd->writesize;
12431 ubi->hdrs_min_io_size = ubi->mtd->writesize >> ubi->mtd->subpage_sft;
12432
12433 - /* Make sure minimal I/O unit is power of 2 */
12434 + /*
12435 + * Make sure minimal I/O unit is power of 2. Note, there is no
12436 + * fundamental reason for this assumption. It is just an optimization
12437 + * which allows us to avoid costly division operations.
12438 + */
12439 if (!is_power_of_2(ubi->min_io_size)) {
12440 ubi_err("min. I/O unit (%d) is not power of 2",
12441 ubi->min_io_size);
12442 @@ -581,7 +627,7 @@
12443 if (ubi->vid_hdr_offset < UBI_EC_HDR_SIZE ||
12444 ubi->leb_start < ubi->vid_hdr_offset + UBI_VID_HDR_SIZE ||
12445 ubi->leb_start > ubi->peb_size - UBI_VID_HDR_SIZE ||
12446 - ubi->leb_start % ubi->min_io_size) {
12447 + ubi->leb_start & (ubi->min_io_size - 1)) {
12448 ubi_err("bad VID header (%d) or data offsets (%d)",
12449 ubi->vid_hdr_offset, ubi->leb_start);
12450 return -EINVAL;
12451 @@ -606,8 +652,16 @@
12452 ubi->ro_mode = 1;
12453 }
12454
12455 - dbg_msg("leb_size %d", ubi->leb_size);
12456 - dbg_msg("ro_mode %d", ubi->ro_mode);
12457 + ubi_msg("physical eraseblock size: %d bytes (%d KiB)",
12458 + ubi->peb_size, ubi->peb_size >> 10);
12459 + ubi_msg("logical eraseblock size: %d bytes", ubi->leb_size);
12460 + ubi_msg("smallest flash I/O unit: %d", ubi->min_io_size);
12461 + if (ubi->hdrs_min_io_size != ubi->min_io_size)
12462 + ubi_msg("sub-page size: %d",
12463 + ubi->hdrs_min_io_size);
12464 + ubi_msg("VID header offset: %d (aligned %d)",
12465 + ubi->vid_hdr_offset, ubi->vid_hdr_aloffset);
12466 + ubi_msg("data offset: %d", ubi->leb_start);
12467
12468 /*
12469 * Note, ideally, we have to initialize ubi->bad_peb_count here. But
12470 @@ -638,7 +692,7 @@
12471
12472 /*
12473 * Clear the auto-resize flag in the volume in-memory copy of the
12474 - * volume table, and 'ubi_resize_volume()' will propogate this change
12475 + * volume table, and 'ubi_resize_volume()' will propagate this change
12476 * to the flash.
12477 */
12478 ubi->vtbl[vol_id].flags &= ~UBI_VTBL_AUTORESIZE_FLG;
12479 @@ -647,7 +701,7 @@
12480 struct ubi_vtbl_record vtbl_rec;
12481
12482 /*
12483 - * No avalilable PEBs to re-size the volume, clear the flag on
12484 + * No available PEBs to re-size the volume, clear the flag on
12485 * flash and exit.
12486 */
12487 memcpy(&vtbl_rec, &ubi->vtbl[vol_id],
12488 @@ -680,7 +734,7 @@
12489 *
12490 * This function attaches MTD device @mtd_dev to UBI and assign @ubi_num number
12491 * to the newly created UBI device, unless @ubi_num is %UBI_DEV_NUM_AUTO, in
12492 - * which case this function finds a vacant device nubert and assings it
12493 + * which case this function finds a vacant device number and assigns it
12494 * automatically. Returns the new UBI device number in case of success and a
12495 * negative error code in case of failure.
12496 *
12497 @@ -690,7 +744,7 @@
12498 int ubi_attach_mtd_dev(struct mtd_info *mtd, int ubi_num, int vid_hdr_offset)
12499 {
12500 struct ubi_device *ubi;
12501 - int i, err;
12502 + int i, err, do_free = 1;
12503
12504 /*
12505 * Check if we already have the same MTD device attached.
12506 @@ -755,8 +809,7 @@
12507 mutex_init(&ubi->volumes_mutex);
12508 spin_lock_init(&ubi->volumes_lock);
12509
12510 - dbg_msg("attaching mtd%d to ubi%d: VID header offset %d",
12511 - mtd->index, ubi_num, vid_hdr_offset);
12512 + ubi_msg("attaching mtd%d to ubi%d", mtd->index, ubi_num);
12513
12514 err = io_init(ubi);
12515 if (err)
12516 @@ -791,7 +844,7 @@
12517
12518 err = uif_init(ubi);
12519 if (err)
12520 - goto out_detach;
12521 + goto out_nofree;
12522
12523 ubi->bgt_thread = kthread_create(ubi_thread, ubi, ubi->bgt_name);
12524 if (IS_ERR(ubi->bgt_thread)) {
12525 @@ -804,15 +857,8 @@
12526 ubi_msg("attached mtd%d to ubi%d", mtd->index, ubi_num);
12527 ubi_msg("MTD device name: \"%s\"", mtd->name);
12528 ubi_msg("MTD device size: %llu MiB", ubi->flash_size >> 20);
12529 - ubi_msg("physical eraseblock size: %d bytes (%d KiB)",
12530 - ubi->peb_size, ubi->peb_size >> 10);
12531 - ubi_msg("logical eraseblock size: %d bytes", ubi->leb_size);
12532 ubi_msg("number of good PEBs: %d", ubi->good_peb_count);
12533 ubi_msg("number of bad PEBs: %d", ubi->bad_peb_count);
12534 - ubi_msg("smallest flash I/O unit: %d", ubi->min_io_size);
12535 - ubi_msg("VID header offset: %d (aligned %d)",
12536 - ubi->vid_hdr_offset, ubi->vid_hdr_aloffset);
12537 - ubi_msg("data offset: %d", ubi->leb_start);
12538 ubi_msg("max. allowed volumes: %d", ubi->vtbl_slots);
12539 ubi_msg("wear-leveling threshold: %d", CONFIG_MTD_UBI_WL_THRESHOLD);
12540 ubi_msg("number of internal volumes: %d", UBI_INT_VOL_COUNT);
12541 @@ -835,9 +881,13 @@
12542
12543 out_uif:
12544 uif_close(ubi);
12545 +out_nofree:
12546 + do_free = 0;
12547 out_detach:
12548 - ubi_eba_close(ubi);
12549 ubi_wl_close(ubi);
12550 + if (do_free)
12551 + free_user_volumes(ubi);
12552 + free_internal_volumes(ubi);
12553 vfree(ubi->vtbl);
12554 out_free:
12555 vfree(ubi->peb_buf1);
12556 @@ -899,8 +949,8 @@
12557 kthread_stop(ubi->bgt_thread);
12558
12559 uif_close(ubi);
12560 - ubi_eba_close(ubi);
12561 ubi_wl_close(ubi);
12562 + free_internal_volumes(ubi);
12563 vfree(ubi->vtbl);
12564 put_mtd_device(ubi->mtd);
12565 vfree(ubi->peb_buf1);
12566 @@ -950,8 +1000,7 @@
12567 BUILD_BUG_ON(sizeof(struct ubi_vid_hdr) != 64);
12568
12569 if (mtd_devs > UBI_MAX_DEVICES) {
12570 - printk(KERN_ERR "UBI error: too many MTD devices, "
12571 - "maximum is %d\n", UBI_MAX_DEVICES);
12572 + ubi_err("too many MTD devices, maximum is %d", UBI_MAX_DEVICES);
12573 return -EINVAL;
12574 }
12575
12576 @@ -959,25 +1008,25 @@
12577 ubi_class = class_create(THIS_MODULE, UBI_NAME_STR);
12578 if (IS_ERR(ubi_class)) {
12579 err = PTR_ERR(ubi_class);
12580 - printk(KERN_ERR "UBI error: cannot create UBI class\n");
12581 + ubi_err("cannot create UBI class");
12582 goto out;
12583 }
12584
12585 err = class_create_file(ubi_class, &ubi_version);
12586 if (err) {
12587 - printk(KERN_ERR "UBI error: cannot create sysfs file\n");
12588 + ubi_err("cannot create sysfs file");
12589 goto out_class;
12590 }
12591
12592 err = misc_register(&ubi_ctrl_cdev);
12593 if (err) {
12594 - printk(KERN_ERR "UBI error: cannot register device\n");
12595 + ubi_err("cannot register device");
12596 goto out_version;
12597 }
12598
12599 ubi_wl_entry_slab = kmem_cache_create("ubi_wl_entry_slab",
12600 - sizeof(struct ubi_wl_entry),
12601 - 0, 0, NULL);
12602 + sizeof(struct ubi_wl_entry),
12603 + 0, 0, NULL);
12604 if (!ubi_wl_entry_slab)
12605 goto out_dev_unreg;
12606
12607 @@ -1000,8 +1049,7 @@
12608 mutex_unlock(&ubi_devices_mutex);
12609 if (err < 0) {
12610 put_mtd_device(mtd);
12611 - printk(KERN_ERR "UBI error: cannot attach mtd%d\n",
12612 - mtd->index);
12613 + ubi_err("cannot attach mtd%d", mtd->index);
12614 goto out_detach;
12615 }
12616 }
12617 @@ -1023,7 +1071,7 @@
12618 out_class:
12619 class_destroy(ubi_class);
12620 out:
12621 - printk(KERN_ERR "UBI error: cannot initialize UBI, error %d\n", err);
12622 + ubi_err("UBI error: cannot initialize UBI, error %d", err);
12623 return err;
12624 }
12625 module_init(ubi_init);
12626 diff --exclude=.git -urN linux-2.6.25.6/drivers/mtd/ubi/cdev.c avr32-2.6/drivers/mtd/ubi/cdev.c
12627 --- linux-2.6.25.6/drivers/mtd/ubi/cdev.c 2008-06-09 20:27:19.000000000 +0200
12628 +++ avr32-2.6/drivers/mtd/ubi/cdev.c 2008-06-12 15:09:41.119815462 +0200
12629 @@ -290,7 +290,7 @@
12630 off = do_div(tmp, vol->usable_leb_size);
12631 lnum = tmp;
12632
12633 - if (off % ubi->min_io_size) {
12634 + if (off & (ubi->min_io_size - 1)) {
12635 dbg_err("unaligned position");
12636 return -EINVAL;
12637 }
12638 @@ -299,7 +299,7 @@
12639 count_save = count = vol->used_bytes - *offp;
12640
12641 /* We can write only in fractions of the minimum I/O unit */
12642 - if (count % ubi->min_io_size) {
12643 + if (count & (ubi->min_io_size - 1)) {
12644 dbg_err("unaligned write length");
12645 return -EINVAL;
12646 }
12647 @@ -559,7 +559,7 @@
12648 if (req->alignment > ubi->leb_size)
12649 goto bad;
12650
12651 - n = req->alignment % ubi->min_io_size;
12652 + n = req->alignment & (ubi->min_io_size - 1);
12653 if (req->alignment != 1 && n)
12654 goto bad;
12655
12656 diff --exclude=.git -urN linux-2.6.25.6/drivers/mtd/ubi/debug.h avr32-2.6/drivers/mtd/ubi/debug.h
12657 --- linux-2.6.25.6/drivers/mtd/ubi/debug.h 2008-06-09 20:27:19.000000000 +0200
12658 +++ avr32-2.6/drivers/mtd/ubi/debug.h 2008-06-12 15:09:41.123815692 +0200
12659 @@ -99,8 +99,10 @@
12660 #ifdef CONFIG_MTD_UBI_DEBUG_MSG_BLD
12661 /* Initialization and build messages */
12662 #define dbg_bld(fmt, ...) dbg_msg(fmt, ##__VA_ARGS__)
12663 +#define UBI_IO_DEBUG 1
12664 #else
12665 #define dbg_bld(fmt, ...) ({})
12666 +#define UBI_IO_DEBUG 0
12667 #endif
12668
12669 #ifdef CONFIG_MTD_UBI_DEBUG_EMULATE_BITFLIPS
12670 diff --exclude=.git -urN linux-2.6.25.6/drivers/mtd/ubi/eba.c avr32-2.6/drivers/mtd/ubi/eba.c
12671 --- linux-2.6.25.6/drivers/mtd/ubi/eba.c 2008-06-09 20:27:19.000000000 +0200
12672 +++ avr32-2.6/drivers/mtd/ubi/eba.c 2008-06-12 15:09:41.123815692 +0200
12673 @@ -752,7 +752,7 @@
12674 /* If this is the last LEB @len may be unaligned */
12675 len = ALIGN(data_size, ubi->min_io_size);
12676 else
12677 - ubi_assert(len % ubi->min_io_size == 0);
12678 + ubi_assert(!(len & (ubi->min_io_size - 1)));
12679
12680 vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
12681 if (!vid_hdr)
12682 @@ -1233,20 +1233,3 @@
12683 }
12684 return err;
12685 }
12686 -
12687 -/**
12688 - * ubi_eba_close - close EBA unit.
12689 - * @ubi: UBI device description object
12690 - */
12691 -void ubi_eba_close(const struct ubi_device *ubi)
12692 -{
12693 - int i, num_volumes = ubi->vtbl_slots + UBI_INT_VOL_COUNT;
12694 -
12695 - dbg_eba("close EBA unit");
12696 -
12697 - for (i = 0; i < num_volumes; i++) {
12698 - if (!ubi->volumes[i])
12699 - continue;
12700 - kfree(ubi->volumes[i]->eba_tbl);
12701 - }
12702 -}
12703 diff --exclude=.git -urN linux-2.6.25.6/drivers/mtd/ubi/gluebi.c avr32-2.6/drivers/mtd/ubi/gluebi.c
12704 --- linux-2.6.25.6/drivers/mtd/ubi/gluebi.c 2008-06-09 20:27:19.000000000 +0200
12705 +++ avr32-2.6/drivers/mtd/ubi/gluebi.c 2008-06-12 15:03:59.587815297 +0200
12706 @@ -291,11 +291,12 @@
12707 /*
12708 * In case of dynamic volume, MTD device size is just volume size. In
12709 * case of a static volume the size is equivalent to the amount of data
12710 - * bytes, which is zero at this moment and will be changed after volume
12711 - * update.
12712 + * bytes.
12713 */
12714 if (vol->vol_type == UBI_DYNAMIC_VOLUME)
12715 mtd->size = vol->usable_leb_size * vol->reserved_pebs;
12716 + else
12717 + mtd->size = vol->used_bytes;
12718
12719 if (add_mtd_device(mtd)) {
12720 ubi_err("cannot not add MTD device\n");
12721 diff --exclude=.git -urN linux-2.6.25.6/drivers/mtd/ubi/io.c avr32-2.6/drivers/mtd/ubi/io.c
12722 --- linux-2.6.25.6/drivers/mtd/ubi/io.c 2008-06-09 20:27:19.000000000 +0200
12723 +++ avr32-2.6/drivers/mtd/ubi/io.c 2008-06-12 15:03:59.587815297 +0200
12724 @@ -631,6 +631,8 @@
12725
12726 dbg_io("read EC header from PEB %d", pnum);
12727 ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
12728 + if (UBI_IO_DEBUG)
12729 + verbose = 1;
12730
12731 err = ubi_io_read(ubi, ec_hdr, pnum, 0, UBI_EC_HDR_SIZE);
12732 if (err) {
12733 @@ -904,6 +906,8 @@
12734
12735 dbg_io("read VID header from PEB %d", pnum);
12736 ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
12737 + if (UBI_IO_DEBUG)
12738 + verbose = 1;
12739
12740 p = (char *)vid_hdr - ubi->vid_hdr_shift;
12741 err = ubi_io_read(ubi, p, pnum, ubi->vid_hdr_aloffset,
12742 diff --exclude=.git -urN linux-2.6.25.6/drivers/mtd/ubi/kapi.c avr32-2.6/drivers/mtd/ubi/kapi.c
12743 --- linux-2.6.25.6/drivers/mtd/ubi/kapi.c 2008-06-09 20:27:19.000000000 +0200
12744 +++ avr32-2.6/drivers/mtd/ubi/kapi.c 2008-06-12 15:09:41.123815692 +0200
12745 @@ -397,8 +397,8 @@
12746 return -EROFS;
12747
12748 if (lnum < 0 || lnum >= vol->reserved_pebs || offset < 0 || len < 0 ||
12749 - offset + len > vol->usable_leb_size || offset % ubi->min_io_size ||
12750 - len % ubi->min_io_size)
12751 + offset + len > vol->usable_leb_size ||
12752 + offset & (ubi->min_io_size - 1) || len & (ubi->min_io_size - 1))
12753 return -EINVAL;
12754
12755 if (dtype != UBI_LONGTERM && dtype != UBI_SHORTTERM &&
12756 @@ -447,7 +447,7 @@
12757 return -EROFS;
12758
12759 if (lnum < 0 || lnum >= vol->reserved_pebs || len < 0 ||
12760 - len > vol->usable_leb_size || len % ubi->min_io_size)
12761 + len > vol->usable_leb_size || len & (ubi->min_io_size - 1))
12762 return -EINVAL;
12763
12764 if (dtype != UBI_LONGTERM && dtype != UBI_SHORTTERM &&
12765 diff --exclude=.git -urN linux-2.6.25.6/drivers/mtd/ubi/Kconfig avr32-2.6/drivers/mtd/ubi/Kconfig
12766 --- linux-2.6.25.6/drivers/mtd/ubi/Kconfig 2008-06-09 20:27:19.000000000 +0200
12767 +++ avr32-2.6/drivers/mtd/ubi/Kconfig 2008-06-12 15:03:59.583815905 +0200
12768 @@ -24,8 +24,13 @@
12769 erase counter value and the lowest erase counter value of eraseblocks
12770 of UBI devices. When this threshold is exceeded, UBI starts performing
12771 wear leveling by means of moving data from eraseblock with low erase
12772 - counter to eraseblocks with high erase counter. Leave the default
12773 - value if unsure.
12774 + counter to eraseblocks with high erase counter.
12775 +
12776 + The default value should be OK for SLC NAND flashes, NOR flashes and
12777 + other flashes which have eraseblock life-cycle 100000 or more.
12778 + However, in case of MLC NAND flashes which typically have eraseblock
12779 + life-cycle less then 10000, the threshold should be lessened (e.g.,
12780 + to 128 or 256, although it does not have to be power of 2).
12781
12782 config MTD_UBI_BEB_RESERVE
12783 int "Percentage of reserved eraseblocks for bad eraseblocks handling"
12784 diff --exclude=.git -urN linux-2.6.25.6/drivers/mtd/ubi/misc.c avr32-2.6/drivers/mtd/ubi/misc.c
12785 --- linux-2.6.25.6/drivers/mtd/ubi/misc.c 2008-06-09 20:27:19.000000000 +0200
12786 +++ avr32-2.6/drivers/mtd/ubi/misc.c 2008-06-12 15:09:41.123815692 +0200
12787 @@ -37,7 +37,7 @@
12788 {
12789 int i;
12790
12791 - ubi_assert(length % ubi->min_io_size == 0);
12792 + ubi_assert(!(length & (ubi->min_io_size - 1)));
12793
12794 for (i = length - 1; i >= 0; i--)
12795 if (((const uint8_t *)buf)[i] != 0xFF)
12796 diff --exclude=.git -urN linux-2.6.25.6/drivers/mtd/ubi/scan.c avr32-2.6/drivers/mtd/ubi/scan.c
12797 --- linux-2.6.25.6/drivers/mtd/ubi/scan.c 2008-06-09 20:27:19.000000000 +0200
12798 +++ avr32-2.6/drivers/mtd/ubi/scan.c 2008-06-12 15:03:59.587815297 +0200
12799 @@ -42,6 +42,7 @@
12800
12801 #include <linux/err.h>
12802 #include <linux/crc32.h>
12803 +#include <asm/div64.h>
12804 #include "ubi.h"
12805
12806 #ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
12807 @@ -92,27 +93,6 @@
12808 }
12809
12810 /**
12811 - * commit_to_mean_value - commit intermediate results to the final mean erase
12812 - * counter value.
12813 - * @si: scanning information
12814 - *
12815 - * This is a helper function which calculates partial mean erase counter mean
12816 - * value and adds it to the resulting mean value. As we can work only in
12817 - * integer arithmetic and we want to calculate the mean value of erase counter
12818 - * accurately, we first sum erase counter values in @si->ec_sum variable and
12819 - * count these components in @si->ec_count. If this temporary @si->ec_sum is
12820 - * going to overflow, we calculate the partial mean value
12821 - * (@si->ec_sum/@si->ec_count) and add it to @si->mean_ec.
12822 - */
12823 -static void commit_to_mean_value(struct ubi_scan_info *si)
12824 -{
12825 - si->ec_sum /= si->ec_count;
12826 - if (si->ec_sum % si->ec_count >= si->ec_count / 2)
12827 - si->mean_ec += 1;
12828 - si->mean_ec += si->ec_sum;
12829 -}
12830 -
12831 -/**
12832 * validate_vid_hdr - check that volume identifier header is correct and
12833 * consistent.
12834 * @vid_hdr: the volume identifier header to check
12835 @@ -901,15 +881,8 @@
12836
12837 adjust_mean_ec:
12838 if (!ec_corr) {
12839 - if (si->ec_sum + ec < ec) {
12840 - commit_to_mean_value(si);
12841 - si->ec_sum = 0;
12842 - si->ec_count = 0;
12843 - } else {
12844 - si->ec_sum += ec;
12845 - si->ec_count += 1;
12846 - }
12847 -
12848 + si->ec_sum += ec;
12849 + si->ec_count += 1;
12850 if (ec > si->max_ec)
12851 si->max_ec = ec;
12852 if (ec < si->min_ec)
12853 @@ -965,9 +938,11 @@
12854
12855 dbg_msg("scanning is finished");
12856
12857 - /* Finish mean erase counter calculations */
12858 - if (si->ec_count)
12859 - commit_to_mean_value(si);
12860 + /* Calculate mean erase counter */
12861 + if (si->ec_count) {
12862 + do_div(si->ec_sum, si->ec_count);
12863 + si->mean_ec = si->ec_sum;
12864 + }
12865
12866 if (si->is_empty)
12867 ubi_msg("empty MTD device detected");
12868 diff --exclude=.git -urN linux-2.6.25.6/drivers/mtd/ubi/scan.h avr32-2.6/drivers/mtd/ubi/scan.h
12869 --- linux-2.6.25.6/drivers/mtd/ubi/scan.h 2008-06-09 20:27:19.000000000 +0200
12870 +++ avr32-2.6/drivers/mtd/ubi/scan.h 2008-06-12 15:03:59.587815297 +0200
12871 @@ -124,7 +124,7 @@
12872 int max_ec;
12873 unsigned long long max_sqnum;
12874 int mean_ec;
12875 - int ec_sum;
12876 + uint64_t ec_sum;
12877 int ec_count;
12878 };
12879
12880 diff --exclude=.git -urN linux-2.6.25.6/drivers/mtd/ubi/ubi.h avr32-2.6/drivers/mtd/ubi/ubi.h
12881 --- linux-2.6.25.6/drivers/mtd/ubi/ubi.h 2008-06-09 20:27:19.000000000 +0200
12882 +++ avr32-2.6/drivers/mtd/ubi/ubi.h 2008-06-12 15:09:41.123815692 +0200
12883 @@ -37,10 +37,9 @@
12884 #include <linux/string.h>
12885 #include <linux/vmalloc.h>
12886 #include <linux/mtd/mtd.h>
12887 -
12888 -#include <mtd/ubi-header.h>
12889 #include <linux/mtd/ubi.h>
12890
12891 +#include "ubi-media.h"
12892 #include "scan.h"
12893 #include "debug.h"
12894
12895 @@ -478,7 +477,6 @@
12896 int ubi_eba_copy_leb(struct ubi_device *ubi, int from, int to,
12897 struct ubi_vid_hdr *vid_hdr);
12898 int ubi_eba_init_scan(struct ubi_device *ubi, struct ubi_scan_info *si);
12899 -void ubi_eba_close(const struct ubi_device *ubi);
12900
12901 /* wl.c */
12902 int ubi_wl_get_peb(struct ubi_device *ubi, int dtype);
12903 diff --exclude=.git -urN linux-2.6.25.6/drivers/mtd/ubi/ubi-media.h avr32-2.6/drivers/mtd/ubi/ubi-media.h
12904 --- linux-2.6.25.6/drivers/mtd/ubi/ubi-media.h 1970-01-01 01:00:00.000000000 +0100
12905 +++ avr32-2.6/drivers/mtd/ubi/ubi-media.h 2008-06-12 15:03:59.587815297 +0200
12906 @@ -0,0 +1,372 @@
12907 +/*
12908 + * Copyright (c) International Business Machines Corp., 2006
12909 + *
12910 + * This program is free software; you can redistribute it and/or modify
12911 + * it under the terms of the GNU General Public License as published by
12912 + * the Free Software Foundation; either version 2 of the License, or
12913 + * (at your option) any later version.
12914 + *
12915 + * This program is distributed in the hope that it will be useful,
12916 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12917 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
12918 + * the GNU General Public License for more details.
12919 + *
12920 + * You should have received a copy of the GNU General Public License
12921 + * along with this program; if not, write to the Free Software
12922 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
12923 + *
12924 + * Authors: Artem Bityutskiy (Битюцкий Артём)
12925 + * Thomas Gleixner
12926 + * Frank Haverkamp
12927 + * Oliver Lohmann
12928 + * Andreas Arnez
12929 + */
12930 +
12931 +/*
12932 + * This file defines the layout of UBI headers and all the other UBI on-flash
12933 + * data structures.
12934 + */
12935 +
12936 +#ifndef __UBI_MEDIA_H__
12937 +#define __UBI_MEDIA_H__
12938 +
12939 +#include <asm/byteorder.h>
12940 +
12941 +/* The version of UBI images supported by this implementation */
12942 +#define UBI_VERSION 1
12943 +
12944 +/* The highest erase counter value supported by this implementation */
12945 +#define UBI_MAX_ERASECOUNTER 0x7FFFFFFF
12946 +
12947 +/* The initial CRC32 value used when calculating CRC checksums */
12948 +#define UBI_CRC32_INIT 0xFFFFFFFFU
12949 +
12950 +/* Erase counter header magic number (ASCII "UBI#") */
12951 +#define UBI_EC_HDR_MAGIC 0x55424923
12952 +/* Volume identifier header magic number (ASCII "UBI!") */
12953 +#define UBI_VID_HDR_MAGIC 0x55424921
12954 +
12955 +/*
12956 + * Volume type constants used in the volume identifier header.
12957 + *
12958 + * @UBI_VID_DYNAMIC: dynamic volume
12959 + * @UBI_VID_STATIC: static volume
12960 + */
12961 +enum {
12962 + UBI_VID_DYNAMIC = 1,
12963 + UBI_VID_STATIC = 2
12964 +};
12965 +
12966 +/*
12967 + * Volume flags used in the volume table record.
12968 + *
12969 + * @UBI_VTBL_AUTORESIZE_FLG: auto-resize this volume
12970 + *
12971 + * %UBI_VTBL_AUTORESIZE_FLG flag can be set only for one volume in the volume
12972 + * table. UBI automatically re-sizes the volume which has this flag and makes
12973 + * the volume to be of largest possible size. This means that if after the
12974 + * initialization UBI finds out that there are available physical eraseblocks
12975 + * present on the device, it automatically appends all of them to the volume
12976 + * (the physical eraseblocks reserved for bad eraseblocks handling and other
12977 + * reserved physical eraseblocks are not taken). So, if there is a volume with
12978 + * the %UBI_VTBL_AUTORESIZE_FLG flag set, the amount of available logical
12979 + * eraseblocks will be zero after UBI is loaded, because all of them will be
12980 + * reserved for this volume. Note, the %UBI_VTBL_AUTORESIZE_FLG bit is cleared
12981 + * after the volume had been initialized.
12982 + *
12983 + * The auto-resize feature is useful for device production purposes. For
12984 + * example, different NAND flash chips may have different amount of initial bad
12985 + * eraseblocks, depending of particular chip instance. Manufacturers of NAND
12986 + * chips usually guarantee that the amount of initial bad eraseblocks does not
12987 + * exceed certain percent, e.g. 2%. When one creates an UBI image which will be
12988 + * flashed to the end devices in production, he does not know the exact amount
12989 + * of good physical eraseblocks the NAND chip on the device will have, but this
12990 + * number is required to calculate the volume sized and put them to the volume
12991 + * table of the UBI image. In this case, one of the volumes (e.g., the one
12992 + * which will store the root file system) is marked as "auto-resizable", and
12993 + * UBI will adjust its size on the first boot if needed.
12994 + *
12995 + * Note, first UBI reserves some amount of physical eraseblocks for bad
12996 + * eraseblock handling, and then re-sizes the volume, not vice-versa. This
12997 + * means that the pool of reserved physical eraseblocks will always be present.
12998 + */
12999 +enum {
13000 + UBI_VTBL_AUTORESIZE_FLG = 0x01,
13001 +};
13002 +
13003 +/*
13004 + * Compatibility constants used by internal volumes.
13005 + *
13006 + * @UBI_COMPAT_DELETE: delete this internal volume before anything is written
13007 + * to the flash
13008 + * @UBI_COMPAT_RO: attach this device in read-only mode
13009 + * @UBI_COMPAT_PRESERVE: preserve this internal volume - do not touch its
13010 + * physical eraseblocks, don't allow the wear-leveling unit to move them
13011 + * @UBI_COMPAT_REJECT: reject this UBI image
13012 + */
13013 +enum {
13014 + UBI_COMPAT_DELETE = 1,
13015 + UBI_COMPAT_RO = 2,
13016 + UBI_COMPAT_PRESERVE = 4,
13017 + UBI_COMPAT_REJECT = 5
13018 +};
13019 +
13020 +/* Sizes of UBI headers */
13021 +#define UBI_EC_HDR_SIZE sizeof(struct ubi_ec_hdr)
13022 +#define UBI_VID_HDR_SIZE sizeof(struct ubi_vid_hdr)
13023 +
13024 +/* Sizes of UBI headers without the ending CRC */
13025 +#define UBI_EC_HDR_SIZE_CRC (UBI_EC_HDR_SIZE - sizeof(__be32))
13026 +#define UBI_VID_HDR_SIZE_CRC (UBI_VID_HDR_SIZE - sizeof(__be32))
13027 +
13028 +/**
13029 + * struct ubi_ec_hdr - UBI erase counter header.
13030 + * @magic: erase counter header magic number (%UBI_EC_HDR_MAGIC)
13031 + * @version: version of UBI implementation which is supposed to accept this
13032 + * UBI image
13033 + * @padding1: reserved for future, zeroes
13034 + * @ec: the erase counter
13035 + * @vid_hdr_offset: where the VID header starts
13036 + * @data_offset: where the user data start
13037 + * @padding2: reserved for future, zeroes
13038 + * @hdr_crc: erase counter header CRC checksum
13039 + *
13040 + * The erase counter header takes 64 bytes and has a plenty of unused space for
13041 + * future usage. The unused fields are zeroed. The @version field is used to
13042 + * indicate the version of UBI implementation which is supposed to be able to
13043 + * work with this UBI image. If @version is greater then the current UBI
13044 + * version, the image is rejected. This may be useful in future if something
13045 + * is changed radically. This field is duplicated in the volume identifier
13046 + * header.
13047 + *
13048 + * The @vid_hdr_offset and @data_offset fields contain the offset of the the
13049 + * volume identifier header and user data, relative to the beginning of the
13050 + * physical eraseblock. These values have to be the same for all physical
13051 + * eraseblocks.
13052 + */
13053 +struct ubi_ec_hdr {
13054 + __be32 magic;
13055 + __u8 version;
13056 + __u8 padding1[3];
13057 + __be64 ec; /* Warning: the current limit is 31-bit anyway! */
13058 + __be32 vid_hdr_offset;
13059 + __be32 data_offset;
13060 + __u8 padding2[36];
13061 + __be32 hdr_crc;
13062 +} __attribute__ ((packed));
13063 +
13064 +/**
13065 + * struct ubi_vid_hdr - on-flash UBI volume identifier header.
13066 + * @magic: volume identifier header magic number (%UBI_VID_HDR_MAGIC)
13067 + * @version: UBI implementation version which is supposed to accept this UBI
13068 + * image (%UBI_VERSION)
13069 + * @vol_type: volume type (%UBI_VID_DYNAMIC or %UBI_VID_STATIC)
13070 + * @copy_flag: if this logical eraseblock was copied from another physical
13071 + * eraseblock (for wear-leveling reasons)
13072 + * @compat: compatibility of this volume (%0, %UBI_COMPAT_DELETE,
13073 + * %UBI_COMPAT_IGNORE, %UBI_COMPAT_PRESERVE, or %UBI_COMPAT_REJECT)
13074 + * @vol_id: ID of this volume
13075 + * @lnum: logical eraseblock number
13076 + * @leb_ver: version of this logical eraseblock (IMPORTANT: obsolete, to be
13077 + * removed, kept only for not breaking older UBI users)
13078 + * @data_size: how many bytes of data this logical eraseblock contains
13079 + * @used_ebs: total number of used logical eraseblocks in this volume
13080 + * @data_pad: how many bytes at the end of this physical eraseblock are not
13081 + * used
13082 + * @data_crc: CRC checksum of the data stored in this logical eraseblock
13083 + * @padding1: reserved for future, zeroes
13084 + * @sqnum: sequence number
13085 + * @padding2: reserved for future, zeroes
13086 + * @hdr_crc: volume identifier header CRC checksum
13087 + *
13088 + * The @sqnum is the value of the global sequence counter at the time when this
13089 + * VID header was created. The global sequence counter is incremented each time
13090 + * UBI writes a new VID header to the flash, i.e. when it maps a logical
13091 + * eraseblock to a new physical eraseblock. The global sequence counter is an
13092 + * unsigned 64-bit integer and we assume it never overflows. The @sqnum
13093 + * (sequence number) is used to distinguish between older and newer versions of
13094 + * logical eraseblocks.
13095 + *
13096 + * There are 2 situations when there may be more then one physical eraseblock
13097 + * corresponding to the same logical eraseblock, i.e., having the same @vol_id
13098 + * and @lnum values in the volume identifier header. Suppose we have a logical
13099 + * eraseblock L and it is mapped to the physical eraseblock P.
13100 + *
13101 + * 1. Because UBI may erase physical eraseblocks asynchronously, the following
13102 + * situation is possible: L is asynchronously erased, so P is scheduled for
13103 + * erasure, then L is written to,i.e. mapped to another physical eraseblock P1,
13104 + * so P1 is written to, then an unclean reboot happens. Result - there are 2
13105 + * physical eraseblocks P and P1 corresponding to the same logical eraseblock
13106 + * L. But P1 has greater sequence number, so UBI picks P1 when it attaches the
13107 + * flash.
13108 + *
13109 + * 2. From time to time UBI moves logical eraseblocks to other physical
13110 + * eraseblocks for wear-leveling reasons. If, for example, UBI moves L from P
13111 + * to P1, and an unclean reboot happens before P is physically erased, there
13112 + * are two physical eraseblocks P and P1 corresponding to L and UBI has to
13113 + * select one of them when the flash is attached. The @sqnum field says which
13114 + * PEB is the original (obviously P will have lower @sqnum) and the copy. But
13115 + * it is not enough to select the physical eraseblock with the higher sequence
13116 + * number, because the unclean reboot could have happen in the middle of the
13117 + * copying process, so the data in P is corrupted. It is also not enough to
13118 + * just select the physical eraseblock with lower sequence number, because the
13119 + * data there may be old (consider a case if more data was added to P1 after
13120 + * the copying). Moreover, the unclean reboot may happen when the erasure of P
13121 + * was just started, so it result in unstable P, which is "mostly" OK, but
13122 + * still has unstable bits.
13123 + *
13124 + * UBI uses the @copy_flag field to indicate that this logical eraseblock is a
13125 + * copy. UBI also calculates data CRC when the data is moved and stores it at
13126 + * the @data_crc field of the copy (P1). So when UBI needs to pick one physical
13127 + * eraseblock of two (P or P1), the @copy_flag of the newer one (P1) is
13128 + * examined. If it is cleared, the situation* is simple and the newer one is
13129 + * picked. If it is set, the data CRC of the copy (P1) is examined. If the CRC
13130 + * checksum is correct, this physical eraseblock is selected (P1). Otherwise
13131 + * the older one (P) is selected.
13132 + *
13133 + * Note, there is an obsolete @leb_ver field which was used instead of @sqnum
13134 + * in the past. But it is not used anymore and we keep it in order to be able
13135 + * to deal with old UBI images. It will be removed at some point.
13136 + *
13137 + * There are 2 sorts of volumes in UBI: user volumes and internal volumes.
13138 + * Internal volumes are not seen from outside and are used for various internal
13139 + * UBI purposes. In this implementation there is only one internal volume - the
13140 + * layout volume. Internal volumes are the main mechanism of UBI extensions.
13141 + * For example, in future one may introduce a journal internal volume. Internal
13142 + * volumes have their own reserved range of IDs.
13143 + *
13144 + * The @compat field is only used for internal volumes and contains the "degree
13145 + * of their compatibility". It is always zero for user volumes. This field
13146 + * provides a mechanism to introduce UBI extensions and to be still compatible
13147 + * with older UBI binaries. For example, if someone introduced a journal in
13148 + * future, he would probably use %UBI_COMPAT_DELETE compatibility for the
13149 + * journal volume. And in this case, older UBI binaries, which know nothing
13150 + * about the journal volume, would just delete this volume and work perfectly
13151 + * fine. This is similar to what Ext2fs does when it is fed by an Ext3fs image
13152 + * - it just ignores the Ext3fs journal.
13153 + *
13154 + * The @data_crc field contains the CRC checksum of the contents of the logical
13155 + * eraseblock if this is a static volume. In case of dynamic volumes, it does
13156 + * not contain the CRC checksum as a rule. The only exception is when the
13157 + * data of the physical eraseblock was moved by the wear-leveling unit, then
13158 + * the wear-leveling unit calculates the data CRC and stores it in the
13159 + * @data_crc field. And of course, the @copy_flag is %in this case.
13160 + *
13161 + * The @data_size field is used only for static volumes because UBI has to know
13162 + * how many bytes of data are stored in this eraseblock. For dynamic volumes,
13163 + * this field usually contains zero. The only exception is when the data of the
13164 + * physical eraseblock was moved to another physical eraseblock for
13165 + * wear-leveling reasons. In this case, UBI calculates CRC checksum of the
13166 + * contents and uses both @data_crc and @data_size fields. In this case, the
13167 + * @data_size field contains data size.
13168 + *
13169 + * The @used_ebs field is used only for static volumes and indicates how many
13170 + * eraseblocks the data of the volume takes. For dynamic volumes this field is
13171 + * not used and always contains zero.
13172 + *
13173 + * The @data_pad is calculated when volumes are created using the alignment
13174 + * parameter. So, effectively, the @data_pad field reduces the size of logical
13175 + * eraseblocks of this volume. This is very handy when one uses block-oriented
13176 + * software (say, cramfs) on top of the UBI volume.
13177 + */
13178 +struct ubi_vid_hdr {
13179 + __be32 magic;
13180 + __u8 version;
13181 + __u8 vol_type;
13182 + __u8 copy_flag;
13183 + __u8 compat;
13184 + __be32 vol_id;
13185 + __be32 lnum;
13186 + __be32 leb_ver; /* obsolete, to be removed, don't use */
13187 + __be32 data_size;
13188 + __be32 used_ebs;
13189 + __be32 data_pad;
13190 + __be32 data_crc;
13191 + __u8 padding1[4];
13192 + __be64 sqnum;
13193 + __u8 padding2[12];
13194 + __be32 hdr_crc;
13195 +} __attribute__ ((packed));
13196 +
13197 +/* Internal UBI volumes count */
13198 +#define UBI_INT_VOL_COUNT 1
13199 +
13200 +/*
13201 + * Starting ID of internal volumes. There is reserved room for 4096 internal
13202 + * volumes.
13203 + */
13204 +#define UBI_INTERNAL_VOL_START (0x7FFFFFFF - 4096)
13205 +
13206 +/* The layout volume contains the volume table */
13207 +
13208 +#define UBI_LAYOUT_VOLUME_ID UBI_INTERNAL_VOL_START
13209 +#define UBI_LAYOUT_VOLUME_TYPE UBI_VID_DYNAMIC
13210 +#define UBI_LAYOUT_VOLUME_ALIGN 1
13211 +#define UBI_LAYOUT_VOLUME_EBS 2
13212 +#define UBI_LAYOUT_VOLUME_NAME "layout volume"
13213 +#define UBI_LAYOUT_VOLUME_COMPAT UBI_COMPAT_REJECT
13214 +
13215 +/* The maximum number of volumes per one UBI device */
13216 +#define UBI_MAX_VOLUMES 128
13217 +
13218 +/* The maximum volume name length */
13219 +#define UBI_VOL_NAME_MAX 127
13220 +
13221 +/* Size of the volume table record */
13222 +#define UBI_VTBL_RECORD_SIZE sizeof(struct ubi_vtbl_record)
13223 +
13224 +/* Size of the volume table record without the ending CRC */
13225 +#define UBI_VTBL_RECORD_SIZE_CRC (UBI_VTBL_RECORD_SIZE - sizeof(__be32))
13226 +
13227 +/**
13228 + * struct ubi_vtbl_record - a record in the volume table.
13229 + * @reserved_pebs: how many physical eraseblocks are reserved for this volume
13230 + * @alignment: volume alignment
13231 + * @data_pad: how many bytes are unused at the end of the each physical
13232 + * eraseblock to satisfy the requested alignment
13233 + * @vol_type: volume type (%UBI_DYNAMIC_VOLUME or %UBI_STATIC_VOLUME)
13234 + * @upd_marker: if volume update was started but not finished
13235 + * @name_len: volume name length
13236 + * @name: the volume name
13237 + * @flags: volume flags (%UBI_VTBL_AUTORESIZE_FLG)
13238 + * @padding: reserved, zeroes
13239 + * @crc: a CRC32 checksum of the record
13240 + *
13241 + * The volume table records are stored in the volume table, which is stored in
13242 + * the layout volume. The layout volume consists of 2 logical eraseblock, each
13243 + * of which contains a copy of the volume table (i.e., the volume table is
13244 + * duplicated). The volume table is an array of &struct ubi_vtbl_record
13245 + * objects indexed by the volume ID.
13246 + *
13247 + * If the size of the logical eraseblock is large enough to fit
13248 + * %UBI_MAX_VOLUMES records, the volume table contains %UBI_MAX_VOLUMES
13249 + * records. Otherwise, it contains as many records as it can fit (i.e., size of
13250 + * logical eraseblock divided by sizeof(struct ubi_vtbl_record)).
13251 + *
13252 + * The @upd_marker flag is used to implement volume update. It is set to %1
13253 + * before update and set to %0 after the update. So if the update operation was
13254 + * interrupted, UBI knows that the volume is corrupted.
13255 + *
13256 + * The @alignment field is specified when the volume is created and cannot be
13257 + * later changed. It may be useful, for example, when a block-oriented file
13258 + * system works on top of UBI. The @data_pad field is calculated using the
13259 + * logical eraseblock size and @alignment. The alignment must be multiple to the
13260 + * minimal flash I/O unit. If @alignment is 1, all the available space of
13261 + * the physical eraseblocks is used.
13262 + *
13263 + * Empty records contain all zeroes and the CRC checksum of those zeroes.
13264 + */
13265 +struct ubi_vtbl_record {
13266 + __be32 reserved_pebs;
13267 + __be32 alignment;
13268 + __be32 data_pad;
13269 + __u8 vol_type;
13270 + __u8 upd_marker;
13271 + __be16 name_len;
13272 + __u8 name[UBI_VOL_NAME_MAX+1];
13273 + __u8 flags;
13274 + __u8 padding[23];
13275 + __be32 crc;
13276 +} __attribute__ ((packed));
13277 +
13278 +#endif /* !__UBI_MEDIA_H__ */
13279 diff --exclude=.git -urN linux-2.6.25.6/drivers/mtd/ubi/upd.c avr32-2.6/drivers/mtd/ubi/upd.c
13280 --- linux-2.6.25.6/drivers/mtd/ubi/upd.c 2008-06-09 20:27:19.000000000 +0200
13281 +++ avr32-2.6/drivers/mtd/ubi/upd.c 2008-06-12 15:09:41.123815692 +0200
13282 @@ -237,10 +237,10 @@
13283 int err;
13284
13285 if (vol->vol_type == UBI_DYNAMIC_VOLUME) {
13286 - len = ALIGN(len, ubi->min_io_size);
13287 - memset(buf + len, 0xFF, len - len);
13288 + int l = ALIGN(len, ubi->min_io_size);
13289
13290 - len = ubi_calc_data_len(ubi, buf, len);
13291 + memset(buf + len, 0xFF, l - len);
13292 + len = ubi_calc_data_len(ubi, buf, l);
13293 if (len == 0) {
13294 dbg_msg("all %d bytes contain 0xFF - skip", len);
13295 return 0;
13296 diff --exclude=.git -urN linux-2.6.25.6/drivers/mtd/ubi/vmt.c avr32-2.6/drivers/mtd/ubi/vmt.c
13297 --- linux-2.6.25.6/drivers/mtd/ubi/vmt.c 2008-06-09 20:27:19.000000000 +0200
13298 +++ avr32-2.6/drivers/mtd/ubi/vmt.c 2008-06-12 15:09:41.123815692 +0200
13299 @@ -127,6 +127,7 @@
13300 {
13301 struct ubi_volume *vol = container_of(dev, struct ubi_volume, dev);
13302
13303 + kfree(vol->eba_tbl);
13304 kfree(vol);
13305 }
13306
13307 @@ -201,7 +202,7 @@
13308 */
13309 int ubi_create_volume(struct ubi_device *ubi, struct ubi_mkvol_req *req)
13310 {
13311 - int i, err, vol_id = req->vol_id, dont_free = 0;
13312 + int i, err, vol_id = req->vol_id, do_free = 1;
13313 struct ubi_volume *vol;
13314 struct ubi_vtbl_record vtbl_rec;
13315 uint64_t bytes;
13316 @@ -365,14 +366,14 @@
13317
13318 out_sysfs:
13319 /*
13320 - * We have registered our device, we should not free the volume*
13321 + * We have registered our device, we should not free the volume
13322 * description object in this function in case of an error - it is
13323 * freed by the release function.
13324 *
13325 * Get device reference to prevent the release function from being
13326 * called just after sysfs has been closed.
13327 */
13328 - dont_free = 1;
13329 + do_free = 0;
13330 get_device(&vol->dev);
13331 volume_sysfs_close(vol);
13332 out_gluebi:
13333 @@ -382,17 +383,18 @@
13334 out_cdev:
13335 cdev_del(&vol->cdev);
13336 out_mapping:
13337 - kfree(vol->eba_tbl);
13338 + if (do_free)
13339 + kfree(vol->eba_tbl);
13340 out_acc:
13341 spin_lock(&ubi->volumes_lock);
13342 ubi->rsvd_pebs -= vol->reserved_pebs;
13343 ubi->avail_pebs += vol->reserved_pebs;
13344 out_unlock:
13345 spin_unlock(&ubi->volumes_lock);
13346 - if (dont_free)
13347 - put_device(&vol->dev);
13348 - else
13349 + if (do_free)
13350 kfree(vol);
13351 + else
13352 + put_device(&vol->dev);
13353 ubi_err("cannot create volume %d, error %d", vol_id, err);
13354 return err;
13355 }
13356 @@ -445,8 +447,6 @@
13357 goto out_err;
13358 }
13359
13360 - kfree(vol->eba_tbl);
13361 - vol->eba_tbl = NULL;
13362 cdev_del(&vol->cdev);
13363 volume_sysfs_close(vol);
13364
13365 @@ -727,7 +727,7 @@
13366 goto fail;
13367 }
13368
13369 - n = vol->alignment % ubi->min_io_size;
13370 + n = vol->alignment & (ubi->min_io_size - 1);
13371 if (vol->alignment != 1 && n) {
13372 ubi_err("alignment is not multiple of min I/O unit");
13373 goto fail;
13374 diff --exclude=.git -urN linux-2.6.25.6/drivers/mtd/ubi/vtbl.c avr32-2.6/drivers/mtd/ubi/vtbl.c
13375 --- linux-2.6.25.6/drivers/mtd/ubi/vtbl.c 2008-06-09 20:27:19.000000000 +0200
13376 +++ avr32-2.6/drivers/mtd/ubi/vtbl.c 2008-06-12 15:09:41.127815922 +0200
13377 @@ -127,7 +127,7 @@
13378 const struct ubi_vtbl_record *vtbl)
13379 {
13380 int i, n, reserved_pebs, alignment, data_pad, vol_type, name_len;
13381 - int upd_marker;
13382 + int upd_marker, err;
13383 uint32_t crc;
13384 const char *name;
13385
13386 @@ -153,7 +153,7 @@
13387 if (reserved_pebs == 0) {
13388 if (memcmp(&vtbl[i], &empty_vtbl_record,
13389 UBI_VTBL_RECORD_SIZE)) {
13390 - dbg_err("bad empty record");
13391 + err = 2;
13392 goto bad;
13393 }
13394 continue;
13395 @@ -161,56 +161,57 @@
13396
13397 if (reserved_pebs < 0 || alignment < 0 || data_pad < 0 ||
13398 name_len < 0) {
13399 - dbg_err("negative values");
13400 + err = 3;
13401 goto bad;
13402 }
13403
13404 if (alignment > ubi->leb_size || alignment == 0) {
13405 - dbg_err("bad alignment");
13406 + err = 4;
13407 goto bad;
13408 }
13409
13410 - n = alignment % ubi->min_io_size;
13411 + n = alignment & (ubi->min_io_size - 1);
13412 if (alignment != 1 && n) {
13413 - dbg_err("alignment is not multiple of min I/O unit");
13414 + err = 5;
13415 goto bad;
13416 }
13417
13418 n = ubi->leb_size % alignment;
13419 if (data_pad != n) {
13420 dbg_err("bad data_pad, has to be %d", n);
13421 + err = 6;
13422 goto bad;
13423 }
13424
13425 if (vol_type != UBI_VID_DYNAMIC && vol_type != UBI_VID_STATIC) {
13426 - dbg_err("bad vol_type");
13427 + err = 7;
13428 goto bad;
13429 }
13430
13431 if (upd_marker != 0 && upd_marker != 1) {
13432 - dbg_err("bad upd_marker");
13433 + err = 8;
13434 goto bad;
13435 }
13436
13437 if (reserved_pebs > ubi->good_peb_count) {
13438 dbg_err("too large reserved_pebs, good PEBs %d",
13439 ubi->good_peb_count);
13440 + err = 9;
13441 goto bad;
13442 }
13443
13444 if (name_len > UBI_VOL_NAME_MAX) {
13445 - dbg_err("too long volume name, max %d",
13446 - UBI_VOL_NAME_MAX);
13447 + err = 10;
13448 goto bad;
13449 }
13450
13451 if (name[0] == '\0') {
13452 - dbg_err("NULL volume name");
13453 + err = 11;
13454 goto bad;
13455 }
13456
13457 if (name_len != strnlen(name, name_len + 1)) {
13458 - dbg_err("bad name_len");
13459 + err = 12;
13460 goto bad;
13461 }
13462 }
13463 @@ -235,7 +236,7 @@
13464 return 0;
13465
13466 bad:
13467 - ubi_err("volume table check failed, record %d", i);
13468 + ubi_err("volume table check failed: record %d, error %d", i, err);
13469 ubi_dbg_dump_vtbl_record(&vtbl[i], i);
13470 return -EINVAL;
13471 }
13472 @@ -384,7 +385,16 @@
13473 err = ubi_io_read_data(ubi, leb[seb->lnum], seb->pnum, 0,
13474 ubi->vtbl_size);
13475 if (err == UBI_IO_BITFLIPS || err == -EBADMSG)
13476 - /* Scrub the PEB later */
13477 + /*
13478 + * Scrub the PEB later. Note, -EBADMSG indicates an
13479 + * uncorrectable ECC error, but we have our own CRC and
13480 + * the data will be checked later. If the data is OK,
13481 + * the PEB will be scrubbed (because we set
13482 + * seb->scrub). If the data is not OK, the contents of
13483 + * the PEB will be recovered from the second copy, and
13484 + * seb->scrub will be cleared in
13485 + * 'ubi_scan_add_used()'.
13486 + */
13487 seb->scrub = 1;
13488 else if (err)
13489 goto out_free;
13490 @@ -620,30 +630,32 @@
13491 static int check_sv(const struct ubi_volume *vol,
13492 const struct ubi_scan_volume *sv)
13493 {
13494 + int err;
13495 +
13496 if (sv->highest_lnum >= vol->reserved_pebs) {
13497 - dbg_err("bad highest_lnum");
13498 + err = 1;
13499 goto bad;
13500 }
13501 if (sv->leb_count > vol->reserved_pebs) {
13502 - dbg_err("bad leb_count");
13503 + err = 2;
13504 goto bad;
13505 }
13506 if (sv->vol_type != vol->vol_type) {
13507 - dbg_err("bad vol_type");
13508 + err = 3;
13509 goto bad;
13510 }
13511 if (sv->used_ebs > vol->reserved_pebs) {
13512 - dbg_err("bad used_ebs");
13513 + err = 4;
13514 goto bad;
13515 }
13516 if (sv->data_pad != vol->data_pad) {
13517 - dbg_err("bad data_pad");
13518 + err = 5;
13519 goto bad;
13520 }
13521 return 0;
13522
13523 bad:
13524 - ubi_err("bad scanning information");
13525 + ubi_err("bad scanning information, error %d", err);
13526 ubi_dbg_dump_sv(sv);
13527 ubi_dbg_dump_vol_info(vol);
13528 return -EINVAL;
13529 @@ -672,14 +684,13 @@
13530 return -EINVAL;
13531 }
13532
13533 - if (si->highest_vol_id >= ubi->vtbl_slots + UBI_INT_VOL_COUNT&&
13534 + if (si->highest_vol_id >= ubi->vtbl_slots + UBI_INT_VOL_COUNT &&
13535 si->highest_vol_id < UBI_INTERNAL_VOL_START) {
13536 ubi_err("too large volume ID %d found by scanning",
13537 si->highest_vol_id);
13538 return -EINVAL;
13539 }
13540
13541 -
13542 for (i = 0; i < ubi->vtbl_slots + UBI_INT_VOL_COUNT; i++) {
13543 cond_resched();
13544
13545 diff --exclude=.git -urN linux-2.6.25.6/drivers/mtd/ubi/wl.c avr32-2.6/drivers/mtd/ubi/wl.c
13546 --- linux-2.6.25.6/drivers/mtd/ubi/wl.c 2008-06-09 20:27:19.000000000 +0200
13547 +++ avr32-2.6/drivers/mtd/ubi/wl.c 2008-06-12 15:09:41.127815922 +0200
13548 @@ -1368,7 +1368,7 @@
13549 int err;
13550
13551 if (kthread_should_stop())
13552 - goto out;
13553 + break;
13554
13555 if (try_to_freeze())
13556 continue;
13557 @@ -1403,7 +1403,6 @@
13558 cond_resched();
13559 }
13560
13561 -out:
13562 dbg_wl("background thread \"%s\" is killed", ubi->bgt_name);
13563 return 0;
13564 }
13565 diff --exclude=.git -urN linux-2.6.25.6/drivers/net/macb.c avr32-2.6/drivers/net/macb.c
13566 --- linux-2.6.25.6/drivers/net/macb.c 2008-06-09 20:27:19.000000000 +0200
13567 +++ avr32-2.6/drivers/net/macb.c 2008-06-12 15:09:41.343816061 +0200
13568 @@ -1277,8 +1277,45 @@
13569 return 0;
13570 }
13571
13572 +#ifdef CONFIG_PM
13573 +static int macb_suspend(struct platform_device *pdev, pm_message_t state)
13574 +{
13575 + struct net_device *netdev = platform_get_drvdata(pdev);
13576 + struct macb *bp = netdev_priv(netdev);
13577 +
13578 + netif_device_detach(netdev);
13579 +
13580 +#ifndef CONFIG_ARCH_AT91
13581 + clk_disable(bp->hclk);
13582 +#endif
13583 + clk_disable(bp->pclk);
13584 +
13585 + return 0;
13586 +}
13587 +
13588 +static int macb_resume(struct platform_device *pdev)
13589 +{
13590 + struct net_device *netdev = platform_get_drvdata(pdev);
13591 + struct macb *bp = netdev_priv(netdev);
13592 +
13593 + clk_enable(bp->pclk);
13594 +#ifndef CONFIG_ARCH_AT91
13595 + clk_enable(bp->hclk);
13596 +#endif
13597 +
13598 + netif_device_attach(netdev);
13599 +
13600 + return 0;
13601 +}
13602 +#else
13603 +#define macb_suspend NULL
13604 +#define macb_resume NULL
13605 +#endif
13606 +
13607 static struct platform_driver macb_driver = {
13608 .remove = __exit_p(macb_remove),
13609 + .suspend = macb_suspend,
13610 + .resume = macb_resume,
13611 .driver = {
13612 .name = "macb",
13613 },
13614 diff --exclude=.git -urN linux-2.6.25.6/drivers/parport/Kconfig avr32-2.6/drivers/parport/Kconfig
13615 --- linux-2.6.25.6/drivers/parport/Kconfig 2008-06-09 20:27:19.000000000 +0200
13616 +++ avr32-2.6/drivers/parport/Kconfig 2008-06-12 15:04:01.310815768 +0200
13617 @@ -36,7 +36,7 @@
13618 config PARPORT_PC
13619 tristate "PC-style hardware"
13620 depends on (!SPARC64 || PCI) && !SPARC32 && !M32R && !FRV && \
13621 - (!M68K || ISA) && !MN10300
13622 + (!M68K || ISA) && !MN10300 && !AVR32
13623 ---help---
13624 You should say Y here if you have a PC-style parallel port. All
13625 IBM PC compatible computers and some Alphas have PC-style
13626 diff --exclude=.git -urN linux-2.6.25.6/drivers/pcmcia/at32_cf.c avr32-2.6/drivers/pcmcia/at32_cf.c
13627 --- linux-2.6.25.6/drivers/pcmcia/at32_cf.c 1970-01-01 01:00:00.000000000 +0100
13628 +++ avr32-2.6/drivers/pcmcia/at32_cf.c 2008-06-12 15:09:42.047816626 +0200
13629 @@ -0,0 +1,533 @@
13630 +/*
13631 + * Driver for AVR32 Static Memory Controller: CompactFlash support
13632 + *
13633 + * Copyright (C) 2006 Atmel Norway
13634 + *
13635 + * This program is free software; you can redistribute it and/or
13636 + * modify it under the terms of the GNU General Public License as
13637 + * published by the Free Software Foundation; either version 2 of the
13638 + * License, or (at your option) any later version.
13639 + *
13640 + * This program is distributed in the hope that it will be useful, but
13641 + * WITHOUT ANY WARRANTY; without even the implied warranty of
13642 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13643 + * General Public License for more details.
13644 + *
13645 + * You should have received a copy of the GNU General Public License
13646 + * along with this program; if not, write to the Free Software
13647 + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
13648 + * 02111-1307, USA.
13649 + *
13650 + * The full GNU General Public License is included in this
13651 + * distribution in the file called COPYING.
13652 + */
13653 +#include <linux/module.h>
13654 +#include <linux/kernel.h>
13655 +#include <linux/platform_device.h>
13656 +#include <linux/init.h>
13657 +#include <linux/device.h>
13658 +#include <linux/delay.h>
13659 +#include <linux/interrupt.h>
13660 +#include <linux/err.h>
13661 +#include <linux/clk.h>
13662 +#include <linux/dma-mapping.h>
13663 +
13664 +#include <pcmcia/ss.h>
13665 +
13666 +#include <asm/gpio.h>
13667 +#include <asm/io.h>
13668 +#include <asm/arch/board.h>
13669 +
13670 +#include <asm/arch/smc.h>
13671 +
13672 +struct at32_cf_socket {
13673 + struct pcmcia_socket socket;
13674 + int detect_pin;
13675 + int reset_pin;
13676 + int vcc_pin;
13677 + int ready_pin;
13678 + struct resource res_attr;
13679 + struct resource res_mem;
13680 + struct resource res_io;
13681 + struct smc_config smc;
13682 + unsigned int irq;
13683 + unsigned int cf_cs;
13684 + socket_state_t state;
13685 + unsigned present:1;
13686 +};
13687 +#define to_at32_cf(sock) container_of(sock, struct at32_cf_socket, socket)
13688 +
13689 +/*
13690 + * We have the following memory layout relative to the base address:
13691 + *
13692 + * Alt IDE Mode: 00e0 0000 -> 00ff ffff
13693 + * True IDE Mode: 00c0 0000 -> 00df ffff
13694 + * I/O memory: 0080 0000 -> 00bf ffff
13695 + * Common memory: 0040 0000 -> 007f ffff
13696 + * Attribute memory: 0000 0000 -> 003f ffff
13697 + */
13698 +#define CF_ATTR_OFFSET 0x00000000
13699 +#define CF_MEM_OFFSET 0x00400000
13700 +#define CF_IO_OFFSET 0x00800000
13701 +#define CF_RES_SIZE 4096
13702 +
13703 +#ifdef DEBUG
13704 +
13705 +static int pc_debug;
13706 +module_param(pc_debug, int, 0644);
13707 +
13708 +static void at32_cf_debug(struct at32_cf_socket *cf, const char *func,
13709 + int level, const char *fmt, ...)
13710 +{
13711 + va_list args;
13712 +
13713 + if (pc_debug > level) {
13714 + printk(KERN_DEBUG "at32_cf/%u: %s: ", cf->cf_cs, func);
13715 + va_start(args, fmt);
13716 + vprintk(fmt, args);
13717 + va_end(args);
13718 + }
13719 +}
13720 +
13721 +#define debug(cf, lvl, fmt, arg...) \
13722 + at32_cf_debug(cf, __func__, lvl, fmt, ##arg)
13723 +
13724 +#else
13725 +#define debug(cf, lvl, fmt, arg...) do { } while (0)
13726 +#endif
13727 +
13728 +static inline int at32_cf_present(struct at32_cf_socket *cf)
13729 +{
13730 + int present = 1;
13731 +
13732 + /* If we don't have a detect pin, assume the card is present */
13733 + if (cf->detect_pin >= 0)
13734 + present = !gpio_get_value(cf->detect_pin);
13735 +
13736 + return present;
13737 +}
13738 +
13739 +static irqreturn_t at32_cf_irq(int irq, void *dev_id)
13740 +{
13741 + struct at32_cf_socket *cf = dev_id;
13742 + unsigned int present;
13743 +
13744 + present = at32_cf_present(cf);
13745 + if (present != cf->present) {
13746 + cf->present = present;
13747 + debug(cf, 3, "card %s\n", present ? "present" : "gone");
13748 + pcmcia_parse_events(&cf->socket, SS_DETECT);
13749 + }
13750 +
13751 + return IRQ_HANDLED;
13752 +}
13753 +
13754 +static int at32_cf_get_status(struct pcmcia_socket *sock, u_int *value)
13755 +{
13756 + struct at32_cf_socket *cf;
13757 + u_int status = 0;
13758 +
13759 + cf = container_of(sock, struct at32_cf_socket, socket);
13760 +
13761 + if (at32_cf_present(cf)) {
13762 + /* NOTE: gpio on AP7xxx is 3.3V */
13763 + status = SS_DETECT | SS_3VCARD;
13764 + if (cf->ready_pin < 0 || gpio_get_value(cf->ready_pin))
13765 + status |= SS_READY;
13766 + if (cf->vcc_pin < 0 || gpio_get_value(cf->vcc_pin))
13767 + status |= SS_POWERON;
13768 + }
13769 +
13770 + *value = status;
13771 + return 0;
13772 +}
13773 +
13774 +static int at32_cf_set_socket(struct pcmcia_socket *sock, socket_state_t *state)
13775 +{
13776 + struct at32_cf_socket *cf = container_of(sock, struct at32_cf_socket, socket);
13777 +
13778 + debug(cf, 2, "mask: %s%s%s%s%s%sflags: %s%s%s%s%s%sVcc %d Vpp %d irq %d\n",
13779 + (state->csc_mask==0)?"<NONE> ":"",
13780 + (state->csc_mask&SS_DETECT)?"DETECT ":"",
13781 + (state->csc_mask&SS_READY)?"READY ":"",
13782 + (state->csc_mask&SS_BATDEAD)?"BATDEAD ":"",
13783 + (state->csc_mask&SS_BATWARN)?"BATWARN ":"",
13784 + (state->csc_mask&SS_STSCHG)?"STSCHG ":"",
13785 + (state->flags==0)?"<NONE> ":"",
13786 + (state->flags&SS_PWR_AUTO)?"PWR_AUTO ":"",
13787 + (state->flags&SS_IOCARD)?"IOCARD ":"",
13788 + (state->flags&SS_RESET)?"RESET ":"",
13789 + (state->flags&SS_SPKR_ENA)?"SPKR_ENA ":"",
13790 + (state->flags&SS_OUTPUT_ENA)?"OUTPUT_ENA ":"",
13791 + state->Vcc, state->Vpp, state->io_irq);
13792 +
13793 + /*
13794 + * TODO: Allow boards to override this in case they have level
13795 + * converters.
13796 + */
13797 + switch (state->Vcc) {
13798 + case 0:
13799 + if (cf->vcc_pin >= 0)
13800 + gpio_set_value(cf->vcc_pin, 0);
13801 + break;
13802 + case 33:
13803 + if (cf->vcc_pin >= 0)
13804 + gpio_set_value(cf->vcc_pin, 1);
13805 + break;
13806 + default:
13807 + return -EINVAL;
13808 + }
13809 +
13810 + if (cf->reset_pin >= 0)
13811 + gpio_set_value(cf->reset_pin, state->flags & SS_RESET);
13812 +
13813 + cf->state = *state;
13814 +
13815 + return 0;
13816 +}
13817 +
13818 +static int at32_cf_socket_init(struct pcmcia_socket *sock)
13819 +{
13820 + debug(to_at32_cf(sock), 2, "called\n");
13821 +
13822 + return 0;
13823 +}
13824 +
13825 +static int at32_cf_suspend(struct pcmcia_socket *sock)
13826 +{
13827 + debug(to_at32_cf(sock), 2, "called\n");
13828 +
13829 + at32_cf_set_socket(sock, &dead_socket);
13830 +
13831 + return 0;
13832 +}
13833 +
13834 +static int at32_cf_set_io_map(struct pcmcia_socket *sock,
13835 + struct pccard_io_map *map)
13836 +{
13837 + struct at32_cf_socket *cf = container_of(sock, struct at32_cf_socket, socket);
13838 + int retval;
13839 +
13840 + debug(cf, 2, "map %u speed %u start 0x%08x stop 0x%08x\n",
13841 + map->map, map->speed, map->start, map->stop);
13842 + debug(cf, 2, "flags: %s%s%s%s%s%s%s%s\n",
13843 + (map->flags == 0) ? "<NONE>":"",
13844 + (map->flags & MAP_ACTIVE) ? "ACTIVE " : "",
13845 + (map->flags & MAP_16BIT) ? "16BIT " : "",
13846 + (map->flags & MAP_AUTOSZ) ? "AUTOSZ " : "",
13847 + (map->flags & MAP_0WS) ? "0WS " : "",
13848 + (map->flags & MAP_WRPROT) ? "WRPROT " : "",
13849 + (map->flags & MAP_USE_WAIT) ? "USE_WAIT " : "",
13850 + (map->flags & MAP_PREFETCH) ? "PREFETCH " : "");
13851 +
13852 + map->flags &= MAP_ACTIVE | MAP_16BIT | MAP_USE_WAIT;
13853 +
13854 + if (map->flags & MAP_16BIT)
13855 + cf->smc.bus_width = 2;
13856 + else
13857 + cf->smc.bus_width = 1;
13858 +
13859 + if (map->flags & MAP_USE_WAIT)
13860 + cf->smc.nwait_mode = 3;
13861 + else
13862 + cf->smc.nwait_mode = 0;
13863 +
13864 + retval = smc_set_configuration(cf->cf_cs, &cf->smc);
13865 + if (retval) {
13866 + printk(KERN_ERR "at32_cf: could not set up SMC for I/O\n");
13867 + return retval;
13868 + }
13869 +
13870 + map->start = cf->socket.io_offset;
13871 + map->stop = map->start + CF_RES_SIZE - 1;
13872 +
13873 + return 0;
13874 +}
13875 +
13876 +static int
13877 +at32_cf_set_mem_map(struct pcmcia_socket *sock, struct pccard_mem_map *map)
13878 +{
13879 + struct at32_cf_socket *cf;
13880 + struct resource *res;
13881 + int retval;
13882 +
13883 + cf = container_of(sock, struct at32_cf_socket, socket);
13884 +
13885 + debug(cf, 2, "map %u speed %u card_start %08x\n",
13886 + map->map, map->speed, map->card_start);
13887 + debug(cf, 2, "flags: %s%s%s%s%s%s%s%s\n",
13888 + (map->flags==0)?"<NONE>":"",
13889 + (map->flags&MAP_ACTIVE)?"ACTIVE ":"",
13890 + (map->flags&MAP_16BIT)?"16BIT ":"",
13891 + (map->flags&MAP_AUTOSZ)?"AUTOSZ ":"",
13892 + (map->flags&MAP_0WS)?"0WS ":"",
13893 + (map->flags&MAP_WRPROT)?"WRPROT ":"",
13894 + (map->flags&MAP_ATTRIB)?"ATTRIB ":"",
13895 + (map->flags&MAP_USE_WAIT)?"USE_WAIT ":"");
13896 +
13897 + if (map->card_start)
13898 + return -EINVAL;
13899 +
13900 + map->flags &= MAP_ACTIVE | MAP_ATTRIB | MAP_16BIT | MAP_USE_WAIT;
13901 +
13902 + if (map->flags & MAP_ATTRIB) {
13903 + res = &cf->res_attr;
13904 +
13905 + /* Linksys WCF12 seems to use WAIT when reading CIS */
13906 + map->flags |= MAP_USE_WAIT;
13907 + } else {
13908 + res = &cf->res_mem;
13909 + }
13910 +
13911 + if (map->flags & MAP_USE_WAIT)
13912 + cf->smc.nwait_mode = 3;
13913 + else
13914 + cf->smc.nwait_mode = 0;
13915 +
13916 + retval = smc_set_configuration(cf->cf_cs, &cf->smc);
13917 + if (retval) {
13918 + printk(KERN_ERR "at32_cf: could not set up SMC for mem\n");
13919 + return retval;
13920 + }
13921 +
13922 + map->static_start = res->start;
13923 +
13924 + return 0;
13925 +}
13926 +
13927 +static struct pccard_operations at32_cf_ops = {
13928 + .init = at32_cf_socket_init,
13929 + .suspend = at32_cf_suspend,
13930 + .get_status = at32_cf_get_status,
13931 + .set_socket = at32_cf_set_socket,
13932 + .set_io_map = at32_cf_set_io_map,
13933 + .set_mem_map = at32_cf_set_mem_map,
13934 +};
13935 +
13936 +static int __init request_pin(struct platform_device *pdev,
13937 + unsigned int pin, const char *name)
13938 +{
13939 + if (gpio_request(pin, name)) {
13940 + dev_warn(&pdev->dev, "failed to request %s pin\n", name);
13941 + return -1;
13942 + }
13943 +
13944 + return pin;
13945 +}
13946 +
13947 +static struct smc_timing at32_cf_timing __initdata = {
13948 + .ncs_read_setup = 30,
13949 + .nrd_setup = 100,
13950 + .ncs_write_setup = 30,
13951 + .nwe_setup = 100,
13952 +
13953 + .ncs_read_pulse = 360,
13954 + .nrd_pulse = 290,
13955 + .ncs_write_pulse = 360,
13956 + .nwe_pulse = 290,
13957 +
13958 + .read_cycle = 420,
13959 + .write_cycle = 420,
13960 +};
13961 +
13962 +static int __init at32_cf_probe(struct platform_device *pdev)
13963 +{
13964 + struct at32_cf_socket *cf;
13965 + struct cf_platform_data *board = pdev->dev.platform_data;
13966 + struct resource *res_skt;
13967 + int irq;
13968 + int ret;
13969 +
13970 + dev_dbg(&pdev->dev, "probe");
13971 +
13972 + if (!board)
13973 + return -ENXIO;
13974 +
13975 + res_skt = platform_get_resource(pdev, IORESOURCE_MEM, 0);
13976 + if (!res_skt)
13977 + return -ENXIO;
13978 +
13979 + irq = platform_get_irq(pdev, 0);
13980 + if (irq < 0)
13981 + return irq;
13982 +
13983 + cf = kzalloc(sizeof(struct at32_cf_socket), GFP_KERNEL);
13984 + if (!cf)
13985 + return -ENOMEM;
13986 +
13987 + cf->detect_pin = -1;
13988 + cf->reset_pin = -1;
13989 + cf->vcc_pin = -1;
13990 + cf->ready_pin = -1;
13991 + cf->cf_cs = board->cs;
13992 +
13993 + if (board->detect_pin != GPIO_PIN_NONE)
13994 + cf->detect_pin = request_pin(pdev, board->detect_pin,
13995 + "cf_detect");
13996 + if (board->reset_pin != GPIO_PIN_NONE)
13997 + cf->reset_pin = request_pin(pdev, board->reset_pin,
13998 + "cf_reset");
13999 + if (board->vcc_pin != GPIO_PIN_NONE)
14000 + cf->vcc_pin = request_pin(pdev, board->vcc_pin,
14001 + "cf_vcc");
14002 + if (board->ready_pin != GPIO_PIN_NONE)
14003 + /* READY is also used for irq through EIM */
14004 + cf->ready_pin = board->ready_pin;
14005 +
14006 + debug(cf, 2, "pins: detect=%d reset=%d vcc=%d\n",
14007 + cf->detect_pin, cf->reset_pin, cf->vcc_pin);
14008 +
14009 + cf->socket.pci_irq = irq;
14010 + cf->socket.ops = &at32_cf_ops;
14011 + cf->socket.resource_ops = &pccard_static_ops;
14012 + cf->socket.dev.parent = &pdev->dev;
14013 + cf->socket.owner = THIS_MODULE;
14014 + cf->socket.features =
14015 + SS_CAP_MEM_ALIGN | SS_CAP_STATIC_MAP | SS_CAP_PCCARD;
14016 + cf->socket.map_size = CF_RES_SIZE;
14017 +
14018 + cf->res_attr.start = res_skt->start + CF_ATTR_OFFSET;
14019 + cf->res_attr.end = cf->res_attr.start + CF_RES_SIZE - 1;
14020 + cf->res_attr.name = "attribute";
14021 + cf->res_attr.flags = IORESOURCE_MEM;
14022 + ret = request_resource(res_skt, &cf->res_attr);
14023 + if (ret)
14024 + goto err_request_res_attr;
14025 +
14026 + cf->res_mem.start = res_skt->start + CF_MEM_OFFSET;
14027 + cf->res_mem.end = cf->res_mem.start + CF_RES_SIZE - 1;
14028 + cf->res_mem.name = "memory";
14029 + cf->res_mem.flags = IORESOURCE_MEM;
14030 + ret = request_resource(res_skt, &cf->res_mem);
14031 + if (ret)
14032 + goto err_request_res_mem;
14033 +
14034 + cf->res_io.start = res_skt->start + CF_IO_OFFSET;
14035 + cf->res_io.end = cf->res_io.start + CF_RES_SIZE - 1;
14036 + cf->res_io.name = "io";
14037 + cf->res_io.flags = IORESOURCE_MEM;
14038 + ret = request_resource(res_skt, &cf->res_io);
14039 + if (ret)
14040 + goto err_request_res_io;
14041 +
14042 + cf->socket.io_offset = cf->res_io.start;
14043 +
14044 + if (cf->detect_pin >= 0) {
14045 + ret = request_irq(gpio_to_irq(cf->detect_pin), at32_cf_irq,
14046 + IRQF_SHARED, "cf_detect", cf);
14047 + if (ret) {
14048 + debug(cf, 1,
14049 + "failed to request cf_detect interrupt\n");
14050 + goto err_detect_irq;
14051 + }
14052 + }
14053 +
14054 + cf->present = at32_cf_present(cf);
14055 +
14056 + /* Setup SMC timings */
14057 + smc_set_timing(&cf->smc, &at32_cf_timing);
14058 +
14059 + cf->smc.bus_width = 2;
14060 + cf->smc.nrd_controlled = 1;
14061 + cf->smc.nwe_controlled = 1;
14062 + cf->smc.nwait_mode = 0;
14063 + cf->smc.byte_write = 0;
14064 + cf->smc.tdf_cycles = 8;
14065 + cf->smc.tdf_mode = 0;
14066 +
14067 + ret = smc_set_configuration(cf->cf_cs, &cf->smc);
14068 + if (ret) {
14069 + debug(cf, 1, "failed to configure SMC\n", ret);
14070 + goto err_smc;
14071 + }
14072 +
14073 + ret = pcmcia_register_socket(&cf->socket);
14074 + if (ret) {
14075 + debug(cf, 1, "failed to register socket: %d\n", ret);
14076 + goto err_register_socket;
14077 + }
14078 +
14079 + if (cf->reset_pin >= 0)
14080 + gpio_direction_output(cf->reset_pin, 0);
14081 +
14082 + platform_set_drvdata(pdev, cf);
14083 +
14084 + dev_info(&pdev->dev, "Atmel SMC CF interface at 0x%08lx\n",
14085 + (unsigned long)res_skt->start);
14086 +
14087 + return 0;
14088 +
14089 +err_register_socket:
14090 +err_smc:
14091 + if (cf->detect_pin >= 0)
14092 + free_irq(gpio_to_irq(cf->detect_pin), cf);
14093 +err_detect_irq:
14094 + release_resource(&cf->res_io);
14095 +err_request_res_io:
14096 + release_resource(&cf->res_mem);
14097 +err_request_res_mem:
14098 + release_resource(&cf->res_attr);
14099 +err_request_res_attr:
14100 + if (cf->vcc_pin >= 0)
14101 + gpio_free(cf->vcc_pin);
14102 + if (cf->reset_pin >= 0)
14103 + gpio_free(cf->reset_pin);
14104 + if (cf->detect_pin >= 0)
14105 + gpio_free(cf->detect_pin);
14106 + kfree(cf);
14107 +
14108 + return ret;
14109 +}
14110 +
14111 +static int __exit at32_cf_remove(struct platform_device *pdev)
14112 +{
14113 + struct at32_cf_socket *cf = platform_get_drvdata(pdev);
14114 +
14115 + pcmcia_unregister_socket(&cf->socket);
14116 + if (cf->detect_pin >= 0) {
14117 + free_irq(gpio_to_irq(cf->detect_pin), cf);
14118 + gpio_free(cf->detect_pin);
14119 + }
14120 + if (cf->vcc_pin >= 0)
14121 + gpio_free(cf->vcc_pin);
14122 + if (cf->reset_pin >= 0)
14123 + gpio_free(cf->reset_pin);
14124 +
14125 + release_resource(&cf->res_io);
14126 + release_resource(&cf->res_mem);
14127 + release_resource(&cf->res_attr);
14128 + kfree(cf);
14129 + platform_set_drvdata(pdev, NULL);
14130 +
14131 + return 0;
14132 +}
14133 +
14134 +static struct platform_driver at32_cf_driver = {
14135 + .remove = __exit_p(at32_cf_remove),
14136 + .driver = {
14137 + .name = "at32_cf",
14138 + .owner = THIS_MODULE,
14139 + },
14140 +};
14141 +
14142 +static int __init at32_cf_init(void)
14143 +{
14144 + int ret;
14145 +
14146 + ret = platform_driver_probe(&at32_cf_driver, at32_cf_probe);
14147 + if (ret)
14148 + printk(KERN_ERR "at32_cf: probe failed: %d\n", ret);
14149 + return ret;
14150 +}
14151 +
14152 +static void __exit at32_cf_exit(void)
14153 +{
14154 + platform_driver_unregister(&at32_cf_driver);
14155 +}
14156 +
14157 +module_init(at32_cf_init);
14158 +module_exit(at32_cf_exit);
14159 +
14160 +MODULE_LICENSE("GPL");
14161 +MODULE_DESCRIPTION("Driver for SMC PCMCIA interface");
14162 +MODULE_AUTHOR("Hans-Christian Egtvedt <hcegtvedt@atmel.com>");
14163 diff --exclude=.git -urN linux-2.6.25.6/drivers/pcmcia/Kconfig avr32-2.6/drivers/pcmcia/Kconfig
14164 --- linux-2.6.25.6/drivers/pcmcia/Kconfig 2008-06-09 20:27:19.000000000 +0200
14165 +++ avr32-2.6/drivers/pcmcia/Kconfig 2008-06-12 15:09:42.047816626 +0200
14166 @@ -277,6 +277,13 @@
14167 Say Y here to support the CompactFlash controller on the
14168 PA Semi Electra eval board.
14169
14170 +config AT32_CF
14171 + tristate "AT32AP CompactFlash Controller"
14172 + depends on PCMCIA && AVR32 && PLATFORM_AT32AP
14173 + help
14174 + Say Y here to support the CompactFlash controller on AT32 chips.
14175 + Or choose M to compile the driver as a module named "at32_cf".
14176 +
14177 config PCCARD_NONSTATIC
14178 tristate
14179
14180 diff --exclude=.git -urN linux-2.6.25.6/drivers/pcmcia/Makefile avr32-2.6/drivers/pcmcia/Makefile
14181 --- linux-2.6.25.6/drivers/pcmcia/Makefile 2008-06-09 20:27:19.000000000 +0200
14182 +++ avr32-2.6/drivers/pcmcia/Makefile 2008-06-12 15:09:42.047816626 +0200
14183 @@ -38,6 +38,7 @@
14184 obj-$(CONFIG_OMAP_CF) += omap_cf.o
14185 obj-$(CONFIG_AT91_CF) += at91_cf.o
14186 obj-$(CONFIG_ELECTRA_CF) += electra_cf.o
14187 +obj-$(CONFIG_AT32_CF) += at32_cf.o
14188
14189 sa11xx_core-y += soc_common.o sa11xx_base.o
14190 pxa2xx_core-y += soc_common.o pxa2xx_base.o
14191 diff --exclude=.git -urN linux-2.6.25.6/drivers/serial/atmel_serial.c avr32-2.6/drivers/serial/atmel_serial.c
14192 --- linux-2.6.25.6/drivers/serial/atmel_serial.c 2008-06-09 20:27:19.000000000 +0200
14193 +++ avr32-2.6/drivers/serial/atmel_serial.c 2008-06-12 15:09:42.514816054 +0200
14194 @@ -1440,6 +1440,15 @@
14195 };
14196
14197 #ifdef CONFIG_PM
14198 +static bool atmel_serial_clk_will_stop(void)
14199 +{
14200 +#ifdef CONFIG_ARCH_AT91
14201 + return at91_suspend_entering_slow_clock();
14202 +#else
14203 + return false;
14204 +#endif
14205 +}
14206 +
14207 static int atmel_serial_suspend(struct platform_device *pdev,
14208 pm_message_t state)
14209 {
14210 @@ -1447,7 +1456,7 @@
14211 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
14212
14213 if (device_may_wakeup(&pdev->dev)
14214 - && !at91_suspend_entering_slow_clock())
14215 + && !atmel_serial_clk_will_stop())
14216 enable_irq_wake(port->irq);
14217 else {
14218 uart_suspend_port(&atmel_uart, port);
14219 diff --exclude=.git -urN linux-2.6.25.6/drivers/spi/atmel_spi.c avr32-2.6/drivers/spi/atmel_spi.c
14220 --- linux-2.6.25.6/drivers/spi/atmel_spi.c 2008-06-09 20:27:19.000000000 +0200
14221 +++ avr32-2.6/drivers/spi/atmel_spi.c 2008-06-12 15:09:42.542815989 +0200
14222 @@ -51,9 +51,7 @@
14223 u8 stopping;
14224 struct list_head queue;
14225 struct spi_transfer *current_transfer;
14226 - unsigned long current_remaining_bytes;
14227 - struct spi_transfer *next_transfer;
14228 - unsigned long next_remaining_bytes;
14229 + unsigned long remaining_bytes;
14230
14231 void *buffer;
14232 dma_addr_t buffer_dma;
14233 @@ -133,48 +131,6 @@
14234 gpio_set_value(gpio, !active);
14235 }
14236
14237 -static inline int atmel_spi_xfer_is_last(struct spi_message *msg,
14238 - struct spi_transfer *xfer)
14239 -{
14240 - return msg->transfers.prev == &xfer->transfer_list;
14241 -}
14242 -
14243 -static inline int atmel_spi_xfer_can_be_chained(struct spi_transfer *xfer)
14244 -{
14245 - return xfer->delay_usecs == 0 && !xfer->cs_change;
14246 -}
14247 -
14248 -static void atmel_spi_next_xfer_data(struct spi_master *master,
14249 - struct spi_transfer *xfer,
14250 - dma_addr_t *tx_dma,
14251 - dma_addr_t *rx_dma,
14252 - u32 *plen)
14253 -{
14254 - struct atmel_spi *as = spi_master_get_devdata(master);
14255 - u32 len = *plen;
14256 -
14257 - /* use scratch buffer only when rx or tx data is unspecified */
14258 - if (xfer->rx_buf)
14259 - *rx_dma = xfer->rx_dma + xfer->len - len;
14260 - else {
14261 - *rx_dma = as->buffer_dma;
14262 - if (len > BUFFER_SIZE)
14263 - len = BUFFER_SIZE;
14264 - }
14265 - if (xfer->tx_buf)
14266 - *tx_dma = xfer->tx_dma + xfer->len - len;
14267 - else {
14268 - *tx_dma = as->buffer_dma;
14269 - if (len > BUFFER_SIZE)
14270 - len = BUFFER_SIZE;
14271 - memset(as->buffer, 0, len);
14272 - dma_sync_single_for_device(&as->pdev->dev,
14273 - as->buffer_dma, len, DMA_TO_DEVICE);
14274 - }
14275 -
14276 - *plen = len;
14277 -}
14278 -
14279 /*
14280 * Submit next transfer for DMA.
14281 * lock is held, spi irq is blocked
14282 @@ -184,78 +140,53 @@
14283 {
14284 struct atmel_spi *as = spi_master_get_devdata(master);
14285 struct spi_transfer *xfer;
14286 - u32 len, remaining, total;
14287 + u32 len;
14288 dma_addr_t tx_dma, rx_dma;
14289
14290 - if (!as->current_transfer)
14291 - xfer = list_entry(msg->transfers.next,
14292 - struct spi_transfer, transfer_list);
14293 - else if (!as->next_transfer)
14294 - xfer = list_entry(as->current_transfer->transfer_list.next,
14295 - struct spi_transfer, transfer_list);
14296 - else
14297 - xfer = NULL;
14298 -
14299 - if (xfer) {
14300 - len = xfer->len;
14301 - atmel_spi_next_xfer_data(master, xfer, &tx_dma, &rx_dma, &len);
14302 - remaining = xfer->len - len;
14303 -
14304 - spi_writel(as, RPR, rx_dma);
14305 - spi_writel(as, TPR, tx_dma);
14306 -
14307 - if (msg->spi->bits_per_word > 8)
14308 - len >>= 1;
14309 - spi_writel(as, RCR, len);
14310 - spi_writel(as, TCR, len);
14311 -
14312 - dev_dbg(&msg->spi->dev,
14313 - " start xfer %p: len %u tx %p/%08x rx %p/%08x\n",
14314 - xfer, xfer->len, xfer->tx_buf, xfer->tx_dma,
14315 - xfer->rx_buf, xfer->rx_dma);
14316 - } else {
14317 - xfer = as->next_transfer;
14318 - remaining = as->next_remaining_bytes;
14319 + xfer = as->current_transfer;
14320 + if (!xfer || as->remaining_bytes == 0) {
14321 + if (xfer)
14322 + xfer = list_entry(xfer->transfer_list.next,
14323 + struct spi_transfer, transfer_list);
14324 + else
14325 + xfer = list_entry(msg->transfers.next,
14326 + struct spi_transfer, transfer_list);
14327 + as->remaining_bytes = xfer->len;
14328 + as->current_transfer = xfer;
14329 }
14330
14331 - as->current_transfer = xfer;
14332 - as->current_remaining_bytes = remaining;
14333 -
14334 - if (remaining > 0)
14335 - len = remaining;
14336 - else if (!atmel_spi_xfer_is_last(msg, xfer)
14337 - && atmel_spi_xfer_can_be_chained(xfer)) {
14338 - xfer = list_entry(xfer->transfer_list.next,
14339 - struct spi_transfer, transfer_list);
14340 - len = xfer->len;
14341 - } else
14342 - xfer = NULL;
14343 + len = as->remaining_bytes;
14344
14345 - as->next_transfer = xfer;
14346 + tx_dma = xfer->tx_dma + xfer->len - len;
14347 + rx_dma = xfer->rx_dma + xfer->len - len;
14348
14349 - if (xfer) {
14350 - total = len;
14351 - atmel_spi_next_xfer_data(master, xfer, &tx_dma, &rx_dma, &len);
14352 - as->next_remaining_bytes = total - len;
14353 -
14354 - spi_writel(as, RNPR, rx_dma);
14355 - spi_writel(as, TNPR, tx_dma);
14356 -
14357 - if (msg->spi->bits_per_word > 8)
14358 - len >>= 1;
14359 - spi_writel(as, RNCR, len);
14360 - spi_writel(as, TNCR, len);
14361 -
14362 - dev_dbg(&msg->spi->dev,
14363 - " next xfer %p: len %u tx %p/%08x rx %p/%08x\n",
14364 - xfer, xfer->len, xfer->tx_buf, xfer->tx_dma,
14365 - xfer->rx_buf, xfer->rx_dma);
14366 - } else {
14367 - spi_writel(as, RNCR, 0);
14368 - spi_writel(as, TNCR, 0);
14369 + /* use scratch buffer only when rx or tx data is unspecified */
14370 + if (!xfer->rx_buf) {
14371 + rx_dma = as->buffer_dma;
14372 + if (len > BUFFER_SIZE)
14373 + len = BUFFER_SIZE;
14374 }
14375 + if (!xfer->tx_buf) {
14376 + tx_dma = as->buffer_dma;
14377 + if (len > BUFFER_SIZE)
14378 + len = BUFFER_SIZE;
14379 + memset(as->buffer, 0, len);
14380 + dma_sync_single_for_device(&as->pdev->dev,
14381 + as->buffer_dma, len, DMA_TO_DEVICE);
14382 + }
14383 +
14384 + spi_writel(as, RPR, rx_dma);
14385 + spi_writel(as, TPR, tx_dma);
14386
14387 - /* REVISIT: We're waiting for ENDRX before we start the next
14388 + as->remaining_bytes -= len;
14389 + if (msg->spi->bits_per_word > 8)
14390 + len >>= 1;
14391 +
14392 + /* REVISIT: when xfer->delay_usecs == 0, the PDC "next transfer"
14393 + * mechanism might help avoid the IRQ latency between transfers
14394 + * (and improve the nCS0 errata handling on at91rm9200 chips)
14395 + *
14396 + * We're also waiting for ENDRX before we start the next
14397 * transfer because we need to handle some difficult timing
14398 * issues otherwise. If we wait for ENDTX in one transfer and
14399 * then starts waiting for ENDRX in the next, it's difficult
14400 @@ -265,7 +196,17 @@
14401 *
14402 * It should be doable, though. Just not now...
14403 */
14404 + spi_writel(as, TNCR, 0);
14405 + spi_writel(as, RNCR, 0);
14406 spi_writel(as, IER, SPI_BIT(ENDRX) | SPI_BIT(OVRES));
14407 +
14408 + dev_dbg(&msg->spi->dev,
14409 + " start xfer %p: len %u tx %p/%08x rx %p/%08x imr %03x\n",
14410 + xfer, xfer->len, xfer->tx_buf, xfer->tx_dma,
14411 + xfer->rx_buf, xfer->rx_dma, spi_readl(as, IMR));
14412 +
14413 + spi_writel(as, RCR, len);
14414 + spi_writel(as, TCR, len);
14415 spi_writel(as, PTCR, SPI_BIT(TXTEN) | SPI_BIT(RXTEN));
14416 }
14417
14418 @@ -363,7 +304,6 @@
14419 spin_lock(&as->lock);
14420
14421 as->current_transfer = NULL;
14422 - as->next_transfer = NULL;
14423
14424 /* continue if needed */
14425 if (list_empty(&as->queue) || as->stopping)
14426 @@ -447,7 +387,7 @@
14427
14428 spi_writel(as, IDR, pending);
14429
14430 - if (as->current_remaining_bytes == 0) {
14431 + if (as->remaining_bytes == 0) {
14432 msg->actual_length += xfer->len;
14433
14434 if (!msg->is_dma_mapped)
14435 @@ -457,7 +397,7 @@
14436 if (xfer->delay_usecs)
14437 udelay(xfer->delay_usecs);
14438
14439 - if (atmel_spi_xfer_is_last(msg, xfer)) {
14440 + if (msg->transfers.prev == &xfer->transfer_list) {
14441 /* report completed message */
14442 atmel_spi_msg_done(master, as, msg, 0,
14443 xfer->cs_change);
14444 diff --exclude=.git -urN linux-2.6.25.6/drivers/usb/gadget/atmel_usba_udc.c avr32-2.6/drivers/usb/gadget/atmel_usba_udc.c
14445 --- linux-2.6.25.6/drivers/usb/gadget/atmel_usba_udc.c 2008-06-09 20:27:19.000000000 +0200
14446 +++ avr32-2.6/drivers/usb/gadget/atmel_usba_udc.c 2008-06-12 15:09:42.586816287 +0200
14447 @@ -18,6 +18,7 @@
14448 #include <linux/platform_device.h>
14449 #include <linux/usb/ch9.h>
14450 #include <linux/usb/gadget.h>
14451 +#include <linux/usb/atmel_usba_udc.h>
14452 #include <linux/delay.h>
14453
14454 #include <asm/gpio.h>
14455 @@ -27,6 +28,7 @@
14456
14457
14458 static struct usba_udc the_udc;
14459 +static struct usba_ep *usba_ep;
14460
14461 #ifdef CONFIG_USB_GADGET_DEBUG_FS
14462 #include <linux/debugfs.h>
14463 @@ -324,53 +326,28 @@
14464 return 1;
14465 }
14466
14467 -static void copy_to_fifo(void __iomem *fifo, const void *buf, int len)
14468 -{
14469 - unsigned long tmp;
14470 +#if defined(CONFIG_AVR32)
14471
14472 - DBG(DBG_FIFO, "copy to FIFO (len %d):\n", len);
14473 - for (; len > 0; len -= 4, buf += 4, fifo += 4) {
14474 - tmp = *(unsigned long *)buf;
14475 - if (len >= 4) {
14476 - DBG(DBG_FIFO, " -> %08lx\n", tmp);
14477 - __raw_writel(tmp, fifo);
14478 - } else {
14479 - do {
14480 - DBG(DBG_FIFO, " -> %02lx\n", tmp >> 24);
14481 - __raw_writeb(tmp >> 24, fifo);
14482 - fifo++;
14483 - tmp <<= 8;
14484 - } while (--len);
14485 - break;
14486 - }
14487 - }
14488 +static void toggle_bias(int is_on)
14489 +{
14490 }
14491
14492 -static void copy_from_fifo(void *buf, void __iomem *fifo, int len)
14493 +#elif defined(CONFIG_ARCH_AT91)
14494 +
14495 +#include <asm/arch/at91_pmc.h>
14496 +
14497 +static void toggle_bias(int is_on)
14498 {
14499 - union {
14500 - unsigned long *w;
14501 - unsigned char *b;
14502 - } p;
14503 - unsigned long tmp;
14504 -
14505 - DBG(DBG_FIFO, "copy from FIFO (len %d):\n", len);
14506 - for (p.w = buf; len > 0; len -= 4, p.w++, fifo += 4) {
14507 - if (len >= 4) {
14508 - tmp = __raw_readl(fifo);
14509 - *p.w = tmp;
14510 - DBG(DBG_FIFO, " -> %08lx\n", tmp);
14511 - } else {
14512 - do {
14513 - tmp = __raw_readb(fifo);
14514 - *p.b = tmp;
14515 - DBG(DBG_FIFO, " -> %02lx\n", tmp);
14516 - fifo++, p.b++;
14517 - } while (--len);
14518 - }
14519 - }
14520 + unsigned int uckr = at91_sys_read(AT91_CKGR_UCKR);
14521 +
14522 + if (is_on)
14523 + at91_sys_write(AT91_CKGR_UCKR, uckr | AT91_PMC_BIASEN);
14524 + else
14525 + at91_sys_write(AT91_CKGR_UCKR, uckr & ~(AT91_PMC_BIASEN));
14526 }
14527
14528 +#endif /* CONFIG_ARCH_AT91 */
14529 +
14530 static void next_fifo_transaction(struct usba_ep *ep, struct usba_request *req)
14531 {
14532 unsigned int transaction_len;
14533 @@ -387,7 +364,7 @@
14534 ep->ep.name, req, transaction_len,
14535 req->last_transaction ? ", done" : "");
14536
14537 - copy_to_fifo(ep->fifo, req->req.buf + req->req.actual, transaction_len);
14538 + memcpy_toio(ep->fifo, req->req.buf + req->req.actual, transaction_len);
14539 usba_ep_writel(ep, SET_STA, USBA_TX_PK_RDY);
14540 req->req.actual += transaction_len;
14541 }
14542 @@ -476,7 +453,7 @@
14543 bytecount = req->req.length - req->req.actual;
14544 }
14545
14546 - copy_from_fifo(req->req.buf + req->req.actual,
14547 + memcpy_fromio(req->req.buf + req->req.actual,
14548 ep->fifo, bytecount);
14549 req->req.actual += bytecount;
14550
14551 @@ -1029,33 +1006,6 @@
14552 .set_selfpowered = usba_udc_set_selfpowered,
14553 };
14554
14555 -#define EP(nam, idx, maxpkt, maxbk, dma, isoc) \
14556 -{ \
14557 - .ep = { \
14558 - .ops = &usba_ep_ops, \
14559 - .name = nam, \
14560 - .maxpacket = maxpkt, \
14561 - }, \
14562 - .udc = &the_udc, \
14563 - .queue = LIST_HEAD_INIT(usba_ep[idx].queue), \
14564 - .fifo_size = maxpkt, \
14565 - .nr_banks = maxbk, \
14566 - .index = idx, \
14567 - .can_dma = dma, \
14568 - .can_isoc = isoc, \
14569 -}
14570 -
14571 -static struct usba_ep usba_ep[] = {
14572 - EP("ep0", 0, 64, 1, 0, 0),
14573 - EP("ep1in-bulk", 1, 512, 2, 1, 1),
14574 - EP("ep2out-bulk", 2, 512, 2, 1, 1),
14575 - EP("ep3in-int", 3, 64, 3, 1, 0),
14576 - EP("ep4out-int", 4, 64, 3, 1, 0),
14577 - EP("ep5in-iso", 5, 1024, 3, 1, 1),
14578 - EP("ep6out-iso", 6, 1024, 3, 1, 1),
14579 -};
14580 -#undef EP
14581 -
14582 static struct usb_endpoint_descriptor usba_ep0_desc = {
14583 .bLength = USB_DT_ENDPOINT_SIZE,
14584 .bDescriptorType = USB_DT_ENDPOINT,
14585 @@ -1074,7 +1024,6 @@
14586 static struct usba_udc the_udc = {
14587 .gadget = {
14588 .ops = &usba_udc_ops,
14589 - .ep0 = &usba_ep[0].ep,
14590 .ep_list = LIST_HEAD_INIT(the_udc.gadget.ep_list),
14591 .is_dualspeed = 1,
14592 .name = "atmel_usba_udc",
14593 @@ -1231,7 +1180,7 @@
14594 } else {
14595 usba_ep_writel(ep, CTL_ENB, USBA_EPT_ENABLE);
14596 usba_writel(udc, TST, USBA_TST_PKT_MODE);
14597 - copy_to_fifo(ep->fifo, test_packet_buffer,
14598 + memcpy_toio(ep->fifo, test_packet_buffer,
14599 sizeof(test_packet_buffer));
14600 usba_ep_writel(ep, SET_STA, USBA_TX_PK_RDY);
14601 dev_info(dev, "Entering Test_Packet mode...\n");
14602 @@ -1530,13 +1479,13 @@
14603 DBG(DBG_HW, "Packet length: %u\n", pkt_len);
14604 if (pkt_len != sizeof(crq)) {
14605 pr_warning("udc: Invalid packet length %u "
14606 - "(expected %lu)\n", pkt_len, sizeof(crq));
14607 + "(expected %zu)\n", pkt_len, sizeof(crq));
14608 set_protocol_stall(udc, ep);
14609 return;
14610 }
14611
14612 DBG(DBG_FIFO, "Copying ctrl request from 0x%p:\n", ep->fifo);
14613 - copy_from_fifo(crq.data, ep->fifo, sizeof(crq));
14614 + memcpy_fromio(crq.data, ep->fifo, sizeof(crq));
14615
14616 /* Free up one bank in the FIFO so that we can
14617 * generate or receive a reply right away. */
14618 @@ -1688,6 +1637,7 @@
14619 DBG(DBG_INT, "irq, status=%#08x\n", status);
14620
14621 if (status & USBA_DET_SUSPEND) {
14622 + toggle_bias(0);
14623 usba_writel(udc, INT_CLR, USBA_DET_SUSPEND);
14624 DBG(DBG_BUS, "Suspend detected\n");
14625 if (udc->gadget.speed != USB_SPEED_UNKNOWN
14626 @@ -1699,6 +1649,7 @@
14627 }
14628
14629 if (status & USBA_WAKE_UP) {
14630 + toggle_bias(1);
14631 usba_writel(udc, INT_CLR, USBA_WAKE_UP);
14632 DBG(DBG_BUS, "Wake Up CPU detected\n");
14633 }
14634 @@ -1792,12 +1743,14 @@
14635 vbus = gpio_get_value(udc->vbus_pin);
14636 if (vbus != udc->vbus_prev) {
14637 if (vbus) {
14638 - usba_writel(udc, CTRL, USBA_EN_USBA);
14639 + toggle_bias(1);
14640 + usba_writel(udc, CTRL, USBA_ENABLE_MASK);
14641 usba_writel(udc, INT_ENB, USBA_END_OF_RESET);
14642 } else {
14643 udc->gadget.speed = USB_SPEED_UNKNOWN;
14644 reset_all_endpoints(udc);
14645 - usba_writel(udc, CTRL, 0);
14646 + toggle_bias(0);
14647 + usba_writel(udc, CTRL, USBA_DISABLE_MASK);
14648 spin_unlock(&udc->lock);
14649 udc->driver->disconnect(&udc->gadget);
14650 spin_lock(&udc->lock);
14651 @@ -1850,7 +1803,8 @@
14652 /* If Vbus is present, enable the controller and wait for reset */
14653 spin_lock_irqsave(&udc->lock, flags);
14654 if (vbus_is_present(udc) && udc->vbus_prev == 0) {
14655 - usba_writel(udc, CTRL, USBA_EN_USBA);
14656 + toggle_bias(1);
14657 + usba_writel(udc, CTRL, USBA_ENABLE_MASK);
14658 usba_writel(udc, INT_ENB, USBA_END_OF_RESET);
14659 }
14660 spin_unlock_irqrestore(&udc->lock, flags);
14661 @@ -1883,7 +1837,8 @@
14662 spin_unlock_irqrestore(&udc->lock, flags);
14663
14664 /* This will also disable the DP pullup */
14665 - usba_writel(udc, CTRL, 0);
14666 + toggle_bias(0);
14667 + usba_writel(udc, CTRL, USBA_DISABLE_MASK);
14668
14669 driver->unbind(&udc->gadget);
14670 udc->gadget.dev.driver = NULL;
14671 @@ -1908,7 +1863,7 @@
14672
14673 regs = platform_get_resource(pdev, IORESOURCE_MEM, CTRL_IOMEM_ID);
14674 fifo = platform_get_resource(pdev, IORESOURCE_MEM, FIFO_IOMEM_ID);
14675 - if (!regs || !fifo)
14676 + if (!regs || !fifo || !pdata)
14677 return -ENXIO;
14678
14679 irq = platform_get_irq(pdev, 0);
14680 @@ -1953,19 +1908,48 @@
14681
14682 /* Make sure we start from a clean slate */
14683 clk_enable(pclk);
14684 - usba_writel(udc, CTRL, 0);
14685 + toggle_bias(0);
14686 + usba_writel(udc, CTRL, USBA_DISABLE_MASK);
14687 clk_disable(pclk);
14688
14689 + usba_ep = kmalloc(sizeof(struct usba_ep) * pdata->num_ep,
14690 + GFP_KERNEL);
14691 + if (!usba_ep)
14692 + goto err_alloc_ep;
14693 +
14694 + the_udc.gadget.ep0 = &usba_ep[0].ep;
14695 +
14696 INIT_LIST_HEAD(&usba_ep[0].ep.ep_list);
14697 usba_ep[0].ep_regs = udc->regs + USBA_EPT_BASE(0);
14698 usba_ep[0].dma_regs = udc->regs + USBA_DMA_BASE(0);
14699 usba_ep[0].fifo = udc->fifo + USBA_FIFO_BASE(0);
14700 - for (i = 1; i < ARRAY_SIZE(usba_ep); i++) {
14701 + usba_ep[0].ep.ops = &usba_ep_ops;
14702 + usba_ep[0].ep.name = pdata->ep[0].name;
14703 + usba_ep[0].ep.maxpacket = pdata->ep[0].fifo_size;
14704 + usba_ep[0].udc = &the_udc;
14705 + INIT_LIST_HEAD(&usba_ep[0].queue);
14706 + usba_ep[0].fifo_size = pdata->ep[0].fifo_size;
14707 + usba_ep[0].nr_banks = pdata->ep[0].nr_banks;
14708 + usba_ep[0].index = pdata->ep[0].index;
14709 + usba_ep[0].can_dma = pdata->ep[0].can_dma;
14710 + usba_ep[0].can_isoc = pdata->ep[0].can_isoc;
14711 +
14712 + for (i = 1; i < pdata->num_ep; i++) {
14713 struct usba_ep *ep = &usba_ep[i];
14714
14715 ep->ep_regs = udc->regs + USBA_EPT_BASE(i);
14716 ep->dma_regs = udc->regs + USBA_DMA_BASE(i);
14717 ep->fifo = udc->fifo + USBA_FIFO_BASE(i);
14718 + ep->ep.ops = &usba_ep_ops;
14719 + ep->ep.name = pdata->ep[i].name;
14720 + ep->ep.maxpacket = pdata->ep[i].fifo_size;
14721 + ep->udc = &the_udc;
14722 + INIT_LIST_HEAD(&ep->queue);
14723 + ep->fifo_size = pdata->ep[i].fifo_size;
14724 + ep->nr_banks = pdata->ep[i].nr_banks;
14725 + ep->index = pdata->ep[i].index;
14726 + ep->can_dma = pdata->ep[i].can_dma;
14727 + ep->can_isoc = pdata->ep[i].can_isoc;
14728
14729 list_add_tail(&ep->ep.ep_list, &udc->gadget.ep_list);
14730 }
14731 @@ -1984,7 +1968,7 @@
14732 goto err_device_add;
14733 }
14734
14735 - if (pdata && pdata->vbus_pin != GPIO_PIN_NONE) {
14736 + if (pdata->vbus_pin >= 0) {
14737 if (!gpio_request(pdata->vbus_pin, "atmel_usba_udc")) {
14738 udc->vbus_pin = pdata->vbus_pin;
14739
14740 @@ -2004,7 +1988,7 @@
14741 }
14742
14743 usba_init_debugfs(udc);
14744 - for (i = 1; i < ARRAY_SIZE(usba_ep); i++)
14745 + for (i = 1; i < pdata->num_ep; i++)
14746 usba_ep_init_debugfs(udc, &usba_ep[i]);
14747
14748 return 0;
14749 @@ -2012,6 +1996,8 @@
14750 err_device_add:
14751 free_irq(irq, udc);
14752 err_request_irq:
14753 + kfree(usba_ep);
14754 +err_alloc_ep:
14755 iounmap(udc->fifo);
14756 err_map_fifo:
14757 iounmap(udc->regs);
14758 @@ -2029,10 +2015,11 @@
14759 {
14760 struct usba_udc *udc;
14761 int i;
14762 + struct usba_platform_data *pdata = pdev->dev.platform_data;
14763
14764 udc = platform_get_drvdata(pdev);
14765
14766 - for (i = 1; i < ARRAY_SIZE(usba_ep); i++)
14767 + for (i = 1; i < pdata->num_ep; i++)
14768 usba_ep_cleanup_debugfs(&usba_ep[i]);
14769 usba_cleanup_debugfs(udc);
14770
14771 @@ -2040,6 +2027,7 @@
14772 gpio_free(udc->vbus_pin);
14773
14774 free_irq(udc->irq, udc);
14775 + kfree(usba_ep);
14776 iounmap(udc->fifo);
14777 iounmap(udc->regs);
14778 clk_put(udc->hclk);
14779 diff --exclude=.git -urN linux-2.6.25.6/drivers/usb/gadget/atmel_usba_udc.h avr32-2.6/drivers/usb/gadget/atmel_usba_udc.h
14780 --- linux-2.6.25.6/drivers/usb/gadget/atmel_usba_udc.h 2008-06-09 20:27:19.000000000 +0200
14781 +++ avr32-2.6/drivers/usb/gadget/atmel_usba_udc.h 2008-06-12 15:04:02.443815949 +0200
14782 @@ -41,6 +41,15 @@
14783 #define USBA_EN_USBA (1 << 8)
14784 #define USBA_DETACH (1 << 9)
14785 #define USBA_REMOTE_WAKE_UP (1 << 10)
14786 +#define USBA_PULLD_DIS (1 << 11)
14787 +
14788 +#if defined(CONFIG_AVR32)
14789 +#define USBA_ENABLE_MASK USBA_EN_USBA
14790 +#define USBA_DISABLE_MASK 0
14791 +#elif defined(CONFIG_ARCH_AT91)
14792 +#define USBA_ENABLE_MASK (USBA_EN_USBA | USBA_PULLD_DIS)
14793 +#define USBA_DISABLE_MASK USBA_DETACH
14794 +#endif /* CONFIG_ARCH_AT91 */
14795
14796 /* Bitfields in FNUM */
14797 #define USBA_MICRO_FRAME_NUM_OFFSET 0
14798 diff --exclude=.git -urN linux-2.6.25.6/drivers/usb/gadget/Kconfig avr32-2.6/drivers/usb/gadget/Kconfig
14799 --- linux-2.6.25.6/drivers/usb/gadget/Kconfig 2008-06-09 20:27:19.000000000 +0200
14800 +++ avr32-2.6/drivers/usb/gadget/Kconfig 2008-06-12 15:09:42.582816057 +0200
14801 @@ -118,10 +118,10 @@
14802 config USB_GADGET_ATMEL_USBA
14803 boolean "Atmel USBA"
14804 select USB_GADGET_DUALSPEED
14805 - depends on AVR32
14806 + depends on AVR32 || ARCH_AT91CAP9
14807 help
14808 USBA is the integrated high-speed USB Device controller on
14809 - the AT32AP700x processors from Atmel.
14810 + the AT32AP700x and AT91CAP9 processors from Atmel.
14811
14812 config USB_ATMEL_USBA
14813 tristate
14814 diff --exclude=.git -urN linux-2.6.25.6/drivers/video/atmel_lcdfb.c avr32-2.6/drivers/video/atmel_lcdfb.c
14815 --- linux-2.6.25.6/drivers/video/atmel_lcdfb.c 2008-06-09 20:27:19.000000000 +0200
14816 +++ avr32-2.6/drivers/video/atmel_lcdfb.c 2008-06-12 15:09:43.343816340 +0200
14817 @@ -38,7 +38,9 @@
14818 #endif
14819
14820 #if defined(CONFIG_ARCH_AT91)
14821 -#define ATMEL_LCDFB_FBINFO_DEFAULT FBINFO_DEFAULT
14822 +#define ATMEL_LCDFB_FBINFO_DEFAULT (FBINFO_DEFAULT \
14823 + | FBINFO_PARTIAL_PAN_OK \
14824 + | FBINFO_HWACCEL_YPAN)
14825
14826 static inline void atmel_lcdfb_update_dma2d(struct atmel_lcdfb_info *sinfo,
14827 struct fb_var_screeninfo *var)
14828 @@ -176,7 +178,7 @@
14829 .type = FB_TYPE_PACKED_PIXELS,
14830 .visual = FB_VISUAL_TRUECOLOR,
14831 .xpanstep = 0,
14832 - .ypanstep = 0,
14833 + .ypanstep = 1,
14834 .ywrapstep = 0,
14835 .accel = FB_ACCEL_NONE,
14836 };
14837 @@ -250,6 +252,8 @@
14838 return -ENOMEM;
14839 }
14840
14841 + memset(info->screen_base, 0, info->fix.smem_len);
14842 +
14843 return 0;
14844 }
14845
14846 @@ -634,7 +638,6 @@
14847 struct fb_info *info = sinfo->info;
14848 int ret = 0;
14849
14850 - memset_io(info->screen_base, 0, info->fix.smem_len);
14851 info->var.activate |= FB_ACTIVATE_FORCE | FB_ACTIVATE_NOW;
14852
14853 dev_info(info->device,
14854 @@ -764,6 +767,11 @@
14855 info->screen_base = ioremap(info->fix.smem_start, info->fix.smem_len);
14856 if (!info->screen_base)
14857 goto release_intmem;
14858 +
14859 + /*
14860 + * Don't clear the framebuffer -- someone may have set
14861 + * up a splash image.
14862 + */
14863 } else {
14864 /* alocate memory buffer */
14865 ret = atmel_lcdfb_alloc_video_memory(sinfo);
14866 diff --exclude=.git -urN linux-2.6.25.6/fs/fs-writeback.c avr32-2.6/fs/fs-writeback.c
14867 --- linux-2.6.25.6/fs/fs-writeback.c 2008-06-09 20:27:19.000000000 +0200
14868 +++ avr32-2.6/fs/fs-writeback.c 2008-06-12 15:09:44.531816544 +0200
14869 @@ -385,8 +385,6 @@
14870 * WB_SYNC_HOLD is a hack for sys_sync(): reattach the inode to sb->s_dirty so
14871 * that it can be located for waiting on in __writeback_single_inode().
14872 *
14873 - * Called under inode_lock.
14874 - *
14875 * If `bdi' is non-zero then we're being asked to writeback a specific queue.
14876 * This function assumes that the blockdev superblock's inodes are backed by
14877 * a variety of queues, so all inodes are searched. For other superblocks,
14878 @@ -402,11 +400,12 @@
14879 * on the writer throttling path, and we get decent balancing between many
14880 * throttled threads: we don't want them all piling up on inode_sync_wait.
14881 */
14882 -static void
14883 -sync_sb_inodes(struct super_block *sb, struct writeback_control *wbc)
14884 +void generic_sync_sb_inodes(struct super_block *sb,
14885 + struct writeback_control *wbc)
14886 {
14887 const unsigned long start = jiffies; /* livelock avoidance */
14888
14889 + spin_lock(&inode_lock);
14890 if (!wbc->for_kupdate || list_empty(&sb->s_io))
14891 queue_io(sb, wbc->older_than_this);
14892
14893 @@ -485,8 +484,16 @@
14894 if (!list_empty(&sb->s_more_io))
14895 wbc->more_io = 1;
14896 }
14897 + spin_unlock(&inode_lock);
14898 return; /* Leave any unwritten inodes on s_io */
14899 }
14900 +EXPORT_SYMBOL_GPL(generic_sync_sb_inodes);
14901 +
14902 +static void sync_sb_inodes(struct super_block *sb,
14903 + struct writeback_control *wbc)
14904 +{
14905 + generic_sync_sb_inodes(sb, wbc);
14906 +}
14907
14908 /*
14909 * Start writeback of dirty pagecache data against all unlocked inodes.
14910 @@ -526,11 +533,8 @@
14911 * be unmounted by the time it is released.
14912 */
14913 if (down_read_trylock(&sb->s_umount)) {
14914 - if (sb->s_root) {
14915 - spin_lock(&inode_lock);
14916 + if (sb->s_root)
14917 sync_sb_inodes(sb, wbc);
14918 - spin_unlock(&inode_lock);
14919 - }
14920 up_read(&sb->s_umount);
14921 }
14922 spin_lock(&sb_lock);
14923 @@ -568,9 +572,7 @@
14924 (inodes_stat.nr_inodes - inodes_stat.nr_unused) +
14925 nr_dirty + nr_unstable;
14926 wbc.nr_to_write += wbc.nr_to_write / 2; /* Bit more for luck */
14927 - spin_lock(&inode_lock);
14928 sync_sb_inodes(sb, &wbc);
14929 - spin_unlock(&inode_lock);
14930 }
14931
14932 /*
14933 diff --exclude=.git -urN linux-2.6.25.6/fs/Kconfig avr32-2.6/fs/Kconfig
14934 --- linux-2.6.25.6/fs/Kconfig 2008-06-09 20:27:19.000000000 +0200
14935 +++ avr32-2.6/fs/Kconfig 2008-06-12 15:09:43.915816293 +0200
14936 @@ -1347,6 +1347,9 @@
14937
14938 endchoice
14939
14940 +# UBIFS File system configuration
14941 +source "fs/ubifs/Kconfig"
14942 +
14943 config CRAMFS
14944 tristate "Compressed ROM file system support (cramfs)"
14945 depends on BLOCK
14946 diff --exclude=.git -urN linux-2.6.25.6/fs/Makefile avr32-2.6/fs/Makefile
14947 --- linux-2.6.25.6/fs/Makefile 2008-06-09 20:27:19.000000000 +0200
14948 +++ avr32-2.6/fs/Makefile 2008-06-12 15:09:43.915816293 +0200
14949 @@ -100,6 +100,7 @@
14950 obj-$(CONFIG_UFS_FS) += ufs/
14951 obj-$(CONFIG_EFS_FS) += efs/
14952 obj-$(CONFIG_JFFS2_FS) += jffs2/
14953 +obj-$(CONFIG_UBIFS_FS) += ubifs/
14954 obj-$(CONFIG_AFFS_FS) += affs/
14955 obj-$(CONFIG_ROMFS_FS) += romfs/
14956 obj-$(CONFIG_QNX4FS_FS) += qnx4/
14957 diff --exclude=.git -urN linux-2.6.25.6/fs/ubifs/budget.c avr32-2.6/fs/ubifs/budget.c
14958 --- linux-2.6.25.6/fs/ubifs/budget.c 1970-01-01 01:00:00.000000000 +0100
14959 +++ avr32-2.6/fs/ubifs/budget.c 2008-06-12 15:09:45.311815896 +0200
14960 @@ -0,0 +1,863 @@
14961 +/*
14962 + * This file is part of UBIFS.
14963 + *
14964 + * Copyright (C) 2006-2008 Nokia Corporation.
14965 + *
14966 + * This program is free software; you can redistribute it and/or modify it
14967 + * under the terms of the GNU General Public License version 2 as published by
14968 + * the Free Software Foundation.
14969 + *
14970 + * This program is distributed in the hope that it will be useful, but WITHOUT
14971 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14972 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14973 + * more details.
14974 + *
14975 + * You should have received a copy of the GNU General Public License along with
14976 + * this program; if not, write to the Free Software Foundation, Inc., 51
14977 + * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
14978 + *
14979 + * Authors: Adrian Hunter
14980 + * Artem Bityutskiy (Битюцкий Артём)
14981 + */
14982 +
14983 +/*
14984 + * This file implements the budgeting unit which is responsible for UBIFS space
14985 + * management.
14986 + *
14987 + * Factors such as compression, wasted space at the ends of LEBs, space in other
14988 + * journal heads, the effect of updates on the index, and so on, make it
14989 + * impossible to accurately predict the amount of space needed. Consequently
14990 + * approximations are used.
14991 + */
14992 +
14993 +#include "ubifs.h"
14994 +#include <linux/writeback.h>
14995 +#include <asm/div64.h>
14996 +
14997 +/*
14998 + * When pessimistic budget calculations say that there is no enough space,
14999 + * UBIFS starts writing back dirty inodes and pages, doing garbage collection,
15000 + * or committing. The below constants define maximum number of times UBIFS
15001 + * repeats the operations.
15002 + */
15003 +#define MAX_SHRINK_RETRIES 8
15004 +#define MAX_GC_RETRIES 4
15005 +#define MAX_CMT_RETRIES 2
15006 +#define MAX_NOSPC_RETRIES 1
15007 +
15008 +/*
15009 + * The below constant defines amount of dirty pages which should be written
15010 + * back at when trying to shrink the liability.
15011 + */
15012 +#define NR_TO_WRITE 16
15013 +
15014 +/**
15015 + * struct retries_info - information about re-tries while making free space.
15016 + * @prev_liability: previous liability
15017 + * @shrink_cnt: how many times the liability was shrinked
15018 + * @shrink_retries: count of liability shrink re-tries (increased when
15019 + * liability does not shrink)
15020 + * @try_gc: GC should be tried first
15021 + * @gc_retries: how many times GC was run
15022 + * @cmt_retries: how many times commit has been done
15023 + * @nospc_retries: how many times GC returned %-ENOSPC
15024 + *
15025 + * Since we consider budgeting to be the fast-path, and this structure has to
15026 + * be allocated on stack and zeroed out, we make it smaller using bit-fields.
15027 + */
15028 +struct retries_info {
15029 + long long prev_liability;
15030 + unsigned int shrink_cnt;
15031 + unsigned int shrink_retries:5;
15032 + unsigned int try_gc:1;
15033 + unsigned int gc_retries:4;
15034 + unsigned int cmt_retries:3;
15035 + unsigned int nospc_retries:1;
15036 +};
15037 +
15038 +/**
15039 + * shrink_liability - write-back some dirty pages/inodes.
15040 + * @c: UBIFS file-system description object
15041 + * @nr_to_write: how many dirty pages to write-back
15042 + *
15043 + * This function shrinks UBIFS liability by means of writing back some amount
15044 + * of dirty inodes and their pages. Returns the amount of pages which were
15045 + * written back. The returned value does not include dirty inodes which were
15046 + * synchronized.
15047 + *
15048 + * Note, this function synchronizes even VFS inodes which are locked
15049 + * (@i_mutex) by the caller of the budgeting function, because write-back does
15050 + * not touch @i_mutex.
15051 + */
15052 +static int shrink_liability(struct ubifs_info *c, int nr_to_write)
15053 +{
15054 + struct writeback_control wbc = {
15055 + .sync_mode = WB_SYNC_NONE,
15056 + .range_end = LLONG_MAX,
15057 + .nr_to_write = nr_to_write,
15058 + };
15059 +
15060 + generic_sync_sb_inodes(c->vfs_sb, &wbc);
15061 + dbg_budg("%ld pages were written back", nr_to_write - wbc.nr_to_write);
15062 + return nr_to_write - wbc.nr_to_write;
15063 +}
15064 +
15065 +
15066 +/**
15067 + * run_gc - run garbage collector.
15068 + * @c: UBIFS file-system description object
15069 + *
15070 + * This function runs garbage collector to make some more free space. Returns
15071 + * zero if a free LEB has been produced, %-EAGAIN if commit is required, and a
15072 + * negative error code in case of failure.
15073 + */
15074 +static int run_gc(struct ubifs_info *c)
15075 +{
15076 + int err, lnum;
15077 +
15078 + /* Make some free space by garbage-collecting dirty space */
15079 + down_read(&c->commit_sem);
15080 + lnum = ubifs_garbage_collect(c, 1);
15081 + up_read(&c->commit_sem);
15082 + if (lnum < 0)
15083 + return lnum;
15084 +
15085 + /* GC freed one LEB, return it to lprops */
15086 + dbg_budg("GC freed LEB %d", lnum);
15087 + err = ubifs_return_leb(c, lnum);
15088 + if (err)
15089 + return err;
15090 +
15091 + return 0;
15092 +}
15093 +
15094 +/**
15095 + * make_free_space - make more free space on the file-system.
15096 + * @c: UBIFS file-system description object
15097 + * @ri: information about previous invocations of this function
15098 + *
15099 + * This function is called when an operation cannot be budgeted because there
15100 + * is supposedly no free space. But in most cases there is some free space:
15101 + * o budgeting is pessimistic, so it always budgets more then it is actually
15102 + * needed, so shrinking the liability is one way to make free space - the
15103 + * cached data will take less space then it was budgeted for;
15104 + * o GC may turn some dark space into free space (budgeting treats dark space
15105 + * as not available);
15106 + * o commit may free some LEB, i.e., turn freeable LEBs into free LEBs.
15107 + *
15108 + * So this function tries to do the above. Returns %-EAGAIN if some free space
15109 + * was presumably made and the caller has to re-try budgeting the operation.
15110 + * Returns %-ENOSPC if it couldn't do more free space, and other negative error
15111 + * codes on failures.
15112 + */
15113 +static int make_free_space(struct ubifs_info *c, struct retries_info *ri)
15114 +{
15115 + int err;
15116 +
15117 + /*
15118 + * If we have some dirty pages and inodes (liability), try to write
15119 + * them back unless this was tried too many times without effect
15120 + * already.
15121 + */
15122 + if (ri->shrink_retries < MAX_SHRINK_RETRIES && !ri->try_gc) {
15123 + long long liability;
15124 +
15125 + spin_lock(&c->space_lock);
15126 + liability = c->budg_idx_growth + c->budg_data_growth +
15127 + c->budg_dd_growth;
15128 + spin_unlock(&c->space_lock);
15129 +
15130 + if (ri->prev_liability >= liability) {
15131 + /* Liability does not shrink, next time try GC then */
15132 + ri->shrink_retries += 1;
15133 + if (ri->gc_retries < MAX_GC_RETRIES)
15134 + ri->try_gc = 1;
15135 + dbg_budg("liability did not shrink: retries %d of %d",
15136 + ri->shrink_retries, MAX_SHRINK_RETRIES);
15137 + }
15138 +
15139 + dbg_budg("force write-back (count %d)", ri->shrink_cnt);
15140 + shrink_liability(c, NR_TO_WRITE + ri->shrink_cnt);
15141 +
15142 + ri->prev_liability = liability;
15143 + ri->shrink_cnt += 1;
15144 + return -EAGAIN;
15145 + }
15146 +
15147 + /*
15148 + * Try to run garbage collector unless it was already tried too many
15149 + * times.
15150 + */
15151 + if (ri->gc_retries < MAX_GC_RETRIES) {
15152 + ri->gc_retries += 1;
15153 + dbg_budg("run GC, retries %d of %d",
15154 + ri->gc_retries, MAX_GC_RETRIES);
15155 +
15156 + ri->try_gc = 0;
15157 + err = run_gc(c);
15158 + if (!err)
15159 + return -EAGAIN;
15160 +
15161 + if (err == -EAGAIN) {
15162 + dbg_budg("GC asked to commit");
15163 + err = ubifs_run_commit(c);
15164 + if (err)
15165 + return err;
15166 + return -EAGAIN;
15167 + }
15168 +
15169 + if (err != -ENOSPC)
15170 + return err;
15171 +
15172 + /*
15173 + * GC could not make any progress. If this is the first time,
15174 + * then it makes sense to try to commit, because it might make
15175 + * some dirty space.
15176 + */
15177 + dbg_budg("GC returned -ENOSPC, retries %d",
15178 + ri->nospc_retries);
15179 + if (ri->nospc_retries >= MAX_NOSPC_RETRIES)
15180 + return err;
15181 + ri->nospc_retries += 1;
15182 + }
15183 +
15184 + /* Neither GC nor write-back helped, try to commit */
15185 + if (ri->cmt_retries < MAX_CMT_RETRIES) {
15186 + ri->cmt_retries += 1;
15187 + dbg_budg("run commit, retries %d of %d",
15188 + ri->cmt_retries, MAX_CMT_RETRIES);
15189 + err = ubifs_run_commit(c);
15190 + if (err)
15191 + return err;
15192 + return -EAGAIN;
15193 + }
15194 +
15195 + return -ENOSPC;
15196 +}
15197 +
15198 +/**
15199 + * ubifs_calc_min_idx_lebs - calculate amount of eraseblocks for the index.
15200 + * @c: UBIFS file-system description object
15201 + *
15202 + * This function calculates and returns the number of eraseblocks which should
15203 + * be kept for index usage.
15204 + */
15205 +int ubifs_calc_min_idx_lebs(struct ubifs_info *c)
15206 +{
15207 + int ret;
15208 + uint64_t idx_size;
15209 +
15210 + idx_size = c->old_idx_sz + c->budg_idx_growth + c->budg_uncommitted_idx;
15211 +
15212 + /* And make sure we have twice the index size of space reserved */
15213 + idx_size <<= 1;
15214 +
15215 + /*
15216 + * We do not maintain 'old_idx_size' as 'old_idx_lebs'/'old_idx_bytes'
15217 + * pair, nor similarly the two variables for the new index size, so we
15218 + * have to do this costly 64-bit division on fast-path.
15219 + */
15220 + if (do_div(idx_size, c->leb_size - c->max_idx_node_sz))
15221 + ret = idx_size + 1;
15222 + else
15223 + ret = idx_size;
15224 + /*
15225 + * The index head is not available for the in-the-gaps method, so add an
15226 + * extra LEB to compensate.
15227 + */
15228 + ret += 1;
15229 + /*
15230 + * At present the index needs at least 2 LEBs: one for the index head
15231 + * and one for in-the-gaps method (which currently does not cater for
15232 + * the index head and so excludes it from consideration).
15233 + */
15234 + if (ret < 2)
15235 + ret = 2;
15236 + return ret;
15237 +}
15238 +
15239 +/**
15240 + * ubifs_calc_available - calculate available FS space.
15241 + * @c: UBIFS file-system description object
15242 + *
15243 + * This function calculates and returns amount of FS space available for use.
15244 + */
15245 +long long ubifs_calc_available(const struct ubifs_info *c)
15246 +{
15247 + long long available, subtract_lebs;
15248 +
15249 + /*
15250 + * Force the amount available to the total size reported if the used
15251 + * space is zero.
15252 + */
15253 + if (c->lst.total_used <= UBIFS_INO_NODE_SZ &&
15254 + c->budg_data_growth + c->budg_dd_growth == 0) {
15255 + /* Do the same calculation as for c->block_cnt */
15256 + available = c->main_lebs - 2;
15257 + available *= c->leb_size - c->dark_wm;
15258 + return available;
15259 + }
15260 +
15261 + available = c->main_bytes - c->lst.total_used;
15262 +
15263 + /*
15264 + * Now 'available' contains theoretically available flash space
15265 + * assuming there is no index, so we have to subtract the space which
15266 + * is reserved for the index.
15267 + */
15268 + subtract_lebs = c->min_idx_lebs;
15269 +
15270 + /* Take into account that GC reserves one LEB for its own needs */
15271 + subtract_lebs += 1;
15272 +
15273 + /*
15274 + * The GC journal head LEB is not really accessible. And since
15275 + * different write types go to different heads, we may count only on
15276 + * one head's space.
15277 + */
15278 + subtract_lebs += c->jhead_cnt - 1;
15279 +
15280 + /* We also reserve one LEB for deletions, which bypass budgeting */
15281 + subtract_lebs += 1;
15282 +
15283 + available -= subtract_lebs * c->leb_size;
15284 +
15285 + /* Subtract the dead space which is not available for use */
15286 + available -= c->lst.total_dead;
15287 +
15288 + /*
15289 + * Subtract dark space, which might or might not be usable - it depends
15290 + * on the data which we have on the media and which will be written. If
15291 + * this is a lot of uncompressed or not-compressible data, the dark
15292 + * space cannot be used.
15293 + */
15294 + available -= c->lst.total_dark;
15295 +
15296 + /*
15297 + * However, there is more dark space. The index may be bigger than
15298 + * min_idx_lebs. Those extra LEBs are assumed to be available, but
15299 + * their dark space is not included in total_dark, so it is subtracted
15300 + * here.
15301 + */
15302 + if (c->lst.idx_lebs > c->min_idx_lebs) {
15303 + subtract_lebs = c->lst.idx_lebs - c->min_idx_lebs;
15304 + available -= subtract_lebs * c->dark_wm;
15305 + }
15306 +
15307 + /* The calculations are rough and may end up with a negative number */
15308 + return available > 0 ? available : 0;
15309 +}
15310 +
15311 +/**
15312 + * rp_can_write - check whether the user is allowed to write.
15313 + * @c: UBIFS file-system description object
15314 + * @avail: available space on FS
15315 + *
15316 + * UBIFS has so-called "reserved pool" which is flash space reserved
15317 + * for the superuser and for uses whose UID/GID is recorded in UBIFS superblock.
15318 + * This function checks whether current user is allowed to write
15319 + * to the file-system - it returns %1 if there is plenty of space or the user
15320 + * is eligible to use the reserved pool and %0 otherwise.
15321 + */
15322 +static int rp_can_write(struct ubifs_info *c, long long avail)
15323 +{
15324 + if (avail > c->rp_size || current->fsuid == c->rp_uid ||
15325 + capable(CAP_SYS_RESOURCE) ||
15326 + (c->rp_gid != 0 && in_group_p(c->rp_gid)))
15327 + return 1;
15328 +
15329 + return 0;
15330 +}
15331 +
15332 +/**
15333 + * do_budget_space - reserve flash space for index and data growth.
15334 + * @c: UBIFS file-system description object
15335 + *
15336 + * This function makes sure UBIFS has enough free eraseblocks for index growth
15337 + * and data.
15338 + *
15339 + * When budgeting index space, UBIFS reserves twice as more LEBs as the index
15340 + * would take if it was consolidated and written to the flash. This guarantees
15341 + * that the "in-the-gaps" commit method always succeeds and UBIFS will always
15342 + * be able to commit dirty index. So this function basically adds amount of
15343 + * budgeted index space to the size of the current index, multiplies this by 2,
15344 + * and makes sure this does not exceed the amount of free eraseblocks.
15345 + *
15346 + * Notes about @c->min_idx_lebs and @c->lst.idx_lebs variables:
15347 + * o @c->lst.idx_lebs is the number of LEBs the index currently uses. It might
15348 + * be large, because UBIFS does not do any index consolidation as long as
15349 + * there is free space. IOW, the index may take a lot of LEBs, but the LEBs
15350 + * will contain a lot of dirt.
15351 + * o @c->min_idx_lebs is the the index presumably takes. IOW, the index may be
15352 + * consolidated to take up to @c->min_idx_lebs LEBs.
15353 + *
15354 + * This function returns zero in case of success, and %-ENOSPC in case of
15355 + * failure.
15356 + */
15357 +static int do_budget_space(struct ubifs_info *c)
15358 +{
15359 + long long outstanding, available;
15360 + int lebs, rsvd_idx_lebs, min_idx_lebs;
15361 +
15362 + /* First budget index space */
15363 + min_idx_lebs = ubifs_calc_min_idx_lebs(c);
15364 +
15365 + /* Now 'min_idx_lebs' contains number of LEBs to reserve */
15366 + if (min_idx_lebs > c->lst.idx_lebs)
15367 + rsvd_idx_lebs = min_idx_lebs - c->lst.idx_lebs;
15368 + else
15369 + rsvd_idx_lebs = 0;
15370 +
15371 + /*
15372 + * The number of LEBs that are available to be used by the index is:
15373 + *
15374 + * @c->lst.empty_lebs + @c->freeable_cnt + @c->idx_gc_cnt -
15375 + * @c->lst.taken_empty_lebs
15376 + *
15377 + * @empty_lebs are available because they are empty. @freeable_cnt are
15378 + * available because they contain only free and dirty space and the
15379 + * index allocation always occurs after wbufs are synch'ed.
15380 + * @idx_gc_cnt are available because they are index LEBs that have been
15381 + * garbage collected (including trivial GC) and are awaiting the commit
15382 + * before they can be unmapped - note that the in-the-gaps method will
15383 + * grab these if it needs them. @taken_empty_lebs are empty_lebs that
15384 + * have already been allocated for some purpose (also includes those
15385 + * LEBs on the @idx_gc list).
15386 + *
15387 + * Note, @taken_empty_lebs may temporarily be higher by one because of
15388 + * the way we serialize LEB allocations and budgeting. See a comment in
15389 + * 'ubifs_find_free_space()'.
15390 + */
15391 + lebs = c->lst.empty_lebs + c->freeable_cnt + c->idx_gc_cnt -
15392 + c->lst.taken_empty_lebs;
15393 + if (unlikely(rsvd_idx_lebs > lebs)) {
15394 + dbg_budg("out of indexing space: min_idx_lebs %d (old %d), "
15395 + "rsvd_idx_lebs %d", min_idx_lebs, c->min_idx_lebs,
15396 + rsvd_idx_lebs);
15397 + return -ENOSPC;
15398 + }
15399 +
15400 + available = ubifs_calc_available(c);
15401 + outstanding = c->budg_data_growth + c->budg_dd_growth;
15402 +
15403 + if (unlikely(available < outstanding)) {
15404 + dbg_budg("out of data space: available %lld, outstanding %lld",
15405 + available, outstanding);
15406 + return -ENOSPC;
15407 + }
15408 +
15409 + if (!rp_can_write(c, available - outstanding))
15410 + return -ENOSPC;
15411 +
15412 + c->min_idx_lebs = min_idx_lebs;
15413 + return 0;
15414 +}
15415 +
15416 +/**
15417 + * calc_idx_growth - calculate approximate index growth from budgeting request.
15418 + * @c: UBIFS file-system description object
15419 + * @req: budgeting request
15420 + *
15421 + * For now we assume each new node adds one znode. But this is rather poor
15422 + * approximation, though.
15423 + */
15424 +static int calc_idx_growth(const struct ubifs_info *c,
15425 + const struct ubifs_budget_req *req)
15426 +{
15427 + int znodes;
15428 +
15429 + znodes = req->new_ino + (req->new_page << UBIFS_BLOCKS_PER_PAGE_SHIFT) +
15430 + req->new_dent;
15431 + return znodes * c->max_idx_node_sz;
15432 +}
15433 +
15434 +/**
15435 + * calc_data_growth - calculate approximate amount of new data from budgeting
15436 + * request.
15437 + * @c: UBIFS file-system description object
15438 + * @req: budgeting request
15439 + */
15440 +static int calc_data_growth(const struct ubifs_info *c,
15441 + const struct ubifs_budget_req *req)
15442 +{
15443 + int data_growth;
15444 +
15445 + data_growth = req->new_ino ? c->inode_budget : 0;
15446 + if (req->new_page)
15447 + data_growth += c->page_budget;
15448 + if (req->new_dent)
15449 + data_growth += c->dent_budget;
15450 + data_growth += req->new_ino_d;
15451 +
15452 + return data_growth;
15453 +}
15454 +
15455 +/**
15456 + * calc_dd_growth - calculate approximate amount of data which makes other data
15457 + * dirty from budgeting request.
15458 + * @c: UBIFS file-system description object
15459 + * @req: budgeting request
15460 + */
15461 +static int calc_dd_growth(const struct ubifs_info *c,
15462 + const struct ubifs_budget_req *req)
15463 +{
15464 + int dd_growth;
15465 +
15466 + dd_growth = req->dirtied_page ? c->page_budget : 0;
15467 +
15468 + if (req->dirtied_ino)
15469 + dd_growth += c->inode_budget << (req->dirtied_ino - 1);
15470 + if (req->mod_dent)
15471 + dd_growth += c->dent_budget;
15472 + dd_growth += req->dirtied_ino_d;
15473 +
15474 + return dd_growth;
15475 +}
15476 +
15477 +/**
15478 + * ubifs_budget_space - ensure there is enough space to complete an operation.
15479 + * @c: UBIFS file-system description object
15480 + * @req: budget request
15481 + *
15482 + * This function allocates budget for an operation. It uses pessimistic
15483 + * approximation of how much flash space the operation needs. The goal of this
15484 + * function is to make sure UBIFS always has flash space to flush all dirty
15485 + * pages, dirty inodes, and dirty znodes (liability). This function may force
15486 + * commit, garbage-collection or write-back. Returns zero in case of success,
15487 + * %-ENOSPC if there is no free space and other negative error codes in case of
15488 + * failures.
15489 + */
15490 +int ubifs_budget_space(struct ubifs_info *c, struct ubifs_budget_req *req)
15491 +{
15492 + int uninitialized_var(cmt_retries), uninitialized_var(wb_retries);
15493 + int err, idx_growth, data_growth, dd_growth;
15494 + struct retries_info ri;
15495 +
15496 + data_growth = calc_data_growth(c, req);
15497 + dd_growth = calc_dd_growth(c, req);
15498 + if (!data_growth && !dd_growth)
15499 + return 0;
15500 + idx_growth = calc_idx_growth(c, req);
15501 + memset(&ri, 0, sizeof(struct retries_info));
15502 +
15503 +again:
15504 + spin_lock(&c->space_lock);
15505 + ubifs_assert(c->budg_idx_growth >= 0);
15506 + ubifs_assert(c->budg_data_growth >= 0);
15507 + ubifs_assert(c->budg_dd_growth >= 0);
15508 +
15509 + c->budg_idx_growth += idx_growth;
15510 + c->budg_data_growth += data_growth;
15511 + c->budg_dd_growth += dd_growth;
15512 +
15513 + err = do_budget_space(c);
15514 + if (unlikely(err)) {
15515 + /* Restore the old values */
15516 + c->budg_idx_growth -= idx_growth;
15517 + c->budg_data_growth -= data_growth;
15518 + c->budg_dd_growth -= dd_growth;
15519 + spin_unlock(&c->space_lock);
15520 +
15521 + goto make_space;
15522 + }
15523 +
15524 + req->idx_growth = idx_growth;
15525 + req->data_growth = data_growth;
15526 + req->dd_growth = dd_growth;
15527 + spin_unlock(&c->space_lock);
15528 +
15529 + return 0;
15530 +
15531 +make_space:
15532 + err = make_free_space(c, &ri);
15533 + if (err == -EAGAIN) {
15534 + dbg_budg("try again");
15535 + cond_resched();
15536 + goto again;
15537 + } else if (err == -ENOSPC)
15538 + dbg_budg("FS is full, -ENOSPC");
15539 + else
15540 + ubifs_err("cannot budget space, error %d", err);
15541 +
15542 + return err;
15543 +}
15544 +
15545 +/**
15546 + * ubifs_release_budget - release budgeted free space.
15547 + * @c: UBIFS file-system description object
15548 + * @req: budget request
15549 + *
15550 + * This function releases the space budgeted by 'ubifs_budget_space()'. Note,
15551 + * since the index changes (which were budgeted for in @req->idx_growth) will
15552 + * only be written to the media on commit, this function moves the index budget
15553 + * from @c->budg_idx_growth to @c->budg_uncommitted_idx. The latter will be
15554 + * zeroed by the commit operation.
15555 + */
15556 +void ubifs_release_budget(struct ubifs_info *c, struct ubifs_budget_req *req)
15557 +{
15558 + if (!req->data_growth && !req->dd_growth)
15559 + return;
15560 +
15561 + if (req->idx_growth == -1)
15562 + req->idx_growth = calc_idx_growth(c, req);
15563 +
15564 + spin_lock(&c->space_lock);
15565 + c->budg_idx_growth -= req->idx_growth;
15566 + c->budg_uncommitted_idx += req->idx_growth;
15567 + c->budg_data_growth -= req->data_growth;
15568 + c->budg_dd_growth -= req->dd_growth;
15569 + c->min_idx_lebs = ubifs_calc_min_idx_lebs(c);
15570 +
15571 + ubifs_assert(c->budg_idx_growth >= 0);
15572 + ubifs_assert(c->budg_data_growth >= 0);
15573 + ubifs_assert(c->min_idx_lebs < c->main_lebs);
15574 + spin_unlock(&c->space_lock);
15575 +}
15576 +
15577 +/**
15578 + * ubifs_convert_page_budget - convert budget of a new page.
15579 + * @c: UBIFS file-system description object
15580 + *
15581 + * This function converts budget which was allocated for a new page of data to
15582 + * the budget of changing an existing page of data. The latter is not larger
15583 + * then the former, so this function only does simple re-calculation and does
15584 + * not involve any write-back.
15585 + */
15586 +void ubifs_convert_page_budget(struct ubifs_info *c)
15587 +{
15588 + spin_lock(&c->space_lock);
15589 + /* Release the index growth reservation */
15590 + c->budg_idx_growth -= c->max_idx_node_sz << UBIFS_BLOCKS_PER_PAGE_SHIFT;
15591 + /* Release the data growth reservation */
15592 + c->budg_data_growth -= c->page_budget;
15593 + /* Increase the dirty data growth reservation instead */
15594 + c->budg_dd_growth += c->page_budget;
15595 + /* And re-calculate the indexing space reservation */
15596 + c->min_idx_lebs = ubifs_calc_min_idx_lebs(c);
15597 + spin_unlock(&c->space_lock);
15598 +}
15599 +
15600 +/**
15601 + * ubifs_budget_inode_op - budget an operation on inode.
15602 + * @c: UBIFS file-system description object
15603 + * @inode: VFS inode which will be made dirty by the operation
15604 + * @req: budget request of the operation
15605 + *
15606 + * This function is called to get budget for an operation which changes an
15607 + * inode. The inode may be in dirty or clean state. The former means there is
15608 + * no need to allocate the budget as it has already been allocated before. The
15609 + * latter means that the inode change budget has to be allocated.
15610 + *
15611 + * The caller has to pass the inode which is going to be changed. This function
15612 + * acquires budget the for as described in @req plus the budget for changing
15613 + * the inode dirty, if needed. Returns zero in case of success, %-ENOSPC if
15614 + * there is no more flash space, and other negative error codes in case of
15615 + * failure.
15616 + *
15617 + * Note, upon exit, this function leaves the inode locked, and the
15618 + * 'ubifs_release_ino_dirty()' or 'ubifs_release_ino_clean()' function has to
15619 + * be called to unlock it.
15620 + */
15621 +int ubifs_budget_inode_op(struct ubifs_info *c, struct inode *inode,
15622 + struct ubifs_budget_req *req)
15623 +{
15624 + struct ubifs_inode *ui = ubifs_inode(inode);
15625 + int err, old = req->dirtied_ino;
15626 +
15627 + ubifs_assert(req->dirtied_ino <= 3);
15628 + ubifs_assert(req->dirtied_ino_d <= UBIFS_MAX_INO_DATA * 3);
15629 +
15630 +again:
15631 + /*
15632 + * If the inode is clean, it will be dirtied by this operation and we
15633 + * have to budget for this.
15634 + */
15635 + req->dirtied_ino += !ui->dirty;
15636 + if (req->dirtied_ino > old)
15637 + req->dirtied_ino_d += ui->data_len;
15638 +
15639 + /*
15640 + * Note, if the budget request does not actually request anything
15641 + * (i.e., @req contains only zeroes), 'ubifs_budget_space()' will
15642 + * return almost straight away.
15643 + */
15644 + err = ubifs_budget_space(c, req);
15645 + if (unlikely(err))
15646 + return err;
15647 +
15648 + mutex_lock(&ui->budg_mutex);
15649 +
15650 + if (req->dirtied_ino != old + !ui->dirty) {
15651 + /* The inode has probably been written back meanwhile */
15652 + ubifs_release_budget(c, req);
15653 + mutex_unlock(&ui->budg_mutex);
15654 + req->dirtied_ino = old;
15655 + req->dirtied_ino_d -= ui->data_len;
15656 + goto again;
15657 + }
15658 +
15659 + UBIFS_DBG(ui->budgeted = 1);
15660 + return 0;
15661 +}
15662 +
15663 +/**
15664 + * ubifs_release_ino_dirty - release budget of a "dirtying" operation.
15665 + * @c: UBIFS file-system description object
15666 + * @inode: VFS inode the operation worked on
15667 + * @req: budget to release
15668 + *
15669 + * This function has to be called at the end of VFS operations which acquired
15670 + * budget via 'ubifs_budget_inode_op()'. It assumes that the inode has been
15671 + * marked as dirty and will be synchronized later by write-back, so it does not
15672 + * release the budget of the inode.
15673 + *
15674 + * Note, this function also avoids releasing page budgets which are released
15675 + * separately.
15676 + */
15677 +void ubifs_release_ino_dirty(struct ubifs_info *c, struct inode *inode,
15678 + struct ubifs_budget_req *req)
15679 +{
15680 + ubifs_assert(req->dirtied_ino <= 4);
15681 + ubifs_assert(req->dirtied_ino_d <= UBIFS_MAX_INO_DATA * 4);
15682 + ubifs_assert(req->idx_growth >= 0);
15683 + ubifs_assert(req->data_growth >= 0);
15684 + ubifs_assert(req->dd_growth >= 0);
15685 +
15686 + if (req->dirtied_ino) {
15687 + req->dd_growth -= c->inode_budget;
15688 + req->dd_growth -= req->dirtied_ino_d;
15689 + }
15690 +
15691 + if (req->dirtied_page) {
15692 + req->dd_growth -= c->page_budget;
15693 + ubifs_assert(req->new_page == 0);
15694 + } else if (req->new_page) {
15695 + req->idx_growth -=
15696 + c->max_idx_node_sz << UBIFS_BLOCKS_PER_PAGE_SHIFT;
15697 + req->data_growth -= c->page_budget;
15698 + ubifs_assert(req->dirtied_page == 0);
15699 + }
15700 +
15701 + ubifs_assert(req->dd_growth >= 0);
15702 + ubifs_release_budget(c, req);
15703 + mutex_unlock(&ubifs_inode(inode)->budg_mutex);
15704 +}
15705 +
15706 +/**
15707 + * ubifs_cancel_ino_op - cancel budget of an operation on inode.
15708 + * @c: UBIFS file-system description object
15709 + * @inode: VFS inode the operation worked on
15710 + * @req: budget to release
15711 + *
15712 + * This function has to be called if the operation failed and whole budget has
15713 + * to be released, including the budget for inode which would had been
15714 + * dirtied. It is important not to mark the inode dirty before calling this
15715 + * function.
15716 + */
15717 +void ubifs_cancel_ino_op(struct ubifs_info *c, struct inode *inode,
15718 + struct ubifs_budget_req *req)
15719 +{
15720 + ubifs_assert(req->dirtied_ino <= 4);
15721 + ubifs_assert(req->dirtied_ino_d <= UBIFS_MAX_INO_DATA * 4);
15722 + ubifs_assert(req->idx_growth >= 0);
15723 + ubifs_assert(req->data_growth >= 0);
15724 + ubifs_assert(req->dd_growth >= 0);
15725 +
15726 + ubifs_release_budget(c, req);
15727 + mutex_unlock(&ubifs_inode(inode)->budg_mutex);
15728 +}
15729 +
15730 +/**
15731 + * ubifs_release_ino_clean - release budget of a "cleaning" operation.
15732 + * @c: UBIFS file-system description object
15733 + * @inode: VFS inode the operation worked on
15734 + * @req: budget to release
15735 + *
15736 + * This function has to be called at the end of VFS operations which acquired
15737 + * budget via 'ubifs_budget_inode_op()'. It assumed the operation synchronized
15738 + * the inode, so it marks the inode clean, unlocks it and releases whole budget.
15739 + *
15740 + * Note, this function also avoids releasing page budgets which are released
15741 + * separately.
15742 + */
15743 +void ubifs_release_ino_clean(struct ubifs_info *c, struct inode *inode,
15744 + struct ubifs_budget_req *req)
15745 +{
15746 + struct ubifs_inode *ui = ubifs_inode(inode);
15747 +
15748 + ubifs_assert(req->dirtied_ino <= 4);
15749 + ubifs_assert(req->dirtied_ino_d <= UBIFS_MAX_INO_DATA * 4);
15750 + ubifs_assert(req->idx_growth >= 0);
15751 + ubifs_assert(req->data_growth >= 0);
15752 + ubifs_assert(req->dd_growth >= 0);
15753 + ubifs_assert(!req->dirtied_page);
15754 + ubifs_assert(!req->new_page);
15755 + UBIFS_DBG(ui->budgeted = 0);
15756 +
15757 + ubifs_release_budget(c, req);
15758 + if (ui->dirty) {
15759 + ui->dirty = 0;
15760 + /*
15761 + * Note, VFS still treats the inode as dirty and
15762 + * 'ubifs_write_inode()' will be called, but it'll do nothing
15763 + * because @ui->dirty is %0.
15764 + */
15765 + atomic_long_dec(&c->dirty_ino_cnt);
15766 + }
15767 + mutex_unlock(&ubifs_inode(inode)->budg_mutex);
15768 +}
15769 +
15770 +/**
15771 + * ubifs_release_new_page_budget - release budget of a new page.
15772 + * @c: UBIFS file-system description object
15773 + *
15774 + * This is a helper function which releases budget corresponding to the budget
15775 + * of one new page of data.
15776 + */
15777 +void ubifs_release_new_page_budget(struct ubifs_info *c)
15778 +{
15779 + struct ubifs_budget_req req = { .new_page = 1,
15780 + .idx_growth = -1,
15781 + .data_growth = c->page_budget };
15782 +
15783 + ubifs_release_budget(c, &req);
15784 +}
15785 +
15786 +/**
15787 + * ubifs_budg_get_free_space - return amount of free space.
15788 + * @c: UBIFS file-system description object
15789 + *
15790 + * This function returns amount of free space on the file-system.
15791 + */
15792 +long long ubifs_budg_get_free_space(struct ubifs_info *c)
15793 +{
15794 + int min_idx_lebs, rsvd_idx_lebs;
15795 + long long available, outstanding, free;
15796 +
15797 + /* Do exactly the same calculations as in 'do_budget_space()' */
15798 + spin_lock(&c->space_lock);
15799 + min_idx_lebs = ubifs_calc_min_idx_lebs(c);
15800 +
15801 + if (min_idx_lebs > c->lst.idx_lebs)
15802 + rsvd_idx_lebs = min_idx_lebs - c->lst.idx_lebs;
15803 + else
15804 + rsvd_idx_lebs = 0;
15805 +
15806 + if (rsvd_idx_lebs > c->lst.empty_lebs + c->freeable_cnt + c->idx_gc_cnt
15807 + - c->lst.taken_empty_lebs) {
15808 + spin_unlock(&c->space_lock);
15809 + return 0;
15810 + }
15811 +
15812 + c->min_idx_lebs = min_idx_lebs;
15813 + available = ubifs_calc_available(c);
15814 + outstanding = c->budg_data_growth + c->budg_dd_growth;
15815 + spin_unlock(&c->space_lock);
15816 +
15817 + if (available > outstanding)
15818 + free = ubifs_reported_space(c, available - outstanding);
15819 + else
15820 + free = 0;
15821 +
15822 + return free;
15823 +}
15824 diff --exclude=.git -urN linux-2.6.25.6/fs/ubifs/commit.c avr32-2.6/fs/ubifs/commit.c
15825 --- linux-2.6.25.6/fs/ubifs/commit.c 1970-01-01 01:00:00.000000000 +0100
15826 +++ avr32-2.6/fs/ubifs/commit.c 2008-06-12 15:09:45.311815896 +0200
15827 @@ -0,0 +1,677 @@
15828 +/*
15829 + * This file is part of UBIFS.
15830 + *
15831 + * Copyright (C) 2006-2008 Nokia Corporation.
15832 + *
15833 + * This program is free software; you can redistribute it and/or modify it
15834 + * under the terms of the GNU General Public License version 2 as published by
15835 + * the Free Software Foundation.
15836 + *
15837 + * This program is distributed in the hope that it will be useful, but WITHOUT
15838 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15839 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15840 + * more details.
15841 + *
15842 + * You should have received a copy of the GNU General Public License along with
15843 + * this program; if not, write to the Free Software Foundation, Inc., 51
15844 + * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
15845 + *
15846 + * Authors: Adrian Hunter
15847 + * Artem Bityutskiy (Битюцкий Артём)
15848 + */
15849 +
15850 +/*
15851 + * This file implements functions that manage the running of the commit process.
15852 + * Each affected module has its own functions to accomplish their part in the
15853 + * commit and those functions are called here.
15854 + *
15855 + * The commit is the process whereby all updates to the index and LEB properties
15856 + * are written out together and the journal becomes empty. This keeps the
15857 + * file system consistent - at all times the state can be recreated by reading
15858 + * the index and LEB properties and then replaying the journal.
15859 + *
15860 + * The commit is split into two parts named "commit start" and "commit end".
15861 + * During commit start, the commit process has exclusive access to the journal
15862 + * by holding the commit semaphore down for writing. As few I/O operations as
15863 + * possible are performed during commit start, instead the nodes that are to be
15864 + * written are merely identified. During commit end, the commit semaphore is no
15865 + * longer held and the journal is again in operation, allowing users to continue
15866 + * to use the file system while the bulk of the commit I/O is performed. The
15867 + * purpose of this two-step approach is to prevent the commit from causing any
15868 + * latency blips. Note that in any case, the commit does not prevent lookups
15869 + * (as permitted by the TNC mutex), or access to VFS data structures e.g. page
15870 + * cache.
15871 + */
15872 +
15873 +#include <linux/freezer.h>
15874 +#include <linux/kthread.h>
15875 +#include "ubifs.h"
15876 +
15877 +/**
15878 + * do_commit - commit the journal.
15879 + * @c: UBIFS file-system description object
15880 + *
15881 + * This function implements UBIFS commit. It has to be called with commit lock
15882 + * locked. Returns zero in case of success and a negative error code in case of
15883 + * failure.
15884 + */
15885 +static int do_commit(struct ubifs_info *c)
15886 +{
15887 + int err, new_ltail_lnum, old_ltail_lnum, i;
15888 + struct ubifs_zbranch zroot;
15889 + struct ubifs_lp_stats lst;
15890 +
15891 + dbg_cmt("start");
15892 + if (c->ro_media) {
15893 + err = -EROFS;
15894 + goto out_up;
15895 + }
15896 +
15897 + /* Sync all write buffers (necessary for recovery) */
15898 + for (i = 0; i < c->jhead_cnt; i++) {
15899 + err = ubifs_wbuf_sync(&c->jheads[i].wbuf);
15900 + if (err)
15901 + goto out_up;
15902 + }
15903 +
15904 + err = ubifs_gc_start_commit(c);
15905 + if (err)
15906 + goto out_up;
15907 + err = dbg_check_lprops(c);
15908 + if (err)
15909 + goto out_up;
15910 + err = ubifs_log_start_commit(c, &new_ltail_lnum);
15911 + if (err)
15912 + goto out_up;
15913 + err = ubifs_tnc_start_commit(c, &zroot);
15914 + if (err)
15915 + goto out_up;
15916 + err = ubifs_lpt_start_commit(c);
15917 + if (err)
15918 + goto out_up;
15919 + err = ubifs_orphan_start_commit(c);
15920 + if (err)
15921 + goto out_up;
15922 +
15923 + ubifs_get_lp_stats(c, &lst);
15924 +
15925 + up_write(&c->commit_sem);
15926 +
15927 + err = ubifs_tnc_end_commit(c);
15928 + if (err)
15929 + goto out;
15930 + err = ubifs_lpt_end_commit(c);
15931 + if (err)
15932 + goto out;
15933 + err = ubifs_orphan_end_commit(c);
15934 + if (err)
15935 + goto out;
15936 + old_ltail_lnum = c->ltail_lnum;
15937 + err = ubifs_log_end_commit(c, new_ltail_lnum);
15938 + if (err)
15939 + goto out;
15940 + err = dbg_check_old_index(c, &zroot);
15941 + if (err)
15942 + goto out;
15943 +
15944 + mutex_lock(&c->mst_mutex);
15945 + c->mst_node->cmt_no = cpu_to_le64(++c->cmt_no);
15946 + c->mst_node->log_lnum = cpu_to_le32(new_ltail_lnum);
15947 + c->mst_node->root_lnum = cpu_to_le32(zroot.lnum);
15948 + c->mst_node->root_offs = cpu_to_le32(zroot.offs);
15949 + c->mst_node->root_len = cpu_to_le32(zroot.len);
15950 + c->mst_node->ihead_lnum = cpu_to_le32(c->ihead_lnum);
15951 + c->mst_node->ihead_offs = cpu_to_le32(c->ihead_offs);
15952 + c->mst_node->index_size = cpu_to_le64(c->old_idx_sz);
15953 + c->mst_node->lpt_lnum = cpu_to_le32(c->lpt_lnum);
15954 + c->mst_node->lpt_offs = cpu_to_le32(c->lpt_offs);
15955 + c->mst_node->nhead_lnum = cpu_to_le32(c->nhead_lnum);
15956 + c->mst_node->nhead_offs = cpu_to_le32(c->nhead_offs);
15957 + c->mst_node->ltab_lnum = cpu_to_le32(c->ltab_lnum);
15958 + c->mst_node->ltab_offs = cpu_to_le32(c->ltab_offs);
15959 + c->mst_node->lsave_lnum = cpu_to_le32(c->lsave_lnum);
15960 + c->mst_node->lsave_offs = cpu_to_le32(c->lsave_offs);
15961 + c->mst_node->lscan_lnum = cpu_to_le32(c->lscan_lnum);
15962 + c->mst_node->empty_lebs = cpu_to_le32(lst.empty_lebs);
15963 + c->mst_node->idx_lebs = cpu_to_le32(lst.idx_lebs);
15964 + c->mst_node->total_free = cpu_to_le64(lst.total_free);
15965 + c->mst_node->total_dirty = cpu_to_le64(lst.total_dirty);
15966 + c->mst_node->total_used = cpu_to_le64(lst.total_used);
15967 + c->mst_node->total_dead = cpu_to_le64(lst.total_dead);
15968 + c->mst_node->total_dark = cpu_to_le64(lst.total_dark);
15969 + if (c->no_orphs)
15970 + c->mst_node->flags |= cpu_to_le32(UBIFS_MST_NO_ORPHS);
15971 + else
15972 + c->mst_node->flags &= ~cpu_to_le32(UBIFS_MST_NO_ORPHS);
15973 + err = ubifs_write_master(c);
15974 + mutex_unlock(&c->mst_mutex);
15975 + if (err)
15976 + goto out;
15977 +
15978 + err = ubifs_log_post_commit(c, old_ltail_lnum);
15979 + if (err)
15980 + goto out;
15981 + err = ubifs_gc_end_commit(c);
15982 + if (err)
15983 + goto out;
15984 + err = ubifs_lpt_post_commit(c);
15985 + if (err)
15986 + goto out;
15987 +
15988 + spin_lock(&c->cs_lock);
15989 + c->cmt_state = COMMIT_RESTING;
15990 + wake_up(&c->cmt_wq);
15991 + dbg_cmt("commit end");
15992 + spin_unlock(&c->cs_lock);
15993 +
15994 + return 0;
15995 +
15996 +out_up:
15997 + up_write(&c->commit_sem);
15998 +out:
15999 + ubifs_err("commit failed, error %d", err);
16000 + spin_lock(&c->cs_lock);
16001 + c->cmt_state = COMMIT_BROKEN;
16002 + wake_up(&c->cmt_wq);
16003 + spin_unlock(&c->cs_lock);
16004 + ubifs_ro_mode(c, err);
16005 + return err;
16006 +}
16007 +
16008 +/**
16009 + * run_bg_commit - run background commit if it is needed.
16010 + * @c: UBIFS file-system description object
16011 + *
16012 + * This function runs background commit if it is needed. Returns zero in case
16013 + * of success and a negative error code in case of failure.
16014 + */
16015 +static int run_bg_commit(struct ubifs_info *c)
16016 +{
16017 + spin_lock(&c->cs_lock);
16018 + /*
16019 + * Run background commit only if background commit was requested or if
16020 + * commit is required.
16021 + */
16022 + if (c->cmt_state != COMMIT_BACKGROUND &&
16023 + c->cmt_state != COMMIT_REQUIRED)
16024 + goto out;
16025 + spin_unlock(&c->cs_lock);
16026 +
16027 + down_write(&c->commit_sem);
16028 + spin_lock(&c->cs_lock);
16029 + if (c->cmt_state == COMMIT_REQUIRED)
16030 + c->cmt_state = COMMIT_RUNNING_REQUIRED;
16031 + else if (c->cmt_state == COMMIT_BACKGROUND)
16032 + c->cmt_state = COMMIT_RUNNING_BACKGROUND;
16033 + else
16034 + goto out_cmt_unlock;
16035 + spin_unlock(&c->cs_lock);
16036 +
16037 + return do_commit(c);
16038 +
16039 +out_cmt_unlock:
16040 + up_write(&c->commit_sem);
16041 +out:
16042 + spin_unlock(&c->cs_lock);
16043 + return 0;
16044 +}
16045 +
16046 +/**
16047 + * ubifs_bg_thread - UBIFS background thread function.
16048 + * @info: points to the file-system description object
16049 + *
16050 + * This function implements various file-system background activities:
16051 + * o when a write-buffer timer expires it synchronizes the appropriate
16052 + * write-buffer;
16053 + * o when the journal is about to be full, it starts in-advance commit.
16054 + *
16055 + * Note, other stuff like background garbage collection may be added here in
16056 + * future.
16057 + */
16058 +int ubifs_bg_thread(void *info)
16059 +{
16060 + int err;
16061 + struct ubifs_info *c = info;
16062 +
16063 + ubifs_msg("background thread \"%s\" started, PID %d",
16064 + c->bgt_name, current->pid);
16065 + set_freezable();
16066 +
16067 + while (1) {
16068 + if (kthread_should_stop())
16069 + break;
16070 +
16071 + if (try_to_freeze())
16072 + continue;
16073 +
16074 + set_current_state(TASK_INTERRUPTIBLE);
16075 + /* Check if there is something to do */
16076 + if (!c->need_bgt) {
16077 + /*
16078 + * Nothing prevents us from going sleep now and
16079 + * be never woken up and block the task which
16080 + * could wait in 'kthread_stop()' forever.
16081 + */
16082 + if (kthread_should_stop())
16083 + break;
16084 + schedule();
16085 + continue;
16086 + } else
16087 + __set_current_state(TASK_RUNNING);
16088 +
16089 + c->need_bgt = 0;
16090 + err = ubifs_bg_wbufs_sync(c);
16091 + if (err)
16092 + ubifs_ro_mode(c, err);
16093 +
16094 + run_bg_commit(c);
16095 + cond_resched();
16096 + }
16097 +
16098 + dbg_msg("background thread \"%s\" stops", c->bgt_name);
16099 + return 0;
16100 +}
16101 +
16102 +/**
16103 + * ubifs_commit_required - set commit state to "required".
16104 + * @c: UBIFS file-system description object
16105 + *
16106 + * This function is called if a commit is required but cannot be done from the
16107 + * calling function, so it is just flagged instead.
16108 + */
16109 +void ubifs_commit_required(struct ubifs_info *c)
16110 +{
16111 + spin_lock(&c->cs_lock);
16112 + switch (c->cmt_state) {
16113 + case COMMIT_RESTING:
16114 + case COMMIT_BACKGROUND:
16115 + dbg_cmt("old: %s, new: %s", dbg_cstate(c->cmt_state),
16116 + dbg_cstate(COMMIT_REQUIRED));
16117 + c->cmt_state = COMMIT_REQUIRED;
16118 + break;
16119 + case COMMIT_RUNNING_BACKGROUND:
16120 + dbg_cmt("old: %s, new: %s", dbg_cstate(c->cmt_state),
16121 + dbg_cstate(COMMIT_RUNNING_REQUIRED));
16122 + c->cmt_state = COMMIT_RUNNING_REQUIRED;
16123 + break;
16124 + case COMMIT_REQUIRED:
16125 + case COMMIT_RUNNING_REQUIRED:
16126 + case COMMIT_BROKEN:
16127 + break;
16128 + }
16129 + spin_unlock(&c->cs_lock);
16130 +}
16131 +
16132 +/**
16133 + * ubifs_request_bg_commit - notify the background thread to do a commit.
16134 + * @c: UBIFS file-system description object
16135 + *
16136 + * This function is called if the journal is full enough to make a commit
16137 + * worthwhile, so background thread is kicked to start it.
16138 + */
16139 +void ubifs_request_bg_commit(struct ubifs_info *c)
16140 +{
16141 + spin_lock(&c->cs_lock);
16142 + if (c->cmt_state == COMMIT_RESTING) {
16143 + dbg_cmt("old: %s, new: %s", dbg_cstate(c->cmt_state),
16144 + dbg_cstate(COMMIT_BACKGROUND));
16145 + c->cmt_state = COMMIT_BACKGROUND;
16146 + spin_unlock(&c->cs_lock);
16147 + ubifs_wake_up_bgt(c);
16148 + } else
16149 + spin_unlock(&c->cs_lock);
16150 +}
16151 +
16152 +/**
16153 + * wait_for_commit - wait for commit.
16154 + * @c: UBIFS file-system description object
16155 + *
16156 + * This function sleeps until the commit operation is no longer running.
16157 + */
16158 +static int wait_for_commit(struct ubifs_info *c)
16159 +{
16160 + dbg_cmt("pid %d goes sleep", current->pid);
16161 +
16162 + /*
16163 + * The following sleeps if the condition is false, and will be woken
16164 + * when the commit ends. It is possible, although very unlikely, that we
16165 + * will wake up and see the subsequent commit running, rather than the
16166 + * one we were waiting for, and go back to sleep. However, we will be
16167 + * woken again, so there is no danger of sleeping forever.
16168 + */
16169 + wait_event(c->cmt_wq, c->cmt_state != COMMIT_RUNNING_BACKGROUND &&
16170 + c->cmt_state != COMMIT_RUNNING_REQUIRED);
16171 + dbg_cmt("commit finished, pid %d woke up", current->pid);
16172 + return 0;
16173 +}
16174 +
16175 +/**
16176 + * ubifs_run_commit - run or wait for commit.
16177 + * @c: UBIFS file-system description object
16178 + *
16179 + * This function runs commit and returns zero in case of success and a negative
16180 + * error code in case of failure.
16181 + */
16182 +int ubifs_run_commit(struct ubifs_info *c)
16183 +{
16184 + int err = 0;
16185 +
16186 + spin_lock(&c->cs_lock);
16187 + if (c->cmt_state == COMMIT_BROKEN) {
16188 + err = -EINVAL;
16189 + goto out;
16190 + }
16191 +
16192 + if (c->cmt_state == COMMIT_RUNNING_BACKGROUND)
16193 + /*
16194 + * We set the commit state to 'running required' to indicate
16195 + * that we want it to complete as quickly as possible.
16196 + */
16197 + c->cmt_state = COMMIT_RUNNING_REQUIRED;
16198 +
16199 + if (c->cmt_state == COMMIT_RUNNING_REQUIRED) {
16200 + spin_unlock(&c->cs_lock);
16201 + return wait_for_commit(c);
16202 + }
16203 + spin_unlock(&c->cs_lock);
16204 +
16205 + /* Ok, the commit is indeed needed */
16206 +
16207 + down_write(&c->commit_sem);
16208 + spin_lock(&c->cs_lock);
16209 + /*
16210 + * Since we unlocked 'c->cs_lock', the state may have changed, so
16211 + * re-check it.
16212 + */
16213 + if (c->cmt_state == COMMIT_BROKEN) {
16214 + err = -EINVAL;
16215 + goto out_cmt_unlock;
16216 + }
16217 +
16218 + if (c->cmt_state == COMMIT_RUNNING_BACKGROUND)
16219 + c->cmt_state = COMMIT_RUNNING_REQUIRED;
16220 +
16221 + if (c->cmt_state == COMMIT_RUNNING_REQUIRED) {
16222 + up_write(&c->commit_sem);
16223 + spin_unlock(&c->cs_lock);
16224 + return wait_for_commit(c);
16225 + }
16226 + c->cmt_state = COMMIT_RUNNING_REQUIRED;
16227 + spin_unlock(&c->cs_lock);
16228 +
16229 + err = do_commit(c);
16230 + return err;
16231 +
16232 +out_cmt_unlock:
16233 + up_write(&c->commit_sem);
16234 +out:
16235 + spin_unlock(&c->cs_lock);
16236 + return err;
16237 +}
16238 +
16239 +/**
16240 + * ubifs_gc_should_commit - determine if it is time for GC to run commit.
16241 + * @c: UBIFS file-system description object
16242 + *
16243 + * This function is called by garbage collection to determine if commit should
16244 + * be run. If commit state is @COMMIT_BACKGROUND, which means that the journal
16245 + * is full enough to start commit, this function returns true. It is not
16246 + * absolutely necessary to commit yet, but it feels like this should be better
16247 + * then to keep doing GC. This function returns %1 if GC has to initiate commit
16248 + * and %0 if not.
16249 + */
16250 +int ubifs_gc_should_commit(struct ubifs_info *c)
16251 +{
16252 + int ret = 0;
16253 +
16254 + spin_lock(&c->cs_lock);
16255 + if (c->cmt_state == COMMIT_BACKGROUND) {
16256 + dbg_cmt("commit required now");
16257 + c->cmt_state = COMMIT_REQUIRED;
16258 + } else
16259 + dbg_cmt("commit not requested");
16260 + if (c->cmt_state == COMMIT_REQUIRED)
16261 + ret = 1;
16262 + spin_unlock(&c->cs_lock);
16263 + return ret;
16264 +}
16265 +
16266 +#ifdef CONFIG_UBIFS_FS_DEBUG
16267 +
16268 +/**
16269 + * struct idx_node - hold index nodes during index tree traversal.
16270 + * @list: list
16271 + * @iip: index in parent (slot number of this indexing node in the parent
16272 + * indexing node)
16273 + * @upper_key: all keys in this indexing node have to be less or equivalent to
16274 + * this key
16275 + * @idx: index node (8-byte aligned because all node structures must be 8-byte
16276 + * aligned)
16277 + */
16278 +struct idx_node {
16279 + struct list_head list;
16280 + int iip;
16281 + union ubifs_key upper_key;
16282 + struct ubifs_idx_node idx __attribute__((aligned(8)));
16283 +};
16284 +
16285 +/**
16286 + * dbg_old_index_check_init - get information for the next old index check.
16287 + * @c: UBIFS file-system description object
16288 + * @zroot: root of the index
16289 + *
16290 + * This function records information about the index that will be needed for the
16291 + * next old index check i.e. 'dbg_check_old_index()'.
16292 + *
16293 + * This function returns %0 on success and a negative error code on failure.
16294 + */
16295 +int dbg_old_index_check_init(struct ubifs_info *c, struct ubifs_zbranch *zroot)
16296 +{
16297 + struct ubifs_idx_node *idx;
16298 + int lnum, offs, len, err = 0;
16299 +
16300 + c->old_zroot = *zroot;
16301 +
16302 + lnum = c->old_zroot.lnum;
16303 + offs = c->old_zroot.offs;
16304 + len = c->old_zroot.len;
16305 +
16306 + idx = kmalloc(c->max_idx_node_sz, GFP_NOFS);
16307 + if (!idx)
16308 + return -ENOMEM;
16309 +
16310 + err = ubifs_read_node(c, idx, UBIFS_IDX_NODE, len, lnum, offs);
16311 + if (err)
16312 + goto out;
16313 +
16314 + c->old_zroot_level = le16_to_cpu(idx->level);
16315 + c->old_zroot_sqnum = le64_to_cpu(idx->ch.sqnum);
16316 +out:
16317 + kfree(idx);
16318 + return err;
16319 +}
16320 +
16321 +/**
16322 + * dbg_check_old_index - check the old copy of the index.
16323 + * @c: UBIFS file-system description object
16324 + * @zroot: root of the new index
16325 + *
16326 + * In order to be able to recover from an unclean unmount, a complete copy of
16327 + * the index must exist on flash. This is the "old" index. The commit process
16328 + * must write the "new" index to flash without overwriting or destroying any
16329 + * part of the old index. This function is run at commit end in order to check
16330 + * that the old index does indeed exist completely intact.
16331 + *
16332 + * This function returns %0 on success and a negative error code on failure.
16333 + */
16334 +int dbg_check_old_index(struct ubifs_info *c, struct ubifs_zbranch *zroot)
16335 +{
16336 + int lnum, offs, len, err = 0, uninitialized_var(last_level), child_cnt;
16337 + int first = 1, iip;
16338 + union ubifs_key lower_key, upper_key, l_key, u_key;
16339 + unsigned long long uninitialized_var(last_sqnum);
16340 + struct ubifs_idx_node *idx;
16341 + struct list_head list;
16342 + struct idx_node *i;
16343 + size_t sz;
16344 +
16345 + if (!(ubifs_chk_flags & UBIFS_CHK_OLD_IDX))
16346 + goto out;
16347 +
16348 + INIT_LIST_HEAD(&list);
16349 +
16350 + sz = sizeof(struct idx_node) + ubifs_idx_node_sz(c, c->fanout) -
16351 + UBIFS_IDX_NODE_SZ;
16352 +
16353 + /* Start at the old zroot */
16354 + lnum = c->old_zroot.lnum;
16355 + offs = c->old_zroot.offs;
16356 + len = c->old_zroot.len;
16357 + iip = 0;
16358 +
16359 + /*
16360 + * Traverse the index tree preorder depth-first i.e. do a node and then
16361 + * its subtrees from left to right.
16362 + */
16363 + while (1) {
16364 + struct ubifs_branch *br;
16365 +
16366 + /* Get the next index node */
16367 + i = kmalloc(sz, GFP_NOFS);
16368 + if (!i) {
16369 + err = -ENOMEM;
16370 + goto out_free;
16371 + }
16372 + i->iip = iip;
16373 + /* Keep the index nodes on our path in a linked list */
16374 + list_add_tail(&i->list, &list);
16375 + /* Read the index node */
16376 + idx = &i->idx;
16377 + err = ubifs_read_node(c, idx, UBIFS_IDX_NODE, len, lnum, offs);
16378 + if (err)
16379 + goto out_free;
16380 + /* Validate index node */
16381 + child_cnt = le16_to_cpu(idx->child_cnt);
16382 + if (child_cnt < 1 || child_cnt > c->fanout) {
16383 + err = 1;
16384 + goto out_dump;
16385 + }
16386 + if (first) {
16387 + first = 0;
16388 + /* Check root level and sqnum */
16389 + if (le16_to_cpu(idx->level) != c->old_zroot_level) {
16390 + err = 2;
16391 + goto out_dump;
16392 + }
16393 + if (le64_to_cpu(idx->ch.sqnum) != c->old_zroot_sqnum) {
16394 + err = 3;
16395 + goto out_dump;
16396 + }
16397 + /* Set last values as though root had a parent */
16398 + last_level = le16_to_cpu(idx->level) + 1;
16399 + last_sqnum = le64_to_cpu(idx->ch.sqnum) + 1;
16400 + key_read(c, ubifs_idx_key(c, idx), &lower_key);
16401 + highest_ino_key(c, &upper_key, INUM_WATERMARK);
16402 + }
16403 + key_copy(c, &upper_key, &i->upper_key);
16404 + if (le16_to_cpu(idx->level) != last_level - 1) {
16405 + err = 3;
16406 + goto out_dump;
16407 + }
16408 + /*
16409 + * The index is always written bottom up hence a child's sqnum
16410 + * is always less than the parents.
16411 + */
16412 + if (le64_to_cpu(idx->ch.sqnum) >= last_sqnum) {
16413 + err = 4;
16414 + goto out_dump;
16415 + }
16416 + /* Check key range */
16417 + key_read(c, ubifs_idx_key(c, idx), &l_key);
16418 + br = ubifs_idx_branch(c, idx, child_cnt - 1);
16419 + key_read(c, &br->key, &u_key);
16420 + if (keys_cmp(c, &lower_key, &l_key) > 0) {
16421 + err = 5;
16422 + goto out_dump;
16423 + }
16424 + if (keys_cmp(c, &upper_key, &u_key) < 0) {
16425 + err = 6;
16426 + goto out_dump;
16427 + }
16428 + if (keys_cmp(c, &upper_key, &u_key) == 0)
16429 + if (!is_hash_key(c, &u_key)) {
16430 + err = 7;
16431 + goto out_dump;
16432 + }
16433 + /* Go to next index node */
16434 + if (le16_to_cpu(idx->level) == 0) {
16435 + /* At the bottom, so go up until can go right */
16436 + while (1) {
16437 + /* Drop the bottom of the list */
16438 + list_del(&i->list);
16439 + kfree(i);
16440 + /* No more list means we are done */
16441 + if (list_empty(&list))
16442 + goto out;
16443 + /* Look at the new bottom */
16444 + i = list_entry(list.prev, struct idx_node,
16445 + list);
16446 + idx = &i->idx;
16447 + /* Can we go right */
16448 + if (iip + 1 < le16_to_cpu(idx->child_cnt)) {
16449 + iip = iip + 1;
16450 + break;
16451 + } else
16452 + /* Nope, so go up again */
16453 + iip = i->iip;
16454 + }
16455 + } else
16456 + /* Go down left */
16457 + iip = 0;
16458 + /*
16459 + * We have the parent in 'idx' and now we set up for reading the
16460 + * child pointed to by slot 'iip'.
16461 + */
16462 + last_level = le16_to_cpu(idx->level);
16463 + last_sqnum = le64_to_cpu(idx->ch.sqnum);
16464 + br = ubifs_idx_branch(c, idx, iip);
16465 + lnum = le32_to_cpu(br->lnum);
16466 + offs = le32_to_cpu(br->offs);
16467 + len = le32_to_cpu(br->len);
16468 + key_read(c, &br->key, &lower_key);
16469 + if (iip + 1 < le16_to_cpu(idx->child_cnt)) {
16470 + br = ubifs_idx_branch(c, idx, iip + 1);
16471 + key_read(c, &br->key, &upper_key);
16472 + } else
16473 + key_copy(c, &i->upper_key, &upper_key);
16474 + }
16475 +out:
16476 + err = dbg_old_index_check_init(c, zroot);
16477 + if (err)
16478 + goto out_free;
16479 +
16480 + return 0;
16481 +
16482 +out_dump:
16483 + dbg_err("dumping index node (iip=%d)", i->iip);
16484 + dbg_dump_node(c, idx);
16485 + list_del(&i->list);
16486 + kfree(i);
16487 + if (!list_empty(&list)) {
16488 + i = list_entry(list.prev, struct idx_node, list);
16489 + dbg_err("dumping parent index node");
16490 + dbg_dump_node(c, &i->idx);
16491 + }
16492 +out_free:
16493 + while (!list_empty(&list)) {
16494 + i = list_entry(list.next, struct idx_node, list);
16495 + list_del(&i->list);
16496 + kfree(i);
16497 + }
16498 + ubifs_err("failed, error %d", err);
16499 + if (err > 0)
16500 + err = -EINVAL;
16501 + return err;
16502 +}
16503 +
16504 +#endif /* CONFIG_UBIFS_FS_DEBUG */
16505 diff --exclude=.git -urN linux-2.6.25.6/fs/ubifs/compress.c avr32-2.6/fs/ubifs/compress.c
16506 --- linux-2.6.25.6/fs/ubifs/compress.c 1970-01-01 01:00:00.000000000 +0100
16507 +++ avr32-2.6/fs/ubifs/compress.c 2008-06-12 15:09:45.315815846 +0200
16508 @@ -0,0 +1,253 @@
16509 +/*
16510 + * This file is part of UBIFS.
16511 + *
16512 + * Copyright (C) 2006-2008 Nokia Corporation.
16513 + * Copyright (C) 2006, 2007 University of Szeged, Hungary
16514 + *
16515 + * This program is free software; you can redistribute it and/or modify it
16516 + * under the terms of the GNU General Public License version 2 as published by
16517 + * the Free Software Foundation.
16518 + *
16519 + * This program is distributed in the hope that it will be useful, but WITHOUT
16520 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16521 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16522 + * more details.
16523 + *
16524 + * You should have received a copy of the GNU General Public License along with
16525 + * this program; if not, write to the Free Software Foundation, Inc., 51
16526 + * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16527 + *
16528 + * Authors: Adrian Hunter
16529 + * Artem Bityutskiy (Битюцкий Артём)
16530 + * Zoltan Sogor
16531 + */
16532 +
16533 +/*
16534 + * This file provides a single place to access to compression and
16535 + * decompression.
16536 + */
16537 +
16538 +#include <linux/crypto.h>
16539 +#include "ubifs.h"
16540 +
16541 +/* Fake description object for the "none" compressor */
16542 +static struct ubifs_compressor none_compr = {
16543 + .compr_type = UBIFS_COMPR_NONE,
16544 + .name = "no compression",
16545 + .capi_name = "",
16546 +};
16547 +
16548 +#ifdef CONFIG_UBIFS_FS_LZO
16549 +static DEFINE_MUTEX(lzo_mutex);
16550 +
16551 +static struct ubifs_compressor lzo_compr = {
16552 + .compr_type = UBIFS_COMPR_LZO,
16553 + .comp_mutex = &lzo_mutex,
16554 + .name = "LZO",
16555 + .capi_name = "lzo",
16556 +};
16557 +#else
16558 +static struct ubifs_compressor lzo_compr = {
16559 + .compr_type = UBIFS_COMPR_LZO,
16560 + .name = "LZO",
16561 +};
16562 +#endif
16563 +
16564 +#ifdef CONFIG_UBIFS_FS_ZLIB
16565 +static DEFINE_MUTEX(deflate_mutex);
16566 +static DEFINE_MUTEX(inflate_mutex);
16567 +
16568 +static struct ubifs_compressor zlib_compr = {
16569 + .compr_type = UBIFS_COMPR_ZLIB,
16570 + .comp_mutex = &deflate_mutex,
16571 + .decomp_mutex = &inflate_mutex,
16572 + .name = "zlib",
16573 + .capi_name = "deflate",
16574 +};
16575 +#else
16576 +static struct ubifs_compressor zlib_compr = {
16577 + .compr_type = UBIFS_COMPR_ZLIB,
16578 + .name = "zlib",
16579 +};
16580 +#endif
16581 +
16582 +/* All UBIFS compressors */
16583 +struct ubifs_compressor *ubifs_compressors[UBIFS_COMPR_TYPES_CNT];
16584 +
16585 +/**
16586 + * ubifs_compress - compress data.
16587 + * @in_buf: data to compress
16588 + * @in_len: length of the data to compress
16589 + * @out_buf: output buffer where compressed data should be stored
16590 + * @out_len: output buffer length is returned here
16591 + * @compr_type: type of compression to use on enter, actually used compression
16592 + * type on exit
16593 + *
16594 + * This function compresses input buffer @in_buf of length @in_len and stores
16595 + * the result in the output buffer @out_buf and the resulting length in
16596 + * @out_len. If the input buffer does not compress, it is just copied to the
16597 + * @out_buf. The same happens if @compr_type is %UBIFS_COMPR_NONE or if
16598 + * compression error occurred.
16599 + *
16600 + * Note, if the input buffer was not compressed, it is copied to the output
16601 + * buffer and %UBIFS_COMPR_NONE is returned in @compr_type.
16602 + *
16603 + * This functions returns %0 on success or a negative error code on failure.
16604 + */
16605 +void ubifs_compress(const void *in_buf, int in_len, void *out_buf, int *out_len,
16606 + int *compr_type)
16607 +{
16608 + int err;
16609 + struct ubifs_compressor *compr = ubifs_compressors[*compr_type];
16610 +
16611 + if (*compr_type == UBIFS_COMPR_NONE)
16612 + goto no_compr;
16613 +
16614 + /* If the input data is small, do not even try to compress it */
16615 + if (in_len < UBIFS_MIN_COMPR_LEN)
16616 + goto no_compr;
16617 +
16618 + if (compr->comp_mutex)
16619 + mutex_lock(compr->comp_mutex);
16620 + err = crypto_comp_compress(compr->cc, in_buf, in_len, out_buf,
16621 + out_len);
16622 + if (compr->comp_mutex)
16623 + mutex_unlock(compr->comp_mutex);
16624 + if (unlikely(err)) {
16625 + ubifs_warn("cannot compress %d bytes, compressor %s, "
16626 + "error %d, leave data uncompressed",
16627 + in_len, compr->name, err);
16628 + goto no_compr;
16629 + }
16630 +
16631 + /*
16632 + * Presently, we just require that compression results in less data,
16633 + * rather than any defined minimum compression ratio or amount.
16634 + */
16635 + if (ALIGN(*out_len, 8) >= ALIGN(in_len, 8))
16636 + goto no_compr;
16637 +
16638 + return;
16639 +
16640 +no_compr:
16641 + memcpy(out_buf, in_buf, in_len);
16642 + *out_len = in_len;
16643 + *compr_type = UBIFS_COMPR_NONE;
16644 +}
16645 +
16646 +/**
16647 + * ubifs_decompress - decompress data.
16648 + * @in_buf: data to decompress
16649 + * @in_len: length of the data to decompress
16650 + * @out_buf: output buffer where decompressed data should
16651 + * @out_len: output length is returned here
16652 + * @compr_type: type of compression
16653 + *
16654 + * This function decompresses data from buffer @in_buf into buffer @out_buf.
16655 + * The length of the uncompressed data is returned in @out_len. This functions
16656 + * returns %0 on success or a negative error code on failure.
16657 + */
16658 +int ubifs_decompress(const void *in_buf, int in_len, void *out_buf,
16659 + int *out_len, int compr_type)
16660 +{
16661 + int err;
16662 + struct ubifs_compressor *compr;
16663 +
16664 + if (unlikely(compr_type < 0 || compr_type >= UBIFS_COMPR_TYPES_CNT)) {
16665 + ubifs_err("invalid compression type %d", compr_type);
16666 + return -EINVAL;
16667 + }
16668 +
16669 + compr = ubifs_compressors[compr_type];
16670 +
16671 + if (unlikely(!compr->capi_name)) {
16672 + ubifs_err("%s compression is not compiled in", compr->name);
16673 + return -EINVAL;
16674 + }
16675 +
16676 + if (compr_type == UBIFS_COMPR_NONE) {
16677 + memcpy(out_buf, in_buf, in_len);
16678 + *out_len = in_len;
16679 + return 0;
16680 + }
16681 +
16682 + if (compr->decomp_mutex)
16683 + mutex_lock(compr->decomp_mutex);
16684 + err = crypto_comp_decompress(compr->cc, in_buf, in_len, out_buf,
16685 + out_len);
16686 + if (compr->decomp_mutex)
16687 + mutex_unlock(compr->decomp_mutex);
16688 + if (err)
16689 + ubifs_err("cannot decompress %d bytes, compressor %s, "
16690 + "error %d", in_len, compr->name, err);
16691 +
16692 + return err;
16693 +}
16694 +
16695 +/**
16696 + * compr_init - initialize a compressor.
16697 + * @compr: compressor description object
16698 + *
16699 + * This function initializes the requested compressor and returns zero in case
16700 + * of success or a negative error code in case of failure.
16701 + */
16702 +static int __init compr_init(struct ubifs_compressor *compr)
16703 +{
16704 + if (compr->capi_name) {
16705 + compr->cc = crypto_alloc_comp(compr->capi_name, 0, 0);
16706 + if (IS_ERR(compr->cc)) {
16707 + ubifs_err("cannot initialize compressor %s, error %ld",
16708 + compr->name, PTR_ERR(compr->cc));
16709 + return PTR_ERR(compr->cc);
16710 + }
16711 + }
16712 +
16713 + ubifs_compressors[compr->compr_type] = compr;
16714 + return 0;
16715 +}
16716 +
16717 +/**
16718 + * compr_exit - de-initialize a compressor.
16719 + * @compr: compressor description object
16720 + */
16721 +static void compr_exit(struct ubifs_compressor *compr)
16722 +{
16723 + if (compr->capi_name)
16724 + crypto_free_comp(compr->cc);
16725 + return;
16726 +}
16727 +
16728 +/**
16729 + * ubifs_compressors_init - initialize UBIFS compressors.
16730 + *
16731 + * This function initializes the compressor which were compiled in. Returns
16732 + * zero in case of success and a negative error code in case of failure.
16733 + */
16734 +int __init ubifs_compressors_init(void)
16735 +{
16736 + int err;
16737 +
16738 + err = compr_init(&lzo_compr);
16739 + if (err)
16740 + return err;
16741 +
16742 + err = compr_init(&zlib_compr);
16743 + if (err)
16744 + goto out_lzo;
16745 +
16746 + ubifs_compressors[UBIFS_COMPR_NONE] = &none_compr;
16747 + return 0;
16748 +
16749 +out_lzo:
16750 + compr_exit(&lzo_compr);
16751 + return err;
16752 +}
16753 +
16754 +/**
16755 + * ubifs_compressors_exit - de-initialize UBIFS compressors.
16756 + */
16757 +void __exit ubifs_compressors_exit(void)
16758 +{
16759 + compr_exit(&lzo_compr);
16760 + compr_exit(&zlib_compr);
16761 +}
16762 diff --exclude=.git -urN linux-2.6.25.6/fs/ubifs/debug.c avr32-2.6/fs/ubifs/debug.c
16763 --- linux-2.6.25.6/fs/ubifs/debug.c 1970-01-01 01:00:00.000000000 +0100
16764 +++ avr32-2.6/fs/ubifs/debug.c 2008-06-12 15:09:45.315815846 +0200
16765 @@ -0,0 +1,2210 @@
16766 +/*
16767 + * This file is part of UBIFS.
16768 + *
16769 + * Copyright (C) 2006-2008 Nokia Corporation
16770 + *
16771 + * This program is free software; you can redistribute it and/or modify it
16772 + * under the terms of the GNU General Public License version 2 as published by
16773 + * the Free Software Foundation.
16774 + *
16775 + * This program is distributed in the hope that it will be useful, but WITHOUT
16776 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16777 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16778 + * more details.
16779 + *
16780 + * You should have received a copy of the GNU General Public License along with
16781 + * this program; if not, write to the Free Software Foundation, Inc., 51
16782 + * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16783 + *
16784 + * Authors: Artem Bityutskiy (Битюцкий Артём)
16785 + * Adrian Hunter
16786 + */
16787 +
16788 +/*
16789 + * This file implements most of the debugging stuff which is compiled in only
16790 + * when it is enabled. But some debugging check functions are implemented in
16791 + * corresponding subsystem, just because they are closely related and utilize
16792 + * various local functions of those subsystems.
16793 + */
16794 +
16795 +#define UBIFS_DBG_PRESERVE_UBI
16796 +
16797 +#include "ubifs.h"
16798 +#include <linux/module.h>
16799 +#include <linux/moduleparam.h>
16800 +
16801 +#ifdef CONFIG_UBIFS_FS_DEBUG
16802 +
16803 +DEFINE_SPINLOCK(dbg_lock);
16804 +
16805 +static char dbg_key_buf0[128];
16806 +static char dbg_key_buf1[128];
16807 +
16808 +unsigned int ubifs_msg_flags = UBIFS_MSG_FLAGS_DEFAULT;
16809 +unsigned int ubifs_chk_flags = UBIFS_CHK_FLAGS_DEFAULT;
16810 +unsigned int ubifs_tst_flags;
16811 +
16812 +module_param_named(debug_msgs, ubifs_msg_flags, uint, S_IRUGO | S_IWUSR);
16813 +module_param_named(debug_chks, ubifs_chk_flags, uint, S_IRUGO | S_IWUSR);
16814 +module_param_named(debug_tsts, ubifs_tst_flags, uint, S_IRUGO | S_IWUSR);
16815 +
16816 +MODULE_PARM_DESC(debug_msgs, "Debug message type flags");
16817 +MODULE_PARM_DESC(debug_chks, "Debug check flags");
16818 +MODULE_PARM_DESC(debug_tsts, "Debug special test flags");
16819 +
16820 +static const char *get_key_fmt(int fmt)
16821 +{
16822 + switch (fmt) {
16823 + case UBIFS_SIMPLE_KEY_FMT:
16824 + return "simple";
16825 + default:
16826 + return "unknown/invalid format";
16827 + }
16828 +}
16829 +
16830 +static const char *get_key_hash(int hash)
16831 +{
16832 + switch (hash) {
16833 + case UBIFS_KEY_HASH_R5:
16834 + return "R5";
16835 + case UBIFS_KEY_HASH_TEST:
16836 + return "test";
16837 + default:
16838 + return "unknown/invalid name hash";
16839 + }
16840 +}
16841 +
16842 +static const char *get_key_type(int type)
16843 +{
16844 + switch (type) {
16845 + case UBIFS_INO_KEY:
16846 + return "inode";
16847 + case UBIFS_DENT_KEY:
16848 + return "direntry";
16849 + case UBIFS_XENT_KEY:
16850 + return "xentry";
16851 + case UBIFS_DATA_KEY:
16852 + return "data";
16853 + case UBIFS_TRUN_KEY:
16854 + return "truncate";
16855 + default:
16856 + return "unknown/invalid key";
16857 + }
16858 +}
16859 +
16860 +static void sprintf_key(const struct ubifs_info *c, const union ubifs_key *key,
16861 + char *buffer)
16862 +{
16863 + char *p = buffer;
16864 + int type = key_type(c, key);
16865 +
16866 + if (c->key_fmt == UBIFS_SIMPLE_KEY_FMT) {
16867 + switch (type) {
16868 + case UBIFS_INO_KEY:
16869 + sprintf(p, "(%lu, %s)", key_inum(c, key),
16870 + get_key_type(type));
16871 + break;
16872 + case UBIFS_DENT_KEY:
16873 + case UBIFS_XENT_KEY:
16874 + sprintf(p, "(%lu, %s, %#08x)", key_inum(c, key),
16875 + get_key_type(type), key_hash(c, key));
16876 + break;
16877 + case UBIFS_DATA_KEY:
16878 + sprintf(p, "(%lu, %s, %u)", key_inum(c, key),
16879 + get_key_type(type), key_block(c, key));
16880 + break;
16881 + case UBIFS_TRUN_KEY:
16882 + sprintf(p, "(%lu, %s)",
16883 + key_inum(c, key), get_key_type(type));
16884 + break;
16885 + default:
16886 + sprintf(p, "(bad key type: %#08x, %#08x)",
16887 + key->u32[0], key->u32[1]);
16888 + }
16889 + } else
16890 + sprintf(p, "bad key format %d", c->key_fmt);
16891 +}
16892 +
16893 +const char *dbg_key_str0(const struct ubifs_info *c, const union ubifs_key *key)
16894 +{
16895 + /* dbg_lock must be held */
16896 + sprintf_key(c, key, dbg_key_buf0);
16897 + return dbg_key_buf0;
16898 +}
16899 +
16900 +const char *dbg_key_str1(const struct ubifs_info *c, const union ubifs_key *key)
16901 +{
16902 + /* dbg_lock must be held */
16903 + sprintf_key(c, key, dbg_key_buf1);
16904 + return dbg_key_buf1;
16905 +}
16906 +
16907 +const char *dbg_ntype(int type)
16908 +{
16909 + switch (type) {
16910 + case UBIFS_PAD_NODE:
16911 + return "padding node";
16912 + case UBIFS_SB_NODE:
16913 + return "superblock node";
16914 + case UBIFS_MST_NODE:
16915 + return "master node";
16916 + case UBIFS_REF_NODE:
16917 + return "reference node";
16918 + case UBIFS_INO_NODE:
16919 + return "inode node";
16920 + case UBIFS_DENT_NODE:
16921 + return "direntry node";
16922 + case UBIFS_XENT_NODE:
16923 + return "xentry node";
16924 + case UBIFS_DATA_NODE:
16925 + return "data node";
16926 + case UBIFS_TRUN_NODE:
16927 + return "truncate node";
16928 + case UBIFS_IDX_NODE:
16929 + return "indexing node";
16930 + case UBIFS_CS_NODE:
16931 + return "commit start node";
16932 + case UBIFS_ORPH_NODE:
16933 + return "orphan node";
16934 + default:
16935 + return "unknown node";
16936 + }
16937 +}
16938 +
16939 +static const char *dbg_gtype(int type)
16940 +{
16941 + switch (type) {
16942 + case UBIFS_NO_NODE_GROUP:
16943 + return "no node group";
16944 + case UBIFS_IN_NODE_GROUP:
16945 + return "in node group";
16946 + case UBIFS_LAST_OF_NODE_GROUP:
16947 + return "last of node group";
16948 + default:
16949 + return "unknown";
16950 + }
16951 +}
16952 +
16953 +const char *dbg_cstate(int cmt_state)
16954 +{
16955 + switch (cmt_state) {
16956 + case COMMIT_RESTING:
16957 + return "commit resting";
16958 + case COMMIT_BACKGROUND:
16959 + return "background commit requested";
16960 + case COMMIT_REQUIRED:
16961 + return "commit required";
16962 + case COMMIT_RUNNING_BACKGROUND:
16963 + return "BACKGROUND commit running";
16964 + case COMMIT_RUNNING_REQUIRED:
16965 + return "commit running and required";
16966 + case COMMIT_BROKEN:
16967 + return "broken commit";
16968 + default:
16969 + return "unknown commit state";
16970 + }
16971 +}
16972 +
16973 +static void dump_ch(const struct ubifs_ch *ch)
16974 +{
16975 + printk(KERN_DEBUG "\tmagic %#x\n", le32_to_cpu(ch->magic));
16976 + printk(KERN_DEBUG "\tcrc %#x\n", le32_to_cpu(ch->crc));
16977 + printk(KERN_DEBUG "\tnode_type %d (%s)\n", ch->node_type,
16978 + dbg_ntype(ch->node_type));
16979 + printk(KERN_DEBUG "\tgroup_type %d (%s)\n", ch->group_type,
16980 + dbg_gtype(ch->group_type));
16981 + printk(KERN_DEBUG "\tsqnum %llu\n",
16982 + (unsigned long long)le64_to_cpu(ch->sqnum));
16983 + printk(KERN_DEBUG "\tlen %u\n", le32_to_cpu(ch->len));
16984 +}
16985 +
16986 +void dbg_dump_inode(const struct ubifs_info *c, const struct inode *inode)
16987 +{
16988 + const struct ubifs_inode *ui = ubifs_inode(inode);
16989 +
16990 + printk(KERN_DEBUG "inode %lu\n", inode->i_ino);
16991 + printk(KERN_DEBUG "size %llu\n",
16992 + (unsigned long long)i_size_read(inode));
16993 + printk(KERN_DEBUG "nlink %u\n", inode->i_nlink);
16994 + printk(KERN_DEBUG "uid %u\n", (unsigned int)inode->i_uid);
16995 + printk(KERN_DEBUG "gid %u\n", (unsigned int)inode->i_gid);
16996 + printk(KERN_DEBUG "atime %u.%u\n",
16997 + (unsigned int)inode->i_atime.tv_sec,
16998 + (unsigned int)inode->i_atime.tv_nsec);
16999 + printk(KERN_DEBUG "mtime %u.%u\n",
17000 + (unsigned int)inode->i_mtime.tv_sec,
17001 + (unsigned int)inode->i_mtime.tv_nsec);
17002 + printk(KERN_DEBUG "ctime %u.%u\n",
17003 + (unsigned int)inode->i_ctime.tv_sec,
17004 + (unsigned int)inode->i_ctime.tv_nsec);
17005 + printk(KERN_DEBUG "creat_sqnum %llu\n", ui->creat_sqnum);
17006 + printk(KERN_DEBUG "xattr_size %lld\n", ui->xattr_size);
17007 + printk(KERN_DEBUG "xattr_cnt %d\n", ui->xattr_cnt);
17008 + printk(KERN_DEBUG "xattr_names %d\n", ui->xattr_names);
17009 + printk(KERN_DEBUG "dirty %u\n", ui->dirty);
17010 + printk(KERN_DEBUG "xattr %u\n", ui->xattr);
17011 + printk(KERN_DEBUG "flags %d\n", ui->flags);
17012 + printk(KERN_DEBUG "compr_type %d\n", ui->compr_type);
17013 + printk(KERN_DEBUG "data_len %d\n", ui->data_len);
17014 +}
17015 +
17016 +void dbg_dump_node(const struct ubifs_info *c, const void *node)
17017 +{
17018 + int i, n;
17019 + union ubifs_key key;
17020 + const struct ubifs_ch *ch = node;
17021 +
17022 + if (dbg_failure_mode)
17023 + return;
17024 +
17025 + /* If the magic is incorrect, just hexdump the first bytes */
17026 + if (le32_to_cpu(ch->magic) != UBIFS_NODE_MAGIC) {
17027 + printk(KERN_DEBUG "Not a node, first %zu bytes:", UBIFS_CH_SZ);
17028 + print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1,
17029 + (void *)node, UBIFS_CH_SZ, 1);
17030 + return;
17031 + }
17032 +
17033 + spin_lock(&dbg_lock);
17034 + dump_ch(node);
17035 +
17036 + switch (ch->node_type) {
17037 + case UBIFS_PAD_NODE:
17038 + {
17039 + const struct ubifs_pad_node *pad = node;
17040 +
17041 + printk(KERN_DEBUG "\tpad_len %u\n",
17042 + le32_to_cpu(pad->pad_len));
17043 + break;
17044 + }
17045 + case UBIFS_SB_NODE:
17046 + {
17047 + const struct ubifs_sb_node *sup = node;
17048 + unsigned int sup_flags = le32_to_cpu(sup->flags);
17049 +
17050 + printk(KERN_DEBUG "\tkey_hash %d (%s)\n",
17051 + (int)sup->key_hash, get_key_hash(sup->key_hash));
17052 + printk(KERN_DEBUG "\tkey_fmt %d (%s)\n",
17053 + (int)sup->key_fmt, get_key_fmt(sup->key_fmt));
17054 + printk(KERN_DEBUG "\tflags %#x\n", sup_flags);
17055 + printk(KERN_DEBUG "\t big_lpt %u\n",
17056 + !!(sup_flags & UBIFS_FLG_BIGLPT));
17057 + printk(KERN_DEBUG "\tmin_io_size %u\n",
17058 + le32_to_cpu(sup->min_io_size));
17059 + printk(KERN_DEBUG "\tleb_size %u\n",
17060 + le32_to_cpu(sup->leb_size));
17061 + printk(KERN_DEBUG "\tleb_cnt %u\n",
17062 + le32_to_cpu(sup->leb_cnt));
17063 + printk(KERN_DEBUG "\tmax_leb_cnt %u\n",
17064 + le32_to_cpu(sup->max_leb_cnt));
17065 + printk(KERN_DEBUG "\tmax_bud_bytes %llu\n",
17066 + (unsigned long long)le64_to_cpu(sup->max_bud_bytes));
17067 + printk(KERN_DEBUG "\tlog_lebs %u\n",
17068 + le32_to_cpu(sup->log_lebs));
17069 + printk(KERN_DEBUG "\tlpt_lebs %u\n",
17070 + le32_to_cpu(sup->lpt_lebs));
17071 + printk(KERN_DEBUG "\torph_lebs %u\n",
17072 + le32_to_cpu(sup->orph_lebs));
17073 + printk(KERN_DEBUG "\tjhead_cnt %u\n",
17074 + le32_to_cpu(sup->jhead_cnt));
17075 + printk(KERN_DEBUG "\tfanout %u\n",
17076 + le32_to_cpu(sup->fanout));
17077 + printk(KERN_DEBUG "\tlsave_cnt %u\n",
17078 + le32_to_cpu(sup->lsave_cnt));
17079 + printk(KERN_DEBUG "\tdefault_compr %u\n",
17080 + (int)le16_to_cpu(sup->default_compr));
17081 + printk(KERN_DEBUG "\trp_size %llu\n",
17082 + (unsigned long long)le64_to_cpu(sup->rp_size));
17083 + printk(KERN_DEBUG "\trp_uid %u\n",
17084 + le32_to_cpu(sup->rp_uid));
17085 + printk(KERN_DEBUG "\trp_gid %u\n",
17086 + le32_to_cpu(sup->rp_gid));
17087 + printk(KERN_DEBUG "\tfmt_version %u\n",
17088 + le32_to_cpu(sup->fmt_version));
17089 + printk(KERN_DEBUG "\ttime_gran %u\n",
17090 + le32_to_cpu(sup->time_gran));
17091 + printk(KERN_DEBUG "\tUUID %02X%02X%02X%02X-%02X%02X"
17092 + "-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X\n",
17093 + sup->uuid[0], sup->uuid[1], sup->uuid[2], sup->uuid[3],
17094 + sup->uuid[4], sup->uuid[5], sup->uuid[6], sup->uuid[7],
17095 + sup->uuid[8], sup->uuid[9], sup->uuid[10], sup->uuid[11],
17096 + sup->uuid[12], sup->uuid[13], sup->uuid[14],
17097 + sup->uuid[15]);
17098 + break;
17099 + }
17100 + case UBIFS_MST_NODE:
17101 + {
17102 + const struct ubifs_mst_node *mst = node;
17103 +
17104 + printk(KERN_DEBUG "\thighest_inum %llu\n",
17105 + (unsigned long long)le64_to_cpu(mst->highest_inum));
17106 + printk(KERN_DEBUG "\tcommit number %llu\n",
17107 + (unsigned long long)le64_to_cpu(mst->cmt_no));
17108 + printk(KERN_DEBUG "\tflags %#x\n",
17109 + le32_to_cpu(mst->flags));
17110 + printk(KERN_DEBUG "\tlog_lnum %u\n",
17111 + le32_to_cpu(mst->log_lnum));
17112 + printk(KERN_DEBUG "\troot_lnum %u\n",
17113 + le32_to_cpu(mst->root_lnum));
17114 + printk(KERN_DEBUG "\troot_offs %u\n",
17115 + le32_to_cpu(mst->root_offs));
17116 + printk(KERN_DEBUG "\troot_len %u\n",
17117 + le32_to_cpu(mst->root_len));
17118 + printk(KERN_DEBUG "\tgc_lnum %u\n",
17119 + le32_to_cpu(mst->gc_lnum));
17120 + printk(KERN_DEBUG "\tihead_lnum %u\n",
17121 + le32_to_cpu(mst->ihead_lnum));
17122 + printk(KERN_DEBUG "\tihead_offs %u\n",
17123 + le32_to_cpu(mst->ihead_offs));
17124 + printk(KERN_DEBUG "\tindex_size %u\n",
17125 + le32_to_cpu(mst->index_size));
17126 + printk(KERN_DEBUG "\tlpt_lnum %u\n",
17127 + le32_to_cpu(mst->lpt_lnum));
17128 + printk(KERN_DEBUG "\tlpt_offs %u\n",
17129 + le32_to_cpu(mst->lpt_offs));
17130 + printk(KERN_DEBUG "\tnhead_lnum %u\n",
17131 + le32_to_cpu(mst->nhead_lnum));
17132 + printk(KERN_DEBUG "\tnhead_offs %u\n",
17133 + le32_to_cpu(mst->nhead_offs));
17134 + printk(KERN_DEBUG "\tltab_lnum %u\n",
17135 + le32_to_cpu(mst->ltab_lnum));
17136 + printk(KERN_DEBUG "\tltab_offs %u\n",
17137 + le32_to_cpu(mst->ltab_offs));
17138 + printk(KERN_DEBUG "\tlsave_lnum %u\n",
17139 + le32_to_cpu(mst->lsave_lnum));
17140 + printk(KERN_DEBUG "\tlsave_offs %u\n",
17141 + le32_to_cpu(mst->lsave_offs));
17142 + printk(KERN_DEBUG "\tlscan_lnum %u\n",
17143 + le32_to_cpu(mst->lscan_lnum));
17144 + printk(KERN_DEBUG "\tleb_cnt %u\n",
17145 + le32_to_cpu(mst->leb_cnt));
17146 + printk(KERN_DEBUG "\tempty_lebs %u\n",
17147 + le32_to_cpu(mst->empty_lebs));
17148 + printk(KERN_DEBUG "\tidx_lebs %u\n",
17149 + le32_to_cpu(mst->idx_lebs));
17150 + printk(KERN_DEBUG "\ttotal_free %llu\n",
17151 + (unsigned long long)le64_to_cpu(mst->total_free));
17152 + printk(KERN_DEBUG "\ttotal_dirty %llu\n",
17153 + (unsigned long long)le64_to_cpu(mst->total_dirty));
17154 + printk(KERN_DEBUG "\ttotal_used %llu\n",
17155 + (unsigned long long)le64_to_cpu(mst->total_used));
17156 + printk(KERN_DEBUG "\ttotal_dead %llu\n",
17157 + (unsigned long long)le64_to_cpu(mst->total_dead));
17158 + printk(KERN_DEBUG "\ttotal_dark %llu\n",
17159 + (unsigned long long)le64_to_cpu(mst->total_dark));
17160 + break;
17161 + }
17162 + case UBIFS_REF_NODE:
17163 + {
17164 + const struct ubifs_ref_node *ref = node;
17165 +
17166 + printk(KERN_DEBUG "\tlnum %u\n",
17167 + le32_to_cpu(ref->lnum));
17168 + printk(KERN_DEBUG "\toffs %u\n",
17169 + le32_to_cpu(ref->offs));
17170 + printk(KERN_DEBUG "\tjhead %u\n",
17171 + le32_to_cpu(ref->jhead));
17172 + break;
17173 + }
17174 + case UBIFS_INO_NODE:
17175 + {
17176 + const struct ubifs_ino_node *ino = node;
17177 +
17178 + key_read(c, &ino->key, &key);
17179 + printk(KERN_DEBUG "\tkey %s\n", DBGKEY(&key));
17180 + printk(KERN_DEBUG "\tcreat_sqnum %llu\n",
17181 + (unsigned long long)le64_to_cpu(ino->creat_sqnum));
17182 + printk(KERN_DEBUG "\tsize %llu\n",
17183 + (unsigned long long)le64_to_cpu(ino->size));
17184 + printk(KERN_DEBUG "\tnlink %u\n",
17185 + le32_to_cpu(ino->nlink));
17186 + printk(KERN_DEBUG "\tatime %lld.%u\n",
17187 + (long long)le64_to_cpu(ino->atime_sec),
17188 + le32_to_cpu(ino->atime_nsec));
17189 + printk(KERN_DEBUG "\tmtime %lld.%u\n",
17190 + (long long)le64_to_cpu(ino->mtime_sec),
17191 + le32_to_cpu(ino->mtime_nsec));
17192 + printk(KERN_DEBUG "\tctime %lld.%u\n",
17193 + (long long)le64_to_cpu(ino->ctime_sec),
17194 + le32_to_cpu(ino->ctime_nsec));
17195 + printk(KERN_DEBUG "\tuid %u\n",
17196 + le32_to_cpu(ino->uid));
17197 + printk(KERN_DEBUG "\tgid %u\n",
17198 + le32_to_cpu(ino->gid));
17199 + printk(KERN_DEBUG "\tmode %u\n",
17200 + le32_to_cpu(ino->mode));
17201 + printk(KERN_DEBUG "\tflags %#x\n",
17202 + le32_to_cpu(ino->flags));
17203 + printk(KERN_DEBUG "\txattr_cnt %u\n",
17204 + le32_to_cpu(ino->xattr_cnt));
17205 + printk(KERN_DEBUG "\txattr_size %llu\n",
17206 + (unsigned long long)le64_to_cpu(ino->xattr_size));
17207 + printk(KERN_DEBUG "\txattr_names %u\n",
17208 + le32_to_cpu(ino->xattr_names));
17209 + printk(KERN_DEBUG "\tcompr_type %#x\n",
17210 + (int)le16_to_cpu(ino->compr_type));
17211 + printk(KERN_DEBUG "\tdata len %u\n",
17212 + le32_to_cpu(ino->data_len));
17213 + break;
17214 + }
17215 + case UBIFS_DENT_NODE:
17216 + case UBIFS_XENT_NODE:
17217 + {
17218 + const struct ubifs_dent_node *dent = node;
17219 + int nlen = le16_to_cpu(dent->nlen);
17220 +
17221 + key_read(c, &dent->key, &key);
17222 + printk(KERN_DEBUG "\tkey %s\n", DBGKEY(&key));
17223 + printk(KERN_DEBUG "\tinum %llu\n",
17224 + (unsigned long long)le64_to_cpu(dent->inum));
17225 + printk(KERN_DEBUG "\ttype %d\n", (int)dent->type);
17226 + printk(KERN_DEBUG "\tnlen %d\n", nlen);
17227 + printk(KERN_DEBUG "\tname ");
17228 +
17229 + if (nlen > UBIFS_MAX_NLEN)
17230 + printk(KERN_DEBUG "(bad name length, not printing, "
17231 + "bad or corrupted node)");
17232 + else {
17233 + for (i = 0; i < nlen && dent->name[i]; i++)
17234 + printk("%c", dent->name[i]);
17235 + }
17236 + printk("\n");
17237 +
17238 + break;
17239 + }
17240 + case UBIFS_DATA_NODE:
17241 + {
17242 + const struct ubifs_data_node *dn = node;
17243 + int dlen = le32_to_cpu(ch->len) - UBIFS_DATA_NODE_SZ;
17244 +
17245 + key_read(c, &dn->key, &key);
17246 + printk(KERN_DEBUG "\tkey %s\n", DBGKEY(&key));
17247 + printk(KERN_DEBUG "\tsize %u\n",
17248 + le32_to_cpu(dn->size));
17249 + printk(KERN_DEBUG "\tcompr_typ %d\n",
17250 + (int)le16_to_cpu(dn->compr_type));
17251 + printk(KERN_DEBUG "\tdata size %d\n",
17252 + dlen);
17253 + printk(KERN_DEBUG "\tdata:\n");
17254 + print_hex_dump(KERN_DEBUG, "\t", DUMP_PREFIX_OFFSET, 32, 1,
17255 + (void *)&dn->data, dlen, 0);
17256 + break;
17257 + }
17258 + case UBIFS_TRUN_NODE:
17259 + {
17260 + const struct ubifs_trun_node *trun = node;
17261 +
17262 + printk(KERN_DEBUG "\tinum %u\n",
17263 + le32_to_cpu(trun->inum));
17264 + printk(KERN_DEBUG "\told_size %llu\n",
17265 + (unsigned long long)le64_to_cpu(trun->old_size));
17266 + printk(KERN_DEBUG "\tnew_size %llu\n",
17267 + (unsigned long long)le64_to_cpu(trun->new_size));
17268 + break;
17269 + }
17270 + case UBIFS_IDX_NODE:
17271 + {
17272 + const struct ubifs_idx_node *idx = node;
17273 +
17274 + n = le16_to_cpu(idx->child_cnt);
17275 + printk(KERN_DEBUG "\tchild_cnt %d\n", n);
17276 + printk(KERN_DEBUG "\tlevel %d\n",
17277 + (int)le16_to_cpu(idx->level));
17278 + printk(KERN_DEBUG "\tBranches:\n");
17279 +
17280 + for (i = 0; i < n && i < c->fanout - 1; i++) {
17281 + const struct ubifs_branch *br;
17282 +
17283 + br = ubifs_idx_branch(c, idx, i);
17284 + key_read(c, &br->key, &key);
17285 + printk(KERN_DEBUG "\t%d: LEB %d:%d len %d key %s\n",
17286 + i, le32_to_cpu(br->lnum), le32_to_cpu(br->offs),
17287 + le32_to_cpu(br->len), DBGKEY(&key));
17288 + }
17289 + break;
17290 + }
17291 + case UBIFS_CS_NODE:
17292 + break;
17293 + case UBIFS_ORPH_NODE:
17294 + {
17295 + const struct ubifs_orph_node *orph = node;
17296 +
17297 + printk(KERN_DEBUG "\tcommit number %llu\n",
17298 + (unsigned long long)
17299 + le64_to_cpu(orph->cmt_no) & LLONG_MAX);
17300 + printk(KERN_DEBUG "\tlast node flag %llu\n",
17301 + (unsigned long long)(le64_to_cpu(orph->cmt_no)) >> 63);
17302 + n = (le32_to_cpu(ch->len) - UBIFS_ORPH_NODE_SZ) >> 3;
17303 + printk(KERN_DEBUG "\t%d orphan inode numbers:\n", n);
17304 + for (i = 0; i < n; i++)
17305 + printk(KERN_DEBUG "\t ino %llu\n",
17306 + le64_to_cpu(orph->inos[i]));
17307 + break;
17308 + }
17309 + default:
17310 + printk(KERN_DEBUG "node type %d was not recognized\n",
17311 + (int)ch->node_type);
17312 + }
17313 + spin_unlock(&dbg_lock);
17314 +}
17315 +
17316 +void dbg_dump_budget_req(const struct ubifs_budget_req *req)
17317 +{
17318 + spin_lock(&dbg_lock);
17319 + printk(KERN_DEBUG "Budgeting request: new_ino %d, dirtied_ino %d\n",
17320 + req->new_ino, req->dirtied_ino);
17321 + printk(KERN_DEBUG "\tnew_ino_d %d, dirtied_ino_d %d\n",
17322 + req->new_ino_d, req->dirtied_ino_d);
17323 + printk(KERN_DEBUG "\tnew_page %d, dirtied_page %d\n",
17324 + req->new_page, req->dirtied_page);
17325 + printk(KERN_DEBUG "\tnew_dent %d, mod_dent %d\n",
17326 + req->new_dent, req->mod_dent);
17327 + printk(KERN_DEBUG "\tidx_growth %d\n", req->idx_growth);
17328 + printk(KERN_DEBUG "\tdata_growth %d dd_growth %d\n",
17329 + req->data_growth, req->dd_growth);
17330 + spin_unlock(&dbg_lock);
17331 +}
17332 +
17333 +void dbg_dump_lstats(const struct ubifs_lp_stats *lst)
17334 +{
17335 + spin_lock(&dbg_lock);
17336 + printk(KERN_DEBUG "Lprops statistics: empty_lebs %d, idx_lebs %d\n",
17337 + lst->empty_lebs, lst->idx_lebs);
17338 + printk(KERN_DEBUG "\ttaken_empty_lebs %d, total_free %lld, "
17339 + "total_dirty %lld\n", lst->taken_empty_lebs, lst->total_free,
17340 + lst->total_dirty);
17341 + printk(KERN_DEBUG "\ttotal_used %lld, total_dark %lld, "
17342 + "total_dead %lld\n", lst->total_used, lst->total_dark,
17343 + lst->total_dead);
17344 + spin_unlock(&dbg_lock);
17345 +}
17346 +
17347 +void dbg_dump_budg(struct ubifs_info *c)
17348 +{
17349 + int i;
17350 + struct rb_node *rb;
17351 + struct ubifs_bud *bud;
17352 + struct ubifs_gced_idx_leb *idx_gc;
17353 +
17354 + spin_lock(&dbg_lock);
17355 + printk(KERN_DEBUG "Budgeting info: budg_data_growth %lld, "
17356 + "budg_dd_growth %lld, budg_idx_growth %lld\n",
17357 + c->budg_data_growth, c->budg_dd_growth, c->budg_idx_growth);
17358 + printk(KERN_DEBUG "\tdata budget sum %lld, total budget sum %lld, "
17359 + "freeable_cnt %d\n", c->budg_data_growth + c->budg_dd_growth,
17360 + c->budg_data_growth + c->budg_dd_growth + c->budg_idx_growth,
17361 + c->freeable_cnt);
17362 + printk(KERN_DEBUG "\tmin_idx_lebs %d, old_idx_sz %lld, "
17363 + "calc_idx_sz %lld, idx_gc_cnt %d\n", c->min_idx_lebs,
17364 + c->old_idx_sz, c->calc_idx_sz, c->idx_gc_cnt);
17365 + printk(KERN_DEBUG "\tdirty_pg_cnt %ld, dirty_ino_cnt %ld, "
17366 + "dirty_zn_cnt %ld, clean_zn_cnt %ld\n",
17367 + atomic_long_read(&c->dirty_pg_cnt),
17368 + atomic_long_read(&c->dirty_ino_cnt),
17369 + atomic_long_read(&c->dirty_zn_cnt),
17370 + atomic_long_read(&c->clean_zn_cnt));
17371 + printk(KERN_DEBUG "\tdark_wm %d, dead_wm %d, max_idx_node_sz %d\n",
17372 + c->dark_wm, c->dead_wm, c->max_idx_node_sz);
17373 + printk(KERN_DEBUG "\tgc_lnum %d, ihead_lnum %d\n",
17374 + c->gc_lnum, c->ihead_lnum);
17375 + for (i = 0; i < c->jhead_cnt; i++)
17376 + printk(KERN_DEBUG "\tjhead %d\t LEB %d\n",
17377 + c->jheads[i].wbuf.jhead, c->jheads[i].wbuf.lnum);
17378 + for (rb = rb_first(&c->buds); rb; rb = rb_next(rb)) {
17379 + bud = rb_entry(rb, struct ubifs_bud, rb);
17380 + printk(KERN_DEBUG "\tbud LEB %d\n", bud->lnum);
17381 + }
17382 + list_for_each_entry(bud, &c->old_buds, list)
17383 + printk(KERN_DEBUG "\told bud LEB %d\n", bud->lnum);
17384 + list_for_each_entry(idx_gc, &c->idx_gc, list)
17385 + printk(KERN_DEBUG "\tGC'ed idx LEB %d unmap %d\n",
17386 + idx_gc->lnum, idx_gc->unmap);
17387 + printk(KERN_DEBUG "\tcommit state %d\n", c->cmt_state);
17388 + spin_unlock(&dbg_lock);
17389 +}
17390 +
17391 +void dbg_dump_lprop(const struct ubifs_info *c, const struct ubifs_lprops *lp)
17392 +{
17393 + printk(KERN_DEBUG "LEB %d lprops: free %d, dirty %d (used %d), "
17394 + "flags %#x\n", lp->lnum, lp->free, lp->dirty,
17395 + c->leb_size - lp->free - lp->dirty, lp->flags);
17396 +}
17397 +
17398 +void dbg_dump_lprops(struct ubifs_info *c)
17399 +{
17400 + int lnum, err;
17401 + struct ubifs_lprops lp;
17402 + struct ubifs_lp_stats lst;
17403 +
17404 + printk(KERN_DEBUG "Dumping LEB properties\n");
17405 + ubifs_get_lp_stats(c, &lst);
17406 + dbg_dump_lstats(&lst);
17407 +
17408 + for (lnum = c->main_first; lnum < c->leb_cnt; lnum++) {
17409 + err = ubifs_read_one_lp(c, lnum, &lp);
17410 + if (err)
17411 + ubifs_err("cannot read lprops for LEB %d", lnum);
17412 +
17413 + dbg_dump_lprop(c, &lp);
17414 + }
17415 +}
17416 +
17417 +void dbg_dump_leb(const struct ubifs_info *c, int lnum)
17418 +{
17419 + struct ubifs_scan_leb *sleb;
17420 + struct ubifs_scan_node *snod;
17421 +
17422 + if (dbg_failure_mode)
17423 + return;
17424 +
17425 + printk(KERN_DEBUG "Dumping LEB %d\n", lnum);
17426 +
17427 + sleb = ubifs_scan(c, lnum, 0, c->dbg_buf);
17428 + if (IS_ERR(sleb)) {
17429 + ubifs_err("scan error %d", (int)PTR_ERR(sleb));
17430 + return;
17431 + }
17432 +
17433 + printk(KERN_DEBUG "LEB %d has %d nodes ending at %d\n", lnum,
17434 + sleb->nodes_cnt, sleb->endpt);
17435 +
17436 + list_for_each_entry(snod, &sleb->nodes, list) {
17437 + cond_resched();
17438 + printk(KERN_DEBUG "Dumping node at LEB %d:%d len %d\n", lnum,
17439 + snod->offs, snod->len);
17440 + dbg_dump_node(c, snod->node);
17441 + }
17442 +
17443 + ubifs_scan_destroy(sleb);
17444 + return;
17445 +}
17446 +
17447 +void dbg_dump_znode(const struct ubifs_info *c,
17448 + const struct ubifs_znode *znode)
17449 +{
17450 + int n;
17451 + const struct ubifs_zbranch *zbr;
17452 +
17453 + spin_lock(&dbg_lock);
17454 + if (znode->parent)
17455 + zbr = &znode->parent->zbranch[znode->iip];
17456 + else
17457 + zbr = &c->zroot;
17458 +
17459 + printk(KERN_DEBUG "znode %p, LEB %d:%d len %d parent %p iip %d level %d"
17460 + " child_cnt %d flags %lx\n", znode, zbr->lnum, zbr->offs,
17461 + zbr->len, znode->parent, znode->iip, znode->level,
17462 + znode->child_cnt, znode->flags);
17463 +
17464 + if (znode->child_cnt <= 0 || znode->child_cnt > c->fanout) {
17465 + spin_unlock(&dbg_lock);
17466 + return;
17467 + }
17468 +
17469 + printk(KERN_DEBUG "zbranches:\n");
17470 + for (n = 0; n < znode->child_cnt; n++) {
17471 + zbr = &znode->zbranch[n];
17472 + if (znode->level > 0)
17473 + printk(KERN_DEBUG "\t%d: znode %p LEB %d:%d len %d key "
17474 + "%s\n", n, zbr->znode, zbr->lnum,
17475 + zbr->offs, zbr->len,
17476 + DBGKEY(&zbr->key));
17477 + else
17478 + printk(KERN_DEBUG "\t%d: LNC %p LEB %d:%d len %d key "
17479 + "%s\n", n, zbr->znode, zbr->lnum,
17480 + zbr->offs, zbr->len,
17481 + DBGKEY(&zbr->key));
17482 + }
17483 + spin_unlock(&dbg_lock);
17484 +}
17485 +
17486 +void dbg_dump_heap(struct ubifs_info *c, struct ubifs_lpt_heap *heap, int cat)
17487 +{
17488 + int i;
17489 +
17490 + printk(KERN_DEBUG "Dumping heap cat %d (%d elements)\n",
17491 + cat, heap->cnt);
17492 + for (i = 0; i < heap->cnt; i++) {
17493 + struct ubifs_lprops *lprops = heap->arr[i];
17494 +
17495 + printk(KERN_DEBUG "\t%d. LEB %d hpos %d free %d dirty %d "
17496 + "flags %d\n", i, lprops->lnum, lprops->hpos,
17497 + lprops->free, lprops->dirty, lprops->flags);
17498 + }
17499 +}
17500 +
17501 +void dbg_dump_pnode(struct ubifs_info *c, struct ubifs_pnode *pnode,
17502 + struct ubifs_nnode *parent, int iip)
17503 +{
17504 + int i;
17505 +
17506 + printk(KERN_DEBUG "Dumping pnode:\n");
17507 + printk(KERN_DEBUG "\taddress %zx parent %zx cnext %zx\n",
17508 + (size_t)pnode, (size_t)parent, (size_t)pnode->cnext);
17509 + printk(KERN_DEBUG "\tflags %lu iip %d level %d num %d\n",
17510 + pnode->flags, iip, pnode->level, pnode->num);
17511 + for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
17512 + struct ubifs_lprops *lp = &pnode->lprops[i];
17513 +
17514 + printk(KERN_DEBUG "\t%d: free %d dirty %d flags %d lnum %d\n",
17515 + i, lp->free, lp->dirty, lp->flags, lp->lnum);
17516 + }
17517 +}
17518 +
17519 +void dbg_dump_tnc(struct ubifs_info *c)
17520 +{
17521 + struct ubifs_znode *znode;
17522 + int level;
17523 +
17524 + printk(KERN_DEBUG "\n");
17525 + printk(KERN_DEBUG "Dumping the TNC tree\n");
17526 + znode = ubifs_tnc_levelorder_next(c->zroot.znode, NULL);
17527 + level = znode->level;
17528 + printk(KERN_DEBUG "== Level %d ==\n", level);
17529 + while (znode) {
17530 + if (level != znode->level) {
17531 + level = znode->level;
17532 + printk(KERN_DEBUG "== Level %d ==\n", level);
17533 + }
17534 + dbg_dump_znode(c, znode);
17535 + znode = ubifs_tnc_levelorder_next(c->zroot.znode, znode);
17536 + }
17537 +
17538 + printk(KERN_DEBUG "\n");
17539 +}
17540 +
17541 +static int dump_znode(struct ubifs_info *c, struct ubifs_znode *znode,
17542 + void *priv)
17543 +{
17544 + dbg_dump_znode(c, znode);
17545 + return 0;
17546 +}
17547 +
17548 +/**
17549 + * dbg_dump_index - dump the on-flash index.
17550 + * @c: UBIFS file-system description object
17551 + *
17552 + * This function dumps whole UBIFS indexing B-tree, unlike 'dbg_dump_tnc()'
17553 + * which dumps only in-memory znodes and does not read znodes which from flash.
17554 + */
17555 +void dbg_dump_index(struct ubifs_info *c)
17556 +{
17557 + dbg_walk_index(c, NULL, dump_znode, NULL);
17558 +}
17559 +
17560 +/*
17561 + * dbg_check_dir - check directory inode size and link count.
17562 + * @c: UBIFS file-system description object
17563 + * @dir: the directory to calculate size for
17564 + * @size: the result is returned here
17565 + *
17566 + * This function makes sure that directory size and link count are correct.
17567 + * Returns zero in case of success and a negative error code in case of
17568 + * failure.
17569 + *
17570 + * Note, it is good idea to make sure the @dir->i_mutex is locked before
17571 + * calling this function.
17572 + */
17573 +int dbg_check_dir_size(struct ubifs_info *c, const struct inode *dir)
17574 +{
17575 + unsigned int nlink = 2;
17576 + union ubifs_key key;
17577 + struct ubifs_dent_node *dent, *pdent = NULL;
17578 + struct qstr nm = { .name = NULL };
17579 + loff_t size = UBIFS_INO_NODE_SZ;
17580 +
17581 + if (!(ubifs_chk_flags & UBIFS_CHK_GEN))
17582 + return 0;
17583 +
17584 + if (!S_ISDIR(dir->i_mode))
17585 + return 0;
17586 +
17587 + lowest_dent_key(c, &key, dir->i_ino);
17588 + while (1) {
17589 + int err;
17590 +
17591 + dent = ubifs_tnc_next_ent(c, &key, &nm);
17592 + if (IS_ERR(dent)) {
17593 + err = PTR_ERR(dent);
17594 + if (err == -ENOENT)
17595 + break;
17596 + return err;
17597 + }
17598 +
17599 + nm.name = dent->name;
17600 + nm.len = le16_to_cpu(dent->nlen);
17601 + size += CALC_DENT_SIZE(nm.len);
17602 + if (dent->type == UBIFS_ITYPE_DIR)
17603 + nlink += 1;
17604 + kfree(pdent);
17605 + pdent = dent;
17606 + key_read(c, &dent->key, &key);
17607 + }
17608 + kfree(pdent);
17609 +
17610 + if (i_size_read(dir) != size) {
17611 + ubifs_err("directory inode %lu has size %llu, "
17612 + "but calculated size is %llu", dir->i_ino,
17613 + (unsigned long long)i_size_read(dir),
17614 + (unsigned long long)size);
17615 + dump_stack();
17616 + return -EINVAL;
17617 + }
17618 + if (dir->i_nlink != nlink) {
17619 + ubifs_err("directory inode %lu has nlink %u, but calculated "
17620 + "nlink is %u", dir->i_ino, dir->i_nlink, nlink);
17621 + dump_stack();
17622 + return -EINVAL;
17623 + }
17624 +
17625 + return 0;
17626 +}
17627 +
17628 +/**
17629 + * dbg_check_key_order - make sure that colliding keys are properly ordered.
17630 + * @c: UBIFS file-system description object
17631 + * @zbr1: first zbranch
17632 + * @zbr1: following zbranch
17633 + *
17634 + * In UBIFS indexing B-tree colliding keys has to be sorted in binary order of
17635 + * names of the direntries/xentries which are referred by the keys. This
17636 + * function reads direntries/xentries referred by @zbr1 and @zbr2 and makes
17637 + * sure the name of direntry/xentry referred by @zbr1 is less than
17638 + * direntry/xentry referred by @zbr2. Returns zero if this is true, %1 if not,
17639 + * and a negative error code in case of failure.
17640 + */
17641 +static int dbg_check_key_order(struct ubifs_info *c, struct ubifs_zbranch *zbr1,
17642 + struct ubifs_zbranch *zbr2)
17643 +{
17644 + int err, nlen1, nlen2, cmp;
17645 + struct ubifs_dent_node *dent1, *dent2;
17646 + union ubifs_key key;
17647 +
17648 + ubifs_assert(!keys_cmp(c, &zbr1->key, &zbr2->key));
17649 + dent1 = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS);
17650 + if (!dent1)
17651 + return -ENOMEM;
17652 + dent2 = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS);
17653 + if (!dent2) {
17654 + err = -ENOMEM;
17655 + goto out_free;
17656 + }
17657 +
17658 + err = ubifs_tnc_read_node(c, zbr1, dent1);
17659 + if (err)
17660 + goto out_free;
17661 + err = ubifs_validate_entry(c, dent1);
17662 + if (err)
17663 + goto out_free;
17664 +
17665 + err = ubifs_tnc_read_node(c, zbr2, dent2);
17666 + if (err)
17667 + goto out_free;
17668 + err = ubifs_validate_entry(c, dent2);
17669 + if (err)
17670 + goto out_free;
17671 +
17672 + /* Make sure node keys are the same as in zbranch */
17673 + err = 1;
17674 + key_read(c, &dent1->key, &key);
17675 + if (keys_cmp(c, &zbr1->key, &key)) {
17676 + dbg_err("1st entry at %d:%d has key %s", zbr1->lnum,
17677 + zbr1->offs, DBGKEY(&key));
17678 + dbg_err("but it should have key %s according to tnc",
17679 + DBGKEY(&zbr1->key));
17680 + dbg_dump_node(c, dent1);
17681 + goto out_free;
17682 + }
17683 +
17684 + key_read(c, &dent2->key, &key);
17685 + if (keys_cmp(c, &zbr2->key, &key)) {
17686 + dbg_err("2nd entry at %d:%d has key %s", zbr1->lnum,
17687 + zbr1->offs, DBGKEY(&key));
17688 + dbg_err("but it should have key %s according to tnc",
17689 + DBGKEY(&zbr2->key));
17690 + dbg_dump_node(c, dent2);
17691 + goto out_free;
17692 + }
17693 +
17694 + nlen1 = le16_to_cpu(dent1->nlen);
17695 + nlen2 = le16_to_cpu(dent2->nlen);
17696 +
17697 + cmp = memcmp(dent1->name, dent2->name, min_t(int, nlen1, nlen2));
17698 + if (cmp < 0 || (cmp == 0 && nlen1 < nlen2)) {
17699 + err = 0;
17700 + goto out_free;
17701 + }
17702 + if (cmp == 0 && nlen1 == nlen2)
17703 + dbg_err("2 xent/dent nodes with the same name");
17704 + else
17705 + dbg_err("bad order of colliding key %s",
17706 + DBGKEY(&key));
17707 +
17708 + dbg_msg("first node at %d:%d\n", zbr1->lnum, zbr1->offs);
17709 + dbg_dump_node(c, dent1);
17710 + dbg_msg("second node at %d:%d\n", zbr2->lnum, zbr2->offs);
17711 + dbg_dump_node(c, dent2);
17712 +
17713 +out_free:
17714 + kfree(dent2);
17715 + kfree(dent1);
17716 + return err;
17717 +}
17718 +
17719 +/**
17720 + * dbg_check_znode - check if znode is all right.
17721 + * @c: UBIFS file-system description object
17722 + * @zbr: zbranch which points to this znode
17723 + *
17724 + * This function makes sure that znode referred to by @zbr is all right.
17725 + * Returns zero if it is, and %-EINVAL if it is not.
17726 + */
17727 +static int dbg_check_znode(struct ubifs_info *c, struct ubifs_zbranch *zbr)
17728 +{
17729 + struct ubifs_znode *znode = zbr->znode;
17730 + struct ubifs_znode *zp = znode->parent;
17731 + int n, err, cmp;
17732 +
17733 + if (znode->child_cnt <= 0 || znode->child_cnt > c->fanout) {
17734 + err = 1;
17735 + goto out;
17736 + }
17737 + if (znode->level < 0) {
17738 + err = 2;
17739 + goto out;
17740 + }
17741 + if (znode->iip < 0 || znode->iip >= c->fanout) {
17742 + err = 3;
17743 + goto out;
17744 + }
17745 +
17746 + if (zbr->len == 0)
17747 + /* Only dirty zbranch may have no on-flash nodes */
17748 + if (!ubifs_zn_dirty(znode)) {
17749 + err = 4;
17750 + goto out;
17751 + }
17752 +
17753 + if (ubifs_zn_dirty(znode)) {
17754 + /*
17755 + * If znode is dirty, its parent has to be dirty as well. The
17756 + * order of the operation is important, so we have to have
17757 + * memory barriers.
17758 + */
17759 + smp_mb();
17760 + if (zp && !ubifs_zn_dirty(zp)) {
17761 + /*
17762 + * The dirty flag is atomic and is cleared outside the
17763 + * TNC mutex, so znode's dirty flag may now have
17764 + * been cleared. The child is always cleared before the
17765 + * parent, so we just need to check again.
17766 + */
17767 + smp_mb();
17768 + if (ubifs_zn_dirty(znode)) {
17769 + err = 5;
17770 + goto out;
17771 + }
17772 + }
17773 + }
17774 +
17775 + if (zp) {
17776 + const union ubifs_key *min, *max;
17777 +
17778 + if (znode->level != zp->level - 1) {
17779 + err = 6;
17780 + goto out;
17781 + }
17782 +
17783 + /* Make sure the 'parent' pointer in our znode is correct */
17784 + err = ubifs_search_zbranch(c, zp, &zbr->key, &n);
17785 + if (!err) {
17786 + /* This zbranch does not exist in the parent */
17787 + err = 7;
17788 + goto out;
17789 + }
17790 +
17791 + if (znode->iip >= zp->child_cnt) {
17792 + err = 8;
17793 + goto out;
17794 + }
17795 +
17796 + if (znode->iip != n) {
17797 + /* This may happen only in case of collisions */
17798 + if (keys_cmp(c, &zp->zbranch[n].key,
17799 + &zp->zbranch[znode->iip].key)) {
17800 + err = 9;
17801 + goto out;
17802 + }
17803 + n = znode->iip;
17804 + }
17805 +
17806 + /*
17807 + * Make sure that the first key in our znode is greater than or
17808 + * equal to the key in the pointing zbranch.
17809 + */
17810 + min = &zbr->key;
17811 + cmp = keys_cmp(c, min, &znode->zbranch[0].key);
17812 + if (cmp == 1) {
17813 + err = 10;
17814 + goto out;
17815 + }
17816 +
17817 + if (n + 1 < zp->child_cnt) {
17818 + max = &zp->zbranch[n + 1].key;
17819 +
17820 + /*
17821 + * Make sure the last key in our znode is less or
17822 + * equivalent than the the key in zbranch which goes
17823 + * after our pointing zbranch.
17824 + */
17825 + cmp = keys_cmp(c, max,
17826 + &znode->zbranch[znode->child_cnt - 1].key);
17827 + if (cmp == -1) {
17828 + err = 11;
17829 + goto out;
17830 + }
17831 + }
17832 + } else {
17833 + /* This may only be root znode */
17834 + if (zbr != &c->zroot) {
17835 + err = 12;
17836 + goto out;
17837 + }
17838 + }
17839 +
17840 + /*
17841 + * Make sure that next key is greater or equivalent then the previous
17842 + * one.
17843 + */
17844 + for (n = 1; n < znode->child_cnt; n++) {
17845 + cmp = keys_cmp(c, &znode->zbranch[n - 1].key,
17846 + &znode->zbranch[n].key);
17847 + if (cmp > 0) {
17848 + err = 13;
17849 + goto out;
17850 + }
17851 + if (cmp == 0) {
17852 + /* This can only be keys with colliding hash */
17853 + if (!is_hash_key(c, &znode->zbranch[n].key)) {
17854 + err = 14;
17855 + goto out;
17856 + }
17857 +
17858 + if (znode->level != 0 || c->replaying)
17859 + continue;
17860 +
17861 + /*
17862 + * Colliding keys should follow binary order of
17863 + * corresponding xentry/dentry names.
17864 + */
17865 + err = dbg_check_key_order(c, &znode->zbranch[n - 1],
17866 + &znode->zbranch[n]);
17867 + if (err < 0)
17868 + return err;
17869 + if (err) {
17870 + err = 15;
17871 + goto out;
17872 + }
17873 + }
17874 + }
17875 +
17876 + for (n = 0; n < znode->child_cnt; n++) {
17877 + if (!znode->zbranch[n].znode &&
17878 + (znode->zbranch[n].lnum == 0 ||
17879 + znode->zbranch[n].len == 0)) {
17880 + err = 16;
17881 + goto out;
17882 + }
17883 +
17884 + if (znode->zbranch[n].lnum != 0 &&
17885 + znode->zbranch[n].len == 0) {
17886 + err = 17;
17887 + goto out;
17888 + }
17889 +
17890 + if (znode->zbranch[n].lnum == 0 &&
17891 + znode->zbranch[n].len != 0) {
17892 + err = 18;
17893 + goto out;
17894 + }
17895 +
17896 + if (znode->zbranch[n].lnum == 0 &&
17897 + znode->zbranch[n].offs != 0) {
17898 + err = 19;
17899 + goto out;
17900 + }
17901 +
17902 + if (znode->level != 0 && znode->zbranch[n].znode)
17903 + if (znode->zbranch[n].znode->parent != znode) {
17904 + err = 20;
17905 + goto out;
17906 + }
17907 + }
17908 +
17909 + return 0;
17910 +
17911 +out:
17912 + ubifs_err("failed, error %d", err);
17913 + ubifs_msg("dump of the znode");
17914 + dbg_dump_znode(c, znode);
17915 + if (zp) {
17916 + ubifs_msg("dump of the parent znode");
17917 + dbg_dump_znode(c, zp);
17918 + }
17919 + dump_stack();
17920 + return -EINVAL;
17921 +}
17922 +
17923 +/**
17924 + * dbg_check_tnc - check TNC tree.
17925 + * @c: UBIFS file-system description object
17926 + * @extra: do extra checks that are possible at start commit
17927 + *
17928 + * This function traverses whole TNC tree and checks every znode. Returns zero
17929 + * if everything is all right and %-EINVAL if something is wrong with TNC.
17930 + */
17931 +int dbg_check_tnc(struct ubifs_info *c, int extra)
17932 +{
17933 + struct ubifs_znode *znode;
17934 + long clean_cnt = 0, dirty_cnt = 0;
17935 + int err, last;
17936 +
17937 + if (!(ubifs_chk_flags & UBIFS_CHK_TNC))
17938 + return 0;
17939 +
17940 + ubifs_assert(mutex_is_locked(&c->tnc_mutex));
17941 + if (!c->zroot.znode)
17942 + return 0;
17943 +
17944 + znode = ubifs_tnc_postorder_first(c->zroot.znode);
17945 + while (1) {
17946 + struct ubifs_znode *prev;
17947 + struct ubifs_zbranch *zbr;
17948 +
17949 + if (!znode->parent)
17950 + zbr = &c->zroot;
17951 + else
17952 + zbr = &znode->parent->zbranch[znode->iip];
17953 +
17954 + err = dbg_check_znode(c, zbr);
17955 + if (err)
17956 + return err;
17957 +
17958 + if (extra) {
17959 + if (ubifs_zn_dirty(znode))
17960 + dirty_cnt += 1;
17961 + else
17962 + clean_cnt += 1;
17963 + }
17964 +
17965 + prev = znode;
17966 + znode = ubifs_tnc_postorder_next(znode);
17967 + if (!znode)
17968 + break;
17969 +
17970 + /*
17971 + * If the last key of this znode is equivalent to the first key
17972 + * of the next znode (collision), then check order of the keys.
17973 + */
17974 + last = prev->child_cnt - 1;
17975 + if (prev->level == 0 && znode->level == 0 && !c->replaying &&
17976 + !keys_cmp(c, &prev->zbranch[last].key,
17977 + &znode->zbranch[0].key)) {
17978 + err = dbg_check_key_order(c, &prev->zbranch[last],
17979 + &znode->zbranch[0]);
17980 + if (err < 0)
17981 + return err;
17982 + if (err) {
17983 + ubifs_msg("first znode");
17984 + dbg_dump_znode(c, prev);
17985 + ubifs_msg("second znode");
17986 + dbg_dump_znode(c, znode);
17987 + return -EINVAL;
17988 + }
17989 + }
17990 + }
17991 +
17992 + if (extra) {
17993 + if (clean_cnt != atomic_long_read(&c->clean_zn_cnt)) {
17994 + ubifs_err("incorrect clean_zn_cnt %ld, calculated %ld",
17995 + atomic_long_read(&c->clean_zn_cnt),
17996 + clean_cnt);
17997 + return -EINVAL;
17998 + }
17999 + if (dirty_cnt != atomic_long_read(&c->dirty_zn_cnt)) {
18000 + ubifs_err("incorrect dirty_zn_cnt %ld, calculated %ld",
18001 + atomic_long_read(&c->dirty_zn_cnt),
18002 + dirty_cnt);
18003 + return -EINVAL;
18004 + }
18005 + }
18006 +
18007 + return 0;
18008 +}
18009 +
18010 +/**
18011 + * dbg_walk_index - walk the on-flash index.
18012 + * @c: UBIFS file-system description object
18013 + * @leaf_cb: called for each leaf node
18014 + * @znode_cb: called for each indexing node
18015 + * @priv: private date which is passed to callbacks
18016 + *
18017 + * This function walks the UBIFS index and calls the @leaf_cb for each leaf
18018 + * node and @znode_cb for each indexing node. Returns zero in case of success
18019 + * and a negative error code in case of failure.
18020 + *
18021 + * It would be better if this function removed every znode it pulled to into
18022 + * the TNC, so that the behavior more closely matched the non-debugging
18023 + * behavior.
18024 + */
18025 +int dbg_walk_index(struct ubifs_info *c, dbg_leaf_callback leaf_cb,
18026 + dbg_znode_callback znode_cb, void *priv)
18027 +{
18028 + int err;
18029 + struct ubifs_zbranch *zbr;
18030 + struct ubifs_znode *znode, *child;
18031 +
18032 + mutex_lock(&c->tnc_mutex);
18033 + /* If the root indexing node is not in TNC - pull it */
18034 + if (!c->zroot.znode) {
18035 + c->zroot.znode = ubifs_load_znode(c, &c->zroot, NULL, 0);
18036 + if (IS_ERR(c->zroot.znode)) {
18037 + err = PTR_ERR(c->zroot.znode);
18038 + c->zroot.znode = NULL;
18039 + goto out_unlock;
18040 + }
18041 + }
18042 +
18043 + /*
18044 + * We are going to traverse the indexing tree in the postorder manner.
18045 + * Go down and find the leftmost indexing node where we are going to
18046 + * start from.
18047 + */
18048 + znode = c->zroot.znode;
18049 + while (znode->level > 0) {
18050 + zbr = &znode->zbranch[0];
18051 + child = zbr->znode;
18052 + if (!child) {
18053 + child = ubifs_load_znode(c, zbr, znode, 0);
18054 + if (IS_ERR(child)) {
18055 + err = PTR_ERR(child);
18056 + goto out_unlock;
18057 + }
18058 + zbr->znode = child;
18059 + }
18060 +
18061 + znode = child;
18062 + }
18063 +
18064 + /* Iterate over all indexing nodes */
18065 + while (1) {
18066 + int idx;
18067 +
18068 + cond_resched();
18069 +
18070 + if (znode_cb) {
18071 + err = znode_cb(c, znode, priv);
18072 + if (err) {
18073 + ubifs_err("znode checking function returned "
18074 + "error %d", err);
18075 + dbg_dump_znode(c, znode);
18076 + goto out_dump;
18077 + }
18078 + }
18079 + if (leaf_cb && znode->level == 0) {
18080 + for (idx = 0; idx < znode->child_cnt; idx++) {
18081 + zbr = &znode->zbranch[idx];
18082 + err = leaf_cb(c, zbr, priv);
18083 + if (err) {
18084 + ubifs_err("leaf checking function "
18085 + "returned error %d, for leaf "
18086 + "at LEB %d:%d",
18087 + err, zbr->lnum, zbr->offs);
18088 + goto out_dump;
18089 + }
18090 + }
18091 + }
18092 +
18093 + if (!znode->parent)
18094 + break;
18095 +
18096 + idx = znode->iip + 1;
18097 + znode = znode->parent;
18098 + if (idx < znode->child_cnt) {
18099 + /* Switch to the next index in the parent */
18100 + zbr = &znode->zbranch[idx];
18101 + child = zbr->znode;
18102 + if (!child) {
18103 + child = ubifs_load_znode(c, zbr, znode, idx);
18104 + if (IS_ERR(child)) {
18105 + err = PTR_ERR(child);
18106 + goto out_unlock;
18107 + }
18108 + zbr->znode = child;
18109 + }
18110 + znode = child;
18111 + } else
18112 + /*
18113 + * This is the last child, switch to the parent and
18114 + * continue.
18115 + */
18116 + continue;
18117 +
18118 + /* Go to the lowest leftmost znode in the new sub-tree */
18119 + while (znode->level > 0) {
18120 + zbr = &znode->zbranch[0];
18121 + child = zbr->znode;
18122 + if (!child) {
18123 + child = ubifs_load_znode(c, zbr, znode, 0);
18124 + if (IS_ERR(child)) {
18125 + err = PTR_ERR(child);
18126 + goto out_unlock;
18127 + }
18128 + zbr->znode = child;
18129 + }
18130 + znode = child;
18131 + }
18132 + }
18133 +
18134 + mutex_unlock(&c->tnc_mutex);
18135 + return 0;
18136 +
18137 +out_dump:
18138 + if (znode->parent)
18139 + zbr = &znode->parent->zbranch[znode->iip];
18140 + else
18141 + zbr = &c->zroot;
18142 + ubifs_msg("dump of znode at LEB %d:%d", zbr->lnum, zbr->offs);
18143 + dbg_dump_znode(c, znode);
18144 +out_unlock:
18145 + mutex_unlock(&c->tnc_mutex);
18146 + return err;
18147 +}
18148 +
18149 +/**
18150 + * add_size - add znode size to partially calculated index size.
18151 + * @c: UBIFS file-system description object
18152 + * @znode: znode to add size for
18153 + * @priv: partially calculated index size
18154 + *
18155 + * This is a helper function for 'dbg_check_idx_size()' which is called for
18156 + * every indexing node and adds its size to the 'long long' variable pointed to
18157 + * by @priv.
18158 + */
18159 +static int add_size(struct ubifs_info *c, struct ubifs_znode *znode, void *priv)
18160 +{
18161 + long long *idx_size = priv;
18162 + int add;
18163 +
18164 + add = ubifs_idx_node_sz(c, znode->child_cnt);
18165 + add = ALIGN(add, 8);
18166 + *idx_size += add;
18167 + return 0;
18168 +}
18169 +
18170 +/**
18171 + * dbg_check_idx_size - check index size.
18172 + * @c: UBIFS file-system description object
18173 + * @idx_size: size to check
18174 + *
18175 + * This function walks the UBIFS index, calculates its size and checks that the
18176 + * size is equivalent to @idx_size. Returns zero in case of success and a
18177 + * negative error code in case of failure.
18178 + */
18179 +int dbg_check_idx_size(struct ubifs_info *c, long long idx_size)
18180 +{
18181 + int err;
18182 + long long calc = 0;
18183 +
18184 + if (!(ubifs_chk_flags & UBIFS_CHK_IDX_SZ))
18185 + return 0;
18186 +
18187 + err = dbg_walk_index(c, NULL, add_size, &calc);
18188 + if (err) {
18189 + ubifs_err("error %d while walking the index", err);
18190 + return err;
18191 + }
18192 +
18193 + if (calc != idx_size) {
18194 + ubifs_err("index size check failed: calculated size is %lld, "
18195 + "should be %lld", calc, idx_size);
18196 + dump_stack();
18197 + return -EINVAL;
18198 + }
18199 +
18200 + return 0;
18201 +}
18202 +
18203 +/**
18204 + * fsck_inode - information about an inode used when checking the file-system.
18205 + * @rb: link in the RB-tree of inodes
18206 + * @inum: inode number
18207 + * @mode: inode type, permissions, etc
18208 + * @nlink: inode link count
18209 + * @xatt_cnt: count of extended attributes
18210 + * @references: how many directory/xattr entries refer this inode (calculated
18211 + * while walking the index)
18212 + * @calc_cnt: for directory inode count of child directories, for regular files
18213 + * count of extended attributes
18214 + * @size: inode size (read from on-flash inode)
18215 + * @xattr_sz: summary size of all extended attributes (read from on-flash
18216 + * inode)
18217 + * @calc_sz: for directories calculated directory size, for regular files
18218 + * calculated summary size of all extended attributes
18219 + */
18220 +struct fsck_inode {
18221 + struct rb_node rb;
18222 + ino_t inum;
18223 + umode_t mode;
18224 + int nlink;
18225 + int xattr_cnt;
18226 + int references;
18227 + int calc_cnt;
18228 + long long size;
18229 + long long xattr_sz;
18230 + long long calc_sz;
18231 +};
18232 +
18233 +/**
18234 + * fsck_data - private FS checking information.
18235 + * @inodes: RB-tree of all inodes (contains @struct fsck_inode objects)
18236 + */
18237 +struct fsck_data {
18238 + struct rb_root inodes;
18239 +};
18240 +
18241 +/**
18242 + * add_inode - add inode information to RB-tree of inodes.
18243 + * @c: UBIFS file-system description object
18244 + * @fsckd: FS checking information
18245 + * @ino: raw UBIFS inode to add
18246 + *
18247 + * This is a helper function for 'check_leaf()' which adds information about
18248 + * inode @ino to the RB-tree of inodes. Returns inode information pointer in
18249 + * case of success and a negative error code in case of failure.
18250 + */
18251 +static struct fsck_inode *add_inode(struct ubifs_info *c,
18252 + struct fsck_data *fsckd,
18253 + struct ubifs_ino_node *ino)
18254 +{
18255 + struct rb_node **p, *parent = NULL;
18256 + struct fsck_inode *fscki;
18257 + ino_t inum = key_inum_flash(c, &ino->key);
18258 +
18259 + p = &fsckd->inodes.rb_node;
18260 + while (*p) {
18261 + parent = *p;
18262 + fscki = rb_entry(parent, struct fsck_inode, rb);
18263 + if (inum < fscki->inum)
18264 + p = &(*p)->rb_left;
18265 + else if (inum > fscki->inum)
18266 + p = &(*p)->rb_right;
18267 + else
18268 + return fscki;
18269 + }
18270 +
18271 + fscki = kzalloc(sizeof(struct fsck_inode), GFP_NOFS);
18272 + if (!fscki)
18273 + return ERR_PTR(-ENOMEM);
18274 +
18275 + fscki->inum = inum;
18276 + fscki->nlink = le32_to_cpu(ino->nlink);
18277 + fscki->size = le64_to_cpu(ino->size);
18278 + fscki->xattr_cnt = le32_to_cpu(ino->xattr_cnt);
18279 + fscki->xattr_sz = le64_to_cpu(ino->xattr_size);
18280 + fscki->mode = le32_to_cpu(ino->mode);
18281 + if (S_ISDIR(fscki->mode)) {
18282 + fscki->calc_sz = UBIFS_INO_NODE_SZ;
18283 + fscki->calc_cnt = 2;
18284 + }
18285 + rb_link_node(&fscki->rb, parent, p);
18286 + rb_insert_color(&fscki->rb, &fsckd->inodes);
18287 + return fscki;
18288 +}
18289 +
18290 +/**
18291 + * search_inode - search inode in the RB-tree of inodes.
18292 + * @fsckd: FS checking information
18293 + * @inum: inode number to search
18294 + *
18295 + * This is a helper function for 'check_leaf()' which searches inode @inum in
18296 + * the RB-tree of inodes and returns an inode information pointer or %NULL if
18297 + * the inode was not found.
18298 + */
18299 +static struct fsck_inode *search_inode(struct fsck_data *fsckd, ino_t inum)
18300 +{
18301 + struct rb_node *p;
18302 + struct fsck_inode *fscki;
18303 +
18304 + p = fsckd->inodes.rb_node;
18305 + while (p) {
18306 + fscki = rb_entry(p, struct fsck_inode, rb);
18307 + if (inum < fscki->inum)
18308 + p = p->rb_left;
18309 + else if (inum > fscki->inum)
18310 + p = p->rb_right;
18311 + else
18312 + return fscki;
18313 + }
18314 + return NULL;
18315 +}
18316 +
18317 +/**
18318 + * read_add_inode - read inode node and add it to RB-tree of inodes.
18319 + * @c: UBIFS file-system description object
18320 + * @fsckd: FS checking information
18321 + * @inum: inode number to read
18322 + *
18323 + * This is a helper function for 'check_leaf()' which finds inode node @inum in
18324 + * the index, reads it, and adds it to the RB-tree of inodes. Returns inode
18325 + * information pointer in case of success and a negative error code in case of
18326 + * failure.
18327 + */
18328 +static struct fsck_inode *read_add_inode(struct ubifs_info *c,
18329 + struct fsck_data *fsckd, ino_t inum)
18330 +{
18331 + int n, err;
18332 + union ubifs_key key;
18333 + struct ubifs_znode *znode;
18334 + struct ubifs_zbranch *zbr;
18335 + struct ubifs_ino_node *ino;
18336 + struct fsck_inode *fscki;
18337 +
18338 + fscki = search_inode(fsckd, inum);
18339 + if (fscki)
18340 + return fscki;
18341 +
18342 + ino_key_init(c, &key, inum);
18343 + err = ubifs_lookup_level0(c, &key, &znode, &n);
18344 + if (!err) {
18345 + ubifs_err("inode %lu not found in index", inum);
18346 + return ERR_PTR(-ENOENT);
18347 + } else if (err < 0) {
18348 + ubifs_err("error %d while looking up inode %lu", err, inum);
18349 + return ERR_PTR(err);
18350 + }
18351 +
18352 + zbr = &znode->zbranch[n];
18353 + if (zbr->len < UBIFS_INO_NODE_SZ) {
18354 + ubifs_err("bad node %lu node length %d", inum, zbr->len);
18355 + return ERR_PTR(-EINVAL);
18356 + }
18357 +
18358 + ino = kmalloc(zbr->len, GFP_NOFS);
18359 + if (!ino)
18360 + return ERR_PTR(-ENOMEM);
18361 +
18362 + err = ubifs_tnc_read_node(c, zbr, ino);
18363 + if (err) {
18364 + ubifs_err("cannot read inode node at LEB %d:%d, error %d",
18365 + zbr->lnum, zbr->offs, err);
18366 + kfree(ino);
18367 + return ERR_PTR(err);
18368 + }
18369 +
18370 + fscki = add_inode(c, fsckd, ino);
18371 + kfree(ino);
18372 + if (IS_ERR(fscki)) {
18373 + ubifs_err("error %ld while adding inode %lu node",
18374 + PTR_ERR(fscki), inum);
18375 + return fscki;
18376 + }
18377 +
18378 + return fscki;
18379 +}
18380 +
18381 +/**
18382 + * check_leaf - check leaf node.
18383 + * @c: UBIFS file-system description object
18384 + * @zbr: zbranch of the leaf node to check
18385 + * @priv: FS checking information
18386 + *
18387 + * This is a helper function for 'dbg_check_filesystem()' which is called for
18388 + * every single leaf node while walking the indexing tree. It checks that the
18389 + * leaf node referred from the indexing tree exists, has correct CRC, and does
18390 + * some other basic validation. This function is also responsible for building
18391 + * an RB-tree of inodes - it adds all inodes into the RB-tree. It also
18392 + * calculates reference count, size, etc for each inode in order to later
18393 + * compare them to the information stored inside the inodes and detect possible
18394 + * inconsistencies. Returns zero in case of success and a negative error code
18395 + * in case of failure.
18396 + */
18397 +static int check_leaf(struct ubifs_info *c, struct ubifs_zbranch *zbr,
18398 + void *priv)
18399 +{
18400 + ino_t inum;
18401 + void *node;
18402 + int err, type = key_type(c, &zbr->key);
18403 + struct fsck_inode *fscki;
18404 +
18405 + if (zbr->len < UBIFS_CH_SZ) {
18406 + ubifs_err("bad leaf length %d (LEB %d:%d)",
18407 + zbr->len, zbr->lnum, zbr->offs);
18408 + return -EINVAL;
18409 + }
18410 +
18411 + node = kmalloc(zbr->len, GFP_NOFS);
18412 + if (!node)
18413 + return -ENOMEM;
18414 +
18415 + err = ubifs_tnc_read_node(c, zbr, node);
18416 + if (err) {
18417 + ubifs_err("cannot read leaf node at LEB %d:%d, error %d",
18418 + zbr->lnum, zbr->offs, err);
18419 + goto out_free;
18420 + }
18421 +
18422 + /* If this is an inode node, add it to RB-tree of inodes */
18423 + if (type == UBIFS_INO_KEY) {
18424 + fscki = add_inode(c, priv, node);
18425 + if (IS_ERR(fscki)) {
18426 + err = PTR_ERR(fscki);
18427 + ubifs_err("error %d while adding inode node", err);
18428 + goto out_dump;
18429 + }
18430 + goto out;
18431 + }
18432 +
18433 + if (type != UBIFS_DENT_KEY && type != UBIFS_XENT_KEY &&
18434 + type != UBIFS_DATA_KEY) {
18435 + ubifs_err("unexpected node type %d at LEB %d:%d",
18436 + type, zbr->lnum, zbr->offs);
18437 + err = -EINVAL;
18438 + goto out_free;
18439 + }
18440 +
18441 + if (type == UBIFS_DATA_KEY) {
18442 + long long blk_offs;
18443 + struct ubifs_data_node *dn = node;
18444 +
18445 + /*
18446 + * Search the inode node this data node belongs to and insert
18447 + * it to the RB-tree of inodes.
18448 + */
18449 + inum = key_inum_flash(c, &dn->key);
18450 + fscki = read_add_inode(c, priv, inum);
18451 + if (IS_ERR(fscki)) {
18452 + err = PTR_ERR(fscki);
18453 + ubifs_err("error %d while processing data node and "
18454 + "trying to find inode node %lu", err, inum);
18455 + goto out_dump;
18456 + }
18457 +
18458 + /* Make sure the data node is within inode size */
18459 + blk_offs = (key_block_flash(c, &dn->key) << UBIFS_BLOCK_SHIFT);
18460 + blk_offs += le32_to_cpu(dn->size);
18461 + if (blk_offs > fscki->size) {
18462 + ubifs_err("data node at LEB %d:%d is not within inode "
18463 + "size %lld", zbr->lnum, zbr->offs, fscki->size);
18464 + err = -EINVAL;
18465 + goto out_dump;
18466 + }
18467 + } else {
18468 + int nlen;
18469 + struct ubifs_dent_node *dent = node;
18470 + struct fsck_inode *fscki1;
18471 +
18472 + err = ubifs_validate_entry(c, dent);
18473 + if (err)
18474 + goto out_dump;
18475 +
18476 + /*
18477 + * Search the inode node this entry refers to and the parent
18478 + * inode node and insert them to the RB-tree of inodes.
18479 + */
18480 + inum = le64_to_cpu(dent->inum);
18481 + fscki = read_add_inode(c, priv, inum);
18482 + if (IS_ERR(fscki)) {
18483 + err = PTR_ERR(fscki);
18484 + ubifs_err("error %d while processing entry node and "
18485 + "trying to find inode node %lu", err, inum);
18486 + goto out_dump;
18487 + }
18488 +
18489 + /* Count how many direntries or xentries refers this inode */
18490 + fscki->references += 1;
18491 +
18492 + inum = key_inum_flash(c, &dent->key);
18493 + fscki1 = read_add_inode(c, priv, inum);
18494 + if (IS_ERR(fscki1)) {
18495 + err = PTR_ERR(fscki);
18496 + ubifs_err("error %d while processing entry node and "
18497 + "trying to find parent inode node %lu",
18498 + err, inum);
18499 + goto out_dump;
18500 + }
18501 +
18502 + nlen = le16_to_cpu(dent->nlen);
18503 + if (type == UBIFS_XENT_KEY) {
18504 + fscki1->calc_cnt += 1;
18505 + fscki1->calc_sz += CALC_DENT_SIZE(nlen);
18506 + fscki1->calc_sz += CALC_XATTR_BYTES(fscki->size);
18507 + } else {
18508 + fscki1->calc_sz += CALC_DENT_SIZE(nlen);
18509 + if (dent->type == UBIFS_ITYPE_DIR)
18510 + fscki1->calc_cnt += 1;
18511 + }
18512 + }
18513 +
18514 +out:
18515 + kfree(node);
18516 + return 0;
18517 +
18518 +out_dump:
18519 + ubifs_msg("dump of node at LEB %d:%d", zbr->lnum, zbr->offs);
18520 + dbg_dump_node(c, node);
18521 +out_free:
18522 + kfree(node);
18523 + return err;
18524 +}
18525 +
18526 +/**
18527 + * free_inodes - free RB-tree of inodes.
18528 + * @fsckd: FS checking information
18529 + */
18530 +static void free_inodes(struct fsck_data *fsckd)
18531 +{
18532 + struct rb_node *this = fsckd->inodes.rb_node;
18533 + struct fsck_inode *fscki;
18534 +
18535 + while (this) {
18536 + if (this->rb_left)
18537 + this = this->rb_left;
18538 + else if (this->rb_right)
18539 + this = this->rb_right;
18540 + else {
18541 + fscki = rb_entry(this, struct fsck_inode, rb);
18542 + this = rb_parent(this);
18543 + if (this) {
18544 + if (this->rb_left == &fscki->rb)
18545 + this->rb_left = NULL;
18546 + else
18547 + this->rb_right = NULL;
18548 + }
18549 + kfree(fscki);
18550 + }
18551 + }
18552 +}
18553 +
18554 +/**
18555 + * check_inodes - checks all inodes.
18556 + * @c: UBIFS file-system description object
18557 + * @fsckd: FS checking information
18558 + *
18559 + * This is a helper function for 'dbg_check_filesystem()' which walks the
18560 + * RB-tree of inodes after the index scan has been finished, and checks that
18561 + * inode nlink, size, etc are correct. Returns zero if inodes are fine,
18562 + * %-EINVAL if not, and a negative error code in case of failure.
18563 + */
18564 +static int check_inodes(struct ubifs_info *c, struct fsck_data *fsckd)
18565 +{
18566 + int n, err;
18567 + union ubifs_key key;
18568 + struct ubifs_znode *znode;
18569 + struct ubifs_zbranch *zbr;
18570 + struct ubifs_ino_node *ino;
18571 + struct fsck_inode *fscki;
18572 + struct rb_node *this = rb_first(&fsckd->inodes);
18573 +
18574 + while (this) {
18575 + fscki = rb_entry(this, struct fsck_inode, rb);
18576 + this = rb_next(this);
18577 +
18578 + if (S_ISDIR(fscki->mode)) {
18579 + /*
18580 + * Directories have to have exactly one reference (they
18581 + * cannot have hardlinks), although root inode is an
18582 + * exception.
18583 + */
18584 + if (fscki->inum != UBIFS_ROOT_INO &&
18585 + fscki->references != 1) {
18586 + ubifs_err("directory inode %lu has %d "
18587 + "direntries which refer it, but "
18588 + "should be 1", fscki->inum,
18589 + fscki->references);
18590 + goto out_dump;
18591 + }
18592 + if (fscki->inum == UBIFS_ROOT_INO &&
18593 + fscki->references != 0) {
18594 + ubifs_err("root inode %lu has non-zero (%d) "
18595 + "direntries which refer it",
18596 + fscki->inum, fscki->references);
18597 + goto out_dump;
18598 + }
18599 + if (fscki->calc_sz != fscki->size) {
18600 + ubifs_err("directory inode %lu size is %lld, "
18601 + "but calculated size is %lld",
18602 + fscki->inum, fscki->size,
18603 + fscki->calc_sz);
18604 + goto out_dump;
18605 + }
18606 + if (fscki->calc_cnt != fscki->nlink) {
18607 + ubifs_err("directory inode %lu nlink is %d, "
18608 + "but calculated nlink is %d",
18609 + fscki->inum, fscki->nlink,
18610 + fscki->calc_cnt);
18611 + goto out_dump;
18612 + }
18613 + } else {
18614 + if (fscki->references != fscki->nlink) {
18615 + ubifs_err("inode %lu nlink is %d, but "
18616 + "calculated nlink is %d", fscki->inum,
18617 + fscki->nlink, fscki->references);
18618 + goto out_dump;
18619 + }
18620 + if (fscki->xattr_sz != fscki->calc_sz) {
18621 + ubifs_err("inode %lu has xattr size %lld, but "
18622 + "calculated size is %lld",
18623 + fscki->inum, fscki->xattr_sz,
18624 + fscki->calc_sz);
18625 + goto out_dump;
18626 + }
18627 + if (fscki->xattr_cnt != fscki->calc_cnt) {
18628 + ubifs_err("inode %lu has %d xattrs, but "
18629 + "calculated count is %d", fscki->inum,
18630 + fscki->xattr_cnt, fscki->calc_cnt);
18631 + goto out_dump;
18632 + }
18633 + }
18634 + }
18635 +
18636 + return 0;
18637 +
18638 +out_dump:
18639 + /* Read the bad inode and dump it */
18640 + ino_key_init(c, &key, fscki->inum);
18641 + err = ubifs_lookup_level0(c, &key, &znode, &n);
18642 + if (!err) {
18643 + ubifs_err("inode %lu not found in index", fscki->inum);
18644 + return -ENOENT;
18645 + } else if (err < 0) {
18646 + ubifs_err("error %d while looking up inode %lu",
18647 + err, fscki->inum);
18648 + return err;
18649 + }
18650 +
18651 + zbr = &znode->zbranch[n];
18652 + ino = kmalloc(zbr->len, GFP_NOFS);
18653 + if (!ino)
18654 + return -ENOMEM;
18655 +
18656 + err = ubifs_tnc_read_node(c, zbr, ino);
18657 + if (err) {
18658 + ubifs_err("cannot read inode node at LEB %d:%d, error %d",
18659 + zbr->lnum, zbr->offs, err);
18660 + kfree(ino);
18661 + return err;
18662 + }
18663 +
18664 + ubifs_msg("dump of the inode %lu sitting in LEB %d:%d",
18665 + fscki->inum, zbr->lnum, zbr->offs);
18666 + dbg_dump_node(c, ino);
18667 + kfree(ino);
18668 + return -EINVAL;
18669 +}
18670 +
18671 +/**
18672 + * dbg_check_filesystem - check the file-system.
18673 + * @c: UBIFS file-system description object
18674 + *
18675 + * This function checks the file system, namely:
18676 + * o makes sure that all leaf nodes exist and their CRCs are correct;
18677 + * o makes sure inode nlink, size, xattr size/count are correct (for all
18678 + * inodes).
18679 + *
18680 + * The function reads whole indexing tree and all nodes, so it is pretty
18681 + * heavy-weight. Returns zero if the file-system is consistent, %-EINVAL if
18682 + * not, and a negative error code in case of failure.
18683 + */
18684 +int dbg_check_filesystem(struct ubifs_info *c)
18685 +{
18686 + int err;
18687 + struct fsck_data fsckd;
18688 +
18689 + if (!(ubifs_chk_flags & UBIFS_CHK_FS))
18690 + return 0;
18691 +
18692 + fsckd.inodes = RB_ROOT;
18693 + err = dbg_walk_index(c, check_leaf, NULL, &fsckd);
18694 + if (err)
18695 + goto out_free;
18696 +
18697 + err = check_inodes(c, &fsckd);
18698 + if (err)
18699 + goto out_free;
18700 +
18701 + free_inodes(&fsckd);
18702 + return 0;
18703 +
18704 +out_free:
18705 + ubifs_err("file-system check failed with error %d", err);
18706 + dump_stack();
18707 + free_inodes(&fsckd);
18708 + return err;
18709 +}
18710 +
18711 +static int invocation_cnt;
18712 +
18713 +int dbg_force_in_the_gaps(void)
18714 +{
18715 + if (!dbg_force_in_the_gaps_enabled)
18716 + return 0;
18717 + /* Force in-the-gaps every 8th commit */
18718 + return !((invocation_cnt++) & 0x7);
18719 +}
18720 +
18721 +/* Failure mode for recovery testing */
18722 +
18723 +#define chance(n, d) (simple_rand() <= (n) * 32768LL / (d))
18724 +
18725 +struct failure_mode_info {
18726 + struct list_head list;
18727 + struct ubifs_info *c;
18728 +};
18729 +
18730 +static LIST_HEAD(fmi_list);
18731 +static DEFINE_SPINLOCK(fmi_lock);
18732 +
18733 +static unsigned int next;
18734 +
18735 +static int simple_rand(void)
18736 +{
18737 + if (next == 0)
18738 + next = current->pid;
18739 + next = next * 1103515245 + 12345;
18740 + return (next >> 16) & 32767;
18741 +}
18742 +
18743 +void dbg_failure_mode_registration(struct ubifs_info *c)
18744 +{
18745 + struct failure_mode_info *fmi;
18746 +
18747 + fmi = kmalloc(sizeof(struct failure_mode_info), GFP_NOFS);
18748 + if (!fmi) {
18749 + dbg_err("Failed to register failure mode - no memory");
18750 + return;
18751 + }
18752 + fmi->c = c;
18753 + spin_lock(&fmi_lock);
18754 + list_add_tail(&fmi->list, &fmi_list);
18755 + spin_unlock(&fmi_lock);
18756 +}
18757 +
18758 +void dbg_failure_mode_deregistration(struct ubifs_info *c)
18759 +{
18760 + struct failure_mode_info *fmi, *tmp;
18761 +
18762 + spin_lock(&fmi_lock);
18763 + list_for_each_entry_safe(fmi, tmp, &fmi_list, list)
18764 + if (fmi->c == c) {
18765 + list_del(&fmi->list);
18766 + kfree(fmi);
18767 + }
18768 + spin_unlock(&fmi_lock);
18769 +}
18770 +
18771 +static struct ubifs_info *dbg_find_info(struct ubi_volume_desc *desc)
18772 +{
18773 + struct failure_mode_info *fmi;
18774 +
18775 + spin_lock(&fmi_lock);
18776 + list_for_each_entry(fmi, &fmi_list, list)
18777 + if (fmi->c->ubi == desc) {
18778 + struct ubifs_info *c = fmi->c;
18779 +
18780 + spin_unlock(&fmi_lock);
18781 + return c;
18782 + }
18783 + spin_unlock(&fmi_lock);
18784 + return NULL;
18785 +}
18786 +
18787 +static int in_failure_mode(struct ubi_volume_desc *desc)
18788 +{
18789 + struct ubifs_info *c = dbg_find_info(desc);
18790 +
18791 + if (c)
18792 + return c->failure_mode;
18793 + return 0;
18794 +}
18795 +
18796 +static int do_fail(struct ubi_volume_desc *desc, int lnum, int write)
18797 +{
18798 + struct ubifs_info *c = dbg_find_info(desc);
18799 +
18800 + if (!c || !dbg_failure_mode)
18801 + return 0;
18802 + if (c->failure_mode)
18803 + return 1;
18804 + if (!c->fail_cnt) {
18805 + /* First call - decide delay to failure */
18806 + if (chance(1, 2)) {
18807 + unsigned int delay = 1 << (simple_rand() >> 11);
18808 +
18809 + if (chance(1, 2)) {
18810 + c->fail_delay = 1;
18811 + c->fail_timeout = jiffies +
18812 + msecs_to_jiffies(delay);
18813 + dbg_rcvry("failing after %ums", delay);
18814 + } else {
18815 + c->fail_delay = 2;
18816 + c->fail_cnt_max = delay;
18817 + dbg_rcvry("failing after %u calls", delay);
18818 + }
18819 + }
18820 + c->fail_cnt += 1;
18821 + }
18822 + /* Determine if failure delay has expired */
18823 + if (c->fail_delay == 1) {
18824 + if (time_before(jiffies, c->fail_timeout))
18825 + return 0;
18826 + } else if (c->fail_delay == 2)
18827 + if (c->fail_cnt++ < c->fail_cnt_max)
18828 + return 0;
18829 + if (lnum == UBIFS_SB_LNUM) {
18830 + if (write) {
18831 + if (chance(1, 2))
18832 + return 0;
18833 + } else if (chance(19, 20))
18834 + return 0;
18835 + dbg_rcvry("failing in super block LEB %d", lnum);
18836 + } else if (lnum == UBIFS_MST_LNUM || lnum == UBIFS_MST_LNUM + 1) {
18837 + if (chance(19, 20))
18838 + return 0;
18839 + dbg_rcvry("failing in master LEB %d", lnum);
18840 + } else if (lnum >= UBIFS_LOG_LNUM && lnum <= c->log_last) {
18841 + if (write) {
18842 + if (chance(99, 100))
18843 + return 0;
18844 + } else if (chance(399, 400))
18845 + return 0;
18846 + dbg_rcvry("failing in log LEB %d", lnum);
18847 + } else if (lnum >= c->lpt_first && lnum <= c->lpt_last) {
18848 + if (write) {
18849 + if (chance(7, 8))
18850 + return 0;
18851 + } else if (chance(19, 20))
18852 + return 0;
18853 + dbg_rcvry("failing in LPT LEB %d", lnum);
18854 + } else if (lnum >= c->orph_first && lnum <= c->orph_last) {
18855 + if (write) {
18856 + if (chance(1, 2))
18857 + return 0;
18858 + } else if (chance(9, 10))
18859 + return 0;
18860 + dbg_rcvry("failing in orphan LEB %d", lnum);
18861 + } else if (lnum == c->ihead_lnum) {
18862 + if (chance(99, 100))
18863 + return 0;
18864 + dbg_rcvry("failing in index head LEB %d", lnum);
18865 + } else if (c->jheads && lnum == c->jheads[GCHD].wbuf.lnum) {
18866 + if (chance(9, 10))
18867 + return 0;
18868 + dbg_rcvry("failing in GC head LEB %d", lnum);
18869 + } else if (write && !RB_EMPTY_ROOT(&c->buds) &&
18870 + !ubifs_search_bud(c, lnum)) {
18871 + if (chance(19, 20))
18872 + return 0;
18873 + dbg_rcvry("failing in non-bud LEB %d", lnum);
18874 + } else if (c->cmt_state == COMMIT_RUNNING_BACKGROUND ||
18875 + c->cmt_state == COMMIT_RUNNING_REQUIRED) {
18876 + if (chance(999, 1000))
18877 + return 0;
18878 + dbg_rcvry("failing in bud LEB %d commit running", lnum);
18879 + } else {
18880 + if (chance(9999, 10000))
18881 + return 0;
18882 + dbg_rcvry("failing in bud LEB %d commit not running", lnum);
18883 + }
18884 + ubifs_err("*** SETTING FAILURE MODE ON (LEB %d) ***", lnum);
18885 + c->failure_mode = 1;
18886 + dump_stack();
18887 + return 1;
18888 +}
18889 +
18890 +static void cut_data(const void *buf, int len)
18891 +{
18892 + int flen, i;
18893 + unsigned char *p = (void *)buf;
18894 +
18895 + flen = (len * (long long)simple_rand()) >> 15;
18896 + for (i = flen; i < len; i++)
18897 + p[i] = 0xff;
18898 +}
18899 +
18900 +int dbg_leb_read(struct ubi_volume_desc *desc, int lnum, char *buf, int offset,
18901 + int len, int check)
18902 +{
18903 + if (in_failure_mode(desc))
18904 + return -EIO;
18905 + return ubi_leb_read(desc, lnum, buf, offset, len, check);
18906 +}
18907 +
18908 +int dbg_leb_write(struct ubi_volume_desc *desc, int lnum, const void *buf,
18909 + int offset, int len, int dtype)
18910 +{
18911 + int err;
18912 +
18913 + if (in_failure_mode(desc))
18914 + return -EIO;
18915 + if (do_fail(desc, lnum, 1))
18916 + cut_data(buf, len);
18917 + err = ubi_leb_write(desc, lnum, buf, offset, len, dtype);
18918 + if (err)
18919 + return err;
18920 + if (in_failure_mode(desc))
18921 + return -EIO;
18922 + return 0;
18923 +}
18924 +
18925 +int dbg_leb_change(struct ubi_volume_desc *desc, int lnum, const void *buf,
18926 + int len, int dtype)
18927 +{
18928 + int err;
18929 +
18930 + if (do_fail(desc, lnum, 1))
18931 + return -EIO;
18932 + err = ubi_leb_change(desc, lnum, buf, len, dtype);
18933 + if (err)
18934 + return err;
18935 + if (do_fail(desc, lnum, 1))
18936 + return -EIO;
18937 + return 0;
18938 +}
18939 +
18940 +int dbg_leb_erase(struct ubi_volume_desc *desc, int lnum)
18941 +{
18942 + int err;
18943 +
18944 + if (do_fail(desc, lnum, 0))
18945 + return -EIO;
18946 + err = ubi_leb_erase(desc, lnum);
18947 + if (err)
18948 + return err;
18949 + if (do_fail(desc, lnum, 0))
18950 + return -EIO;
18951 + return 0;
18952 +}
18953 +
18954 +int dbg_leb_unmap(struct ubi_volume_desc *desc, int lnum)
18955 +{
18956 + int err;
18957 +
18958 + if (do_fail(desc, lnum, 0))
18959 + return -EIO;
18960 + err = ubi_leb_unmap(desc, lnum);
18961 + if (err)
18962 + return err;
18963 + if (do_fail(desc, lnum, 0))
18964 + return -EIO;
18965 + return 0;
18966 +}
18967 +
18968 +int dbg_is_mapped(struct ubi_volume_desc *desc, int lnum)
18969 +{
18970 + if (in_failure_mode(desc))
18971 + return -EIO;
18972 + return ubi_is_mapped(desc, lnum);
18973 +}
18974 +
18975 +#endif /* CONFIG_UBIFS_FS_DEBUG */
18976 diff --exclude=.git -urN linux-2.6.25.6/fs/ubifs/debug.h avr32-2.6/fs/ubifs/debug.h
18977 --- linux-2.6.25.6/fs/ubifs/debug.h 1970-01-01 01:00:00.000000000 +0100
18978 +++ avr32-2.6/fs/ubifs/debug.h 2008-06-12 15:09:45.315815846 +0200
18979 @@ -0,0 +1,396 @@
18980 +/*
18981 + * This file is part of UBIFS.
18982 + *
18983 + * Copyright (C) 2006-2008 Nokia Corporation.
18984 + *
18985 + * This program is free software; you can redistribute it and/or modify it
18986 + * under the terms of the GNU General Public License version 2 as published by
18987 + * the Free Software Foundation.
18988 + *
18989 + * This program is distributed in the hope that it will be useful, but WITHOUT
18990 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18991 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
18992 + * more details.
18993 + *
18994 + * You should have received a copy of the GNU General Public License along with
18995 + * this program; if not, write to the Free Software Foundation, Inc., 51
18996 + * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18997 + *
18998 + * Authors: Artem Bityutskiy (Битюцкий Артём)
18999 + * Adrian Hunter
19000 + */
19001 +
19002 +#ifndef __UBIFS_DEBUG_H__
19003 +#define __UBIFS_DEBUG_H__
19004 +
19005 +#ifdef CONFIG_UBIFS_FS_DEBUG
19006 +
19007 +#define UBIFS_DBG(op) op
19008 +
19009 +#define ubifs_assert(expr) do { \
19010 + if (unlikely(!(expr))) { \
19011 + printk(KERN_CRIT "UBIFS assert failed in %s at %u (pid %d)\n", \
19012 + __func__, __LINE__, current->pid); \
19013 + dbg_dump_stack(); \
19014 + } \
19015 +} while (0)
19016 +
19017 +#define ubifs_assert_cmt_locked(c) do { \
19018 + if (unlikely(down_write_trylock(&(c)->commit_sem))) { \
19019 + up_write(&(c)->commit_sem); \
19020 + printk(KERN_CRIT "commit lock is not locked!\n"); \
19021 + ubifs_assert(0); \
19022 + } \
19023 +} while (0)
19024 +
19025 +#define dbg_dump_stack() do { \
19026 + if (!dbg_failure_mode) \
19027 + dump_stack(); \
19028 +} while (0)
19029 +
19030 +/* Generic debugging messages */
19031 +#define dbg_msg(fmt, ...) do { \
19032 + spin_lock(&dbg_lock); \
19033 + printk(KERN_DEBUG "UBIFS DBG (pid %d): %s: " fmt "\n", current->pid, \
19034 + __func__, ##__VA_ARGS__); \
19035 + spin_unlock(&dbg_lock); \
19036 +} while (0)
19037 +
19038 +#define dbg_do_msg(typ, fmt, ...) do { \
19039 + if (ubifs_msg_flags & typ) \
19040 + dbg_msg(fmt, ##__VA_ARGS__); \
19041 +} while (0)
19042 +
19043 +#define dbg_err(fmt, ...) do { \
19044 + spin_lock(&dbg_lock); \
19045 + ubifs_err(fmt, ##__VA_ARGS__); \
19046 + spin_unlock(&dbg_lock); \
19047 +} while (0)
19048 +
19049 +const char *dbg_key_str0(const struct ubifs_info *c,
19050 + const union ubifs_key *key);
19051 +const char *dbg_key_str1(const struct ubifs_info *c,
19052 + const union ubifs_key *key);
19053 +
19054 +/*
19055 + * DBGKEY macros require dbg_lock to be held, which it is in the dbg message
19056 + * macros.
19057 + */
19058 +#define DBGKEY(key) dbg_key_str0(c, (key))
19059 +#define DBGKEY1(key) dbg_key_str1(c, (key))
19060 +
19061 +/* General messages */
19062 +#define dbg_gen(fmt, ...) dbg_do_msg(UBIFS_MSG_GEN, fmt, ##__VA_ARGS__)
19063 +
19064 +/* Additional journal messages */
19065 +#define dbg_jnl(fmt, ...) dbg_do_msg(UBIFS_MSG_JNL, fmt, ##__VA_ARGS__)
19066 +
19067 +/* Additional TNC messages */
19068 +#define dbg_tnc(fmt, ...) dbg_do_msg(UBIFS_MSG_TNC, fmt, ##__VA_ARGS__)
19069 +
19070 +/* Additional lprops messages */
19071 +#define dbg_lp(fmt, ...) dbg_do_msg(UBIFS_MSG_LP, fmt, ##__VA_ARGS__)
19072 +
19073 +/* Additional LEB find messages */
19074 +#define dbg_find(fmt, ...) dbg_do_msg(UBIFS_MSG_FIND, fmt, ##__VA_ARGS__)
19075 +
19076 +/* Additional mount messages */
19077 +#define dbg_mnt(fmt, ...) dbg_do_msg(UBIFS_MSG_MNT, fmt, ##__VA_ARGS__)
19078 +
19079 +/* Additional I/O messages */
19080 +#define dbg_io(fmt, ...) dbg_do_msg(UBIFS_MSG_IO, fmt, ##__VA_ARGS__)
19081 +
19082 +/* Additional commit messages */
19083 +#define dbg_cmt(fmt, ...) dbg_do_msg(UBIFS_MSG_CMT, fmt, ##__VA_ARGS__)
19084 +
19085 +/* Additional budgeting messages */
19086 +#define dbg_budg(fmt, ...) dbg_do_msg(UBIFS_MSG_BUDG, fmt, ##__VA_ARGS__)
19087 +
19088 +/* Additional log messages */
19089 +#define dbg_log(fmt, ...) dbg_do_msg(UBIFS_MSG_LOG, fmt, ##__VA_ARGS__)
19090 +
19091 +/* Additional gc messages */
19092 +#define dbg_gc(fmt, ...) dbg_do_msg(UBIFS_MSG_GC, fmt, ##__VA_ARGS__)
19093 +
19094 +/* Additional scan messages */
19095 +#define dbg_scan(fmt, ...) dbg_do_msg(UBIFS_MSG_SCAN, fmt, ##__VA_ARGS__)
19096 +
19097 +/* Additional recovery messages */
19098 +#define dbg_rcvry(fmt, ...) dbg_do_msg(UBIFS_MSG_RCVRY, fmt, ##__VA_ARGS__)
19099 +
19100 +/*
19101 + * Debugging message type flags (must match msg_type_names in debug.c).
19102 + *
19103 + * UBIFS_MSG_GEN: general messages
19104 + * UBIFS_MSG_JNL: journal messages
19105 + * UBIFS_MSG_MNT: mount messages
19106 + * UBIFS_MSG_CMT: commit messages
19107 + * UBIFS_MSG_FIND: LEB find messages
19108 + * UBIFS_MSG_BUDG: budgeting messages
19109 + * UBIFS_MSG_GC: garbage collection messages
19110 + * UBIFS_MSG_TNC: TNC messages
19111 + * UBIFS_MSG_LP: lprops messages
19112 + * UBIFS_MSG_IO: I/O messages
19113 + * UBIFS_MSG_LOG: log messages
19114 + * UBIFS_MSG_SCAN: scan messages
19115 + * UBIFS_MSG_RCVRY: recovery messages
19116 + */
19117 +enum {
19118 + UBIFS_MSG_GEN = 0x1,
19119 + UBIFS_MSG_JNL = 0x2,
19120 + UBIFS_MSG_MNT = 0x4,
19121 + UBIFS_MSG_CMT = 0x8,
19122 + UBIFS_MSG_FIND = 0x10,
19123 + UBIFS_MSG_BUDG = 0x20,
19124 + UBIFS_MSG_GC = 0x40,
19125 + UBIFS_MSG_TNC = 0x80,
19126 + UBIFS_MSG_LP = 0x100,
19127 + UBIFS_MSG_IO = 0x200,
19128 + UBIFS_MSG_LOG = 0x400,
19129 + UBIFS_MSG_SCAN = 0x800,
19130 + UBIFS_MSG_RCVRY = 0x1000,
19131 +};
19132 +
19133 +/* Debugging message type flags for each default debug message level */
19134 +#define UBIFS_MSG_LVL_0 0
19135 +#define UBIFS_MSG_LVL_1 0x1
19136 +#define UBIFS_MSG_LVL_2 0x7f
19137 +#define UBIFS_MSG_LVL_3 0xffff
19138 +
19139 +/*
19140 + * Debugging check flags (must match chk_names in debug.c).
19141 + *
19142 + * UBIFS_CHK_GEN: general checks
19143 + * UBIFS_CHK_TNC: check TNC
19144 + * UBIFS_CHK_IDX_SZ: check index size
19145 + * UBIFS_CHK_ORPH: check orphans
19146 + * UBIFS_CHK_OLD_IDX: check the old index
19147 + * UBIFS_CHK_LPROPS: check lprops
19148 + * UBIFS_CHK_FS: check the file-system
19149 + */
19150 +enum {
19151 + UBIFS_CHK_GEN = 0x1,
19152 + UBIFS_CHK_TNC = 0x2,
19153 + UBIFS_CHK_IDX_SZ = 0x4,
19154 + UBIFS_CHK_ORPH = 0x8,
19155 + UBIFS_CHK_OLD_IDX = 0x10,
19156 + UBIFS_CHK_LPROPS = 0x20,
19157 + UBIFS_CHK_FS = 0x40,
19158 +};
19159 +
19160 +/*
19161 + * Special testing flags (must match tst_names in debug.c).
19162 + *
19163 + * UBIFS_TST_FORCE_IN_THE_GAPS: force the use of in-the-gaps method
19164 + * UBIFS_TST_RCVRY: failure mode for recovery testing
19165 + */
19166 +enum {
19167 + UBIFS_TST_FORCE_IN_THE_GAPS = 0x2,
19168 + UBIFS_TST_RCVRY = 0x4,
19169 +};
19170 +
19171 +#if CONFIG_UBIFS_FS_DEBUG_MSG_LVL == 1
19172 +#define UBIFS_MSG_FLAGS_DEFAULT UBIFS_MSG_LVL_1
19173 +#elif CONFIG_UBIFS_FS_DEBUG_MSG_LVL == 2
19174 +#define UBIFS_MSG_FLAGS_DEFAULT UBIFS_MSG_LVL_2
19175 +#elif CONFIG_UBIFS_FS_DEBUG_MSG_LVL == 3
19176 +#define UBIFS_MSG_FLAGS_DEFAULT UBIFS_MSG_LVL_3
19177 +#else
19178 +#define UBIFS_MSG_FLAGS_DEFAULT UBIFS_MSG_LVL_0
19179 +#endif
19180 +
19181 +#ifdef CONFIG_UBIFS_FS_DEBUG_CHKS
19182 +#define UBIFS_CHK_FLAGS_DEFAULT 0xffffffff
19183 +#else
19184 +#define UBIFS_CHK_FLAGS_DEFAULT 0
19185 +#endif
19186 +
19187 +extern spinlock_t dbg_lock;
19188 +
19189 +extern unsigned int ubifs_msg_flags;
19190 +extern unsigned int ubifs_chk_flags;
19191 +extern unsigned int ubifs_tst_flags;
19192 +
19193 +/* Dump functions */
19194 +
19195 +const char *dbg_ntype(int type);
19196 +const char *dbg_cstate(int cmt_state);
19197 +const char *dbg_get_key_dump(const struct ubifs_info *c,
19198 + const union ubifs_key *key);
19199 +void dbg_dump_inode(const struct ubifs_info *c, const struct inode *inode);
19200 +void dbg_dump_node(const struct ubifs_info *c, const void *node);
19201 +void dbg_dump_budget_req(const struct ubifs_budget_req *req);
19202 +void dbg_dump_lstats(const struct ubifs_lp_stats *lst);
19203 +void dbg_dump_budg(struct ubifs_info *c);
19204 +void dbg_dump_lprop(const struct ubifs_info *c, const struct ubifs_lprops *lp);
19205 +void dbg_dump_lprops(struct ubifs_info *c);
19206 +void dbg_dump_leb(const struct ubifs_info *c, int lnum);
19207 +void dbg_dump_znode(const struct ubifs_info *c,
19208 + const struct ubifs_znode *znode);
19209 +void dbg_dump_heap(struct ubifs_info *c, struct ubifs_lpt_heap *heap, int cat);
19210 +void dbg_dump_pnode(struct ubifs_info *c, struct ubifs_pnode *pnode,
19211 + struct ubifs_nnode *parent, int iip);
19212 +void dbg_dump_tnc(struct ubifs_info *c);
19213 +void dbg_dump_index(struct ubifs_info *c);
19214 +
19215 +/* Checking helper functions */
19216 +
19217 +typedef int (*dbg_leaf_callback)(struct ubifs_info *c,
19218 + struct ubifs_zbranch *zbr, void *priv);
19219 +typedef int (*dbg_znode_callback)(struct ubifs_info *c,
19220 + struct ubifs_znode *znode, void *priv);
19221 +
19222 +int dbg_walk_index(struct ubifs_info *c, dbg_leaf_callback leaf_cb,
19223 + dbg_znode_callback znode_cb, void *priv);
19224 +
19225 +/* Checking functions */
19226 +
19227 +int dbg_check_lprops(struct ubifs_info *c);
19228 +
19229 +int dbg_old_index_check_init(struct ubifs_info *c, struct ubifs_zbranch *zroot);
19230 +int dbg_check_old_index(struct ubifs_info *c, struct ubifs_zbranch *zroot);
19231 +
19232 +int dbg_check_cats(struct ubifs_info *c);
19233 +
19234 +int dbg_check_ltab(struct ubifs_info *c);
19235 +
19236 +int dbg_check_dir_size(struct ubifs_info *c, const struct inode *dir);
19237 +
19238 +int dbg_check_tnc(struct ubifs_info *c, int extra);
19239 +
19240 +int dbg_check_idx_size(struct ubifs_info *c, long long idx_size);
19241 +
19242 +int dbg_check_filesystem(struct ubifs_info *c);
19243 +
19244 +void dbg_check_heap(struct ubifs_info *c, struct ubifs_lpt_heap *heap, int cat,
19245 + int add_pos);
19246 +
19247 +int dbg_check_lprops(struct ubifs_info *c);
19248 +int dbg_check_lpt_nodes(struct ubifs_info *c, struct ubifs_cnode *cnode,
19249 + int row, int col);
19250 +
19251 +/* Force the use of in-the-gaps method for testing */
19252 +
19253 +#define dbg_force_in_the_gaps_enabled \
19254 + (ubifs_tst_flags & UBIFS_TST_FORCE_IN_THE_GAPS)
19255 +
19256 +int dbg_force_in_the_gaps(void);
19257 +
19258 +/* Failure mode for recovery testing */
19259 +
19260 +#define dbg_failure_mode (ubifs_tst_flags & UBIFS_TST_RCVRY)
19261 +
19262 +void dbg_failure_mode_registration(struct ubifs_info *c);
19263 +void dbg_failure_mode_deregistration(struct ubifs_info *c);
19264 +
19265 +#ifndef UBIFS_DBG_PRESERVE_UBI
19266 +
19267 +#define ubi_leb_read dbg_leb_read
19268 +#define ubi_leb_write dbg_leb_write
19269 +#define ubi_leb_change dbg_leb_change
19270 +#define ubi_leb_erase dbg_leb_erase
19271 +#define ubi_leb_unmap dbg_leb_unmap
19272 +#define ubi_is_mapped dbg_is_mapped
19273 +
19274 +#endif
19275 +
19276 +int dbg_leb_read(struct ubi_volume_desc *desc, int lnum, char *buf, int offset,
19277 + int len, int check);
19278 +int dbg_leb_write(struct ubi_volume_desc *desc, int lnum, const void *buf,
19279 + int offset, int len, int dtype);
19280 +int dbg_leb_change(struct ubi_volume_desc *desc, int lnum, const void *buf,
19281 + int len, int dtype);
19282 +int dbg_leb_erase(struct ubi_volume_desc *desc, int lnum);
19283 +int dbg_leb_unmap(struct ubi_volume_desc *desc, int lnum);
19284 +int dbg_is_mapped(struct ubi_volume_desc *desc, int lnum);
19285 +
19286 +static inline int dbg_read(struct ubi_volume_desc *desc, int lnum, char *buf,
19287 + int offset, int len)
19288 +{
19289 + return dbg_leb_read(desc, lnum, buf, offset, len, 0);
19290 +}
19291 +
19292 +static inline int dbg_write(struct ubi_volume_desc *desc, int lnum,
19293 + const void *buf, int offset, int len)
19294 +{
19295 + return dbg_leb_write(desc, lnum, buf, offset, len, UBI_UNKNOWN);
19296 +}
19297 +
19298 +static inline int dbg_change(struct ubi_volume_desc *desc, int lnum,
19299 + const void *buf, int len)
19300 +{
19301 + return dbg_leb_change(desc, lnum, buf, len, UBI_UNKNOWN);
19302 +}
19303 +
19304 +#else /* !CONFIG_UBIFS_FS_DEBUG */
19305 +
19306 +#define UBIFS_DBG(op)
19307 +#define ubifs_assert(expr) ({})
19308 +#define ubifs_assert_cmt_locked(c)
19309 +#define dbg_dump_stack()
19310 +#define dbg_err(fmt, ...) ({})
19311 +#define dbg_msg(fmt, ...) ({})
19312 +#define dbg_key(c, key, fmt, ...) ({})
19313 +
19314 +#define dbg_gen(fmt, ...) ({})
19315 +#define dbg_jnl(fmt, ...) ({})
19316 +#define dbg_tnc(fmt, ...) ({})
19317 +#define dbg_lp(fmt, ...) ({})
19318 +#define dbg_find(fmt, ...) ({})
19319 +#define dbg_mnt(fmt, ...) ({})
19320 +#define dbg_io(fmt, ...) ({})
19321 +#define dbg_cmt(fmt, ...) ({})
19322 +#define dbg_budg(fmt, ...) ({})
19323 +#define dbg_log(fmt, ...) ({})
19324 +#define dbg_gc(fmt, ...) ({})
19325 +#define dbg_scan(fmt, ...) ({})
19326 +#define dbg_rcvry(fmt, ...) ({})
19327 +
19328 +#define dbg_ntype(type) ""
19329 +#define dbg_cstate(cmt_state) ""
19330 +#define dbg_get_key_dump(c, key) ({})
19331 +#define dbg_dump_inode(c, inode) ({})
19332 +#define dbg_dump_node(c, node) ({})
19333 +#define dbg_dump_budget_req(req) ({})
19334 +#define dbg_dump_lstats(lst) ({})
19335 +#define dbg_dump_budg(c) ({})
19336 +#define dbg_dump_lprop(c, lp) ({})
19337 +#define dbg_dump_lprops(c) ({})
19338 +#define dbg_dump_leb(c, lnum) ({})
19339 +#define dbg_dump_znode(c, znode) ({})
19340 +#define dbg_dump_heap(c, heap, cat) ({})
19341 +#define dbg_dump_pnode(c, pnode, parent, iip) ({})
19342 +#define dbg_dump_tnc(c) ({})
19343 +#define dbg_dump_index(c) ({})
19344 +
19345 +#define dbg_walk_index(c, leaf_cb, znode_cb, priv) 0
19346 +
19347 +#define dbg_old_index_check_init(c, zroot) 0
19348 +#define dbg_check_old_index(c, zroot) 0
19349 +
19350 +#define dbg_check_cats(c) 0
19351 +
19352 +#define dbg_check_ltab(c) 0
19353 +
19354 +#define dbg_check_dir_size(c, dir) 0
19355 +
19356 +#define dbg_check_tnc(c, x) 0
19357 +
19358 +#define dbg_check_idx_size(c, idx_size) 0
19359 +#define dbg_check_filesystem(c) 0
19360 +
19361 +#define dbg_check_heap(c, heap, cat, add_pos) ({})
19362 +
19363 +#define dbg_check_lprops(c) 0
19364 +#define dbg_check_lpt_nodes(c, cnode, row, col) 0
19365 +
19366 +#define dbg_force_in_the_gaps_enabled 0
19367 +#define dbg_force_in_the_gaps() 0
19368 +
19369 +#define dbg_failure_mode 0
19370 +#define dbg_failure_mode_registration(c) ({})
19371 +#define dbg_failure_mode_deregistration(c) ({})
19372 +
19373 +#endif /* !CONFIG_UBIFS_FS_DEBUG */
19374 +
19375 +#endif /* !__UBIFS_DEBUG_H__ */
19376 diff --exclude=.git -urN linux-2.6.25.6/fs/ubifs/dir.c avr32-2.6/fs/ubifs/dir.c
19377 --- linux-2.6.25.6/fs/ubifs/dir.c 1970-01-01 01:00:00.000000000 +0100
19378 +++ avr32-2.6/fs/ubifs/dir.c 2008-06-12 15:09:45.364399968 +0200
19379 @@ -0,0 +1,1017 @@
19380 +/*
19381 + * This file is part of UBIFS.
19382 + *
19383 + * Copyright (C) 2006-2008 Nokia Corporation.
19384 + * Copyright (C) 2006, 2007 University of Szeged, Hungary
19385 + *
19386 + * This program is free software; you can redistribute it and/or modify it
19387 + * under the terms of the GNU General Public License version 2 as published by
19388 + * the Free Software Foundation.
19389 + *
19390 + * This program is distributed in the hope that it will be useful, but WITHOUT
19391 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19392 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
19393 + * more details.
19394 + *
19395 + * You should have received a copy of the GNU General Public License along with
19396 + * this program; if not, write to the Free Software Foundation, Inc., 51
19397 + * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19398 + *
19399 + * Authors: Artem Bityutskiy (Битюцкий Артём)
19400 + * Adrian Hunter
19401 + * Zoltan Sogor
19402 + */
19403 +
19404 +/*
19405 + * This file implements directory operations.
19406 + *
19407 + * All FS operations in this file allocate budget before writing anything to the
19408 + * media. If they fail to allocate it, the error is returned. The only
19409 + * exceptions are 'ubifs_unlink()' and 'ubifs_rmdir()' which keep working even
19410 + * if they unable to allocate the budget, because deletion %-ENOSPC failure is
19411 + * not what users are usually ready to get. UBIFS budgeting subsystem has some
19412 + * space reserved for these purposes.
19413 + *
19414 + * All operations in this file change the parent inode, e.g., 'ubifs_link()'
19415 + * changes ctime and nlink of the parent inode. The parent inode is written to
19416 + * the media straight away - it is not marked as dirty and there is no
19417 + * write-back for it. This was done to simplify file-system recovery which
19418 + * would otherwise be very difficult to do. So instead of marking the parent
19419 + * inode dirty, the operations mark it clean.
19420 + */
19421 +
19422 +#include "ubifs.h"
19423 +
19424 +/*
19425 + * Provide backing_dev_info in order to disable readahead. For UBIFS, I/O is
19426 + * not deferred, it is done immediately in readpage, which means the user would
19427 + * have to wait not just for their own I/O but the readahead I/O as well i.e.
19428 + * completely pointless.
19429 + */
19430 +struct backing_dev_info ubifs_backing_dev_info = {
19431 + .ra_pages = 0, /* Set to zero to disable readahead */
19432 + .state = 0,
19433 + .capabilities = BDI_CAP_MAP_COPY,
19434 + .unplug_io_fn = default_unplug_io_fn,
19435 +};
19436 +
19437 +/**
19438 + * inherit_flags - inherit flags of the parent inode.
19439 + * @dir: parent inode
19440 + * @mode: new inode mode flags
19441 + *
19442 + * This is a helper function for 'ubifs_new_inode()' which inherits flag of the
19443 + * parent directory inode @dir. UBIFS inodes inherit the following flags:
19444 + * o %UBIFS_COMPR_FL, which is useful to switch compression on/of on
19445 + * sub-directory basis;
19446 + * o %UBIFS_SYNC_FL - useful for the same reasons;
19447 + * o %UBIFS_DIRSYNC_FL - similar, but relevant only to directories.
19448 + *
19449 + * This function returns the inherited flags.
19450 + */
19451 +static int inherit_flags(const struct inode *dir, int mode)
19452 +{
19453 + int flags;
19454 + const struct ubifs_inode *ui = ubifs_inode(dir);
19455 +
19456 + if (!S_ISDIR(dir->i_mode))
19457 + /*
19458 + * The parent is not a directory, which means that an extended
19459 + * attribute inode is being created. No flags.
19460 + */
19461 + return 0;
19462 +
19463 + flags = ui->flags & (UBIFS_COMPR_FL | UBIFS_SYNC_FL | UBIFS_DIRSYNC_FL);
19464 + if (!S_ISDIR(mode))
19465 + /* The "DIRSYNC" flag only applies to directories */
19466 + flags &= ~UBIFS_DIRSYNC_FL;
19467 +
19468 + return flags;
19469 +}
19470 +
19471 +/**
19472 + * ubifs_new_inode - allocate new UBIFS inode object.
19473 + * @c: UBIFS file-system description object
19474 + * @dir: parent directory inode
19475 + * @mode: inode mode flags
19476 + *
19477 + * This function finds an unused inode number, allocates new inode and
19478 + * initializes it. Returns new inode in case of success and an error code in
19479 + * case of failure.
19480 + */
19481 +struct inode *ubifs_new_inode(struct ubifs_info *c, const struct inode *dir,
19482 + int mode)
19483 +{
19484 + struct inode *inode;
19485 + struct ubifs_inode *ui;
19486 +
19487 + inode = new_inode(c->vfs_sb);
19488 + if (!inode)
19489 + return ERR_PTR(-ENOMEM);
19490 +
19491 + /*
19492 + * Set 'S_NOCMTIME' to prevent VFS form updating [mc]time of inodes and
19493 + * marking them dirty in file write path (see 'file_update_time()').
19494 + * UBIFS has to fully control "clean <-> dirty" transitions of inodes
19495 + * to make budgeting work.
19496 + */
19497 + inode->i_flags |= (S_NOCMTIME);
19498 +
19499 + inode->i_uid = current->fsuid;
19500 + if (dir->i_mode & S_ISGID) {
19501 + inode->i_gid = dir->i_gid;
19502 + if (S_ISDIR(mode))
19503 + mode |= S_ISGID;
19504 + } else
19505 + inode->i_gid = current->fsgid;
19506 + inode->i_mode = mode;
19507 + inode->i_mtime = inode->i_atime = inode->i_ctime =
19508 + ubifs_current_time(inode);
19509 + inode->i_mapping->nrpages = 0;
19510 + /* Disable readahead */
19511 + inode->i_mapping->backing_dev_info = &ubifs_backing_dev_info;
19512 +
19513 + switch (mode & S_IFMT) {
19514 + case S_IFREG:
19515 + inode->i_mapping->a_ops = &ubifs_file_address_operations;
19516 + inode->i_op = &ubifs_file_inode_operations;
19517 + inode->i_fop = &ubifs_file_operations;
19518 + break;
19519 + case S_IFDIR:
19520 + inode->i_op = &ubifs_dir_inode_operations;
19521 + inode->i_fop = &ubifs_dir_operations;
19522 + inode->i_size = UBIFS_INO_NODE_SZ;
19523 + break;
19524 + case S_IFLNK:
19525 + inode->i_op = &ubifs_symlink_inode_operations;
19526 + break;
19527 + case S_IFSOCK:
19528 + case S_IFIFO:
19529 + case S_IFBLK:
19530 + case S_IFCHR:
19531 + inode->i_op = &ubifs_file_inode_operations;
19532 + break;
19533 + default:
19534 + BUG();
19535 + }
19536 +
19537 + ui = ubifs_inode(inode);
19538 + ui->flags = inherit_flags(dir, mode);
19539 + ubifs_set_inode_flags(inode);
19540 +
19541 + if (S_ISREG(mode))
19542 + ui->compr_type = c->default_compr;
19543 + else
19544 + ui->compr_type = UBIFS_COMPR_NONE;
19545 +
19546 + spin_lock(&c->cnt_lock);
19547 + /* Inode number overflow is currently not supported */
19548 + if (c->highest_inum >= INUM_WARN_WATERMARK) {
19549 + if (c->highest_inum >= INUM_WATERMARK) {
19550 + spin_unlock(&c->cnt_lock);
19551 + ubifs_err("out of inode numbers");
19552 + make_bad_inode(inode);
19553 + iput(inode);
19554 + return ERR_PTR(-EINVAL);
19555 + }
19556 + ubifs_warn("running out of inode numbers (current %lu, max %d)",
19557 + c->highest_inum, INUM_WATERMARK);
19558 + }
19559 +
19560 + inode->i_ino = ++c->highest_inum;
19561 + inode->i_generation = ++c->vfs_gen;
19562 + /*
19563 + * The creation sequence number remains with this inode for its
19564 + * lifetime. All nodes for this inode have a greater sequence number,
19565 + * and so it is possible to distinguish obsolete nodes belonging to a
19566 + * previous incarnation of the same inode number - for example, for the
19567 + * purpose of rebuilding the index.
19568 + */
19569 + ui->creat_sqnum = ++c->max_sqnum;
19570 + spin_unlock(&c->cnt_lock);
19571 +
19572 + return inode;
19573 +}
19574 +
19575 +#ifdef CONFIG_UBIFS_FS_DEBUG
19576 +
19577 +static int dbg_check_name(struct ubifs_dent_node *dent, struct qstr *nm)
19578 +{
19579 + if (!(ubifs_chk_flags & UBIFS_CHK_GEN))
19580 + return 0;
19581 + if (le16_to_cpu(dent->nlen) != nm->len)
19582 + return -EINVAL;
19583 + if (memcmp(dent->name, nm->name, nm->len))
19584 + return -EINVAL;
19585 + return 0;
19586 +}
19587 +
19588 +#else
19589 +
19590 +#define dbg_check_name(dent, nm) 0
19591 +
19592 +#endif
19593 +
19594 +static struct dentry *ubifs_lookup(struct inode *dir, struct dentry *dentry,
19595 + struct nameidata *nd)
19596 +{
19597 + int err;
19598 + union ubifs_key key;
19599 + struct inode *inode = NULL;
19600 + struct ubifs_dent_node *dent;
19601 + struct ubifs_info *c = dir->i_sb->s_fs_info;
19602 +
19603 + dbg_gen("'%.*s' in dir ino %lu",
19604 + dentry->d_name.len, dentry->d_name.name, dir->i_ino);
19605 +
19606 + if (dentry->d_name.len > UBIFS_MAX_NLEN)
19607 + return ERR_PTR(-ENAMETOOLONG);
19608 +
19609 + dent = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS);
19610 + if (!dent)
19611 + return ERR_PTR(-ENOMEM);
19612 +
19613 + dent_key_init(c, &key, dir->i_ino, &dentry->d_name);
19614 +
19615 + err = ubifs_tnc_lookup_nm(c, &key, dent, &dentry->d_name);
19616 + if (err) {
19617 + if (err == -ENOENT) {
19618 + dbg_gen("not found");
19619 + goto done;
19620 + }
19621 + goto out;
19622 + }
19623 +
19624 + if (dbg_check_name(dent, &dentry->d_name)) {
19625 + err = -EINVAL;
19626 + goto out;
19627 + }
19628 +
19629 + inode = ubifs_iget(dir->i_sb, le64_to_cpu(dent->inum));
19630 + if (IS_ERR(inode)) {
19631 + /*
19632 + * This should not happen. Probably the file-system needs
19633 + * checking.
19634 + */
19635 + err = PTR_ERR(inode);
19636 + ubifs_err("dead directory entry '%.*s', error %d",
19637 + dentry->d_name.len, dentry->d_name.name, err);
19638 + ubifs_ro_mode(c, err);
19639 + goto out;
19640 + }
19641 +
19642 +done:
19643 + kfree(dent);
19644 + /*
19645 + * Note, d_splice_alias() would be required instead if we supported
19646 + * NFS.
19647 + */
19648 + d_add(dentry, inode);
19649 + return NULL;
19650 +
19651 +out:
19652 + kfree(dent);
19653 + return ERR_PTR(err);
19654 +}
19655 +
19656 +static int ubifs_create(struct inode *dir, struct dentry *dentry, int mode,
19657 + struct nameidata *nd)
19658 +{
19659 + struct inode *inode;
19660 + struct ubifs_info *c = dir->i_sb->s_fs_info;
19661 + struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1 };
19662 + int err, sz_change = CALC_DENT_SIZE(dentry->d_name.len);
19663 +
19664 + dbg_gen("dent '%.*s', mode %#x in dir ino %lu",
19665 + dentry->d_name.len, dentry->d_name.name, mode, dir->i_ino);
19666 +
19667 + inode = ubifs_new_inode(c, dir, mode);
19668 + if (IS_ERR(inode))
19669 + return PTR_ERR(inode);
19670 +
19671 + err = ubifs_budget_inode_op(c, dir, &req);
19672 + if (err)
19673 + goto out;
19674 +
19675 + dir->i_size += sz_change;
19676 +
19677 + err = ubifs_jnl_update(c, dir, &dentry->d_name, inode, 0,
19678 + IS_DIRSYNC(dir), 0);
19679 + if (err)
19680 + goto out_budg;
19681 +
19682 + insert_inode_hash(inode);
19683 + d_instantiate(dentry, inode);
19684 + ubifs_release_ino_clean(c, dir, &req);
19685 + return 0;
19686 +
19687 +out_budg:
19688 + dir->i_size -= sz_change;
19689 + ubifs_cancel_ino_op(c, dir, &req);
19690 + ubifs_err("cannot create regular file, error %d", err);
19691 +out:
19692 + make_bad_inode(inode);
19693 + iput(inode);
19694 + return err;
19695 +}
19696 +
19697 +/**
19698 + * vfs_dent_type - get VFS directory entry type.
19699 + * @type: UBIFS directory entry type
19700 + *
19701 + * This function converts UBIFS directory entry type into VFS directory entry
19702 + * type.
19703 + */
19704 +static unsigned int vfs_dent_type(uint8_t type)
19705 +{
19706 + switch (type) {
19707 + case UBIFS_ITYPE_REG:
19708 + return DT_REG;
19709 + case UBIFS_ITYPE_DIR:
19710 + return DT_DIR;
19711 + case UBIFS_ITYPE_LNK:
19712 + return DT_LNK;
19713 + case UBIFS_ITYPE_BLK:
19714 + return DT_BLK;
19715 + case UBIFS_ITYPE_CHR:
19716 + return DT_CHR;
19717 + case UBIFS_ITYPE_FIFO:
19718 + return DT_FIFO;
19719 + case UBIFS_ITYPE_SOCK:
19720 + return DT_SOCK;
19721 + default:
19722 + BUG();
19723 + }
19724 + return 0;
19725 +}
19726 +
19727 +/*
19728 + * The classical Unix view for directory is that it is a linear array of
19729 + * (name, inode number) entries. Linux/VFS assumes this model as well.
19730 + * Particularly, 'readdir()' call wants us to return a directory entry offset
19731 + * which later may be used to continue 'readdir()'ing the directory or to
19732 + * 'seek()' to that specific direntry. Obviously UBIFS does not really fit this
19733 + * model because directory entries are identified by keys, which may collide.
19734 + *
19735 + * UBIFS uses directory entry hash value for directory offsets, so
19736 + * 'seekdir()'/'telldir()' may not always work because of possible key
19737 + * collisions. But UBIFS guarantees that consecutive 'readdir()' calls work
19738 + * properly by means of saving full directory entry name in the private field
19739 + * of the file description object.
19740 + *
19741 + * This means that UBIFS cannot support NFS which requires full
19742 + * 'seekdir()'/'telldir()' support.
19743 + */
19744 +static int ubifs_readdir(struct file *file, void *dirent, filldir_t filldir)
19745 +{
19746 + int err, over = 0;
19747 + struct qstr nm;
19748 + union ubifs_key key;
19749 + struct ubifs_dent_node *dent;
19750 + struct inode *dir = file->f_path.dentry->d_inode;
19751 + struct ubifs_info *c = dir->i_sb->s_fs_info;
19752 +
19753 + dbg_gen("dir ino %lu, f_pos %#llx", dir->i_ino, file->f_pos);
19754 +
19755 + if (file->f_pos > UBIFS_S_KEY_HASH_MASK || file->f_pos == 2)
19756 + /*
19757 + * The directory was seek'ed to a senseless position or there
19758 + * are no more entries.
19759 + */
19760 + return 0;
19761 +
19762 + /* File positions 0 and 1 correspond to "." and ".." */
19763 + if (file->f_pos == 0) {
19764 + ubifs_assert(!file->private_data);
19765 + over = filldir(dirent, ".", 1, 0, dir->i_ino, DT_DIR);
19766 + if (over)
19767 + return 0;
19768 + file->f_pos = 1;
19769 + }
19770 +
19771 + if (file->f_pos == 1) {
19772 + ubifs_assert(!file->private_data);
19773 + over = filldir(dirent, "..", 2, 1,
19774 + parent_ino(file->f_path.dentry), DT_DIR);
19775 + if (over)
19776 + return 0;
19777 +
19778 + /* Find the first entry in TNC and save it */
19779 + lowest_dent_key(c, &key, dir->i_ino);
19780 + nm.name = NULL;
19781 + dent = ubifs_tnc_next_ent(c, &key, &nm);
19782 + if (IS_ERR(dent)) {
19783 + err = PTR_ERR(dent);
19784 + goto out;
19785 + }
19786 +
19787 + file->f_pos = key_hash_flash(c, &dent->key);
19788 + file->private_data = dent;
19789 + }
19790 +
19791 + dent = file->private_data;
19792 + if (!dent) {
19793 + /*
19794 + * The directory was seek'ed to and is now readdir'ed.
19795 + * Find the entry corresponding to @file->f_pos or the
19796 + * closest one.
19797 + */
19798 + dent_key_init_hash(c, &key, dir->i_ino, file->f_pos);
19799 + nm.name = NULL;
19800 + dent = ubifs_tnc_next_ent(c, &key, &nm);
19801 + if (IS_ERR(dent)) {
19802 + err = PTR_ERR(dent);
19803 + goto out;
19804 + }
19805 + file->f_pos = key_hash_flash(c, &dent->key);
19806 + file->private_data = dent;
19807 + }
19808 +
19809 + while (1) {
19810 + dbg_gen("feed '%s', ino %llu, new f_pos %#x",
19811 + dent->name, le64_to_cpu(dent->inum),
19812 + key_hash_flash(c, &dent->key));
19813 + ubifs_assert(dent->ch.sqnum > ubifs_inode(dir)->creat_sqnum);
19814 +
19815 + nm.len = le16_to_cpu(dent->nlen);
19816 + over = filldir(dirent, dent->name, nm.len, file->f_pos,
19817 + le64_to_cpu(dent->inum),
19818 + vfs_dent_type(dent->type));
19819 + if (over)
19820 + return 0;
19821 +
19822 + /* Switch to the next entry */
19823 + key_read(c, &dent->key, &key);
19824 + nm.name = dent->name;
19825 + dent = ubifs_tnc_next_ent(c, &key, &nm);
19826 + if (IS_ERR(dent)) {
19827 + err = PTR_ERR(dent);
19828 + goto out;
19829 + }
19830 +
19831 + kfree(file->private_data);
19832 + file->f_pos = key_hash_flash(c, &dent->key);
19833 + file->private_data = dent;
19834 + cond_resched();
19835 + }
19836 +
19837 +out:
19838 + if (err != -ENOENT) {
19839 + ubifs_err("cannot find next direntry, error %d", err);
19840 + return err;
19841 + }
19842 +
19843 + kfree(file->private_data);
19844 + file->private_data = NULL;
19845 + file->f_pos = 2;
19846 + return 0;
19847 +}
19848 +
19849 +/* If a directory is seeked, we have to free saved readdir() state */
19850 +loff_t ubifs_dir_llseek(struct file *file, loff_t offset, int origin)
19851 +{
19852 + kfree(file->private_data);
19853 + file->private_data = NULL;
19854 + return generic_file_llseek(file, offset, origin);
19855 +}
19856 +
19857 +/* Free saved readdir() state when the directory is closed */
19858 +static int ubifs_dir_release(struct inode *dir, struct file *file)
19859 +{
19860 + kfree(file->private_data);
19861 + file->private_data = NULL;
19862 + return 0;
19863 +}
19864 +
19865 +static int ubifs_link(struct dentry *old_dentry, struct inode *dir,
19866 + struct dentry *dentry)
19867 +{
19868 + struct ubifs_info *c = dir->i_sb->s_fs_info;
19869 + struct inode *inode = old_dentry->d_inode;
19870 + struct ubifs_inode *ui = ubifs_inode(inode);
19871 + struct ubifs_budget_req req = { .new_dent = 1, .dirtied_ino = 1,
19872 + .dirtied_ino_d = ui->data_len };
19873 + int err, sz_change = CALC_DENT_SIZE(dentry->d_name.len);
19874 +
19875 + dbg_gen("dent '%.*s' to ino %lu (nlink %d) in dir ino %lu",
19876 + dentry->d_name.len, dentry->d_name.name, inode->i_ino,
19877 + inode->i_nlink, dir->i_ino);
19878 +
19879 + err = ubifs_budget_inode_op(c, dir, &req);
19880 + if (err)
19881 + return err;
19882 +
19883 + inc_nlink(inode);
19884 + dir->i_size += sz_change;
19885 + inode->i_ctime = dir->i_mtime = dir->i_ctime =
19886 + ubifs_current_time(inode);
19887 +
19888 + err = ubifs_jnl_update(c, dir, &dentry->d_name, inode, 0,
19889 + IS_DIRSYNC(dir), 0);
19890 + if (err)
19891 + goto out_budg;
19892 +
19893 + atomic_inc(&inode->i_count);
19894 + d_instantiate(dentry, inode);
19895 + ubifs_release_ino_clean(c, dir, &req);
19896 + return 0;
19897 +
19898 +out_budg:
19899 + dir->i_size -= sz_change;
19900 + ubifs_cancel_ino_op(c, dir, &req);
19901 + drop_nlink(inode);
19902 + iput(inode);
19903 + return err;
19904 +}
19905 +
19906 +static int ubifs_unlink(struct inode *dir, struct dentry *dentry)
19907 +{
19908 + struct ubifs_info *c = dir->i_sb->s_fs_info;
19909 + struct inode *inode = dentry->d_inode;
19910 + struct ubifs_budget_req req = { .mod_dent = 1, .dirtied_ino = 1 };
19911 + int sz_change = CALC_DENT_SIZE(dentry->d_name.len);
19912 + int err, budgeted = 1;
19913 +
19914 + dbg_gen("dent '%.*s' from ino %lu (nlink %d) in dir ino %lu",
19915 + dentry->d_name.len, dentry->d_name.name, inode->i_ino,
19916 + inode->i_nlink, dir->i_ino);
19917 +
19918 + err = ubifs_budget_inode_op(c, dir, &req);
19919 + if (err) {
19920 + if (err != -ENOSPC)
19921 + return err;
19922 + err = 0;
19923 + budgeted = 0;
19924 + }
19925 +
19926 + dir->i_size -= sz_change;
19927 + dir->i_mtime = dir->i_ctime = ubifs_current_time(dir);
19928 +
19929 + inode->i_ctime = dir->i_ctime;
19930 + drop_nlink(inode);
19931 +
19932 + err = ubifs_jnl_update(c, dir, &dentry->d_name, inode, 1,
19933 + IS_DIRSYNC(dir), 0);
19934 + if (err)
19935 + goto out_budg;
19936 +
19937 + if (budgeted)
19938 + ubifs_release_ino_clean(c, dir, &req);
19939 +
19940 + return 0;
19941 +
19942 +out_budg:
19943 + dir->i_size += sz_change;
19944 + inc_nlink(inode);
19945 + if (budgeted)
19946 + ubifs_cancel_ino_op(c, dir, &req);
19947 + return err;
19948 +}
19949 +
19950 +/**
19951 + * check_dir_empty - check if a directory is empty or not.
19952 + * @c: UBIFS file-system description object
19953 + * @dir: VFS inode object of the directory to check
19954 + *
19955 + * This function checks if directory @dir is empty. Returns zero if the
19956 + * directory is empty, %-ENOTEMPTY if it is not, and other negative error codes
19957 + * in case of of errors.
19958 + */
19959 +static int check_dir_empty(struct ubifs_info *c, struct inode *dir)
19960 +{
19961 + struct qstr nm = { .name = NULL };
19962 + struct ubifs_dent_node *dent;
19963 + union ubifs_key key;
19964 + int err;
19965 +
19966 + lowest_dent_key(c, &key, dir->i_ino);
19967 + dent = ubifs_tnc_next_ent(c, &key, &nm);
19968 + if (IS_ERR(dent)) {
19969 + err = PTR_ERR(dent);
19970 + if (err == -ENOENT)
19971 + err = 0;
19972 + } else {
19973 + kfree(dent);
19974 + err = -ENOTEMPTY;
19975 + }
19976 +
19977 + return err;
19978 +}
19979 +
19980 +static int ubifs_rmdir(struct inode *dir, struct dentry *dentry)
19981 +{
19982 + struct ubifs_info *c = dir->i_sb->s_fs_info;
19983 + struct inode *inode = dentry->d_inode;
19984 + struct ubifs_budget_req req = { .mod_dent = 1, .dirtied_ino = 1 };
19985 + int sz_change = CALC_DENT_SIZE(dentry->d_name.len);
19986 + int err, budgeted = 0;
19987 +
19988 + dbg_gen("directory '%.*s', ino %lu in dir ino %lu", dentry->d_name.len,
19989 + dentry->d_name.name, inode->i_ino, dir->i_ino);
19990 +
19991 + err = check_dir_empty(c, dentry->d_inode);
19992 + if (err)
19993 + return err;
19994 +
19995 + budgeted = 1;
19996 + err = ubifs_budget_inode_op(c, dir, &req);
19997 + if (err) {
19998 + if (err != -ENOSPC)
19999 + return err;
20000 + budgeted = 0;
20001 + }
20002 +
20003 + dir->i_size -= sz_change;
20004 + dir->i_mtime = dir->i_ctime = ubifs_current_time(dir);
20005 + drop_nlink(dir);
20006 +
20007 + inode->i_size = 0;
20008 + inode->i_ctime = dir->i_ctime;
20009 + clear_nlink(inode);
20010 +
20011 + err = ubifs_jnl_update(c, dir, &dentry->d_name, inode, 1,
20012 + IS_DIRSYNC(dir), 0);
20013 + if (err)
20014 + goto out_budg;
20015 +
20016 + if (budgeted)
20017 + ubifs_release_ino_clean(c, dir, &req);
20018 +
20019 + return 0;
20020 +
20021 +out_budg:
20022 + dir->i_size += sz_change;
20023 + inc_nlink(dir);
20024 + inc_nlink(inode);
20025 + inc_nlink(inode);
20026 + if (budgeted)
20027 + ubifs_cancel_ino_op(c, dir, &req);
20028 + return err;
20029 +}
20030 +
20031 +static int ubifs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
20032 +{
20033 + struct inode *inode;
20034 + struct ubifs_info *c = dir->i_sb->s_fs_info;
20035 + struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1 };
20036 + int err, sz_change = CALC_DENT_SIZE(dentry->d_name.len);
20037 +
20038 + dbg_gen("dent '%.*s', mode %#x in dir ino %lu",
20039 + dentry->d_name.len, dentry->d_name.name, mode, dir->i_ino);
20040 +
20041 + err = ubifs_budget_inode_op(c, dir, &req);
20042 + if (err)
20043 + return err;
20044 +
20045 + inode = ubifs_new_inode(c, dir, S_IFDIR | mode);
20046 + if (IS_ERR(inode)) {
20047 + err = PTR_ERR(inode);
20048 + goto out_budg;
20049 + }
20050 +
20051 + insert_inode_hash(inode);
20052 + inc_nlink(inode);
20053 +
20054 + dir->i_mtime = dir->i_ctime = ubifs_current_time(dir);
20055 + dir->i_size += sz_change;
20056 + inc_nlink(dir);
20057 +
20058 + err = ubifs_jnl_update(c, dir, &dentry->d_name, inode, 0,
20059 + IS_DIRSYNC(dir), 0);
20060 + if (err) {
20061 + ubifs_err("cannot create directory, error %d", err);
20062 + goto out_inode;
20063 + }
20064 +
20065 + d_instantiate(dentry, inode);
20066 + ubifs_release_ino_clean(c, dir, &req);
20067 + return 0;
20068 +
20069 +out_inode:
20070 + dir->i_size -= sz_change;
20071 + drop_nlink(dir);
20072 + make_bad_inode(inode);
20073 + iput(inode);
20074 +out_budg:
20075 + ubifs_cancel_ino_op(c, dir, &req);
20076 + return err;
20077 +}
20078 +
20079 +static int ubifs_mknod(struct inode *dir, struct dentry *dentry,
20080 + int mode, dev_t rdev)
20081 +{
20082 + struct inode *inode;
20083 + struct ubifs_info *c = dir->i_sb->s_fs_info;
20084 + struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1 };
20085 + union ubifs_dev_desc *dev = NULL;
20086 + int sz_change = CALC_DENT_SIZE(dentry->d_name.len);
20087 + int err, devlen = 0;
20088 +
20089 + dbg_gen("dent '%.*s' in dir ino %lu",
20090 + dentry->d_name.len, dentry->d_name.name, dir->i_ino);
20091 +
20092 + if (!new_valid_dev(rdev))
20093 + return -EINVAL;
20094 +
20095 + if (S_ISBLK(mode) || S_ISCHR(mode)) {
20096 + dev = kmalloc(sizeof(union ubifs_dev_desc), GFP_NOFS);
20097 + if (!dev)
20098 + return -ENOMEM;
20099 + devlen = ubifs_encode_dev(dev, rdev);
20100 + }
20101 +
20102 + err = ubifs_budget_inode_op(c, dir, &req);
20103 + if (err) {
20104 + kfree(dev);
20105 + return err;
20106 + }
20107 +
20108 + inode = ubifs_new_inode(c, dir, mode);
20109 + if (IS_ERR(inode)) {
20110 + kfree(dev);
20111 + err = PTR_ERR(inode);
20112 + goto out_budg;
20113 + }
20114 +
20115 + init_special_inode(inode, inode->i_mode, rdev);
20116 +
20117 + inode->i_size = devlen;
20118 + ubifs_inode(inode)->data = dev;
20119 + ubifs_inode(inode)->data_len = devlen;
20120 +
20121 + dir->i_size += sz_change;
20122 +
20123 + err = ubifs_jnl_update(c, dir, &dentry->d_name, inode, 0,
20124 + IS_DIRSYNC(dir), 0);
20125 + if (err)
20126 + goto out_inode;
20127 +
20128 + insert_inode_hash(inode);
20129 + d_instantiate(dentry, inode);
20130 + ubifs_release_ino_clean(c, dir, &req);
20131 + return 0;
20132 +
20133 +out_inode:
20134 + dir->i_size -= sz_change;
20135 + make_bad_inode(inode);
20136 + iput(inode);
20137 +out_budg:
20138 + ubifs_cancel_ino_op(c, dir, &req);
20139 + return err;
20140 +}
20141 +
20142 +static int ubifs_symlink(struct inode *dir, struct dentry *dentry,
20143 + const char *symname)
20144 +{
20145 + struct inode *inode;
20146 + struct ubifs_inode *ui;
20147 + struct ubifs_info *c = dir->i_sb->s_fs_info;
20148 + int err, len = strlen(symname);
20149 + int sz_change = CALC_DENT_SIZE(dentry->d_name.len);
20150 + struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1,
20151 + .new_ino_d = len };
20152 +
20153 + dbg_gen("dent '%.*s', target '%s' in dir ino %lu", dentry->d_name.len,
20154 + dentry->d_name.name, symname, dir->i_ino);
20155 +
20156 + if (len > UBIFS_MAX_INO_DATA)
20157 + return -ENAMETOOLONG;
20158 +
20159 + err = ubifs_budget_inode_op(c, dir, &req);
20160 + if (err)
20161 + return err;
20162 +
20163 + inode = ubifs_new_inode(c, dir, S_IFLNK | S_IRWXUGO);
20164 + if (IS_ERR(inode)) {
20165 + err = PTR_ERR(inode);
20166 + goto out_budg;
20167 + }
20168 +
20169 + ui = ubifs_inode(inode);
20170 + ui->data = kmalloc(len + 1, GFP_NOFS);
20171 + if (!ui->data) {
20172 + err = -ENOMEM;
20173 + goto out_inode;
20174 + }
20175 +
20176 + memcpy(ui->data, symname, len);
20177 + ((char *)ui->data)[len] = '\0';
20178 + /*
20179 + * The terminating zero byte is not written to the flash media and it
20180 + * is put just to make later in-memory string processing simpler. Thus,
20181 + * data length is @len, not @len + %1.
20182 + */
20183 + ui->data_len = len;
20184 + inode->i_size = len;
20185 +
20186 + dir->i_size += sz_change;
20187 +
20188 + err = ubifs_jnl_update(c, dir, &dentry->d_name, inode, 0,
20189 + IS_DIRSYNC(dir), 0);
20190 + if (err)
20191 + goto out_dir;
20192 +
20193 + insert_inode_hash(inode);
20194 + d_instantiate(dentry, inode);
20195 + ubifs_release_ino_clean(c, dir, &req);
20196 + return 0;
20197 +
20198 +out_dir:
20199 + dir->i_size -= sz_change;
20200 +out_inode:
20201 + make_bad_inode(inode);
20202 + iput(inode);
20203 +out_budg:
20204 + ubifs_cancel_ino_op(c, dir, &req);
20205 + return err;
20206 +}
20207 +
20208 +static int ubifs_rename(struct inode *old_dir, struct dentry *old_dentry,
20209 + struct inode *new_dir, struct dentry *new_dentry)
20210 +{
20211 + struct ubifs_info *c = old_dir->i_sb->s_fs_info;
20212 + struct inode *old_inode = old_dentry->d_inode;
20213 + struct inode *new_inode = new_dentry->d_inode;
20214 + int err, move = (new_dir != old_dir);
20215 + int is_dir = S_ISDIR(old_inode->i_mode);
20216 + int unlink = !!new_inode;
20217 + int dirsync = (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir));
20218 + int new_sz = CALC_DENT_SIZE(new_dentry->d_name.len);
20219 + int old_sz = CALC_DENT_SIZE(old_dentry->d_name.len);
20220 + struct ubifs_budget_req req = { .new_dent = 1, .mod_dent = 1 };
20221 + struct timespec time = ubifs_current_time(old_dir);
20222 +
20223 + dbg_gen("dent '%.*s' ino %lu in dir ino %lu to dent '%.*s' in "
20224 + "dir ino %lu", old_dentry->d_name.len, old_dentry->d_name.name,
20225 + old_inode->i_ino, old_dir->i_ino, new_dentry->d_name.len,
20226 + new_dentry->d_name.name, new_dir->i_ino);
20227 +
20228 + if (unlink && is_dir) {
20229 + err = check_dir_empty(c, new_inode);
20230 + if (err)
20231 + return err;
20232 + }
20233 +
20234 + if (move) {
20235 + req.dirtied_ino = 1;
20236 + if (unlink) {
20237 + req.dirtied_ino += 2;
20238 + req.dirtied_ino_d = ubifs_inode(new_inode)->data_len;
20239 + }
20240 + }
20241 +
20242 + /*
20243 + * Note, rename may write @new_dir inode if the directory entry is
20244 + * moved there. And if the @new_dir is dirty, we do not bother to make
20245 + * it clean. It could be done, but requires extra coding which does not
20246 + * seem to be really worth it.
20247 + */
20248 + err = ubifs_budget_inode_op(c, old_dir, &req);
20249 + if (err)
20250 + return err;
20251 +
20252 + /*
20253 + * Like most other Unix systems, set the ctime for inodes on a
20254 + * rename.
20255 + */
20256 + old_inode->i_ctime = time;
20257 +
20258 + /*
20259 + * If we moved a directory to another parent directory, decrement
20260 + * 'i_nlink' of the old parent. Also, update 'i_size' of the old parent
20261 + * as well as its [mc]time.
20262 + */
20263 + if (is_dir && move)
20264 + drop_nlink(old_dir);
20265 + old_dir->i_size -= old_sz;
20266 + old_dir->i_mtime = old_dir->i_ctime = time;
20267 + new_dir->i_mtime = new_dir->i_ctime = time;
20268 +
20269 + /*
20270 + * If we moved a directory object to new directory, parent's 'i_nlink'
20271 + * should be adjusted.
20272 + */
20273 + if (move && is_dir)
20274 + inc_nlink(new_dir);
20275 +
20276 + /*
20277 + * And finally, if we unlinked a direntry which happened to have the
20278 + * same name as the moved direntry, we have to decrement 'i_nlink' of
20279 + * the unlinked inode and change its ctime.
20280 + */
20281 + if (unlink) {
20282 + /*
20283 + * Directories cannot have hard-links, so if this is a
20284 + * directory, decrement its 'i_nlink' twice because an empty
20285 + * directory has 'i_nlink' 2.
20286 + */
20287 + if (is_dir)
20288 + drop_nlink(new_inode);
20289 + new_inode->i_ctime = time;
20290 + drop_nlink(new_inode);
20291 + } else
20292 + new_dir->i_size += new_sz;
20293 +
20294 + err = ubifs_jnl_rename(c, old_dir, old_dentry, new_dir, new_dentry,
20295 + dirsync);
20296 + if (err)
20297 + goto out_inode;
20298 +
20299 + ubifs_release_ino_clean(c, old_dir, &req);
20300 + return 0;
20301 +
20302 +out_inode:
20303 + if (unlink) {
20304 + if (is_dir)
20305 + inc_nlink(new_inode);
20306 + inc_nlink(new_inode);
20307 + } else
20308 + new_dir->i_size -= new_sz;
20309 + old_dir->i_size += old_sz;
20310 + if (is_dir && move) {
20311 + drop_nlink(new_dir);
20312 + inc_nlink(old_dir);
20313 + }
20314 + ubifs_cancel_ino_op(c, old_dir, &req);
20315 + return err;
20316 +}
20317 +
20318 +int ubifs_getattr(struct vfsmount *mnt, struct dentry *dentry,
20319 + struct kstat *stat)
20320 +{
20321 + struct inode *inode = dentry->d_inode;
20322 + loff_t size;
20323 +
20324 + stat->dev = inode->i_sb->s_dev;
20325 + stat->ino = inode->i_ino;
20326 + stat->mode = inode->i_mode;
20327 + stat->nlink = inode->i_nlink;
20328 + stat->uid = inode->i_uid;
20329 + stat->gid = inode->i_gid;
20330 + stat->rdev = inode->i_rdev;
20331 + stat->atime = inode->i_atime;
20332 + stat->mtime = inode->i_mtime;
20333 + stat->ctime = inode->i_ctime;
20334 + stat->blksize = UBIFS_BLOCK_SIZE;
20335 + stat->size = i_size_read(inode);
20336 +
20337 + spin_lock(&inode->i_lock);
20338 + size = ubifs_inode(inode)->xattr_size;
20339 + spin_unlock(&inode->i_lock);
20340 +
20341 + /*
20342 + * Unfortunately, the 'stat()' system call was designed for block
20343 + * device based file systems, and it is not appropriate for UBIFS,
20344 + * because UBIFS does not have notion of "block". For example, it is
20345 + * difficult to tell how many block a directory takes - it actually
20346 + * takes less than 300 bytes, but we have to round it to block size,
20347 + * which introduces large mistake. This makes utilities like 'du' to
20348 + * report completely senseless numbers. This is the reason why UBIFS
20349 + * goes the same way as JFFS2 - it reports zero blocks for everything
20350 + * but regular files, which makes more sense than reporting completely
20351 + * wrong sizes.
20352 + */
20353 + if (S_ISREG(inode->i_mode))
20354 + size += stat->size;
20355 +
20356 + size = ALIGN(size, UBIFS_BLOCK_SIZE);
20357 + /*
20358 + * Note, user-space expects 512-byte blocks count irrespectively of what
20359 + * was reported in @stat->size.
20360 + */
20361 + stat->blocks = size >> 9;
20362 +
20363 + return 0;
20364 +}
20365 +
20366 +struct inode_operations ubifs_dir_inode_operations = {
20367 + .lookup = ubifs_lookup,
20368 + .create = ubifs_create,
20369 + .link = ubifs_link,
20370 + .symlink = ubifs_symlink,
20371 + .unlink = ubifs_unlink,
20372 + .mkdir = ubifs_mkdir,
20373 + .rmdir = ubifs_rmdir,
20374 + .mknod = ubifs_mknod,
20375 + .rename = ubifs_rename,
20376 + .setattr = ubifs_setattr,
20377 + .getattr = ubifs_getattr,
20378 +#ifdef CONFIG_UBIFS_FS_XATTR
20379 + .setxattr = ubifs_setxattr,
20380 + .getxattr = ubifs_getxattr,
20381 + .listxattr = ubifs_listxattr,
20382 + .removexattr = ubifs_removexattr,
20383 +#endif
20384 +};
20385 +
20386 +struct file_operations ubifs_dir_operations = {
20387 + .llseek = ubifs_dir_llseek,
20388 + .release = ubifs_dir_release,
20389 + .read = generic_read_dir,
20390 + .readdir = ubifs_readdir,
20391 + .fsync = ubifs_fsync,
20392 + .unlocked_ioctl = ubifs_ioctl,
20393 +#ifdef CONFIG_COMPAT
20394 + .compat_ioctl = ubifs_compat_ioctl,
20395 +#endif
20396 +};
20397 diff --exclude=.git -urN linux-2.6.25.6/fs/ubifs/file.c avr32-2.6/fs/ubifs/file.c
20398 --- linux-2.6.25.6/fs/ubifs/file.c 1970-01-01 01:00:00.000000000 +0100
20399 +++ avr32-2.6/fs/ubifs/file.c 2008-06-12 15:09:45.364399968 +0200
20400 @@ -0,0 +1,960 @@
20401 +/*
20402 + * This file is part of UBIFS.
20403 + *
20404 + * Copyright (C) 2006-2008 Nokia Corporation.
20405 + *
20406 + * This program is free software; you can redistribute it and/or modify it
20407 + * under the terms of the GNU General Public License version 2 as published by
20408 + * the Free Software Foundation.
20409 + *
20410 + * This program is distributed in the hope that it will be useful, but WITHOUT
20411 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20412 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
20413 + * more details.
20414 + *
20415 + * You should have received a copy of the GNU General Public License along with
20416 + * this program; if not, write to the Free Software Foundation, Inc., 51
20417 + * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20418 + *
20419 + * Authors: Artem Bityutskiy (Битюцкий Артём)
20420 + * Adrian Hunter
20421 + */
20422 +
20423 +/*
20424 + * This file implements VFS file and inode operations of regular files, device
20425 + * nodes and symlinks as well as address space operations.
20426 + *
20427 + * UBIFS uses 2 page flags: PG_private and PG_checked. PG_private is set if the
20428 + * page is dirty and is used for budgeting purposes - dirty pages should not be
20429 + * budgeted. The PG_checked flag is set if full budgeting is required for the
20430 + * page e.g., when it corresponds to a file hole or it is just beyond the file
20431 + * size. The budgeting is done in 'ubifs_write_begin()', because it is OK to
20432 + * fail in this function, and the budget is released in 'ubifs_write_end()'. So
20433 + * the PG_private and PG_checked flags carry the information about how the page
20434 + * was budgeted, to make it possible to release the budget properly.
20435 + *
20436 + * A thing to keep in mind: inode's 'i_mutex' is locked in most VFS operations
20437 + * we implement. However, this is not true for '->writepage()', which might be
20438 + * called with 'i_mutex' unlocked. For example, when pdflush is performing
20439 + * write-back, it calls 'writepage()' with unlocked 'i_mutex', although the
20440 + * inode has 'I_LOCK' flag in this case. At "normal" work-paths 'i_mutex' is
20441 + * locked in '->writepage', e.g. in "sys_write -> alloc_pages -> direct reclaim
20442 + * path'. So, in '->writepage()' we are only guaranteed that the page is
20443 + * locked.
20444 + *
20445 + * Similarly, 'i_mutex' does not have to be locked in readpage(), e.g.,
20446 + * readahead path does not have it locked ("sys_read -> generic_file_aio_read
20447 + * -> ondemand_readahead -> readpage"). In case of readahead, 'I_LOCK' flag is
20448 + * not set as well.
20449 + *
20450 + * This, for example means that there might be 2 concurrent '->writepage()'
20451 + * calls for the same inode, but different inode dirty pages.
20452 + */
20453 +
20454 +#include "ubifs.h"
20455 +#include <linux/mount.h>
20456 +
20457 +static int read_block(struct inode *inode, void *addr, unsigned int block,
20458 + struct ubifs_data_node *dn)
20459 +{
20460 + struct ubifs_info *c = inode->i_sb->s_fs_info;
20461 + int err, len, out_len;
20462 + union ubifs_key key;
20463 + unsigned int dlen;
20464 +
20465 + data_key_init(c, &key, inode->i_ino, block);
20466 + err = ubifs_tnc_lookup(c, &key, dn);
20467 + if (err) {
20468 + if (err == -ENOENT)
20469 + /* Not found, so it must be a hole */
20470 + memset(addr, 0, UBIFS_BLOCK_SIZE);
20471 + return err;
20472 + }
20473 +
20474 + ubifs_assert(dn->ch.sqnum > ubifs_inode(inode)->creat_sqnum);
20475 +
20476 + len = le32_to_cpu(dn->size);
20477 + if (len <= 0 || len > UBIFS_BLOCK_SIZE)
20478 + goto dump;
20479 +
20480 + dlen = le32_to_cpu(dn->ch.len) - UBIFS_DATA_NODE_SZ;
20481 + out_len = UBIFS_BLOCK_SIZE;
20482 + err = ubifs_decompress(&dn->data, dlen, addr, &out_len,
20483 + le16_to_cpu(dn->compr_type));
20484 + if (err || len != out_len)
20485 + goto dump;
20486 +
20487 + /*
20488 + * Data length can be less than a full block, even for blocks that are
20489 + * not the last in the file (e.g., as a result of making a hole and
20490 + * appending data). Ensure that the remainder is zeroed out.
20491 + */
20492 + if (len < UBIFS_BLOCK_SIZE)
20493 + memset(addr + len, 0, UBIFS_BLOCK_SIZE - len);
20494 +
20495 + return 0;
20496 +
20497 +dump:
20498 + ubifs_err("bad data node (block %u, inode %lu)",
20499 + block, inode->i_ino);
20500 + dbg_dump_node(c, dn);
20501 + return -EINVAL;
20502 +}
20503 +
20504 +static int do_readpage(struct page *page)
20505 +{
20506 + void *addr;
20507 + int err = 0, i;
20508 + unsigned int block, beyond;
20509 + struct ubifs_data_node *dn;
20510 + struct inode *inode = page->mapping->host;
20511 + loff_t i_size = i_size_read(inode);
20512 +
20513 + dbg_gen("ino %lu, pg %lu, i_size %lld, flags %#lx",
20514 + inode->i_ino, page->index, i_size, page->flags);
20515 + ubifs_assert(!PageChecked(page));
20516 + ubifs_assert(!PagePrivate(page));
20517 +
20518 + addr = kmap(page);
20519 +
20520 + block = page->index << UBIFS_BLOCKS_PER_PAGE_SHIFT;
20521 + beyond = (i_size + UBIFS_BLOCK_SIZE - 1) >> UBIFS_BLOCK_SHIFT;
20522 + if (block >= beyond) {
20523 + /* Reading beyond inode */
20524 + SetPageChecked(page);
20525 + memset(addr, 0, PAGE_CACHE_SIZE);
20526 + goto out;
20527 + }
20528 +
20529 + dn = kmalloc(UBIFS_MAX_DATA_NODE_SZ, GFP_NOFS);
20530 + if (!dn) {
20531 + err = -ENOMEM;
20532 + goto error;
20533 + }
20534 +
20535 + i = 0;
20536 + while (1) {
20537 + int ret;
20538 +
20539 + if (block >= beyond) {
20540 + /* Reading beyond inode */
20541 + err = -ENOENT;
20542 + memset(addr, 0, UBIFS_BLOCK_SIZE);
20543 + } else {
20544 + ret = read_block(inode, addr, block, dn);
20545 + if (ret) {
20546 + err = ret;
20547 + if (err != -ENOENT)
20548 + break;
20549 + }
20550 + }
20551 + if (++i >= UBIFS_BLOCKS_PER_PAGE)
20552 + break;
20553 + block += 1;
20554 + addr += UBIFS_BLOCK_SIZE;
20555 + }
20556 + if (err) {
20557 + if (err == -ENOENT) {
20558 + /* Not found, so it must be a hole */
20559 + SetPageChecked(page);
20560 + dbg_gen("hole");
20561 + goto out_free;
20562 + }
20563 + ubifs_err("cannot read page %lu of inode %lu, error %d",
20564 + page->index, inode->i_ino, err);
20565 + goto error;
20566 + }
20567 +
20568 +out_free:
20569 + kfree(dn);
20570 +out:
20571 + SetPageUptodate(page);
20572 + ClearPageError(page);
20573 + flush_dcache_page(page);
20574 + kunmap(page);
20575 + return 0;
20576 +
20577 +error:
20578 + kfree(dn);
20579 + ClearPageUptodate(page);
20580 + SetPageError(page);
20581 + flush_dcache_page(page);
20582 + kunmap(page);
20583 + return err;
20584 +}
20585 +
20586 +static int ubifs_write_begin(struct file *file, struct address_space *mapping,
20587 + loff_t pos, unsigned len, unsigned flags,
20588 + struct page **pagep, void **fsdata)
20589 +{
20590 + struct inode *inode = mapping->host;
20591 + struct ubifs_info *c = inode->i_sb->s_fs_info;
20592 + pgoff_t index = pos >> PAGE_CACHE_SHIFT;
20593 + struct ubifs_budget_req req = { .new_page = 1 };
20594 + loff_t i_size = i_size_read(inode);
20595 + int uninitialized_var(err);
20596 + struct page *page;
20597 +
20598 + ubifs_assert(!(inode->i_sb->s_flags & MS_RDONLY));
20599 +
20600 + if (unlikely(c->ro_media))
20601 + return -EROFS;
20602 +
20603 + /*
20604 + * We are about to have a page of data written and we have to budget for
20605 + * this. The very important point here is that we have to budget before
20606 + * locking the page, because budgeting may force write-back, which
20607 + * would wait on locked pages and deadlock if we had the page locked.
20608 + *
20609 + * At this point we do not know anything about the page of data we are
20610 + * going to change, so assume the biggest budget (i.e., assume that
20611 + * this is a new page of data and it does not override an older page of
20612 + * data in the inode). Later the budget will be amended if this is not
20613 + * true.
20614 + */
20615 + if (pos + len > i_size)
20616 + /*
20617 + * We are writing beyond the file which means we are going to
20618 + * change inode size and make the inode dirty. And in turn,
20619 + * this means we have to budget for making the inode dirty.
20620 + *
20621 + * Note, if the inode is already dirty,
20622 + * 'ubifs_budget_inode_op()' will not allocate any budget,
20623 + * but will just lock the @budg_mutex of the inode to prevent
20624 + * it from becoming clean before we have changed its size,
20625 + * which is going to happen in 'ubifs_write_end()'.
20626 + */
20627 + err = ubifs_budget_inode_op(c, inode, &req);
20628 + else
20629 + /*
20630 + * The inode is not going to be marked as dirty by this write
20631 + * operation, do not budget for this.
20632 + */
20633 + err = ubifs_budget_space(c, &req);
20634 + if (unlikely(err))
20635 + return err;
20636 +
20637 + page = __grab_cache_page(mapping, index);
20638 + if (unlikely(!page)) {
20639 + err = -ENOMEM;
20640 + goto out_release;
20641 + }
20642 +
20643 + if (!PageUptodate(page)) {
20644 + /*
20645 + * The page is not loaded from the flash and has to be loaded
20646 + * unless we are writing all of it.
20647 + */
20648 + if (!(pos & PAGE_CACHE_MASK) && len == PAGE_CACHE_SIZE)
20649 + /*
20650 + * Set the PG_checked flag to make the further code
20651 + * assume the page is new.
20652 + */
20653 + SetPageChecked(page);
20654 + else {
20655 + err = do_readpage(page);
20656 + if (err)
20657 + goto out_unlock;
20658 + }
20659 +
20660 + SetPageUptodate(page);
20661 + ClearPageError(page);
20662 + }
20663 +
20664 + if (PagePrivate(page))
20665 + /*
20666 + * The page is dirty, which means it was budgeted twice:
20667 + * o first time the budget was allocated by the task which
20668 + * made the page dirty and set the PG_private flag;
20669 + * o and then we budgeted for it for the second time at the
20670 + * very beginning of this function.
20671 + *
20672 + * So what we have to do is to release the page budget we
20673 + * allocated.
20674 + *
20675 + * Note, the page write operation may change the inode length,
20676 + * which makes it dirty and means the budget should be
20677 + * allocated. This was done above in the "pos + len > i_size"
20678 + * case. If this was done, we do not free the the inode budget,
20679 + * because we cannot as we are really going to mark it dirty in
20680 + * the 'ubifs_write_end()' function.
20681 + */
20682 + ubifs_release_new_page_budget(c);
20683 + else if (!PageChecked(page))
20684 + /*
20685 + * The page is not new, which means we are changing the page
20686 + * which already exists on the media. This means that changing
20687 + * the page does not make the amount of indexing information
20688 + * larger, and this part of the budget which we have already
20689 + * acquired may be released.
20690 + */
20691 + ubifs_convert_page_budget(c);
20692 +
20693 + *pagep = page;
20694 + return 0;
20695 +
20696 +out_unlock:
20697 + unlock_page(page);
20698 + page_cache_release(page);
20699 +out_release:
20700 + if (pos + len > i_size)
20701 + ubifs_cancel_ino_op(c, inode, &req);
20702 + else
20703 + ubifs_release_budget(c, &req);
20704 + return err;
20705 +}
20706 +
20707 +static int ubifs_write_end(struct file *file, struct address_space *mapping,
20708 + loff_t pos, unsigned len, unsigned copied,
20709 + struct page *page, void *fsdata)
20710 +{
20711 + struct inode *inode = mapping->host;
20712 + struct ubifs_inode *ui = ubifs_inode(inode);
20713 + struct ubifs_info *c = inode->i_sb->s_fs_info;
20714 + loff_t i_size = i_size_read(inode);
20715 +
20716 + dbg_gen("ino %lu, pos %llu, pg %lu, len %u, copied %d, i_size %lld",
20717 + inode->i_ino, pos, page->index, len, copied, i_size);
20718 +
20719 + if (unlikely(copied < len && len == PAGE_CACHE_SIZE)) {
20720 + /*
20721 + * VFS copied less data to the page that it intended and
20722 + * declared in its '->write_begin()' call via the @len
20723 + * argument. If the page was not up-to-date, and @len was
20724 + * @PAGE_CACHE_SIZE, the 'ubifs_write_begin()' function did
20725 + * not load it from the media (for optimization reasons). This
20726 + * means that part of the page contains garbage. So read the
20727 + * page now.
20728 + */
20729 + dbg_gen("copied %d instead of %d, read page and repeat",
20730 + copied, len);
20731 +
20732 + if (pos + len > i_size)
20733 + /* See a comment below about this hacky unlock */
20734 + mutex_unlock(&ui->budg_mutex);
20735 +
20736 + copied = do_readpage(page);
20737 +
20738 + /*
20739 + * Return 0 to force VFS to repeat the whole operation, or the
20740 + * error code if 'do_readpage()' failed.
20741 + */
20742 + goto out;
20743 + }
20744 +
20745 + if (!PagePrivate(page)) {
20746 + SetPagePrivate(page);
20747 + atomic_long_inc(&c->dirty_pg_cnt);
20748 + __set_page_dirty_nobuffers(page);
20749 + }
20750 +
20751 + if (pos + len > i_size) {
20752 + i_size_write(inode, pos + len);
20753 +
20754 + /*
20755 + * Note, we do not set @I_DIRTY_PAGES (which means that the
20756 + * inode has dirty pages), this has been done in
20757 + * '__set_page_dirty_nobuffers()'.
20758 + */
20759 + mark_inode_dirty_sync(inode);
20760 +
20761 + /*
20762 + * The inode has been marked dirty, unlock it. This is a bit
20763 + * hacky because normally we would have to call
20764 + * 'ubifs_release_ino_dirty()'. But we know there is nothing
20765 + * to release because page's budget will be released in
20766 + * 'ubifs_write_page()' and inode's budget will be released in
20767 + * 'ubifs_write_inode()', so just unlock the inode here for
20768 + * optimization.
20769 + */
20770 + mutex_unlock(&ui->budg_mutex);
20771 + }
20772 +
20773 +out:
20774 + unlock_page(page);
20775 + page_cache_release(page);
20776 + return copied;
20777 +}
20778 +
20779 +static int ubifs_readpage(struct file *file, struct page *page)
20780 +{
20781 + do_readpage(page);
20782 + unlock_page(page);
20783 + return 0;
20784 +}
20785 +
20786 +/**
20787 + * release_existing_page_budget - release budget of an existing page.
20788 + * @c: UBIFS file-system description object
20789 + *
20790 + * This is a helper function which releases budget corresponding to the budget
20791 + * of changing one one page of data which already exists on the flash media.
20792 + *
20793 + * This function was not moved to "budget.c" because there is only one user.
20794 + */
20795 +static void release_existing_page_budget(struct ubifs_info *c)
20796 +{
20797 + struct ubifs_budget_req req = { .dd_growth = c->page_budget};
20798 +
20799 + ubifs_release_budget(c, &req);
20800 +}
20801 +
20802 +static int do_writepage(struct page *page, int len)
20803 +{
20804 + int err = 0, i, blen;
20805 + unsigned int block;
20806 + void *addr;
20807 + union ubifs_key key;
20808 + struct inode *inode = page->mapping->host;
20809 + struct ubifs_info *c = inode->i_sb->s_fs_info;
20810 +
20811 + /* Update radix tree tags */
20812 + set_page_writeback(page);
20813 +
20814 + addr = kmap(page);
20815 +
20816 + block = page->index << UBIFS_BLOCKS_PER_PAGE_SHIFT;
20817 + i = 0;
20818 + while (len) {
20819 + blen = min_t(int, len, UBIFS_BLOCK_SIZE);
20820 + data_key_init(c, &key, inode->i_ino, block);
20821 + err = ubifs_jnl_write_data(c, inode, &key, addr, blen);
20822 + if (err)
20823 + break;
20824 + if (++i >= UBIFS_BLOCKS_PER_PAGE)
20825 + break;
20826 + block += 1;
20827 + addr += blen;
20828 + len -= blen;
20829 + }
20830 + if (err) {
20831 + SetPageError(page);
20832 + ubifs_err("cannot write page %lu of inode %lu, error %d",
20833 + page->index, inode->i_ino, err);
20834 + ubifs_ro_mode(c, err);
20835 + }
20836 +
20837 + ubifs_assert(PagePrivate(page));
20838 + if (PageChecked(page))
20839 + ubifs_release_new_page_budget(c);
20840 + else
20841 + release_existing_page_budget(c);
20842 +
20843 + atomic_long_dec(&c->dirty_pg_cnt);
20844 + ClearPagePrivate(page);
20845 + ClearPageChecked(page);
20846 +
20847 + kunmap(page);
20848 + unlock_page(page);
20849 + end_page_writeback(page);
20850 +
20851 + return err;
20852 +}
20853 +
20854 +static int ubifs_writepage(struct page *page, struct writeback_control *wbc)
20855 +{
20856 + struct inode *inode = page->mapping->host;
20857 + loff_t i_size = i_size_read(inode);
20858 + pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT;
20859 + int len;
20860 + void *kaddr;
20861 +
20862 + dbg_gen("ino %lu, pg %lu, pg flags %#lx",
20863 + inode->i_ino, page->index, page->flags);
20864 + ubifs_assert(PagePrivate(page));
20865 +
20866 + /* Is the page fully inside i_size? */
20867 + if (page->index < end_index)
20868 + return do_writepage(page, PAGE_CACHE_SIZE);
20869 +
20870 + /* Is the page fully outside i_size? (truncate in progress) */
20871 + len = i_size & (PAGE_CACHE_SIZE - 1);
20872 + if (page->index >= end_index + 1 || !len) {
20873 + unlock_page(page);
20874 + return 0;
20875 + }
20876 +
20877 + /*
20878 + * The page straddles i_size. It must be zeroed out on each and every
20879 + * writepage invocation because it may be mmapped. "A file is mapped
20880 + * in multiples of the page size. For a file that is not a multiple of
20881 + * the page size, the remaining memory is zeroed when mapped, and
20882 + * writes to that region are not written out to the file."
20883 + */
20884 + kaddr = kmap_atomic(page, KM_USER0);
20885 + memset(kaddr + len, 0, PAGE_CACHE_SIZE - len);
20886 + flush_dcache_page(page);
20887 + kunmap_atomic(kaddr, KM_USER0);
20888 +
20889 + return do_writepage(page, len);
20890 +}
20891 +
20892 +static int ubifs_trunc(struct inode *inode, loff_t new_size)
20893 +{
20894 + loff_t old_size;
20895 + int err;
20896 +
20897 + dbg_gen("ino %lu, size %lld -> %lld",
20898 + inode->i_ino, inode->i_size, new_size);
20899 + old_size = inode->i_size;
20900 +
20901 + err = vmtruncate(inode, new_size);
20902 + if (err)
20903 + return err;
20904 +
20905 + if (new_size < old_size) {
20906 + struct ubifs_info *c = inode->i_sb->s_fs_info;
20907 + int offset = new_size & (UBIFS_BLOCK_SIZE - 1);
20908 +
20909 + if (offset) {
20910 + pgoff_t index = new_size >> PAGE_CACHE_SHIFT;
20911 + struct page *page;
20912 +
20913 + page = find_lock_page(inode->i_mapping, index);
20914 + if (page) {
20915 + if (PageDirty(page)) {
20916 + ubifs_assert(PagePrivate(page));
20917 +
20918 + clear_page_dirty_for_io(page);
20919 + if (UBIFS_BLOCKS_PER_PAGE_SHIFT)
20920 + offset = new_size &
20921 + (PAGE_CACHE_SIZE - 1);
20922 + err = do_writepage(page, offset);
20923 + page_cache_release(page);
20924 + if (err)
20925 + return err;
20926 + /*
20927 + * We could now tell ubifs_jnl_truncate
20928 + * not to read the last block.
20929 + */
20930 + } else {
20931 + /*
20932 + * We could 'kmap()' the page and
20933 + * pass the data to ubifs_jnl_truncate
20934 + * to save it from having to read it.
20935 + */
20936 + unlock_page(page);
20937 + page_cache_release(page);
20938 + }
20939 + }
20940 + }
20941 + err = ubifs_jnl_truncate(c, inode->i_ino, old_size, new_size);
20942 + if (err)
20943 + return err;
20944 + }
20945 +
20946 + return 0;
20947 +}
20948 +
20949 +int ubifs_setattr(struct dentry *dentry, struct iattr *attr)
20950 +{
20951 + unsigned int ia_valid = attr->ia_valid;
20952 + struct inode *inode = dentry->d_inode;
20953 + struct ubifs_info *c = inode->i_sb->s_fs_info;
20954 + struct ubifs_budget_req req;
20955 + int truncation, err = 0;
20956 +
20957 + dbg_gen("ino %lu, ia_valid %#x", inode->i_ino, ia_valid);
20958 + err = inode_change_ok(inode, attr);
20959 + if (err)
20960 + return err;
20961 +
20962 + memset(&req, 0, sizeof(struct ubifs_budget_req));
20963 +
20964 + /*
20965 + * If this is truncation, and we do not truncate on a block boundary,
20966 + * budget for changing one data block, because the last block will be
20967 + * re-written.
20968 + */
20969 + truncation = (ia_valid & ATTR_SIZE) && attr->ia_size != inode->i_size;
20970 + if (truncation && attr->ia_size < inode->i_size &&
20971 + (attr->ia_size & (UBIFS_BLOCK_SIZE - 1)))
20972 + req.dirtied_page = 1;
20973 +
20974 + err = ubifs_budget_inode_op(c, inode, &req);
20975 + if (err)
20976 + return err;
20977 +
20978 + if (truncation) {
20979 + err = ubifs_trunc(inode, attr->ia_size);
20980 + if (err) {
20981 + ubifs_cancel_ino_op(c, inode, &req);
20982 + return err;
20983 + }
20984 +
20985 + inode->i_mtime = inode->i_ctime = ubifs_current_time(inode);
20986 + }
20987 +
20988 + if (ia_valid & ATTR_UID)
20989 + inode->i_uid = attr->ia_uid;
20990 + if (ia_valid & ATTR_GID)
20991 + inode->i_gid = attr->ia_gid;
20992 + if (ia_valid & ATTR_ATIME)
20993 + inode->i_atime = timespec_trunc(attr->ia_atime,
20994 + inode->i_sb->s_time_gran);
20995 + if (ia_valid & ATTR_MTIME)
20996 + inode->i_mtime = timespec_trunc(attr->ia_mtime,
20997 + inode->i_sb->s_time_gran);
20998 + if (ia_valid & ATTR_CTIME)
20999 + inode->i_ctime = timespec_trunc(attr->ia_ctime,
21000 + inode->i_sb->s_time_gran);
21001 + if (ia_valid & ATTR_MODE) {
21002 + umode_t mode = attr->ia_mode;
21003 +
21004 + if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
21005 + mode &= ~S_ISGID;
21006 + inode->i_mode = mode;
21007 + }
21008 +
21009 + mark_inode_dirty_sync(inode);
21010 + ubifs_release_ino_dirty(c, inode, &req);
21011 +
21012 + if (req.dirtied_page) {
21013 + /*
21014 + * Truncation code does not make the reenacted page dirty, it
21015 + * just changes it on journal level, so we have to release page
21016 + * change budget.
21017 + */
21018 + memset(&req, 0, sizeof(struct ubifs_budget_req));
21019 + req.dd_growth = c->page_budget;
21020 + ubifs_release_budget(c, &req);
21021 + }
21022 +
21023 + if (IS_SYNC(inode))
21024 + err = write_inode_now(inode, 1);
21025 +
21026 + return err;
21027 +}
21028 +
21029 +static void ubifs_invalidatepage(struct page *page, unsigned long offset)
21030 +{
21031 + struct inode *inode = page->mapping->host;
21032 + struct ubifs_info *c = inode->i_sb->s_fs_info;
21033 + struct ubifs_budget_req req;
21034 +
21035 + ubifs_assert(PagePrivate(page));
21036 + if (offset)
21037 + /* Partial page remains dirty */
21038 + return;
21039 +
21040 + memset(&req, 0, sizeof(struct ubifs_budget_req));
21041 + if (PageChecked(page)) {
21042 + req.new_page = 1;
21043 + req.idx_growth = -1;
21044 + req.data_growth = c->page_budget;
21045 + } else
21046 + req.dd_growth = c->page_budget;
21047 + ubifs_release_budget(c, &req);
21048 +
21049 + atomic_long_dec(&c->dirty_pg_cnt);
21050 + ClearPagePrivate(page);
21051 + ClearPageChecked(page);
21052 +}
21053 +
21054 +static void *ubifs_follow_link(struct dentry *dentry, struct nameidata *nd)
21055 +{
21056 + struct ubifs_inode *ui = ubifs_inode(dentry->d_inode);
21057 +
21058 + nd_set_link(nd, ui->data);
21059 + return NULL;
21060 +}
21061 +
21062 +int ubifs_fsync(struct file *file, struct dentry *dentry, int datasync)
21063 +{
21064 + struct inode *inode = dentry->d_inode;
21065 + struct ubifs_info *c = inode->i_sb->s_fs_info;
21066 + int err;
21067 +
21068 + dbg_gen("syncing inode %lu", inode->i_ino);
21069 +
21070 + /* Synchronize the inode and dirty pages */
21071 + err = write_inode_now(inode, 1);
21072 + if (err)
21073 + return err;
21074 +
21075 + /*
21076 + * Some data related to this inode may still sit in a write-buffer.
21077 + * Flush them.
21078 + */
21079 + err = ubifs_sync_wbufs_by_inodes(c, &inode, 1);
21080 + if (err)
21081 + return err;
21082 +
21083 + return 0;
21084 +}
21085 +
21086 +/**
21087 + * mctime_update_needed - check if mtime or ctime update is needed.
21088 + * @inode: the inode to do the check for
21089 + * @now: current time
21090 + *
21091 + * This helper function checks if the inode mtime/ctime should be updated or
21092 + * not. If current values of the time-stamps are within the UBIFS inode time
21093 + * granularity, they are not updated. This is an optimization.
21094 + */
21095 +static inline int mctime_update_needed(const struct inode *inode,
21096 + const struct timespec *now)
21097 +{
21098 + if (!timespec_equal(&inode->i_mtime, now) ||
21099 + !timespec_equal(&inode->i_ctime, now))
21100 + return 1;
21101 + return 0;
21102 +}
21103 +
21104 +/**
21105 + * update_ctime - update mtime and ctime of an inode.
21106 + * @c: UBIFS file-system description object
21107 + * @inode: inode to update
21108 + *
21109 + * This function updates mtime and ctime of the inode if it is not equivalent to
21110 + * current time. Returns zero in case of success and a negative error code in
21111 + * case of failure.
21112 + */
21113 +static int update_mctime(struct ubifs_info *c, struct inode *inode)
21114 +{
21115 + struct timespec now = ubifs_current_time(inode);
21116 +
21117 + if (mctime_update_needed(inode, &now)) {
21118 + struct ubifs_budget_req req;
21119 + int err;
21120 +
21121 + memset(&req, 0, sizeof(struct ubifs_budget_req));
21122 + err = ubifs_budget_inode_op(c, inode, &req);
21123 + if (err)
21124 + return err;
21125 +
21126 + inode->i_mtime = inode->i_ctime = now;
21127 + mark_inode_dirty_sync(inode);
21128 + mutex_unlock(&ubifs_inode(inode)->budg_mutex);
21129 + }
21130 +
21131 + return 0;
21132 +}
21133 +
21134 +static ssize_t ubifs_write(struct file *file, const char __user *buf,
21135 + size_t len, loff_t *ppos)
21136 +{
21137 + int err;
21138 + ssize_t ret;
21139 + struct inode *inode = file->f_mapping->host;
21140 + struct ubifs_info *c = inode->i_sb->s_fs_info;
21141 +
21142 + err = update_mctime(c, inode);
21143 + if (err)
21144 + return err;
21145 +
21146 + ret = do_sync_write(file, buf, len, ppos);
21147 + if (ret < 0)
21148 + return ret;
21149 +
21150 + if (ret > 0 && IS_SYNC(inode)) {
21151 + err = ubifs_sync_wbufs_by_inodes(c, &inode, 1);
21152 + if (err)
21153 + return err;
21154 + }
21155 +
21156 + return ret;
21157 +}
21158 +
21159 +static ssize_t ubifs_aio_write(struct kiocb *iocb, const struct iovec *iov,
21160 + unsigned long nr_segs, loff_t pos)
21161 +{
21162 + int err;
21163 + ssize_t ret;
21164 + struct inode *inode = iocb->ki_filp->f_mapping->host;
21165 + struct ubifs_info *c = inode->i_sb->s_fs_info;
21166 +
21167 + err = update_mctime(c, inode);
21168 + if (err)
21169 + return err;
21170 +
21171 + ret = generic_file_aio_write(iocb, iov, nr_segs, pos);
21172 + if (ret < 0)
21173 + return ret;
21174 +
21175 + if (ret > 0 && IS_SYNC(inode)) {
21176 + err = ubifs_sync_wbufs_by_inodes(c, &inode, 1);
21177 + if (err)
21178 + return err;
21179 + }
21180 +
21181 + return ret;
21182 +}
21183 +
21184 +static int ubifs_set_page_dirty(struct page *page)
21185 +{
21186 + int ret;
21187 +
21188 + ret = __set_page_dirty_nobuffers(page);
21189 + /*
21190 + * An attempt to dirty a page without budgeting for it - should not
21191 + * happen.
21192 + */
21193 + ubifs_assert(ret == 0);
21194 + return ret;
21195 +}
21196 +
21197 +static int ubifs_releasepage(struct page *page, gfp_t unused_gfp_flags)
21198 +{
21199 + /*
21200 + * An attempt to release a dirty page without budgeting for it - should
21201 + * not happen.
21202 + */
21203 + if (PageWriteback(page))
21204 + return 0;
21205 + ubifs_assert(PagePrivate(page));
21206 + ubifs_assert(0);
21207 + ClearPagePrivate(page);
21208 + ClearPageChecked(page);
21209 + return 1;
21210 +}
21211 +
21212 +/*
21213 + * mmap()d file has taken write protection fault and is being made
21214 + * writable. UBIFS must ensure page is budgeted for.
21215 + */
21216 +static int ubifs_vm_page_mkwrite(struct vm_area_struct *vma, struct page *page)
21217 +{
21218 + struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
21219 + struct ubifs_info *c = inode->i_sb->s_fs_info;
21220 + struct timespec now = ubifs_current_time(inode);
21221 + struct ubifs_budget_req req = { .new_page = 1 };
21222 + int err, update_time;
21223 +
21224 + dbg_gen("ino %lu, pg %lu, i_size %lld", inode->i_ino, page->index,
21225 + i_size_read(inode));
21226 + ubifs_assert(!(inode->i_sb->s_flags & MS_RDONLY));
21227 +
21228 + if (unlikely(c->ro_media))
21229 + return -EROFS;
21230 +
21231 + /*
21232 + * We have not locked @page so far so we may budget for changing the
21233 + * page. Note, we cannot do this after we locked the page, because
21234 + * budgeting may cause write-back which would cause deadlock.
21235 + *
21236 + * At the moment we do not know whether the page is dirty or not, so we
21237 + * assume that it is not and budget for a new page. We could look at
21238 + * the @PG_private flag and figure this out, but we may race with write
21239 + * back and the page state may change by the time we lock it, so this
21240 + * would need additional care. We do not bother with this at the
21241 + * moment, although it might be good idea to do. Instead, we allocate
21242 + * budget for a new page and amend it later on if the page was in fact
21243 + * dirty.
21244 + *
21245 + * The budgeting-related logic of this function is similar to what we
21246 + * do in 'ubifs_write_begin()' and 'ubifs_write_end()'. Glance there
21247 + * for more comments.
21248 + */
21249 + if (mctime_update_needed(inode, &now)) {
21250 + /*
21251 + * We have to change inode time stamp which requires extra
21252 + * budgeting.
21253 + */
21254 + update_time = 1;
21255 + err = ubifs_budget_inode_op(c, inode, &req);
21256 + } else {
21257 + update_time = 0;
21258 + err = ubifs_budget_space(c, &req);
21259 + }
21260 + if (unlikely(err)) {
21261 + if (err == -ENOSPC)
21262 + ubifs_warn("out of space for mmapped file "
21263 + "(inode number %lu)", inode->i_ino);
21264 + return err;
21265 + }
21266 +
21267 + lock_page(page);
21268 + if (unlikely(page->mapping != inode->i_mapping ||
21269 + page_offset(page) > i_size_read(inode))) {
21270 + /* Page got truncated out from underneath us */
21271 + err = -EINVAL;
21272 + goto out_unlock;
21273 + }
21274 +
21275 + if (PagePrivate(page))
21276 + ubifs_release_new_page_budget(c);
21277 + else {
21278 + if (!PageChecked(page))
21279 + ubifs_convert_page_budget(c);
21280 + SetPagePrivate(page);
21281 + atomic_long_inc(&c->dirty_pg_cnt);
21282 + __set_page_dirty_nobuffers(page);
21283 + }
21284 +
21285 + if (update_time) {
21286 + inode->i_mtime = inode->i_ctime = now;
21287 + mark_inode_dirty_sync(inode);
21288 + mutex_unlock(&ubifs_inode(inode)->budg_mutex);
21289 + }
21290 +
21291 + unlock_page(page);
21292 + return 0;
21293 +
21294 +out_unlock:
21295 + unlock_page(page);
21296 + if (update_time)
21297 + ubifs_cancel_ino_op(c, inode, &req);
21298 + else
21299 + ubifs_release_budget(c, &req);
21300 + return err;
21301 +}
21302 +
21303 +struct vm_operations_struct ubifs_file_vm_ops = {
21304 + .fault = filemap_fault,
21305 + .page_mkwrite = ubifs_vm_page_mkwrite,
21306 +};
21307 +
21308 +static int ubifs_file_mmap(struct file *file, struct vm_area_struct *vma)
21309 +{
21310 + int err;
21311 +
21312 + /* 'generic_file_mmap()' takes care of NOMMU case */
21313 + err = generic_file_mmap(file, vma);
21314 + if (err)
21315 + return err;
21316 + vma->vm_ops = &ubifs_file_vm_ops;
21317 + return 0;
21318 +}
21319 +
21320 +struct address_space_operations ubifs_file_address_operations = {
21321 + .readpage = ubifs_readpage,
21322 + .writepage = ubifs_writepage,
21323 + .write_begin = ubifs_write_begin,
21324 + .write_end = ubifs_write_end,
21325 + .invalidatepage = ubifs_invalidatepage,
21326 + .set_page_dirty = ubifs_set_page_dirty,
21327 + .releasepage = ubifs_releasepage,
21328 +};
21329 +
21330 +struct inode_operations ubifs_file_inode_operations = {
21331 + .setattr = ubifs_setattr,
21332 + .getattr = ubifs_getattr,
21333 +#ifdef CONFIG_UBIFS_FS_XATTR
21334 + .setxattr = ubifs_setxattr,
21335 + .getxattr = ubifs_getxattr,
21336 + .listxattr = ubifs_listxattr,
21337 + .removexattr = ubifs_removexattr,
21338 +#endif
21339 +};
21340 +
21341 +struct inode_operations ubifs_symlink_inode_operations = {
21342 + .readlink = generic_readlink,
21343 + .follow_link = ubifs_follow_link,
21344 + .setattr = ubifs_setattr,
21345 + .getattr = ubifs_getattr,
21346 +};
21347 +
21348 +struct file_operations ubifs_file_operations = {
21349 + .llseek = generic_file_llseek,
21350 + .read = do_sync_read,
21351 + .write = ubifs_write,
21352 + .aio_read = generic_file_aio_read,
21353 + .aio_write = ubifs_aio_write,
21354 + .mmap = ubifs_file_mmap,
21355 + .fsync = ubifs_fsync,
21356 + .unlocked_ioctl = ubifs_ioctl,
21357 +#ifdef CONFIG_COMPAT
21358 + .compat_ioctl = ubifs_compat_ioctl,
21359 +#endif
21360 +};
21361 diff --exclude=.git -urN linux-2.6.25.6/fs/ubifs/find.c avr32-2.6/fs/ubifs/find.c
21362 --- linux-2.6.25.6/fs/ubifs/find.c 1970-01-01 01:00:00.000000000 +0100
21363 +++ avr32-2.6/fs/ubifs/find.c 2008-06-12 15:09:45.364399968 +0200
21364 @@ -0,0 +1,977 @@
21365 +/*
21366 + * This file is part of UBIFS.
21367 + *
21368 + * Copyright (C) 2006-2008 Nokia Corporation.
21369 + *
21370 + * This program is free software; you can redistribute it and/or modify it
21371 + * under the terms of the GNU General Public License version 2 as published by
21372 + * the Free Software Foundation.
21373 + *
21374 + * This program is distributed in the hope that it will be useful, but WITHOUT
21375 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21376 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
21377 + * more details.
21378 + *
21379 + * You should have received a copy of the GNU General Public License along with
21380 + * this program; if not, write to the Free Software Foundation, Inc., 51
21381 + * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21382 + *
21383 + * Authors: Artem Bityutskiy (Битюцкий Артём)
21384 + * Adrian Hunter
21385 + */
21386 +
21387 +/*
21388 + * This file contains functions for finding LEBs for various purposes e.g.
21389 + * garbage collection. In general, lprops category heaps and lists are used
21390 + * for fast access, falling back on scanning the LPT as a last resort.
21391 + */
21392 +
21393 +#include <linux/sort.h>
21394 +#include "ubifs.h"
21395 +
21396 +/**
21397 + * struct scan_data - data provided to scan callback functions
21398 + * @min_space: minimum number of bytes for which to scan
21399 + * @pick_free: whether it is OK to scan for empty LEBs
21400 + * @lnum: LEB number found is returned here
21401 + * @exclude_index: whether to exclude index LEBs
21402 + */
21403 +struct scan_data {
21404 + int min_space;
21405 + int pick_free;
21406 + int lnum;
21407 + int exclude_index;
21408 +};
21409 +
21410 +/**
21411 + * valuable - determine whether LEB properties are valuable.
21412 + * @c: the UBIFS file-system description object
21413 + * @lprops: LEB properties
21414 + *
21415 + * This function return %1 if the LEB properties should be added to the LEB
21416 + * properties tree in memory. Otherwise %0 is returned.
21417 + */
21418 +static int valuable(struct ubifs_info *c, const struct ubifs_lprops *lprops)
21419 +{
21420 + int n, cat = lprops->flags & LPROPS_CAT_MASK;
21421 + struct ubifs_lpt_heap *heap;
21422 +
21423 + switch (cat) {
21424 + case LPROPS_DIRTY:
21425 + case LPROPS_DIRTY_IDX:
21426 + case LPROPS_FREE:
21427 + heap = &c->lpt_heap[cat - 1];
21428 + if (heap->cnt < heap->max_cnt)
21429 + return 1;
21430 + if (lprops->free + lprops->dirty >= c->dark_wm)
21431 + return 1;
21432 + return 0;
21433 + case LPROPS_EMPTY:
21434 + n = c->lst.empty_lebs + c->freeable_cnt -
21435 + c->lst.taken_empty_lebs;
21436 + if (n < c->lsave_cnt)
21437 + return 1;
21438 + return 0;
21439 + case LPROPS_FREEABLE:
21440 + return 1;
21441 + case LPROPS_FRDI_IDX:
21442 + return 1;
21443 + }
21444 + return 0;
21445 +}
21446 +
21447 +/**
21448 + * scan_for_dirty_cb - dirty space scan callback.
21449 + * @c: the UBIFS file-system description object
21450 + * @lprops: LEB properties to scan
21451 + * @in_tree: whether the LEB properties are in main memory
21452 + * @data: information passed to and from the caller of the scan
21453 + *
21454 + * This function returns a code that indicates whether the scan should continue
21455 + * (%LPT_SCAN_CONTINUE), whether the LEB properties should be added to the tree
21456 + * in main memory (%LPT_SCAN_ADD), or whether the scan should stop
21457 + * (%LPT_SCAN_STOP).
21458 + */
21459 +static int scan_for_dirty_cb(struct ubifs_info *c,
21460 + const struct ubifs_lprops *lprops, int in_tree,
21461 + struct scan_data *data)
21462 +{
21463 + int ret = LPT_SCAN_CONTINUE;
21464 +
21465 + /* Exclude LEBs that are currently in use */
21466 + if (lprops->flags & LPROPS_TAKEN)
21467 + return LPT_SCAN_CONTINUE;
21468 + /* Determine whether to add these LEB properties to the tree */
21469 + if (!in_tree && valuable(c, lprops))
21470 + ret |= LPT_SCAN_ADD;
21471 + /* Exclude LEBs with too little space */
21472 + if (lprops->free + lprops->dirty < data->min_space)
21473 + return ret;
21474 + /* If specified, exclude index LEBs */
21475 + if (data->exclude_index && lprops->flags & LPROPS_INDEX)
21476 + return ret;
21477 + /* If specified, exclude empty or freeable LEBs */
21478 + if (lprops->free + lprops->dirty == c->leb_size) {
21479 + if (!data->pick_free)
21480 + return ret;
21481 + /* Exclude LEBs with too little dirty space (unless it is empty) */
21482 + } else if (lprops->dirty < c->dead_wm)
21483 + return ret;
21484 + /* Finally we found space */
21485 + data->lnum = lprops->lnum;
21486 + return LPT_SCAN_ADD | LPT_SCAN_STOP;
21487 +}
21488 +
21489 +/**
21490 + * scan_for_dirty - find a data LEB with free space.
21491 + * @c: the UBIFS file-system description object
21492 + * @min_space: minimum amount free plus dirty space the returned LEB has to
21493 + * have
21494 + * @pick_free: if it is OK to return a free or freeable LEB
21495 + * @exclude_index: whether to exclude index LEBs
21496 + *
21497 + * This function returns a pointer to the LEB properties found or a negative
21498 + * error code.
21499 + */
21500 +static const struct ubifs_lprops *scan_for_dirty(struct ubifs_info *c,
21501 + int min_space, int pick_free,
21502 + int exclude_index)
21503 +{
21504 + const struct ubifs_lprops *lprops;
21505 + struct ubifs_lpt_heap *heap;
21506 + struct scan_data data;
21507 + int err, i;
21508 +
21509 + /* There may be an LEB with enough dirty space on the free heap */
21510 + heap = &c->lpt_heap[LPROPS_FREE - 1];
21511 + for (i = 0; i < heap->cnt; i++) {
21512 + lprops = heap->arr[i];
21513 + if (lprops->free + lprops->dirty < min_space)
21514 + continue;
21515 + if (lprops->dirty < c->dead_wm)
21516 + continue;
21517 + return lprops;
21518 + }
21519 + /*
21520 + * A LEB may have fallen off of the bottom of the dirty heap, and ended
21521 + * up as uncategorized even though it has enough dirty space for us now,
21522 + * so check the uncategorized list. N.B. neither empty nor freeable LEBs
21523 + * can end up as uncategorized because they are kept on lists not
21524 + * finite-sized heaps.
21525 + */
21526 + list_for_each_entry(lprops, &c->uncat_list, list) {
21527 + if (lprops->flags & LPROPS_TAKEN)
21528 + continue;
21529 + if (lprops->free + lprops->dirty < min_space)
21530 + continue;
21531 + if (exclude_index && (lprops->flags & LPROPS_INDEX))
21532 + continue;
21533 + if (lprops->dirty < c->dead_wm)
21534 + continue;
21535 + return lprops;
21536 + }
21537 + /* We have looked everywhere in main memory, now scan the flash */
21538 + if (c->pnodes_have >= c->pnode_cnt)
21539 + /* All pnodes are in memory, so skip scan */
21540 + return ERR_PTR(-ENOSPC);
21541 + data.min_space = min_space;
21542 + data.pick_free = pick_free;
21543 + data.lnum = -1;
21544 + data.exclude_index = exclude_index;
21545 + err = ubifs_lpt_scan_nolock(c, -1, c->lscan_lnum,
21546 + (ubifs_lpt_scan_callback)scan_for_dirty_cb,
21547 + &data);
21548 + if (err)
21549 + return ERR_PTR(err);
21550 + ubifs_assert(data.lnum >= c->main_first && data.lnum < c->leb_cnt);
21551 + c->lscan_lnum = data.lnum;
21552 + lprops = ubifs_lpt_lookup_dirty(c, data.lnum);
21553 + if (IS_ERR(lprops))
21554 + return lprops;
21555 + ubifs_assert(lprops->lnum == data.lnum);
21556 + ubifs_assert(lprops->free + lprops->dirty >= min_space);
21557 + ubifs_assert(lprops->dirty >= c->dead_wm ||
21558 + (pick_free &&
21559 + lprops->free + lprops->dirty == c->leb_size));
21560 + ubifs_assert(!(lprops->flags & LPROPS_TAKEN));
21561 + ubifs_assert(!(lprops->flags & LPROPS_INDEX));
21562 + return lprops;
21563 +}
21564 +
21565 +/**
21566 + * ubifs_find_dirty_leb - find a dirty LEB for the Garbage Collector.
21567 + * @c: the UBIFS file-system description object
21568 + * @ret_lp: LEB properties are returned here on exit
21569 + * @min_space: minimum amount free plus dirty space the returned LEB has to
21570 + * have
21571 + * @pick_free: controls whether it is OK to pick empty or index LEBs
21572 + *
21573 + * This function tries to find a dirty logical eraseblock which has at least
21574 + * @min_space free and dirty space. It prefers to take an LEB from the dirty or
21575 + * dirty index heap, and it falls-back to LPT scanning if the heaps are empty
21576 + * or do not have an LEB which satisfies the @min_space criteria.
21577 + *
21578 + * Note:
21579 + * o LEBs which have less than dead watermark of dirty space are never picked
21580 + * by this function;
21581 + *
21582 + * Returns zero and the LEB properties of
21583 + * found dirty LEB in case of success, %-ENOSPC if no dirty LEB was found and a
21584 + * negative error code in case of other failures. The returned LEB is marked as
21585 + * "taken".
21586 + *
21587 + * The additional @pick_free argument controls if this function has to return a
21588 + * free or freeable LEB if one is present. For example, GC must to set it to %1,
21589 + * when called from the journal space reservation function, because the
21590 + * appearance of free space may coincide with the loss of enough dirty space
21591 + * for GC to succeed anyway.
21592 + *
21593 + * In contrast, if the Garbage Collector is called from budgeting, it should
21594 + * just make free space, not return LEBs which are already free or freeable.
21595 + *
21596 + * In addition @pick_free is set to %2 by the recovery process in order to
21597 + * recover gc_lnum in which case an index LEB must not be returned.
21598 + */
21599 +int ubifs_find_dirty_leb(struct ubifs_info *c, struct ubifs_lprops *ret_lp,
21600 + int min_space, int pick_free)
21601 +{
21602 + int err = 0, sum, exclude_index = pick_free == 2 ? 1 : 0;
21603 + const struct ubifs_lprops *lp = NULL, *idx_lp = NULL;
21604 + struct ubifs_lpt_heap *heap, *idx_heap;
21605 +
21606 + ubifs_get_lprops(c);
21607 +
21608 + if (pick_free) {
21609 + int lebs, rsvd_idx_lebs = 0;
21610 +
21611 + spin_lock(&c->space_lock);
21612 + lebs = c->lst.empty_lebs;
21613 + lebs += c->freeable_cnt - c->lst.taken_empty_lebs;
21614 +
21615 + /*
21616 + * Note, the index may consume more LEBs than have been reserved
21617 + * for it. It is OK because it might be consolidated by GC.
21618 + * But if the index takes fewer LEBs than it is reserved for it,
21619 + * this function must avoid picking those reserved LEBs.
21620 + */
21621 + if (c->min_idx_lebs >= c->lst.idx_lebs) {
21622 + rsvd_idx_lebs = c->min_idx_lebs - c->lst.idx_lebs;
21623 + exclude_index = 1;
21624 + }
21625 + spin_unlock(&c->space_lock);
21626 +
21627 + /* Check if there are enough free LEBs for the index */
21628 + if (rsvd_idx_lebs < lebs) {
21629 + /* OK, try to find an empty LEB */
21630 + lp = ubifs_fast_find_empty(c);
21631 + if (lp)
21632 + goto found;
21633 +
21634 + /* Or a freeable LEB */
21635 + lp = ubifs_fast_find_freeable(c);
21636 + if (lp)
21637 + goto found;
21638 + } else
21639 + /*
21640 + * We cannot pick free/freeable LEBs in the below code.
21641 + */
21642 + pick_free = 0;
21643 + } else {
21644 + spin_lock(&c->space_lock);
21645 + exclude_index = (c->min_idx_lebs >= c->lst.idx_lebs);
21646 + spin_unlock(&c->space_lock);
21647 + }
21648 +
21649 + /* Look on the dirty and dirty index heaps */
21650 + heap = &c->lpt_heap[LPROPS_DIRTY - 1];
21651 + idx_heap = &c->lpt_heap[LPROPS_DIRTY_IDX - 1];
21652 +
21653 + if (idx_heap->cnt && !exclude_index) {
21654 + idx_lp = idx_heap->arr[0];
21655 + sum = idx_lp->free + idx_lp->dirty;
21656 + /*
21657 + * Since we reserve twice as more space for the index than it
21658 + * actually takes, it does not make sense to pick indexing LEBs
21659 + * with less than half LEB of dirty space.
21660 + */
21661 + if (sum < min_space || sum < c->half_leb_size)
21662 + idx_lp = NULL;
21663 + }
21664 +
21665 + if (heap->cnt) {
21666 + lp = heap->arr[0];
21667 + if (lp->dirty + lp->free < min_space)
21668 + lp = NULL;
21669 + }
21670 +
21671 + /* Pick the LEB with most space */
21672 + if (idx_lp && lp) {
21673 + if (idx_lp->free + idx_lp->dirty >= lp->free + lp->dirty)
21674 + lp = idx_lp;
21675 + } else if (idx_lp && !lp)
21676 + lp = idx_lp;
21677 +
21678 + if (lp) {
21679 + ubifs_assert(lp->dirty >= c->dead_wm);
21680 + goto found;
21681 + }
21682 +
21683 + /* Did not find a dirty LEB on the dirty heaps, have to scan */
21684 + dbg_find("scanning LPT for a dirty LEB");
21685 + lp = scan_for_dirty(c, min_space, pick_free, exclude_index);
21686 + if (IS_ERR(lp)) {
21687 + err = PTR_ERR(lp);
21688 + goto out;
21689 + }
21690 + ubifs_assert(lp->dirty >= c->dead_wm ||
21691 + (pick_free && lp->free + lp->dirty == c->leb_size));
21692 +
21693 +found:
21694 + dbg_find("found LEB %d, free %d, dirty %d, flags %#x",
21695 + lp->lnum, lp->free, lp->dirty, lp->flags);
21696 +
21697 + lp = ubifs_change_lp(c, lp, LPROPS_NC, LPROPS_NC,
21698 + lp->flags | LPROPS_TAKEN, 0);
21699 + if (IS_ERR(lp)) {
21700 + err = PTR_ERR(lp);
21701 + goto out;
21702 + }
21703 +
21704 + memcpy(ret_lp, lp, sizeof(struct ubifs_lprops));
21705 +
21706 +out:
21707 + ubifs_release_lprops(c);
21708 + return err;
21709 +}
21710 +
21711 +/**
21712 + * scan_for_free_cb - free space scan callback.
21713 + * @c: the UBIFS file-system description object
21714 + * @lprops: LEB properties to scan
21715 + * @in_tree: whether the LEB properties are in main memory
21716 + * @data: information passed to and from the caller of the scan
21717 + *
21718 + * This function returns a code that indicates whether the scan should continue
21719 + * (%LPT_SCAN_CONTINUE), whether the LEB properties should be added to the tree
21720 + * in main memory (%LPT_SCAN_ADD), or whether the scan should stop
21721 + * (%LPT_SCAN_STOP).
21722 + */
21723 +static int scan_for_free_cb(struct ubifs_info *c,
21724 + const struct ubifs_lprops *lprops, int in_tree,
21725 + struct scan_data *data)
21726 +{
21727 + int ret = LPT_SCAN_CONTINUE;
21728 +
21729 + /* Exclude LEBs that are currently in use */
21730 + if (lprops->flags & LPROPS_TAKEN)
21731 + return LPT_SCAN_CONTINUE;
21732 + /* Determine whether to add these LEB properties to the tree */
21733 + if (!in_tree && valuable(c, lprops))
21734 + ret |= LPT_SCAN_ADD;
21735 + /* Exclude index LEBs */
21736 + if (lprops->flags & LPROPS_INDEX)
21737 + return ret;
21738 + /* Exclude LEBs with too little space */
21739 + if (lprops->free < data->min_space)
21740 + return ret;
21741 + /* If specified, exclude empty LEBs */
21742 + if (!data->pick_free && lprops->free == c->leb_size)
21743 + return ret;
21744 + /*
21745 + * LEBs that have only free and dirty space must not be allocated
21746 + * because they may have been unmapped already or they may have data
21747 + * that is obsolete only because of nodes that are still sitting in a
21748 + * wbuf.
21749 + */
21750 + if (lprops->free + lprops->dirty == c->leb_size && lprops->dirty > 0)
21751 + return ret;
21752 + /* Finally we found space */
21753 + data->lnum = lprops->lnum;
21754 + return LPT_SCAN_ADD | LPT_SCAN_STOP;
21755 +}
21756 +
21757 +/**
21758 + * do_find_free_space - find a data LEB with free space.
21759 + * @c: the UBIFS file-system description object
21760 + * @min_space: minimum amount of free space required
21761 + * @pick_free: whether it is OK to scan for empty LEBs
21762 + * @squeeze: whether to try to find space in a non-empty LEB first
21763 + *
21764 + * This function returns a pointer to the LEB properties found or a negative
21765 + * error code.
21766 + */
21767 +static
21768 +const struct ubifs_lprops *do_find_free_space(struct ubifs_info *c,
21769 + int min_space, int pick_free,
21770 + int squeeze)
21771 +{
21772 + const struct ubifs_lprops *lprops;
21773 + struct ubifs_lpt_heap *heap;
21774 + struct scan_data data;
21775 + int err, i;
21776 +
21777 + if (squeeze) {
21778 + lprops = ubifs_fast_find_free(c);
21779 + if (lprops && lprops->free >= min_space)
21780 + return lprops;
21781 + }
21782 + if (pick_free) {
21783 + lprops = ubifs_fast_find_empty(c);
21784 + if (lprops)
21785 + return lprops;
21786 + }
21787 + if (!squeeze) {
21788 + lprops = ubifs_fast_find_free(c);
21789 + if (lprops && lprops->free >= min_space)
21790 + return lprops;
21791 + }
21792 + /* There may be an LEB with enough free space on the dirty heap */
21793 + heap = &c->lpt_heap[LPROPS_DIRTY - 1];
21794 + for (i = 0; i < heap->cnt; i++) {
21795 + lprops = heap->arr[i];
21796 + if (lprops->free >= min_space)
21797 + return lprops;
21798 + }
21799 + /*
21800 + * A LEB may have fallen off of the bottom of the free heap, and ended
21801 + * up as uncategorized even though it has enough free space for us now,
21802 + * so check the uncategorized list. N.B. neither empty nor freeable LEBs
21803 + * can end up as uncategorized because they are kept on lists not
21804 + * finite-sized heaps.
21805 + */
21806 + list_for_each_entry(lprops, &c->uncat_list, list) {
21807 + if (lprops->flags & LPROPS_TAKEN)
21808 + continue;
21809 + if (lprops->flags & LPROPS_INDEX)
21810 + continue;
21811 + if (lprops->free >= min_space)
21812 + return lprops;
21813 + }
21814 + /* We have looked everywhere in main memory, now scan the flash */
21815 + if (c->pnodes_have >= c->pnode_cnt)
21816 + /* All pnodes are in memory, so skip scan */
21817 + return ERR_PTR(-ENOSPC);
21818 + data.min_space = min_space;
21819 + data.pick_free = pick_free;
21820 + data.lnum = -1;
21821 + err = ubifs_lpt_scan_nolock(c, -1, c->lscan_lnum,
21822 + (ubifs_lpt_scan_callback)scan_for_free_cb,
21823 + &data);
21824 + if (err)
21825 + return ERR_PTR(err);
21826 + ubifs_assert(data.lnum >= c->main_first && data.lnum < c->leb_cnt);
21827 + c->lscan_lnum = data.lnum;
21828 + lprops = ubifs_lpt_lookup_dirty(c, data.lnum);
21829 + if (IS_ERR(lprops))
21830 + return lprops;
21831 + ubifs_assert(lprops->lnum == data.lnum);
21832 + ubifs_assert(lprops->free >= min_space);
21833 + ubifs_assert(!(lprops->flags & LPROPS_TAKEN));
21834 + ubifs_assert(!(lprops->flags & LPROPS_INDEX));
21835 + return lprops;
21836 +}
21837 +
21838 +/**
21839 + * ubifs_find_free_space - find a data LEB with free space.
21840 + * @c: the UBIFS file-system description object
21841 + * @min_space: minimum amount of required free space
21842 + * @free: contains amount of free space in the LEB on exit
21843 + * @squeeze: whether to try to find space in a non-empty LEB first
21844 + *
21845 + * This function looks for an LEB with at least @min_space bytes of free space.
21846 + * It tries to find an empty LEB if possible. If no empty LEBs are available,
21847 + * this function searches for a non-empty data LEB. The returned LEB is marked
21848 + * as "taken".
21849 + *
21850 + * This function returns found LEB number in case of success, %-ENOSPC if it
21851 + * failed to find a LEB with @min_space bytes of free space and other a negative
21852 + * error codes in case of failure.
21853 + */
21854 +int ubifs_find_free_space(struct ubifs_info *c, int min_space, int *free,
21855 + int squeeze)
21856 +{
21857 + const struct ubifs_lprops *lprops;
21858 + int lebs, rsvd_idx_lebs, pick_free = 0, err, lnum, flags;
21859 +
21860 + dbg_find("min_space %d", min_space);
21861 + ubifs_assert(min_space > 0 && min_space <= c->dark_wm);
21862 +
21863 + ubifs_get_lprops(c);
21864 +
21865 + /* Check if there are enough empty LEBs for commit */
21866 + spin_lock(&c->space_lock);
21867 + if (c->min_idx_lebs > c->lst.idx_lebs)
21868 + rsvd_idx_lebs = c->min_idx_lebs - c->lst.idx_lebs;
21869 + else
21870 + rsvd_idx_lebs = 0;
21871 + lebs = c->lst.empty_lebs + c->freeable_cnt + c->idx_gc_cnt -
21872 + c->lst.taken_empty_lebs;
21873 + ubifs_assert(lebs + c->lst.idx_lebs >= c->min_idx_lebs);
21874 + if (rsvd_idx_lebs < lebs)
21875 + /*
21876 + * OK to allocate an empty LEB, but we still don't want to go
21877 + * looking for one if there aren't any.
21878 + */
21879 + if (c->lst.empty_lebs - c->lst.taken_empty_lebs > 0) {
21880 + pick_free = 1;
21881 + /*
21882 + * Because we release the space lock, we must account
21883 + * for this allocation here. After the LEB properties
21884 + * flags have been updated, we subtract one. Note, the
21885 + * result of this is that lprops also decreases
21886 + * @taken_empty_lebs in 'ubifs_change_lp()', so it is
21887 + * off by one for a short period of time which may
21888 + * introduce a small disturbance to budgeting
21889 + * calculations, but this is harmless because at the
21890 + * worst case this would make the budgeting subsystem
21891 + * be more pessimistic than needed.
21892 + *
21893 + * Fundamentally, this is about serialization of the
21894 + * budgeting and lprops subsystems. We could make the
21895 + * @space_lock a mutex and avoid dropping it before
21896 + * calling 'ubifs_change_lp()', but mutex is more
21897 + * heavy-weight, and we want budgeting to be as fast as
21898 + * possible.
21899 + */
21900 + c->lst.taken_empty_lebs += 1;
21901 + }
21902 + spin_unlock(&c->space_lock);
21903 +
21904 + lprops = do_find_free_space(c, min_space, pick_free, squeeze);
21905 + if (IS_ERR(lprops)) {
21906 + err = PTR_ERR(lprops);
21907 + goto out;
21908 + }
21909 +
21910 + lnum = lprops->lnum;
21911 + flags = lprops->flags | LPROPS_TAKEN;
21912 +
21913 + lprops = ubifs_change_lp(c, lprops, LPROPS_NC, LPROPS_NC, flags, 0);
21914 + if (IS_ERR(lprops)) {
21915 + err = PTR_ERR(lprops);
21916 + goto out;
21917 + }
21918 +
21919 + if (pick_free) {
21920 + spin_lock(&c->space_lock);
21921 + c->lst.taken_empty_lebs -= 1;
21922 + spin_unlock(&c->space_lock);
21923 + }
21924 +
21925 + *free = lprops->free;
21926 + ubifs_release_lprops(c);
21927 +
21928 + if (*free == c->leb_size) {
21929 + /*
21930 + * Ensure that empty LEBs have been unmapped. They may not have
21931 + * been, for example, because of an unclean unmount. Also
21932 + * LEBs that were freeable LEBs (free + dirty == leb_size) will
21933 + * not have been unmapped.
21934 + */
21935 + err = ubifs_leb_unmap(c, lnum);
21936 + if (err)
21937 + return err;
21938 + }
21939 +
21940 + dbg_find("found LEB %d, free %d", lnum, *free);
21941 + ubifs_assert(*free >= min_space);
21942 + return lnum;
21943 +
21944 +out:
21945 + if (pick_free) {
21946 + spin_lock(&c->space_lock);
21947 + c->lst.taken_empty_lebs -= 1;
21948 + spin_unlock(&c->space_lock);
21949 + }
21950 + ubifs_release_lprops(c);
21951 + return err;
21952 +}
21953 +
21954 +/**
21955 + * scan_for_idx_cb - callback used by the scan for a free LEB for the index.
21956 + * @c: the UBIFS file-system description object
21957 + * @lprops: LEB properties to scan
21958 + * @in_tree: whether the LEB properties are in main memory
21959 + * @data: information passed to and from the caller of the scan
21960 + *
21961 + * This function returns a code that indicates whether the scan should continue
21962 + * (%LPT_SCAN_CONTINUE), whether the LEB properties should be added to the tree
21963 + * in main memory (%LPT_SCAN_ADD), or whether the scan should stop
21964 + * (%LPT_SCAN_STOP).
21965 + */
21966 +static int scan_for_idx_cb(struct ubifs_info *c,
21967 + const struct ubifs_lprops *lprops, int in_tree,
21968 + struct scan_data *data)
21969 +{
21970 + int ret = LPT_SCAN_CONTINUE;
21971 +
21972 + /* Exclude LEBs that are currently in use */
21973 + if (lprops->flags & LPROPS_TAKEN)
21974 + return LPT_SCAN_CONTINUE;
21975 + /* Determine whether to add these LEB properties to the tree */
21976 + if (!in_tree && valuable(c, lprops))
21977 + ret |= LPT_SCAN_ADD;
21978 + /* Exclude index LEBS */
21979 + if (lprops->flags & LPROPS_INDEX)
21980 + return ret;
21981 + /* Exclude LEBs that cannot be made empty */
21982 + if (lprops->free + lprops->dirty != c->leb_size)
21983 + return ret;
21984 + /*
21985 + * We are allocating for the index so it is safe to allocate LEBs with
21986 + * only free and dirty space, because write buffers are sync'd at commit
21987 + * start.
21988 + */
21989 + data->lnum = lprops->lnum;
21990 + return LPT_SCAN_ADD | LPT_SCAN_STOP;
21991 +}
21992 +
21993 +/**
21994 + * scan_for_leb_for_idx - scan for a free LEB for the index.
21995 + * @c: the UBIFS file-system description object
21996 + */
21997 +static const struct ubifs_lprops *scan_for_leb_for_idx(struct ubifs_info *c)
21998 +{
21999 + struct ubifs_lprops *lprops;
22000 + struct scan_data data;
22001 + int err;
22002 +
22003 + data.lnum = -1;
22004 + err = ubifs_lpt_scan_nolock(c, -1, c->lscan_lnum,
22005 + (ubifs_lpt_scan_callback)scan_for_idx_cb,
22006 + &data);
22007 + if (err)
22008 + return ERR_PTR(err);
22009 + ubifs_assert(data.lnum >= c->main_first && data.lnum < c->leb_cnt);
22010 + c->lscan_lnum = data.lnum;
22011 + lprops = ubifs_lpt_lookup_dirty(c, data.lnum);
22012 + if (IS_ERR(lprops))
22013 + return lprops;
22014 + ubifs_assert(lprops->lnum == data.lnum);
22015 + ubifs_assert(lprops->free + lprops->dirty == c->leb_size);
22016 + ubifs_assert(!(lprops->flags & LPROPS_TAKEN));
22017 + ubifs_assert(!(lprops->flags & LPROPS_INDEX));
22018 + return lprops;
22019 +}
22020 +
22021 +/**
22022 + * ubifs_find_free_leb_for_idx - find a free LEB for the index.
22023 + * @c: the UBIFS file-system description object
22024 + *
22025 + * This function looks for a free LEB and returns that LEB number. The returned
22026 + * LEB is marked as "taken", "index".
22027 + *
22028 + * Only empty LEBs are allocated. This is for two reasons. First, the commit
22029 + * calculates the number of LEBs to allocate based on the assumption that they
22030 + * will be empty. Secondly, free space at the end of an index LEB is not
22031 + * guaranteed to be empty because it may have been used by the in-the-gaps
22032 + * method prior to an unclean unmount.
22033 + *
22034 + * If no LEB is found %-ENOSPC is returned. For other failures another negative
22035 + * error code is returned.
22036 + */
22037 +int ubifs_find_free_leb_for_idx(struct ubifs_info *c)
22038 +{
22039 + const struct ubifs_lprops *lprops;
22040 + int lnum = -1, err, flags;
22041 +
22042 + ubifs_get_lprops(c);
22043 +
22044 + lprops = ubifs_fast_find_empty(c);
22045 + if (!lprops) {
22046 + lprops = ubifs_fast_find_freeable(c);
22047 + if (!lprops) {
22048 + ubifs_assert(c->freeable_cnt == 0);
22049 + if (c->lst.empty_lebs - c->lst.taken_empty_lebs > 0) {
22050 + lprops = scan_for_leb_for_idx(c);
22051 + if (IS_ERR(lprops)) {
22052 + err = PTR_ERR(lprops);
22053 + goto out;
22054 + }
22055 + }
22056 + }
22057 + }
22058 +
22059 + if (!lprops) {
22060 + err = -ENOSPC;
22061 + goto out;
22062 + }
22063 +
22064 + lnum = lprops->lnum;
22065 +
22066 + dbg_find("found LEB %d, free %d, dirty %d, flags %#x",
22067 + lnum, lprops->free, lprops->dirty, lprops->flags);
22068 +
22069 + flags = lprops->flags | LPROPS_TAKEN | LPROPS_INDEX;
22070 + lprops = ubifs_change_lp(c, lprops, c->leb_size, 0, flags, 0);
22071 + if (IS_ERR(lprops)) {
22072 + err = PTR_ERR(lprops);
22073 + goto out;
22074 + }
22075 +
22076 + ubifs_release_lprops(c);
22077 +
22078 + /*
22079 + * Ensure that empty LEBs have been unmapped. They may not have been,
22080 + * for example, because of an unclean unmount. Also LEBs that were
22081 + * freeable LEBs (free + dirty == leb_size) will not have been unmapped.
22082 + */
22083 + err = ubifs_leb_unmap(c, lnum);
22084 + if (err) {
22085 + ubifs_change_one_lp(c, lnum, LPROPS_NC, LPROPS_NC, 0,
22086 + LPROPS_TAKEN | LPROPS_INDEX, 0);
22087 + return err;
22088 + }
22089 +
22090 + return lnum;
22091 +
22092 +out:
22093 + ubifs_release_lprops(c);
22094 + return err;
22095 +}
22096 +
22097 +static int cmp_dirty_idx(const struct ubifs_lprops **a,
22098 + const struct ubifs_lprops **b)
22099 +{
22100 + const struct ubifs_lprops *lpa = *a;
22101 + const struct ubifs_lprops *lpb = *b;
22102 +
22103 + return lpa->dirty + lpa->free - lpb->dirty - lpb->free;
22104 +}
22105 +
22106 +static void swap_dirty_idx(struct ubifs_lprops **a, struct ubifs_lprops **b,
22107 + int size)
22108 +{
22109 + struct ubifs_lprops *t = *a;
22110 +
22111 + *a = *b;
22112 + *b = t;
22113 +}
22114 +
22115 +/**
22116 + * ubifs_save_dirty_idx_lnums - save an array of the most dirty index LEB nos.
22117 + * @c: the UBIFS file-system description object
22118 + *
22119 + * This function is called each commit to create an array of LEB numbers of
22120 + * dirty index LEBs sorted in order of dirty and free space. This is used by
22121 + * the in-the-gaps method of TNC commit.
22122 + */
22123 +int ubifs_save_dirty_idx_lnums(struct ubifs_info *c)
22124 +{
22125 + int i;
22126 +
22127 + ubifs_get_lprops(c);
22128 + /* Copy the LPROPS_DIRTY_IDX heap */
22129 + c->dirty_idx.cnt = c->lpt_heap[LPROPS_DIRTY_IDX - 1].cnt;
22130 + memcpy(c->dirty_idx.arr, c->lpt_heap[LPROPS_DIRTY_IDX - 1].arr,
22131 + sizeof(void *) * c->dirty_idx.cnt);
22132 + /* Sort it so that the dirtiest is now at the end */
22133 + sort(c->dirty_idx.arr, c->dirty_idx.cnt, sizeof(void *),
22134 + (int (*)(const void *, const void *))cmp_dirty_idx,
22135 + (void (*)(void *, void *, int))swap_dirty_idx);
22136 + dbg_find("found %d dirty index LEBs", c->dirty_idx.cnt);
22137 + if (c->dirty_idx.cnt)
22138 + dbg_find("dirtiest index LEB is %d with dirty %d and free %d",
22139 + c->dirty_idx.arr[c->dirty_idx.cnt - 1]->lnum,
22140 + c->dirty_idx.arr[c->dirty_idx.cnt - 1]->dirty,
22141 + c->dirty_idx.arr[c->dirty_idx.cnt - 1]->free);
22142 + /* Replace the lprops pointers with LEB numbers */
22143 + for (i = 0; i < c->dirty_idx.cnt; i++)
22144 + c->dirty_idx.arr[i] = (void *)(size_t)c->dirty_idx.arr[i]->lnum;
22145 + ubifs_release_lprops(c);
22146 + return 0;
22147 +}
22148 +
22149 +/**
22150 + * scan_dirty_idx_cb - callback used by the scan for a dirty index LEB.
22151 + * @c: the UBIFS file-system description object
22152 + * @lprops: LEB properties to scan
22153 + * @in_tree: whether the LEB properties are in main memory
22154 + * @data: information passed to and from the caller of the scan
22155 + *
22156 + * This function returns a code that indicates whether the scan should continue
22157 + * (%LPT_SCAN_CONTINUE), whether the LEB properties should be added to the tree
22158 + * in main memory (%LPT_SCAN_ADD), or whether the scan should stop
22159 + * (%LPT_SCAN_STOP).
22160 + */
22161 +static int scan_dirty_idx_cb(struct ubifs_info *c,
22162 + const struct ubifs_lprops *lprops, int in_tree,
22163 + struct scan_data *data)
22164 +{
22165 + int ret = LPT_SCAN_CONTINUE;
22166 +
22167 + /* Exclude LEBs that are currently in use */
22168 + if (lprops->flags & LPROPS_TAKEN)
22169 + return LPT_SCAN_CONTINUE;
22170 + /* Determine whether to add these LEB properties to the tree */
22171 + if (!in_tree && valuable(c, lprops))
22172 + ret |= LPT_SCAN_ADD;
22173 + /* Exclude non-index LEBs */
22174 + if (!(lprops->flags & LPROPS_INDEX))
22175 + return ret;
22176 + /* Exclude LEBs with too little space */
22177 + if (lprops->free + lprops->dirty < c->min_idx_node_sz)
22178 + return ret;
22179 + /* Finally we found space */
22180 + data->lnum = lprops->lnum;
22181 + return LPT_SCAN_ADD | LPT_SCAN_STOP;
22182 +}
22183 +
22184 +/**
22185 + * find_dirty_idx_leb - find a dirty index LEB.
22186 + * @c: the UBIFS file-system description object
22187 + *
22188 + * This function returns LEB number upon success and a negative error code upon
22189 + * failure. In particular, -ENOSPC is returned if a dirty index LEB is not
22190 + * found.
22191 + *
22192 + * Note that this function scans the entire LPT but it is called very rarely.
22193 + */
22194 +static int find_dirty_idx_leb(struct ubifs_info *c)
22195 +{
22196 + const struct ubifs_lprops *lprops;
22197 + struct ubifs_lpt_heap *heap;
22198 + struct scan_data data;
22199 + int err, i, ret;
22200 +
22201 + /* Check all structures in memory first */
22202 + data.lnum = -1;
22203 + heap = &c->lpt_heap[LPROPS_DIRTY_IDX - 1];
22204 + for (i = 0; i < heap->cnt; i++) {
22205 + lprops = heap->arr[i];
22206 + ret = scan_dirty_idx_cb(c, lprops, 1, &data);
22207 + if (ret & LPT_SCAN_STOP)
22208 + goto found;
22209 + }
22210 + list_for_each_entry(lprops, &c->frdi_idx_list, list) {
22211 + ret = scan_dirty_idx_cb(c, lprops, 1, &data);
22212 + if (ret & LPT_SCAN_STOP)
22213 + goto found;
22214 + }
22215 + list_for_each_entry(lprops, &c->uncat_list, list) {
22216 + ret = scan_dirty_idx_cb(c, lprops, 1, &data);
22217 + if (ret & LPT_SCAN_STOP)
22218 + goto found;
22219 + }
22220 + if (c->pnodes_have >= c->pnode_cnt)
22221 + /* All pnodes are in memory, so skip scan */
22222 + return -ENOSPC;
22223 + err = ubifs_lpt_scan_nolock(c, -1, c->lscan_lnum,
22224 + (ubifs_lpt_scan_callback)scan_dirty_idx_cb,
22225 + &data);
22226 + if (err)
22227 + return err;
22228 +found:
22229 + ubifs_assert(data.lnum >= c->main_first && data.lnum < c->leb_cnt);
22230 + c->lscan_lnum = data.lnum;
22231 + lprops = ubifs_lpt_lookup_dirty(c, data.lnum);
22232 + if (IS_ERR(lprops))
22233 + return PTR_ERR(lprops);
22234 + ubifs_assert(lprops->lnum == data.lnum);
22235 + ubifs_assert(lprops->free + lprops->dirty >= c->min_idx_node_sz);
22236 + ubifs_assert(!(lprops->flags & LPROPS_TAKEN));
22237 + ubifs_assert((lprops->flags & LPROPS_INDEX));
22238 +
22239 + dbg_find("found dirty LEB %d, free %d, dirty %d, flags %#x",
22240 + lprops->lnum, lprops->free, lprops->dirty, lprops->flags);
22241 +
22242 + lprops = ubifs_change_lp(c, lprops, LPROPS_NC, LPROPS_NC,
22243 + lprops->flags | LPROPS_TAKEN, 0);
22244 + if (IS_ERR(lprops))
22245 + return PTR_ERR(lprops);
22246 +
22247 + return lprops->lnum;
22248 +}
22249 +
22250 +/**
22251 + * get_idx_gc_leb - try to get a LEB number from trivial GC.
22252 + * @c: the UBIFS file-system description object
22253 + */
22254 +static int get_idx_gc_leb(struct ubifs_info *c)
22255 +{
22256 + const struct ubifs_lprops *lp;
22257 + int err, lnum;
22258 +
22259 + err = ubifs_get_idx_gc_leb(c);
22260 + if (err < 0)
22261 + return err;
22262 + lnum = err;
22263 + /*
22264 + * The LEB was due to be unmapped after the commit but
22265 + * it is needed now for this commit.
22266 + */
22267 + lp = ubifs_lpt_lookup_dirty(c, lnum);
22268 + if (unlikely(IS_ERR(lp)))
22269 + return PTR_ERR(lp);
22270 + lp = ubifs_change_lp(c, lp, LPROPS_NC, LPROPS_NC,
22271 + lp->flags | LPROPS_INDEX, -1);
22272 + if (unlikely(IS_ERR(lp)))
22273 + return PTR_ERR(lp);
22274 + dbg_find("LEB %d, dirty %d and free %d flags %#x",
22275 + lp->lnum, lp->dirty, lp->free, lp->flags);
22276 + return lnum;
22277 +}
22278 +
22279 +/**
22280 + * find_dirtiest_idx_leb - find dirtiest index LEB from dirtiest array.
22281 + * @c: the UBIFS file-system description object
22282 + */
22283 +static int find_dirtiest_idx_leb(struct ubifs_info *c)
22284 +{
22285 + const struct ubifs_lprops *lp;
22286 + int lnum;
22287 +
22288 + while (1) {
22289 + if (!c->dirty_idx.cnt)
22290 + return -ENOSPC;
22291 + /* The lprops pointers were replaced by LEB numbers */
22292 + lnum = (size_t)c->dirty_idx.arr[--c->dirty_idx.cnt];
22293 + lp = ubifs_lpt_lookup(c, lnum);
22294 + if (IS_ERR(lp))
22295 + return PTR_ERR(lp);
22296 + if ((lp->flags & LPROPS_TAKEN) || !(lp->flags & LPROPS_INDEX))
22297 + continue;
22298 + lp = ubifs_change_lp(c, lp, LPROPS_NC, LPROPS_NC,
22299 + lp->flags | LPROPS_TAKEN, 0);
22300 + if (IS_ERR(lp))
22301 + return PTR_ERR(lp);
22302 + break;
22303 + }
22304 + dbg_find("LEB %d, dirty %d and free %d flags %#x", lp->lnum, lp->dirty,
22305 + lp->free, lp->flags);
22306 + ubifs_assert(lp->flags | LPROPS_TAKEN);
22307 + ubifs_assert(lp->flags | LPROPS_INDEX);
22308 + return lnum;
22309 +}
22310 +
22311 +/**
22312 + * ubifs_find_dirty_idx_leb - try to find dirtiest index LEB as at last commit.
22313 + * @c: the UBIFS file-system description object
22314 + *
22315 + * This function attempts to find an untaken index LEB with the most free and
22316 + * dirty space that can be used without overwriting index nodes that were in the
22317 + * last index committed.
22318 + */
22319 +int ubifs_find_dirty_idx_leb(struct ubifs_info *c)
22320 +{
22321 + int err;
22322 +
22323 + ubifs_get_lprops(c);
22324 +
22325 + /*
22326 + * We made an array of the dirtiest index LEB numbers as at the start of
22327 + * last commit. Try that array first.
22328 + */
22329 + err = find_dirtiest_idx_leb(c);
22330 +
22331 + /* Next try scanning the entire LPT */
22332 + if (err == -ENOSPC)
22333 + err = find_dirty_idx_leb(c);
22334 +
22335 + /* Finally take any index LEBs awaiting trivial GC */
22336 + if (err == -ENOSPC)
22337 + err = get_idx_gc_leb(c);
22338 +
22339 + ubifs_release_lprops(c);
22340 + return err;
22341 +}
22342 diff --exclude=.git -urN linux-2.6.25.6/fs/ubifs/gc.c avr32-2.6/fs/ubifs/gc.c
22343 --- linux-2.6.25.6/fs/ubifs/gc.c 1970-01-01 01:00:00.000000000 +0100
22344 +++ avr32-2.6/fs/ubifs/gc.c 2008-06-12 15:09:45.367815766 +0200
22345 @@ -0,0 +1,762 @@
22346 +/*
22347 + * This file is part of UBIFS.
22348 + *
22349 + * Copyright (C) 2006-2008 Nokia Corporation.
22350 + *
22351 + * This program is free software; you can redistribute it and/or modify it
22352 + * under the terms of the GNU General Public License version 2 as published by
22353 + * the Free Software Foundation.
22354 + *
22355 + * This program is distributed in the hope that it will be useful, but WITHOUT
22356 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
22357 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
22358 + * more details.
22359 + *
22360 + * You should have received a copy of the GNU General Public License along with
22361 + * this program; if not, write to the Free Software Foundation, Inc., 51
22362 + * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22363 + *
22364 + * Authors: Adrian Hunter
22365 + * Artem Bityutskiy (Битюцкий Артём)
22366 + */
22367 +
22368 +/*
22369 + * This file implements garbage collection. The procedure for garbage collection
22370 + * is different depending on whether a LEB as an index LEB (contains index
22371 + * nodes) or not. For non-index LEBs, garbage collection finds a LEB which
22372 + * contains a lot of dirty space (obsolete nodes), and copies the non-obsolete
22373 + * nodes to the journal, at which point the garbage-collected LEB is free to be
22374 + * reused. For index LEBs, garbage collection marks the non-obsolete index nodes
22375 + * dirty in the TNC, and after the next commit, the garbage-collected LEB is
22376 + * to be reused. Garbage collection will cause the number of dirty index nodes
22377 + * to grow, however sufficient space is reserved for the index to ensure the
22378 + * commit will never run out of space.
22379 + */
22380 +
22381 +#include <linux/pagemap.h>
22382 +#include "ubifs.h"
22383 +
22384 +/*
22385 + * GC tries to optimize the way it fit nodes to available space, and it sorts
22386 + * nodes a little. The below constants are watermarks which define "large",
22387 + * "medium", and "small" nodes.
22388 + */
22389 +#define MEDIUM_NODE_WM (UBIFS_BLOCK_SIZE / 4)
22390 +#define SMALL_NODE_WM UBIFS_MAX_DENT_NODE_SZ
22391 +
22392 +/*
22393 + * GC may need to move more then one LEB to make progress. The below constants
22394 + * define "soft" and "hard" limits on the number of LEBs the garbage collector
22395 + * may move.
22396 + */
22397 +#define SOFT_LEBS_LIMIT 4
22398 +#define HARD_LEBS_LIMIT 32
22399 +
22400 +/**
22401 + * switch_gc_head - switch the garbage collection journal head.
22402 + * @c: UBIFS file-system description object
22403 + * @buf: buffer to write
22404 + * @len: length of the buffer to write
22405 + * @lnum: LEB number written is returned here
22406 + * @offs: offset written is returned here
22407 + *
22408 + * This function switch the GC head to the next LEB which is reserved in
22409 + * @c->gc_lnum. Returns %0 in case of success, %-EAGAIN if commit is required,
22410 + * and other negative error code in case of failures.
22411 + */
22412 +static int switch_gc_head(struct ubifs_info *c)
22413 +{
22414 + int err, gc_lnum = c->gc_lnum;
22415 + struct ubifs_wbuf *wbuf = &c->jheads[GCHD].wbuf;
22416 +
22417 + ubifs_assert(gc_lnum != -1);
22418 + dbg_gc("switch GC head from LEB %d:%d to LEB %d (waste %d bytes)",
22419 + wbuf->lnum, wbuf->offs + wbuf->used, gc_lnum,
22420 + c->leb_size - wbuf->offs - wbuf->used);
22421 +
22422 + err = ubifs_wbuf_sync_nolock(wbuf);
22423 + if (err)
22424 + return err;
22425 +
22426 + /*
22427 + * The GC write-buffer was synchronized, we may safely unmap
22428 + * 'c->gc_lnum'.
22429 + */
22430 + err = ubifs_leb_unmap(c, gc_lnum);
22431 + if (err)
22432 + return err;
22433 +
22434 + err = ubifs_add_bud_to_log(c, GCHD, gc_lnum, 0);
22435 + if (err)
22436 + return err;
22437 +
22438 + c->gc_lnum = -1;
22439 + err = ubifs_wbuf_seek_nolock(wbuf, gc_lnum, 0, UBI_LONGTERM);
22440 + return err;
22441 +}
22442 +
22443 +/**
22444 + * move_nodes - move nodes.
22445 + * @c: UBIFS file-system description object
22446 + * @sleb: describes nodes to move
22447 + *
22448 + * This function moves valid nodes from data LEB described by @sleb to the GC
22449 + * journal head. The obsolete nodes are dropped.
22450 + *
22451 + * When moving nodes we have to deal with classical bin-packing problem: the
22452 + * space in the current GC journal head LEB and in @c->gc_lnum are the "bins",
22453 + * where the nodes in the @sleb->nodes list are the elements which should be
22454 + * fit optimally to the bins. This function uses the "first fit decreasing"
22455 + * strategy, although it does not really sort the nodes but just split them on
22456 + * 3 classes - large, medium, and small, so they are roughly sorted.
22457 + *
22458 + * This function returns zero in case of success, %-EAGAIN if commit is
22459 + * required, and other negative error codes in case of other failures.
22460 + */
22461 +static int move_nodes(struct ubifs_info *c, struct ubifs_scan_leb *sleb)
22462 +{
22463 + struct ubifs_scan_node *snod, *tmp;
22464 + struct list_head large, medium, small;
22465 + struct ubifs_wbuf *wbuf = &c->jheads[GCHD].wbuf;
22466 + int avail, err, min = INT_MAX;
22467 +
22468 + INIT_LIST_HEAD(&large);
22469 + INIT_LIST_HEAD(&medium);
22470 + INIT_LIST_HEAD(&small);
22471 +
22472 + list_for_each_entry_safe(snod, tmp, &sleb->nodes, list) {
22473 + struct list_head *lst;
22474 +
22475 + ubifs_assert(snod->type != UBIFS_IDX_NODE);
22476 + ubifs_assert(snod->type != UBIFS_REF_NODE);
22477 + ubifs_assert(snod->type != UBIFS_CS_NODE);
22478 +
22479 + err = ubifs_tnc_has_node(c, &snod->key, 0, sleb->lnum,
22480 + snod->offs, 0);
22481 + if (err < 0)
22482 + goto out;
22483 +
22484 + lst = &snod->list;
22485 + list_del(lst);
22486 + if (!err) {
22487 + /* The node is obsolete, remove it from the list */
22488 + kfree(snod);
22489 + continue;
22490 + }
22491 +
22492 + /*
22493 + * Sort the list of nodes so that large nodes go first, and
22494 + * small nodes go last.
22495 + */
22496 + if (snod->len > MEDIUM_NODE_WM)
22497 + list_add(lst, &large);
22498 + else if (snod->len > SMALL_NODE_WM)
22499 + list_add(lst, &medium);
22500 + else
22501 + list_add(lst, &small);
22502 +
22503 + /* And find the smallest node */
22504 + if (snod->len < min)
22505 + min = snod->len;
22506 + }
22507 +
22508 + /*
22509 + * Join the tree lists so that we'd have one roughly sorted list
22510 + * ('large' will be the head of the joined list).
22511 + */
22512 + list_splice(&medium, large.prev);
22513 + list_splice(&small, large.prev);
22514 +
22515 + if (wbuf->lnum == -1) {
22516 + /*
22517 + * The GC journal head is not set, because it is the first GC
22518 + * invocation since mount.
22519 + */
22520 + err = switch_gc_head(c);
22521 + if (err)
22522 + goto out;
22523 + }
22524 +
22525 + /* Write nodes to their new location. Use the first-fit strategy */
22526 + while (1) {
22527 + avail = c->leb_size - wbuf->offs - wbuf->used;
22528 + list_for_each_entry_safe(snod, tmp, &large, list) {
22529 + int new_lnum, new_offs;
22530 +
22531 + if (avail < min)
22532 + break;
22533 +
22534 + if (snod->len > avail)
22535 + /* This node does not fit */
22536 + continue;
22537 +
22538 + cond_resched();
22539 +
22540 + new_lnum = wbuf->lnum;
22541 + new_offs = wbuf->offs + wbuf->used;
22542 + err = ubifs_wbuf_write_nolock(wbuf, snod->node,
22543 + snod->len);
22544 +
22545 + err = ubifs_tnc_replace(c, &snod->key, sleb->lnum,
22546 + snod->offs, new_lnum, new_offs,
22547 + snod->len);
22548 + if (err)
22549 + goto out;
22550 +
22551 + avail = c->leb_size - wbuf->offs - wbuf->used;
22552 + list_del(&snod->list);
22553 + kfree(snod);
22554 + }
22555 +
22556 + if (list_empty(&large))
22557 + break;
22558 +
22559 + /*
22560 + * Waste the rest of the space in the LEB and switch to the
22561 + * next LEB.
22562 + */
22563 + err = switch_gc_head(c);
22564 + if (err)
22565 + goto out;
22566 + }
22567 +
22568 + return 0;
22569 +
22570 +out:
22571 + list_for_each_entry_safe(snod, tmp, &large, list) {
22572 + list_del(&snod->list);
22573 + kfree(snod);
22574 + }
22575 + return err;
22576 +}
22577 +
22578 +/**
22579 + * gc_sync_wbufs - sync write-buffers for GC.
22580 + * @c: UBIFS file-system description object
22581 + *
22582 + * We must guarantee that obsoleting nodes are on flash. Unfortunately they may
22583 + * be in a write-buffer instead. That is, a node could be written to a
22584 + * write-buffer, obsoleting another node in a LEB that is GC'd. If that LEB is
22585 + * erased before the write-buffer is sync'd and then there is an unclean
22586 + * unmount, then an existing node is lost. To avoid this, we sync all
22587 + * write-buffers.
22588 + *
22589 + * This function returns %0 on success or a negative error code on failure.
22590 + */
22591 +static int gc_sync_wbufs(struct ubifs_info *c)
22592 +{
22593 + int err, i;
22594 +
22595 + for (i = 0; i < c->jhead_cnt; i++) {
22596 + if (i == GCHD)
22597 + continue;
22598 + err = ubifs_wbuf_sync(&c->jheads[i].wbuf);
22599 + if (err)
22600 + return err;
22601 + }
22602 + return 0;
22603 +}
22604 +
22605 +/**
22606 + * ubifs_garbage_collect_leb - garbage-collect a logical eraseblock.
22607 + * @c: UBIFS file-system description object
22608 + * @lp: describes the LEB to garbage collect
22609 + *
22610 + * This function garbage-collects an LEB and returns one of the @LEB_FREED,
22611 + * @LEB_RETAINED, etc positive codes in case of success, %-EAGAIN if commit is
22612 + * required, and other negative error codes in case of failures.
22613 + */
22614 +int ubifs_garbage_collect_leb(struct ubifs_info *c, struct ubifs_lprops *lp)
22615 +{
22616 + struct ubifs_scan_leb *sleb;
22617 + struct ubifs_scan_node *snod;
22618 + struct ubifs_wbuf *wbuf = &c->jheads[GCHD].wbuf;
22619 + int err = 0, lnum = lp->lnum;
22620 +
22621 + ubifs_assert(c->gc_lnum != -1 || wbuf->offs + wbuf->used == 0 ||
22622 + c->need_recovery);
22623 + ubifs_assert(c->gc_lnum != lnum);
22624 + ubifs_assert(wbuf->lnum != lnum);
22625 +
22626 + /*
22627 + * We scan the entire LEB even though we only really need to scan up to
22628 + * (c->leb_size - lp->free).
22629 + */
22630 + sleb = ubifs_scan(c, lnum, 0, c->sbuf);
22631 + if (IS_ERR(sleb))
22632 + return PTR_ERR(sleb);
22633 +
22634 + ubifs_assert(!list_empty(&sleb->nodes));
22635 + snod = list_entry(sleb->nodes.next, struct ubifs_scan_node, list);
22636 +
22637 + if (snod->type == UBIFS_IDX_NODE) {
22638 + struct ubifs_gced_idx_leb *idx_gc;
22639 +
22640 + dbg_gc("indexing LEB %d (free %d, dirty %d)",
22641 + lnum, lp->free, lp->dirty);
22642 + list_for_each_entry(snod, &sleb->nodes, list) {
22643 + struct ubifs_idx_node *idx = snod->node;
22644 + int level = le16_to_cpu(idx->level);
22645 +
22646 + ubifs_assert(snod->type == UBIFS_IDX_NODE);
22647 + key_read(c, ubifs_idx_key(c, idx), &snod->key);
22648 + err = ubifs_dirty_idx_node(c, &snod->key, level, lnum,
22649 + snod->offs);
22650 + if (err)
22651 + goto out;
22652 + }
22653 +
22654 + idx_gc = kmalloc(sizeof(struct ubifs_gced_idx_leb), GFP_NOFS);
22655 + if (!idx_gc) {
22656 + err = -ENOMEM;
22657 + goto out;
22658 + }
22659 +
22660 + idx_gc->lnum = lnum;
22661 + idx_gc->unmap = 0;
22662 + list_add(&idx_gc->list, &c->idx_gc);
22663 +
22664 + /*
22665 + * Don't release the LEB until after the next commit, because
22666 + * it may contain date which is needed for recovery. So
22667 + * although we freed this LEB, it will become usable only after
22668 + * the commit.
22669 + */
22670 + err = ubifs_change_one_lp(c, lnum, c->leb_size, 0, 0,
22671 + LPROPS_INDEX, 1);
22672 + if (err)
22673 + goto out;
22674 + err = LEB_FREED_IDX;
22675 + } else {
22676 + dbg_gc("data LEB %d (free %d, dirty %d)",
22677 + lnum, lp->free, lp->dirty);
22678 +
22679 + err = move_nodes(c, sleb);
22680 + if (err)
22681 + goto out;
22682 +
22683 + err = gc_sync_wbufs(c);
22684 + if (err)
22685 + goto out;
22686 +
22687 + err = ubifs_change_one_lp(c, lnum, c->leb_size, 0, 0, 0, 0);
22688 + if (err)
22689 + goto out;
22690 +
22691 + if (c->gc_lnum == -1) {
22692 + c->gc_lnum = lnum;
22693 + err = LEB_RETAINED;
22694 + } else {
22695 + err = ubifs_wbuf_sync_nolock(wbuf);
22696 + if (err)
22697 + goto out;
22698 +
22699 + err = ubifs_leb_unmap(c, lnum);
22700 + if (err)
22701 + goto out;
22702 +
22703 + err = LEB_FREED;
22704 + }
22705 + }
22706 +
22707 +out:
22708 + ubifs_scan_destroy(sleb);
22709 + return err;
22710 +}
22711 +
22712 +/**
22713 + * ubifs_garbage_collect - UBIFS garbage collector.
22714 + * @c: UBIFS file-system description object
22715 + * @anyway: do GC even if there are free LEBs
22716 + *
22717 + * This function does out-of-place garbage collection. The return codes are:
22718 + * o positive LEB number if the LEB has been freed and may be used;
22719 + * o %-EAGAIN if the caller has to run commit;
22720 + * o %-ENOSPC if GC failed to make any progress;
22721 + * o other negative error codes in case of other errors.
22722 + *
22723 + * Garbage collector writes data to the journal when GC'ing data LEBs, and just
22724 + * marking indexing nodes dirty when GC'ing indexing LEBs. Thus, at some point
22725 + * commit may be required. But commit cannot be run from inside GC, because the
22726 + * caller might be holding the commit lock, so %-EAGAIN is returned instead;
22727 + * And this error code means that the caller has to run commit, and re-run GC
22728 + * if there is still no free space.
22729 + *
22730 + * There are many reasons why this function may return %-EAGAIN:
22731 + * o the log is full and there is no space to write an LEB reference for
22732 + * @c->gc_lnum;
22733 + * o the journal is too large and exceeds size limitations;
22734 + * o GC moved indexing LEBs, but they can be used only after the commit;
22735 + * o the shrinker fails to find clean znodes to free and requests the commit;
22736 + * o etc.
22737 + *
22738 + * Note, if the file-system is close to be full, this function may return
22739 + * %-EAGAIN infinitely, so the caller has to limit amount of re-invocations of
22740 + * the function. E.g., this happens if the limits on the journal size are too
22741 + * tough and GC writes too much to the journal before an LEB is freed. This
22742 + * might also mean that the journal is too large, and the TNC becomes to big,
22743 + * so that the shrinker is constantly called, finds not clean znodes to free,
22744 + * and requests commit. Well, this may also happen if the journal is all right,
22745 + * but another kernel process consumes too much memory. Anyway, infinite
22746 + * %-EAGAIN may happen, but in some extreme/misconfiguration cases.
22747 + */
22748 +int ubifs_garbage_collect(struct ubifs_info *c, int anyway)
22749 +{
22750 + int i, err, ret, min_space = c->dead_wm;
22751 + struct ubifs_lprops lp;
22752 + struct ubifs_wbuf *wbuf = &c->jheads[GCHD].wbuf;
22753 +
22754 + ubifs_assert_cmt_locked(c);
22755 +
22756 + if (ubifs_gc_should_commit(c))
22757 + return -EAGAIN;
22758 +
22759 + mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead);
22760 + /* We expect the write-buffer to be empty on entry */
22761 + ubifs_assert(!wbuf->used);
22762 +
22763 + for (i = 0; ; i++) {
22764 + int space_before = c->leb_size - wbuf->offs - wbuf->used;
22765 + int space_after;
22766 +
22767 + cond_resched();
22768 +
22769 + /* Give the commit an opportunity to run */
22770 + if (ubifs_gc_should_commit(c)) {
22771 + ret = -EAGAIN;
22772 + break;
22773 + }
22774 +
22775 + if (i > SOFT_LEBS_LIMIT && !list_empty(&c->idx_gc)) {
22776 + /*
22777 + * We've done enough iterations. Indexing LEBs were
22778 + * moved and will be available after the commit.
22779 + */
22780 + dbg_gc("soft limit, some index LEBs GC'ed, -EAGAIN");
22781 + ubifs_commit_required(c);
22782 + ret = -EAGAIN;
22783 + break;
22784 + }
22785 +
22786 + if (i > HARD_LEBS_LIMIT) {
22787 + /*
22788 + * We've moved too many LEBs and have not made
22789 + * progress, give up.
22790 + */
22791 + dbg_gc("hard limit, -ENOSPC");
22792 + ret = -ENOSPC;
22793 + break;
22794 + }
22795 +
22796 + /*
22797 + * Empty and freeable LEBs can turn up while we waited for
22798 + * the wbuf lock, or while we have been running GC. In that
22799 + * case, we should just return one of those instead of
22800 + * continuing to GC dirty LEBs. Hence we request
22801 + * 'ubifs_find_dirty_leb()' to return an empty LEB if it can.
22802 + */
22803 + ret = ubifs_find_dirty_leb(c, &lp, min_space, anyway ? 0 : 1);
22804 + if (ret) {
22805 + if (ret == -ENOSPC)
22806 + dbg_gc("no more dirty LEBs");
22807 + break;
22808 + }
22809 +
22810 + dbg_gc("found LEB %d: free %d, dirty %d, sum %d "
22811 + "(min. space %d)", lp.lnum, lp.free, lp.dirty,
22812 + lp.free + lp.dirty, min_space);
22813 +
22814 + if (lp.free + lp.dirty == c->leb_size) {
22815 + /* An empty LEB was returned */
22816 + dbg_gc("LEB %d is free, return it", lp.lnum);
22817 + /*
22818 + * ubifs_find_dirty_leb() doesn't return freeable index
22819 + * LEBs.
22820 + */
22821 + ubifs_assert(!(lp.flags & LPROPS_INDEX));
22822 + if (lp.free != c->leb_size) {
22823 + /*
22824 + * Write buffers must be sync'd before
22825 + * unmapping freeable LEBs, because one of them
22826 + * may contain data which obsoletes something
22827 + * in 'lp.pnum'.
22828 + */
22829 + ret = gc_sync_wbufs(c);
22830 + if (ret)
22831 + goto out;
22832 + ret = ubifs_change_one_lp(c, lp.lnum,
22833 + c->leb_size, 0, 0, 0,
22834 + 0);
22835 + if (ret)
22836 + goto out;
22837 + }
22838 + ret = ubifs_leb_unmap(c, lp.lnum);
22839 + if (ret)
22840 + goto out;
22841 + ret = lp.lnum;
22842 + break;
22843 + }
22844 +
22845 + space_before = c->leb_size - wbuf->offs - wbuf->used;
22846 + if (wbuf->lnum == -1)
22847 + space_before = 0;
22848 +
22849 + ret = ubifs_garbage_collect_leb(c, &lp);
22850 + if (ret < 0) {
22851 + if (ret == -EAGAIN || ret == -ENOSPC) {
22852 + /*
22853 + * These codes are not errors, so we have to
22854 + * return the LEB to lprops. But if the
22855 + * 'ubifs_return_leb()' function fails, its
22856 + * failure code is propagated to the caller
22857 + * instead of the original '-EAGAIN' or
22858 + * '-ENOSPC'.
22859 + */
22860 + err = ubifs_return_leb(c, lp.lnum);
22861 + if (err)
22862 + ret = err;
22863 + break;
22864 + }
22865 + goto out;
22866 + }
22867 +
22868 + if (ret == LEB_FREED) {
22869 + /* An LEB has been freed and is ready for use */
22870 + dbg_gc("LEB %d freed, return", lp.lnum);
22871 + ret = lp.lnum;
22872 + break;
22873 + }
22874 +
22875 + if (ret == LEB_FREED_IDX) {
22876 + /*
22877 + * This was an indexing LEB and it cannot be
22878 + * immediately used. And instead of requesting the
22879 + * commit straight away, we try to garbage collect some
22880 + * more.
22881 + */
22882 + dbg_gc("indexing LEB %d freed, continue", lp.lnum);
22883 + continue;
22884 + }
22885 +
22886 + ubifs_assert(ret == LEB_RETAINED);
22887 + space_after = c->leb_size - wbuf->offs - wbuf->used;
22888 + dbg_gc("LEB %d retained, freed %d bytes", lp.lnum,
22889 + space_after - space_before);
22890 +
22891 + if (space_after > space_before) {
22892 + /* GC makes progress, keep working */
22893 + min_space >>= 1;
22894 + if (min_space < c->dead_wm)
22895 + min_space = c->dead_wm;
22896 + continue;
22897 + }
22898 +
22899 + dbg_gc("did not make progress");
22900 +
22901 + /*
22902 + * GC moved an LEB bud have not done any progress. This means
22903 + * that the previous GC head LEB contained too few free space
22904 + * and the LEB which was GC'ed contained only large nodes which
22905 + * did not fit that space.
22906 + *
22907 + * We can do 2 things:
22908 + * 1. pick another LEB in a hope it'll contain a small node
22909 + * which will fit the space we have at the end of current GC
22910 + * head LEB, but there is no guarantee, so we try this out
22911 + * unless we have already been working for too long;
22912 + * 2. request an LEB with more dirty space, which will force
22913 + * 'ubifs_find_dirty_leb()' to start scanning the lprops
22914 + * table, instead of just picking one from the heap
22915 + * (previously it already picked the dirtiest LEB).
22916 + */
22917 + if (i < SOFT_LEBS_LIMIT) {
22918 + dbg_gc("try again");
22919 + continue;
22920 + }
22921 +
22922 + min_space <<= 1;
22923 + if (min_space > c->dark_wm)
22924 + min_space = c->dark_wm;
22925 + dbg_gc("set min. space to %d", min_space);
22926 + }
22927 +
22928 + if (ret == -ENOSPC && !list_empty(&c->idx_gc)) {
22929 + dbg_gc("no space, some index LEBs GC'ed, -EAGAIN");
22930 + ubifs_commit_required(c);
22931 + ret = -EAGAIN;
22932 + }
22933 +
22934 + err = ubifs_wbuf_sync_nolock(wbuf);
22935 + if (!err)
22936 + err = ubifs_leb_unmap(c, c->gc_lnum);
22937 + if (err)
22938 + ret = err;
22939 + mutex_unlock(&wbuf->io_mutex);
22940 + return ret;
22941 +
22942 +out:
22943 + ubifs_assert(ret < 0);
22944 + ubifs_assert(ret != -ENOSPC && ret != -EAGAIN);
22945 + ubifs_wbuf_sync_nolock(wbuf);
22946 + mutex_unlock(&wbuf->io_mutex);
22947 + ubifs_return_leb(c, lp.lnum);
22948 + return ret;
22949 +}
22950 +
22951 +/**
22952 + * ubifs_gc_start_commit - garbage collection at start of commit.
22953 + * @c: UBIFS file-system description object
22954 + *
22955 + * If a LEB has only dirty and free space, then we may safely unmap it and make
22956 + * it free. Note, we cannot do this with indexing LEBs because dirty space may
22957 + * correspond index nodes that are required for recovery. In that case, the
22958 + * LEB cannot be unmapped until after the next commit.
22959 + *
22960 + * This function returns %0 upon success and a negative error code upon failure.
22961 + */
22962 +int ubifs_gc_start_commit(struct ubifs_info *c)
22963 +{
22964 + struct ubifs_gced_idx_leb *idx_gc;
22965 + const struct ubifs_lprops *lp;
22966 + int err = 0, flags;
22967 +
22968 + ubifs_get_lprops(c);
22969 +
22970 + /*
22971 + * Unmap (non-index) freeable LEBs. Note that recovery requires that all
22972 + * wbufs are sync'd before this, which is done in 'do_commit()'.
22973 + */
22974 + while (1) {
22975 + lp = ubifs_fast_find_freeable(c);
22976 + if (unlikely(IS_ERR(lp))) {
22977 + err = PTR_ERR(lp);
22978 + goto out;
22979 + }
22980 + if (!lp)
22981 + break;
22982 + ubifs_assert(!(lp->flags & LPROPS_TAKEN));
22983 + ubifs_assert(!(lp->flags & LPROPS_INDEX));
22984 + err = ubifs_leb_unmap(c, lp->lnum);
22985 + if (err)
22986 + goto out;
22987 + lp = ubifs_change_lp(c, lp, c->leb_size, 0, lp->flags, 0);
22988 + if (unlikely(IS_ERR(lp))) {
22989 + err = PTR_ERR(lp);
22990 + goto out;
22991 + }
22992 + ubifs_assert(!(lp->flags & LPROPS_TAKEN));
22993 + ubifs_assert(!(lp->flags & LPROPS_INDEX));
22994 + }
22995 +
22996 + /* Mark GC'd index LEBs OK to unmap after this commit finishes */
22997 + list_for_each_entry(idx_gc, &c->idx_gc, list)
22998 + idx_gc->unmap = 1;
22999 +
23000 + /* Record index freeable LEBs for unmapping after commit */
23001 + while (1) {
23002 + lp = ubifs_fast_find_frdi_idx(c);
23003 + if (unlikely(IS_ERR(lp))) {
23004 + err = PTR_ERR(lp);
23005 + goto out;
23006 + }
23007 + if (!lp)
23008 + break;
23009 + idx_gc = kmalloc(sizeof(struct ubifs_gced_idx_leb), GFP_NOFS);
23010 + if (!idx_gc) {
23011 + err = -ENOMEM;
23012 + goto out;
23013 + }
23014 + ubifs_assert(!(lp->flags & LPROPS_TAKEN));
23015 + ubifs_assert(lp->flags & LPROPS_INDEX);
23016 + /* Don't release the LEB until after the next commit */
23017 + flags = (lp->flags | LPROPS_TAKEN) ^ LPROPS_INDEX;
23018 + lp = ubifs_change_lp(c, lp, c->leb_size, 0, flags, 1);
23019 + if (unlikely(IS_ERR(lp))) {
23020 + err = PTR_ERR(lp);
23021 + kfree(idx_gc);
23022 + goto out;
23023 + }
23024 + ubifs_assert(lp->flags & LPROPS_TAKEN);
23025 + ubifs_assert(!(lp->flags & LPROPS_INDEX));
23026 + idx_gc->lnum = lp->lnum;
23027 + idx_gc->unmap = 1;
23028 + list_add(&idx_gc->list, &c->idx_gc);
23029 + }
23030 +out:
23031 + ubifs_release_lprops(c);
23032 + return err;
23033 +}
23034 +
23035 +/**
23036 + * ubifs_gc_end_commit - garbage collection at end of commit.
23037 + * @c: UBIFS file-system description object
23038 + *
23039 + * This function completes out-of-place garbage collection of index LEBs.
23040 + */
23041 +int ubifs_gc_end_commit(struct ubifs_info *c)
23042 +{
23043 + struct ubifs_gced_idx_leb *idx_gc, *tmp;
23044 + struct ubifs_wbuf *wbuf;
23045 + int err = 0;
23046 +
23047 + wbuf = &c->jheads[GCHD].wbuf;
23048 + mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead);
23049 + list_for_each_entry_safe(idx_gc, tmp, &c->idx_gc, list)
23050 + if (idx_gc->unmap) {
23051 + dbg_gc("LEB %d", idx_gc->lnum);
23052 + err = ubifs_leb_unmap(c, idx_gc->lnum);
23053 + if (err)
23054 + goto out;
23055 + err = ubifs_change_one_lp(c, idx_gc->lnum, LPROPS_NC,
23056 + LPROPS_NC, 0, LPROPS_TAKEN, -1);
23057 + if (err)
23058 + goto out;
23059 + list_del(&idx_gc->list);
23060 + kfree(idx_gc);
23061 + }
23062 +out:
23063 + mutex_unlock(&wbuf->io_mutex);
23064 + return err;
23065 +}
23066 +
23067 +/**
23068 + * ubifs_destroy_idx_gc - destroy idx_gc list.
23069 + * @c: UBIFS file-system description object
23070 + *
23071 + * This function destroys the idx_gc list. It is called when unmounting or
23072 + * remounting read-only so locks are not needed.
23073 + */
23074 +void ubifs_destroy_idx_gc(struct ubifs_info *c)
23075 +{
23076 + while (!list_empty(&c->idx_gc)) {
23077 + struct ubifs_gced_idx_leb *idx_gc;
23078 +
23079 + idx_gc = list_entry(c->idx_gc.next, struct ubifs_gced_idx_leb,
23080 + list);
23081 + c->idx_gc_cnt -= 1;
23082 + list_del(&idx_gc->list);
23083 + kfree(idx_gc);
23084 + }
23085 +
23086 +}
23087 +
23088 +/**
23089 + * ubifs_get_idx_gc_leb - get a LEB from GC'd index LEB list.
23090 + * @c: UBIFS file-system description object
23091 + *
23092 + * Called during start commit so locks are not needed.
23093 + */
23094 +int ubifs_get_idx_gc_leb(struct ubifs_info *c)
23095 +{
23096 + struct ubifs_gced_idx_leb *idx_gc;
23097 + int lnum;
23098 +
23099 + if (list_empty(&c->idx_gc))
23100 + return -ENOSPC;
23101 + idx_gc = list_entry(c->idx_gc.next, struct ubifs_gced_idx_leb, list);
23102 + lnum = idx_gc->lnum;
23103 + /* c->idx_gc_cnt is updated by the caller when lprops are updated */
23104 + list_del(&idx_gc->list);
23105 + kfree(idx_gc);
23106 + return lnum;
23107 +}
23108 diff --exclude=.git -urN linux-2.6.25.6/fs/ubifs/io.c avr32-2.6/fs/ubifs/io.c
23109 --- linux-2.6.25.6/fs/ubifs/io.c 1970-01-01 01:00:00.000000000 +0100
23110 +++ avr32-2.6/fs/ubifs/io.c 2008-06-12 15:09:45.367815766 +0200
23111 @@ -0,0 +1,921 @@
23112 +/*
23113 + * This file is part of UBIFS.
23114 + *
23115 + * Copyright (C) 2006-2008 Nokia Corporation.
23116 + * Copyright (C) 2006, 2007 University of Szeged, Hungary
23117 + *
23118 + * This program is free software; you can redistribute it and/or modify it
23119 + * under the terms of the GNU General Public License version 2 as published by
23120 + * the Free Software Foundation.
23121 + *
23122 + * This program is distributed in the hope that it will be useful, but WITHOUT
23123 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
23124 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
23125 + * more details.
23126 + *
23127 + * You should have received a copy of the GNU General Public License along with
23128 + * this program; if not, write to the Free Software Foundation, Inc., 51
23129 + * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23130 + *
23131 + * Authors: Artem Bityutskiy (Битюцкий Артём)
23132 + * Adrian Hunter
23133 + * Zoltan Sogor
23134 + */
23135 +
23136 +/*
23137 + * This file implements UBIFS I/O subsystem which provides various I/O-related
23138 + * helper functions (reading/writing/checking/validating nodes) and implements
23139 + * write-buffering support. Write buffers help to save space which otherwise
23140 + * would have been wasted for padding to the nearest minimal I/O unit boundary.
23141 + * Instead, data first goes to the write-buffer and is flushed when the
23142 + * buffer is full or when it is not used for some time (by timer). This is
23143 + * similarto the mechanism is used by JFFS2.
23144 + *
23145 + * Write-buffers are defined by 'struct ubifs_wbuf' objects and protected by
23146 + * mutexes defined inside these objects. Since sometimes upper-level code
23147 + * has to lock the write-buffer (e.g. journal space reservation code), many
23148 + * functions related to write-buffers have "nolock" suffix which means that the
23149 + * caller has to lock the write-buffer before calling this function.
23150 + *
23151 + * UBIFS stores nodes at 64 bit-aligned addresses. If the node length is not
23152 + * aligned, UBIFS starts the next node from the aligned address, and the padded
23153 + * bytes may contain any rubbish. In other words, UBIFS does not put padding
23154 + * bytes in those small gaps. Common headers of nodes store real node lengths,
23155 + * not aligned lengths. Indexing nodes also store real lengths in branches.
23156 + *
23157 + * UBIFS uses padding when it pads to the next min. I/O unit. In this case it
23158 + * uses padding nodes or padding bytes, if the padding node does not fit.
23159 + *
23160 + * All UBIFS nodes are protected by CRC checksums and UBIFS checks all nodes
23161 + * every time they are read from the flash media.
23162 + */
23163 +
23164 +#include <linux/crc32.h>
23165 +#include "ubifs.h"
23166 +
23167 +/**
23168 + * ubifs_check_node - check node.
23169 + * @c: UBIFS file-system description object
23170 + * @buf: node to check
23171 + * @lnum: logical eraseblock number
23172 + * @offs: offset within the logical eraseblock
23173 + * @quiet: print no messages
23174 + *
23175 + * This function checks node magic number and CRC checksum. This function also
23176 + * validates node length to prevent UBIFS from becoming crazy when an attacker
23177 + * feeds it a file-system image with incorrect nodes. For example, too large
23178 + * node length in the common header could cause UBIFS to read memory outside of
23179 + * allocated buffer when checking the CRC checksum.
23180 + *
23181 + * This function returns zero in case of success %-EUCLEAN in case of bad CRC
23182 + * or magic.
23183 + */
23184 +int ubifs_check_node(const struct ubifs_info *c, const void *buf, int lnum,
23185 + int offs, int quiet)
23186 +{
23187 + int err = -EINVAL, type, node_len;
23188 + uint32_t crc, node_crc, magic;
23189 + const struct ubifs_ch *ch = buf;
23190 +
23191 + ubifs_assert(lnum >= 0 && lnum < c->leb_cnt && offs >= 0);
23192 + ubifs_assert(!(offs & 7) && offs < c->leb_size);
23193 +
23194 + magic = le32_to_cpu(ch->magic);
23195 + if (magic != UBIFS_NODE_MAGIC) {
23196 + if (!quiet)
23197 + ubifs_err("bad magic %#08x, expected %#08x",
23198 + magic, UBIFS_NODE_MAGIC);
23199 + err = -EUCLEAN;
23200 + goto out;
23201 + }
23202 +
23203 + type = ch->node_type;
23204 + if (type < 0 || type >= UBIFS_NODE_TYPES_CNT) {
23205 + if (!quiet)
23206 + ubifs_err("bad node type %d", type);
23207 + goto out;
23208 + }
23209 +
23210 + node_len = le32_to_cpu(ch->len);
23211 + if (node_len + offs > c->leb_size)
23212 + goto out_len;
23213 +
23214 + if (c->ranges[type].max_len == 0) {
23215 + if (node_len != c->ranges[type].len)
23216 + goto out_len;
23217 + } else if (node_len < c->ranges[type].min_len ||
23218 + node_len > c->ranges[type].max_len)
23219 + goto out_len;
23220 +
23221 + crc = crc32(UBIFS_CRC32_INIT, buf + 8, node_len - 8);
23222 + node_crc = le32_to_cpu(ch->crc);
23223 + if (crc != node_crc) {
23224 + if (!quiet)
23225 + ubifs_err("bad CRC: calculated %#08x, read %#08x",
23226 + crc, node_crc);
23227 + err = -EUCLEAN;
23228 + goto out;
23229 + }
23230 +
23231 + return 0;
23232 +
23233 +out_len:
23234 + if (!quiet)
23235 + ubifs_err("bad node length %d", node_len);
23236 +out:
23237 + if (!quiet) {
23238 + ubifs_err("bad node at LEB %d:%d", lnum, offs);
23239 + dbg_dump_node(c, buf);
23240 + dbg_dump_stack();
23241 + }
23242 + return err;
23243 +}
23244 +
23245 +/**
23246 + * ubifs_pad - pad flash space.
23247 + * @c: UBIFS file-system description object
23248 + * @buf: buffer to put padding to
23249 + * @pad: how many bytes to pad
23250 + *
23251 + * The flash media obliges us to write only in chunks of %c->min_io_size and
23252 + * when we have to write less data we add padding node to the write-buffer and
23253 + * pad it to the next minimal I/O unit's boundary. Padding nodes help when the
23254 + * media is being scanned. If the amount of wasted space is not enough to fit a
23255 + * padding node which takes %UBIFS_PAD_NODE_SZ bytes, we write padding bytes
23256 + * pattern (%UBIFS_PADDING_BYTE).
23257 + *
23258 + * Padding nodes are also used to fill gaps when the "commit-in-gaps" method is
23259 + * used.
23260 + */
23261 +void ubifs_pad(const struct ubifs_info *c, void *buf, int pad)
23262 +{
23263 + uint32_t crc;
23264 +
23265 + ubifs_assert(pad >= 0 && !(pad & 7));
23266 +
23267 + if (pad >= UBIFS_PAD_NODE_SZ) {
23268 + struct ubifs_ch *ch = buf;
23269 + struct ubifs_pad_node *pad_node = buf;
23270 +
23271 + ch->magic = cpu_to_le32(UBIFS_NODE_MAGIC);
23272 + ch->node_type = UBIFS_PAD_NODE;
23273 + ch->group_type = UBIFS_NO_NODE_GROUP;
23274 + ch->padding[0] = ch->padding[1] = 0;
23275 + ch->sqnum = 0;
23276 + ch->len = cpu_to_le32(UBIFS_PAD_NODE_SZ);
23277 + pad -= UBIFS_PAD_NODE_SZ;
23278 + pad_node->pad_len = cpu_to_le32(pad);
23279 + crc = crc32(UBIFS_CRC32_INIT, buf + 8, UBIFS_PAD_NODE_SZ - 8);
23280 + ch->crc = cpu_to_le32(crc);
23281 + memset(buf + UBIFS_PAD_NODE_SZ, 0, pad);
23282 + } else if (pad > 0)
23283 + /* Too little space, padding node won't fit */
23284 + memset(buf, UBIFS_PADDING_BYTE, pad);
23285 +}
23286 +
23287 +/**
23288 + * next_sqnum - get next sequence number.
23289 + * @c: UBIFS file-system description object
23290 + */
23291 +static unsigned long long next_sqnum(struct ubifs_info *c)
23292 +{
23293 + unsigned long long sqnum;
23294 +
23295 + spin_lock(&c->cnt_lock);
23296 + sqnum = ++c->max_sqnum;
23297 + spin_unlock(&c->cnt_lock);
23298 +
23299 + if (unlikely(sqnum >= SQNUM_WARN_WATERMARK)) {
23300 + if (sqnum >= SQNUM_WATERMARK) {
23301 + ubifs_err("sequence number overflow %llu, end of life",
23302 + sqnum);
23303 + ubifs_ro_mode(c, -EINVAL);
23304 + }
23305 + ubifs_warn("running out of sequence numbers, end of life soon");
23306 + }
23307 +
23308 + return sqnum;
23309 +}
23310 +
23311 +/**
23312 + * ubifs_prepare_node - prepare node to be written to flash.
23313 + * @c: UBIFS file-system description object
23314 + * @node: the node to pad
23315 + * @len: node length
23316 + * @pad: if the buffer has to be padded
23317 + *
23318 + * This function prepares node at @node to be written to the media - it
23319 + * calculates node CRC, fills the common header, and adds proper padding up to
23320 + * the next minimum I/O unit if @pad is not zero.
23321 + */
23322 +void ubifs_prepare_node(struct ubifs_info *c, void *node, int len, int pad)
23323 +{
23324 + uint32_t crc;
23325 + struct ubifs_ch *ch = node;
23326 + unsigned long long sqnum = next_sqnum(c);
23327 +
23328 + ubifs_assert(len >= UBIFS_CH_SZ);
23329 +
23330 + ch->magic = cpu_to_le32(UBIFS_NODE_MAGIC);
23331 + ch->len = cpu_to_le32(len);
23332 + ch->group_type = UBIFS_NO_NODE_GROUP;
23333 + ch->sqnum = cpu_to_le64(sqnum);
23334 + ch->padding[0] = ch->padding[1] = 0;
23335 + crc = crc32(UBIFS_CRC32_INIT, node + 8, len - 8);
23336 + ch->crc = cpu_to_le32(crc);
23337 +
23338 + if (pad) {
23339 + len = ALIGN(len, 8);
23340 + pad = ALIGN(len, c->min_io_size) - len;
23341 + ubifs_pad(c, node + len, pad);
23342 + }
23343 +}
23344 +
23345 +/**
23346 + * ubifs_prep_grp_node - prepare node of a group to be written to flash.
23347 + * @c: UBIFS file-system description object
23348 + * @node: the node to pad
23349 + * @len: node length
23350 + * @last: indicates the last node of the group
23351 + *
23352 + * This function prepares node at @node to be written to the media - it
23353 + * calculates node CRC and fills the common header.
23354 + */
23355 +void ubifs_prep_grp_node(struct ubifs_info *c, void *node, int len, int last)
23356 +{
23357 + uint32_t crc;
23358 + struct ubifs_ch *ch = node;
23359 + unsigned long long sqnum = next_sqnum(c);
23360 +
23361 + ubifs_assert(len >= UBIFS_CH_SZ);
23362 +
23363 + ch->magic = cpu_to_le32(UBIFS_NODE_MAGIC);
23364 + ch->len = cpu_to_le32(len);
23365 + if (last)
23366 + ch->group_type = UBIFS_LAST_OF_NODE_GROUP;
23367 + else
23368 + ch->group_type = UBIFS_IN_NODE_GROUP;
23369 + ch->sqnum = cpu_to_le64(sqnum);
23370 + ch->padding[0] = ch->padding[1] = 0;
23371 + crc = crc32(UBIFS_CRC32_INIT, node + 8, len - 8);
23372 + ch->crc = cpu_to_le32(crc);
23373 +}
23374 +
23375 +/**
23376 + * wbuf_timer_callback - write-buffer timer callback function.
23377 + * @data: timer data (write-buffer descriptor)
23378 + *
23379 + * This function is called when the write-buffer timer expires.
23380 + */
23381 +static void wbuf_timer_callback_nolock(unsigned long data)
23382 +{
23383 + struct ubifs_wbuf *wbuf = (struct ubifs_wbuf *)data;
23384 +
23385 + wbuf->need_sync = 1;
23386 + wbuf->c->need_wbuf_sync = 1;
23387 + ubifs_wake_up_bgt(wbuf->c);
23388 +}
23389 +
23390 +/**
23391 + * new_wbuf_timer - start new write-buffer timer.
23392 + * @wbuf: write-buffer descriptor
23393 + */
23394 +static void new_wbuf_timer_nolock(struct ubifs_wbuf *wbuf)
23395 +{
23396 + ubifs_assert(!timer_pending(&wbuf->timer));
23397 +
23398 + if (!wbuf->timeout)
23399 + return;
23400 +
23401 + wbuf->timer.expires = jiffies + wbuf->timeout;
23402 + add_timer(&wbuf->timer);
23403 +}
23404 +
23405 +/**
23406 + * cancel_wbuf_timer - cancel write-buffer timer.
23407 + * @wbuf: write-buffer descriptor
23408 + */
23409 +static void cancel_wbuf_timer_nolock(struct ubifs_wbuf *wbuf)
23410 +{
23411 + /*
23412 + * If the syncer is waiting for the lock (from the background thread's
23413 + * context) and another task is changing write-buffer then the syncing
23414 + * should be canceled.
23415 + */
23416 + wbuf->need_sync = 0;
23417 + del_timer(&wbuf->timer);
23418 +}
23419 +
23420 +/**
23421 + * ubifs_wbuf_sync_nolock - synchronize write-buffer.
23422 + * @wbuf: write-buffer to synchronize
23423 + *
23424 + * This function synchronizes write-buffer @buf and returns zero in case of
23425 + * success or a negative error code in case of failure.
23426 + */
23427 +int ubifs_wbuf_sync_nolock(struct ubifs_wbuf *wbuf)
23428 +{
23429 + struct ubifs_info *c = wbuf->c;
23430 + int err, dirt;
23431 +
23432 + cancel_wbuf_timer_nolock(wbuf);
23433 + if (!wbuf->used || wbuf->lnum == -1)
23434 + /* Write-buffer is empty or not seeked */
23435 + return 0;
23436 +
23437 + dbg_io("LEB %d:%d, %d bytes",
23438 + wbuf->lnum, wbuf->offs, wbuf->used);
23439 + ubifs_assert(!(c->vfs_sb->s_flags & MS_RDONLY));
23440 + ubifs_assert(!(wbuf->avail & 7));
23441 + ubifs_assert(wbuf->offs + c->min_io_size <= c->leb_size);
23442 +
23443 + if (c->ro_media)
23444 + return -EROFS;
23445 +
23446 + ubifs_pad(c, wbuf->buf + wbuf->used, wbuf->avail);
23447 + err = ubi_leb_write(c->ubi, wbuf->lnum, wbuf->buf, wbuf->offs,
23448 + c->min_io_size, wbuf->dtype);
23449 + if (err) {
23450 + ubifs_err("cannot write %d bytes to LEB %d:%d",
23451 + c->min_io_size, wbuf->lnum, wbuf->offs);
23452 + dbg_dump_stack();
23453 + return err;
23454 + }
23455 +
23456 + dirt = wbuf->avail;
23457 +
23458 + spin_lock(&wbuf->lock);
23459 + wbuf->offs += c->min_io_size;
23460 + wbuf->avail = c->min_io_size;
23461 + wbuf->used = 0;
23462 + wbuf->next_ino = 0;
23463 + spin_unlock(&wbuf->lock);
23464 +
23465 + if (wbuf->sync_callback)
23466 + err = wbuf->sync_callback(c, wbuf->lnum,
23467 + c->leb_size - wbuf->offs, dirt);
23468 + return err;
23469 +}
23470 +
23471 +/**
23472 + * ubifs_wbuf_seek_nolock - seek write-buffer.
23473 + * @wbuf: write-buffer
23474 + * @lnum: logical eraseblock number to seek to
23475 + * @offs: logical eraseblock offset to seek to
23476 + * @dtype: data type
23477 + *
23478 + * This function targets the write buffer to logical eraseblock @lnum:@offs.
23479 + * The write-buffer is synchronized if it is not empty. Returns zero in case of
23480 + * success and a negative error code in case of failure.
23481 + */
23482 +int ubifs_wbuf_seek_nolock(struct ubifs_wbuf *wbuf, int lnum, int offs,
23483 + int dtype)
23484 +{
23485 + const struct ubifs_info *c = wbuf->c;
23486 +
23487 + dbg_io("LEB %d:%d", lnum, offs);
23488 + ubifs_assert(lnum >= 0 && lnum < c->leb_cnt);
23489 + ubifs_assert(offs >= 0 && offs <= c->leb_size);
23490 + ubifs_assert(offs % c->min_io_size == 0 && !(offs & 7));
23491 + ubifs_assert(lnum != wbuf->lnum);
23492 +
23493 + if (wbuf->used > 0) {
23494 + int err = ubifs_wbuf_sync_nolock(wbuf);
23495 +
23496 + if (err)
23497 + return err;
23498 + }
23499 +
23500 + spin_lock(&wbuf->lock);
23501 + wbuf->lnum = lnum;
23502 + wbuf->offs = offs;
23503 + wbuf->avail = c->min_io_size;
23504 + wbuf->used = 0;
23505 + spin_unlock(&wbuf->lock);
23506 + wbuf->dtype = dtype;
23507 +
23508 + return 0;
23509 +}
23510 +
23511 +/**
23512 + * ubifs_bg_wbufs_sync - synchronize write-buffers.
23513 + * @c: UBIFS file-system description object
23514 + *
23515 + * This function is called by background thread to synchronize write-buffers.
23516 + * Returns zero in case of success and a negative error code in case of
23517 + * failure.
23518 + */
23519 +int ubifs_bg_wbufs_sync(struct ubifs_info *c)
23520 +{
23521 + int err, i;
23522 +
23523 + if (!c->need_wbuf_sync)
23524 + return 0;
23525 + c->need_wbuf_sync = 0;
23526 +
23527 + if (c->ro_media) {
23528 + err = -EROFS;
23529 + goto out_timers;
23530 + }
23531 +
23532 + dbg_io("synchronize");
23533 + for (i = 0; i < c->jhead_cnt; i++) {
23534 + struct ubifs_wbuf *wbuf = &c->jheads[i].wbuf;
23535 +
23536 + cond_resched();
23537 +
23538 + /*
23539 + * If the mutex is locked then wbuf is being changed, so
23540 + * synchronization is not necessary.
23541 + */
23542 + if (mutex_is_locked(&wbuf->io_mutex))
23543 + continue;
23544 +
23545 + mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead);
23546 + if (!wbuf->need_sync) {
23547 + mutex_unlock(&wbuf->io_mutex);
23548 + continue;
23549 + }
23550 +
23551 + err = ubifs_wbuf_sync_nolock(wbuf);
23552 + mutex_unlock(&wbuf->io_mutex);
23553 + if (err) {
23554 + ubifs_err("cannot sync write-buffer, error %d", err);
23555 + ubifs_ro_mode(c, err);
23556 + goto out_timers;
23557 + }
23558 + }
23559 +
23560 + return 0;
23561 +
23562 +out_timers:
23563 + /* Cancel all timers to prevent repeated errors */
23564 + for (i = 0; i < c->jhead_cnt; i++) {
23565 + struct ubifs_wbuf *wbuf = &c->jheads[i].wbuf;
23566 +
23567 + mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead);
23568 + cancel_wbuf_timer_nolock(wbuf);
23569 + mutex_unlock(&wbuf->io_mutex);
23570 + }
23571 + return err;
23572 +}
23573 +
23574 +/**
23575 + * ubifs_wbuf_write_nolock - write data to flash via write-buffer.
23576 + * @wbuf: write-buffer
23577 + * @buf: node to write
23578 + * @len: node length
23579 + *
23580 + * This function writes data to flash via write-buffer @wbuf. This means that
23581 + * the last piece of the node won't reach the flash media immediately if it
23582 + * does not take whole minimal I/O unit. Instead, the node will sit in RAM
23583 + * until the write-buffer is synchronized (e.g., by timer).
23584 + *
23585 + * This function returns zero in case of success and a negative error code in
23586 + * case of failure. If the node cannot be written because there is no more
23587 + * space in this logical eraseblock, %-ENOSPC is returned.
23588 + */
23589 +int ubifs_wbuf_write_nolock(struct ubifs_wbuf *wbuf, void *buf, int len)
23590 +{
23591 + struct ubifs_info *c = wbuf->c;
23592 + int err, written, n, aligned_len = ALIGN(len, 8), offs;
23593 +
23594 + dbg_io("%d bytes (%s) to wbuf at LEB %d:%d", len,
23595 + dbg_ntype(((struct ubifs_ch *)buf)->node_type), wbuf->lnum,
23596 + wbuf->offs + wbuf->used);
23597 + ubifs_assert(len > 0 && wbuf->lnum >= 0 && wbuf->lnum < c->leb_cnt);
23598 + ubifs_assert(wbuf->offs >= 0 && wbuf->offs % c->min_io_size == 0);
23599 + ubifs_assert(!(wbuf->offs & 7) && wbuf->offs <= c->leb_size);
23600 + ubifs_assert(wbuf->avail > 0 && wbuf->avail <= c->min_io_size);
23601 + ubifs_assert(mutex_is_locked(&wbuf->io_mutex));
23602 +
23603 + if (c->leb_size - wbuf->offs - wbuf->used < aligned_len) {
23604 + err = -ENOSPC;
23605 + goto out;
23606 + }
23607 +
23608 + cancel_wbuf_timer_nolock(wbuf);
23609 +
23610 + if (c->ro_media)
23611 + return -EROFS;
23612 +
23613 + if (aligned_len <= wbuf->avail) {
23614 + /*
23615 + * The node is not very large and fits entirely within
23616 + * write-buffer.
23617 + */
23618 + memcpy(wbuf->buf + wbuf->used, buf, len);
23619 +
23620 + if (aligned_len == wbuf->avail) {
23621 + dbg_io("flush wbuf to LEB %d:%d", wbuf->lnum,
23622 + wbuf->offs);
23623 + err = ubi_leb_write(c->ubi, wbuf->lnum, wbuf->buf,
23624 + wbuf->offs, c->min_io_size,
23625 + wbuf->dtype);
23626 + if (err)
23627 + goto out;
23628 +
23629 + spin_lock(&wbuf->lock);
23630 + wbuf->offs += c->min_io_size;
23631 + wbuf->avail = c->min_io_size;
23632 + wbuf->used = 0;
23633 + wbuf->next_ino = 0;
23634 + spin_unlock(&wbuf->lock);
23635 + } else {
23636 + spin_lock(&wbuf->lock);
23637 + wbuf->avail -= aligned_len;
23638 + wbuf->used += aligned_len;
23639 + spin_unlock(&wbuf->lock);
23640 + }
23641 +
23642 + goto exit;
23643 + }
23644 +
23645 + /*
23646 + * The node is large enough and does not fit entirely within current
23647 + * minimal I/O unit. We have to fill and flush write-buffer and switch
23648 + * to the next min. I/O unit.
23649 + */
23650 + dbg_io("flush wbuf to LEB %d:%d", wbuf->lnum, wbuf->offs);
23651 + memcpy(wbuf->buf + wbuf->used, buf, wbuf->avail);
23652 + err = ubi_leb_write(c->ubi, wbuf->lnum, wbuf->buf, wbuf->offs,
23653 + c->min_io_size, wbuf->dtype);
23654 + if (err)
23655 + goto out;
23656 +
23657 + offs = wbuf->offs + c->min_io_size;
23658 + len -= wbuf->avail;
23659 + aligned_len -= wbuf->avail;
23660 + written = wbuf->avail;
23661 +
23662 + /*
23663 + * The remaining data may take more whole min. I/O units, so write the
23664 + * remains multiple to min. I/O unit size directly to the flash media.
23665 + * We align node length to 8-byte boundary because we anyway flash wbuf
23666 + * if the remaining space is less than 8 bytes.
23667 + */
23668 + n = aligned_len >> c->min_io_shift;
23669 + if (n) {
23670 + n <<= c->min_io_shift;
23671 + dbg_io("write %d bytes to LEB %d:%d", n, wbuf->lnum, offs);
23672 + err = ubi_leb_write(c->ubi, wbuf->lnum, buf + written, offs, n,
23673 + wbuf->dtype);
23674 + if (err)
23675 + goto out;
23676 + offs += n;
23677 + aligned_len -= n;
23678 + len -= n;
23679 + written += n;
23680 + }
23681 +
23682 + spin_lock(&wbuf->lock);
23683 + if (aligned_len)
23684 + /*
23685 + * And now we have what's left and what does not take whole
23686 + * min. I/O unit, so write it to the write-buffer and we are
23687 + * done.
23688 + */
23689 + memcpy(wbuf->buf, buf + written, len);
23690 +
23691 + wbuf->offs = offs;
23692 + wbuf->used = aligned_len;
23693 + wbuf->avail = c->min_io_size - aligned_len;
23694 + wbuf->next_ino = 0;
23695 + spin_unlock(&wbuf->lock);
23696 +
23697 +exit:
23698 + if (wbuf->sync_callback) {
23699 + int free = c->leb_size - wbuf->offs - wbuf->used;
23700 +
23701 + err = wbuf->sync_callback(c, wbuf->lnum, free, 0);
23702 + if (err)
23703 + goto out;
23704 + }
23705 +
23706 + if (wbuf->used)
23707 + new_wbuf_timer_nolock(wbuf);
23708 +
23709 + return 0;
23710 +
23711 +out:
23712 + ubifs_err("cannot write %d bytes to LEB %d:%d, error %d",
23713 + len, wbuf->lnum, wbuf->offs, err);
23714 + dbg_dump_node(c, buf);
23715 + dbg_dump_stack();
23716 + dbg_dump_leb(c, wbuf->lnum);
23717 + return err;
23718 +}
23719 +
23720 +/**
23721 + * ubifs_write_node - write node to the media.
23722 + * @c: UBIFS file-system description object
23723 + * @buf: the node to write
23724 + * @len: node length
23725 + * @lnum: logical eraseblock number
23726 + * @offs: offset within the logical eraseblock
23727 + * @dtype: node life-time hint (%UBI_LONGTERM, %UBI_SHORTTERM, %UBI_UNKNOWN)
23728 + *
23729 + * This function automatically fills node magic number, assigns sequence
23730 + * number, and calculates node CRC checksum. The length of the @buf buffer has
23731 + * to be aligned to the minimal I/O unit size. This function automatically
23732 + * appends padding node and padding bytes if needed. Returns zero in case of
23733 + * success and a negative error code in case of failure.
23734 + */
23735 +int ubifs_write_node(struct ubifs_info *c, void *buf, int len, int lnum,
23736 + int offs, int dtype)
23737 +{
23738 + int err, buf_len = ALIGN(len, c->min_io_size);
23739 +
23740 + dbg_io("LEB %d:%d, %s, length %d (aligned %d)",
23741 + lnum, offs, dbg_ntype(((struct ubifs_ch *)buf)->node_type), len,
23742 + buf_len);
23743 + ubifs_assert(lnum >= 0 && lnum < c->leb_cnt && offs >= 0);
23744 + ubifs_assert(offs % c->min_io_size == 0 && offs < c->leb_size);
23745 +
23746 + if (c->ro_media)
23747 + return -EROFS;
23748 +
23749 + ubifs_prepare_node(c, buf, len, 1);
23750 + err = ubi_leb_write(c->ubi, lnum, buf, offs, buf_len, dtype);
23751 + if (err) {
23752 + ubifs_err("cannot write %d bytes to LEB %d:%d, error %d",
23753 + buf_len, lnum, offs, err);
23754 + dbg_dump_node(c, buf);
23755 + dbg_dump_stack();
23756 + }
23757 +
23758 + return err;
23759 +}
23760 +
23761 +/**
23762 + * ubifs_read_node_wbuf - read node from the media or write-buffer.
23763 + * @wbuf: wbuf to check for un-written data
23764 + * @buf: buffer to read to
23765 + * @type: node type
23766 + * @len: node length
23767 + * @lnum: logical eraseblock number
23768 + * @offs: offset within the logical eraseblock
23769 + *
23770 + * This function reads a node of known type and length, checks it and stores
23771 + * in @buf. If the node partially or fully sits in the write-buffer, this
23772 + * function takes data from the buffer, otherwise it reads the flash media.
23773 + * Returns zero in case of success, %-EUCLEAN if CRC mismatched and a negative
23774 + * error code in case of failure.
23775 + */
23776 +int ubifs_read_node_wbuf(struct ubifs_wbuf *wbuf, void *buf, int type, int len,
23777 + int lnum, int offs)
23778 +{
23779 + const struct ubifs_info *c = wbuf->c;
23780 + int err, rlen, overlap;
23781 + struct ubifs_ch *ch = buf;
23782 +
23783 + dbg_io("LEB %d:%d, %s, length %d", lnum, offs, dbg_ntype(type), len);
23784 + ubifs_assert(wbuf && lnum >= 0 && lnum < c->leb_cnt && offs >= 0);
23785 + ubifs_assert(!(offs & 7) && offs < c->leb_size);
23786 + ubifs_assert(type >= 0 && type < UBIFS_NODE_TYPES_CNT);
23787 +
23788 + spin_lock(&wbuf->lock);
23789 + overlap = (lnum == wbuf->lnum && offs + len > wbuf->offs);
23790 + if (!overlap) {
23791 + /* We may safely unlock the write-buffer and read the data */
23792 + spin_unlock(&wbuf->lock);
23793 + return ubifs_read_node(c, buf, type, len, lnum, offs);
23794 + }
23795 +
23796 + /* Don't read under wbuf */
23797 + rlen = wbuf->offs - offs;
23798 + if (rlen < 0)
23799 + rlen = 0;
23800 +
23801 + /* Copy the rest from the write-buffer */
23802 + memcpy(buf + rlen, wbuf->buf + offs + rlen - wbuf->offs, len - rlen);
23803 + spin_unlock(&wbuf->lock);
23804 +
23805 + if (rlen > 0) {
23806 + /* Read everything that goes before write-buffer */
23807 + err = ubi_read(c->ubi, lnum, buf, offs, rlen);
23808 + if (err && err != -EBADMSG) {
23809 + ubifs_err("failed to read node %d from LEB %d:%d, "
23810 + "error %d", type, lnum, offs, err);
23811 + dbg_dump_stack();
23812 + return err;
23813 + }
23814 + }
23815 +
23816 + err = ubifs_check_node(c, buf, lnum, offs, 0);
23817 + if (err) {
23818 + ubifs_err("expected node type %d", type);
23819 + return err;
23820 + }
23821 +
23822 + if (type != ch->node_type) {
23823 + ubifs_err("bad node type (%d but expected %d)",
23824 + ch->node_type, type);
23825 + goto out;
23826 + }
23827 +
23828 + rlen = le32_to_cpu(ch->len);
23829 + if (rlen != len) {
23830 + ubifs_err("bad node length %d, expected %d", rlen, len);
23831 + goto out;
23832 + }
23833 +
23834 + return 0;
23835 +
23836 +out:
23837 + ubifs_err("bad node at LEB %d:%d", lnum, offs);
23838 + dbg_dump_node(c, buf);
23839 + dbg_dump_stack();
23840 + return -EINVAL;
23841 +}
23842 +
23843 +/**
23844 + * ubifs_read_node - read node.
23845 + * @c: UBIFS file-system description object
23846 + * @buf: buffer to read to
23847 + * @type: node type
23848 + * @len: node length (not aligned)
23849 + * @lnum: logical eraseblock number
23850 + * @offs: offset within the logical eraseblock
23851 + *
23852 + * This function reads a node of known type and and length, checks it and
23853 + * stores in @buf. Returns zero in case of success, %-EUCLEAN if CRC mismatched
23854 + * and a negative error code in case of failure.
23855 + */
23856 +int ubifs_read_node(const struct ubifs_info *c, void *buf, int type, int len,
23857 + int lnum, int offs)
23858 +{
23859 + int err, l;
23860 + struct ubifs_ch *ch = buf;
23861 +
23862 + dbg_io("LEB %d:%d, %s, length %d", lnum, offs, dbg_ntype(type), len);
23863 + ubifs_assert(lnum >= 0 && lnum < c->leb_cnt && offs >= 0);
23864 + ubifs_assert(len >= UBIFS_CH_SZ && offs + len <= c->leb_size);
23865 + ubifs_assert(!(offs & 7) && offs < c->leb_size);
23866 + ubifs_assert(type >= 0 && type < UBIFS_NODE_TYPES_CNT);
23867 +
23868 + err = ubi_read(c->ubi, lnum, buf, offs, len);
23869 + if (err && err != -EBADMSG) {
23870 + ubifs_err("cannot read node %d from LEB %d:%d, error %d",
23871 + type, lnum, offs, err);
23872 + return err;
23873 + }
23874 +
23875 + err = ubifs_check_node(c, buf, lnum, offs, 0);
23876 + if (err) {
23877 + ubifs_err("expected node type %d", type);
23878 + return err;
23879 + }
23880 +
23881 + if (type != ch->node_type) {
23882 + ubifs_err("bad node type (%d but expected %d)",
23883 + ch->node_type, type);
23884 + goto out;
23885 + }
23886 +
23887 + l = le32_to_cpu(ch->len);
23888 + if (l != len) {
23889 + ubifs_err("bad node length %d, expected %d", l, len);
23890 + goto out;
23891 + }
23892 +
23893 + return 0;
23894 +
23895 +out:
23896 + ubifs_err("bad node at LEB %d:%d", lnum, offs);
23897 + dbg_dump_node(c, buf);
23898 + dbg_dump_stack();
23899 + return -EINVAL;
23900 +}
23901 +
23902 +/**
23903 + * ubifs_wbuf_init - initialize write-buffer.
23904 + * @c: UBIFS file-system description object
23905 + * @wbuf: write-buffer to initialize
23906 + *
23907 + * This function initializes write buffer. Returns zero in case of success
23908 + * %-ENOMEM in case of failure.
23909 + */
23910 +int ubifs_wbuf_init(struct ubifs_info *c, struct ubifs_wbuf *wbuf)
23911 +{
23912 + size_t size;
23913 +
23914 + wbuf->buf = kmalloc(c->min_io_size, GFP_KERNEL);
23915 + if (!wbuf->buf)
23916 + return -ENOMEM;
23917 +
23918 + size = (c->min_io_size / UBIFS_CH_SZ + 1) * sizeof(ino_t);
23919 + wbuf->inodes = kmalloc(size, GFP_KERNEL);
23920 + if (!wbuf->inodes) {
23921 + kfree(wbuf->buf);
23922 + wbuf->buf = NULL;
23923 + return -ENOMEM;
23924 + }
23925 +
23926 + wbuf->used = 0;
23927 + wbuf->lnum = wbuf->offs = -1;
23928 + wbuf->avail = c->min_io_size;
23929 + wbuf->dtype = UBI_UNKNOWN;
23930 + wbuf->sync_callback = NULL;
23931 + mutex_init(&wbuf->io_mutex);
23932 + spin_lock_init(&wbuf->lock);
23933 +
23934 + wbuf->c = c;
23935 + init_timer(&wbuf->timer);
23936 + wbuf->timer.function = wbuf_timer_callback_nolock;
23937 + wbuf->timer.data = (unsigned long)wbuf;
23938 + wbuf->timeout = DEFAULT_WBUF_TIMEOUT;
23939 + wbuf->next_ino = 0;
23940 +
23941 + return 0;
23942 +}
23943 +
23944 +/**
23945 + * ubifs_wbuf_add_ino_nolock - add an inode number into the wbuf inode array.
23946 + * @wbuf: the write-buffer whereto add
23947 + * @inum: the inode number
23948 + *
23949 + * This function adds an inode number to the inode array of the write-buffer.
23950 + */
23951 +void ubifs_wbuf_add_ino_nolock(struct ubifs_wbuf *wbuf, ino_t inum)
23952 +{
23953 + if (!wbuf->buf)
23954 + /* NOR flash or something similar */
23955 + return;
23956 +
23957 + spin_lock(&wbuf->lock);
23958 + if (wbuf->used)
23959 + wbuf->inodes[wbuf->next_ino++] = inum;
23960 + spin_unlock(&wbuf->lock);
23961 +}
23962 +
23963 +/**
23964 + * wbuf_has_ino - returns if the wbuf contains data from the inode.
23965 + * @wbuf: the write-buffer
23966 + * @inum: the inode number
23967 + *
23968 + * This function returns with %1 if the write-buffer contains some data from the
23969 + * given inode otherwise it returns with %0.
23970 + */
23971 +static int wbuf_has_ino(struct ubifs_wbuf *wbuf, ino_t inum)
23972 +{
23973 + int i, ret = 0;
23974 +
23975 + spin_lock(&wbuf->lock);
23976 + for (i = 0; i < wbuf->next_ino; i++)
23977 + if (inum == wbuf->inodes[i]) {
23978 + ret = 1;
23979 + break;
23980 + }
23981 + spin_unlock(&wbuf->lock);
23982 +
23983 + return ret;
23984 +}
23985 +
23986 +/**
23987 + * ubifs_sync_wbufs_by_inodes - synchronize write-buffers which have data.
23988 + * belonging to specified inodes.
23989 + * @c: UBIFS file-system description object
23990 + * @inodes: array of inodes
23991 + * @count: number of elements in @inodes
23992 + *
23993 + * This function synchronizes write-buffers which contain nodes belonging to
23994 + * any inode specified in @inodes array. Returns zero in case of success and a
23995 + * negative error code in case of failure.
23996 + */
23997 +int ubifs_sync_wbufs_by_inodes(struct ubifs_info *c,
23998 + struct inode * const *inodes, int count)
23999 +{
24000 + int i, j, err = 0;
24001 +
24002 + ubifs_assert(count);
24003 +
24004 + for (i = 0; i < c->jhead_cnt; i++) {
24005 + struct ubifs_wbuf *wbuf = &c->jheads[i].wbuf;
24006 +
24007 + if (i == GCHD)
24008 + /*
24009 + * GC head is special, do not look at it. Even if the
24010 + * head contains something related to this inode, it is
24011 + * a _copy_ of corresponding on-flash node which sits
24012 + * somewhere else.
24013 + */
24014 + continue;
24015 +
24016 + for (j = 0; j < count && !err; j++)
24017 + if (wbuf_has_ino(wbuf, inodes[j]->i_ino)) {
24018 + mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead);
24019 + if (wbuf_has_ino(wbuf, inodes[j]->i_ino))
24020 + err = ubifs_wbuf_sync_nolock(wbuf);
24021 + mutex_unlock(&wbuf->io_mutex);
24022 + break;
24023 + }
24024 +
24025 + if (err) {
24026 + ubifs_ro_mode(c, err);
24027 + break;
24028 + }
24029 + }
24030 +
24031 + return err;
24032 +}
24033 diff --exclude=.git -urN linux-2.6.25.6/fs/ubifs/ioctl.c avr32-2.6/fs/ubifs/ioctl.c
24034 --- linux-2.6.25.6/fs/ubifs/ioctl.c 1970-01-01 01:00:00.000000000 +0100
24035 +++ avr32-2.6/fs/ubifs/ioctl.c 2008-06-12 15:09:45.367815766 +0200
24036 @@ -0,0 +1,204 @@
24037 +/*
24038 + * This file is part of UBIFS.
24039 + *
24040 + * Copyright (C) 2006-2008 Nokia Corporation.
24041 + * Copyright (C) 2006, 2007 University of Szeged, Hungary
24042 + *
24043 + * This program is free software; you can redistribute it and/or modify it
24044 + * under the terms of the GNU General Public License version 2 as published by
24045 + * the Free Software Foundation.
24046 + *
24047 + * This program is distributed in the hope that it will be useful, but WITHOUT
24048 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
24049 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
24050 + * more details.
24051 + *
24052 + * You should have received a copy of the GNU General Public License along with
24053 + * this program; if not, write to the Free Software Foundation, Inc., 51
24054 + * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24055 + *
24056 + * Authors: Zoltan Sogor
24057 + * Artem Bityutskiy (Битюцкий Артём)
24058 + * Adrian Hunter
24059 + */
24060 +
24061 +/* This file implements EXT2-compatible extended attribute ioctl() calls */
24062 +
24063 +#include <linux/compat.h>
24064 +#include <linux/smp_lock.h>
24065 +#include "ubifs.h"
24066 +
24067 +/**
24068 + * ubifs_set_inode_flags - set VFS inode flags.
24069 + * @inode: VFS inode to set flags for
24070 + *
24071 + * This function propagates flags from UBIFS inode object to VFS inode object.
24072 + */
24073 +void ubifs_set_inode_flags(struct inode *inode)
24074 +{
24075 + unsigned int flags = ubifs_inode(inode)->flags;
24076 +
24077 + inode->i_flags &= ~(S_SYNC | S_APPEND | S_IMMUTABLE | S_DIRSYNC);
24078 + if (flags & UBIFS_SYNC_FL)
24079 + inode->i_flags |= S_SYNC;
24080 + if (flags & UBIFS_APPEND_FL)
24081 + inode->i_flags |= S_APPEND;
24082 + if (flags & UBIFS_IMMUTABLE_FL)
24083 + inode->i_flags |= S_IMMUTABLE;
24084 + if (flags & UBIFS_DIRSYNC_FL)
24085 + inode->i_flags |= S_DIRSYNC;
24086 +}
24087 +
24088 +/*
24089 + * ioctl2ubifs - convert ioctl inode flags to UBIFS inode flags.
24090 + * @ioctl_flags: flags to convert
24091 + *
24092 + * This function convert ioctl flags (@FS_COMPR_FL, etc) to UBIFS inode flags
24093 + * (@UBIFS_COMPR_FL, etc).
24094 + */
24095 +static int ioctl2ubifs(int ioctl_flags)
24096 +{
24097 + int ubifs_flags = 0;
24098 +
24099 + if (ioctl_flags & FS_COMPR_FL)
24100 + ubifs_flags |= UBIFS_COMPR_FL;
24101 + if (ioctl_flags & FS_SYNC_FL)
24102 + ubifs_flags |= UBIFS_SYNC_FL;
24103 + if (ioctl_flags & FS_APPEND_FL)
24104 + ubifs_flags |= UBIFS_APPEND_FL;
24105 + if (ioctl_flags & FS_IMMUTABLE_FL)
24106 + ubifs_flags |= UBIFS_IMMUTABLE_FL;
24107 + if (ioctl_flags & FS_DIRSYNC_FL)
24108 + ubifs_flags |= UBIFS_DIRSYNC_FL;
24109 +
24110 + return ubifs_flags;
24111 +}
24112 +
24113 +/*
24114 + * ubifs2ioctl - convert UBIFS inode flags to ioctl inode flags.
24115 + * @ubifs_flags: flags to convert
24116 + *
24117 + * This function convert UBIFS (@UBIFS_COMPR_FL, etc) to ioctl flags
24118 + * (@FS_COMPR_FL, etc).
24119 + */
24120 +static int ubifs2ioctl(int ubifs_flags)
24121 +{
24122 + int ioctl_flags = 0;
24123 +
24124 + if (ubifs_flags & UBIFS_COMPR_FL)
24125 + ioctl_flags |= FS_COMPR_FL;
24126 + if (ubifs_flags & UBIFS_SYNC_FL)
24127 + ioctl_flags |= FS_SYNC_FL;
24128 + if (ubifs_flags & UBIFS_APPEND_FL)
24129 + ioctl_flags |= FS_APPEND_FL;
24130 + if (ubifs_flags & UBIFS_IMMUTABLE_FL)
24131 + ioctl_flags |= FS_IMMUTABLE_FL;
24132 + if (ubifs_flags & UBIFS_DIRSYNC_FL)
24133 + ioctl_flags |= FS_DIRSYNC_FL;
24134 +
24135 + return ioctl_flags;
24136 +}
24137 +
24138 +static int setflags(struct inode *inode, int flags)
24139 +{
24140 + struct ubifs_inode *ui = ubifs_inode(inode);
24141 + struct ubifs_info *c = inode->i_sb->s_fs_info;
24142 + struct ubifs_budget_req req;
24143 + int oldflags, err;
24144 +
24145 + mutex_lock(&inode->i_mutex);
24146 +
24147 + memset(&req, 0 , sizeof(struct ubifs_budget_req));
24148 + err = ubifs_budget_inode_op(c, inode, &req);
24149 + if (err)
24150 + goto out;
24151 +
24152 + /*
24153 + * The IMMUTABLE and APPEND_ONLY flags can only be changed by
24154 + * the relevant capability.
24155 + */
24156 + oldflags = ubifs2ioctl(ui->flags);
24157 + if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
24158 + if (!capable(CAP_LINUX_IMMUTABLE)) {
24159 + err = -EPERM;
24160 + goto out_budg;
24161 + }
24162 + }
24163 +
24164 + ui->flags = ioctl2ubifs(flags);
24165 + ubifs_set_inode_flags(inode);
24166 +
24167 + inode->i_ctime = ubifs_current_time(inode);
24168 + mark_inode_dirty_sync(inode);
24169 +
24170 + ubifs_release_ino_dirty(c, inode, &req);
24171 +
24172 + if (IS_SYNC(inode))
24173 + err = write_inode_now(inode, 1);
24174 +
24175 + mutex_unlock(&inode->i_mutex);
24176 + return err;
24177 +
24178 +out_budg:
24179 + ubifs_cancel_ino_op(c, inode, &req);
24180 +out:
24181 + ubifs_err("can't modify inode %lu attributes", inode->i_ino);
24182 + mutex_unlock(&inode->i_mutex);
24183 + return err;
24184 +}
24185 +
24186 +long ubifs_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
24187 +{
24188 + int flags;
24189 + struct inode *inode = file->f_path.dentry->d_inode;
24190 +
24191 + switch (cmd) {
24192 + case FS_IOC_GETFLAGS:
24193 + flags = ubifs2ioctl(ubifs_inode(inode)->flags);
24194 +
24195 + return put_user(flags, (int __user *) arg);
24196 +
24197 + case FS_IOC_SETFLAGS: {
24198 + if (IS_RDONLY(inode))
24199 + return -EROFS;
24200 +
24201 + if (!is_owner_or_cap(inode))
24202 + return -EACCES;
24203 +
24204 + if (get_user(flags, (int __user *) arg))
24205 + return -EFAULT;
24206 +
24207 + if (!S_ISDIR(inode->i_mode))
24208 + flags &= ~FS_DIRSYNC_FL;
24209 +
24210 + return setflags(inode, flags);
24211 + }
24212 +
24213 + default:
24214 + return -ENOTTY;
24215 + }
24216 +}
24217 +
24218 +#ifdef CONFIG_COMPAT
24219 +long ubifs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
24220 +{
24221 + int err;
24222 +
24223 + switch (cmd) {
24224 + case FS_IOC32_GETFLAGS:
24225 + cmd = FS_IOC_GETFLAGS;
24226 + break;
24227 + case FS_IOC32_SETFLAGS:
24228 + cmd = FS_IOC_SETFLAGS;
24229 + break;
24230 + default:
24231 + return -ENOIOCTLCMD;
24232 + }
24233 +
24234 + lock_kernel();
24235 + err = ubifs_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
24236 + unlock_kernel();
24237 +
24238 + return err;
24239 +}
24240 +#endif
24241 diff --exclude=.git -urN linux-2.6.25.6/fs/ubifs/journal.c avr32-2.6/fs/ubifs/journal.c
24242 --- linux-2.6.25.6/fs/ubifs/journal.c 1970-01-01 01:00:00.000000000 +0100
24243 +++ avr32-2.6/fs/ubifs/journal.c 2008-06-12 15:09:45.367815766 +0200
24244 @@ -0,0 +1,1286 @@
24245 +/*
24246 + * This file is part of UBIFS.
24247 + *
24248 + * Copyright (C) 2006-2008 Nokia Corporation.
24249 + *
24250 + * This program is free software; you can redistribute it and/or modify it
24251 + * under the terms of the GNU General Public License version 2 as published by
24252 + * the Free Software Foundation.
24253 + *
24254 + * This program is distributed in the hope that it will be useful, but WITHOUT
24255 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
24256 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
24257 + * more details.
24258 + *
24259 + * You should have received a copy of the GNU General Public License along with
24260 + * this program; if not, write to the Free Software Foundation, Inc., 51
24261 + * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24262 + *
24263 + * Authors: Artem Bityutskiy (Битюцкий Артём)
24264 + * Adrian Hunter
24265 + */
24266 +
24267 +/*
24268 + * This file implements UBIFS journal.
24269 + *
24270 + * The journal consists of 2 parts - the log and bud LEBs. The log has fixed
24271 + * length and position, while a bud logical eraseblock is any LEB in the main
24272 + * area. Buds contain file system data - data nodes, inode nodes, etc. The log
24273 + * contains only references to buds and some other stuff like commit
24274 + * start node. The idea is that when we commit the journal, we do
24275 + * not copy the data, the buds just become indexed. Since after the commit the
24276 + * nodes in bud eraseblocks become leaf nodes of the file system index tree, we
24277 + * use term "bud". Analogy is obvious, bud eraseblocks contain nodes which will
24278 + * become leafs in the future.
24279 + *
24280 + * The journal is multi-headed because we want to write data to the journal as
24281 + * optimally as possible. It is nice to have nodes belonging to the same inode
24282 + * in one LEB, so we may write data owned by different inodes to different
24283 + * journal heads, although at present only one data head is used.
24284 + *
24285 + * For recovery reasons, the base head contains all inode nodes, all directory
24286 + * entry nodes and all truncate nodes. This means that the other heads contain
24287 + * only data nodes.
24288 + *
24289 + * Bud LEBs may be half-indexed. For example, if the bud was not full at the
24290 + * time of commit, the bud is retained to continue to be used in the journal,
24291 + * even though the "front" of the LEB is now indexed. In that case, the log
24292 + * reference contains the offset where the bud starts for the purposes of the
24293 + * journal.
24294 + *
24295 + * The journal size has to be limited, because the larger is the journal, the
24296 + * longer it takes to mount UBIFS (scanning the journal) and the more memory it
24297 + * takes (indexing in the TNC).
24298 + *
24299 + * Note, all the journal write operations like 'ubifs_jnl_update()' here, which
24300 + * write multiple UBIFS nodes to the journal at one go, are atomic with respect
24301 + * to unclean reboots. Should the unclean reboot happen, the recovery code drops
24302 + * all the nodes.
24303 + */
24304 +
24305 +#include "ubifs.h"
24306 +
24307 +/**
24308 + * zero_ino_node_unused - zero out unused fields of an on-flash inode node.
24309 + * @ino: the inode to zero out
24310 + */
24311 +static inline void zero_ino_node_unused(struct ubifs_ino_node *ino)
24312 +{
24313 + memset(ino->padding, 0, 26);
24314 +}
24315 +
24316 +/**
24317 + * zero_dent_node_unused - zero out unused fields of an on-flash directory
24318 + * entry node.
24319 + * @ino: the directory entry to zero out
24320 + */
24321 +static inline void zero_dent_node_unused(struct ubifs_dent_node *dent)
24322 +{
24323 + dent->padding1 = 0;
24324 + memset(dent->padding2, 0, 4);
24325 +}
24326 +
24327 +/**
24328 + * zero_data_node_unused - zero out unused fields of an on-flash data node.
24329 + * @ino: the data node to zero out
24330 + */
24331 +static inline void zero_data_node_unused(struct ubifs_data_node *data)
24332 +{
24333 + memset(data->padding, 0, 2);
24334 +}
24335 +
24336 +/**
24337 + * zero_trun_node_unused - zero out unused fields of an on-flash truncation
24338 + * node.
24339 + * @ino: the truncation node to zero out
24340 + */
24341 +static inline void zero_trun_node_unused(struct ubifs_trun_node *trun)
24342 +{
24343 + memset(trun->padding, 0, 12);
24344 +}
24345 +
24346 +/**
24347 + * reserve_space - reserve space in the journal.
24348 + * @c: UBIFS file-system description object
24349 + * @jhead: journal head number
24350 + * @len: node length
24351 + *
24352 + * This function reserves space in journal head @head. If the reservation
24353 + * succeeded, the journal head stays locked and later has to be unlocked using
24354 + * 'release_head()'. 'write_node()' and 'write_head()' functions also unlock
24355 + * it. Returns zero in case of success, %-EAGAIN if commit has to be done, and
24356 + * other negative error codes in case of other failures.
24357 + */
24358 +static int reserve_space(struct ubifs_info *c, int jhead, int len)
24359 +{
24360 + int err = 0, err1, retries = 0, avail, lnum, offs, free, squeeze;
24361 + struct ubifs_wbuf *wbuf = &c->jheads[jhead].wbuf;
24362 +
24363 + /*
24364 + * Typically, the base head has smaller nodes written to it, so it is
24365 + * better to try to allocate space at the ends of eraseblocks. This is
24366 + * what the squeeze parameter does.
24367 + */
24368 + squeeze = (jhead == BASEHD);
24369 +again:
24370 + mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead);
24371 + avail = c->leb_size - wbuf->offs - wbuf->used;
24372 +
24373 + if (wbuf->lnum != -1 && avail >= len)
24374 + return 0;
24375 +
24376 + /*
24377 + * Write buffer wasn't seek'ed or there is no enough space - look for an
24378 + * LEB with some empty space.
24379 + */
24380 + lnum = ubifs_find_free_space(c, len, &free, squeeze);
24381 + if (lnum >= 0) {
24382 + /* Found an LEB, add it to the journal head */
24383 + offs = c->leb_size - free;
24384 + err = ubifs_add_bud_to_log(c, jhead, lnum, offs);
24385 + if (err)
24386 + goto out_return;
24387 + /* A new bud was successfully allocated and added to the log */
24388 + goto out;
24389 + }
24390 +
24391 + err = lnum;
24392 + if (err != -ENOSPC)
24393 + goto out_unlock;
24394 +
24395 + /*
24396 + * No free space, we have to run garbage collector to make
24397 + * some. But the write-buffer mutex has to be unlocked because
24398 + * GC have to sync write buffers, which may lead a deadlock.
24399 + */
24400 + dbg_jnl("no free space jhead %d, run GC", jhead);
24401 + mutex_unlock(&wbuf->io_mutex);
24402 +
24403 + lnum = ubifs_garbage_collect(c, 0);
24404 + if (lnum < 0) {
24405 + err = lnum;
24406 + if (err != -ENOSPC)
24407 + return err;
24408 +
24409 + /*
24410 + * GC could not make a free LEB. But someone else may
24411 + * have allocated new bud for this journal head,
24412 + * because we dropped the 'io_mutex', so try once
24413 + * again.
24414 + */
24415 + dbg_jnl("GC couldn't make a free LEB for jhead %d", jhead);
24416 + if (retries++ < 2) {
24417 + dbg_jnl("retry (%d)", retries);
24418 + goto again;
24419 + }
24420 +
24421 + dbg_jnl("return -ENOSPC");
24422 + return err;
24423 + }
24424 +
24425 + mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead);
24426 + dbg_jnl("got LEB %d for jhead %d", lnum, jhead);
24427 + avail = c->leb_size - wbuf->offs - wbuf->used;
24428 +
24429 + if (wbuf->lnum != -1 && avail >= len) {
24430 + /*
24431 + * Someone else has switched the journal head and we have
24432 + * enough space now. This happens when more then one process is
24433 + * trying to write to the same journal head at the same time.
24434 + */
24435 + dbg_jnl("return LEB %d back, already have LEB %d:%d",
24436 + lnum, wbuf->lnum, wbuf->offs + wbuf->used);
24437 + err = ubifs_return_leb(c, lnum);
24438 + if (err)
24439 + goto out_unlock;
24440 + return 0;
24441 + }
24442 +
24443 + err = ubifs_add_bud_to_log(c, jhead, lnum, 0);
24444 + if (err)
24445 + goto out_return;
24446 + offs = 0;
24447 +
24448 +out:
24449 + err = ubifs_wbuf_seek_nolock(wbuf, lnum, offs, UBI_SHORTTERM);
24450 + if (err)
24451 + goto out_unlock;
24452 +
24453 + return 0;
24454 +
24455 +out_unlock:
24456 + mutex_unlock(&wbuf->io_mutex);
24457 + return err;
24458 +
24459 +out_return:
24460 + /* An error occurred and the LEB has to be returned to lprops */
24461 + ubifs_assert(err < 0);
24462 + err1 = ubifs_return_leb(c, lnum);
24463 + if (err1 && err == -EAGAIN)
24464 + /*
24465 + * Return original error code 'err' only if it is not
24466 + * '-EAGAIN', which is not really an error. Otherwise, return
24467 + * the error code of 'ubifs_return_leb()'.
24468 + */
24469 + err = err1;
24470 + mutex_unlock(&wbuf->io_mutex);
24471 + return err;
24472 +}
24473 +
24474 +/**
24475 + * write_node - write node to a journal head.
24476 + * @c: UBIFS file-system description object
24477 + * @jhead: journal head
24478 + * @node: node to write
24479 + * @len: node length
24480 + * @lnum: LEB number written is returned here
24481 + * @offs: offset written is returned here
24482 + *
24483 + * This function writes a node to reserved space of journal head @jhead.
24484 + * Returns zero in case of success and a negative error code in case of
24485 + * failure.
24486 + */
24487 +static int write_node(struct ubifs_info *c, int jhead, void *node, int len,
24488 + int *lnum, int *offs)
24489 +{
24490 + struct ubifs_wbuf *wbuf = &c->jheads[jhead].wbuf;
24491 +
24492 + ubifs_assert(jhead != GCHD);
24493 +
24494 + *lnum = c->jheads[jhead].wbuf.lnum;
24495 + *offs = c->jheads[jhead].wbuf.offs + c->jheads[jhead].wbuf.used;
24496 +
24497 + dbg_jnl("jhead %d, LEB %d:%d, len %d", jhead, *lnum, *offs, len);
24498 + ubifs_prepare_node(c, node, len, 0);
24499 +
24500 + return ubifs_wbuf_write_nolock(wbuf, node, len);
24501 +}
24502 +
24503 +/**
24504 + * write_head - write data to a journal head.
24505 + * @c: UBIFS file-system description object
24506 + * @jhead: journal head
24507 + * @buf: buffer to write
24508 + * @len: length to write
24509 + * @lnum: LEB number written is returned here
24510 + * @offs: offset written is returned here
24511 + * @sync: non-zero if the write-buffer has to by synchronized
24512 + *
24513 + * This function is the same as 'write_node()' but it does not assume the
24514 + * buffer it is writing is a node, so it does not prepare it (which means
24515 + * initializing common header and calculating CRC).
24516 + */
24517 +static int write_head(struct ubifs_info *c, int jhead, void *buf, int len,
24518 + int *lnum, int *offs, int sync)
24519 +{
24520 + int err;
24521 + struct ubifs_wbuf *wbuf = &c->jheads[jhead].wbuf;
24522 +
24523 + ubifs_assert(jhead != GCHD);
24524 +
24525 + *lnum = c->jheads[jhead].wbuf.lnum;
24526 + *offs = c->jheads[jhead].wbuf.offs + c->jheads[jhead].wbuf.used;
24527 + dbg_jnl("jhead %d, LEB %d:%d, len %d", jhead, *lnum, *offs, len);
24528 +
24529 + err = ubifs_wbuf_write_nolock(wbuf, buf, len);
24530 + if (err)
24531 + return err;
24532 + if (sync)
24533 + err = ubifs_wbuf_sync_nolock(wbuf);
24534 + return err;
24535 +}
24536 +
24537 +/**
24538 + * make_reservation - reserve journal space.
24539 + * @c: UBIFS file-system description object
24540 + * @jhead: journal head
24541 + * @len: how many bytes to reserve
24542 + *
24543 + * This function makes space reservation in journal head @jhead. The function
24544 + * takes the commit lock and locks the journal head, and the caller has to
24545 + * unlock the head and finish the reservation with 'finish_reservation()'.
24546 + * Returns zero in case of success and a negative error code in case of
24547 + * failure.
24548 + *
24549 + * Note, the journal head may be unlocked as soon as the data is written, while
24550 + * the commit lock has to be released after the data has been added to the
24551 + * TNC.
24552 + */
24553 +static int make_reservation(struct ubifs_info *c, int jhead, int len)
24554 +{
24555 + int err, cmt_retries = 0, nospc_retries = 0;
24556 +
24557 + ubifs_assert(len <= c->dark_wm);
24558 +
24559 +again:
24560 + down_read(&c->commit_sem);
24561 + err = reserve_space(c, jhead, len);
24562 + if (!err)
24563 + return 0;
24564 + up_read(&c->commit_sem);
24565 +
24566 + if (err == -ENOSPC) {
24567 + /*
24568 + * GC could not make any progress. We should try to commit
24569 + * once because it could make some dirty space and GC would
24570 + * make progress, so make the error -EAGAIN so that the below
24571 + * will commit and re-try.
24572 + */
24573 + if (nospc_retries++ < 2) {
24574 + dbg_jnl("no space, retry");
24575 + err = -EAGAIN;
24576 + }
24577 +
24578 + /*
24579 + * This means that the budgeting is incorrect. We always have
24580 + * to be able to write to the media, because all operations are
24581 + * budgeted. Deletions are not budgeted, though, but we reserve
24582 + * an extra LEB for them.
24583 + */
24584 + }
24585 +
24586 + if (err != -EAGAIN)
24587 + goto out;
24588 +
24589 + /*
24590 + * -EAGAIN means that the journal is full or too large, or the above
24591 + * code wants to do one commit. Do this and re-try.
24592 + */
24593 + if (cmt_retries > 128) {
24594 + /*
24595 + * This should not happen unless the journal size limitations
24596 + * are too tough.
24597 + */
24598 + ubifs_err("stuck in space allocation");
24599 + err = -ENOSPC;
24600 + goto out;
24601 + } else if (cmt_retries > 32)
24602 + ubifs_warn("too many space allocation re-tries (%d)",
24603 + cmt_retries);
24604 +
24605 + dbg_jnl("-EAGAIN, commit and retry (retried %d times)",
24606 + cmt_retries);
24607 + cmt_retries += 1;
24608 +
24609 + err = ubifs_run_commit(c);
24610 + if (err)
24611 + return err;
24612 + goto again;
24613 +
24614 +out:
24615 + ubifs_err("cannot reserve %d bytes in jhead %d, error %d",
24616 + len, jhead, err);
24617 + if (err == -ENOSPC) {
24618 + /* This are some budgeting problems, print useful information */
24619 + down_write(&c->commit_sem);
24620 + spin_lock(&c->space_lock);
24621 + dbg_dump_stack();
24622 + dbg_dump_budg(c);
24623 + spin_unlock(&c->space_lock);
24624 + dbg_dump_lprops(c);
24625 + cmt_retries = dbg_check_lprops(c);
24626 + up_write(&c->commit_sem);
24627 + }
24628 +
24629 + return err;
24630 +}
24631 +
24632 +/**
24633 + * release_head - release a journal head.
24634 + * @c: UBIFS file-system description object
24635 + * @jhead: journal head
24636 + *
24637 + * This function releases journal head @jhead which was locked by
24638 + * the 'make_reservation()' function. It has to be called after each successful
24639 + * 'make_reservation()' invocation.
24640 + */
24641 +static inline void release_head(struct ubifs_info *c, int jhead)
24642 +{
24643 + mutex_unlock(&c->jheads[jhead].wbuf.io_mutex);
24644 +}
24645 +
24646 +/**
24647 + * finish_reservation - finish a reservation.
24648 + * @c: UBIFS file-system description object
24649 + *
24650 + * This function finishes journal space reservation. It must be called after
24651 + * 'make_reservation()'.
24652 + */
24653 +static void finish_reservation(struct ubifs_info *c)
24654 +{
24655 + up_read(&c->commit_sem);
24656 +}
24657 +
24658 +/**
24659 + * get_dent_type - translate VFS inode mode to UBIFS directory entry type.
24660 + * @mode: inode mode
24661 + */
24662 +static int get_dent_type(int mode)
24663 +{
24664 + switch (mode & S_IFMT) {
24665 + case S_IFREG:
24666 + return UBIFS_ITYPE_REG;
24667 + case S_IFDIR:
24668 + return UBIFS_ITYPE_DIR;
24669 + case S_IFLNK:
24670 + return UBIFS_ITYPE_LNK;
24671 + case S_IFBLK:
24672 + return UBIFS_ITYPE_BLK;
24673 + case S_IFCHR:
24674 + return UBIFS_ITYPE_CHR;
24675 + case S_IFIFO:
24676 + return UBIFS_ITYPE_FIFO;
24677 + case S_IFSOCK:
24678 + return UBIFS_ITYPE_SOCK;
24679 + default:
24680 + BUG();
24681 + }
24682 + return 0;
24683 +}
24684 +
24685 +/**
24686 + * pack_inode - pack an inode node.
24687 + * @c: UBIFS file-system description object
24688 + * @ino: buffer in which to pack inode node
24689 + * @inode: inode to pack
24690 + * @last: indicates the last node of the group
24691 + * @last_reference: non-zero if this is a deletion inode
24692 + */
24693 +static void pack_inode(struct ubifs_info *c, struct ubifs_ino_node *ino,
24694 + const struct inode *inode, int last, int last_reference)
24695 +{
24696 + int data_len = 0;
24697 + struct ubifs_inode *ui = ubifs_inode(inode);
24698 +
24699 + ino->ch.node_type = UBIFS_INO_NODE;
24700 + ino_key_init_flash(c, &ino->key, inode->i_ino);
24701 + ino->creat_sqnum = cpu_to_le64(ui->creat_sqnum);
24702 + ino->size = cpu_to_le64(i_size_read(inode));
24703 + ino->nlink = cpu_to_le32(inode->i_nlink);
24704 + ino->atime_sec = cpu_to_le64(inode->i_atime.tv_sec);
24705 + ino->atime_nsec = cpu_to_le32(inode->i_atime.tv_nsec);
24706 + ino->ctime_sec = cpu_to_le64(inode->i_ctime.tv_sec);
24707 + ino->ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
24708 + ino->mtime_sec = cpu_to_le64(inode->i_mtime.tv_sec);
24709 + ino->mtime_nsec = cpu_to_le32(inode->i_mtime.tv_nsec);
24710 + ino->uid = cpu_to_le32(inode->i_uid);
24711 + ino->gid = cpu_to_le32(inode->i_gid);
24712 + ino->mode = cpu_to_le32(inode->i_mode);
24713 + ino->flags = cpu_to_le32(ui->flags);
24714 + ino->compr_type = cpu_to_le16(ui->compr_type);
24715 + ino->xattr_cnt = cpu_to_le32(ui->xattr_cnt);
24716 + ino->xattr_size = cpu_to_le64(ui->xattr_size);
24717 + ino->xattr_names = cpu_to_le32(ui->xattr_names);
24718 + ino->data_len = cpu_to_le32(ui->data_len);
24719 + zero_ino_node_unused(ino);
24720 +
24721 + /*
24722 + * Drop the attached data if this is a deletion inode, the data is not
24723 + * needed anymore.
24724 + */
24725 + if (!last_reference) {
24726 + memcpy(ino->data, ui->data, ui->data_len);
24727 + data_len = ui->data_len;
24728 + }
24729 +
24730 + ubifs_prep_grp_node(c, ino, UBIFS_INO_NODE_SZ + data_len, last);
24731 +}
24732 +
24733 +/**
24734 + * ubifs_jnl_update - update inode.
24735 + * @c: UBIFS file-system description object
24736 + * @dir: parent inode or host inode in case of extended attributes
24737 + * @nm: directory entry name
24738 + * @inode: inode
24739 + * @deletion: indicates a directory entry deletion i.e unlink or rmdir
24740 + * @sync: non-zero if the write-buffer has to be synchronized
24741 + * @xent: non-zero if the directory entry is an extended attribute entry
24742 + *
24743 + * This function updates an inode by writing a directory entry (or extended
24744 + * attribute entry), the inode itself, and the parent directory inode (or the
24745 + * host inode) to the journal.
24746 + *
24747 + * The function writes the host inode @dir last, which is important in case of
24748 + * extended attributes. Indeed, then we guarantee that if the host inode gets
24749 + * synchronized, and the write-buffer it sits in gets flushed, the extended
24750 + * attribute inode gets flushed too. And this is exactly what the user expects -
24751 + * synchronizing the host inode synchronizes its extended attributes.
24752 + * Similarly, this guarantees that if @dir is synchronized, its directory entry
24753 + * corresponding to @nm gets synchronized too.
24754 + *
24755 + * This function returns %0 on success and a negative error code on failure.
24756 + */
24757 +int ubifs_jnl_update(struct ubifs_info *c, const struct inode *dir,
24758 + const struct qstr *nm, const struct inode *inode,
24759 + int deletion, int sync, int xent)
24760 +{
24761 + int err, dlen, ilen, len, lnum, ino_offs, dent_offs;
24762 + int aligned_dlen, aligned_ilen;
24763 + int last_reference = !!(deletion && inode->i_nlink == 0);
24764 + struct ubifs_dent_node *dent;
24765 + struct ubifs_ino_node *ino;
24766 + union ubifs_key dent_key, ino_key;
24767 +
24768 + dbg_jnl("ino %lu, dent '%.*s', data len %d in dir ino %lu",
24769 + inode->i_ino, nm->len, nm->name, ubifs_inode(inode)->data_len,
24770 + dir->i_ino);
24771 + ubifs_assert(ubifs_inode(dir)->data_len == 0);
24772 +
24773 + dlen = UBIFS_DENT_NODE_SZ + nm->len + 1;
24774 + ilen = UBIFS_INO_NODE_SZ;
24775 +
24776 + /*
24777 + * If the last reference to the inode is being deleted, then there is no
24778 + * need to attach and write inode data, it is being deleted anyway.
24779 + */
24780 + if (!last_reference)
24781 + ilen += ubifs_inode(inode)->data_len;
24782 +
24783 + aligned_dlen = ALIGN(dlen, 8);
24784 + aligned_ilen = ALIGN(ilen, 8);
24785 +
24786 + len = aligned_dlen + aligned_ilen + UBIFS_INO_NODE_SZ;
24787 +
24788 + dent = kmalloc(len, GFP_NOFS);
24789 + if (!dent)
24790 + return -ENOMEM;
24791 +
24792 + /* Make reservation before allocating sequence numbers */
24793 + err = make_reservation(c, BASEHD, len);
24794 + if (err)
24795 + goto out_free;
24796 +
24797 + if (!xent) {
24798 + dent->ch.node_type = UBIFS_DENT_NODE;
24799 + dent_key_init(c, &dent_key, dir->i_ino, nm);
24800 + } else {
24801 + dent->ch.node_type = UBIFS_XENT_NODE;
24802 + xent_key_init(c, &dent_key, dir->i_ino, nm);
24803 + }
24804 +
24805 + key_write(c, &dent_key, dent->key);
24806 + dent->inum = deletion ? 0 : cpu_to_le64(inode->i_ino);
24807 + dent->type = get_dent_type(inode->i_mode);
24808 + dent->nlen = cpu_to_le16(nm->len);
24809 + memcpy(dent->name, nm->name, nm->len);
24810 + dent->name[nm->len] = '\0';
24811 + zero_dent_node_unused(dent);
24812 + ubifs_prep_grp_node(c, dent, dlen, 0);
24813 +
24814 + ino = (void *)dent + aligned_dlen;
24815 + pack_inode(c, ino, inode, 0, last_reference);
24816 +
24817 + ino = (void *)ino + aligned_ilen;
24818 + pack_inode(c, ino, dir, 1, 0);
24819 +
24820 + if (last_reference) {
24821 + err = ubifs_add_orphan(c, inode->i_ino);
24822 + if (err) {
24823 + release_head(c, BASEHD);
24824 + goto out_finish;
24825 + }
24826 + }
24827 +
24828 + err = write_head(c, BASEHD, dent, len, &lnum, &dent_offs, sync);
24829 + if (!sync && !err) {
24830 + struct ubifs_wbuf *wbuf = &c->jheads[BASEHD].wbuf;
24831 +
24832 + ubifs_wbuf_add_ino_nolock(wbuf, inode->i_ino);
24833 + ubifs_wbuf_add_ino_nolock(wbuf, dir->i_ino);
24834 + }
24835 + release_head(c, BASEHD);
24836 + kfree(dent);
24837 + if (err)
24838 + goto out_ro;
24839 +
24840 + if (deletion) {
24841 + err = ubifs_tnc_remove_nm(c, &dent_key, nm);
24842 + if (err)
24843 + goto out_ro;
24844 + err = ubifs_add_dirt(c, lnum, dlen);
24845 + } else
24846 + err = ubifs_tnc_add_nm(c, &dent_key, lnum, dent_offs, dlen, nm);
24847 + if (err)
24848 + goto out_ro;
24849 +
24850 + /*
24851 + * Note, we do not remove the inode from TNC even if the last reference
24852 + * to it has just been deleted, because the inode may still be opened.
24853 + * Instead, the inode has been added to orphan lists and the orphan
24854 + * subsystem will take further care about it.
24855 + */
24856 + ino_key_init(c, &ino_key, inode->i_ino);
24857 + ino_offs = dent_offs + aligned_dlen;
24858 + err = ubifs_tnc_add(c, &ino_key, lnum, ino_offs, ilen);
24859 + if (err)
24860 + goto out_ro;
24861 +
24862 + ino_key_init(c, &ino_key, dir->i_ino);
24863 + ino_offs += aligned_ilen;
24864 + err = ubifs_tnc_add(c, &ino_key, lnum, ino_offs, UBIFS_INO_NODE_SZ);
24865 + if (err)
24866 + goto out_ro;
24867 +
24868 + finish_reservation(c);
24869 + return 0;
24870 +
24871 +out_finish:
24872 + finish_reservation(c);
24873 +out_free:
24874 + kfree(dent);
24875 + return err;
24876 +
24877 +out_ro:
24878 + ubifs_ro_mode(c, err);
24879 + if (last_reference)
24880 + ubifs_delete_orphan(c, inode->i_ino);
24881 + finish_reservation(c);
24882 + return err;
24883 +}
24884 +
24885 +/**
24886 + * ubifs_jnl_write_data - write a data node to the journal.
24887 + * @c: UBIFS file-system description object
24888 + * @inode: inode the data node belongs to
24889 + * @key: node key
24890 + * @buf: buffer to write
24891 + * @len: data length (must not exceed %UBIFS_BLOCK_SIZE)
24892 + *
24893 + * This function writes a data node to the journal. Returns %0 if the data node
24894 + * was successfully written, and a negative error code in case of failure.
24895 + */
24896 +int ubifs_jnl_write_data(struct ubifs_info *c, const struct inode *inode,
24897 + const union ubifs_key *key, const void *buf, int len)
24898 +{
24899 + int err, lnum, offs, compr_type, out_len;
24900 + int dlen = UBIFS_DATA_NODE_SZ + UBIFS_BLOCK_SIZE * WORST_COMPR_FACTOR;
24901 + const struct ubifs_inode *ui = ubifs_inode(inode);
24902 + struct ubifs_data_node *data;
24903 +
24904 + dbg_jnl("ino %lu, blk %u, len %d, key %s", key_inum(c, key),
24905 + key_block(c, key), len, DBGKEY(key));
24906 + ubifs_assert(len <= UBIFS_BLOCK_SIZE);
24907 +
24908 + data = kmalloc(dlen, GFP_NOFS);
24909 + if (!data)
24910 + return -ENOMEM;
24911 +
24912 + data->ch.node_type = UBIFS_DATA_NODE;
24913 + key_write(c, key, &data->key);
24914 + data->size = cpu_to_le32(len);
24915 + zero_data_node_unused(data);
24916 +
24917 + if (!(ui->flags && UBIFS_COMPR_FL))
24918 + /* Compression is disabled for this inode */
24919 + compr_type = UBIFS_COMPR_NONE;
24920 + else
24921 + compr_type = ui->compr_type;
24922 +
24923 + out_len = dlen - UBIFS_DATA_NODE_SZ;
24924 + ubifs_compress(buf, len, &data->data, &out_len, &compr_type);
24925 + ubifs_assert(out_len <= UBIFS_BLOCK_SIZE);
24926 +
24927 + dlen = UBIFS_DATA_NODE_SZ + out_len;
24928 + data->compr_type = cpu_to_le16(compr_type);
24929 +
24930 + /* Make reservation before allocating sequence numbers */
24931 + err = make_reservation(c, DATAHD, dlen);
24932 + if (err)
24933 + goto out_free;
24934 +
24935 + err = write_node(c, DATAHD, data, dlen, &lnum, &offs);
24936 + if (!err)
24937 + ubifs_wbuf_add_ino_nolock(&c->jheads[DATAHD].wbuf,
24938 + key_inum(c, key));
24939 + release_head(c, DATAHD);
24940 + if (err)
24941 + goto out_ro;
24942 +
24943 + err = ubifs_tnc_add(c, key, lnum, offs, dlen);
24944 + if (err)
24945 + goto out_ro;
24946 +
24947 + finish_reservation(c);
24948 + kfree(data);
24949 + return 0;
24950 +
24951 +out_ro:
24952 + ubifs_ro_mode(c, err);
24953 + finish_reservation(c);
24954 +out_free:
24955 + kfree(data);
24956 + return err;
24957 +}
24958 +
24959 +/**
24960 + * ubifs_jnl_write_inode - flush inode to the journal.
24961 + * @c: UBIFS file-system description object
24962 + * @inode: inode to flush
24963 + * @last_reference: inode has been deleted
24964 + * @sync: non-zero if the write-buffer has to be synchronized
24965 + *
24966 + * This function writes inode @inode to the journal (to the base head). Returns
24967 + * zero in case of success and a negative error code in case of failure.
24968 + */
24969 +int ubifs_jnl_write_inode(struct ubifs_info *c, const struct inode *inode,
24970 + int last_reference, int sync)
24971 +{
24972 + int err, len, lnum, offs;
24973 + struct ubifs_ino_node *ino;
24974 + struct ubifs_inode *ui = ubifs_inode(inode);
24975 +
24976 + dbg_jnl("ino %lu%s", inode->i_ino,
24977 + last_reference ? " (last reference)" : "");
24978 + if (last_reference)
24979 + ubifs_assert(inode->i_nlink == 0);
24980 +
24981 + /* If the inode is deleted, do not write the attached data */
24982 + len = UBIFS_INO_NODE_SZ;
24983 + if (!last_reference)
24984 + len += ui->data_len;
24985 + ino = kmalloc(len, GFP_NOFS);
24986 + if (!ino)
24987 + return -ENOMEM;
24988 +
24989 + /* Make reservation before allocating sequence numbers */
24990 + err = make_reservation(c, BASEHD, len);
24991 + if (err)
24992 + goto out_free;
24993 +
24994 + pack_inode(c, ino, inode, 1, last_reference);
24995 +
24996 + err = write_head(c, BASEHD, ino, len, &lnum, &offs, sync);
24997 + if (!sync && !err)
24998 + ubifs_wbuf_add_ino_nolock(&c->jheads[BASEHD].wbuf,
24999 + inode->i_ino);
25000 + release_head(c, BASEHD);
25001 + if (err)
25002 + goto out_ro;
25003 +
25004 + if (last_reference) {
25005 + err = ubifs_tnc_remove_ino(c, inode->i_ino);
25006 + if (err)
25007 + goto out_ro;
25008 + ubifs_delete_orphan(c, inode->i_ino);
25009 + err = ubifs_add_dirt(c, lnum, len);
25010 + } else {
25011 + union ubifs_key key;
25012 +
25013 + ino_key_init(c, &key, inode->i_ino);
25014 + err = ubifs_tnc_add(c, &key, lnum, offs, len);
25015 + }
25016 + if (err)
25017 + goto out_ro;
25018 +
25019 + finish_reservation(c);
25020 + kfree(ino);
25021 + return 0;
25022 +
25023 +out_ro:
25024 + ubifs_ro_mode(c, err);
25025 + finish_reservation(c);
25026 +out_free:
25027 + kfree(ino);
25028 + return err;
25029 +}
25030 +
25031 +/**
25032 + * ubifs_jnl_rename - rename a directory entry.
25033 + * @c: UBIFS file-system description object
25034 + * @old_dir: parent inode of directory entry to rename
25035 + * @old_dentry: directory entry to rename
25036 + * @new_dir: parent inode of directory entry to rename
25037 + * @new_dentry: new directory entry (or directory entry to replace)
25038 + * @sync: non-zero if the write-buffer has to be synchronized
25039 + *
25040 + * Returns zero in case of success and a negative error code in case of failure.
25041 + */
25042 +int ubifs_jnl_rename(struct ubifs_info *c, const struct inode *old_dir,
25043 + const struct dentry *old_dentry,
25044 + const struct inode *new_dir,
25045 + const struct dentry *new_dentry, int sync)
25046 +{
25047 + const struct inode *old_inode = old_dentry->d_inode;
25048 + const struct inode *new_inode = new_dentry->d_inode;
25049 + int err, dlen1, dlen2, ilen, lnum, offs, len;
25050 + int aligned_dlen1, aligned_dlen2, plen = UBIFS_INO_NODE_SZ;
25051 + int last_reference = !!(new_inode && new_inode->i_nlink == 0);
25052 + struct ubifs_dent_node *dent, *dent2;
25053 + void *p;
25054 + union ubifs_key key;
25055 +
25056 + dbg_jnl("dent '%.*s' in dir ino %lu to dent '%.*s' in dir ino %lu",
25057 + old_dentry->d_name.len, old_dentry->d_name.name,
25058 + old_dir->i_ino, new_dentry->d_name.len,
25059 + new_dentry->d_name.name, new_dir->i_ino);
25060 +
25061 + ubifs_assert(ubifs_inode(old_dir)->data_len == 0);
25062 + ubifs_assert(ubifs_inode(new_dir)->data_len == 0);
25063 +
25064 + dlen1 = UBIFS_DENT_NODE_SZ + new_dentry->d_name.len + 1;
25065 + dlen2 = UBIFS_DENT_NODE_SZ + old_dentry->d_name.len + 1;
25066 + if (new_inode) {
25067 + ilen = UBIFS_INO_NODE_SZ;
25068 + if (!last_reference)
25069 + ilen += ubifs_inode(new_inode)->data_len;
25070 + } else
25071 + ilen = 0;
25072 +
25073 + aligned_dlen1 = ALIGN(dlen1, 8);
25074 + aligned_dlen2 = ALIGN(dlen2, 8);
25075 +
25076 + len = aligned_dlen1 + aligned_dlen2 + ALIGN(ilen, 8) + ALIGN(plen, 8);
25077 + if (old_dir != new_dir)
25078 + len += plen;
25079 +
25080 + dent = kmalloc(len, GFP_NOFS);
25081 + if (!dent)
25082 + return -ENOMEM;
25083 +
25084 + /* Make reservation before allocating sequence numbers */
25085 + err = make_reservation(c, BASEHD, len);
25086 + if (err)
25087 + goto out_free;
25088 +
25089 + /* Make new dent */
25090 + dent->ch.node_type = UBIFS_DENT_NODE;
25091 + dent_key_init_flash(c, &dent->key, new_dir->i_ino, &new_dentry->d_name);
25092 + dent->inum = cpu_to_le64(old_inode->i_ino);
25093 + dent->type = get_dent_type(old_inode->i_mode);
25094 + dent->nlen = cpu_to_le16(new_dentry->d_name.len);
25095 + memcpy(dent->name, new_dentry->d_name.name, new_dentry->d_name.len);
25096 + dent->name[new_dentry->d_name.len] = '\0';
25097 + zero_dent_node_unused(dent);
25098 + ubifs_prep_grp_node(c, dent, dlen1, 0);
25099 +
25100 + dent2 = (void *)dent + aligned_dlen1;
25101 +
25102 + /* Make deletion dent */
25103 + dent2->ch.node_type = UBIFS_DENT_NODE;
25104 + dent_key_init_flash(c, &dent2->key, old_dir->i_ino,
25105 + &old_dentry->d_name);
25106 + dent2->inum = 0;
25107 + dent2->type = DT_UNKNOWN;
25108 + dent2->nlen = cpu_to_le16(old_dentry->d_name.len);
25109 + memcpy(dent2->name, old_dentry->d_name.name, old_dentry->d_name.len);
25110 + dent2->name[old_dentry->d_name.len] = '\0';
25111 + zero_dent_node_unused(dent2);
25112 + ubifs_prep_grp_node(c, dent2, dlen2, 0);
25113 +
25114 + p = (void *)dent2 + aligned_dlen2;
25115 + if (new_inode) {
25116 + pack_inode(c, p, new_inode, 0, last_reference);
25117 + p += ALIGN(ilen, 8);
25118 + }
25119 +
25120 + if (old_dir == new_dir)
25121 + pack_inode(c, p, old_dir, 1, 0);
25122 + else {
25123 + pack_inode(c, p, old_dir, 0, 0);
25124 + p += ALIGN(plen, 8);
25125 + pack_inode(c, p, new_dir, 1, 0);
25126 + }
25127 +
25128 + if (last_reference) {
25129 + err = ubifs_add_orphan(c, new_inode->i_ino);
25130 + if (err) {
25131 + release_head(c, BASEHD);
25132 + goto out_finish;
25133 + }
25134 + }
25135 +
25136 + err = write_head(c, BASEHD, dent, len, &lnum, &offs, sync);
25137 + if (!sync && !err) {
25138 + struct ubifs_wbuf *wbuf = &c->jheads[BASEHD].wbuf;
25139 +
25140 + ubifs_wbuf_add_ino_nolock(wbuf, new_dir->i_ino);
25141 + ubifs_wbuf_add_ino_nolock(wbuf, old_dir->i_ino);
25142 + if (new_inode)
25143 + ubifs_wbuf_add_ino_nolock(&c->jheads[BASEHD].wbuf,
25144 + new_inode->i_ino);
25145 + }
25146 + release_head(c, BASEHD);
25147 + if (err)
25148 + goto out_ro;
25149 +
25150 + dent_key_init(c, &key, new_dir->i_ino, &new_dentry->d_name);
25151 + err = ubifs_tnc_add_nm(c, &key, lnum, offs, dlen1, &new_dentry->d_name);
25152 + if (err)
25153 + goto out_ro;
25154 +
25155 + err = ubifs_add_dirt(c, lnum, dlen2);
25156 + if (err)
25157 + goto out_ro;
25158 +
25159 + dent_key_init(c, &key, old_dir->i_ino, &old_dentry->d_name);
25160 + err = ubifs_tnc_remove_nm(c, &key, &old_dentry->d_name);
25161 + if (err)
25162 + goto out_ro;
25163 +
25164 + offs += aligned_dlen1 + aligned_dlen2;
25165 + if (new_inode) {
25166 + ino_key_init(c, &key, new_inode->i_ino);
25167 + err = ubifs_tnc_add(c, &key, lnum, offs, ilen);
25168 + if (err)
25169 + goto out_ro;
25170 + offs += ALIGN(ilen, 8);
25171 + }
25172 +
25173 + ino_key_init(c, &key, old_dir->i_ino);
25174 + err = ubifs_tnc_add(c, &key, lnum, offs, plen);
25175 + if (err)
25176 + goto out_ro;
25177 +
25178 + if (old_dir != new_dir) {
25179 + offs += ALIGN(plen, 8);
25180 + ino_key_init(c, &key, new_dir->i_ino);
25181 + err = ubifs_tnc_add(c, &key, lnum, offs, plen);
25182 + if (err)
25183 + goto out_ro;
25184 + }
25185 +
25186 + finish_reservation(c);
25187 + kfree(dent);
25188 + return 0;
25189 +
25190 +out_ro:
25191 + ubifs_ro_mode(c, err);
25192 + if (last_reference)
25193 + ubifs_delete_orphan(c, new_inode->i_ino);
25194 +out_finish:
25195 + finish_reservation(c);
25196 +out_free:
25197 + kfree(dent);
25198 + return err;
25199 +}
25200 +
25201 +/**
25202 + * recomp_data_node - re-compress a truncated data node.
25203 + * @dn: data node to re-compress
25204 + * @new_len: new length
25205 + *
25206 + * This function is used when an inode is truncated and the last data node of
25207 + * the inode has to be re-compressed and re-written.
25208 + */
25209 +static int recomp_data_node(struct ubifs_data_node *dn, int *new_len)
25210 +{
25211 + void *buf;
25212 + int err, len, compr_type, out_len;
25213 +
25214 + out_len = le32_to_cpu(dn->size);
25215 + buf = kmalloc(out_len * WORST_COMPR_FACTOR, GFP_NOFS);
25216 + if (!buf)
25217 + return -ENOMEM;
25218 +
25219 + len = le32_to_cpu(dn->ch.len) - UBIFS_DATA_NODE_SZ;
25220 + compr_type = le16_to_cpu(dn->compr_type);
25221 + err = ubifs_decompress(&dn->data, len, buf, &out_len, compr_type);
25222 + if (err)
25223 + goto out;
25224 +
25225 + ubifs_compress(buf, *new_len, &dn->data, &out_len, &compr_type);
25226 + ubifs_assert(out_len <= UBIFS_BLOCK_SIZE);
25227 + dn->compr_type = cpu_to_le16(compr_type);
25228 + dn->size = cpu_to_le32(*new_len);
25229 + *new_len = UBIFS_DATA_NODE_SZ + out_len;
25230 +out:
25231 + kfree(buf);
25232 + return err;
25233 +}
25234 +
25235 +/**
25236 + * ubifs_jnl_truncate - update the journal for a truncation.
25237 + * @c: UBIFS file-system description object
25238 + * @inum: inode number of inode being truncated
25239 + * @old_size: old size
25240 + * @new_size: new size
25241 + *
25242 + * When the size of a file decreases due to truncation, a truncation node is
25243 + * written, the journal tree is updated, and the last data block is re-written
25244 + * if it has been affected.
25245 + *
25246 + * This function returns %0 in the case of success, and a negative error code in
25247 + * case of failure.
25248 + */
25249 +int ubifs_jnl_truncate(struct ubifs_info *c, ino_t inum,
25250 + loff_t old_size, loff_t new_size)
25251 +{
25252 + union ubifs_key key, to_key;
25253 + struct ubifs_trun_node *trun;
25254 + struct ubifs_data_node *uninitialized_var(dn);
25255 + int err, dlen, len, lnum, offs, bit, sz;
25256 + unsigned int blk;
25257 +
25258 + dbg_jnl("ino %lu, size %lld -> %lld", inum, old_size, new_size);
25259 +
25260 + sz = UBIFS_TRUN_NODE_SZ + UBIFS_MAX_DATA_NODE_SZ * WORST_COMPR_FACTOR;
25261 + trun = kmalloc(sz, GFP_NOFS);
25262 + if (!trun)
25263 + return -ENOMEM;
25264 +
25265 + trun->ch.node_type = UBIFS_TRUN_NODE;
25266 + trun->inum = cpu_to_le32(inum);
25267 + trun->old_size = cpu_to_le64(old_size);
25268 + trun->new_size = cpu_to_le64(new_size);
25269 + zero_trun_node_unused(trun);
25270 +
25271 + dlen = new_size & (UBIFS_BLOCK_SIZE - 1);
25272 +
25273 + if (dlen) {
25274 + /* Get last data block so it can be truncated */
25275 + dn = (void *)trun + ALIGN(UBIFS_TRUN_NODE_SZ, 8);
25276 + blk = new_size / UBIFS_BLOCK_SIZE;
25277 + data_key_init(c, &key, inum, blk);
25278 + dbg_jnl("last block key %s", DBGKEY(&key));
25279 + err = ubifs_tnc_lookup(c, &key, dn);
25280 + if (err == -ENOENT)
25281 + dlen = 0; /* Not found (so it is a hole) */
25282 + else if (err)
25283 + goto out_free;
25284 + else {
25285 + if (le32_to_cpu(dn->size) <= dlen)
25286 + dlen = 0; /* Nothing to do */
25287 + else {
25288 + int compr_type = le16_to_cpu(dn->compr_type);
25289 +
25290 + if (compr_type != UBIFS_COMPR_NONE) {
25291 + err = recomp_data_node(dn, &dlen);
25292 + if (err)
25293 + goto out_free;
25294 + } else {
25295 + dn->size = cpu_to_le32(dlen);
25296 + dlen += UBIFS_DATA_NODE_SZ;
25297 + }
25298 + zero_data_node_unused(dn);
25299 + }
25300 + }
25301 + }
25302 +
25303 + if (dlen)
25304 + len = ALIGN(UBIFS_TRUN_NODE_SZ, 8) + dlen;
25305 + else
25306 + len = UBIFS_TRUN_NODE_SZ;
25307 +
25308 + /* Must make reservation before allocating sequence numbers */
25309 + err = make_reservation(c, BASEHD, len);
25310 + if (err)
25311 + goto out_free;
25312 +
25313 + ubifs_prepare_node(c, trun, UBIFS_TRUN_NODE_SZ, 0);
25314 + if (dlen)
25315 + ubifs_prepare_node(c, dn, dlen, 0);
25316 +
25317 + err = write_head(c, BASEHD, trun, len, &lnum, &offs, 0);
25318 + if (!err)
25319 + ubifs_wbuf_add_ino_nolock(&c->jheads[BASEHD].wbuf, inum);
25320 + release_head(c, BASEHD);
25321 + if (err)
25322 + goto out_ro;
25323 +
25324 + if (dlen) {
25325 + offs += ALIGN(UBIFS_TRUN_NODE_SZ, 8);
25326 + err = ubifs_tnc_add(c, &key, lnum, offs, dlen);
25327 + if (err)
25328 + goto out_ro;
25329 + }
25330 +
25331 + err = ubifs_add_dirt(c, lnum, UBIFS_TRUN_NODE_SZ);
25332 + if (err)
25333 + goto out_ro;
25334 +
25335 + bit = new_size & (UBIFS_BLOCK_SIZE - 1);
25336 +
25337 + blk = new_size / UBIFS_BLOCK_SIZE + (bit ? 1 : 0);
25338 + data_key_init(c, &key, inum, blk);
25339 +
25340 + bit = old_size & (UBIFS_BLOCK_SIZE - 1);
25341 +
25342 + blk = old_size / UBIFS_BLOCK_SIZE - (bit ? 0: 1);
25343 + data_key_init(c, &to_key, inum, blk);
25344 +
25345 + err = ubifs_tnc_remove_range(c, &key, &to_key);
25346 + if (err)
25347 + goto out_ro;
25348 +
25349 + finish_reservation(c);
25350 + kfree(trun);
25351 + return 0;
25352 +
25353 +out_ro:
25354 + ubifs_ro_mode(c, err);
25355 + finish_reservation(c);
25356 +out_free:
25357 + kfree(trun);
25358 + return err;
25359 +}
25360 +
25361 +#ifdef CONFIG_UBIFS_FS_XATTR
25362 +
25363 +int ubifs_jnl_delete_xattr(struct ubifs_info *c, const struct inode *host,
25364 + const struct inode *inode, const struct qstr *nm,
25365 + int sync)
25366 +{
25367 + int err, xlen, hlen, len, lnum, xent_offs, aligned_xlen;
25368 + struct ubifs_dent_node *xent;
25369 + struct ubifs_ino_node *ino;
25370 + union ubifs_key xent_key, key1, key2;
25371 +
25372 + dbg_jnl("host %lu, xattr ino %lu, name '%s', data len %d",
25373 + host->i_ino, inode->i_ino, nm->name,
25374 + ubifs_inode(inode)->data_len);
25375 + ubifs_assert(inode->i_nlink == 0);
25376 +
25377 + /*
25378 + * Since we are deleting the inode, we do not bother to attach any data
25379 + * to it and assume its length is %UBIFS_INO_NODE_SZ.
25380 + */
25381 + xlen = UBIFS_DENT_NODE_SZ + nm->len + 1;
25382 + aligned_xlen = ALIGN(xlen, 8);
25383 + hlen = ubifs_inode(host)->data_len + UBIFS_INO_NODE_SZ;
25384 + len = aligned_xlen + UBIFS_INO_NODE_SZ + ALIGN(hlen, 8);
25385 +
25386 + xent = kmalloc(len, GFP_NOFS);
25387 + if (!xent)
25388 + return -ENOMEM;
25389 +
25390 + /* Make reservation before allocating sequence numbers */
25391 + err = make_reservation(c, BASEHD, len);
25392 + if (err) {
25393 + kfree(xent);
25394 + return err;
25395 + }
25396 +
25397 + xent->ch.node_type = UBIFS_XENT_NODE;
25398 + xent_key_init(c, &xent_key, host->i_ino, nm);
25399 + key_write(c, &xent_key, xent->key);
25400 + xent->inum = 0;
25401 + xent->type = get_dent_type(inode->i_mode);
25402 + xent->nlen = cpu_to_le16(nm->len);
25403 + memcpy(xent->name, nm->name, nm->len);
25404 + xent->name[nm->len] = '\0';
25405 + zero_dent_node_unused(xent);
25406 + ubifs_prep_grp_node(c, xent, xlen, 0);
25407 +
25408 + ino = (void *)xent + aligned_xlen;
25409 + pack_inode(c, ino, inode, 0, 1);
25410 +
25411 + ino = (void *)ino + UBIFS_INO_NODE_SZ;
25412 + pack_inode(c, ino, host, 1, 0);
25413 +
25414 + err = write_head(c, BASEHD, xent, len, &lnum, &xent_offs, sync);
25415 + if (!sync && !err)
25416 + ubifs_wbuf_add_ino_nolock(&c->jheads[BASEHD].wbuf, host->i_ino);
25417 + release_head(c, BASEHD);
25418 + kfree(xent);
25419 + if (err)
25420 + goto out_ro;
25421 +
25422 + /* Remove the extended attribute entry from TNC */
25423 + err = ubifs_tnc_remove_nm(c, &xent_key, nm);
25424 + if (err)
25425 + goto out_ro;
25426 + err = ubifs_add_dirt(c, lnum, xlen);
25427 + if (err)
25428 + goto out_ro;
25429 +
25430 + /*
25431 + * Remove all nodes belonging to the extended attribute inode from TNC.
25432 + * Well, there actually must be only one node - the inode itself.
25433 + */
25434 + lowest_ino_key(c, &key1, inode->i_ino);
25435 + highest_ino_key(c, &key2, inode->i_ino);
25436 + err = ubifs_tnc_remove_range(c, &key1, &key2);
25437 + if (err)
25438 + goto out_ro;
25439 + err = ubifs_add_dirt(c, lnum, UBIFS_INO_NODE_SZ);
25440 + if (err)
25441 + goto out_ro;
25442 +
25443 + /* And update TNC with the new host inode position */
25444 + ino_key_init(c, &key1, host->i_ino);
25445 + err = ubifs_tnc_add(c, &key1, lnum, xent_offs + len - hlen, hlen);
25446 + if (err)
25447 + goto out_ro;
25448 +
25449 + finish_reservation(c);
25450 + return 0;
25451 +
25452 +out_ro:
25453 + ubifs_ro_mode(c, err);
25454 + finish_reservation(c);
25455 + return err;
25456 +}
25457 +
25458 +/**
25459 + * ubifs_jnl_write_2_inodes - write 2 inodes to the journal.
25460 + * @c: UBIFS file-system description object
25461 + * @inode1: first inode to write
25462 + * @inode2: second inode to write
25463 + * @sync: non-zero if the write-buffer has to be synchronized
25464 + *
25465 + * This function writes 2 inodes @inode1 and @inode2 to the journal (to the
25466 + * base head - first @inode1, then @inode2). Returns zero in case of success
25467 + * and a negative error code in case of failure.
25468 + */
25469 +int ubifs_jnl_write_2_inodes(struct ubifs_info *c, const struct inode *inode1,
25470 + const struct inode *inode2, int sync)
25471 +{
25472 + int err, len1, len2, aligned_len, aligned_len1, lnum, offs;
25473 + struct ubifs_ino_node *ino;
25474 + union ubifs_key key;
25475 +
25476 + dbg_jnl("ino %lu, ino %lu", inode1->i_ino, inode2->i_ino);
25477 + ubifs_assert(inode1->i_nlink > 0);
25478 + ubifs_assert(inode2->i_nlink > 0);
25479 +
25480 + len1 = UBIFS_INO_NODE_SZ + ubifs_inode(inode1)->data_len;
25481 + len2 = UBIFS_INO_NODE_SZ + ubifs_inode(inode2)->data_len;
25482 + aligned_len1 = ALIGN(len1, 8);
25483 + aligned_len = aligned_len1 + ALIGN(len2, 8);
25484 +
25485 + ino = kmalloc(aligned_len, GFP_NOFS);
25486 + if (!ino)
25487 + return -ENOMEM;
25488 +
25489 + /* Make reservation before allocating sequence numbers */
25490 + err = make_reservation(c, BASEHD, aligned_len);
25491 + if (err)
25492 + goto out_free;
25493 +
25494 + pack_inode(c, ino, inode1, 0, 0);
25495 + pack_inode(c, (void *)ino + aligned_len1, inode2, 1, 0);
25496 +
25497 + err = write_head(c, BASEHD, ino, aligned_len, &lnum, &offs, 0);
25498 + if (!sync && !err) {
25499 + struct ubifs_wbuf *wbuf = &c->jheads[BASEHD].wbuf;
25500 +
25501 + ubifs_wbuf_add_ino_nolock(wbuf, inode1->i_ino);
25502 + ubifs_wbuf_add_ino_nolock(wbuf, inode2->i_ino);
25503 + }
25504 + release_head(c, BASEHD);
25505 + if (err)
25506 + goto out_ro;
25507 +
25508 + ino_key_init(c, &key, inode1->i_ino);
25509 + err = ubifs_tnc_add(c, &key, lnum, offs, len1);
25510 + if (err)
25511 + goto out_ro;
25512 +
25513 + ino_key_init(c, &key, inode2->i_ino);
25514 + err = ubifs_tnc_add(c, &key, lnum, offs + aligned_len1, len2);
25515 + if (err)
25516 + goto out_ro;
25517 +
25518 + finish_reservation(c);
25519 + kfree(ino);
25520 + return 0;
25521 +
25522 +out_ro:
25523 + ubifs_ro_mode(c, err);
25524 + finish_reservation(c);
25525 +out_free:
25526 + kfree(ino);
25527 + return err;
25528 +}
25529 +
25530 +#endif /* CONFIG_UBIFS_FS_XATTR */
25531 diff --exclude=.git -urN linux-2.6.25.6/fs/ubifs/Kconfig avr32-2.6/fs/ubifs/Kconfig
25532 --- linux-2.6.25.6/fs/ubifs/Kconfig 1970-01-01 01:00:00.000000000 +0100
25533 +++ avr32-2.6/fs/ubifs/Kconfig 2008-06-12 15:09:45.311815896 +0200
25534 @@ -0,0 +1,72 @@
25535 +config UBIFS_FS
25536 + tristate "UBIFS file system support"
25537 + select CRC16
25538 + select CRC32
25539 + select CRYPTO if UBIFS_FS_ADVANCED_COMPR
25540 + select CRYPTO if UBIFS_FS_LZO
25541 + select CRYPTO if UBIFS_FS_ZLIB
25542 + select CRYPTO_LZO if UBIFS_FS_LZO
25543 + select CRYPTO_DEFLATE if UBIFS_FS_ZLIB
25544 + depends on MTD_UBI
25545 + help
25546 + UBIFS is a file system for flash devices which works on top of UBI.
25547 +
25548 +config UBIFS_FS_XATTR
25549 + bool "Extended attributes support"
25550 + depends on UBIFS_FS
25551 + help
25552 + This option enables support of extended attributes.
25553 +
25554 +config UBIFS_FS_ADVANCED_COMPR
25555 + bool "Advanced compression options"
25556 + depends on UBIFS_FS
25557 + help
25558 + This option allows to explicitly choose which compressions, if any,
25559 + are enabled in UBIFS. Removing compressors means inbility to read
25560 + existing file systems.
25561 +
25562 + If unsure, say 'N'.
25563 +
25564 +config UBIFS_FS_LZO
25565 + bool "LZO compression support" if UBIFS_FS_ADVANCED_COMPR
25566 + depends on UBIFS_FS
25567 + default y
25568 + help
25569 + LZO compressor is generally faster then zlib but compresses worse.
25570 + Say 'Y' if unsure.
25571 +
25572 +config UBIFS_FS_ZLIB
25573 + bool "ZLIB compression support" if UBIFS_FS_ADVANCED_COMPR
25574 + depends on UBIFS_FS
25575 + default y
25576 + help
25577 + Zlib copresses better then LZO but it is slower. Say 'Y' if unsure.
25578 +
25579 +# Debugging-related stuff
25580 +config UBIFS_FS_DEBUG
25581 + bool "Enable debugging"
25582 + depends on UBIFS_FS
25583 + select DEBUG_FS
25584 + select KALLSYMS_ALL
25585 + help
25586 + This option enables UBIFS debugging.
25587 +
25588 +config UBIFS_FS_DEBUG_MSG_LVL
25589 + int "Default message level (0 = no extra messages, 3 = lots)"
25590 + depends on UBIFS_FS_DEBUG
25591 + default "0"
25592 + help
25593 + This controls the amount of debugging messages produced by UBIFS.
25594 + If reporting bugs, please try to have available a full dump of the
25595 + messages at level 1 while the misbehaviour was occurring. Level 2
25596 + may become necessary if level 1 messages were not enough to find the
25597 + bug. Generally Level 3 should be avoided.
25598 +
25599 +config UBIFS_FS_DEBUG_CHKS
25600 + bool "Enable extra checks"
25601 + depends on UBIFS_FS_DEBUG
25602 + help
25603 + If extra checks are enabled UBIFS will check the consistency of its
25604 + internal data structures during operation. However, UBIFS performance
25605 + is dramatically slower when this option is selected especially if the
25606 + file system is large.
25607 diff --exclude=.git -urN linux-2.6.25.6/fs/ubifs/key.h avr32-2.6/fs/ubifs/key.h
25608 --- linux-2.6.25.6/fs/ubifs/key.h 1970-01-01 01:00:00.000000000 +0100
25609 +++ avr32-2.6/fs/ubifs/key.h 2008-06-12 15:09:45.367815766 +0200
25610 @@ -0,0 +1,533 @@
25611 +/*
25612 + * This file is part of UBIFS.
25613 + *
25614 + * Copyright (C) 2006-2008 Nokia Corporation.
25615 + *
25616 + * This program is free software; you can redistribute it and/or modify it
25617 + * under the terms of the GNU General Public License version 2 as published by
25618 + * the Free Software Foundation.
25619 + *
25620 + * This program is distributed in the hope that it will be useful, but WITHOUT
25621 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
25622 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
25623 + * more details.
25624 + *
25625 + * You should have received a copy of the GNU General Public License along with
25626 + * this program; if not, write to the Free Software Foundation, Inc., 51
25627 + * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25628 + *
25629 + * Authors: Artem Bityutskiy (Битюцкий Артём)
25630 + * Adrian Hunter
25631 + */
25632 +
25633 +/*
25634 + * This header contains various key-related definitions and helper function.
25635 + * UBIFS allows several key schemes, so we access key fields only via these
25636 + * helpers. At the moment only one key scheme is supported.
25637 + *
25638 + * Simple key scheme
25639 + * ~~~~~~~~~~~~~~~~~
25640 + *
25641 + * Keys are 64-bits long. First 32-bits are inode number (parent inode number
25642 + * in case of direntry key). Next 3 bits are node type. The last 29 bits are
25643 + * 4KiB offset in case of inode node, and direntry hash in case of a direntry
25644 + * node. We use "r5" hash borrowed from reiserfs.
25645 + */
25646 +
25647 +#ifndef __UBIFS_KEY_H__
25648 +#define __UBIFS_KEY_H__
25649 +
25650 +/**
25651 + * key_r5_hash - R5 hash function (borrowed from reiserfs).
25652 + * @s: direntry name
25653 + * @len: name length
25654 + */
25655 +static inline uint32_t key_r5_hash(const char *s, int len)
25656 +{
25657 + uint32_t a = 0;
25658 + const signed char *str = (const signed char *)s;
25659 +
25660 + while (*str) {
25661 + a += *str << 4;
25662 + a += *str >> 4;
25663 + a *= 11;
25664 + str++;
25665 + }
25666 +
25667 + a &= UBIFS_S_KEY_HASH_MASK;
25668 +
25669 + /*
25670 + * We use hash values as offset in directories, so values %0 and %1 are
25671 + * reserved for "." and "..". %2 is reserved for "end of readdir"
25672 + * marker.
25673 + */
25674 + if (unlikely(a >= 0 && a <= 2))
25675 + a += 3;
25676 + return a;
25677 +}
25678 +
25679 +/**
25680 + * key_test_hash - testing hash function.
25681 + * @str: direntry name
25682 + * @len: name length
25683 + */
25684 +static inline uint32_t key_test_hash(const char *str, int len)
25685 +{
25686 + uint32_t a = 0;
25687 +
25688 + len = min_t(uint32_t, len, 4);
25689 + memcpy(&a, str, len);
25690 + a &= UBIFS_S_KEY_HASH_MASK;
25691 + if (unlikely(a >= 0 && a <= 2))
25692 + a += 3;
25693 + return a;
25694 +}
25695 +
25696 +/**
25697 + * ino_key_init - initialize inode key.
25698 + * @c: UBIFS file-system description object
25699 + * @key: key to initialize
25700 + * @inum: inode number
25701 + */
25702 +static inline void ino_key_init(const struct ubifs_info *c,
25703 + union ubifs_key *key, ino_t inum)
25704 +{
25705 + key->u32[0] = inum;
25706 + key->u32[1] = UBIFS_INO_KEY << UBIFS_S_KEY_BLOCK_BITS;
25707 +}
25708 +
25709 +/**
25710 + * ino_key_init_flash - initialize on-flash inode key.
25711 + * @c: UBIFS file-system description object
25712 + * @k: key to initialize
25713 + * @inum: inode number
25714 + */
25715 +static inline void ino_key_init_flash(const struct ubifs_info *c, void *k,
25716 + ino_t inum)
25717 +{
25718 + union ubifs_key *key = k;
25719 +
25720 + key->j32[0] = cpu_to_le32(inum);
25721 + key->j32[1] = cpu_to_le32(UBIFS_INO_KEY << UBIFS_S_KEY_BLOCK_BITS);
25722 + memset(k + 8, 0, UBIFS_MAX_KEY_LEN - 8);
25723 +}
25724 +
25725 +/**
25726 + * lowest_ino_key - get the lowest possible inode key.
25727 + * @c: UBIFS file-system description object
25728 + * @key: key to initialize
25729 + * @inum: inode number
25730 + */
25731 +static inline void lowest_ino_key(const struct ubifs_info *c,
25732 + union ubifs_key *key, ino_t inum)
25733 +{
25734 + key->u32[0] = inum;
25735 + key->u32[1] = 0;
25736 +}
25737 +
25738 +/**
25739 + * highest_ino_key - get the highest possible inode key.
25740 + * @c: UBIFS file-system description object
25741 + * @key: key to initialize
25742 + * @inum: inode number
25743 + */
25744 +static inline void highest_ino_key(const struct ubifs_info *c,
25745 + union ubifs_key *key, ino_t inum)
25746 +{
25747 + key->u32[0] = inum;
25748 + key->u32[1] = 0xffffffff;
25749 +}
25750 +
25751 +/**
25752 + * dent_key_init - initialize directory entry key.
25753 + * @c: UBIFS file-system description object
25754 + * @key: key to initialize
25755 + * @inum: parent inode number
25756 + * @nm: direntry name and length
25757 + */
25758 +static inline void dent_key_init(const struct ubifs_info *c,
25759 + union ubifs_key *key, ino_t inum,
25760 + const struct qstr *nm)
25761 +{
25762 + uint32_t hash = c->key_hash(nm->name, nm->len);
25763 +
25764 + ubifs_assert(!(hash & ~UBIFS_S_KEY_HASH_MASK));
25765 + key->u32[0] = inum;
25766 + key->u32[1] = hash | (UBIFS_DENT_KEY << UBIFS_S_KEY_HASH_BITS);
25767 +}
25768 +
25769 +/**
25770 + * dent_key_init_hash - initialize directory entry key without re-calculating
25771 + * hash function.
25772 + * @c: UBIFS file-system description object
25773 + * @key: key to initialize
25774 + * @inum: parent inode number
25775 + * @hash: direntry name hash
25776 + */
25777 +static inline void dent_key_init_hash(const struct ubifs_info *c,
25778 + union ubifs_key *key, ino_t inum,
25779 + uint32_t hash)
25780 +{
25781 + ubifs_assert(!(hash & ~UBIFS_S_KEY_HASH_MASK));
25782 + key->u32[0] = inum;
25783 + key->u32[1] = hash | (UBIFS_DENT_KEY << UBIFS_S_KEY_HASH_BITS);
25784 +}
25785 +
25786 +/**
25787 + * dent_key_init_flash - initialize on-flash directory entry key.
25788 + * @c: UBIFS file-system description object
25789 + * @k: key to initialize
25790 + * @inum: parent inode number
25791 + * @nm: direntry name and length
25792 + */
25793 +static inline void dent_key_init_flash(const struct ubifs_info *c, void *k,
25794 + ino_t inum, const struct qstr *nm)
25795 +{
25796 + union ubifs_key *key = k;
25797 + uint32_t hash = c->key_hash(nm->name, nm->len);
25798 +
25799 + ubifs_assert(!(hash & ~UBIFS_S_KEY_HASH_MASK));
25800 + key->j32[0] = cpu_to_le32(inum);
25801 + key->j32[1] = cpu_to_le32(hash |
25802 + (UBIFS_DENT_KEY << UBIFS_S_KEY_HASH_BITS));
25803 + memset(k + 8, 0, UBIFS_MAX_KEY_LEN - 8);
25804 +}
25805 +
25806 +/**
25807 + * lowest_dent_key - get the lowest possible directory entry key.
25808 + * @c: UBIFS file-system description object
25809 + * @key: where to store the lowest key
25810 + * @inum: parent inode number
25811 + */
25812 +static inline void lowest_dent_key(const struct ubifs_info *c,
25813 + union ubifs_key *key, ino_t inum)
25814 +{
25815 + key->u32[0] = inum;
25816 + key->u32[1] = UBIFS_DENT_KEY << UBIFS_S_KEY_HASH_BITS;
25817 +}
25818 +
25819 +/**
25820 + * xent_key_init - initialize extended attribute entry key.
25821 + * @c: UBIFS file-system description object
25822 + * @key: key to initialize
25823 + * @inum: host inode number
25824 + * @nm: extended attribute entry name and length
25825 + */
25826 +static inline void xent_key_init(const struct ubifs_info *c,
25827 + union ubifs_key *key, ino_t inum,
25828 + const struct qstr *nm)
25829 +{
25830 + uint32_t hash = c->key_hash(nm->name, nm->len);
25831 +
25832 + ubifs_assert(!(hash & ~UBIFS_S_KEY_HASH_MASK));
25833 + key->u32[0] = inum;
25834 + key->u32[1] = hash | (UBIFS_XENT_KEY << UBIFS_S_KEY_HASH_BITS);
25835 +}
25836 +
25837 +/**
25838 + * xent_key_init_hash - initialize extended attribute entry key without
25839 + * re-calculating hash function.
25840 + * @c: UBIFS file-system description object
25841 + * @key: key to initialize
25842 + * @inum: host inode number
25843 + * @hash: extended attribute entry name hash
25844 + */
25845 +static inline void xent_key_init_hash(const struct ubifs_info *c,
25846 + union ubifs_key *key, ino_t inum,
25847 + uint32_t hash)
25848 +{
25849 + ubifs_assert(!(hash & ~UBIFS_S_KEY_HASH_MASK));
25850 + key->u32[0] = inum;
25851 + key->u32[1] = hash | (UBIFS_XENT_KEY << UBIFS_S_KEY_HASH_BITS);
25852 +}
25853 +
25854 +/**
25855 + * xent_key_init_flash - initialize on-flash extended attribute entry key.
25856 + * @c: UBIFS file-system description object
25857 + * @k: key to initialize
25858 + * @inum: host inode number
25859 + * @nm: extended attribute entry name and length
25860 + */
25861 +static inline void xent_key_init_flash(const struct ubifs_info *c, void *k,
25862 + ino_t inum, const struct qstr *nm)
25863 +{
25864 + union ubifs_key *key = k;
25865 + uint32_t hash = c->key_hash(nm->name, nm->len);
25866 +
25867 + ubifs_assert(!(hash & ~UBIFS_S_KEY_HASH_MASK));
25868 + key->j32[0] = cpu_to_le32(inum);
25869 + key->j32[1] = cpu_to_le32(hash |
25870 + (UBIFS_XENT_KEY << UBIFS_S_KEY_HASH_BITS));
25871 + memset(k + 8, 0, UBIFS_MAX_KEY_LEN - 8);
25872 +}
25873 +
25874 +/**
25875 + * lowest_xent_key - get the lowest possible extended attribute entry key.
25876 + * @c: UBIFS file-system description object
25877 + * @key: where to store the lowest key
25878 + * @inum: host inode number
25879 + */
25880 +static inline void lowest_xent_key(const struct ubifs_info *c,
25881 + union ubifs_key *key, ino_t inum)
25882 +{
25883 + key->u32[0] = inum;
25884 + key->u32[1] = UBIFS_XENT_KEY << UBIFS_S_KEY_HASH_BITS;
25885 +}
25886 +
25887 +/**
25888 + * data_key_init - initialize data key.
25889 + * @c: UBIFS file-system description object
25890 + * @key: key to initialize
25891 + * @inum: inode number
25892 + * @block: block number
25893 + */
25894 +static inline void data_key_init(const struct ubifs_info *c,
25895 + union ubifs_key *key, ino_t inum,
25896 + unsigned int block)
25897 +{
25898 + ubifs_assert(!(block & ~UBIFS_S_KEY_BLOCK_MASK));
25899 + key->u32[0] = inum;
25900 + key->u32[1] = block | (UBIFS_DATA_KEY << UBIFS_S_KEY_BLOCK_BITS);
25901 +}
25902 +
25903 +/**
25904 + * data_key_init_flash - initialize on-flash data key.
25905 + * @c: UBIFS file-system description object
25906 + * @k: key to initialize
25907 + * @inum: inode number
25908 + * @block: block number
25909 + */
25910 +static inline void data_key_init_flash(const struct ubifs_info *c, void *k,
25911 + ino_t inum, unsigned int block)
25912 +{
25913 + union ubifs_key *key = k;
25914 +
25915 + ubifs_assert(!(block & ~UBIFS_S_KEY_BLOCK_MASK));
25916 + key->j32[0] = cpu_to_le32(inum);
25917 + key->j32[1] = cpu_to_le32(block |
25918 + (UBIFS_DATA_KEY << UBIFS_S_KEY_BLOCK_BITS));
25919 + memset(k + 8, 0, UBIFS_MAX_KEY_LEN - 8);
25920 +}
25921 +
25922 +/**
25923 + * trun_key_init - initialize truncation node key.
25924 + * @c: UBIFS file-system description object
25925 + * @key: key to initialize
25926 + * @inum: inode number
25927 + *
25928 + * Note, UBIFS does not have truncation keys on the media and this function is
25929 + * only used for purposes of replay.
25930 + */
25931 +static inline void trun_key_init(const struct ubifs_info *c,
25932 + union ubifs_key *key, ino_t inum)
25933 +{
25934 + key->u32[0] = inum;
25935 + key->u32[1] = UBIFS_TRUN_KEY << UBIFS_S_KEY_BLOCK_BITS;
25936 +}
25937 +
25938 +/**
25939 + * key_type - get key type.
25940 + * @c: UBIFS file-system description object
25941 + * @key: key to get type of
25942 + */
25943 +static inline int key_type(const struct ubifs_info *c,
25944 + const union ubifs_key *key)
25945 +{
25946 + return key->u32[1] >> UBIFS_S_KEY_BLOCK_BITS;
25947 +}
25948 +
25949 +/**
25950 + * key_type_flash - get type of a on-flash formatted key.
25951 + * @c: UBIFS file-system description object
25952 + * @k: key to get type of
25953 + */
25954 +static inline int key_type_flash(const struct ubifs_info *c, const void *k)
25955 +{
25956 + const union ubifs_key *key = k;
25957 +
25958 + return le32_to_cpu(key->u32[1]) >> UBIFS_S_KEY_BLOCK_BITS;
25959 +}
25960 +
25961 +/**
25962 + * key_inum - fetch inode number from key.
25963 + * @c: UBIFS file-system description object
25964 + * @k: key to fetch inode number from
25965 + */
25966 +static inline ino_t key_inum(const struct ubifs_info *c, const void *k)
25967 +{
25968 + const union ubifs_key *key = k;
25969 +
25970 + return key->u32[0];
25971 +}
25972 +
25973 +/**
25974 + * key_inum_flash - fetch inode number from an on-flash formatted key.
25975 + * @c: UBIFS file-system description object
25976 + * @k: key to fetch inode number from
25977 + */
25978 +static inline ino_t key_inum_flash(const struct ubifs_info *c, const void *k)
25979 +{
25980 + const union ubifs_key *key = k;
25981 +
25982 + return le32_to_cpu(key->j32[0]);
25983 +}
25984 +
25985 +/**
25986 + * key_hash - get directory entry hash.
25987 + * @c: UBIFS file-system description object
25988 + * @key: the key to get hash from
25989 + */
25990 +static inline int key_hash(const struct ubifs_info *c,
25991 + const union ubifs_key *key)
25992 +{
25993 + return key->u32[1] & UBIFS_S_KEY_HASH_MASK;
25994 +}
25995 +
25996 +/**
25997 + * key_hash_flash - get directory entry hash from an on-flash formatted key.
25998 + * @c: UBIFS file-system description object
25999 + * @k: the key to get hash from
26000 + */
26001 +static inline int key_hash_flash(const struct ubifs_info *c, const void *k)
26002 +{
26003 + const union ubifs_key *key = k;
26004 +
26005 + return le32_to_cpu(key->j32[1]) & UBIFS_S_KEY_HASH_MASK;
26006 +}
26007 +
26008 +/**
26009 + * key_block - get data block number.
26010 + * @c: UBIFS file-system description object
26011 + * @key: the key to get the block number from
26012 + */
26013 +static inline unsigned int key_block(const struct ubifs_info *c,
26014 + const union ubifs_key *key)
26015 +{
26016 + return key->u32[1] & UBIFS_S_KEY_BLOCK_MASK;
26017 +}
26018 +
26019 +/**
26020 + * key_block_flash - get data block number from an on-flash formatted key.
26021 + * @c: UBIFS file-system description object
26022 + * @k: the key to get the block number from
26023 + */
26024 +static inline unsigned int key_block_flash(const struct ubifs_info *c,
26025 + const void *k)
26026 +{
26027 + const union ubifs_key *key = k;
26028 +
26029 + return le32_to_cpu(key->u32[1]) & UBIFS_S_KEY_BLOCK_MASK;
26030 +}
26031 +
26032 +/**
26033 + * key_read - transform a key to in-memory format.
26034 + * @c: UBIFS file-system description object
26035 + * @from: the key to transform
26036 + * @to: the key to store the result
26037 + */
26038 +static inline void key_read(const struct ubifs_info *c, const void *from,
26039 + union ubifs_key *to)
26040 +{
26041 + const union ubifs_key *f = from;
26042 +
26043 + to->u32[0] = le32_to_cpu(f->j32[0]);
26044 + to->u32[1] = le32_to_cpu(f->j32[1]);
26045 +}
26046 +
26047 +/**
26048 + * key_write - transform a key from in-memory format.
26049 + * @c: UBIFS file-system description object
26050 + * @from: the key to transform
26051 + * @to: the key to store the result
26052 + */
26053 +static inline void key_write(const struct ubifs_info *c,
26054 + const union ubifs_key *from, void *to)
26055 +{
26056 + union ubifs_key *t = to;
26057 +
26058 + t->j32[0] = cpu_to_le32(from->u32[0]);
26059 + t->j32[1] = cpu_to_le32(from->u32[1]);
26060 + memset(to + 8, 0, UBIFS_MAX_KEY_LEN - 8);
26061 +}
26062 +
26063 +/**
26064 + * key_write_idx - transform a key from in-memory format for the index.
26065 + * @c: UBIFS file-system description object
26066 + * @from: the key to transform
26067 + * @to: the key to store the result
26068 + */
26069 +static inline void key_write_idx(const struct ubifs_info *c,
26070 + const union ubifs_key *from, void *to)
26071 +{
26072 + union ubifs_key *t = to;
26073 +
26074 + t->j32[0] = cpu_to_le32(from->u32[0]);
26075 + t->j32[1] = cpu_to_le32(from->u32[1]);
26076 +}
26077 +
26078 +/**
26079 + * key_copy - copy a key.
26080 + * @c: UBIFS file-system description object
26081 + * @from: the key to copy from
26082 + * @to: the key to copy to
26083 + */
26084 +static inline void key_copy(const struct ubifs_info *c,
26085 + const union ubifs_key *from, union ubifs_key *to)
26086 +{
26087 + to->u64[0] = from->u64[0];
26088 +}
26089 +
26090 +/**
26091 + * keys_cmp - compare keys.
26092 + * @c: UBIFS file-system description object
26093 + * @key1: the first key to compare
26094 + * @key2: the second key to compare
26095 + *
26096 + * This function compares 2 keys and returns %-1 if @key1 is less than
26097 + * @key2, 0 if the keys are equivalent and %1 if @key1 is greater than @key2.
26098 + */
26099 +static inline int keys_cmp(const struct ubifs_info *c,
26100 + const union ubifs_key *key1,
26101 + const union ubifs_key *key2)
26102 +{
26103 + if (key1->u32[0] < key2->u32[0])
26104 + return -1;
26105 + if (key1->u32[0] > key2->u32[0])
26106 + return 1;
26107 + if (key1->u32[1] < key2->u32[1])
26108 + return -1;
26109 + if (key1->u32[1] > key2->u32[1])
26110 + return 1;
26111 +
26112 + return 0;
26113 +}
26114 +
26115 +/**
26116 + * is_hash_key - is a key vulnerable to hash collisions.
26117 + * @c: UBIFS file-system description object
26118 + * @key: key
26119 + *
26120 + * This function returns %1 if @key is a hashed key or %0 otherwise.
26121 + */
26122 +static inline int is_hash_key(const struct ubifs_info *c,
26123 + const union ubifs_key *key)
26124 +{
26125 + int type = key_type(c, key);
26126 +
26127 + return type == UBIFS_DENT_KEY || type == UBIFS_XENT_KEY;
26128 +}
26129 +
26130 +/**
26131 + * key_max_inode_size - get maximum file size allowed by current key format.
26132 + * @c: UBIFS file-system description object
26133 + */
26134 +static inline unsigned long long key_max_inode_size(const struct ubifs_info *c)
26135 +{
26136 + switch (c->key_fmt) {
26137 + case UBIFS_SIMPLE_KEY_FMT:
26138 + return (1ULL << UBIFS_S_KEY_BLOCK_BITS) * UBIFS_BLOCK_SIZE;
26139 + default:
26140 + return 0;
26141 + }
26142 +}
26143 +#endif /* !__UBIFS_KEY_H__ */
26144 diff --exclude=.git -urN linux-2.6.25.6/fs/ubifs/log.c avr32-2.6/fs/ubifs/log.c
26145 --- linux-2.6.25.6/fs/ubifs/log.c 1970-01-01 01:00:00.000000000 +0100
26146 +++ avr32-2.6/fs/ubifs/log.c 2008-06-12 15:09:45.367815766 +0200
26147 @@ -0,0 +1,799 @@
26148 +/*
26149 + * This file is part of UBIFS.
26150 + *
26151 + * Copyright (C) 2006-2008 Nokia Corporation.
26152 + *
26153 + * This program is free software; you can redistribute it and/or modify it
26154 + * under the terms of the GNU General Public License version 2 as published by
26155 + * the Free Software Foundation.
26156 + *
26157 + * This program is distributed in the hope that it will be useful, but WITHOUT
26158 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
26159 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
26160 + * more details.
26161 + *
26162 + * You should have received a copy of the GNU General Public License along with
26163 + * this program; if not, write to the Free Software Foundation, Inc., 51
26164 + * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26165 + *
26166 + * Authors: Artem Bityutskiy (Битюцкий Артём)
26167 + * Adrian Hunter
26168 + */
26169 +
26170 +/*
26171 + * This file is a part of UBIFS journal implementation and contains various
26172 + * functions which manipulate the log. The log is a fixed area on the flash
26173 + * which does not contain any data but refers to buds. The log is a part of the
26174 + * journal.
26175 + */
26176 +
26177 +#include "ubifs.h"
26178 +
26179 +#ifdef CONFIG_UBIFS_FS_DEBUG
26180 +static int dbg_check_bud_bytes(struct ubifs_info *c);
26181 +#else
26182 +#define dbg_check_bud_bytes(c) 0
26183 +#endif
26184 +
26185 +/**
26186 + * ubifs_search_bud - search bud LEB.
26187 + * @c: UBIFS file-system description object
26188 + * @lnum: logical eraseblock number to search
26189 + *
26190 + * This function searches bud LEB @lnum. Returns bud description object in case
26191 + * of success and %NULL if there is no bud with this LEB number.
26192 + */
26193 +struct ubifs_bud *ubifs_search_bud(struct ubifs_info *c, int lnum)
26194 +{
26195 + struct rb_node *p;
26196 + struct ubifs_bud *bud;
26197 +
26198 + spin_lock(&c->buds_lock);
26199 + p = c->buds.rb_node;
26200 + while (p) {
26201 + bud = rb_entry(p, struct ubifs_bud, rb);
26202 + if (lnum < bud->lnum)
26203 + p = p->rb_left;
26204 + else if (lnum > bud->lnum)
26205 + p = p->rb_right;
26206 + else {
26207 + spin_unlock(&c->buds_lock);
26208 + return bud;
26209 + }
26210 + }
26211 + spin_unlock(&c->buds_lock);
26212 + return NULL;
26213 +}
26214 +
26215 +/**
26216 + * ubifs_get_wbuf - get the wbuf associated with a LEB, if there is one.
26217 + * @c: UBIFS file-system description object
26218 + * @lnum: logical eraseblock number to search
26219 + *
26220 + * This functions returns the wbuf for @lnum or %NULL if there is not one.
26221 + */
26222 +struct ubifs_wbuf *ubifs_get_wbuf(struct ubifs_info *c, int lnum)
26223 +{
26224 + struct rb_node *p;
26225 + struct ubifs_bud *bud;
26226 + int jhead;
26227 +
26228 + if (!c->jheads)
26229 + return NULL;
26230 +
26231 + spin_lock(&c->buds_lock);
26232 + p = c->buds.rb_node;
26233 + while (p) {
26234 + bud = rb_entry(p, struct ubifs_bud, rb);
26235 + if (lnum < bud->lnum)
26236 + p = p->rb_left;
26237 + else if (lnum > bud->lnum)
26238 + p = p->rb_right;
26239 + else {
26240 + jhead = bud->jhead;
26241 + spin_unlock(&c->buds_lock);
26242 + return &c->jheads[jhead].wbuf;
26243 + }
26244 + }
26245 + spin_unlock(&c->buds_lock);
26246 + return NULL;
26247 +}
26248 +
26249 +/**
26250 + * next_log_lnum - switch to the next log LEB.
26251 + * @c: UBIFS file-system description object
26252 + * @lnum: current log LEB
26253 + */
26254 +static inline int next_log_lnum(const struct ubifs_info *c, int lnum)
26255 +{
26256 + lnum += 1;
26257 + if (lnum > c->log_last)
26258 + lnum = UBIFS_LOG_LNUM;
26259 +
26260 + return lnum;
26261 +}
26262 +
26263 +/**
26264 + * empty_log_bytes - calculate amount of empty space in the log.
26265 + * @c: UBIFS file-system description object
26266 + */
26267 +static inline long long empty_log_bytes(const struct ubifs_info *c)
26268 +{
26269 + long long h, t;
26270 +
26271 + h = c->lhead_lnum * c->leb_size + c->lhead_offs;
26272 + t = c->ltail_lnum * c->leb_size;
26273 +
26274 + if (h >= t)
26275 + return c->log_bytes - h + t;
26276 + else
26277 + return t - h;
26278 +}
26279 +
26280 +/**
26281 + * ubifs_add_bud - add bud LEB to the tree of buds and its journal head list.
26282 + * @c: UBIFS file-system description object
26283 + * @bud: the bud to add
26284 + */
26285 +void ubifs_add_bud(struct ubifs_info *c, struct ubifs_bud *bud)
26286 +{
26287 + struct rb_node **p, *parent = NULL;
26288 + struct ubifs_bud *b;
26289 + struct ubifs_jhead *jhead;
26290 +
26291 + spin_lock(&c->buds_lock);
26292 + p = &c->buds.rb_node;
26293 + while (*p) {
26294 + parent = *p;
26295 + b = rb_entry(parent, struct ubifs_bud, rb);
26296 + ubifs_assert(bud->lnum != b->lnum);
26297 + if (bud->lnum < b->lnum)
26298 + p = &(*p)->rb_left;
26299 + else
26300 + p = &(*p)->rb_right;
26301 + }
26302 +
26303 + rb_link_node(&bud->rb, parent, p);
26304 + rb_insert_color(&bud->rb, &c->buds);
26305 + if (c->jheads) {
26306 + jhead = &c->jheads[bud->jhead];
26307 + list_add_tail(&bud->list, &jhead->buds_list);
26308 + } else
26309 + ubifs_assert(c->replaying && (c->vfs_sb->s_flags & MS_RDONLY));
26310 +
26311 + /*
26312 + * Note, although this is a new bud, we anyway account this space now,
26313 + * before any data has been written to it, because this is about to
26314 + * guarantee fixed mount time, and this bud will anyway be read and
26315 + * scanned.
26316 + */
26317 + c->bud_bytes += c->leb_size - bud->start;
26318 +
26319 + dbg_log("LEB %d:%d, jhead %d, bud_bytes %lld", bud->lnum,
26320 + bud->start, bud->jhead, c->bud_bytes);
26321 + spin_unlock(&c->buds_lock);
26322 +}
26323 +
26324 +/**
26325 + * ubifs_create_buds_lists - create journal head buds lists for remount rw.
26326 + * @c: UBIFS file-system description object
26327 + */
26328 +void ubifs_create_buds_lists(struct ubifs_info *c)
26329 +{
26330 + struct rb_node *p;
26331 +
26332 + spin_lock(&c->buds_lock);
26333 + p = rb_first(&c->buds);
26334 + while (p) {
26335 + struct ubifs_bud *bud = rb_entry(p, struct ubifs_bud, rb);
26336 + struct ubifs_jhead *jhead = &c->jheads[bud->jhead];
26337 +
26338 + list_add_tail(&bud->list, &jhead->buds_list);
26339 + p = rb_next(p);
26340 + }
26341 + spin_unlock(&c->buds_lock);
26342 +}
26343 +
26344 +/**
26345 + * ubifs_add_bud_to_log - add a new bud to the log.
26346 + * @c: UBIFS file-system description object
26347 + * @jhead: journal head the bud belongs to
26348 + * @lnum: LEB number of the bud
26349 + * @offs: starting offset of the bud
26350 + *
26351 + * This function writes reference node for the new bud LEB @lnum it to the log,
26352 + * and adds it to the buds tress. It also makes sure that log size does not
26353 + * exceed the 'c->max_bud_bytes' limit. Returns zero in case of success,
26354 + * %-EAGAIN if commit is required, and a negative error codes in case of
26355 + * failure.
26356 + */
26357 +int ubifs_add_bud_to_log(struct ubifs_info *c, int jhead, int lnum, int offs)
26358 +{
26359 + int err;
26360 + struct ubifs_bud *bud;
26361 + struct ubifs_ref_node *ref;
26362 +
26363 + bud = kmalloc(sizeof(struct ubifs_bud), GFP_NOFS);
26364 + if (!bud)
26365 + return -ENOMEM;
26366 + ref = kzalloc(c->ref_node_alsz, GFP_NOFS);
26367 + if (!ref) {
26368 + kfree(bud);
26369 + return -ENOMEM;
26370 + }
26371 +
26372 + mutex_lock(&c->log_mutex);
26373 + /* Make sure we have enough space in the log */
26374 + if (empty_log_bytes(c) - c->ref_node_alsz < c->min_log_bytes) {
26375 + dbg_log("not enough log space - %lld, required %d",
26376 + empty_log_bytes(c), c->min_log_bytes);
26377 + ubifs_commit_required(c);
26378 + err = -EAGAIN;
26379 + goto out_unlock;
26380 + }
26381 +
26382 + /*
26383 + * Make sure the the amount of space in buds will not exceed
26384 + * 'c->max_bud_bytes' limit, because we want to guarantee mount time
26385 + * limits.
26386 + *
26387 + * It is not necessary to hold @c->buds_lock when reading @c->bud_bytes
26388 + * because we are holding @c->log_mutex. All @c->bud_bytes take place
26389 + * when both @c->log_mutex and @c->bud_bytes are locked.
26390 + */
26391 + if (c->bud_bytes + c->leb_size - offs > c->max_bud_bytes) {
26392 + dbg_log("bud bytes %lld (%lld max), require commit",
26393 + c->bud_bytes, c->max_bud_bytes);
26394 + ubifs_commit_required(c);
26395 + err = -EAGAIN;
26396 + goto out_unlock;
26397 + }
26398 +
26399 + /*
26400 + * If the journal is full enough - start background commit. Note, it is
26401 + * OK to read 'c->cmt_state' without spinlock because integer reads
26402 + * are atomic in the kernel.
26403 + */
26404 + if (c->bud_bytes >= c->bg_bud_bytes &&
26405 + c->cmt_state == COMMIT_RESTING) {
26406 + dbg_log("bud bytes %lld (%lld max), initiate BG commit",
26407 + c->bud_bytes, c->max_bud_bytes);
26408 + ubifs_request_bg_commit(c);
26409 + }
26410 +
26411 + bud->lnum = lnum;
26412 + bud->start = offs;
26413 + bud->jhead = jhead;
26414 +
26415 + ref->ch.node_type = UBIFS_REF_NODE;
26416 + ref->lnum = cpu_to_le32(bud->lnum);
26417 + ref->offs = cpu_to_le32(bud->start);
26418 + ref->jhead = cpu_to_le32(jhead);
26419 +
26420 + if (c->lhead_offs > c->leb_size - c->ref_node_alsz) {
26421 + c->lhead_lnum = next_log_lnum(c, c->lhead_lnum);
26422 + c->lhead_offs = 0;
26423 + }
26424 +
26425 + if (c->lhead_offs == 0) {
26426 + /* Must ensure next log LEB has been unmapped */
26427 + err = ubifs_leb_unmap(c, c->lhead_lnum);
26428 + if (err)
26429 + goto out_unlock;
26430 + }
26431 +
26432 + if (bud->start == 0) {
26433 + /*
26434 + * Before writing the LEB reference which refers an empty LEB
26435 + * to the log, we have to make sure it is mapped, because
26436 + * otherwise we'd risk to refer an LEB with garbage in case of
26437 + * an unclean reboot, because the target LEB might have been
26438 + * unmapped, but not yet physically erased.
26439 + */
26440 + err = ubi_leb_map(c->ubi, bud->lnum, UBI_SHORTTERM);
26441 + if (err)
26442 + goto out_unlock;
26443 + }
26444 +
26445 + dbg_log("write ref LEB %d:%d",
26446 + c->lhead_lnum, c->lhead_offs);
26447 + err = ubifs_write_node(c, ref, UBIFS_REF_NODE_SZ, c->lhead_lnum,
26448 + c->lhead_offs, UBI_SHORTTERM);
26449 + c->lhead_offs += c->ref_node_alsz;
26450 + if (err)
26451 + goto out_unlock;
26452 +
26453 + ubifs_add_bud(c, bud);
26454 +
26455 + mutex_unlock(&c->log_mutex);
26456 + kfree(ref);
26457 + return 0;
26458 +
26459 +out_unlock:
26460 + mutex_unlock(&c->log_mutex);
26461 + kfree(ref);
26462 + kfree(bud);
26463 + return err;
26464 +}
26465 +
26466 +/**
26467 + * remove_buds - remove used buds.
26468 + * @c: UBIFS file-system description object
26469 + *
26470 + * This function removes use buds from the buds tree. It does not remove the
26471 + * buds which are pointed to by journal heads.
26472 + */
26473 +static void remove_buds(struct ubifs_info *c)
26474 +{
26475 + struct rb_node *p;
26476 +
26477 + ubifs_assert(list_empty(&c->old_buds));
26478 + c->cmt_bud_bytes = 0;
26479 + spin_lock(&c->buds_lock);
26480 + p = rb_first(&c->buds);
26481 + while (p) {
26482 + struct rb_node *p1 = p;
26483 + struct ubifs_bud *bud;
26484 + struct ubifs_wbuf *wbuf;
26485 +
26486 + p = rb_next(p);
26487 + bud = rb_entry(p1, struct ubifs_bud, rb);
26488 + wbuf = &c->jheads[bud->jhead].wbuf;
26489 +
26490 + if (wbuf->lnum == bud->lnum) {
26491 + /*
26492 + * Do not remove buds which are pointed to by journal
26493 + * heads (non-closed buds).
26494 + */
26495 + c->cmt_bud_bytes += wbuf->offs - bud->start;
26496 + dbg_log("preserve %d:%d, jhead %d, bud bytes %d, "
26497 + "cmt_bud_bytes %lld", bud->lnum, bud->start,
26498 + bud->jhead, wbuf->offs - bud->start,
26499 + c->cmt_bud_bytes);
26500 + bud->start = wbuf->offs;
26501 + } else {
26502 + c->cmt_bud_bytes += c->leb_size - bud->start;
26503 + dbg_log("remove %d:%d, jhead %d, bud bytes %d, "
26504 + "cmt_bud_bytes %lld", bud->lnum, bud->start,
26505 + bud->jhead, c->leb_size - bud->start,
26506 + c->cmt_bud_bytes);
26507 + rb_erase(p1, &c->buds);
26508 + list_del(&bud->list);
26509 + /*
26510 + * If the commit does not finish, the recovery will need
26511 + * to replay the journal, in which case the old buds
26512 + * must be unchanged. Do not release them until post
26513 + * commit i.e. do not allow them to be garbage
26514 + * collected.
26515 + */
26516 + list_add(&bud->list, &c->old_buds);
26517 + }
26518 + }
26519 + spin_unlock(&c->buds_lock);
26520 +}
26521 +
26522 +/**
26523 + * ubifs_log_start_commit - start commit.
26524 + * @c: UBIFS file-system description object
26525 + * @ltail_lnum: return new log tail LEB number
26526 + *
26527 + * The commit operation starts with writing "commit start" node to the log and
26528 + * reference nodes for all journal heads which will define new journal after
26529 + * the commit has been finished. The commit start and reference nodes are
26530 + * written in one go to the nearest empty log LEB (hence, when commit is
26531 + * finished UBIFS may safely unmap all the previous log LEBs). This function
26532 + * returns zero in case of success and a negative error code in case of
26533 + * failure.
26534 + */
26535 +int ubifs_log_start_commit(struct ubifs_info *c, int *ltail_lnum)
26536 +{
26537 + void *buf;
26538 + struct ubifs_cs_node *cs;
26539 + struct ubifs_ref_node *ref;
26540 + int err, i, max_len, len;
26541 +
26542 + err = dbg_check_bud_bytes(c);
26543 + if (err)
26544 + return err;
26545 +
26546 + max_len = UBIFS_CS_NODE_SZ + c->jhead_cnt * UBIFS_REF_NODE_SZ;
26547 + max_len = ALIGN(max_len, c->min_io_size);
26548 + buf = cs = kmalloc(max_len, GFP_NOFS);
26549 + if (!buf)
26550 + return -ENOMEM;
26551 +
26552 + cs->ch.node_type = UBIFS_CS_NODE;
26553 + cs->cmt_no = cpu_to_le64(c->cmt_no + 1);
26554 + ubifs_prepare_node(c, cs, UBIFS_CS_NODE_SZ, 0);
26555 +
26556 + /*
26557 + * Note, we do not lock 'c->log_mutex' because this is the commit start
26558 + * phase and we are exclusively using the log. And we do not lock
26559 + * write-buffer because nobody can write to the file-system at this
26560 + * phase.
26561 + */
26562 +
26563 + len = UBIFS_CS_NODE_SZ;
26564 + for (i = 0; i < c->jhead_cnt; i++) {
26565 + int lnum = c->jheads[i].wbuf.lnum;
26566 + int offs = c->jheads[i].wbuf.offs;
26567 +
26568 + if (lnum == -1 || offs == c->leb_size)
26569 + continue;
26570 +
26571 + dbg_log("add ref to LEB %d:%d for jhead %d", lnum, offs, i);
26572 + ref = buf + len;
26573 + ref->ch.node_type = UBIFS_REF_NODE;
26574 + ref->lnum = cpu_to_le32(lnum);
26575 + ref->offs = cpu_to_le32(offs);
26576 + ref->jhead = cpu_to_le32(i);
26577 +
26578 + ubifs_prepare_node(c, ref, UBIFS_REF_NODE_SZ, 0);
26579 + len += UBIFS_REF_NODE_SZ;
26580 + }
26581 +
26582 + ubifs_pad(c, buf + len, ALIGN(len, c->min_io_size) - len);
26583 +
26584 + /* Switch to the next log LEB */
26585 + if (c->lhead_offs) {
26586 + c->lhead_lnum = next_log_lnum(c, c->lhead_lnum);
26587 + c->lhead_offs = 0;
26588 + }
26589 +
26590 + if (c->lhead_offs == 0) {
26591 + /* Must ensure next LEB has been unmapped */
26592 + err = ubifs_leb_unmap(c, c->lhead_lnum);
26593 + if (err)
26594 + goto out;
26595 + }
26596 +
26597 + len = ALIGN(len, c->min_io_size);
26598 + dbg_log("writing commit start at LEB %d:0, len %d", c->lhead_lnum, len);
26599 + err = ubifs_leb_write(c, c->lhead_lnum, cs, 0, len, UBI_SHORTTERM);
26600 + if (err)
26601 + goto out;
26602 +
26603 + *ltail_lnum = c->lhead_lnum;
26604 +
26605 + c->lhead_offs += len;
26606 + if (c->lhead_offs == c->leb_size) {
26607 + c->lhead_lnum = next_log_lnum(c, c->lhead_lnum);
26608 + c->lhead_offs = 0;
26609 + }
26610 +
26611 + remove_buds(c);
26612 +
26613 + /*
26614 + * We have started the commit and now users may use the rest of the log
26615 + * for new writes.
26616 + */
26617 + c->min_log_bytes = 0;
26618 +
26619 +out:
26620 + kfree(buf);
26621 + return err;
26622 +}
26623 +
26624 +/**
26625 + * ubifs_log_end_commit - end commit.
26626 + * @c: UBIFS file-system description object
26627 + * @ltail_lnum: new log tail LEB number
26628 + *
26629 + * This function is called on when the commit operation was finished. It
26630 + * moves log tail to new position and unmaps LEBs which contain obsolete data.
26631 + * Returns zero in case of success and a negative error code in case of
26632 + * failure.
26633 + */
26634 +int ubifs_log_end_commit(struct ubifs_info *c, int ltail_lnum)
26635 +{
26636 + int err;
26637 +
26638 + /*
26639 + * At this phase we have to lock 'c->log_mutex' because UBIFS allows FS
26640 + * writes during commit. Its only short "commit" start phase when
26641 + * writers are blocked.
26642 + */
26643 + mutex_lock(&c->log_mutex);
26644 +
26645 + dbg_log("old tail was LEB %d:0, new tail is LEB %d:0",
26646 + c->ltail_lnum, ltail_lnum);
26647 +
26648 + c->ltail_lnum = ltail_lnum;
26649 + /*
26650 + * The commit is finished and from now on it must be guaranteed that
26651 + * there is always enough space for the next commit.
26652 + */
26653 + c->min_log_bytes = c->leb_size;
26654 +
26655 + spin_lock(&c->buds_lock);
26656 + c->bud_bytes -= c->cmt_bud_bytes;
26657 + spin_unlock(&c->buds_lock);
26658 +
26659 + err = dbg_check_bud_bytes(c);
26660 +
26661 + mutex_unlock(&c->log_mutex);
26662 + return err;
26663 +}
26664 +
26665 +/**
26666 + * ubifs_log_post_commit - things to do after commit is completed.
26667 + * @c: UBIFS file-system description object
26668 + * @old_ltail_lnum: old log tail LEB number
26669 + *
26670 + * Release buds only after commit is completed, because they must be unchanged
26671 + * if recovery is needed.
26672 + *
26673 + * Unmap log LEBs only after commit is completed, because they may be needed for
26674 + * recovery.
26675 + *
26676 + * This function returns %0 on success and a negative error code on failure.
26677 + */
26678 +int ubifs_log_post_commit(struct ubifs_info *c, int old_ltail_lnum)
26679 +{
26680 + int lnum, err = 0;
26681 +
26682 + while (!list_empty(&c->old_buds)) {
26683 + struct ubifs_bud *bud;
26684 +
26685 + bud = list_entry(c->old_buds.next, struct ubifs_bud, list);
26686 + err = ubifs_return_leb(c, bud->lnum);
26687 + if (err)
26688 + return err;
26689 + list_del(&bud->list);
26690 + kfree(bud);
26691 + }
26692 + mutex_lock(&c->log_mutex);
26693 + for (lnum = old_ltail_lnum; lnum != c->ltail_lnum;
26694 + lnum = next_log_lnum(c, lnum)) {
26695 + dbg_log("unmap log LEB %d", lnum);
26696 + err = ubifs_leb_unmap(c, lnum);
26697 + if (err)
26698 + goto out;
26699 + }
26700 +out:
26701 + mutex_unlock(&c->log_mutex);
26702 + return err;
26703 +}
26704 +
26705 +/**
26706 + * struct done_ref - references that have been done.
26707 + * @rb: rb-tree node
26708 + * @lnum: LEB number
26709 + */
26710 +struct done_ref {
26711 + struct rb_node rb;
26712 + int lnum;
26713 +};
26714 +
26715 +/**
26716 + * done_already - determine if a reference has been done already.
26717 + * @done_tree: rb-tree to store references that have been done
26718 + * @lnum: LEB number of reference
26719 + *
26720 + * This function returns %1 if the reference has been done, %0 if not, otherwise
26721 + * a negative error code is returned.
26722 + */
26723 +static int done_already(struct rb_root *done_tree, int lnum)
26724 +{
26725 + struct rb_node **p = &done_tree->rb_node, *parent = NULL;
26726 + struct done_ref *dr;
26727 +
26728 + while (*p) {
26729 + parent = *p;
26730 + dr = rb_entry(parent, struct done_ref, rb);
26731 + if (lnum < dr->lnum)
26732 + p = &(*p)->rb_left;
26733 + else if (lnum > dr->lnum)
26734 + p = &(*p)->rb_right;
26735 + else
26736 + return 1;
26737 + }
26738 +
26739 + dr = kzalloc(sizeof(struct done_ref), GFP_NOFS);
26740 + if (!dr)
26741 + return -ENOMEM;
26742 +
26743 + dr->lnum = lnum;
26744 +
26745 + rb_link_node(&dr->rb, parent, p);
26746 + rb_insert_color(&dr->rb, done_tree);
26747 +
26748 + return 0;
26749 +}
26750 +
26751 +/**
26752 + * destroy_done_tree - destroy the done tree.
26753 + * @done_tree: done tree to destroy
26754 + */
26755 +static void destroy_done_tree(struct rb_root *done_tree)
26756 +{
26757 + struct rb_node *this = done_tree->rb_node;
26758 + struct done_ref *dr;
26759 +
26760 + while (this) {
26761 + if (this->rb_left) {
26762 + this = this->rb_left;
26763 + continue;
26764 + } else if (this->rb_right) {
26765 + this = this->rb_right;
26766 + continue;
26767 + }
26768 + dr = rb_entry(this, struct done_ref, rb);
26769 + this = rb_parent(this);
26770 + if (this) {
26771 + if (this->rb_left == &dr->rb)
26772 + this->rb_left = NULL;
26773 + else
26774 + this->rb_right = NULL;
26775 + }
26776 + kfree(dr);
26777 + }
26778 +}
26779 +
26780 +/**
26781 + * add_node - add a node to the consolidated log.
26782 + * @c: UBIFS file-system description object
26783 + * @buf: buffer to which to add
26784 + * @lnum: LEB number to which to write is passed and returned here
26785 + * @offs: offset to where to write is passed and returned here
26786 + * @node: node to add
26787 + *
26788 + * This function returns %0 on success and a negative error code on failure.
26789 + */
26790 +static int add_node(struct ubifs_info *c, void *buf, int *lnum, int *offs,
26791 + void *node)
26792 +{
26793 + struct ubifs_ch *ch = node;
26794 + int len = le32_to_cpu(ch->len), remains = c->leb_size - *offs;
26795 +
26796 + if (len > remains) {
26797 + int sz = ALIGN(*offs, c->min_io_size), err;
26798 +
26799 + ubifs_pad(c, buf + *offs, sz - *offs);
26800 + err = ubi_leb_change(c->ubi, *lnum, buf, sz, UBI_SHORTTERM);
26801 + if (err)
26802 + return err;
26803 + *lnum = next_log_lnum(c, *lnum);
26804 + *offs = 0;
26805 + }
26806 + memcpy(buf + *offs, node, len);
26807 + *offs += ALIGN(len, 8);
26808 + return 0;
26809 +}
26810 +
26811 +/**
26812 + * ubifs_consolidate_log - consolidate the log.
26813 + * @c: UBIFS file-system description object
26814 + *
26815 + * Repeated failed commits could cause the log to be full, but at least 1 LEB is
26816 + * needed for commit. This function rewrites the reference nodes in the log
26817 + * omitting duplicates, and failed CS nodes, and leaving no gaps.
26818 + *
26819 + * This function returns %0 on success and a negative error code on failure.
26820 + */
26821 +int ubifs_consolidate_log(struct ubifs_info *c)
26822 +{
26823 + struct ubifs_scan_leb *sleb;
26824 + struct ubifs_scan_node *snod;
26825 + struct rb_root done_tree = RB_ROOT;
26826 + int lnum, err, first = 1, write_lnum, offs = 0;
26827 + void *buf;
26828 +
26829 + dbg_rcvry("log tail LEB %d, log head LEB %d", c->ltail_lnum,
26830 + c->lhead_lnum);
26831 + buf = vmalloc(c->leb_size);
26832 + if (!buf)
26833 + return -ENOMEM;
26834 + lnum = c->ltail_lnum;
26835 + write_lnum = lnum;
26836 + while (1) {
26837 + sleb = ubifs_scan(c, lnum, 0, c->sbuf);
26838 + if (IS_ERR(sleb)) {
26839 + err = PTR_ERR(sleb);
26840 + goto out_free;
26841 + }
26842 + list_for_each_entry(snod, &sleb->nodes, list) {
26843 + switch (snod->type) {
26844 + case UBIFS_REF_NODE: {
26845 + struct ubifs_ref_node *ref = snod->node;
26846 + int ref_lnum = le32_to_cpu(ref->lnum);
26847 +
26848 + err = done_already(&done_tree, ref_lnum);
26849 + if (err < 0)
26850 + goto out_scan;
26851 + if (err != 1) {
26852 + err = add_node(c, buf, &write_lnum,
26853 + &offs, snod->node);
26854 + if (err)
26855 + goto out_scan;
26856 + }
26857 + break;
26858 + }
26859 + case UBIFS_CS_NODE:
26860 + if (!first)
26861 + break;
26862 + err = add_node(c, buf, &write_lnum, &offs,
26863 + snod->node);
26864 + if (err)
26865 + goto out_scan;
26866 + first = 0;
26867 + break;
26868 + }
26869 + }
26870 + ubifs_scan_destroy(sleb);
26871 + if (lnum == c->lhead_lnum)
26872 + break;
26873 + lnum = next_log_lnum(c, lnum);
26874 + }
26875 + if (offs) {
26876 + int sz = ALIGN(offs, c->min_io_size);
26877 +
26878 + ubifs_pad(c, buf + offs, sz - offs);
26879 + err = ubi_leb_change(c->ubi, write_lnum, buf, sz,
26880 + UBI_SHORTTERM);
26881 + if (err)
26882 + goto out_free;
26883 + offs = ALIGN(offs, c->min_io_size);
26884 + }
26885 + destroy_done_tree(&done_tree);
26886 + vfree(buf);
26887 + if (write_lnum == c->lhead_lnum) {
26888 + ubifs_err("log is too full");
26889 + return -EINVAL;
26890 + }
26891 + /* Unmap remaining LEBs */
26892 + lnum = write_lnum;
26893 + do {
26894 + lnum = next_log_lnum(c, lnum);
26895 + err = ubifs_leb_unmap(c, lnum);
26896 + if (err)
26897 + return err;
26898 + } while (lnum != c->lhead_lnum);
26899 + c->lhead_lnum = write_lnum;
26900 + c->lhead_offs = offs;
26901 + dbg_rcvry("new log head at %d:%d", c->lhead_lnum, c->lhead_offs);
26902 + return 0;
26903 +
26904 +out_scan:
26905 + ubifs_scan_destroy(sleb);
26906 +out_free:
26907 + destroy_done_tree(&done_tree);
26908 + vfree(buf);
26909 + return err;
26910 +}
26911 +
26912 +#ifdef CONFIG_UBIFS_FS_DEBUG
26913 +
26914 +/**
26915 + * dbg_check_bud_bytes - make sure bud bytes calculation are all right.
26916 + * @c: UBIFS file-system description object
26917 + *
26918 + * This function makes sure the amount of flash space used by closed buds
26919 + * ('c->bud_bytes' is correct). Returns zero in case of success and %-EINVAL in
26920 + * case of failure.
26921 + */
26922 +static int dbg_check_bud_bytes(struct ubifs_info *c)
26923 +{
26924 + int i, err = 0;
26925 + struct ubifs_bud *bud;
26926 + long long bud_bytes = 0;
26927 +
26928 + if (!(ubifs_chk_flags & UBIFS_CHK_GEN))
26929 + return 0;
26930 +
26931 + spin_lock(&c->buds_lock);
26932 + for (i = 0; i < c->jhead_cnt; i++)
26933 + list_for_each_entry(bud, &c->jheads[i].buds_list, list)
26934 + bud_bytes += c->leb_size - bud->start;
26935 +
26936 + if (c->bud_bytes != bud_bytes) {
26937 + ubifs_err("bad bud_bytes %lld, calculated %lld",
26938 + c->bud_bytes, bud_bytes);
26939 + err = -EINVAL;
26940 + }
26941 + spin_unlock(&c->buds_lock);
26942 +
26943 + return err;
26944 +}
26945 +
26946 +#endif /* CONFIG_UBIFS_FS_DEBUG */
26947 diff --exclude=.git -urN linux-2.6.25.6/fs/ubifs/lprops.c avr32-2.6/fs/ubifs/lprops.c
26948 --- linux-2.6.25.6/fs/ubifs/lprops.c 1970-01-01 01:00:00.000000000 +0100
26949 +++ avr32-2.6/fs/ubifs/lprops.c 2008-06-12 15:09:45.371816276 +0200
26950 @@ -0,0 +1,1355 @@
26951 +/*
26952 + * This file is part of UBIFS.
26953 + *
26954 + * Copyright (C) 2006-2008 Nokia Corporation.
26955 + *
26956 + * This program is free software; you can redistribute it and/or modify it
26957 + * under the terms of the GNU General Public License version 2 as published by
26958 + * the Free Software Foundation.
26959 + *
26960 + * This program is distributed in the hope that it will be useful, but WITHOUT
26961 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
26962 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
26963 + * more details.
26964 + *
26965 + * You should have received a copy of the GNU General Public License along with
26966 + * this program; if not, write to the Free Software Foundation, Inc., 51
26967 + * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26968 + *
26969 + * Authors: Adrian Hunter
26970 + * Artem Bityutskiy (Битюцкий Артём)
26971 + */
26972 +
26973 +/*
26974 + * This file implements the functions that access LEB properties and their
26975 + * categories. LEBs are categorized based on the needs of UBIFS, and the
26976 + * categories are stored as either heaps or lists to provide a fast way of
26977 + * finding a LEB in a particular category. For example, UBIFS may need to find
26978 + * an empty LEB for the journal, or a very dirty LEB for garbage collection.
26979 + */
26980 +
26981 +#include "ubifs.h"
26982 +
26983 +/**
26984 + * get_heap_comp_val - get the LEB properties value for heap comparisons.
26985 + * @lprops: LEB properties
26986 + * @cat: LEB category
26987 + */
26988 +static int get_heap_comp_val(struct ubifs_lprops *lprops, int cat)
26989 +{
26990 + switch (cat) {
26991 + case LPROPS_FREE:
26992 + return lprops->free;
26993 + case LPROPS_DIRTY_IDX:
26994 + return lprops->free + lprops->dirty;
26995 + default:
26996 + return lprops->dirty;
26997 + }
26998 +}
26999 +
27000 +/**
27001 + * move_up_lpt_heap - move a new heap entry up as far as possible.
27002 + * @c: UBIFS file-system description object
27003 + * @heap: LEB category heap
27004 + * @lprops: LEB properties to move
27005 + * @cat: LEB category
27006 + *
27007 + * New entries to a heap are added at the bottom and then moved up until the
27008 + * parent's value is greater. In the case of LPT's category heaps, the value
27009 + * is either the amount of free space or the amount of dirty space, depending
27010 + * on the category.
27011 + */
27012 +static void move_up_lpt_heap(struct ubifs_info *c, struct ubifs_lpt_heap *heap,
27013 + struct ubifs_lprops *lprops, int cat)
27014 +{
27015 + int val1, val2, hpos;
27016 +
27017 + hpos = lprops->hpos;
27018 + if (!hpos)
27019 + return; /* Already top of the heap */
27020 + val1 = get_heap_comp_val(lprops, cat);
27021 + /* Compare to parent and, if greater, move up the heap */
27022 + do {
27023 + int ppos = (hpos - 1) / 2;
27024 +
27025 + val2 = get_heap_comp_val(heap->arr[ppos], cat);
27026 + if (val2 >= val1)
27027 + return;
27028 + /* Greater than parent so move up */
27029 + heap->arr[ppos]->hpos = hpos;
27030 + heap->arr[hpos] = heap->arr[ppos];
27031 + heap->arr[ppos] = lprops;
27032 + lprops->hpos = ppos;
27033 + hpos = ppos;
27034 + } while (hpos);
27035 +}
27036 +
27037 +/**
27038 + * adjust_lpt_heap - move a changed heap entry up or down the heap.
27039 + * @c: UBIFS file-system description object
27040 + * @heap: LEB category heap
27041 + * @lprops: LEB properties to move
27042 + * @hpos: heap position of @lprops
27043 + * @cat: LEB category
27044 + *
27045 + * Changed entries in a heap are moved up or down until the parent's value is
27046 + * greater. In the case of LPT's category heaps, the value is either the amount
27047 + * of free space or the amount of dirty space, depending on the category.
27048 + */
27049 +static void adjust_lpt_heap(struct ubifs_info *c, struct ubifs_lpt_heap *heap,
27050 + struct ubifs_lprops *lprops, int hpos, int cat)
27051 +{
27052 + int val1, val2, val3, cpos;
27053 +
27054 + val1 = get_heap_comp_val(lprops, cat);
27055 + /* Compare to parent and, if greater than parent, move up the heap */
27056 + if (hpos) {
27057 + int ppos = (hpos - 1) / 2;
27058 +
27059 + val2 = get_heap_comp_val(heap->arr[ppos], cat);
27060 + if (val1 > val2) {
27061 + /* Greater than parent so move up */
27062 + while (1) {
27063 + heap->arr[ppos]->hpos = hpos;
27064 + heap->arr[hpos] = heap->arr[ppos];
27065 + heap->arr[ppos] = lprops;
27066 + lprops->hpos = ppos;
27067 + hpos = ppos;
27068 + if (!hpos)
27069 + return;
27070 + ppos = (hpos - 1) / 2;
27071 + val2 = get_heap_comp_val(heap->arr[ppos], cat);
27072 + if (val1 <= val2)
27073 + return;
27074 + /* Still greater than parent so keep going */
27075 + }
27076 + }
27077 + }
27078 + /* Not greater than parent, so compare to children */
27079 + while (1) {
27080 + /* Compare to left child */
27081 + cpos = hpos * 2 + 1;
27082 + if (cpos >= heap->cnt)
27083 + return;
27084 + val2 = get_heap_comp_val(heap->arr[cpos], cat);
27085 + if (val1 < val2) {
27086 + /* Less than left child, so promote biggest child */
27087 + if (cpos + 1 < heap->cnt) {
27088 + val3 = get_heap_comp_val(heap->arr[cpos + 1],
27089 + cat);
27090 + if (val3 > val2)
27091 + cpos += 1; /* Right child is bigger */
27092 + }
27093 + heap->arr[cpos]->hpos = hpos;
27094 + heap->arr[hpos] = heap->arr[cpos];
27095 + heap->arr[cpos] = lprops;
27096 + lprops->hpos = cpos;
27097 + hpos = cpos;
27098 + continue;
27099 + }
27100 + /* Compare to right child */
27101 + cpos += 1;
27102 + if (cpos >= heap->cnt)
27103 + return;
27104 + val3 = get_heap_comp_val(heap->arr[cpos], cat);
27105 + if (val1 < val3) {
27106 + /* Less than right child, so promote right child */
27107 + heap->arr[cpos]->hpos = hpos;
27108 + heap->arr[hpos] = heap->arr[cpos];
27109 + heap->arr[cpos] = lprops;
27110 + lprops->hpos = cpos;
27111 + hpos = cpos;
27112 + continue;
27113 + }
27114 + return;
27115 + }
27116 +}
27117 +
27118 +/**
27119 + * add_to_lpt_heap - add LEB properties to a LEB category heap.
27120 + * @c: UBIFS file-system description object
27121 + * @lprops: LEB properties to add
27122 + * @cat: LEB category
27123 + *
27124 + * This function returns %1 if @lprops is added to the heap for LEB category
27125 + * @cat, otherwise %0 is returned because the heap is full.
27126 + */
27127 +static int add_to_lpt_heap(struct ubifs_info *c, struct ubifs_lprops *lprops,
27128 + int cat)
27129 +{
27130 + struct ubifs_lpt_heap *heap = &c->lpt_heap[cat - 1];
27131 +
27132 + if (heap->cnt >= heap->max_cnt) {
27133 + const int b = LPT_HEAP_SZ / 2 - 1;
27134 + int cpos, val1, val2;
27135 +
27136 + /* Compare to some other LEB on the bottom of heap */
27137 + /* Pick a position kind of randomly */
27138 + cpos = (((size_t)lprops >> 4) & b) + b;
27139 + ubifs_assert(cpos >= b);
27140 + ubifs_assert(cpos < LPT_HEAP_SZ);
27141 + ubifs_assert(cpos < heap->cnt);
27142 +
27143 + val1 = get_heap_comp_val(lprops, cat);
27144 + val2 = get_heap_comp_val(heap->arr[cpos], cat);
27145 + if (val1 > val2) {
27146 + struct ubifs_lprops *lp;
27147 +
27148 + lp = heap->arr[cpos];
27149 + lp->flags &= ~LPROPS_CAT_MASK;
27150 + lp->flags |= LPROPS_UNCAT;
27151 + list_add(&lp->list, &c->uncat_list);
27152 + lprops->hpos = cpos;
27153 + heap->arr[cpos] = lprops;
27154 + move_up_lpt_heap(c, heap, lprops, cat);
27155 + dbg_check_heap(c, heap, cat, lprops->hpos);
27156 + return 1; /* Added to heap */
27157 + }
27158 + dbg_check_heap(c, heap, cat, -1);
27159 + return 0; /* Not added to heap */
27160 + } else {
27161 + lprops->hpos = heap->cnt++;
27162 + heap->arr[lprops->hpos] = lprops;
27163 + move_up_lpt_heap(c, heap, lprops, cat);
27164 + dbg_check_heap(c, heap, cat, lprops->hpos);
27165 + return 1; /* Added to heap */
27166 + }
27167 +}
27168 +
27169 +/**
27170 + * remove_from_lpt_heap - remove LEB properties from a LEB category heap.
27171 + * @c: UBIFS file-system description object
27172 + * @lprops: LEB properties to remove
27173 + * @cat: LEB category
27174 + */
27175 +static void remove_from_lpt_heap(struct ubifs_info *c,
27176 + struct ubifs_lprops *lprops, int cat)
27177 +{
27178 + struct ubifs_lpt_heap *heap;
27179 + int hpos = lprops->hpos;
27180 +
27181 + heap = &c->lpt_heap[cat - 1];
27182 + ubifs_assert(hpos >= 0 && hpos < heap->cnt);
27183 + ubifs_assert(heap->arr[hpos] == lprops);
27184 + heap->cnt -= 1;
27185 + if (hpos < heap->cnt) {
27186 + heap->arr[hpos] = heap->arr[heap->cnt];
27187 + heap->arr[hpos]->hpos = hpos;
27188 + adjust_lpt_heap(c, heap, heap->arr[hpos], hpos, cat);
27189 + }
27190 + dbg_check_heap(c, heap, cat, -1);
27191 +}
27192 +
27193 +/**
27194 + * lpt_heap_replace - replace lprops in a category heap.
27195 + * @c: UBIFS file-system description object
27196 + * @old_lprops: LEB properties to replace
27197 + * @new_lprops: LEB properties with which to replace
27198 + * @cat: LEB category
27199 + *
27200 + * During commit it is sometimes necessary to copy a pnode (see dirty_cow_pnode)
27201 + * and the lprops that the pnode contains. When that happens, references in
27202 + * the category heaps to those lprops must be updated to point to the new
27203 + * lprops. This function does that.
27204 + */
27205 +static void lpt_heap_replace(struct ubifs_info *c,
27206 + struct ubifs_lprops *old_lprops,
27207 + struct ubifs_lprops *new_lprops, int cat)
27208 +{
27209 + struct ubifs_lpt_heap *heap;
27210 + int hpos = new_lprops->hpos;
27211 +
27212 + heap = &c->lpt_heap[cat - 1];
27213 + heap->arr[hpos] = new_lprops;
27214 +}
27215 +
27216 +/**
27217 + * ubifs_add_to_cat - add LEB properties to a category list or heap.
27218 + * @c: UBIFS file-system description object
27219 + * @lprops: LEB properties to add
27220 + * @cat: LEB category to which to add
27221 + *
27222 + * LEB properties are categorized to enable fast find operations.
27223 + */
27224 +void ubifs_add_to_cat(struct ubifs_info *c, struct ubifs_lprops *lprops,
27225 + int cat)
27226 +{
27227 + switch (cat) {
27228 + case LPROPS_DIRTY:
27229 + case LPROPS_DIRTY_IDX:
27230 + case LPROPS_FREE:
27231 + if (add_to_lpt_heap(c, lprops, cat))
27232 + break;
27233 + /* No more room on heap so make it uncategorized */
27234 + cat = LPROPS_UNCAT;
27235 + /* Fall through */
27236 + case LPROPS_UNCAT:
27237 + list_add(&lprops->list, &c->uncat_list);
27238 + break;
27239 + case LPROPS_EMPTY:
27240 + list_add(&lprops->list, &c->empty_list);
27241 + break;
27242 + case LPROPS_FREEABLE:
27243 + list_add(&lprops->list, &c->freeable_list);
27244 + c->freeable_cnt += 1;
27245 + break;
27246 + case LPROPS_FRDI_IDX:
27247 + list_add(&lprops->list, &c->frdi_idx_list);
27248 + break;
27249 + default:
27250 + ubifs_assert(0);
27251 + }
27252 + lprops->flags &= ~LPROPS_CAT_MASK;
27253 + lprops->flags |= cat;
27254 +}
27255 +
27256 +/**
27257 + * ubifs_remove_from_cat - remove LEB properties from a category list or heap.
27258 + * @c: UBIFS file-system description object
27259 + * @lprops: LEB properties to remove
27260 + * @cat: LEB category from which to remove
27261 + *
27262 + * LEB properties are categorized to enable fast find operations.
27263 + */
27264 +static void ubifs_remove_from_cat(struct ubifs_info *c,
27265 + struct ubifs_lprops *lprops, int cat)
27266 +{
27267 + switch (cat) {
27268 + case LPROPS_DIRTY:
27269 + case LPROPS_DIRTY_IDX:
27270 + case LPROPS_FREE:
27271 + remove_from_lpt_heap(c, lprops, cat);
27272 + break;
27273 + case LPROPS_FREEABLE:
27274 + c->freeable_cnt -= 1;
27275 + ubifs_assert(c->freeable_cnt >= 0);
27276 + /* Fall through */
27277 + case LPROPS_UNCAT:
27278 + case LPROPS_EMPTY:
27279 + case LPROPS_FRDI_IDX:
27280 + ubifs_assert(!list_empty(&lprops->list));
27281 + list_del(&lprops->list);
27282 + break;
27283 + default:
27284 + ubifs_assert(0);
27285 + }
27286 +}
27287 +
27288 +/**
27289 + * ubifs_replace_cat - replace lprops in a category list or heap.
27290 + * @c: UBIFS file-system description object
27291 + * @old_lprops: LEB properties to replace
27292 + * @new_lprops: LEB properties with which to replace
27293 + *
27294 + * During commit it is sometimes necessary to copy a pnode (see dirty_cow_pnode)
27295 + * and the lprops that the pnode contains. When that happens, references in
27296 + * category lists and heaps must be replaced. This function does that.
27297 + */
27298 +void ubifs_replace_cat(struct ubifs_info *c, struct ubifs_lprops *old_lprops,
27299 + struct ubifs_lprops *new_lprops)
27300 +{
27301 + int cat;
27302 +
27303 + cat = new_lprops->flags & LPROPS_CAT_MASK;
27304 + switch (cat) {
27305 + case LPROPS_DIRTY:
27306 + case LPROPS_DIRTY_IDX:
27307 + case LPROPS_FREE:
27308 + lpt_heap_replace(c, old_lprops, new_lprops, cat);
27309 + break;
27310 + case LPROPS_UNCAT:
27311 + case LPROPS_EMPTY:
27312 + case LPROPS_FREEABLE:
27313 + case LPROPS_FRDI_IDX:
27314 + list_replace(&old_lprops->list, &new_lprops->list);
27315 + break;
27316 + default:
27317 + ubifs_assert(0);
27318 + }
27319 +}
27320 +
27321 +/**
27322 + * ubifs_ensure_cat - ensure LEB properties are categorized.
27323 + * @c: UBIFS file-system description object
27324 + * @lprops: LEB properties
27325 + *
27326 + * A LEB may have fallen off of the bottom of a heap, and ended up as
27327 + * uncategorized even though it has enough space for us now. If that is the case
27328 + * this function will put the LEB back onto a heap.
27329 + */
27330 +void ubifs_ensure_cat(struct ubifs_info *c, struct ubifs_lprops *lprops)
27331 +{
27332 + int cat = lprops->flags & LPROPS_CAT_MASK;
27333 +
27334 + if (cat != LPROPS_UNCAT)
27335 + return;
27336 + cat = ubifs_categorize_lprops(c, lprops);
27337 + if (cat == LPROPS_UNCAT)
27338 + return;
27339 + ubifs_remove_from_cat(c, lprops, LPROPS_UNCAT);
27340 + ubifs_add_to_cat(c, lprops, cat);
27341 +}
27342 +
27343 +/**
27344 + * ubifs_categorize_lprops - categorize LEB properties.
27345 + * @c: UBIFS file-system description object
27346 + * @lprops: LEB properties to categorize
27347 + *
27348 + * LEB properties are categorized to enable fast find operations. This function
27349 + * returns the LEB category to which the LEB properties belong. Note however
27350 + * that if the LEB category is stored as a heap and the heap is full, the
27351 + * LEB properties may have their category changed to %LPROPS_UNCAT.
27352 + */
27353 +int ubifs_categorize_lprops(const struct ubifs_info *c,
27354 + const struct ubifs_lprops *lprops)
27355 +{
27356 + if (lprops->flags & LPROPS_TAKEN)
27357 + return LPROPS_UNCAT;
27358 +
27359 + if (lprops->free == c->leb_size) {
27360 + ubifs_assert(!(lprops->flags & LPROPS_INDEX));
27361 + return LPROPS_EMPTY;
27362 + }
27363 +
27364 + if (lprops->free + lprops->dirty == c->leb_size) {
27365 + if (lprops->flags & LPROPS_INDEX)
27366 + return LPROPS_FRDI_IDX;
27367 + else
27368 + return LPROPS_FREEABLE;
27369 + }
27370 +
27371 + if (lprops->flags & LPROPS_INDEX) {
27372 + if (lprops->dirty + lprops->free >= c->min_idx_node_sz)
27373 + return LPROPS_DIRTY_IDX;
27374 + } else {
27375 + if (lprops->dirty >= c->dead_wm &&
27376 + lprops->dirty > lprops->free)
27377 + return LPROPS_DIRTY;
27378 + if (lprops->free > 0)
27379 + return LPROPS_FREE;
27380 + }
27381 +
27382 + return LPROPS_UNCAT;
27383 +}
27384 +
27385 +/**
27386 + * change_category - change LEB properties category.
27387 + * @c: UBIFS file-system description object
27388 + * @lprops: LEB properties to recategorize
27389 + *
27390 + * LEB properties are categorized to enable fast find operations. When the LEB
27391 + * properties change they must be recategorized.
27392 + */
27393 +static void change_category(struct ubifs_info *c, struct ubifs_lprops *lprops)
27394 +{
27395 + int old_cat = lprops->flags & LPROPS_CAT_MASK;
27396 + int new_cat = ubifs_categorize_lprops(c, lprops);
27397 +
27398 + if (old_cat == new_cat) {
27399 + struct ubifs_lpt_heap *heap = &c->lpt_heap[new_cat - 1];
27400 +
27401 + /* lprops on a heap now must be moved up or down */
27402 + if (new_cat < 1 || new_cat > LPROPS_HEAP_CNT)
27403 + return; /* Not on a heap */
27404 + heap = &c->lpt_heap[new_cat - 1];
27405 + adjust_lpt_heap(c, heap, lprops, lprops->hpos, new_cat);
27406 + } else {
27407 + ubifs_remove_from_cat(c, lprops, old_cat);
27408 + ubifs_add_to_cat(c, lprops, new_cat);
27409 + }
27410 +}
27411 +
27412 +/**
27413 + * ubifs_get_lprops - get reference to LEB properties.
27414 + * @c: the UBIFS file-system description object
27415 + *
27416 + * This function locks lprops. Lprops have to be unlocked by
27417 + * 'ubifs_release_lprops()'.
27418 + */
27419 +void ubifs_get_lprops(struct ubifs_info *c)
27420 +{
27421 + mutex_lock(&c->lp_mutex);
27422 +}
27423 +
27424 +/**
27425 + * calc_dark - calculate LEB dark space size.
27426 + * @c: the UBIFS file-system description object
27427 + * @spc: amount of free and dirty space in the LEB
27428 + *
27429 + * This function calculates amount of dark space in an LEB which has @spc bytes
27430 + * of free and dirty space. Returns the calculations result.
27431 + *
27432 + * Dark space is the space which is not always usable - it depends on which
27433 + * nodes are written in which order. E.g., if an LEB has only 512 free bytes,
27434 + * it is dark space, because it cannot fit a large data node. So UBIFS cannot
27435 + * count on this LEB and treat these 512 bytes as usable because it is not true
27436 + * if, for example, only big chunks of uncompressible data will be written to
27437 + * the FS.
27438 + */
27439 +static int calc_dark(struct ubifs_info *c, int spc)
27440 +{
27441 + ubifs_assert(!(spc & 7));
27442 +
27443 + if (spc < c->dark_wm)
27444 + return spc;
27445 +
27446 + /*
27447 + * If we have slightly more space then the dark space watermark, we can
27448 + * anyway safely assume it we'll be able to write a node of the
27449 + * smallest size there.
27450 + */
27451 + if (spc - c->dark_wm < MIN_WRITE_SZ)
27452 + return spc - MIN_WRITE_SZ;
27453 +
27454 + return c->dark_wm;
27455 +}
27456 +
27457 +/**
27458 + * is_lprops_dirty - determine if LEB properties are dirty.
27459 + * @c: the UBIFS file-system description object
27460 + * @lprops: LEB properties to test
27461 + */
27462 +static int is_lprops_dirty(struct ubifs_info *c, struct ubifs_lprops *lprops)
27463 +{
27464 + struct ubifs_pnode *pnode;
27465 + int pos;
27466 +
27467 + pos = (lprops->lnum - c->main_first) & (UBIFS_LPT_FANOUT - 1);
27468 + pnode = (struct ubifs_pnode *)container_of(lprops - pos,
27469 + struct ubifs_pnode,
27470 + lprops[0]);
27471 + return !test_bit(COW_ZNODE, &pnode->flags) &&
27472 + test_bit(DIRTY_CNODE, &pnode->flags);
27473 +}
27474 +
27475 +/**
27476 + * ubifs_change_lp - change LEB properties.
27477 + * @c: the UBIFS file-system description object
27478 + * @lp: LEB properties to change
27479 + * @free: new free space amount
27480 + * @dirty: new dirty space amount
27481 + * @flags: new flags
27482 + * @idx_gc_cnt: change to the count of idx_gc list
27483 + *
27484 + * This function changes LEB properties. This function does not change a LEB
27485 + * property (@free, @dirty or @flag) if the value passed is %LPROPS_NC.
27486 + *
27487 + * This function returns a pointer to the updated LEB properties on success
27488 + * and a negative error code on failure. N.B. the LEB properties may have had to
27489 + * be copied (due to COW) and consequently the pointer returned may not be the
27490 + * same as the pointer passed.
27491 + */
27492 +const struct ubifs_lprops *ubifs_change_lp(struct ubifs_info *c,
27493 + const struct ubifs_lprops *lp,
27494 + int free, int dirty, int flags,
27495 + int idx_gc_cnt)
27496 +{
27497 + /*
27498 + * This is the only function that is allowed to change lprops, so we
27499 + * discard the const qualifier.
27500 + */
27501 + struct ubifs_lprops *lprops = (struct ubifs_lprops *)lp;
27502 +
27503 + dbg_lp("LEB %d, free %d, dirty %d, flags %d",
27504 + lprops->lnum, free, dirty, flags);
27505 +
27506 + ubifs_assert(mutex_is_locked(&c->lp_mutex));
27507 + ubifs_assert(c->lst.empty_lebs >= 0 &&
27508 + c->lst.empty_lebs <= c->main_lebs);
27509 + ubifs_assert(c->freeable_cnt >= 0);
27510 + ubifs_assert(c->freeable_cnt <= c->main_lebs);
27511 + ubifs_assert(c->lst.taken_empty_lebs >= 0);
27512 + ubifs_assert(c->lst.taken_empty_lebs <= c->lst.empty_lebs);
27513 + ubifs_assert(!(c->lst.total_free & 7) && !(c->lst.total_dirty & 7));
27514 + ubifs_assert(!(c->lst.total_dead & 7) && !(c->lst.total_dark & 7));
27515 + ubifs_assert(!(c->lst.total_used & 7));
27516 + ubifs_assert(free == LPROPS_NC || free >= 0);
27517 + ubifs_assert(dirty == LPROPS_NC || dirty >= 0);
27518 +
27519 + if (!is_lprops_dirty(c, lprops)) {
27520 + lprops = ubifs_lpt_lookup_dirty(c, lprops->lnum);
27521 + if (IS_ERR(lprops))
27522 + return lprops;
27523 + } else
27524 + ubifs_assert(lprops == ubifs_lpt_lookup_dirty(c, lprops->lnum));
27525 +
27526 + ubifs_assert(!(lprops->free & 7) && !(lprops->dirty & 7));
27527 +
27528 + spin_lock(&c->space_lock);
27529 +
27530 + if ((lprops->flags & LPROPS_TAKEN) && lprops->free == c->leb_size)
27531 + c->lst.taken_empty_lebs -= 1;
27532 +
27533 + if (!(lprops->flags & LPROPS_INDEX)) {
27534 + int old_spc;
27535 +
27536 + old_spc = lprops->free + lprops->dirty;
27537 + if (old_spc < c->dead_wm)
27538 + c->lst.total_dead -= old_spc;
27539 + else
27540 + c->lst.total_dark -= calc_dark(c, old_spc);
27541 +
27542 + c->lst.total_used -= c->leb_size - old_spc;
27543 + }
27544 +
27545 + if (free != LPROPS_NC) {
27546 + free = ALIGN(free, 8);
27547 + c->lst.total_free += free - lprops->free;
27548 +
27549 + /* Increase or decrease empty LEBs counter if needed */
27550 + if (free == c->leb_size) {
27551 + if (lprops->free != c->leb_size)
27552 + c->lst.empty_lebs += 1;
27553 + } else if (lprops->free == c->leb_size)
27554 + c->lst.empty_lebs -= 1;
27555 + lprops->free = free;
27556 + }
27557 +
27558 + if (dirty != LPROPS_NC) {
27559 + dirty = ALIGN(dirty, 8);
27560 + c->lst.total_dirty += dirty - lprops->dirty;
27561 + lprops->dirty = dirty;
27562 + }
27563 +
27564 + if (flags != LPROPS_NC) {
27565 + /* Take care about indexing LEBs counter if needed */
27566 + if ((lprops->flags & LPROPS_INDEX)) {
27567 + if (!(flags & LPROPS_INDEX))
27568 + c->lst.idx_lebs -= 1;
27569 + } else if (flags & LPROPS_INDEX)
27570 + c->lst.idx_lebs += 1;
27571 + lprops->flags = flags;
27572 + }
27573 +
27574 + if (!(lprops->flags & LPROPS_INDEX)) {
27575 + int new_spc;
27576 +
27577 + new_spc = lprops->free + lprops->dirty;
27578 + if (new_spc < c->dead_wm)
27579 + c->lst.total_dead += new_spc;
27580 + else
27581 + c->lst.total_dark += calc_dark(c, new_spc);
27582 +
27583 + c->lst.total_used += c->leb_size - new_spc;
27584 + }
27585 +
27586 + if ((lprops->flags & LPROPS_TAKEN) && lprops->free == c->leb_size)
27587 + c->lst.taken_empty_lebs += 1;
27588 +
27589 + change_category(c, lprops);
27590 +
27591 + c->idx_gc_cnt += idx_gc_cnt;
27592 +
27593 + spin_unlock(&c->space_lock);
27594 +
27595 + return lprops;
27596 +}
27597 +
27598 +/**
27599 + * ubifs_release_lprops - release lprops lock.
27600 + * @c: the UBIFS file-system description object
27601 + *
27602 + * This function has to be called after each 'ubifs_get_lprops()' call to
27603 + * unlock lprops.
27604 + */
27605 +void ubifs_release_lprops(struct ubifs_info *c)
27606 +{
27607 + ubifs_assert(mutex_is_locked(&c->lp_mutex));
27608 + ubifs_assert(c->lst.empty_lebs >= 0 &&
27609 + c->lst.empty_lebs <= c->main_lebs);
27610 +
27611 + mutex_unlock(&c->lp_mutex);
27612 +}
27613 +
27614 +/**
27615 + * ubifs_get_lp_stats - get lprops statistics.
27616 + * @c: UBIFS file-system description object
27617 + * @st: return statistics
27618 + */
27619 +void ubifs_get_lp_stats(struct ubifs_info *c, struct ubifs_lp_stats *st)
27620 +{
27621 + spin_lock(&c->space_lock);
27622 + memcpy(st, &c->lst, sizeof(struct ubifs_lp_stats));
27623 + spin_unlock(&c->space_lock);
27624 +}
27625 +
27626 +/**
27627 + * ubifs_change_one_lp - change LEB properties.
27628 + * @c: the UBIFS file-system description object
27629 + * @lnum: LEB to change properties for
27630 + * @free: amount of free space
27631 + * @dirty: amount of dirty space
27632 + * @flags_set: flags to set
27633 + * @flags_clean: flags to clean
27634 + * @idx_gc_cnt: change to the count of idx_gc list
27635 + *
27636 + * This function changes properties of LEB @lnum. It is a helper wrapper over
27637 + * 'ubifs_change_lp()' which hides lprops get/release. The arguments are the
27638 + * same as in case of 'ubifs_change_lp()'. Returns zero in case of success and
27639 + * a negative error code in case of failure.
27640 + */
27641 +int ubifs_change_one_lp(struct ubifs_info *c, int lnum, int free, int dirty,
27642 + int flags_set, int flags_clean, int idx_gc_cnt)
27643 +{
27644 + int err = 0, flags;
27645 + const struct ubifs_lprops *lp;
27646 +
27647 + ubifs_get_lprops(c);
27648 +
27649 + lp = ubifs_lpt_lookup_dirty(c, lnum);
27650 + if (IS_ERR(lp)) {
27651 + err = PTR_ERR(lp);
27652 + goto out;
27653 + }
27654 +
27655 + flags = (lp->flags | flags_set) & ~flags_clean;
27656 + lp = ubifs_change_lp(c, lp, free, dirty, flags, idx_gc_cnt);
27657 + if (IS_ERR(lp))
27658 + err = PTR_ERR(lp);
27659 +
27660 +out:
27661 + ubifs_release_lprops(c);
27662 + return err;
27663 +}
27664 +
27665 +/**
27666 + * ubifs_update_one_lp - update LEB properties.
27667 + * @c: the UBIFS file-system description object
27668 + * @lnum: LEB to change properties for
27669 + * @free: amount of free space
27670 + * @dirty: amount of dirty space to add
27671 + * @flags_set: flags to set
27672 + * @flags_clean: flags to clean
27673 + *
27674 + * This function is the same as 'ubifs_change_one_lp()' but @dirty is added to
27675 + * current dirty space, not substitutes it.
27676 + */
27677 +int ubifs_update_one_lp(struct ubifs_info *c, int lnum, int free, int dirty,
27678 + int flags_set, int flags_clean)
27679 +{
27680 + int err = 0, flags;
27681 + const struct ubifs_lprops *lp;
27682 +
27683 + ubifs_get_lprops(c);
27684 +
27685 + lp = ubifs_lpt_lookup_dirty(c, lnum);
27686 + if (IS_ERR(lp)) {
27687 + err = PTR_ERR(lp);
27688 + goto out;
27689 + }
27690 +
27691 + flags = (lp->flags | flags_set) & ~flags_clean;
27692 + lp = ubifs_change_lp(c, lp, free, lp->dirty + dirty, flags, 0);
27693 + if (IS_ERR(lp))
27694 + err = PTR_ERR(lp);
27695 +
27696 +out:
27697 + ubifs_release_lprops(c);
27698 + return err;
27699 +}
27700 +
27701 +/**
27702 + * ubifs_read_one_lp - read LEB properties.
27703 + * @c: the UBIFS file-system description object
27704 + * @lnum: LEB to read properties for
27705 + * @lp: where to store read properties
27706 + *
27707 + * This helper function reads properties of a LEB @lnum and stores them in @lp.
27708 + * Returns zero in case of success and a negative error code in case of
27709 + * failure.
27710 + */
27711 +int ubifs_read_one_lp(struct ubifs_info *c, int lnum, struct ubifs_lprops *lp)
27712 +{
27713 + int err = 0;
27714 + const struct ubifs_lprops *lpp;
27715 +
27716 + ubifs_get_lprops(c);
27717 +
27718 + lpp = ubifs_lpt_lookup(c, lnum);
27719 + if (IS_ERR(lpp)) {
27720 + err = PTR_ERR(lpp);
27721 + goto out;
27722 + }
27723 +
27724 + memcpy(lp, lpp, sizeof(struct ubifs_lprops));
27725 +
27726 +out:
27727 + ubifs_release_lprops(c);
27728 + return err;
27729 +}
27730 +
27731 +/**
27732 + * ubifs_fast_find_free - try to find a LEB with free space quickly.
27733 + * @c: the UBIFS file-system description object
27734 + *
27735 + * This function returns LEB properties for a LEB with free space or %NULL if
27736 + * the function is unable to find a LEB quickly.
27737 + */
27738 +const struct ubifs_lprops *ubifs_fast_find_free(struct ubifs_info *c)
27739 +{
27740 + struct ubifs_lprops *lprops;
27741 + struct ubifs_lpt_heap *heap;
27742 +
27743 + ubifs_assert(mutex_is_locked(&c->lp_mutex));
27744 +
27745 + heap = &c->lpt_heap[LPROPS_FREE - 1];
27746 + if (heap->cnt == 0)
27747 + return NULL;
27748 +
27749 + lprops = heap->arr[0];
27750 + ubifs_assert(!(lprops->flags & LPROPS_TAKEN));
27751 + ubifs_assert(!(lprops->flags & LPROPS_INDEX));
27752 + return lprops;
27753 +}
27754 +
27755 +/**
27756 + * ubifs_fast_find_empty - try to find an empty LEB quickly.
27757 + * @c: the UBIFS file-system description object
27758 + *
27759 + * This function returns LEB properties for an empty LEB or %NULL if the
27760 + * function is unable to find an empty LEB quickly.
27761 + */
27762 +const struct ubifs_lprops *ubifs_fast_find_empty(struct ubifs_info *c)
27763 +{
27764 + struct ubifs_lprops *lprops;
27765 +
27766 + ubifs_assert(mutex_is_locked(&c->lp_mutex));
27767 +
27768 + if (list_empty(&c->empty_list))
27769 + return NULL;
27770 +
27771 + lprops = list_entry(c->empty_list.next, struct ubifs_lprops, list);
27772 + ubifs_assert(!(lprops->flags & LPROPS_TAKEN));
27773 + ubifs_assert(!(lprops->flags & LPROPS_INDEX));
27774 + ubifs_assert(lprops->free == c->leb_size);
27775 + return lprops;
27776 +}
27777 +
27778 +/**
27779 + * ubifs_fast_find_freeable - try to find a freeable LEB quickly.
27780 + * @c: the UBIFS file-system description object
27781 + *
27782 + * This function returns LEB properties for a freeable LEB or %NULL if the
27783 + * function is unable to find a freeable LEB quickly.
27784 + */
27785 +const struct ubifs_lprops *ubifs_fast_find_freeable(struct ubifs_info *c)
27786 +{
27787 + struct ubifs_lprops *lprops;
27788 +
27789 + ubifs_assert(mutex_is_locked(&c->lp_mutex));
27790 +
27791 + if (list_empty(&c->freeable_list))
27792 + return NULL;
27793 +
27794 + lprops = list_entry(c->freeable_list.next, struct ubifs_lprops, list);
27795 + ubifs_assert(!(lprops->flags & LPROPS_TAKEN));
27796 + ubifs_assert(!(lprops->flags & LPROPS_INDEX));
27797 + ubifs_assert(lprops->free + lprops->dirty == c->leb_size);
27798 + ubifs_assert(c->freeable_cnt > 0);
27799 + return lprops;
27800 +}
27801 +
27802 +/**
27803 + * ubifs_fast_find_frdi_idx - try to find a freeable index LEB quickly.
27804 + * @c: the UBIFS file-system description object
27805 + *
27806 + * This function returns LEB properties for a freeable index LEB or %NULL if the
27807 + * function is unable to find a freeable index LEB quickly.
27808 + */
27809 +const struct ubifs_lprops *ubifs_fast_find_frdi_idx(struct ubifs_info *c)
27810 +{
27811 + struct ubifs_lprops *lprops;
27812 +
27813 + ubifs_assert(mutex_is_locked(&c->lp_mutex));
27814 +
27815 + if (list_empty(&c->frdi_idx_list))
27816 + return NULL;
27817 +
27818 + lprops = list_entry(c->frdi_idx_list.next, struct ubifs_lprops, list);
27819 + ubifs_assert(!(lprops->flags & LPROPS_TAKEN));
27820 + ubifs_assert((lprops->flags & LPROPS_INDEX));
27821 + ubifs_assert(lprops->free + lprops->dirty == c->leb_size);
27822 + return lprops;
27823 +}
27824 +
27825 +#ifdef CONFIG_UBIFS_FS_DEBUG
27826 +
27827 +/**
27828 + * dbg_check_cats - check category heaps and lists.
27829 + * @c: UBIFS file-system description object
27830 + *
27831 + * This function returns %0 on success and a negative error code on failure.
27832 + */
27833 +int dbg_check_cats(struct ubifs_info *c)
27834 +{
27835 + struct ubifs_lprops *lprops;
27836 + struct list_head *pos;
27837 + int i, cat;
27838 +
27839 + if (!(ubifs_chk_flags & (UBIFS_CHK_GEN | UBIFS_CHK_LPROPS)))
27840 + return 0;
27841 +
27842 + list_for_each_entry(lprops, &c->empty_list, list) {
27843 + if (lprops->free != c->leb_size) {
27844 + ubifs_err("non-empty LEB %d on empty list "
27845 + "(free %d dirty %d flags %d)", lprops->lnum,
27846 + lprops->free, lprops->dirty, lprops->flags);
27847 + return -EINVAL;
27848 + }
27849 + if (lprops->flags & LPROPS_TAKEN) {
27850 + ubifs_err("taken LEB %d on empty list "
27851 + "(free %d dirty %d flags %d)", lprops->lnum,
27852 + lprops->free, lprops->dirty, lprops->flags);
27853 + return -EINVAL;
27854 + }
27855 + }
27856 +
27857 + i = 0;
27858 + list_for_each_entry(lprops, &c->freeable_list, list) {
27859 + if (lprops->free + lprops->dirty != c->leb_size) {
27860 + ubifs_err("non-freeable LEB %d on freeable list "
27861 + "(free %d dirty %d flags %d)", lprops->lnum,
27862 + lprops->free, lprops->dirty, lprops->flags);
27863 + return -EINVAL;
27864 + }
27865 + if (lprops->flags & LPROPS_TAKEN) {
27866 + ubifs_err("taken LEB %d on freeable list "
27867 + "(free %d dirty %d flags %d)", lprops->lnum,
27868 + lprops->free, lprops->dirty, lprops->flags);
27869 + return -EINVAL;
27870 + }
27871 + i += 1;
27872 + }
27873 + if (i != c->freeable_cnt) {
27874 + ubifs_err("freeable list count %d expected %d", i,
27875 + c->freeable_cnt);
27876 + return -EINVAL;
27877 + }
27878 +
27879 + i = 0;
27880 + list_for_each(pos, &c->idx_gc)
27881 + i += 1;
27882 + if (i != c->idx_gc_cnt) {
27883 + ubifs_err("idx_gc list count %d expected %d", i,
27884 + c->idx_gc_cnt);
27885 + return -EINVAL;
27886 + }
27887 +
27888 + list_for_each_entry(lprops, &c->frdi_idx_list, list) {
27889 + if (lprops->free + lprops->dirty != c->leb_size) {
27890 + ubifs_err("non-freeable LEB %d on frdi_idx list "
27891 + "(free %d dirty %d flags %d)", lprops->lnum,
27892 + lprops->free, lprops->dirty, lprops->flags);
27893 + return -EINVAL;
27894 + }
27895 + if (lprops->flags & LPROPS_TAKEN) {
27896 + ubifs_err("taken LEB %d on frdi_idx list "
27897 + "(free %d dirty %d flags %d)", lprops->lnum,
27898 + lprops->free, lprops->dirty, lprops->flags);
27899 + return -EINVAL;
27900 + }
27901 + if (!(lprops->flags & LPROPS_INDEX)) {
27902 + ubifs_err("non-index LEB %d on frdi_idx list "
27903 + "(free %d dirty %d flags %d)", lprops->lnum,
27904 + lprops->free, lprops->dirty, lprops->flags);
27905 + return -EINVAL;
27906 + }
27907 + }
27908 +
27909 + for (cat = 1; cat <= LPROPS_HEAP_CNT; cat++) {
27910 + struct ubifs_lpt_heap *heap = &c->lpt_heap[cat - 1];
27911 +
27912 + for (i = 0; i < heap->cnt; i++) {
27913 + lprops = heap->arr[i];
27914 + if (!lprops) {
27915 + ubifs_err("null ptr in LPT heap cat %d", cat);
27916 + return -EINVAL;
27917 + }
27918 + if (lprops->hpos != i) {
27919 + ubifs_err("bad ptr in LPT heap cat %d", cat);
27920 + return -EINVAL;
27921 + }
27922 + if (lprops->flags & LPROPS_TAKEN) {
27923 + ubifs_err("taken LEB in LPT heap cat %d", cat);
27924 + return -EINVAL;
27925 + }
27926 + }
27927 + }
27928 +
27929 + return 0;
27930 +}
27931 +
27932 +void dbg_check_heap(struct ubifs_info *c, struct ubifs_lpt_heap *heap, int cat,
27933 + int add_pos)
27934 +{
27935 + int i = 0, j, err = 0;
27936 +
27937 + if (!(ubifs_chk_flags & (UBIFS_CHK_GEN | UBIFS_CHK_LPROPS)))
27938 + return;
27939 +
27940 + for (i = 0; i < heap->cnt; i++) {
27941 + struct ubifs_lprops *lprops = heap->arr[i];
27942 + struct ubifs_lprops *lp;
27943 +
27944 + if (i != add_pos)
27945 + if ((lprops->flags & LPROPS_CAT_MASK) != cat) {
27946 + err = 1;
27947 + goto out;
27948 + }
27949 + if (lprops->hpos != i) {
27950 + err = 2;
27951 + goto out;
27952 + }
27953 + lp = ubifs_lpt_lookup(c, lprops->lnum);
27954 + if (IS_ERR(lp)) {
27955 + err = 3;
27956 + goto out;
27957 + }
27958 + if (lprops != lp) {
27959 + dbg_msg("lprops %zx lp %zx lprops->lnum %d lp->lnum %d",
27960 + (size_t)lprops, (size_t)lp, lprops->lnum,
27961 + lp->lnum);
27962 + err = 4;
27963 + goto out;
27964 + }
27965 + for (j = 0; j < i; j++) {
27966 + lp = heap->arr[j];
27967 + if (lp == lprops) {
27968 + err = 5;
27969 + goto out;
27970 + }
27971 + if (lp->lnum == lprops->lnum) {
27972 + err = 6;
27973 + goto out;
27974 + }
27975 + }
27976 + }
27977 +out:
27978 + if (err) {
27979 + dbg_msg("failed cat %d hpos %d err %d", cat, i, err);
27980 + dbg_dump_stack();
27981 + dbg_dump_heap(c, heap, cat);
27982 + }
27983 +}
27984 +
27985 +/**
27986 + * struct scan_check_data - data provided to scan callback function.
27987 + * @lst: LEB properties statistics
27988 + * @err: error code
27989 + */
27990 +struct scan_check_data {
27991 + struct ubifs_lp_stats lst;
27992 + int err;
27993 +};
27994 +
27995 +/**
27996 + * scan_check_cb - scan callback.
27997 + * @c: the UBIFS file-system description object
27998 + * @lp: LEB properties to scan
27999 + * @in_tree: whether the LEB properties are in main memory
28000 + * @data: information passed to and from the caller of the scan
28001 + *
28002 + * This function returns a code that indicates whether the scan should continue
28003 + * (%LPT_SCAN_CONTINUE), whether the LEB properties should be added to the tree
28004 + * in main memory (%LPT_SCAN_ADD), or whether the scan should stop
28005 + * (%LPT_SCAN_STOP).
28006 + */
28007 +static int scan_check_cb(struct ubifs_info *c,
28008 + const struct ubifs_lprops *lp, int in_tree,
28009 + struct scan_check_data *data)
28010 +{
28011 + struct ubifs_scan_leb *sleb;
28012 + struct ubifs_scan_node *snod;
28013 + struct ubifs_lp_stats *lst = &data->lst;
28014 + int cat, lnum = lp->lnum, is_idx = 0, used = 0, free, dirty;
28015 +
28016 + cat = lp->flags & LPROPS_CAT_MASK;
28017 + if (cat != LPROPS_UNCAT) {
28018 + cat = ubifs_categorize_lprops(c, lp);
28019 + if (cat != (lp->flags & LPROPS_CAT_MASK)) {
28020 + ubifs_err("bad LEB category %d expected %d",
28021 + (lp->flags & LPROPS_CAT_MASK), cat);
28022 + goto out;
28023 + }
28024 + }
28025 +
28026 + /* Check lp is on its category list (if it has one) */
28027 + if (in_tree) {
28028 + struct list_head *list = NULL;
28029 +
28030 + switch (cat) {
28031 + case LPROPS_EMPTY:
28032 + list = &c->empty_list;
28033 + break;
28034 + case LPROPS_FREEABLE:
28035 + list = &c->freeable_list;
28036 + break;
28037 + case LPROPS_FRDI_IDX:
28038 + list = &c->frdi_idx_list;
28039 + break;
28040 + case LPROPS_UNCAT:
28041 + list = &c->uncat_list;
28042 + break;
28043 + }
28044 + if (list) {
28045 + struct ubifs_lprops *lprops;
28046 + int found = 0;
28047 +
28048 + list_for_each_entry(lprops, list, list) {
28049 + if (lprops == lp) {
28050 + found = 1;
28051 + break;
28052 + }
28053 + }
28054 + if (!found) {
28055 + ubifs_err("bad LPT list (category %d)", cat);
28056 + goto out;
28057 + }
28058 + }
28059 + }
28060 +
28061 + /* Check lp is on its category heap (if it has one) */
28062 + if (in_tree && cat > 0 && cat <= LPROPS_HEAP_CNT) {
28063 + struct ubifs_lpt_heap *heap = &c->lpt_heap[cat - 1];
28064 +
28065 + if ((lp->hpos != -1 && heap->arr[lp->hpos]->lnum != lnum) ||
28066 + lp != heap->arr[lp->hpos]) {
28067 + ubifs_err("bad LPT heap (category %d)", cat);
28068 + goto out;
28069 + }
28070 + }
28071 +
28072 + sleb = ubifs_scan(c, lnum, 0, c->dbg_buf);
28073 + if (IS_ERR(sleb)) {
28074 + /*
28075 + * After an unclean unmount, empty and freeable LEBs
28076 + * may contain garbage.
28077 + */
28078 + if (lp->free == c->leb_size) {
28079 + ubifs_err("scan errors were in empty LEB "
28080 + "- continuing checking");
28081 + lst->empty_lebs += 1;
28082 + lst->total_free += c->leb_size;
28083 + lst->total_dark += calc_dark(c, c->leb_size);
28084 + return LPT_SCAN_CONTINUE;
28085 + }
28086 +
28087 + if (lp->free + lp->dirty == c->leb_size &&
28088 + !(lp->flags & LPROPS_INDEX)) {
28089 + ubifs_err("scan errors were in freeable LEB "
28090 + "- continuing checking");
28091 + lst->total_free += lp->free;
28092 + lst->total_dirty += lp->dirty;
28093 + lst->total_dark += calc_dark(c, c->leb_size);
28094 + return LPT_SCAN_CONTINUE;
28095 + }
28096 + data->err = PTR_ERR(sleb);
28097 + return LPT_SCAN_STOP;
28098 + }
28099 +
28100 + is_idx = -1;
28101 + list_for_each_entry(snod, &sleb->nodes, list) {
28102 + int found, level = 0;
28103 +
28104 + cond_resched();
28105 +
28106 + if (is_idx == -1)
28107 + is_idx = (snod->type == UBIFS_IDX_NODE) ? 1 : 0;
28108 +
28109 + if (is_idx && snod->type != UBIFS_IDX_NODE) {
28110 + ubifs_err("indexing node in data LEB %d:%d",
28111 + lnum, snod->offs);
28112 + goto out_destroy;
28113 + }
28114 +
28115 + if (snod->type == UBIFS_IDX_NODE) {
28116 + struct ubifs_idx_node *idx = snod->node;
28117 +
28118 + key_read(c, ubifs_idx_key(c, idx), &snod->key);
28119 + level = le16_to_cpu(idx->level);
28120 + }
28121 +
28122 + found = ubifs_tnc_has_node(c, &snod->key, level, lnum,
28123 + snod->offs, is_idx);
28124 + if (found) {
28125 + if (found < 0)
28126 + goto out_destroy;
28127 + used += ALIGN(snod->len, 8);
28128 + }
28129 + }
28130 +
28131 + free = c->leb_size - sleb->endpt;
28132 + dirty = sleb->endpt - used;
28133 +
28134 + if (free > c->leb_size || free < 0 || dirty > c->leb_size ||
28135 + dirty < 0) {
28136 + ubifs_err("bad calculated accounting for LEB %d: "
28137 + "free %d, dirty %d", lnum, free, dirty);
28138 + goto out_destroy;
28139 + }
28140 +
28141 + if (lp->free + lp->dirty == c->leb_size &&
28142 + free + dirty == c->leb_size)
28143 + if ((is_idx && !(lp->flags & LPROPS_INDEX)) ||
28144 + (!is_idx && free == c->leb_size)) {
28145 + /*
28146 + * Empty or freeable LEBs could contain index
28147 + * nodes from an uncompleted commit due to an
28148 + * unclean unmount. Or they could be empty for
28149 + * the same reason.
28150 + */
28151 + free = lp->free;
28152 + dirty = lp->dirty;
28153 + is_idx = 0;
28154 + }
28155 +
28156 + if (is_idx && lp->free + lp->dirty == free + dirty &&
28157 + lnum != c->ihead_lnum) {
28158 + /*
28159 + * After an unclean unmount, an index LEB could have a different
28160 + * amount of free space than the value recorded by lprops. That
28161 + * is because the in-the-gaps method may use free space or
28162 + * create free space (as a side-effect of using ubi_leb_change
28163 + * and not writing the whole LEB). The incorrect free space
28164 + * value is not a problem because the index is only ever
28165 + * allocated empty LEBs, so there will never be an attempt to
28166 + * write to the free space at the end of an index LEB - except
28167 + * by the in-the-gaps method for which it is not a problem.
28168 + */
28169 + free = lp->free;
28170 + dirty = lp->dirty;
28171 + }
28172 +
28173 + if (lp->free != free || lp->dirty != dirty)
28174 + goto out_print;
28175 +
28176 + if (is_idx && !(lp->flags & LPROPS_INDEX)) {
28177 + if (free == c->leb_size)
28178 + /* Free but not unmapped LEB, it's fine */
28179 + is_idx = 0;
28180 + else {
28181 + ubifs_err("indexing node without indexing "
28182 + "flag");
28183 + goto out_print;
28184 + }
28185 + }
28186 +
28187 + if (!is_idx && (lp->flags & LPROPS_INDEX)) {
28188 + ubifs_err("data node with indexing flag");
28189 + goto out_print;
28190 + }
28191 +
28192 + if (free == c->leb_size)
28193 + lst->empty_lebs += 1;
28194 +
28195 + if (is_idx)
28196 + lst->idx_lebs += 1;
28197 +
28198 + if (!(lp->flags & LPROPS_INDEX))
28199 + lst->total_used += c->leb_size - free - dirty;
28200 + lst->total_free += free;
28201 + lst->total_dirty += dirty;
28202 +
28203 + if (!(lp->flags & LPROPS_INDEX)) {
28204 + int spc = free + dirty;
28205 +
28206 + if (spc < c->dead_wm)
28207 + lst->total_dead += spc;
28208 + else
28209 + lst->total_dark += calc_dark(c, spc);
28210 + }
28211 +
28212 + ubifs_scan_destroy(sleb);
28213 +
28214 + return LPT_SCAN_CONTINUE;
28215 +
28216 +out_print:
28217 + ubifs_err("bad accounting of LEB %d: free %d, dirty %d flags %#x, "
28218 + "should be free %d, dirty %d",
28219 + lnum, lp->free, lp->dirty, lp->flags, free, dirty);
28220 + dbg_dump_leb(c, lnum);
28221 +out_destroy:
28222 + ubifs_scan_destroy(sleb);
28223 +out:
28224 + data->err = -EINVAL;
28225 + return LPT_SCAN_STOP;
28226 +}
28227 +
28228 +/**
28229 + * dbg_check_lprops - check all LEB properties.
28230 + * @c: UBIFS file-system description object
28231 + *
28232 + * This function checks all LEB properties and makes sure they are all correct.
28233 + * It returns zero if everything is fine, %-EINVAL if there is an inconsistency
28234 + * and other negative error codes in case of other errors. This function is
28235 + * called while the file system is locked (because of commit start), so no
28236 + * additional locking is required. Note that locking the LPT mutex would cause
28237 + * a circular lock dependency with the TNC mutex.
28238 + */
28239 +int dbg_check_lprops(struct ubifs_info *c)
28240 +{
28241 + int i, err;
28242 + struct scan_check_data data;
28243 + struct ubifs_lp_stats *lst = &data.lst;
28244 +
28245 + if (!(ubifs_chk_flags & UBIFS_CHK_LPROPS))
28246 + return 0;
28247 +
28248 + /*
28249 + * As we are going to scan the media, the write buffers have to be
28250 + * synchronized.
28251 + */
28252 + for (i = 0; i < c->jhead_cnt; i++) {
28253 + err = ubifs_wbuf_sync(&c->jheads[i].wbuf);
28254 + if (err)
28255 + return err;
28256 + }
28257 +
28258 + memset(lst, 0, sizeof(struct ubifs_lp_stats));
28259 +
28260 + data.err = 0;
28261 + err = ubifs_lpt_scan_nolock(c, c->main_first, c->leb_cnt - 1,
28262 + (ubifs_lpt_scan_callback)scan_check_cb,
28263 + &data);
28264 + if (err && err != -ENOSPC)
28265 + goto out;
28266 + if (data.err) {
28267 + err = data.err;
28268 + goto out;
28269 + }
28270 +
28271 + if (lst->empty_lebs != c->lst.empty_lebs ||
28272 + lst->idx_lebs != c->lst.idx_lebs ||
28273 + lst->total_free != c->lst.total_free ||
28274 + lst->total_dirty != c->lst.total_dirty ||
28275 + lst->total_used != c->lst.total_used) {
28276 + ubifs_err("bad overall accounting");
28277 + ubifs_err("calculated: empty_lebs %d, idx_lebs %d, "
28278 + "total_free %lld, total_dirty %lld, total_used %lld",
28279 + lst->empty_lebs, lst->idx_lebs, lst->total_free,
28280 + lst->total_dirty, lst->total_used);
28281 + ubifs_err("read from lprops: empty_lebs %d, idx_lebs %d, "
28282 + "total_free %lld, total_dirty %lld, total_used %lld",
28283 + c->lst.empty_lebs, c->lst.idx_lebs, c->lst.total_free,
28284 + c->lst.total_dirty, c->lst.total_used);
28285 + err = -EINVAL;
28286 + goto out;
28287 + }
28288 +
28289 + if (lst->total_dead != c->lst.total_dead ||
28290 + lst->total_dark != c->lst.total_dark) {
28291 + ubifs_err("bad dead/dark space accounting");
28292 + ubifs_err("calculated: total_dead %lld, total_dark %lld",
28293 + lst->total_dead, lst->total_dark);
28294 + ubifs_err("read from lprops: total_dead %lld, total_dark %lld",
28295 + c->lst.total_dead, c->lst.total_dark);
28296 + err = -EINVAL;
28297 + goto out;
28298 + }
28299 +
28300 + err = dbg_check_cats(c);
28301 +out:
28302 + return err;
28303 +}
28304 +
28305 +#endif /* CONFIG_UBIFS_FS_DEBUG */
28306 diff --exclude=.git -urN linux-2.6.25.6/fs/ubifs/lpt.c avr32-2.6/fs/ubifs/lpt.c
28307 --- linux-2.6.25.6/fs/ubifs/lpt.c 1970-01-01 01:00:00.000000000 +0100
28308 +++ avr32-2.6/fs/ubifs/lpt.c 2008-06-12 15:09:45.475816115 +0200
28309 @@ -0,0 +1,2241 @@
28310 +/*
28311 + * This file is part of UBIFS.
28312 + *
28313 + * Copyright (C) 2006-2008 Nokia Corporation.
28314 + *
28315 + * This program is free software; you can redistribute it and/or modify it
28316 + * under the terms of the GNU General Public License version 2 as published by
28317 + * the Free Software Foundation.
28318 + *
28319 + * This program is distributed in the hope that it will be useful, but WITHOUT
28320 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
28321 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
28322 + * more details.
28323 + *
28324 + * You should have received a copy of the GNU General Public License along with
28325 + * this program; if not, write to the Free Software Foundation, Inc., 51
28326 + * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
28327 + *
28328 + * Authors: Adrian Hunter
28329 + * Artem Bityutskiy (Битюцкий Артём)
28330 + */
28331 +
28332 +/*
28333 + * This file implements the LEB properties tree (LPT) area. The LPT area
28334 + * contains the LEB properties tree, a table of LPT area eraseblocks (ltab), and
28335 + * (for the "big" model) a table of saved LEB numbers (lsave). The LPT area sits
28336 + * between the log and the orphan area.
28337 + *
28338 + * The LPT area is like a miniature self-contained file system. It is required
28339 + * that it never runs out of space, is fast to access and update, and scales
28340 + * logarithmically. The LEB properties tree is implemented as a wandering tree
28341 + * much like the TNC, and the LPT area has its own garbage collection.
28342 + *
28343 + * The LPT has two slightly different forms called the "small model" and the
28344 + * "big model". The small model is used when the entire LEB properties table
28345 + * can be written into a single eraseblock. In that case, garbage collection
28346 + * consists of just writing the whole table, which therefore makes all other
28347 + * eraseblocks reusable. In the case of the big model, dirty eraseblocks are
28348 + * selected for garbage collection, which consists are marking the nodes in
28349 + * that LEB as dirty, and then only the dirty nodes are written out. Also, in
28350 + * the case of the big model, a table of LEB numbers is saved so that the entire
28351 + * LPT does not to be scanned looking for empty eraseblocks when UBIFS is first
28352 + * mounted.
28353 + */
28354 +
28355 +#include <linux/crc16.h>
28356 +#include "ubifs.h"
28357 +
28358 +/**
28359 + * do_calc_lpt_geom - calculate sizes for the LPT area.
28360 + * @c: the UBIFS file-system description object
28361 + *
28362 + * Calculate the sizes of LPT bit fields, nodes, and tree, based on the
28363 + * properties of the flash and whether LPT is "big" (c->big_lpt).
28364 + */
28365 +static void do_calc_lpt_geom(struct ubifs_info *c)
28366 +{
28367 + int i, n, bits, per_leb_wastage, max_pnode_cnt;
28368 + long long sz, tot_wastage;
28369 +
28370 + n = c->main_lebs + c->max_leb_cnt - c->leb_cnt;
28371 + max_pnode_cnt = DIV_ROUND_UP(n, UBIFS_LPT_FANOUT);
28372 +
28373 + c->lpt_hght = 1;
28374 + n = UBIFS_LPT_FANOUT;
28375 + while (n < max_pnode_cnt) {
28376 + c->lpt_hght += 1;
28377 + n <<= UBIFS_LPT_FANOUT_SHIFT;
28378 + }
28379 +
28380 + c->pnode_cnt = DIV_ROUND_UP(c->main_lebs, UBIFS_LPT_FANOUT);
28381 +
28382 + n = DIV_ROUND_UP(c->pnode_cnt, UBIFS_LPT_FANOUT);
28383 + c->nnode_cnt = n;
28384 + for (i = 1; i < c->lpt_hght; i++) {
28385 + n = DIV_ROUND_UP(n, UBIFS_LPT_FANOUT);
28386 + c->nnode_cnt += n;
28387 + }
28388 +
28389 + c->space_bits = fls(c->leb_size) - 3;
28390 + c->lpt_lnum_bits = fls(c->lpt_lebs);
28391 + c->lpt_offs_bits = fls(c->leb_size - 1);
28392 + c->lpt_spc_bits = fls(c->leb_size);
28393 +
28394 + n = DIV_ROUND_UP(c->max_leb_cnt, UBIFS_LPT_FANOUT);
28395 + c->pcnt_bits = fls(n - 1);
28396 +
28397 + c->lnum_bits = fls(c->max_leb_cnt - 1);
28398 +
28399 + bits = UBIFS_LPT_CRC_BITS + UBIFS_LPT_TYPE_BITS +
28400 + (c->big_lpt ? c->pcnt_bits : 0) +
28401 + (c->space_bits * 2 + 1) * UBIFS_LPT_FANOUT;
28402 + c->pnode_sz = (bits + 7) / 8;
28403 +
28404 + bits = UBIFS_LPT_CRC_BITS + UBIFS_LPT_TYPE_BITS +
28405 + (c->big_lpt ? c->pcnt_bits : 0) +
28406 + (c->lpt_lnum_bits + c->lpt_offs_bits) * UBIFS_LPT_FANOUT;
28407 + c->nnode_sz = (bits + 7) / 8;
28408 +
28409 + bits = UBIFS_LPT_CRC_BITS + UBIFS_LPT_TYPE_BITS +
28410 + c->lpt_lebs * c->lpt_spc_bits * 2;
28411 + c->ltab_sz = (bits + 7) / 8;
28412 +
28413 + bits = UBIFS_LPT_CRC_BITS + UBIFS_LPT_TYPE_BITS +
28414 + c->lnum_bits * c->lsave_cnt;
28415 + c->lsave_sz = (bits + 7) / 8;
28416 +
28417 + /* Calculate the minimum LPT size */
28418 + c->lpt_sz = (long long)c->pnode_cnt * c->pnode_sz;
28419 + c->lpt_sz += (long long)c->nnode_cnt * c->nnode_sz;
28420 + c->lpt_sz += c->ltab_sz;
28421 + c->lpt_sz += c->lsave_sz;
28422 +
28423 + /* Add wastage */
28424 + sz = c->lpt_sz;
28425 + per_leb_wastage = max_t(int, c->pnode_sz, c->nnode_sz);
28426 + sz += per_leb_wastage;
28427 + tot_wastage = per_leb_wastage;
28428 + while (sz > c->leb_size) {
28429 + sz += per_leb_wastage;
28430 + sz -= c->leb_size;
28431 + tot_wastage += per_leb_wastage;
28432 + }
28433 + tot_wastage += ALIGN(sz, c->min_io_size) - sz;
28434 + c->lpt_sz += tot_wastage;
28435 +}
28436 +
28437 +/**
28438 + * ubifs_calc_lpt_geom - calculate and check sizes for the LPT area.
28439 + * @c: the UBIFS file-system description object
28440 + *
28441 + * This function returns %0 on success and a negative error code on failure.
28442 + */
28443 +int ubifs_calc_lpt_geom(struct ubifs_info *c)
28444 +{
28445 + int lebs_needed;
28446 + uint64_t sz;
28447 +
28448 + do_calc_lpt_geom(c);
28449 +
28450 + /* Verify that lpt_lebs is big enough */
28451 + sz = c->lpt_sz * 2; /* Must have at least 2 times the size */
28452 + sz += c->leb_size - 1;
28453 + do_div(sz, c->leb_size);
28454 + lebs_needed = sz;
28455 + if (lebs_needed > c->lpt_lebs) {
28456 + ubifs_err("too few LPT LEBs");
28457 + return -EINVAL;
28458 + }
28459 +
28460 + /* Verify that ltab fits in a single LEB (since ltab is a single node */
28461 + if (c->ltab_sz > c->leb_size) {
28462 + ubifs_err("LPT ltab too big");
28463 + return -EINVAL;
28464 + }
28465 +
28466 + return 0;
28467 +}
28468 +
28469 +/**
28470 + * calc_dflt_lpt_geom - calculate default LPT geometry.
28471 + * @c: the UBIFS file-system description object
28472 + * @main_lebs: number of main area LEBs is passed and returned here
28473 + * @big_lpt: whether the LPT area is "big" is returned here
28474 + *
28475 + * The size of the LPT area depends on parameters that themselves are dependent
28476 + * on the size of the LPT area. This function, successively recalculates the LPT
28477 + * area geometry until the parameters and resultant geometry are consistent.
28478 + *
28479 + * This function returns %0 on success and a negative error code on failure.
28480 + */
28481 +static int calc_dflt_lpt_geom(struct ubifs_info *c, int *main_lebs,
28482 + int *big_lpt)
28483 +{
28484 + int i, lebs_needed;
28485 + uint64_t sz;
28486 +
28487 + /* Start by assuming the minimum number of LPT LEBs */
28488 + c->lpt_lebs = UBIFS_MIN_LPT_LEBS;
28489 + c->main_lebs = *main_lebs - c->lpt_lebs;
28490 + if (c->main_lebs <= 0)
28491 + return -EINVAL;
28492 +
28493 + /* And assume we will use the small LPT model */
28494 + c->big_lpt = 0;
28495 +
28496 + /*
28497 + * Calculate the geometry based on assumptions above and then see if it
28498 + * makes sense
28499 + */
28500 + do_calc_lpt_geom(c);
28501 +
28502 + /* Small LPT model must have lpt_sz < leb_size */
28503 + if (c->lpt_sz > c->leb_size) {
28504 + /* Nope, so try again using big LPT model */
28505 + c->big_lpt = 1;
28506 + do_calc_lpt_geom(c);
28507 + }
28508 +
28509 + /* Now check there are enough LPT LEBs */
28510 + for (i = 0; i < 64 ; i++) {
28511 + sz = c->lpt_sz * 4; /* Allow 4 times the size */
28512 + sz += c->leb_size - 1;
28513 + do_div(sz, c->leb_size);
28514 + lebs_needed = sz;
28515 + if (lebs_needed > c->lpt_lebs) {
28516 + /* Not enough LPT LEBs so try again with more */
28517 + c->lpt_lebs = lebs_needed;
28518 + c->main_lebs = *main_lebs - c->lpt_lebs;
28519 + if (c->main_lebs <= 0)
28520 + return -EINVAL;
28521 + do_calc_lpt_geom(c);
28522 + continue;
28523 + }
28524 + if (c->ltab_sz > c->leb_size) {
28525 + ubifs_err("LPT ltab too big");
28526 + return -EINVAL;
28527 + }
28528 + *main_lebs = c->main_lebs;
28529 + *big_lpt = c->big_lpt;
28530 + return 0;
28531 + }
28532 + return -EINVAL;
28533 +}
28534 +
28535 +/**
28536 + * pack_bits - pack bit fields end-to-end.
28537 + * @addr: address at which to pack (passed and next address returned)
28538 + * @pos: bit position at which to pack (passed and next position returned)
28539 + * @val: value to pack
28540 + * @nrbits: number of bits of value to pack (1-32)
28541 + */
28542 +static void pack_bits(uint8_t **addr, int *pos, uint32_t val, int nrbits)
28543 +{
28544 + uint8_t *p = *addr;
28545 + int b = *pos;
28546 +
28547 + ubifs_assert(nrbits > 0);
28548 + ubifs_assert(nrbits <= 32);
28549 + ubifs_assert(*pos >= 0);
28550 + ubifs_assert(*pos < 8);
28551 + ubifs_assert((val >> nrbits) == 0 || nrbits == 32);
28552 + if (b) {
28553 + *p |= ((uint8_t)val) << b;
28554 + nrbits += b;
28555 + if (nrbits > 8) {
28556 + *++p = (uint8_t)(val >>= (8 - b));
28557 + if (nrbits > 16) {
28558 + *++p = (uint8_t)(val >>= 8);
28559 + if (nrbits > 24) {
28560 + *++p = (uint8_t)(val >>= 8);
28561 + if (nrbits > 32)
28562 + *++p = (uint8_t)(val >>= 8);
28563 + }
28564 + }
28565 + }
28566 + } else {
28567 + *p = (uint8_t)val;
28568 + if (nrbits > 8) {
28569 + *++p = (uint8_t)(val >>= 8);
28570 + if (nrbits > 16) {
28571 + *++p = (uint8_t)(val >>= 8);
28572 + if (nrbits > 24)
28573 + *++p = (uint8_t)(val >>= 8);
28574 + }
28575 + }
28576 + }
28577 + b = nrbits & 7;
28578 + if (b == 0)
28579 + p++;
28580 + *addr = p;
28581 + *pos = b;
28582 +}
28583 +
28584 +/**
28585 + * ubifs_unpack_bits - unpack bit fields.
28586 + * @addr: address at which to unpack (passed and next address returned)
28587 + * @pos: bit position at which to unpack (passed and next position returned)
28588 + * @nrbits: number of bits of value to unpack (1-32)
28589 + *
28590 + * This functions returns the value unpacked.
28591 + */
28592 +uint32_t ubifs_unpack_bits(uint8_t **addr, int *pos, int nrbits)
28593 +{
28594 + const int k = 32 - nrbits;
28595 + uint8_t *p = *addr;
28596 + int b = *pos;
28597 + uint32_t val;
28598 +
28599 + ubifs_assert(nrbits > 0);
28600 + ubifs_assert(nrbits <= 32);
28601 + ubifs_assert(*pos >= 0);
28602 + ubifs_assert(*pos < 8);
28603 + if (b) {
28604 + val = p[1] | ((uint32_t)p[2] << 8) | ((uint32_t)p[3] << 16) |
28605 + ((uint32_t)p[4] << 24);
28606 + val <<= (8 - b);
28607 + val |= *p >> b;
28608 + nrbits += b;
28609 + } else
28610 + val = p[0] | ((uint32_t)p[1] << 8) | ((uint32_t)p[2] << 16) |
28611 + ((uint32_t)p[3] << 24);
28612 + val <<= k;
28613 + val >>= k;
28614 + b = nrbits & 7;
28615 + p += nrbits / 8;
28616 + *addr = p;
28617 + *pos = b;
28618 + ubifs_assert((val >> nrbits) == 0 || nrbits - b == 32);
28619 + return val;
28620 +}
28621 +
28622 +/**
28623 + * ubifs_pack_pnode - pack all the bit fields of a pnode.
28624 + * @c: UBIFS file-system description object
28625 + * @buf: buffer into which to pack
28626 + * @pnode: pnode to pack
28627 + */
28628 +void ubifs_pack_pnode(struct ubifs_info *c, void *buf,
28629 + struct ubifs_pnode *pnode)
28630 +{
28631 + uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
28632 + int i, pos = 0;
28633 + uint16_t crc;
28634 +
28635 + pack_bits(&addr, &pos, UBIFS_LPT_PNODE, UBIFS_LPT_TYPE_BITS);
28636 + if (c->big_lpt)
28637 + pack_bits(&addr, &pos, pnode->num, c->pcnt_bits);
28638 + for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
28639 + pack_bits(&addr, &pos, pnode->lprops[i].free >> 3,
28640 + c->space_bits);
28641 + pack_bits(&addr, &pos, pnode->lprops[i].dirty >> 3,
28642 + c->space_bits);
28643 + if (pnode->lprops[i].flags & LPROPS_INDEX)
28644 + pack_bits(&addr, &pos, 1, 1);
28645 + else
28646 + pack_bits(&addr, &pos, 0, 1);
28647 + }
28648 + crc = crc16(-1, buf + UBIFS_LPT_CRC_BYTES,
28649 + c->pnode_sz - UBIFS_LPT_CRC_BYTES);
28650 + addr = buf;
28651 + pos = 0;
28652 + pack_bits(&addr, &pos, crc, UBIFS_LPT_CRC_BITS);
28653 +}
28654 +
28655 +/**
28656 + * ubifs_pack_nnode - pack all the bit fields of a nnode.
28657 + * @c: UBIFS file-system description object
28658 + * @buf: buffer into which to pack
28659 + * @nnode: nnode to pack
28660 + */
28661 +void ubifs_pack_nnode(struct ubifs_info *c, void *buf,
28662 + struct ubifs_nnode *nnode)
28663 +{
28664 + uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
28665 + int i, pos = 0;
28666 + uint16_t crc;
28667 +
28668 + pack_bits(&addr, &pos, UBIFS_LPT_NNODE, UBIFS_LPT_TYPE_BITS);
28669 + if (c->big_lpt)
28670 + pack_bits(&addr, &pos, nnode->num, c->pcnt_bits);
28671 + for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
28672 + int lnum = nnode->nbranch[i].lnum;
28673 +
28674 + if (lnum == 0)
28675 + lnum = c->lpt_last + 1;
28676 + pack_bits(&addr, &pos, lnum - c->lpt_first, c->lpt_lnum_bits);
28677 + pack_bits(&addr, &pos, nnode->nbranch[i].offs,
28678 + c->lpt_offs_bits);
28679 + }
28680 + crc = crc16(-1, buf + UBIFS_LPT_CRC_BYTES,
28681 + c->nnode_sz - UBIFS_LPT_CRC_BYTES);
28682 + addr = buf;
28683 + pos = 0;
28684 + pack_bits(&addr, &pos, crc, UBIFS_LPT_CRC_BITS);
28685 +}
28686 +
28687 +/**
28688 + * ubifs_pack_ltab - pack the LPT's own lprops table.
28689 + * @c: UBIFS file-system description object
28690 + * @buf: buffer into which to pack
28691 + * @ltab: LPT's own lprops table to pack
28692 + */
28693 +void ubifs_pack_ltab(struct ubifs_info *c, void *buf,
28694 + struct ubifs_lpt_lprops *ltab)
28695 +{
28696 + uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
28697 + int i, pos = 0;
28698 + uint16_t crc;
28699 +
28700 + pack_bits(&addr, &pos, UBIFS_LPT_LTAB, UBIFS_LPT_TYPE_BITS);
28701 + for (i = 0; i < c->lpt_lebs; i++) {
28702 + pack_bits(&addr, &pos, ltab[i].free, c->lpt_spc_bits);
28703 + pack_bits(&addr, &pos, ltab[i].dirty, c->lpt_spc_bits);
28704 + }
28705 + crc = crc16(-1, buf + UBIFS_LPT_CRC_BYTES,
28706 + c->ltab_sz - UBIFS_LPT_CRC_BYTES);
28707 + addr = buf;
28708 + pos = 0;
28709 + pack_bits(&addr, &pos, crc, UBIFS_LPT_CRC_BITS);
28710 +}
28711 +
28712 +/**
28713 + * ubifs_pack_lsave - pack the LPT's save table.
28714 + * @c: UBIFS file-system description object
28715 + * @buf: buffer into which to pack
28716 + * @lsave: LPT's save table to pack
28717 + */
28718 +void ubifs_pack_lsave(struct ubifs_info *c, void *buf, int *lsave)
28719 +{
28720 + uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
28721 + int i, pos = 0;
28722 + uint16_t crc;
28723 +
28724 + pack_bits(&addr, &pos, UBIFS_LPT_LSAVE, UBIFS_LPT_TYPE_BITS);
28725 + for (i = 0; i < c->lsave_cnt; i++)
28726 + pack_bits(&addr, &pos, lsave[i], c->lnum_bits);
28727 + crc = crc16(-1, buf + UBIFS_LPT_CRC_BYTES,
28728 + c->lsave_sz - UBIFS_LPT_CRC_BYTES);
28729 + addr = buf;
28730 + pos = 0;
28731 + pack_bits(&addr, &pos, crc, UBIFS_LPT_CRC_BITS);
28732 +}
28733 +
28734 +/**
28735 + * ubifs_add_lpt_dirt - add dirty space to LPT LEB properties.
28736 + * @c: UBIFS file-system description object
28737 + * @lnum: LEB number to which to add dirty space
28738 + * @dirty: amount of dirty space to add
28739 + */
28740 +void ubifs_add_lpt_dirt(struct ubifs_info *c, int lnum, int dirty)
28741 +{
28742 + if (!dirty || !lnum)
28743 + return;
28744 + dbg_lp("LEB %d add %d to %d",
28745 + lnum, dirty, c->ltab[lnum - c->lpt_first].dirty);
28746 + ubifs_assert(lnum >= c->lpt_first && lnum <= c->lpt_last);
28747 + c->ltab[lnum - c->lpt_first].dirty += dirty;
28748 +}
28749 +
28750 +/**
28751 + * set_ltab - set LPT LEB properties.
28752 + * @c: UBIFS file-system description object
28753 + * @lnum: LEB number
28754 + * @free: amount of free space
28755 + * @dirty: amount of dirty space
28756 + */
28757 +static void set_ltab(struct ubifs_info *c, int lnum, int free, int dirty)
28758 +{
28759 + dbg_lp("LEB %d free %d dirty %d to %d %d",
28760 + lnum, c->ltab[lnum - c->lpt_first].free,
28761 + c->ltab[lnum - c->lpt_first].dirty, free, dirty);
28762 + ubifs_assert(lnum >= c->lpt_first && lnum <= c->lpt_last);
28763 + c->ltab[lnum - c->lpt_first].free = free;
28764 + c->ltab[lnum - c->lpt_first].dirty = dirty;
28765 +}
28766 +
28767 +/**
28768 + * ubifs_add_nnode_dirt - add dirty space to LPT LEB properties.
28769 + * @c: UBIFS file-system description object
28770 + * @nnode: nnode for which to add dirt
28771 + */
28772 +void ubifs_add_nnode_dirt(struct ubifs_info *c, struct ubifs_nnode *nnode)
28773 +{
28774 + struct ubifs_nnode *np = nnode->parent;
28775 +
28776 + if (np)
28777 + ubifs_add_lpt_dirt(c, np->nbranch[nnode->iip].lnum,
28778 + c->nnode_sz);
28779 + else {
28780 + ubifs_add_lpt_dirt(c, c->lpt_lnum, c->nnode_sz);
28781 + if (!(c->lpt_drty_flgs & LTAB_DIRTY)) {
28782 + c->lpt_drty_flgs |= LTAB_DIRTY;
28783 + ubifs_add_lpt_dirt(c, c->ltab_lnum, c->ltab_sz);
28784 + }
28785 + }
28786 +}
28787 +
28788 +/**
28789 + * add_pnode_dirt - add dirty space to LPT LEB properties.
28790 + * @c: UBIFS file-system description object
28791 + * @pnode: pnode for which to add dirt
28792 + */
28793 +static void add_pnode_dirt(struct ubifs_info *c, struct ubifs_pnode *pnode)
28794 +{
28795 + ubifs_add_lpt_dirt(c, pnode->parent->nbranch[pnode->iip].lnum,
28796 + c->pnode_sz);
28797 +}
28798 +
28799 +/**
28800 + * calc_nnode_num - calculate nnode number.
28801 + * @row: the row in the tree (root is zero)
28802 + * @col: the column in the row (leftmost is zero)
28803 + *
28804 + * The nnode number is a number that uniquely identifies a nnode and can be used
28805 + * easily to traverse the tree from the root to that nnode.
28806 + *
28807 + * This function calculates and returns the nnode number for the nnode at @row
28808 + * and @col.
28809 + */
28810 +static int calc_nnode_num(int row, int col)
28811 +{
28812 + int num, bits;
28813 +
28814 + num = 1;
28815 + while (row--) {
28816 + bits = (col & (UBIFS_LPT_FANOUT - 1));
28817 + col >>= UBIFS_LPT_FANOUT_SHIFT;
28818 + num <<= UBIFS_LPT_FANOUT_SHIFT;
28819 + num |= bits;
28820 + }
28821 + return num;
28822 +}
28823 +
28824 +/**
28825 + * calc_nnode_num_from_parent - calculate nnode number.
28826 + * @c: UBIFS file-system description object
28827 + * @parent: parent nnode
28828 + * @iip: index in parent
28829 + *
28830 + * The nnode number is a number that uniquely identifies a nnode and can be used
28831 + * easily to traverse the tree from the root to that nnode.
28832 + *
28833 + * This function calculates and returns the nnode number based on the parent's
28834 + * nnode number and the index in parent.
28835 + */
28836 +static int calc_nnode_num_from_parent(struct ubifs_info *c,
28837 + struct ubifs_nnode *parent, int iip)
28838 +{
28839 + int num, shft;
28840 +
28841 + if (!parent)
28842 + return 1;
28843 + shft = (c->lpt_hght - parent->level) * UBIFS_LPT_FANOUT_SHIFT;
28844 + num = parent->num ^ (1 << shft);
28845 + num |= (UBIFS_LPT_FANOUT + iip) << shft;
28846 + return num;
28847 +}
28848 +
28849 +/**
28850 + * calc_pnode_num_from_parent - calculate pnode number.
28851 + * @c: UBIFS file-system description object
28852 + * @parent: parent nnode
28853 + * @iip: index in parent
28854 + *
28855 + * The pnode number is a number that uniquely identifies a pnode and can be used
28856 + * easily to traverse the tree from the root to that pnode.
28857 + *
28858 + * This function calculates and returns the pnode number based on the parent's
28859 + * nnode number and the index in parent.
28860 + */
28861 +static int calc_pnode_num_from_parent(struct ubifs_info *c,
28862 + struct ubifs_nnode *parent, int iip)
28863 +{
28864 + int i, n = c->lpt_hght - 1, pnum = parent->num, num = 0;
28865 +
28866 + for (i = 0; i < n; i++) {
28867 + num <<= UBIFS_LPT_FANOUT_SHIFT;
28868 + num |= pnum & (UBIFS_LPT_FANOUT - 1);
28869 + pnum >>= UBIFS_LPT_FANOUT_SHIFT;
28870 + }
28871 + num <<= UBIFS_LPT_FANOUT_SHIFT;
28872 + num |= iip;
28873 + return num;
28874 +}
28875 +
28876 +/**
28877 + * ubifs_create_dflt_lpt - create default LPT.
28878 + * @c: UBIFS file-system description object
28879 + * @main_lebs: number of main area LEBs is passed and returned here
28880 + * @lpt_first: LEB number of first LPT LEB
28881 + * @lpt_lebs: number of LEBs for LPT is passed and returned here
28882 + * @big_lpt: use big LPT model is passed and returned here
28883 + *
28884 + * This function returns %0 on success and a negative error code on failure.
28885 + */
28886 +int ubifs_create_dflt_lpt(struct ubifs_info *c, int *main_lebs, int lpt_first,
28887 + int *lpt_lebs, int *big_lpt)
28888 +{
28889 + int lnum, err = 0, node_sz, iopos, i, j, cnt, len, alen, row;
28890 + int blnum, boffs, bsz, bcnt;
28891 + struct ubifs_pnode *pnode = NULL;
28892 + struct ubifs_nnode *nnode = NULL;
28893 + void *buf = NULL, *p;
28894 + struct ubifs_lpt_lprops *ltab = NULL;
28895 + int *lsave = NULL;
28896 +
28897 + err = calc_dflt_lpt_geom(c, main_lebs, big_lpt);
28898 + if (err)
28899 + return err;
28900 + *lpt_lebs = c->lpt_lebs;
28901 +
28902 + /* Needed by 'ubifs_pack_nnode()' and 'set_ltab()' */
28903 + c->lpt_first = lpt_first;
28904 + /* Needed by 'set_ltab()' */
28905 + c->lpt_last = lpt_first + c->lpt_lebs - 1;
28906 + /* Needed by 'ubifs_pack_lsave()' */
28907 + c->main_first = c->leb_cnt - *main_lebs;
28908 +
28909 + lsave = kmalloc(sizeof(int) * c->lsave_cnt, GFP_KERNEL);
28910 + pnode = kzalloc(sizeof(struct ubifs_pnode), GFP_KERNEL);
28911 + nnode = kzalloc(sizeof(struct ubifs_nnode), GFP_KERNEL);
28912 + buf = vmalloc(c->leb_size);
28913 + ltab = vmalloc(sizeof(struct ubifs_lpt_lprops) * c->lpt_lebs);
28914 + if (!pnode || !nnode || !buf || !ltab || !lsave) {
28915 + err = -ENOMEM;
28916 + goto out;
28917 + }
28918 +
28919 + ubifs_assert(!c->ltab);
28920 + c->ltab = ltab; /* Needed by set_ltab */
28921 +
28922 + /* Initialize LPT's own lprops */
28923 + for (i = 0; i < c->lpt_lebs; i++) {
28924 + ltab[i].free = c->leb_size;
28925 + ltab[i].dirty = 0;
28926 + ltab[i].tgc = 0;
28927 + ltab[i].cmt = 0;
28928 + }
28929 +
28930 + lnum = lpt_first;
28931 + p = buf;
28932 + /* Number of leaf nodes (pnodes) */
28933 + cnt = c->pnode_cnt;
28934 +
28935 + /*
28936 + * The first pnode contains the LEB properties for the LEBs that contain
28937 + * the root inode node and the root index node of the index tree.
28938 + */
28939 + node_sz = ALIGN(ubifs_idx_node_sz(c, 1), 8);
28940 + iopos = ALIGN(node_sz, c->min_io_size);
28941 + pnode->lprops[0].free = c->leb_size - iopos;
28942 + pnode->lprops[0].dirty = iopos - node_sz;
28943 + pnode->lprops[0].flags = LPROPS_INDEX;
28944 +
28945 + node_sz = UBIFS_INO_NODE_SZ;
28946 + iopos = ALIGN(node_sz, c->min_io_size);
28947 + pnode->lprops[1].free = c->leb_size - iopos;
28948 + pnode->lprops[1].dirty = iopos - node_sz;
28949 +
28950 + for (i = 2; i < UBIFS_LPT_FANOUT; i++)
28951 + pnode->lprops[i].free = c->leb_size;
28952 +
28953 + /* Add first pnode */
28954 + ubifs_pack_pnode(c, p, pnode);
28955 + p += c->pnode_sz;
28956 + len = c->pnode_sz;
28957 + pnode->num += 1;
28958 +
28959 + /* Reset pnode values for remaining pnodes */
28960 + pnode->lprops[0].free = c->leb_size;
28961 + pnode->lprops[0].dirty = 0;
28962 + pnode->lprops[0].flags = 0;
28963 +
28964 + pnode->lprops[1].free = c->leb_size;
28965 + pnode->lprops[1].dirty = 0;
28966 +
28967 + /*
28968 + * To calculate the internal node branches, we keep information about
28969 + * the level below.
28970 + */
28971 + blnum = lnum; /* LEB number of level below */
28972 + boffs = 0; /* Offset of level below */
28973 + bcnt = cnt; /* Number of nodes in level below */
28974 + bsz = c->pnode_sz; /* Size of nodes in level below */
28975 +
28976 + /* Add all remaining pnodes */
28977 + for (i = 1; i < cnt; i++) {
28978 + if (len + c->pnode_sz > c->leb_size) {
28979 + alen = ALIGN(len, c->min_io_size);
28980 + set_ltab(c, lnum, c->leb_size - alen, alen - len);
28981 + memset(p, 0xff, alen - len);
28982 + err = ubi_leb_change(c->ubi, lnum++, buf, alen,
28983 + UBI_SHORTTERM);
28984 + if (err)
28985 + goto out;
28986 + p = buf;
28987 + len = 0;
28988 + }
28989 + ubifs_pack_pnode(c, p, pnode);
28990 + p += c->pnode_sz;
28991 + len += c->pnode_sz;
28992 + /*
28993 + * pnodes are simply numbered left to right starting at zero,
28994 + * which means the pnode number can be used easily to traverse
28995 + * down the tree to the corresponding pnode.
28996 + */
28997 + pnode->num += 1;
28998 + }
28999 +
29000 + row = 0;
29001 + for (i = UBIFS_LPT_FANOUT; cnt > i; i <<= UBIFS_LPT_FANOUT_SHIFT)
29002 + row += 1;
29003 + /* Add all nnodes, one level at a time */
29004 + while (1) {
29005 + /* Number of internal nodes (nnodes) at next level */
29006 + cnt = DIV_ROUND_UP(cnt, UBIFS_LPT_FANOUT);
29007 + for (i = 0; i < cnt; i++) {
29008 + if (len + c->nnode_sz > c->leb_size) {
29009 + alen = ALIGN(len, c->min_io_size);
29010 + set_ltab(c, lnum, c->leb_size - alen,
29011 + alen - len);
29012 + memset(p, 0xff, alen - len);
29013 + err = ubi_leb_change(c->ubi, lnum++, buf, alen,
29014 + UBI_SHORTTERM);
29015 + if (err)
29016 + goto out;
29017 + p = buf;
29018 + len = 0;
29019 + }
29020 + /* Only 1 nnode at this level, so it is the root */
29021 + if (cnt == 1) {
29022 + c->lpt_lnum = lnum;
29023 + c->lpt_offs = len;
29024 + }
29025 + /* Set branches to the level below */
29026 + for (j = 0; j < UBIFS_LPT_FANOUT; j++) {
29027 + if (bcnt) {
29028 + if (boffs + bsz > c->leb_size) {
29029 + blnum += 1;
29030 + boffs = 0;
29031 + }
29032 + nnode->nbranch[j].lnum = blnum;
29033 + nnode->nbranch[j].offs = boffs;
29034 + boffs += bsz;
29035 + bcnt--;
29036 + } else {
29037 + nnode->nbranch[j].lnum = 0;
29038 + nnode->nbranch[j].offs = 0;
29039 + }
29040 + }
29041 + nnode->num = calc_nnode_num(row, i);
29042 + ubifs_pack_nnode(c, p, nnode);
29043 + p += c->nnode_sz;
29044 + len += c->nnode_sz;
29045 + }
29046 + /* Only 1 nnode at this level, so it is the root */
29047 + if (cnt == 1)
29048 + break;
29049 + /* Update the information about the level below */
29050 + bcnt = cnt;
29051 + bsz = c->nnode_sz;
29052 + row -= 1;
29053 + }
29054 +
29055 + if (*big_lpt) {
29056 + /* Need to add LPT's save table */
29057 + if (len + c->lsave_sz > c->leb_size) {
29058 + alen = ALIGN(len, c->min_io_size);
29059 + set_ltab(c, lnum, c->leb_size - alen, alen - len);
29060 + memset(p, 0xff, alen - len);
29061 + err = ubi_leb_change(c->ubi, lnum++, buf, alen,
29062 + UBI_SHORTTERM);
29063 + if (err)
29064 + goto out;
29065 + p = buf;
29066 + len = 0;
29067 + }
29068 +
29069 + c->lsave_lnum = lnum;
29070 + c->lsave_offs = len;
29071 +
29072 + for (i = 0; i < c->lsave_cnt && i < *main_lebs; i++)
29073 + lsave[i] = c->main_first + i;
29074 + for (; i < c->lsave_cnt; i++)
29075 + lsave[i] = c->main_first;
29076 +
29077 + ubifs_pack_lsave(c, p, lsave);
29078 + p += c->lsave_sz;
29079 + len += c->lsave_sz;
29080 + }
29081 +
29082 + /* Need to add LPT's own LEB properties table */
29083 + if (len + c->ltab_sz > c->leb_size) {
29084 + alen = ALIGN(len, c->min_io_size);
29085 + set_ltab(c, lnum, c->leb_size - alen, alen - len);
29086 + memset(p, 0xff, alen - len);
29087 + err = ubi_leb_change(c->ubi, lnum++, buf, alen, UBI_SHORTTERM);
29088 + if (err)
29089 + goto out;
29090 + p = buf;
29091 + len = 0;
29092 + }
29093 +
29094 + c->ltab_lnum = lnum;
29095 + c->ltab_offs = len;
29096 +
29097 + /* Update ltab before packing it */
29098 + len += c->ltab_sz;
29099 + alen = ALIGN(len, c->min_io_size);
29100 + set_ltab(c, lnum, c->leb_size - alen, alen - len);
29101 +
29102 + ubifs_pack_ltab(c, p, ltab);
29103 + p += c->ltab_sz;
29104 +
29105 + /* Write remaining buffer */
29106 + memset(p, 0xff, alen - len);
29107 + err = ubi_leb_change(c->ubi, lnum, buf, alen, UBI_SHORTTERM);
29108 + if (err)
29109 + goto out;
29110 +
29111 + c->nhead_lnum = lnum;
29112 + c->nhead_offs = ALIGN(len, c->min_io_size);
29113 +
29114 + dbg_lp("space_bits %d", c->space_bits);
29115 + dbg_lp("lpt_lnum_bits %d", c->lpt_lnum_bits);
29116 + dbg_lp("lpt_offs_bits %d", c->lpt_offs_bits);
29117 + dbg_lp("lpt_spc_bits %d", c->lpt_spc_bits);
29118 + dbg_lp("pcnt_bits %d", c->pcnt_bits);
29119 + dbg_lp("lnum_bits %d", c->lnum_bits);
29120 + dbg_lp("pnode_sz %d", c->pnode_sz);
29121 + dbg_lp("nnode_sz %d", c->nnode_sz);
29122 + dbg_lp("ltab_sz %d", c->ltab_sz);
29123 + dbg_lp("lsave_sz %d", c->lsave_sz);
29124 + dbg_lp("lsave_cnt %d", c->lsave_cnt);
29125 + dbg_lp("lpt_hght %d", c->lpt_hght);
29126 + dbg_lp("big_lpt %d", c->big_lpt);
29127 + dbg_lp("LPT root is at %d:%d", c->lpt_lnum, c->lpt_offs);
29128 + dbg_lp("LPT head is at %d:%d", c->nhead_lnum, c->nhead_offs);
29129 + dbg_lp("LPT ltab is at %d:%d", c->ltab_lnum, c->ltab_offs);
29130 + if (c->big_lpt)
29131 + dbg_lp("LPT lsave is at %d:%d", c->lsave_lnum, c->lsave_offs);
29132 +out:
29133 + c->ltab = NULL;
29134 + kfree(lsave);
29135 + vfree(ltab);
29136 + vfree(buf);
29137 + kfree(nnode);
29138 + kfree(pnode);
29139 + return err;
29140 +}
29141 +
29142 +/**
29143 + * update_cats - add LEB properties of a pnode to LEB category lists and heaps.
29144 + * @c: UBIFS file-system description object
29145 + * @pnode: pnode
29146 + *
29147 + * When a pnode is loaded into memory, the LEB properties it contains are added,
29148 + * by this function, to the LEB category lists and heaps.
29149 + */
29150 +static void update_cats(struct ubifs_info *c, struct ubifs_pnode *pnode)
29151 +{
29152 + int i;
29153 +
29154 + for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
29155 + int cat = pnode->lprops[i].flags & LPROPS_CAT_MASK;
29156 + int lnum = pnode->lprops[i].lnum;
29157 +
29158 + if (!lnum)
29159 + return;
29160 + ubifs_add_to_cat(c, &pnode->lprops[i], cat);
29161 + }
29162 +}
29163 +
29164 +/**
29165 + * replace_cats - add LEB properties of a pnode to LEB category lists and heaps.
29166 + * @c: UBIFS file-system description object
29167 + * @old_pnode: pnode copied
29168 + * @new_pnode: pnode copy
29169 + *
29170 + * During commit it is sometimes necessary to copy a pnode
29171 + * (see dirty_cow_pnode). When that happens, references in
29172 + * category lists and heaps must be replaced. This function does that.
29173 + */
29174 +static void replace_cats(struct ubifs_info *c, struct ubifs_pnode *old_pnode,
29175 + struct ubifs_pnode *new_pnode)
29176 +{
29177 + int i;
29178 +
29179 + for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
29180 + if (!new_pnode->lprops[i].lnum)
29181 + return;
29182 + ubifs_replace_cat(c, &old_pnode->lprops[i],
29183 + &new_pnode->lprops[i]);
29184 + }
29185 +}
29186 +
29187 +/**
29188 + * check_lpt_crc - check LPT node crc is correct.
29189 + * @c: UBIFS file-system description object
29190 + * @buf: buffer containing node
29191 + * @len: length of node
29192 + *
29193 + * This function returns %0 on success and a negative error code on failure.
29194 + */
29195 +static int check_lpt_crc(void *buf, int len)
29196 +{
29197 + int pos = 0;
29198 + uint8_t *addr = buf;
29199 + uint16_t crc, calc_crc;
29200 +
29201 + crc = ubifs_unpack_bits(&addr, &pos, UBIFS_LPT_CRC_BITS);
29202 + calc_crc = crc16(-1, buf + UBIFS_LPT_CRC_BYTES,
29203 + len - UBIFS_LPT_CRC_BYTES);
29204 + if (crc != calc_crc) {
29205 + ubifs_err("invalid crc in LPT node: crc %hx calc %hx", crc,
29206 + calc_crc);
29207 + dbg_dump_stack();
29208 + return -EINVAL;
29209 + }
29210 + return 0;
29211 +}
29212 +
29213 +/**
29214 + * check_lpt_type - check LPT node type is correct.
29215 + * @c: UBIFS file-system description object
29216 + * @addr: address of type bit field is passed and returned updated here
29217 + * @pos: position of type bit field is passed and returned updated here
29218 + * @type: expected type
29219 + *
29220 + * This function returns %0 on success and a negative error code on failure.
29221 + */
29222 +static int check_lpt_type(uint8_t **addr, int *pos, int type)
29223 +{
29224 + int node_type;
29225 +
29226 + node_type = ubifs_unpack_bits(addr, pos, UBIFS_LPT_TYPE_BITS);
29227 + if (node_type != type) {
29228 + ubifs_err("invalid type (%d) in LPT node type %d", node_type,
29229 + type);
29230 + dbg_dump_stack();
29231 + return -EINVAL;
29232 + }
29233 + return 0;
29234 +}
29235 +
29236 +/**
29237 + * unpack_pnode - unpack a pnode.
29238 + * @c: UBIFS file-system description object
29239 + * @buf: buffer containing packed pnode to unpack
29240 + * @pnode: pnode structure to fill
29241 + *
29242 + * This function returns %0 on success and a negative error code on failure.
29243 + */
29244 +static int unpack_pnode(struct ubifs_info *c, void *buf,
29245 + struct ubifs_pnode *pnode)
29246 +{
29247 + uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
29248 + int i, pos = 0, err;
29249 +
29250 + err = check_lpt_type(&addr, &pos, UBIFS_LPT_PNODE);
29251 + if (err)
29252 + return err;
29253 + if (c->big_lpt)
29254 + pnode->num = ubifs_unpack_bits(&addr, &pos, c->pcnt_bits);
29255 + for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
29256 + struct ubifs_lprops * const lprops = &pnode->lprops[i];
29257 +
29258 + lprops->free = ubifs_unpack_bits(&addr, &pos, c->space_bits);
29259 + lprops->free <<= 3;
29260 + lprops->dirty = ubifs_unpack_bits(&addr, &pos, c->space_bits);
29261 + lprops->dirty <<= 3;
29262 +
29263 + if (ubifs_unpack_bits(&addr, &pos, 1))
29264 + lprops->flags = LPROPS_INDEX;
29265 + else
29266 + lprops->flags = 0;
29267 + lprops->flags |= ubifs_categorize_lprops(c, lprops);
29268 + }
29269 + err = check_lpt_crc(buf, c->pnode_sz);
29270 + return err;
29271 +}
29272 +
29273 +/**
29274 + * unpack_nnode - unpack a nnode.
29275 + * @c: UBIFS file-system description object
29276 + * @buf: buffer containing packed nnode to unpack
29277 + * @nnode: nnode structure to fill
29278 + *
29279 + * This function returns %0 on success and a negative error code on failure.
29280 + */
29281 +static int unpack_nnode(struct ubifs_info *c, void *buf,
29282 + struct ubifs_nnode *nnode)
29283 +{
29284 + uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
29285 + int i, pos = 0, err;
29286 +
29287 + err = check_lpt_type(&addr, &pos, UBIFS_LPT_NNODE);
29288 + if (err)
29289 + return err;
29290 + if (c->big_lpt)
29291 + nnode->num = ubifs_unpack_bits(&addr, &pos, c->pcnt_bits);
29292 + for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
29293 + int lnum;
29294 +
29295 + lnum = ubifs_unpack_bits(&addr, &pos, c->lpt_lnum_bits) +
29296 + c->lpt_first;
29297 + if (lnum == c->lpt_last + 1)
29298 + lnum = 0;
29299 + nnode->nbranch[i].lnum = lnum;
29300 + nnode->nbranch[i].offs = ubifs_unpack_bits(&addr, &pos,
29301 + c->lpt_offs_bits);
29302 + }
29303 + err = check_lpt_crc(buf, c->nnode_sz);
29304 + return err;
29305 +}
29306 +
29307 +/**
29308 + * unpack_ltab - unpack the LPT's own lprops table.
29309 + * @c: UBIFS file-system description object
29310 + * @buf: buffer from which to unpack
29311 + *
29312 + * This function returns %0 on success and a negative error code on failure.
29313 + */
29314 +static int unpack_ltab(struct ubifs_info *c, void *buf)
29315 +{
29316 + uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
29317 + int i, pos = 0, err;
29318 +
29319 + err = check_lpt_type(&addr, &pos, UBIFS_LPT_LTAB);
29320 + if (err)
29321 + return err;
29322 + for (i = 0; i < c->lpt_lebs; i++) {
29323 + int free = ubifs_unpack_bits(&addr, &pos, c->lpt_spc_bits);
29324 + int dirty = ubifs_unpack_bits(&addr, &pos, c->lpt_spc_bits);
29325 +
29326 + if (free < 0 || free > c->leb_size || dirty < 0 ||
29327 + dirty > c->leb_size || free + dirty > c->leb_size)
29328 + return -EINVAL;
29329 +
29330 + c->ltab[i].free = free;
29331 + c->ltab[i].dirty = dirty;
29332 + c->ltab[i].tgc = 0;
29333 + c->ltab[i].cmt = 0;
29334 + }
29335 + err = check_lpt_crc(buf, c->ltab_sz);
29336 + return err;
29337 +}
29338 +
29339 +/**
29340 + * unpack_lsave - unpack the LPT's save table.
29341 + * @c: UBIFS file-system description object
29342 + * @buf: buffer from which to unpack
29343 + *
29344 + * This function returns %0 on success and a negative error code on failure.
29345 + */
29346 +static int unpack_lsave(struct ubifs_info *c, void *buf)
29347 +{
29348 + uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
29349 + int i, pos = 0, err;
29350 +
29351 + err = check_lpt_type(&addr, &pos, UBIFS_LPT_LSAVE);
29352 + if (err)
29353 + return err;
29354 + for (i = 0; i < c->lsave_cnt; i++) {
29355 + int lnum = ubifs_unpack_bits(&addr, &pos, c->lnum_bits);
29356 +
29357 + if (lnum < c->main_first || lnum >= c->leb_cnt)
29358 + return -EINVAL;
29359 + c->lsave[i] = lnum;
29360 + }
29361 + err = check_lpt_crc(buf, c->lsave_sz);
29362 + return err;
29363 +}
29364 +
29365 +/**
29366 + * validate_nnode - validate a nnode.
29367 + * @c: UBIFS file-system description object
29368 + * @nnode: nnode to validate
29369 + * @parent: parent nnode (or NULL for the root nnode)
29370 + * @iip: index in parent
29371 + *
29372 + * This function returns %0 on success and a negative error code on failure.
29373 + */
29374 +static int validate_nnode(struct ubifs_info *c, struct ubifs_nnode *nnode,
29375 + struct ubifs_nnode *parent, int iip)
29376 +{
29377 + int i, lvl, max_offs;
29378 +
29379 + if (c->big_lpt) {
29380 + int num = calc_nnode_num_from_parent(c, parent, iip);
29381 +
29382 + if (nnode->num != num)
29383 + return -EINVAL;
29384 + }
29385 + lvl = parent ? parent->level - 1 : c->lpt_hght;
29386 + if (lvl < 1)
29387 + return -EINVAL;
29388 + if (lvl == 1)
29389 + max_offs = c->leb_size - c->pnode_sz;
29390 + else
29391 + max_offs = c->leb_size - c->nnode_sz;
29392 + for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
29393 + int lnum = nnode->nbranch[i].lnum;
29394 + int offs = nnode->nbranch[i].offs;
29395 +
29396 + if (lnum == 0) {
29397 + if (offs != 0)
29398 + return -EINVAL;
29399 + continue;
29400 + }
29401 + if (lnum < c->lpt_first || lnum > c->lpt_last)
29402 + return -EINVAL;
29403 + if (offs < 0 || offs > max_offs)
29404 + return -EINVAL;
29405 + }
29406 + return 0;
29407 +}
29408 +
29409 +/**
29410 + * validate_pnode - validate a pnode.
29411 + * @c: UBIFS file-system description object
29412 + * @pnode: pnode to validate
29413 + * @parent: parent nnode
29414 + * @iip: index in parent
29415 + *
29416 + * This function returns %0 on success and a negative error code on failure.
29417 + */
29418 +static int validate_pnode(struct ubifs_info *c, struct ubifs_pnode *pnode,
29419 + struct ubifs_nnode *parent, int iip)
29420 +{
29421 + int i;
29422 +
29423 + if (c->big_lpt) {
29424 + int num = calc_pnode_num_from_parent(c, parent, iip);
29425 +
29426 + if (pnode->num != num)
29427 + return -EINVAL;
29428 + }
29429 + for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
29430 + int free = pnode->lprops[i].free;
29431 + int dirty = pnode->lprops[i].dirty;
29432 +
29433 + if (free < 0 || free > c->leb_size || free % c->min_io_size ||
29434 + (free & 7))
29435 + return -EINVAL;
29436 + if (dirty < 0 || dirty > c->leb_size || (dirty & 7))
29437 + return -EINVAL;
29438 + if (dirty + free > c->leb_size)
29439 + return -EINVAL;
29440 + }
29441 + return 0;
29442 +}
29443 +
29444 +/**
29445 + * set_pnode_lnum - set LEB numbers on a pnode.
29446 + * @c: UBIFS file-system description object
29447 + * @pnode: pnode to update
29448 + *
29449 + * This function calculates the LEB numbers for the LEB properties it contains
29450 + * based on the pnode number.
29451 + */
29452 +static void set_pnode_lnum(struct ubifs_info *c, struct ubifs_pnode *pnode)
29453 +{
29454 + int i, lnum;
29455 +
29456 + lnum = (pnode->num << UBIFS_LPT_FANOUT_SHIFT) + c->main_first;
29457 + for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
29458 + if (lnum >= c->leb_cnt)
29459 + return;
29460 + pnode->lprops[i].lnum = lnum++;
29461 + }
29462 +}
29463 +
29464 +/**
29465 + * ubifs_read_nnode - read a nnode from flash and link it to the tree in memory.
29466 + * @c: UBIFS file-system description object
29467 + * @parent: parent nnode (or NULL for the root)
29468 + * @iip: index in parent
29469 + *
29470 + * This function returns %0 on success and a negative error code on failure.
29471 + */
29472 +int ubifs_read_nnode(struct ubifs_info *c, struct ubifs_nnode *parent, int iip)
29473 +{
29474 + struct ubifs_nbranch *branch = NULL;
29475 + struct ubifs_nnode *nnode = NULL;
29476 + void *buf = c->lpt_nod_buf;
29477 + int err, lnum, offs;
29478 +
29479 + if (parent) {
29480 + branch = &parent->nbranch[iip];
29481 + lnum = branch->lnum;
29482 + offs = branch->offs;
29483 + } else {
29484 + lnum = c->lpt_lnum;
29485 + offs = c->lpt_offs;
29486 + }
29487 + nnode = kzalloc(sizeof(struct ubifs_nnode), GFP_NOFS);
29488 + if (!nnode) {
29489 + err = -ENOMEM;
29490 + goto out;
29491 + }
29492 + if (lnum == 0) {
29493 + /*
29494 + * This nnode was not written which just means that the LEB
29495 + * properties in the subtree below it describe empty LEBs. We
29496 + * make the nnode as though we had read it, which in fact means
29497 + * doing almost nothing.
29498 + */
29499 + if (c->big_lpt)
29500 + nnode->num = calc_nnode_num_from_parent(c, parent, iip);
29501 + } else {
29502 + err = ubi_read(c->ubi, lnum, buf, offs, c->nnode_sz);
29503 + if (err)
29504 + goto out;
29505 + err = unpack_nnode(c, buf, nnode);
29506 + if (err)
29507 + goto out;
29508 + }
29509 + err = validate_nnode(c, nnode, parent, iip);
29510 + if (err)
29511 + goto out;
29512 + if (!c->big_lpt)
29513 + nnode->num = calc_nnode_num_from_parent(c, parent, iip);
29514 + if (parent) {
29515 + branch->nnode = nnode;
29516 + nnode->level = parent->level - 1;
29517 + } else {
29518 + c->nroot = nnode;
29519 + nnode->level = c->lpt_hght;
29520 + }
29521 + nnode->parent = parent;
29522 + nnode->iip = iip;
29523 + return 0;
29524 +
29525 +out:
29526 + ubifs_err("error %d reading nnode at %d:%d", err, lnum, offs);
29527 + kfree(nnode);
29528 + return err;
29529 +}
29530 +
29531 +/**
29532 + * read_pnode - read a pnode from flash and link it to the tree in memory.
29533 + * @c: UBIFS file-system description object
29534 + * @parent: parent nnode
29535 + * @iip: index in parent
29536 + *
29537 + * This function returns %0 on success and a negative error code on failure.
29538 + */
29539 +static int read_pnode(struct ubifs_info *c, struct ubifs_nnode *parent, int iip)
29540 +{
29541 + struct ubifs_nbranch *branch;
29542 + struct ubifs_pnode *pnode = NULL;
29543 + void *buf = c->lpt_nod_buf;
29544 + int err, lnum, offs;
29545 +
29546 + branch = &parent->nbranch[iip];
29547 + lnum = branch->lnum;
29548 + offs = branch->offs;
29549 + pnode = kzalloc(sizeof(struct ubifs_pnode), GFP_NOFS);
29550 + if (!pnode) {
29551 + err = -ENOMEM;
29552 + goto out;
29553 + }
29554 + if (lnum == 0) {
29555 + /*
29556 + * This pnode was not written which just means that the LEB
29557 + * properties in it describe empty LEBs. We make the pnode as
29558 + * though we had read it.
29559 + */
29560 + int i;
29561 +
29562 + if (c->big_lpt)
29563 + pnode->num = calc_pnode_num_from_parent(c, parent, iip);
29564 + for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
29565 + struct ubifs_lprops * const lprops = &pnode->lprops[i];
29566 +
29567 + lprops->free = c->leb_size;
29568 + lprops->flags = ubifs_categorize_lprops(c, lprops);
29569 + }
29570 + } else {
29571 + err = ubi_read(c->ubi, lnum, buf, offs, c->pnode_sz);
29572 + if (err)
29573 + goto out;
29574 + err = unpack_pnode(c, buf, pnode);
29575 + if (err)
29576 + goto out;
29577 + }
29578 + err = validate_pnode(c, pnode, parent, iip);
29579 + if (err)
29580 + goto out;
29581 + if (!c->big_lpt)
29582 + pnode->num = calc_pnode_num_from_parent(c, parent, iip);
29583 + branch->pnode = pnode;
29584 + pnode->parent = parent;
29585 + pnode->iip = iip;
29586 + set_pnode_lnum(c, pnode);
29587 + c->pnodes_have += 1;
29588 + return 0;
29589 +
29590 +out:
29591 + ubifs_err("error %d reading pnode at %d:%d", err, lnum, offs);
29592 + dbg_dump_pnode(c, pnode, parent, iip);
29593 + dbg_msg("calc num: %d", calc_pnode_num_from_parent(c, parent, iip));
29594 + kfree(pnode);
29595 + return err;
29596 +}
29597 +
29598 +/**
29599 + * read_ltab - read LPT's own lprops table.
29600 + * @c: UBIFS file-system description object
29601 + *
29602 + * This function returns %0 on success and a negative error code on failure.
29603 + */
29604 +static int read_ltab(struct ubifs_info *c)
29605 +{
29606 + int err;
29607 + void *buf;
29608 +
29609 + buf = vmalloc(c->ltab_sz);
29610 + if (!buf)
29611 + return -ENOMEM;
29612 + err = ubi_read(c->ubi, c->ltab_lnum, buf, c->ltab_offs, c->ltab_sz);
29613 + if (err)
29614 + goto out;
29615 + err = unpack_ltab(c, buf);
29616 +out:
29617 + vfree(buf);
29618 + return err;
29619 +}
29620 +
29621 +/**
29622 + * read_lsave - read LPT's save table.
29623 + * @c: UBIFS file-system description object
29624 + *
29625 + * This function returns %0 on success and a negative error code on failure.
29626 + */
29627 +static int read_lsave(struct ubifs_info *c)
29628 +{
29629 + int err, i;
29630 + void *buf;
29631 +
29632 + buf = vmalloc(c->lsave_sz);
29633 + if (!buf)
29634 + return -ENOMEM;
29635 + err = ubi_read(c->ubi, c->lsave_lnum, buf, c->lsave_offs, c->lsave_sz);
29636 + if (err)
29637 + goto out;
29638 + err = unpack_lsave(c, buf);
29639 + if (err)
29640 + goto out;
29641 + for (i = 0; i < c->lsave_cnt; i++) {
29642 + int lnum = c->lsave[i];
29643 +
29644 + /*
29645 + * Due to automatic resizing, the values in the lsave table
29646 + * could be beyond the volume size - just ignore them.
29647 + */
29648 + if (lnum >= c->leb_cnt)
29649 + continue;
29650 + ubifs_lpt_lookup(c, lnum);
29651 + }
29652 +out:
29653 + vfree(buf);
29654 + return err;
29655 +}
29656 +
29657 +/**
29658 + * ubifs_get_nnode - get a nnode.
29659 + * @c: UBIFS file-system description object
29660 + * @parent: parent nnode (or NULL for the root)
29661 + * @iip: index in parent
29662 + *
29663 + * This function returns a pointer to the nnode on success or a negative error
29664 + * code on failure.
29665 + */
29666 +struct ubifs_nnode *ubifs_get_nnode(struct ubifs_info *c,
29667 + struct ubifs_nnode *parent, int iip)
29668 +{
29669 + struct ubifs_nbranch *branch;
29670 + struct ubifs_nnode *nnode;
29671 + int err;
29672 +
29673 + branch = &parent->nbranch[iip];
29674 + nnode = branch->nnode;
29675 + if (nnode)
29676 + return nnode;
29677 + err = ubifs_read_nnode(c, parent, iip);
29678 + if (err)
29679 + return ERR_PTR(err);
29680 + return branch->nnode;
29681 +}
29682 +
29683 +/**
29684 + * ubifs_get_pnode - get a pnode.
29685 + * @c: UBIFS file-system description object
29686 + * @parent: parent nnode
29687 + * @iip: index in parent
29688 + *
29689 + * This function returns a pointer to the pnode on success or a negative error
29690 + * code on failure.
29691 + */
29692 +struct ubifs_pnode *ubifs_get_pnode(struct ubifs_info *c,
29693 + struct ubifs_nnode *parent, int iip)
29694 +{
29695 + struct ubifs_nbranch *branch;
29696 + struct ubifs_pnode *pnode;
29697 + int err;
29698 +
29699 + branch = &parent->nbranch[iip];
29700 + pnode = branch->pnode;
29701 + if (pnode)
29702 + return pnode;
29703 + err = read_pnode(c, parent, iip);
29704 + if (err)
29705 + return ERR_PTR(err);
29706 + update_cats(c, branch->pnode);
29707 + return branch->pnode;
29708 +}
29709 +
29710 +/**
29711 + * ubifs_lpt_lookup - lookup LEB properties in the LPT.
29712 + * @c: UBIFS file-system description object
29713 + * @lnum: LEB number to lookup
29714 + *
29715 + * This function returns a pointer to the LEB properties on success or a
29716 + * negative error code on failure.
29717 + */
29718 +struct ubifs_lprops *ubifs_lpt_lookup(struct ubifs_info *c, int lnum)
29719 +{
29720 + int err, i, h, iip, shft;
29721 + struct ubifs_nnode *nnode;
29722 + struct ubifs_pnode *pnode;
29723 +
29724 + if (!c->nroot) {
29725 + err = ubifs_read_nnode(c, NULL, 0);
29726 + if (err)
29727 + return ERR_PTR(err);
29728 + }
29729 + nnode = c->nroot;
29730 + i = lnum - c->main_first;
29731 + shft = c->lpt_hght * UBIFS_LPT_FANOUT_SHIFT;
29732 + for (h = 1; h < c->lpt_hght; h++) {
29733 + iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
29734 + shft -= UBIFS_LPT_FANOUT_SHIFT;
29735 + nnode = ubifs_get_nnode(c, nnode, iip);
29736 + if (IS_ERR(nnode))
29737 + return ERR_PTR(PTR_ERR(nnode));
29738 + }
29739 + iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
29740 + shft -= UBIFS_LPT_FANOUT_SHIFT;
29741 + pnode = ubifs_get_pnode(c, nnode, iip);
29742 + if (IS_ERR(pnode))
29743 + return ERR_PTR(PTR_ERR(pnode));
29744 + iip = (i & (UBIFS_LPT_FANOUT - 1));
29745 + dbg_lp("LEB %d, free %d, dirty %d, flags %d", lnum,
29746 + pnode->lprops[iip].free, pnode->lprops[iip].dirty,
29747 + pnode->lprops[iip].flags);
29748 + return &pnode->lprops[iip];
29749 +}
29750 +
29751 +/**
29752 + * dirty_cow_nnode - ensure a nnode is not being committed.
29753 + * @c: UBIFS file-system description object
29754 + * @nnode: nnode to check
29755 + *
29756 + * Returns dirtied nnode on success or negative error code on failure.
29757 + */
29758 +static struct ubifs_nnode *dirty_cow_nnode(struct ubifs_info *c,
29759 + struct ubifs_nnode *nnode)
29760 +{
29761 + struct ubifs_nnode *n;
29762 + int i;
29763 +
29764 + if (!test_bit(COW_CNODE, &nnode->flags)) {
29765 + /* nnode is not being committed */
29766 + if (!test_and_set_bit(DIRTY_CNODE, &nnode->flags)) {
29767 + c->dirty_nn_cnt += 1;
29768 + ubifs_add_nnode_dirt(c, nnode);
29769 + }
29770 + return nnode;
29771 + }
29772 +
29773 + /* nnode is being committed, so copy it */
29774 + n = kmalloc(sizeof(struct ubifs_nnode), GFP_NOFS);
29775 + if (unlikely(!n))
29776 + return ERR_PTR(-ENOMEM);
29777 +
29778 + memcpy(n, nnode, sizeof(struct ubifs_nnode));
29779 + n->cnext = NULL;
29780 + __set_bit(DIRTY_CNODE, &n->flags);
29781 + __clear_bit(COW_CNODE, &n->flags);
29782 +
29783 + /* The children now have new parent */
29784 + for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
29785 + struct ubifs_nbranch *branch = &n->nbranch[i];
29786 +
29787 + if (branch->cnode)
29788 + branch->cnode->parent = n;
29789 + }
29790 +
29791 + ubifs_assert(!test_bit(OBSOLETE_CNODE, &nnode->flags));
29792 + __set_bit(OBSOLETE_CNODE, &nnode->flags);
29793 +
29794 + c->dirty_nn_cnt += 1;
29795 + ubifs_add_nnode_dirt(c, nnode);
29796 + if (nnode->parent)
29797 + nnode->parent->nbranch[n->iip].nnode = n;
29798 + else
29799 + c->nroot = n;
29800 + return n;
29801 +}
29802 +
29803 +/**
29804 + * dirty_cow_pnode - ensure a pnode is not being committed.
29805 + * @c: UBIFS file-system description object
29806 + * @pnode: pnode to check
29807 + *
29808 + * Returns dirtied pnode on success or negative error code on failure.
29809 + */
29810 +static struct ubifs_pnode *dirty_cow_pnode(struct ubifs_info *c,
29811 + struct ubifs_pnode *pnode)
29812 +{
29813 + struct ubifs_pnode *p;
29814 +
29815 + if (!test_bit(COW_CNODE, &pnode->flags)) {
29816 + /* pnode is not being committed */
29817 + if (!test_and_set_bit(DIRTY_CNODE, &pnode->flags)) {
29818 + c->dirty_pn_cnt += 1;
29819 + add_pnode_dirt(c, pnode);
29820 + }
29821 + return pnode;
29822 + }
29823 +
29824 + /* pnode is being committed, so copy it */
29825 + p = kmalloc(sizeof(struct ubifs_pnode), GFP_NOFS);
29826 + if (unlikely(!p))
29827 + return ERR_PTR(-ENOMEM);
29828 +
29829 + memcpy(p, pnode, sizeof(struct ubifs_pnode));
29830 + p->cnext = NULL;
29831 + __set_bit(DIRTY_CNODE, &p->flags);
29832 + __clear_bit(COW_CNODE, &p->flags);
29833 + replace_cats(c, pnode, p);
29834 +
29835 + ubifs_assert(!test_bit(OBSOLETE_CNODE, &pnode->flags));
29836 + __set_bit(OBSOLETE_CNODE, &pnode->flags);
29837 +
29838 + c->dirty_pn_cnt += 1;
29839 + add_pnode_dirt(c, pnode);
29840 + pnode->parent->nbranch[p->iip].pnode = p;
29841 + return p;
29842 +}
29843 +
29844 +/**
29845 + * ubifs_lpt_lookup_dirty - lookup LEB properties in the LPT.
29846 + * @c: UBIFS file-system description object
29847 + * @lnum: LEB number to lookup
29848 + *
29849 + * This function returns a pointer to the LEB properties on success or a
29850 + * negative error code on failure.
29851 + */
29852 +struct ubifs_lprops *ubifs_lpt_lookup_dirty(struct ubifs_info *c, int lnum)
29853 +{
29854 + int err, i, h, iip, shft;
29855 + struct ubifs_nnode *nnode;
29856 + struct ubifs_pnode *pnode;
29857 +
29858 + if (!c->nroot) {
29859 + err = ubifs_read_nnode(c, NULL, 0);
29860 + if (err)
29861 + return ERR_PTR(err);
29862 + }
29863 + nnode = c->nroot;
29864 + nnode = dirty_cow_nnode(c, nnode);
29865 + if (IS_ERR(nnode))
29866 + return ERR_PTR(PTR_ERR(nnode));
29867 + i = lnum - c->main_first;
29868 + shft = c->lpt_hght * UBIFS_LPT_FANOUT_SHIFT;
29869 + for (h = 1; h < c->lpt_hght; h++) {
29870 + iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
29871 + shft -= UBIFS_LPT_FANOUT_SHIFT;
29872 + nnode = ubifs_get_nnode(c, nnode, iip);
29873 + if (IS_ERR(nnode))
29874 + return ERR_PTR(PTR_ERR(nnode));
29875 + nnode = dirty_cow_nnode(c, nnode);
29876 + if (IS_ERR(nnode))
29877 + return ERR_PTR(PTR_ERR(nnode));
29878 + }
29879 + iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
29880 + shft -= UBIFS_LPT_FANOUT_SHIFT;
29881 + pnode = ubifs_get_pnode(c, nnode, iip);
29882 + if (IS_ERR(pnode))
29883 + return ERR_PTR(PTR_ERR(pnode));
29884 + pnode = dirty_cow_pnode(c, pnode);
29885 + if (IS_ERR(pnode))
29886 + return ERR_PTR(PTR_ERR(pnode));
29887 + iip = (i & (UBIFS_LPT_FANOUT - 1));
29888 + dbg_lp("LEB %d, free %d, dirty %d, flags %d", lnum,
29889 + pnode->lprops[iip].free, pnode->lprops[iip].dirty,
29890 + pnode->lprops[iip].flags);
29891 + ubifs_assert(test_bit(DIRTY_CNODE, &pnode->flags));
29892 + return &pnode->lprops[iip];
29893 +}
29894 +
29895 +/**
29896 + * lpt_init_rd - initialize the LPT for reading.
29897 + * @c: UBIFS file-system description object
29898 + *
29899 + * This function returns %0 on success and a negative error code on failure.
29900 + */
29901 +static int lpt_init_rd(struct ubifs_info *c)
29902 +{
29903 + int err, i;
29904 +
29905 + c->ltab = vmalloc(sizeof(struct ubifs_lpt_lprops) * c->lpt_lebs);
29906 + if (!c->ltab)
29907 + return -ENOMEM;
29908 +
29909 + i = max_t(int, c->nnode_sz, c->pnode_sz);
29910 + c->lpt_nod_buf = kmalloc(i, GFP_KERNEL);
29911 + if (!c->lpt_nod_buf)
29912 + return -ENOMEM;
29913 +
29914 + for (i = 0; i < LPROPS_HEAP_CNT; i++) {
29915 + c->lpt_heap[i].arr = kmalloc(sizeof(void *) * LPT_HEAP_SZ,
29916 + GFP_KERNEL);
29917 + if (!c->lpt_heap[i].arr)
29918 + return -ENOMEM;
29919 + c->lpt_heap[i].cnt = 0;
29920 + c->lpt_heap[i].max_cnt = LPT_HEAP_SZ;
29921 + }
29922 +
29923 + c->dirty_idx.arr = kmalloc(sizeof(void *) * LPT_HEAP_SZ, GFP_KERNEL);
29924 + if (!c->dirty_idx.arr)
29925 + return -ENOMEM;
29926 + c->dirty_idx.cnt = 0;
29927 + c->dirty_idx.max_cnt = LPT_HEAP_SZ;
29928 +
29929 + err = read_ltab(c);
29930 + if (err)
29931 + return err;
29932 +
29933 + dbg_lp("space_bits %d", c->space_bits);
29934 + dbg_lp("lpt_lnum_bits %d", c->lpt_lnum_bits);
29935 + dbg_lp("lpt_offs_bits %d", c->lpt_offs_bits);
29936 + dbg_lp("lpt_spc_bits %d", c->lpt_spc_bits);
29937 + dbg_lp("pcnt_bits %d", c->pcnt_bits);
29938 + dbg_lp("lnum_bits %d", c->lnum_bits);
29939 + dbg_lp("pnode_sz %d", c->pnode_sz);
29940 + dbg_lp("nnode_sz %d", c->nnode_sz);
29941 + dbg_lp("ltab_sz %d", c->ltab_sz);
29942 + dbg_lp("lsave_sz %d", c->lsave_sz);
29943 + dbg_lp("lsave_cnt %d", c->lsave_cnt);
29944 + dbg_lp("lpt_hght %d", c->lpt_hght);
29945 + dbg_lp("big_lpt %d", c->big_lpt);
29946 + dbg_lp("LPT root is at %d:%d", c->lpt_lnum, c->lpt_offs);
29947 + dbg_lp("LPT head is at %d:%d", c->nhead_lnum, c->nhead_offs);
29948 + dbg_lp("LPT ltab is at %d:%d", c->ltab_lnum, c->ltab_offs);
29949 + if (c->big_lpt)
29950 + dbg_lp("LPT lsave is at %d:%d", c->lsave_lnum, c->lsave_offs);
29951 +
29952 + return 0;
29953 +}
29954 +
29955 +/**
29956 + * lpt_init_wr - initialize the LPT for writing.
29957 + * @c: UBIFS file-system description object
29958 + *
29959 + * 'lpt_init_rd()' must have been called already.
29960 + *
29961 + * This function returns %0 on success and a negative error code on failure.
29962 + */
29963 +static int lpt_init_wr(struct ubifs_info *c)
29964 +{
29965 + int err, i;
29966 +
29967 + c->ltab_cmt = vmalloc(sizeof(struct ubifs_lpt_lprops) * c->lpt_lebs);
29968 + if (!c->ltab_cmt)
29969 + return -ENOMEM;
29970 +
29971 + c->lpt_buf = vmalloc(c->leb_size);
29972 + if (!c->lpt_buf)
29973 + return -ENOMEM;
29974 +
29975 + if (c->big_lpt) {
29976 + c->lsave = kmalloc(sizeof(int) * c->lsave_cnt, GFP_NOFS);
29977 + if (!c->lsave)
29978 + return -ENOMEM;
29979 + err = read_lsave(c);
29980 + if (err)
29981 + return err;
29982 + }
29983 +
29984 + for (i = 0; i < c->lpt_lebs; i++)
29985 + if (c->ltab[i].free == c->leb_size) {
29986 + err = ubifs_leb_unmap(c, i + c->lpt_first);
29987 + if (err)
29988 + return err;
29989 + }
29990 +
29991 + return 0;
29992 +}
29993 +
29994 +/**
29995 + * ubifs_lpt_init - initialize the LPT.
29996 + * @c: UBIFS file-system description object
29997 + * @rd: whether to initialize lpt for reading
29998 + * @wr: whether to initialize lpt for writing
29999 + *
30000 + * For mounting 'rw', @rd and @wr are both true. For mounting 'ro', @rd is true
30001 + * and @wr is false. For mounting from 'ro' to 'rw', @rd is false and @wr is
30002 + * true.
30003 + *
30004 + * This function returns %0 on success and a negative error code on failure.
30005 + */
30006 +int ubifs_lpt_init(struct ubifs_info *c, int rd, int wr)
30007 +{
30008 + int err;
30009 +
30010 + if (rd) {
30011 + err = lpt_init_rd(c);
30012 + if (err)
30013 + return err;
30014 + }
30015 +
30016 + if (wr) {
30017 + err = lpt_init_wr(c);
30018 + if (err)
30019 + return err;
30020 + }
30021 +
30022 + return 0;
30023 +}
30024 +
30025 +/**
30026 + * struct lpt_scan_node - somewhere to put nodes while we scan LPT.
30027 + * @nnode: where to keep a nnode
30028 + * @pnode: where to keep a pnode
30029 + * @cnode: where to keep a cnode
30030 + * @in_tree: is the node in the tree in memory
30031 + * @ptr.nnode: pointer to the nnode (if it is an nnode) which may be here or in
30032 + * the tree
30033 + * @ptr.pnode: ditto for pnode
30034 + * @ptr.cnode: ditto for cnode
30035 + */
30036 +struct lpt_scan_node {
30037 + union {
30038 + struct ubifs_nnode nnode;
30039 + struct ubifs_pnode pnode;
30040 + struct ubifs_cnode cnode;
30041 + };
30042 + int in_tree;
30043 + union {
30044 + struct ubifs_nnode *nnode;
30045 + struct ubifs_pnode *pnode;
30046 + struct ubifs_cnode *cnode;
30047 + } ptr;
30048 +};
30049 +
30050 +/**
30051 + * scan_get_nnode - for the scan, get a nnode from either the tree or flash.
30052 + * @c: the UBIFS file-system description object
30053 + * @path: where to put the nnode
30054 + * @parent: parent of the nnode
30055 + * @iip: index in parent of the nnode
30056 + *
30057 + * This function returns a pointer to the nnode on success or a negative error
30058 + * code on failure.
30059 + */
30060 +static struct ubifs_nnode *scan_get_nnode(struct ubifs_info *c,
30061 + struct lpt_scan_node *path,
30062 + struct ubifs_nnode *parent, int iip)
30063 +{
30064 + struct ubifs_nbranch *branch;
30065 + struct ubifs_nnode *nnode;
30066 + void *buf = c->lpt_nod_buf;
30067 + int err;
30068 +
30069 + branch = &parent->nbranch[iip];
30070 + nnode = branch->nnode;
30071 + if (nnode) {
30072 + path->in_tree = 1;
30073 + path->ptr.nnode = nnode;
30074 + return nnode;
30075 + }
30076 + nnode = &path->nnode;
30077 + path->in_tree = 0;
30078 + path->ptr.nnode = nnode;
30079 + memset(nnode, 0, sizeof(struct ubifs_nnode));
30080 + if (branch->lnum == 0) {
30081 + /*
30082 + * This nnode was not written which just means that the LEB
30083 + * properties in the subtree below it describe empty LEBs. We
30084 + * make the nnode as though we had read it, which in fact means
30085 + * doing almost nothing.
30086 + */
30087 + if (c->big_lpt)
30088 + nnode->num = calc_nnode_num_from_parent(c, parent, iip);
30089 + } else {
30090 + err = ubi_read(c->ubi, branch->lnum, buf, branch->offs,
30091 + c->nnode_sz);
30092 + if (err)
30093 + return ERR_PTR(err);
30094 + err = unpack_nnode(c, buf, nnode);
30095 + if (err)
30096 + return ERR_PTR(err);
30097 + }
30098 + err = validate_nnode(c, nnode, parent, iip);
30099 + if (err)
30100 + return ERR_PTR(err);
30101 + if (!c->big_lpt)
30102 + nnode->num = calc_nnode_num_from_parent(c, parent, iip);
30103 + nnode->level = parent->level - 1;
30104 + nnode->parent = parent;
30105 + nnode->iip = iip;
30106 + return nnode;
30107 +}
30108 +
30109 +/**
30110 + * scan_get_pnode - for the scan, get a pnode from either the tree or flash.
30111 + * @c: the UBIFS file-system description object
30112 + * @path: where to put the pnode
30113 + * @parent: parent of the pnode
30114 + * @iip: index in parent of the pnode
30115 + *
30116 + * This function returns a pointer to the pnode on success or a negative error
30117 + * code on failure.
30118 + */
30119 +static struct ubifs_pnode *scan_get_pnode(struct ubifs_info *c,
30120 + struct lpt_scan_node *path,
30121 + struct ubifs_nnode *parent, int iip)
30122 +{
30123 + struct ubifs_nbranch *branch;
30124 + struct ubifs_pnode *pnode;
30125 + void *buf = c->lpt_nod_buf;
30126 + int err;
30127 +
30128 + branch = &parent->nbranch[iip];
30129 + pnode = branch->pnode;
30130 + if (pnode) {
30131 + path->in_tree = 1;
30132 + path->ptr.pnode = pnode;
30133 + return pnode;
30134 + }
30135 + pnode = &path->pnode;
30136 + path->in_tree = 0;
30137 + path->ptr.pnode = pnode;
30138 + memset(pnode, 0, sizeof(struct ubifs_pnode));
30139 + if (branch->lnum == 0) {
30140 + /*
30141 + * This pnode was not written which just means that the LEB
30142 + * properties in it describe empty LEBs. We make the pnode as
30143 + * though we had read it.
30144 + */
30145 + int i;
30146 +
30147 + if (c->big_lpt)
30148 + pnode->num = calc_pnode_num_from_parent(c, parent, iip);
30149 + for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
30150 + struct ubifs_lprops * const lprops = &pnode->lprops[i];
30151 +
30152 + lprops->free = c->leb_size;
30153 + lprops->flags = ubifs_categorize_lprops(c, lprops);
30154 + }
30155 + } else {
30156 + ubifs_assert(branch->lnum >= c->lpt_first &&
30157 + branch->lnum <= c->lpt_last);
30158 + ubifs_assert(branch->offs >= 0 && branch->offs < c->leb_size);
30159 + err = ubi_read(c->ubi, branch->lnum, buf, branch->offs,
30160 + c->pnode_sz);
30161 + if (err)
30162 + return ERR_PTR(err);
30163 + err = unpack_pnode(c, buf, pnode);
30164 + if (err)
30165 + return ERR_PTR(err);
30166 + }
30167 + err = validate_pnode(c, pnode, parent, iip);
30168 + if (err)
30169 + return ERR_PTR(err);
30170 + if (!c->big_lpt)
30171 + pnode->num = calc_pnode_num_from_parent(c, parent, iip);
30172 + pnode->parent = parent;
30173 + pnode->iip = iip;
30174 + set_pnode_lnum(c, pnode);
30175 + return pnode;
30176 +}
30177 +
30178 +/**
30179 + * ubifs_lpt_scan_nolock - scan the LPT.
30180 + * @c: the UBIFS file-system description object
30181 + * @start_lnum: LEB number from which to start scanning
30182 + * @end_lnum: LEB number at which to stop scanning
30183 + * @scan_cb: callback function called for each lprops
30184 + * @data: data to be passed to the callback function
30185 + *
30186 + * This function returns %0 on success and a negative error code on failure.
30187 + */
30188 +int ubifs_lpt_scan_nolock(struct ubifs_info *c, int start_lnum, int end_lnum,
30189 + ubifs_lpt_scan_callback scan_cb, void *data)
30190 +{
30191 + int err = 0, i, h, iip, shft;
30192 + struct ubifs_nnode *nnode;
30193 + struct ubifs_pnode *pnode;
30194 + struct lpt_scan_node *path;
30195 +
30196 + if (start_lnum == -1) {
30197 + start_lnum = end_lnum + 1;
30198 + if (start_lnum >= c->leb_cnt)
30199 + start_lnum = c->main_first;
30200 + }
30201 +
30202 + ubifs_assert(start_lnum >= c->main_first && start_lnum < c->leb_cnt);
30203 + ubifs_assert(end_lnum >= c->main_first && end_lnum < c->leb_cnt);
30204 +
30205 + if (!c->nroot) {
30206 + err = ubifs_read_nnode(c, NULL, 0);
30207 + if (err)
30208 + return err;
30209 + }
30210 +
30211 + path = kmalloc(sizeof(struct lpt_scan_node) * (c->lpt_hght + 1),
30212 + GFP_NOFS);
30213 + if (!path)
30214 + return -ENOMEM;
30215 +
30216 + path[0].ptr.nnode = c->nroot;
30217 + path[0].in_tree = 1;
30218 +again:
30219 + /* Descend to the pnode containing start_lnum */
30220 + nnode = c->nroot;
30221 + i = start_lnum - c->main_first;
30222 + shft = c->lpt_hght * UBIFS_LPT_FANOUT_SHIFT;
30223 + for (h = 1; h < c->lpt_hght; h++) {
30224 + iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
30225 + shft -= UBIFS_LPT_FANOUT_SHIFT;
30226 + nnode = scan_get_nnode(c, path + h, nnode, iip);
30227 + if (IS_ERR(nnode)) {
30228 + err = PTR_ERR(nnode);
30229 + goto out;
30230 + }
30231 + }
30232 + iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
30233 + shft -= UBIFS_LPT_FANOUT_SHIFT;
30234 + pnode = scan_get_pnode(c, path + h, nnode, iip);
30235 + if (IS_ERR(pnode)) {
30236 + err = PTR_ERR(pnode);
30237 + goto out;
30238 + }
30239 + iip = (i & (UBIFS_LPT_FANOUT - 1));
30240 +
30241 + /* Loop for each lprops */
30242 + while (1) {
30243 + struct ubifs_lprops *lprops = &pnode->lprops[iip];
30244 + int ret, lnum = lprops->lnum;
30245 +
30246 + ret = scan_cb(c, lprops, path[h].in_tree, data);
30247 + if (ret < 0) {
30248 + err = ret;
30249 + goto out;
30250 + }
30251 + if (ret & LPT_SCAN_ADD) {
30252 + /* Add all the nodes in path to the tree in memory */
30253 + for (h = 1; h < c->lpt_hght; h++) {
30254 + const size_t sz = sizeof(struct ubifs_nnode);
30255 + struct ubifs_nnode *parent;
30256 +
30257 + if (path[h].in_tree)
30258 + continue;
30259 + nnode = kmalloc(sz, GFP_NOFS);
30260 + if (!nnode) {
30261 + err = -ENOMEM;
30262 + goto out;
30263 + }
30264 + memcpy(nnode, &path[h].nnode, sz);
30265 + parent = nnode->parent;
30266 + parent->nbranch[nnode->iip].nnode = nnode;
30267 + path[h].ptr.nnode = nnode;
30268 + path[h].in_tree = 1;
30269 + path[h + 1].cnode.parent = nnode;
30270 + }
30271 + if (path[h].in_tree)
30272 + ubifs_ensure_cat(c, lprops);
30273 + else {
30274 + const size_t sz = sizeof(struct ubifs_pnode);
30275 + struct ubifs_nnode *parent;
30276 +
30277 + pnode = kmalloc(sz, GFP_NOFS);
30278 + if (!pnode) {
30279 + err = -ENOMEM;
30280 + goto out;
30281 + }
30282 + memcpy(pnode, &path[h].pnode, sz);
30283 + parent = pnode->parent;
30284 + parent->nbranch[pnode->iip].pnode = pnode;
30285 + path[h].ptr.pnode = pnode;
30286 + path[h].in_tree = 1;
30287 + update_cats(c, pnode);
30288 + c->pnodes_have += 1;
30289 + }
30290 + err = dbg_check_lpt_nodes(c, (struct ubifs_cnode *)
30291 + c->nroot, 0, 0);
30292 + if (err)
30293 + goto out;
30294 + err = dbg_check_cats(c);
30295 + if (err)
30296 + goto out;
30297 + }
30298 + if (ret & LPT_SCAN_STOP) {
30299 + err = 0;
30300 + break;
30301 + }
30302 + /* Get the next lprops */
30303 + if (lnum == end_lnum) {
30304 + /*
30305 + * We got to the end without finding what we were
30306 + * looking for
30307 + */
30308 + err = -ENOSPC;
30309 + goto out;
30310 + }
30311 + if (lnum + 1 >= c->leb_cnt) {
30312 + /* Wrap-around to the beginning */
30313 + start_lnum = c->main_first;
30314 + goto again;
30315 + }
30316 + if (iip + 1 < UBIFS_LPT_FANOUT) {
30317 + /* Next lprops is in the same pnode */
30318 + iip += 1;
30319 + continue;
30320 + }
30321 + /* We need to get the next pnode. Go up until we can go right */
30322 + iip = pnode->iip;
30323 + while (1) {
30324 + h -= 1;
30325 + ubifs_assert(h >= 0);
30326 + nnode = path[h].ptr.nnode;
30327 + if (iip + 1 < UBIFS_LPT_FANOUT)
30328 + break;
30329 + iip = nnode->iip;
30330 + }
30331 + /* Go right */
30332 + iip += 1;
30333 + /* Descend to the pnode */
30334 + h += 1;
30335 + for (; h < c->lpt_hght; h++) {
30336 + nnode = scan_get_nnode(c, path + h, nnode, iip);
30337 + if (IS_ERR(nnode)) {
30338 + err = PTR_ERR(nnode);
30339 + goto out;
30340 + }
30341 + iip = 0;
30342 + }
30343 + pnode = scan_get_pnode(c, path + h, nnode, iip);
30344 + if (IS_ERR(pnode)) {
30345 + err = PTR_ERR(pnode);
30346 + goto out;
30347 + }
30348 + iip = 0;
30349 + }
30350 +out:
30351 + kfree(path);
30352 + return err;
30353 +}
30354 +
30355 +#ifdef CONFIG_UBIFS_FS_DEBUG
30356 +
30357 +/**
30358 + * dbg_chk_pnode - check a pnode.
30359 + * @c: the UBIFS file-system description object
30360 + * @pnode: pnode to check
30361 + * @col: pnode column
30362 + *
30363 + * This function returns %0 on success and a negative error code on failure.
30364 + */
30365 +static int dbg_chk_pnode(struct ubifs_info *c, struct ubifs_pnode *pnode,
30366 + int col)
30367 +{
30368 + int i;
30369 +
30370 + if (pnode->num != col) {
30371 + dbg_err("pnode num %d expected %d parent num %d iip %d",
30372 + pnode->num, col, pnode->parent->num, pnode->iip);
30373 + return -EINVAL;
30374 + }
30375 + for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
30376 + struct ubifs_lprops *lp, *lprops = &pnode->lprops[i];
30377 + int lnum = (pnode->num << UBIFS_LPT_FANOUT_SHIFT) + i +
30378 + c->main_first;
30379 + int found, cat = lprops->flags & LPROPS_CAT_MASK;
30380 + struct ubifs_lpt_heap *heap;
30381 + struct list_head *list = NULL;
30382 +
30383 + if (lnum >= c->leb_cnt)
30384 + continue;
30385 + if (lprops->lnum != lnum) {
30386 + dbg_err("bad LEB number %d expected %d",
30387 + lprops->lnum, lnum);
30388 + return -EINVAL;
30389 + }
30390 + if (lprops->flags & LPROPS_TAKEN) {
30391 + if (cat != LPROPS_UNCAT) {
30392 + dbg_err("LEB %d taken but not uncat %d",
30393 + lprops->lnum, cat);
30394 + return -EINVAL;
30395 + }
30396 + continue;
30397 + }
30398 + if (lprops->flags & LPROPS_INDEX) {
30399 + switch (cat) {
30400 + case LPROPS_UNCAT:
30401 + case LPROPS_DIRTY_IDX:
30402 + case LPROPS_FRDI_IDX:
30403 + break;
30404 + default:
30405 + dbg_err("LEB %d index but cat %d",
30406 + lprops->lnum, cat);
30407 + return -EINVAL;
30408 + }
30409 + } else {
30410 + switch (cat) {
30411 + case LPROPS_UNCAT:
30412 + case LPROPS_DIRTY:
30413 + case LPROPS_FREE:
30414 + case LPROPS_EMPTY:
30415 + case LPROPS_FREEABLE:
30416 + break;
30417 + default:
30418 + dbg_err("LEB %d not index but cat %d",
30419 + lprops->lnum, cat);
30420 + return -EINVAL;
30421 + }
30422 + }
30423 + switch (cat) {
30424 + case LPROPS_UNCAT:
30425 + list = &c->uncat_list;
30426 + break;
30427 + case LPROPS_EMPTY:
30428 + list = &c->empty_list;
30429 + break;
30430 + case LPROPS_FREEABLE:
30431 + list = &c->freeable_list;
30432 + break;
30433 + case LPROPS_FRDI_IDX:
30434 + list = &c->frdi_idx_list;
30435 + break;
30436 + }
30437 + found = 0;
30438 + switch (cat) {
30439 + case LPROPS_DIRTY:
30440 + case LPROPS_DIRTY_IDX:
30441 + case LPROPS_FREE:
30442 + heap = &c->lpt_heap[cat - 1];
30443 + if (lprops->hpos < heap->cnt &&
30444 + heap->arr[lprops->hpos] == lprops)
30445 + found = 1;
30446 + break;
30447 + case LPROPS_UNCAT:
30448 + case LPROPS_EMPTY:
30449 + case LPROPS_FREEABLE:
30450 + case LPROPS_FRDI_IDX:
30451 + list_for_each_entry(lp, list, list)
30452 + if (lprops == lp) {
30453 + found = 1;
30454 + break;
30455 + }
30456 + break;
30457 + }
30458 + if (!found) {
30459 + dbg_err("LEB %d cat %d not found in cat heap/list",
30460 + lprops->lnum, cat);
30461 + return -EINVAL;
30462 + }
30463 + switch (cat) {
30464 + case LPROPS_EMPTY:
30465 + if (lprops->free != c->leb_size) {
30466 + dbg_err("LEB %d cat %d free %d dirty %d",
30467 + lprops->lnum, cat, lprops->free,
30468 + lprops->dirty);
30469 + return -EINVAL;
30470 + }
30471 + case LPROPS_FREEABLE:
30472 + case LPROPS_FRDI_IDX:
30473 + if (lprops->free + lprops->dirty != c->leb_size) {
30474 + dbg_err("LEB %d cat %d free %d dirty %d",
30475 + lprops->lnum, cat, lprops->free,
30476 + lprops->dirty);
30477 + return -EINVAL;
30478 + }
30479 + }
30480 + }
30481 + return 0;
30482 +}
30483 +
30484 +/**
30485 + * dbg_check_lpt_nodes - check nnodes and pnodes.
30486 + * @c: the UBIFS file-system description object
30487 + * @cnode: next cnode (nnode or pnode) to check
30488 + * @row: row of cnode (root is zero)
30489 + * @col: column of cnode (leftmost is zero)
30490 + *
30491 + * This function returns %0 on success and a negative error code on failure.
30492 + */
30493 +int dbg_check_lpt_nodes(struct ubifs_info *c, struct ubifs_cnode *cnode,
30494 + int row, int col)
30495 +{
30496 + struct ubifs_nnode *nnode, *nn;
30497 + struct ubifs_cnode *cn;
30498 + int num, iip = 0, err;
30499 +
30500 + if (!(ubifs_chk_flags & UBIFS_CHK_LPROPS))
30501 + return 0;
30502 +
30503 + while (cnode) {
30504 + ubifs_assert(row >= 0);
30505 + nnode = cnode->parent;
30506 + if (cnode->level) {
30507 + /* cnode is a nnode */
30508 + num = calc_nnode_num(row, col);
30509 + if (cnode->num != num) {
30510 + dbg_err("nnode num %d expected %d "
30511 + "parent num %d iip %d", cnode->num, num,
30512 + (nnode ? nnode->num : 0), cnode->iip);
30513 + return -EINVAL;
30514 + }
30515 + nn = (struct ubifs_nnode *)cnode;
30516 + while (iip < UBIFS_LPT_FANOUT) {
30517 + cn = nn->nbranch[iip].cnode;
30518 + if (cn) {
30519 + /* Go down */
30520 + row += 1;
30521 + col <<= UBIFS_LPT_FANOUT_SHIFT;
30522 + col += iip;
30523 + iip = 0;
30524 + cnode = cn;
30525 + break;
30526 + }
30527 + /* Go right */
30528 + iip += 1;
30529 + }
30530 + if (iip < UBIFS_LPT_FANOUT)
30531 + continue;
30532 + } else {
30533 + struct ubifs_pnode *pnode;
30534 +
30535 + /* cnode is a pnode */
30536 + pnode = (struct ubifs_pnode *)cnode;
30537 + err = dbg_chk_pnode(c, pnode, col);
30538 + if (err)
30539 + return err;
30540 + }
30541 + /* Go up and to the right */
30542 + row -= 1;
30543 + col >>= UBIFS_LPT_FANOUT_SHIFT;
30544 + iip = cnode->iip + 1;
30545 + cnode = (struct ubifs_cnode *)nnode;
30546 + }
30547 + return 0;
30548 +}
30549 +
30550 +#endif /* CONFIG_UBIFS_FS_DEBUG */
30551 diff --exclude=.git -urN linux-2.6.25.6/fs/ubifs/lpt_commit.c avr32-2.6/fs/ubifs/lpt_commit.c
30552 --- linux-2.6.25.6/fs/ubifs/lpt_commit.c 1970-01-01 01:00:00.000000000 +0100
30553 +++ avr32-2.6/fs/ubifs/lpt_commit.c 2008-06-12 15:09:45.475816115 +0200
30554 @@ -0,0 +1,1631 @@
30555 +/*
30556 + * This file is part of UBIFS.
30557 + *
30558 + * Copyright (C) 2006-2008 Nokia Corporation.
30559 + *
30560 + * This program is free software; you can redistribute it and/or modify it
30561 + * under the terms of the GNU General Public License version 2 as published by
30562 + * the Free Software Foundation.
30563 + *
30564 + * This program is distributed in the hope that it will be useful, but WITHOUT
30565 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
30566 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
30567 + * more details.
30568 + *
30569 + * You should have received a copy of the GNU General Public License along with
30570 + * this program; if not, write to the Free Software Foundation, Inc., 51
30571 + * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
30572 + *
30573 + * Authors: Adrian Hunter
30574 + * Artem Bityutskiy (Битюцкий Артём)
30575 + */
30576 +
30577 +/*
30578 + * This file implements commit-related functionality of the LEB properties
30579 + * subsystem.
30580 + */
30581 +
30582 +#include <linux/crc16.h>
30583 +#include "ubifs.h"
30584 +
30585 +/**
30586 + * first_dirty_cnode - find first dirty cnode.
30587 + * @c: UBIFS file-system description object
30588 + * @nnode: nnode at which to start
30589 + *
30590 + * This function returns the first dirty cnode or %NULL if there is not one.
30591 + */
30592 +static struct ubifs_cnode *first_dirty_cnode(struct ubifs_nnode *nnode)
30593 +{
30594 + ubifs_assert(nnode);
30595 + while (1) {
30596 + int i, cont = 0;
30597 +
30598 + for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
30599 + struct ubifs_cnode *cnode;
30600 +
30601 + cnode = nnode->nbranch[i].cnode;
30602 + if (cnode &&
30603 + test_bit(DIRTY_CNODE, &cnode->flags)) {
30604 + if (cnode->level == 0)
30605 + return cnode;
30606 + nnode = (struct ubifs_nnode *)cnode;
30607 + cont = 1;
30608 + break;
30609 + }
30610 + }
30611 + if (!cont)
30612 + return (struct ubifs_cnode *)nnode;
30613 + }
30614 +}
30615 +
30616 +/**
30617 + * next_dirty_cnode - find next dirty cnode.
30618 + * @cnode: cnode from which to begin searching
30619 + *
30620 + * This function returns the next dirty cnode or %NULL if there is not one.
30621 + */
30622 +static struct ubifs_cnode *next_dirty_cnode(struct ubifs_cnode *cnode)
30623 +{
30624 + struct ubifs_nnode *nnode;
30625 + int i;
30626 +
30627 + ubifs_assert(cnode);
30628 + nnode = cnode->parent;
30629 + if (!nnode)
30630 + return NULL;
30631 + for (i = cnode->iip + 1; i < UBIFS_LPT_FANOUT; i++) {
30632 + cnode = nnode->nbranch[i].cnode;
30633 + if (cnode && test_bit(DIRTY_CNODE, &cnode->flags)) {
30634 + if (cnode->level == 0)
30635 + return cnode; /* cnode is a pnode */
30636 + /* cnode is a nnode */
30637 + return first_dirty_cnode((struct ubifs_nnode *)cnode);
30638 + }
30639 + }
30640 + return (struct ubifs_cnode *)nnode;
30641 +}
30642 +
30643 +/**
30644 + * get_cnodes_to_commit - create list of dirty cnodes to commit.
30645 + * @c: UBIFS file-system description object
30646 + *
30647 + * This function returns the number of cnodes to commit.
30648 + */
30649 +static int get_cnodes_to_commit(struct ubifs_info *c)
30650 +{
30651 + struct ubifs_cnode *cnode, *cnext;
30652 + int cnt = 0;
30653 +
30654 + if (!c->nroot)
30655 + return 0;
30656 +
30657 + if (!test_bit(DIRTY_CNODE, &c->nroot->flags))
30658 + return 0;
30659 +
30660 + c->lpt_cnext = first_dirty_cnode(c->nroot);
30661 + cnode = c->lpt_cnext;
30662 + if (!cnode)
30663 + return 0;
30664 + cnt += 1;
30665 + while (1) {
30666 + ubifs_assert(!test_bit(COW_ZNODE, &cnode->flags));
30667 + __set_bit(COW_ZNODE, &cnode->flags);
30668 + cnext = next_dirty_cnode(cnode);
30669 + if (!cnext) {
30670 + cnode->cnext = c->lpt_cnext;
30671 + break;
30672 + }
30673 + cnode->cnext = cnext;
30674 + cnode = cnext;
30675 + cnt += 1;
30676 + }
30677 + dbg_cmt("committing %d cnodes", cnt);
30678 + dbg_lp("committing %d cnodes", cnt);
30679 + ubifs_assert(cnt == c->dirty_nn_cnt + c->dirty_pn_cnt);
30680 + return cnt;
30681 +}
30682 +
30683 +/**
30684 + * upd_ltab - update LPT LEB properties.
30685 + * @c: UBIFS file-system description object
30686 + * @lnum: LEB number
30687 + * @free: amount of free space
30688 + * @dirty: amount of dirty space to add
30689 + */
30690 +static void upd_ltab(struct ubifs_info *c, int lnum, int free, int dirty)
30691 +{
30692 + dbg_lp("LEB %d free %d dirty %d to %d +%d",
30693 + lnum, c->ltab[lnum - c->lpt_first].free,
30694 + c->ltab[lnum - c->lpt_first].dirty, free, dirty);
30695 + ubifs_assert(lnum >= c->lpt_first && lnum <= c->lpt_last);
30696 + c->ltab[lnum - c->lpt_first].free = free;
30697 + c->ltab[lnum - c->lpt_first].dirty += dirty;
30698 +}
30699 +
30700 +/**
30701 + * alloc_lpt_leb - allocate an LPT LEB that is empty.
30702 + * @c: UBIFS file-system description object
30703 + * @lnum: LEB number is passed and returned here
30704 + *
30705 + * This function finds the next empty LEB in the ltab starting from @lnum. If a
30706 + * an empty LEB is found it is returned in @lnum and the function returns %0.
30707 + * Otherwise the function returns -ENOSPC. Note however, that LPT is designed
30708 + * never to run out of space.
30709 + */
30710 +static int alloc_lpt_leb(struct ubifs_info *c, int *lnum)
30711 +{
30712 + int i, n;
30713 +
30714 + n = *lnum - c->lpt_first + 1;
30715 + for (i = n; i < c->lpt_lebs; i++) {
30716 + if (c->ltab[i].tgc || c->ltab[i].cmt)
30717 + continue;
30718 + if (c->ltab[i].free == c->leb_size) {
30719 + c->ltab[i].cmt = 1;
30720 + *lnum = i + c->lpt_first;
30721 + return 0;
30722 + }
30723 + }
30724 +
30725 + for (i = 0; i < n; i++) {
30726 + if (c->ltab[i].tgc || c->ltab[i].cmt)
30727 + continue;
30728 + if (c->ltab[i].free == c->leb_size) {
30729 + c->ltab[i].cmt = 1;
30730 + *lnum = i + c->lpt_first;
30731 + return 0;
30732 + }
30733 + }
30734 + dbg_err("last LEB %d", *lnum);
30735 + dump_stack();
30736 + return -ENOSPC;
30737 +}
30738 +
30739 +/**
30740 + * layout_cnodes - layout cnodes for commit.
30741 + * @c: UBIFS file-system description object
30742 + *
30743 + * This function returns %0 on success and a negative error code on failure.
30744 + */
30745 +static int layout_cnodes(struct ubifs_info *c)
30746 +{
30747 + int lnum, offs, len, alen, done_lsave, done_ltab, err;
30748 + struct ubifs_cnode *cnode;
30749 +
30750 + cnode = c->lpt_cnext;
30751 + if (!cnode)
30752 + return 0;
30753 + lnum = c->nhead_lnum;
30754 + offs = c->nhead_offs;
30755 + /* Try to place lsave and ltab nicely */
30756 + done_lsave = !c->big_lpt;
30757 + done_ltab = 0;
30758 + if (!done_lsave && offs + c->lsave_sz <= c->leb_size) {
30759 + done_lsave = 1;
30760 + c->lsave_lnum = lnum;
30761 + c->lsave_offs = offs;
30762 + offs += c->lsave_sz;
30763 + }
30764 +
30765 + if (offs + c->ltab_sz <= c->leb_size) {
30766 + done_ltab = 1;
30767 + c->ltab_lnum = lnum;
30768 + c->ltab_offs = offs;
30769 + offs += c->ltab_sz;
30770 + }
30771 +
30772 + do {
30773 + if (cnode->level) {
30774 + len = c->nnode_sz;
30775 + c->dirty_nn_cnt -= 1;
30776 + } else {
30777 + len = c->pnode_sz;
30778 + c->dirty_pn_cnt -= 1;
30779 + }
30780 + while (offs + len > c->leb_size) {
30781 + alen = ALIGN(offs, c->min_io_size);
30782 + upd_ltab(c, lnum, c->leb_size - alen, alen - offs);
30783 + err = alloc_lpt_leb(c, &lnum);
30784 + if (err)
30785 + return err;
30786 + offs = 0;
30787 + ubifs_assert(lnum >= c->lpt_first &&
30788 + lnum <= c->lpt_last);
30789 + /* Try to place lsave and ltab nicely */
30790 + if (!done_lsave) {
30791 + done_lsave = 1;
30792 + c->lsave_lnum = lnum;
30793 + c->lsave_offs = offs;
30794 + offs += c->lsave_sz;
30795 + continue;
30796 + }
30797 + if (!done_ltab) {
30798 + done_ltab = 1;
30799 + c->ltab_lnum = lnum;
30800 + c->ltab_offs = offs;
30801 + offs += c->ltab_sz;
30802 + continue;
30803 + }
30804 + break;
30805 + }
30806 + if (cnode->parent) {
30807 + cnode->parent->nbranch[cnode->iip].lnum = lnum;
30808 + cnode->parent->nbranch[cnode->iip].offs = offs;
30809 + } else {
30810 + c->lpt_lnum = lnum;
30811 + c->lpt_offs = offs;
30812 + }
30813 + offs += len;
30814 + cnode = cnode->cnext;
30815 + } while (cnode && cnode != c->lpt_cnext);
30816 +
30817 + /* Make sure to place LPT's save table */
30818 + if (!done_lsave) {
30819 + if (offs + c->lsave_sz > c->leb_size) {
30820 + alen = ALIGN(offs, c->min_io_size);
30821 + upd_ltab(c, lnum, c->leb_size - alen, alen - offs);
30822 + err = alloc_lpt_leb(c, &lnum);
30823 + if (err)
30824 + return err;
30825 + offs = 0;
30826 + ubifs_assert(lnum >= c->lpt_first &&
30827 + lnum <= c->lpt_last);
30828 + }
30829 + done_lsave = 1;
30830 + c->lsave_lnum = lnum;
30831 + c->lsave_offs = offs;
30832 + offs += c->lsave_sz;
30833 + }
30834 +
30835 + /* Make sure to place LPT's own lprops table */
30836 + if (!done_ltab) {
30837 + if (offs + c->ltab_sz > c->leb_size) {
30838 + alen = ALIGN(offs, c->min_io_size);
30839 + upd_ltab(c, lnum, c->leb_size - alen, alen - offs);
30840 + err = alloc_lpt_leb(c, &lnum);
30841 + if (err)
30842 + return err;
30843 + offs = 0;
30844 + ubifs_assert(lnum >= c->lpt_first &&
30845 + lnum <= c->lpt_last);
30846 + }
30847 + done_ltab = 1;
30848 + c->ltab_lnum = lnum;
30849 + c->ltab_offs = offs;
30850 + offs += c->ltab_sz;
30851 + }
30852 +
30853 + alen = ALIGN(offs, c->min_io_size);
30854 + upd_ltab(c, lnum, c->leb_size - alen, alen - offs);
30855 + return 0;
30856 +}
30857 +
30858 +/**
30859 + * realloc_lpt_leb - allocate an LPT LEB that is empty.
30860 + * @c: UBIFS file-system description object
30861 + * @lnum: LEB number is passed and returned here
30862 + *
30863 + * This function duplicates exactly the results of the function alloc_lpt_leb.
30864 + * It is used during end commit to reallocate the same LEB numbers that were
30865 + * allocated by alloc_lpt_leb during start commit.
30866 + *
30867 + * This function finds the next LEB that was allocated by the alloc_lpt_leb
30868 + * function starting from @lnum. If a LEB is found it is returned in @lnum and
30869 + * the function returns %0. Otherwise the function returns -ENOSPC.
30870 + * Note however, that LPT is designed never to run out of space.
30871 + */
30872 +static int realloc_lpt_leb(struct ubifs_info *c, int *lnum)
30873 +{
30874 + int i, n;
30875 +
30876 + n = *lnum - c->lpt_first + 1;
30877 + for (i = n; i < c->lpt_lebs; i++)
30878 + if (c->ltab[i].cmt) {
30879 + c->ltab[i].cmt = 0;
30880 + *lnum = i + c->lpt_first;
30881 + return 0;
30882 + }
30883 +
30884 + for (i = 0; i < n; i++)
30885 + if (c->ltab[i].cmt) {
30886 + c->ltab[i].cmt = 0;
30887 + *lnum = i + c->lpt_first;
30888 + return 0;
30889 + }
30890 + dbg_err("last LEB %d", *lnum);
30891 + dump_stack();
30892 + return -ENOSPC;
30893 +}
30894 +
30895 +/**
30896 + * write_cnodes - write cnodes for commit.
30897 + * @c: UBIFS file-system description object
30898 + *
30899 + * This function returns %0 on success and a negative error code on failure.
30900 + */
30901 +static int write_cnodes(struct ubifs_info *c)
30902 +{
30903 + int lnum, offs, len, from, err, wlen, alen, done_ltab, done_lsave;
30904 + struct ubifs_cnode *cnode;
30905 + void *buf = c->lpt_buf;
30906 +
30907 + cnode = c->lpt_cnext;
30908 + if (!cnode)
30909 + return 0;
30910 + lnum = c->nhead_lnum;
30911 + offs = c->nhead_offs;
30912 + from = offs;
30913 + /* Ensure empty LEB is unmapped */
30914 + if (offs == 0) {
30915 + err = ubifs_leb_unmap(c, lnum);
30916 + if (err)
30917 + return err;
30918 + }
30919 + /* Try to place lsave and ltab nicely */
30920 + done_lsave = !c->big_lpt;
30921 + done_ltab = 0;
30922 + if (!done_lsave && offs + c->lsave_sz <= c->leb_size) {
30923 + done_lsave = 1;
30924 + ubifs_pack_lsave(c, buf + offs, c->lsave);
30925 + offs += c->lsave_sz;
30926 + }
30927 +
30928 + if (offs + c->ltab_sz <= c->leb_size) {
30929 + done_ltab = 1;
30930 + ubifs_pack_ltab(c, buf + offs, c->ltab_cmt);
30931 + offs += c->ltab_sz;
30932 + }
30933 +
30934 + /* Loop for each cnode */
30935 + do {
30936 + if (cnode->level)
30937 + len = c->nnode_sz;
30938 + else
30939 + len = c->pnode_sz;
30940 + while (offs + len > c->leb_size) {
30941 + wlen = offs - from;
30942 + if (wlen) {
30943 + alen = ALIGN(wlen, c->min_io_size);
30944 + memset(buf + offs, 0xff, alen - wlen);
30945 + err = ubifs_leb_write(c, lnum, buf + from, from,
30946 + alen, UBI_SHORTTERM);
30947 + if (err)
30948 + return err;
30949 + }
30950 + err = realloc_lpt_leb(c, &lnum);
30951 + if (err)
30952 + return err;
30953 + offs = 0;
30954 + from = 0;
30955 + ubifs_assert(lnum >= c->lpt_first &&
30956 + lnum <= c->lpt_last);
30957 + err = ubifs_leb_unmap(c, lnum);
30958 + if (err)
30959 + return err;
30960 + /* Try to place lsave and ltab nicely */
30961 + if (!done_lsave) {
30962 + done_lsave = 1;
30963 + ubifs_pack_lsave(c, buf + offs, c->lsave);
30964 + offs += c->lsave_sz;
30965 + continue;
30966 + }
30967 + if (!done_ltab) {
30968 + done_ltab = 1;
30969 + ubifs_pack_ltab(c, buf + offs, c->ltab_cmt);
30970 + offs += c->ltab_sz;
30971 + continue;
30972 + }
30973 + break;
30974 + }
30975 + if (cnode->level)
30976 + ubifs_pack_nnode(c, buf + offs,
30977 + (struct ubifs_nnode *)cnode);
30978 + else
30979 + ubifs_pack_pnode(c, buf + offs,
30980 + (struct ubifs_pnode *)cnode);
30981 + /*
30982 + * The reason for the barriers is the same as in case of TNC.
30983 + * See comment in 'write_index()'. 'dirty_cow_nnode()' and
30984 + * 'dirty_cow_pnode()' are the functions for which this is
30985 + * important.
30986 + */
30987 + clear_bit(DIRTY_CNODE, &cnode->flags);
30988 + smp_mb__before_clear_bit();
30989 + clear_bit(COW_ZNODE, &cnode->flags);
30990 + smp_mb__after_clear_bit();
30991 + offs += len;
30992 + cnode = cnode->cnext;
30993 + } while (cnode && cnode != c->lpt_cnext);
30994 +
30995 + /* Make sure to place LPT's save table */
30996 + if (!done_lsave) {
30997 + if (offs + c->lsave_sz > c->leb_size) {
30998 + wlen = offs - from;
30999 + alen = ALIGN(wlen, c->min_io_size);
31000 + memset(buf + offs, 0xff, alen - wlen);
31001 + err = ubifs_leb_write(c, lnum, buf + from, from, alen,
31002 + UBI_SHORTTERM);
31003 + if (err)
31004 + return err;
31005 + err = realloc_lpt_leb(c, &lnum);
31006 + if (err)
31007 + return err;
31008 + offs = 0;
31009 + ubifs_assert(lnum >= c->lpt_first &&
31010 + lnum <= c->lpt_last);
31011 + err = ubifs_leb_unmap(c, lnum);
31012 + if (err)
31013 + return err;
31014 + }
31015 + done_lsave = 1;
31016 + ubifs_pack_lsave(c, buf + offs, c->lsave);
31017 + offs += c->lsave_sz;
31018 + }
31019 +
31020 + /* Make sure to place LPT's own lprops table */
31021 + if (!done_ltab) {
31022 + if (offs + c->ltab_sz > c->leb_size) {
31023 + wlen = offs - from;
31024 + alen = ALIGN(wlen, c->min_io_size);
31025 + memset(buf + offs, 0xff, alen - wlen);
31026 + err = ubifs_leb_write(c, lnum, buf + from, from, alen,
31027 + UBI_SHORTTERM);
31028 + if (err)
31029 + return err;
31030 + err = realloc_lpt_leb(c, &lnum);
31031 + if (err)
31032 + return err;
31033 + offs = 0;
31034 + ubifs_assert(lnum >= c->lpt_first &&
31035 + lnum <= c->lpt_last);
31036 + err = ubifs_leb_unmap(c, lnum);
31037 + if (err)
31038 + return err;
31039 + }
31040 + done_ltab = 1;
31041 + ubifs_pack_ltab(c, buf + offs, c->ltab_cmt);
31042 + offs += c->ltab_sz;
31043 + }
31044 +
31045 + /* Write remaining data in buffer */
31046 + wlen = offs - from;
31047 + alen = ALIGN(wlen, c->min_io_size);
31048 + memset(buf + offs, 0xff, alen - wlen);
31049 + err = ubifs_leb_write(c, lnum, buf + from, from, alen, UBI_SHORTTERM);
31050 + if (err)
31051 + return err;
31052 + c->nhead_lnum = lnum;
31053 + c->nhead_offs = ALIGN(offs, c->min_io_size);
31054 +
31055 + dbg_lp("LPT root is at %d:%d", c->lpt_lnum, c->lpt_offs);
31056 + dbg_lp("LPT head is at %d:%d", c->nhead_lnum, c->nhead_offs);
31057 + dbg_lp("LPT ltab is at %d:%d", c->ltab_lnum, c->ltab_offs);
31058 + if (c->big_lpt)
31059 + dbg_lp("LPT lsave is at %d:%d", c->lsave_lnum, c->lsave_offs);
31060 + return 0;
31061 +}
31062 +
31063 +/**
31064 + * next_pnode - find next pnode.
31065 + * @c: UBIFS file-system description object
31066 + * @pnode: pnode
31067 + *
31068 + * This function returns the next pnode or %NULL if there are no more pnodes.
31069 + */
31070 +static struct ubifs_pnode *next_pnode(struct ubifs_info *c,
31071 + struct ubifs_pnode *pnode)
31072 +{
31073 + struct ubifs_nnode *nnode;
31074 + int iip;
31075 +
31076 + /* Try to go right */
31077 + nnode = pnode->parent;
31078 + iip = pnode->iip + 1;
31079 + if (iip < UBIFS_LPT_FANOUT) {
31080 + /* We assume here that LEB zero is never an LPT LEB */
31081 + if (nnode->nbranch[iip].lnum)
31082 + return ubifs_get_pnode(c, nnode, iip);
31083 + else
31084 + return NULL;
31085 + }
31086 +
31087 + /* Go up while can't go right */
31088 + do {
31089 + iip = nnode->iip + 1;
31090 + nnode = nnode->parent;
31091 + if (!nnode)
31092 + return NULL;
31093 + /* We assume here that LEB zero is never an LPT LEB */
31094 + } while (iip >= UBIFS_LPT_FANOUT || !nnode->nbranch[iip].lnum);
31095 +
31096 + /* Go right */
31097 + nnode = ubifs_get_nnode(c, nnode, iip);
31098 + if (IS_ERR(nnode))
31099 + return (void *)nnode;
31100 +
31101 + /* Go down to level 1 */
31102 + while (nnode->level > 1) {
31103 + nnode = ubifs_get_nnode(c, nnode, 0);
31104 + if (IS_ERR(nnode))
31105 + return (void *)nnode;
31106 + }
31107 +
31108 + return ubifs_get_pnode(c, nnode, 0);
31109 +}
31110 +
31111 +/**
31112 + * pnode_lookup - lookup a pnode in the LPT.
31113 + * @c: UBIFS file-system description object
31114 + * @i: pnode number (0 to main_lebs - 1)
31115 + *
31116 + * This function returns a pointer to the pnode on success or a negative
31117 + * error code on failure.
31118 + */
31119 +static struct ubifs_pnode *pnode_lookup(struct ubifs_info *c, int i)
31120 +{
31121 + int err, h, iip, shft;
31122 + struct ubifs_nnode *nnode;
31123 +
31124 + if (!c->nroot) {
31125 + err = ubifs_read_nnode(c, NULL, 0);
31126 + if (err)
31127 + return ERR_PTR(err);
31128 + }
31129 + i <<= UBIFS_LPT_FANOUT_SHIFT;
31130 + nnode = c->nroot;
31131 + shft = c->lpt_hght * UBIFS_LPT_FANOUT_SHIFT;
31132 + for (h = 1; h < c->lpt_hght; h++) {
31133 + iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
31134 + shft -= UBIFS_LPT_FANOUT_SHIFT;
31135 + nnode = ubifs_get_nnode(c, nnode, iip);
31136 + if (IS_ERR(nnode))
31137 + return ERR_PTR(PTR_ERR(nnode));
31138 + }
31139 + iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
31140 + return ubifs_get_pnode(c, nnode, iip);
31141 +}
31142 +
31143 +/**
31144 + * add_pnode_dirt - add dirty space to LPT LEB properties.
31145 + * @c: UBIFS file-system description object
31146 + * @pnode: pnode for which to add dirt
31147 + */
31148 +static void add_pnode_dirt(struct ubifs_info *c, struct ubifs_pnode *pnode)
31149 +{
31150 + ubifs_add_lpt_dirt(c, pnode->parent->nbranch[pnode->iip].lnum,
31151 + c->pnode_sz);
31152 +}
31153 +
31154 +/**
31155 + * do_make_pnode_dirty - mark a pnode dirty.
31156 + * @c: UBIFS file-system description object
31157 + * @pnode: pnode to mark dirty
31158 + */
31159 +static void do_make_pnode_dirty(struct ubifs_info *c, struct ubifs_pnode *pnode)
31160 +{
31161 + /* Assumes cnext list is empty i.e. not called during commit */
31162 + if (!test_and_set_bit(DIRTY_CNODE, &pnode->flags)) {
31163 + struct ubifs_nnode *nnode;
31164 +
31165 + c->dirty_pn_cnt += 1;
31166 + add_pnode_dirt(c, pnode);
31167 + /* Mark parent and ancestors dirty too */
31168 + nnode = pnode->parent;
31169 + while (nnode) {
31170 + if (!test_and_set_bit(DIRTY_CNODE, &nnode->flags)) {
31171 + c->dirty_nn_cnt += 1;
31172 + ubifs_add_nnode_dirt(c, nnode);
31173 + nnode = nnode->parent;
31174 + } else
31175 + break;
31176 + }
31177 + }
31178 +}
31179 +
31180 +/**
31181 + * make_tree_dirty - mark the entire LEB properties tree dirty.
31182 + * @c: UBIFS file-system description object
31183 + *
31184 + * This function is used by the "small" LPT model to cause the entire LEB
31185 + * properties tree to be written. The "small" LPT model does not use LPT
31186 + * garbage collection because it is more efficient to write the entire tree
31187 + * (because it is small).
31188 + *
31189 + * This function returns %0 on success and a negative error code on failure.
31190 + */
31191 +static int make_tree_dirty(struct ubifs_info *c)
31192 +{
31193 + struct ubifs_pnode *pnode;
31194 +
31195 + pnode = pnode_lookup(c, 0);
31196 + while (pnode) {
31197 + do_make_pnode_dirty(c, pnode);
31198 + pnode = next_pnode(c, pnode);
31199 + if (IS_ERR(pnode))
31200 + return PTR_ERR(pnode);
31201 + }
31202 + return 0;
31203 +}
31204 +
31205 +/**
31206 + * need_write_all - determine if the LPT area is running out of free space.
31207 + * @c: UBIFS file-system description object
31208 + *
31209 + * This function returns %1 if the LPT area is running out of free space and %0
31210 + * if it is not.
31211 + */
31212 +static int need_write_all(struct ubifs_info *c)
31213 +{
31214 + long long free = 0;
31215 + int i;
31216 +
31217 + for (i = 0; i < c->lpt_lebs; i++) {
31218 + if (i + c->lpt_first == c->nhead_lnum)
31219 + free += c->leb_size - c->nhead_offs;
31220 + else if (c->ltab[i].free == c->leb_size)
31221 + free += c->leb_size;
31222 + else if (c->ltab[i].free + c->ltab[i].dirty == c->leb_size)
31223 + free += c->leb_size;
31224 + }
31225 + /* Less than twice the size left */
31226 + if (free <= c->lpt_sz * 2)
31227 + return 1;
31228 + return 0;
31229 +}
31230 +
31231 +/**
31232 + * lpt_tgc_start - start trivial garbage collection of LPT LEBs.
31233 + * @c: UBIFS file-system description object
31234 + *
31235 + * LPT trivial garbage collection is where a LPT LEB contains only dirty and
31236 + * free space and so may be reused as soon as the next commit is completed.
31237 + * This function is called during start commit to mark LPT LEBs for trivial GC.
31238 + */
31239 +static void lpt_tgc_start(struct ubifs_info *c)
31240 +{
31241 + int i;
31242 +
31243 + for (i = 0; i < c->lpt_lebs; i++) {
31244 + if (i + c->lpt_first == c->nhead_lnum)
31245 + continue;
31246 + if (c->ltab[i].dirty > 0 &&
31247 + c->ltab[i].free + c->ltab[i].dirty == c->leb_size) {
31248 + c->ltab[i].tgc = 1;
31249 + c->ltab[i].free = c->leb_size;
31250 + c->ltab[i].dirty = 0;
31251 + dbg_lp("LEB %d", i + c->lpt_first);
31252 + }
31253 + }
31254 +}
31255 +
31256 +/**
31257 + * lpt_tgc_end - end trivial garbage collection of LPT LEBs.
31258 + * @c: UBIFS file-system description object
31259 + *
31260 + * LPT trivial garbage collection is where a LPT LEB contains only dirty and
31261 + * free space and so may be reused as soon as the next commit is completed.
31262 + * This function is called after the commit is completed (master node has been
31263 + * written) and unmaps LPT LEBs that were marked for trivial GC.
31264 + */
31265 +static int lpt_tgc_end(struct ubifs_info *c)
31266 +{
31267 + int i, err;
31268 +
31269 + for (i = 0; i < c->lpt_lebs; i++)
31270 + if (c->ltab[i].tgc) {
31271 + err = ubifs_leb_unmap(c, i + c->lpt_first);
31272 + if (err)
31273 + return err;
31274 + c->ltab[i].tgc = 0;
31275 + dbg_lp("LEB %d", i + c->lpt_first);
31276 + }
31277 + return 0;
31278 +}
31279 +
31280 +/**
31281 + * populate_lsave - fill the lsave array with important LEB numbers.
31282 + * @c: the UBIFS file-system description object
31283 + *
31284 + * This function is only called for the "big" model. It records a small number
31285 + * of LEB numbers of important LEBs. Important LEBs are ones that are (from
31286 + * most important to least important): empty, freeable, freeable index, dirty
31287 + * index, dirty or free. Upon mount, we read this list of LEB numbers and bring
31288 + * their pnodes into memory. That will stop us from having to scan the LPT
31289 + * straight away. For the "small" model we assume that scanning the LPT is no
31290 + * big deal.
31291 + */
31292 +static void populate_lsave(struct ubifs_info *c)
31293 +{
31294 + struct ubifs_lprops *lprops;
31295 + struct ubifs_lpt_heap *heap;
31296 + int i, cnt = 0;
31297 +
31298 + ubifs_assert(c->big_lpt);
31299 + if (!(c->lpt_drty_flgs & LSAVE_DIRTY)) {
31300 + c->lpt_drty_flgs |= LSAVE_DIRTY;
31301 + ubifs_add_lpt_dirt(c, c->lsave_lnum, c->lsave_sz);
31302 + }
31303 + list_for_each_entry(lprops, &c->empty_list, list) {
31304 + c->lsave[cnt++] = lprops->lnum;
31305 + if (cnt >= c->lsave_cnt)
31306 + return;
31307 + }
31308 + list_for_each_entry(lprops, &c->freeable_list, list) {
31309 + c->lsave[cnt++] = lprops->lnum;
31310 + if (cnt >= c->lsave_cnt)
31311 + return;
31312 + }
31313 + list_for_each_entry(lprops, &c->frdi_idx_list, list) {
31314 + c->lsave[cnt++] = lprops->lnum;
31315 + if (cnt >= c->lsave_cnt)
31316 + return;
31317 + }
31318 + heap = &c->lpt_heap[LPROPS_DIRTY_IDX - 1];
31319 + for (i = 0; i < heap->cnt; i++) {
31320 + c->lsave[cnt++] = heap->arr[i]->lnum;
31321 + if (cnt >= c->lsave_cnt)
31322 + return;
31323 + }
31324 + heap = &c->lpt_heap[LPROPS_DIRTY - 1];
31325 + for (i = 0; i < heap->cnt; i++) {
31326 + c->lsave[cnt++] = heap->arr[i]->lnum;
31327 + if (cnt >= c->lsave_cnt)
31328 + return;
31329 + }
31330 + heap = &c->lpt_heap[LPROPS_FREE - 1];
31331 + for (i = 0; i < heap->cnt; i++) {
31332 + c->lsave[cnt++] = heap->arr[i]->lnum;
31333 + if (cnt >= c->lsave_cnt)
31334 + return;
31335 + }
31336 + /* Fill it up completely */
31337 + while (cnt < c->lsave_cnt)
31338 + c->lsave[cnt++] = c->main_first;
31339 +}
31340 +
31341 +/**
31342 + * ubifs_lpt_start_commit - UBIFS commit starts.
31343 + * @c: the UBIFS file-system description object
31344 + *
31345 + * This function has to be called when UBIFS starts the commit operation.
31346 + * This function "freezes" all currently dirty LEB properties and does not
31347 + * change them anymore. Further changes are saved and tracked separately
31348 + * because they are not part of this commit. This function returns zero in case
31349 + * of success and a negative error code in case of failure.
31350 + */
31351 +int ubifs_lpt_start_commit(struct ubifs_info *c)
31352 +{
31353 + int err, cnt;
31354 +
31355 + dbg_lp("");
31356 +
31357 + mutex_lock(&c->lp_mutex);
31358 + err = dbg_check_ltab(c);
31359 + if (err)
31360 + goto out;
31361 +
31362 + lpt_tgc_start(c);
31363 +
31364 + if (!c->dirty_pn_cnt) {
31365 + dbg_cmt("no cnodes to commit");
31366 + err = 0;
31367 + goto out;
31368 + }
31369 +
31370 + if (!c->big_lpt && need_write_all(c)) {
31371 + /* If needed, write everything */
31372 + err = make_tree_dirty(c);
31373 + if (err)
31374 + goto out;
31375 + lpt_tgc_start(c);
31376 + }
31377 +
31378 + if (c->big_lpt)
31379 + populate_lsave(c);
31380 +
31381 + cnt = get_cnodes_to_commit(c);
31382 + ubifs_assert(cnt != 0);
31383 +
31384 + err = layout_cnodes(c);
31385 + if (err)
31386 + goto out;
31387 +
31388 + /* Copy the LPT's own lprops for end commit to write */
31389 + memcpy(c->ltab_cmt, c->ltab,
31390 + sizeof(struct ubifs_lpt_lprops) * c->lpt_lebs);
31391 + c->lpt_drty_flgs &= ~(LTAB_DIRTY | LSAVE_DIRTY);
31392 +
31393 +out:
31394 + mutex_unlock(&c->lp_mutex);
31395 + return err;
31396 +}
31397 +
31398 +/**
31399 + * free_obsolete_cnodes - free obsolete cnodes for commit end.
31400 + * @c: UBIFS file-system description object
31401 + */
31402 +static void free_obsolete_cnodes(struct ubifs_info *c)
31403 +{
31404 + struct ubifs_cnode *cnode, *cnext;
31405 +
31406 + cnext = c->lpt_cnext;
31407 + if (!cnext)
31408 + return;
31409 + do {
31410 + cnode = cnext;
31411 + cnext = cnode->cnext;
31412 + if (test_bit(OBSOLETE_CNODE, &cnode->flags))
31413 + kfree(cnode);
31414 + else
31415 + cnode->cnext = NULL;
31416 + } while (cnext != c->lpt_cnext);
31417 + c->lpt_cnext = NULL;
31418 +}
31419 +
31420 +/**
31421 + * ubifs_lpt_end_commit - finish the commit operation.
31422 + * @c: the UBIFS file-system description object
31423 + *
31424 + * This function has to be called when the commit operation finishes. It
31425 + * flushes the changes which were "frozen" by 'ubifs_lprops_start_commit()' to
31426 + * the media. Returns zero in case of success and a negative error code in case
31427 + * of failure.
31428 + */
31429 +int ubifs_lpt_end_commit(struct ubifs_info *c)
31430 +{
31431 + int err;
31432 +
31433 + dbg_lp("");
31434 +
31435 + if (!c->lpt_cnext)
31436 + return 0;
31437 +
31438 + err = write_cnodes(c);
31439 + if (err)
31440 + return err;
31441 +
31442 + mutex_lock(&c->lp_mutex);
31443 + free_obsolete_cnodes(c);
31444 + mutex_unlock(&c->lp_mutex);
31445 +
31446 + return 0;
31447 +}
31448 +
31449 +/**
31450 + * nnode_lookup - lookup a nnode in the LPT.
31451 + * @c: UBIFS file-system description object
31452 + * @i: nnode number
31453 + *
31454 + * This function returns a pointer to the nnode on success or a negative
31455 + * error code on failure.
31456 + */
31457 +static struct ubifs_nnode *nnode_lookup(struct ubifs_info *c, int i)
31458 +{
31459 + int err, iip;
31460 + struct ubifs_nnode *nnode;
31461 +
31462 + if (!c->nroot) {
31463 + err = ubifs_read_nnode(c, NULL, 0);
31464 + if (err)
31465 + return ERR_PTR(err);
31466 + }
31467 + nnode = c->nroot;
31468 + while (1) {
31469 + iip = i & (UBIFS_LPT_FANOUT - 1);
31470 + i >>= UBIFS_LPT_FANOUT_SHIFT;
31471 + if (!i)
31472 + break;
31473 + nnode = ubifs_get_nnode(c, nnode, iip);
31474 + if (IS_ERR(nnode))
31475 + return nnode;
31476 + }
31477 + return nnode;
31478 +}
31479 +
31480 +/**
31481 + * make_nnode_dirty - find a nnode and, if found, make it dirty.
31482 + * @c: UBIFS file-system description object
31483 + * @node_num: nnode number of nnode to make dirty
31484 + * @lnum: LEB number where nnode was written
31485 + * @offs: offset where nnode was written
31486 + *
31487 + * This function is used by LPT garbage collection. LPT garbage collection is
31488 + * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
31489 + * simply involves marking all the nodes in the LEB being garbage-collected as
31490 + * dirty. The dirty nodes are written next commit, after which the LEB is free
31491 + * to be reused.
31492 + *
31493 + * This function returns %0 on success and a negative error code on failure.
31494 + */
31495 +static int make_nnode_dirty(struct ubifs_info *c, int node_num, int lnum,
31496 + int offs)
31497 +{
31498 + struct ubifs_nnode *nnode;
31499 +
31500 + nnode = nnode_lookup(c, node_num);
31501 + if (IS_ERR(nnode))
31502 + return PTR_ERR(nnode);
31503 + if (nnode->parent) {
31504 + struct ubifs_nbranch *branch;
31505 +
31506 + branch = &nnode->parent->nbranch[nnode->iip];
31507 + if (branch->lnum != lnum || branch->offs != offs)
31508 + return 0; /* nnode is obsolete */
31509 + } else if (c->lpt_lnum != lnum || c->lpt_offs != offs)
31510 + return 0; /* nnode is obsolete */
31511 + /* Assumes cnext list is empty i.e. not called during commit */
31512 + if (!test_and_set_bit(DIRTY_CNODE, &nnode->flags)) {
31513 + c->dirty_nn_cnt += 1;
31514 + ubifs_add_nnode_dirt(c, nnode);
31515 + /* Mark parent and ancestors dirty too */
31516 + nnode = nnode->parent;
31517 + while (nnode) {
31518 + if (!test_and_set_bit(DIRTY_CNODE, &nnode->flags)) {
31519 + c->dirty_nn_cnt += 1;
31520 + ubifs_add_nnode_dirt(c, nnode);
31521 + nnode = nnode->parent;
31522 + } else
31523 + break;
31524 + }
31525 + }
31526 + return 0;
31527 +}
31528 +
31529 +/**
31530 + * make_pnode_dirty - find a pnode and, if found, make it dirty.
31531 + * @c: UBIFS file-system description object
31532 + * @node_num: pnode number of pnode to make dirty
31533 + * @lnum: LEB number where pnode was written
31534 + * @offs: offset where pnode was written
31535 + *
31536 + * This function is used by LPT garbage collection. LPT garbage collection is
31537 + * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
31538 + * simply involves marking all the nodes in the LEB being garbage-collected as
31539 + * dirty. The dirty nodes are written next commit, after which the LEB is free
31540 + * to be reused.
31541 + *
31542 + * This function returns %0 on success and a negative error code on failure.
31543 + */
31544 +static int make_pnode_dirty(struct ubifs_info *c, int node_num, int lnum,
31545 + int offs)
31546 +{
31547 + struct ubifs_pnode *pnode;
31548 + struct ubifs_nbranch *branch;
31549 +
31550 + pnode = pnode_lookup(c, node_num);
31551 + if (IS_ERR(pnode))
31552 + return PTR_ERR(pnode);
31553 + branch = &pnode->parent->nbranch[pnode->iip];
31554 + if (branch->lnum != lnum || branch->offs != offs)
31555 + return 0;
31556 + do_make_pnode_dirty(c, pnode);
31557 + return 0;
31558 +}
31559 +
31560 +/**
31561 + * make_ltab_dirty - make ltab node dirty.
31562 + * @c: UBIFS file-system description object
31563 + * @lnum: LEB number where ltab was written
31564 + * @offs: offset where ltab was written
31565 + *
31566 + * This function is used by LPT garbage collection. LPT garbage collection is
31567 + * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
31568 + * simply involves marking all the nodes in the LEB being garbage-collected as
31569 + * dirty. The dirty nodes are written next commit, after which the LEB is free
31570 + * to be reused.
31571 + *
31572 + * This function returns %0 on success and a negative error code on failure.
31573 + */
31574 +static int make_ltab_dirty(struct ubifs_info *c, int lnum, int offs)
31575 +{
31576 + if (lnum != c->ltab_lnum || offs != c->ltab_offs)
31577 + return 0; /* This ltab node is obsolete */
31578 + if (!(c->lpt_drty_flgs & LTAB_DIRTY)) {
31579 + c->lpt_drty_flgs |= LTAB_DIRTY;
31580 + ubifs_add_lpt_dirt(c, c->ltab_lnum, c->ltab_sz);
31581 + }
31582 + return 0;
31583 +}
31584 +
31585 +/**
31586 + * make_lsave_dirty - make lsave node dirty.
31587 + * @c: UBIFS file-system description object
31588 + * @lnum: LEB number where lsave was written
31589 + * @offs: offset where lsave was written
31590 + *
31591 + * This function is used by LPT garbage collection. LPT garbage collection is
31592 + * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
31593 + * simply involves marking all the nodes in the LEB being garbage-collected as
31594 + * dirty. The dirty nodes are written next commit, after which the LEB is free
31595 + * to be reused.
31596 + *
31597 + * This function returns %0 on success and a negative error code on failure.
31598 + */
31599 +static int make_lsave_dirty(struct ubifs_info *c, int lnum, int offs)
31600 +{
31601 + if (lnum != c->lsave_lnum || offs != c->lsave_offs)
31602 + return 0; /* This lsave node is obsolete */
31603 + if (!(c->lpt_drty_flgs & LSAVE_DIRTY)) {
31604 + c->lpt_drty_flgs |= LSAVE_DIRTY;
31605 + ubifs_add_lpt_dirt(c, c->lsave_lnum, c->lsave_sz);
31606 + }
31607 + return 0;
31608 +}
31609 +
31610 +/**
31611 + * make_node_dirty - make node dirty.
31612 + * @c: UBIFS file-system description object
31613 + * @node_type: LPT node type
31614 + * @node_num: node number
31615 + * @lnum: LEB number where node was written
31616 + * @offs: offset where node was written
31617 + *
31618 + * This function is used by LPT garbage collection. LPT garbage collection is
31619 + * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
31620 + * simply involves marking all the nodes in the LEB being garbage-collected as
31621 + * dirty. The dirty nodes are written next commit, after which the LEB is free
31622 + * to be reused.
31623 + *
31624 + * This function returns %0 on success and a negative error code on failure.
31625 + */
31626 +static int make_node_dirty(struct ubifs_info *c, int node_type, int node_num,
31627 + int lnum, int offs)
31628 +{
31629 + switch (node_type) {
31630 + case UBIFS_LPT_NNODE:
31631 + return make_nnode_dirty(c, node_num, lnum, offs);
31632 + case UBIFS_LPT_PNODE:
31633 + return make_pnode_dirty(c, node_num, lnum, offs);
31634 + case UBIFS_LPT_LTAB:
31635 + return make_ltab_dirty(c, lnum, offs);
31636 + case UBIFS_LPT_LSAVE:
31637 + return make_lsave_dirty(c, lnum, offs);
31638 + }
31639 + return -EINVAL;
31640 +}
31641 +
31642 +/**
31643 + * get_lpt_node_len - return the length of a node based on its type.
31644 + * @c: UBIFS file-system description object
31645 + * @node_type: LPT node type
31646 + */
31647 +static int get_lpt_node_len(struct ubifs_info *c, int node_type)
31648 +{
31649 + switch (node_type) {
31650 + case UBIFS_LPT_NNODE:
31651 + return c->nnode_sz;
31652 + case UBIFS_LPT_PNODE:
31653 + return c->pnode_sz;
31654 + case UBIFS_LPT_LTAB:
31655 + return c->ltab_sz;
31656 + case UBIFS_LPT_LSAVE:
31657 + return c->lsave_sz;
31658 + }
31659 + return 0;
31660 +}
31661 +
31662 +/**
31663 + * get_pad_len - return the length of padding in a buffer.
31664 + * @c: UBIFS file-system description object
31665 + * @buf: buffer
31666 + * @len: length of buffer
31667 + */
31668 +static int get_pad_len(struct ubifs_info *c, uint8_t *buf, int len)
31669 +{
31670 + int offs, pad_len;
31671 +
31672 + if (c->min_io_size == 1)
31673 + return 0;
31674 + offs = c->leb_size - len;
31675 + pad_len = ALIGN(offs, c->min_io_size) - offs;
31676 + return pad_len;
31677 +}
31678 +
31679 +/**
31680 + * get_lpt_node_type - return type (and node number) of a node in a buffer.
31681 + * @c: UBIFS file-system description object
31682 + * @buf: buffer
31683 + * @node_num: node number is returned here
31684 + */
31685 +static int get_lpt_node_type(struct ubifs_info *c, uint8_t *buf, int *node_num)
31686 +{
31687 + uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
31688 + int pos = 0, node_type;
31689 +
31690 + node_type = ubifs_unpack_bits(&addr, &pos, UBIFS_LPT_TYPE_BITS);
31691 + *node_num = ubifs_unpack_bits(&addr, &pos, c->pcnt_bits);
31692 + return node_type;
31693 +}
31694 +
31695 +/**
31696 + * is_a_node - determine if a buffer contains a node.
31697 + * @c: UBIFS file-system description object
31698 + * @buf: buffer
31699 + * @len: length of buffer
31700 + *
31701 + * This function returns %1 if the buffer contains a node or %0 if it does not.
31702 + */
31703 +static int is_a_node(struct ubifs_info *c, uint8_t *buf, int len)
31704 +{
31705 + uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
31706 + int pos = 0, node_type, node_len;
31707 + uint16_t crc, calc_crc;
31708 +
31709 + node_type = ubifs_unpack_bits(&addr, &pos, UBIFS_LPT_TYPE_BITS);
31710 + if (node_type == UBIFS_LPT_NOT_A_NODE)
31711 + return 0;
31712 + node_len = get_lpt_node_len(c, node_type);
31713 + if (!node_len || node_len > len)
31714 + return 0;
31715 + pos = 0;
31716 + addr = buf;
31717 + crc = ubifs_unpack_bits(&addr, &pos, UBIFS_LPT_CRC_BITS);
31718 + calc_crc = crc16(-1, buf + UBIFS_LPT_CRC_BYTES,
31719 + node_len - UBIFS_LPT_CRC_BYTES);
31720 + if (crc != calc_crc)
31721 + return 0;
31722 + return 1;
31723 +}
31724 +
31725 +
31726 +/**
31727 + * lpt_gc_lnum - garbage collect a LPT LEB.
31728 + * @c: UBIFS file-system description object
31729 + * @lnum: LEB number to garbage collect
31730 + *
31731 + * LPT garbage collection is used only for the "big" LPT model
31732 + * (c->big_lpt == 1). Garbage collection simply involves marking all the nodes
31733 + * in the LEB being garbage-collected as dirty. The dirty nodes are written
31734 + * next commit, after which the LEB is free to be reused.
31735 + *
31736 + * This function returns %0 on success and a negative error code on failure.
31737 + */
31738 +static int lpt_gc_lnum(struct ubifs_info *c, int lnum)
31739 +{
31740 + int err, len = c->leb_size, node_type, node_num, node_len, offs;
31741 + void *buf = c->lpt_buf;
31742 +
31743 + dbg_lp("LEB %d", lnum);
31744 + err = ubi_read(c->ubi, lnum, buf, 0, c->leb_size);
31745 + if (err) {
31746 + ubifs_err("cannot read LEB %d, error %d", lnum, err);
31747 + return err;
31748 + }
31749 + while (1) {
31750 + if (!is_a_node(c, buf, len)) {
31751 + int pad_len;
31752 +
31753 + pad_len = get_pad_len(c, buf, len);
31754 + if (pad_len) {
31755 + buf += pad_len;
31756 + len -= pad_len;
31757 + continue;
31758 + }
31759 + return 0;
31760 + }
31761 + node_type = get_lpt_node_type(c, buf, &node_num);
31762 + node_len = get_lpt_node_len(c, node_type);
31763 + offs = c->leb_size - len;
31764 + ubifs_assert(node_len != 0);
31765 + mutex_lock(&c->lp_mutex);
31766 + err = make_node_dirty(c, node_type, node_num, lnum, offs);
31767 + mutex_unlock(&c->lp_mutex);
31768 + if (err)
31769 + return err;
31770 + buf += node_len;
31771 + len -= node_len;
31772 + }
31773 + return 0;
31774 +}
31775 +
31776 +/**
31777 + * lpt_gc - LPT garbage collection.
31778 + * @c: UBIFS file-system description object
31779 + *
31780 + * Select a LPT LEB for LPT garbage collection and call 'lpt_gc_lnum()'.
31781 + * Returns %0 on success and a negative error code on failure.
31782 + */
31783 +static int lpt_gc(struct ubifs_info *c)
31784 +{
31785 + int i, lnum = -1, dirty = 0;
31786 +
31787 + mutex_lock(&c->lp_mutex);
31788 + for (i = 0; i < c->lpt_lebs; i++) {
31789 + ubifs_assert(!c->ltab[i].tgc);
31790 + if (i + c->lpt_first == c->nhead_lnum ||
31791 + c->ltab[i].free + c->ltab[i].dirty == c->leb_size)
31792 + continue;
31793 + if (c->ltab[i].dirty > dirty) {
31794 + dirty = c->ltab[i].dirty;
31795 + lnum = i + c->lpt_first;
31796 + }
31797 + }
31798 + mutex_unlock(&c->lp_mutex);
31799 + if (lnum == -1)
31800 + return -ENOSPC;
31801 + return lpt_gc_lnum(c, lnum);
31802 +}
31803 +
31804 +/**
31805 + * ubifs_lpt_post_commit - post commit LPT trivial GC and LPT GC.
31806 + * @c: UBIFS file-system description object
31807 + *
31808 + * LPT trivial GC is completed after a commit. Also LPT GC is done after a
31809 + * commit for the "big" LPT model.
31810 + */
31811 +int ubifs_lpt_post_commit(struct ubifs_info *c)
31812 +{
31813 + int err;
31814 +
31815 + mutex_lock(&c->lp_mutex);
31816 + err = lpt_tgc_end(c);
31817 + if (err)
31818 + goto out;
31819 + if (c->big_lpt)
31820 + while (need_write_all(c)) {
31821 + mutex_unlock(&c->lp_mutex);
31822 + err = lpt_gc(c);
31823 + if (err)
31824 + return err;
31825 + mutex_lock(&c->lp_mutex);
31826 + }
31827 +out:
31828 + mutex_unlock(&c->lp_mutex);
31829 + return err;
31830 +}
31831 +
31832 +/**
31833 + * first_nnode - find the first nnode in memory.
31834 + * @c: UBIFS file-system description object
31835 + * @hght: height of tree where nnode found is returned here
31836 + *
31837 + * This function returns a pointer to the nnode found or %NULL if no nnode is
31838 + * found. This function is a helper to 'ubifs_lpt_free()'.
31839 + */
31840 +static struct ubifs_nnode *first_nnode(struct ubifs_info *c, int *hght)
31841 +{
31842 + struct ubifs_nnode *nnode;
31843 + int h, i, found;
31844 +
31845 + nnode = c->nroot;
31846 + *hght = 0;
31847 + if (!nnode)
31848 + return NULL;
31849 + for (h = 1; h < c->lpt_hght; h++) {
31850 + found = 0;
31851 + for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
31852 + if (nnode->nbranch[i].nnode) {
31853 + found = 1;
31854 + nnode = nnode->nbranch[i].nnode;
31855 + *hght = h;
31856 + break;
31857 + }
31858 + }
31859 + if (!found)
31860 + break;
31861 + }
31862 + return nnode;
31863 +}
31864 +
31865 +/**
31866 + * next_nnode - find the next nnode in memory.
31867 + * @c: UBIFS file-system description object
31868 + * @nnode: nnode from which to start.
31869 + * @hght: height of tree where nnode is, is passed and returned here
31870 + *
31871 + * This function returns a pointer to the nnode found or %NULL if no nnode is
31872 + * found. This function is a helper to 'ubifs_lpt_free()'.
31873 + */
31874 +static struct ubifs_nnode *next_nnode(struct ubifs_info *c,
31875 + struct ubifs_nnode *nnode, int *hght)
31876 +{
31877 + struct ubifs_nnode *parent;
31878 + int iip, h, i, found;
31879 +
31880 + parent = nnode->parent;
31881 + if (!parent)
31882 + return NULL;
31883 + if (nnode->iip == UBIFS_LPT_FANOUT - 1) {
31884 + *hght -= 1;
31885 + return parent;
31886 + }
31887 + for (iip = nnode->iip + 1; iip < UBIFS_LPT_FANOUT; iip++) {
31888 + nnode = parent->nbranch[iip].nnode;
31889 + if (nnode)
31890 + break;
31891 + }
31892 + if (!nnode) {
31893 + *hght -= 1;
31894 + return parent;
31895 + }
31896 + for (h = *hght + 1; h < c->lpt_hght; h++) {
31897 + found = 0;
31898 + for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
31899 + if (nnode->nbranch[i].nnode) {
31900 + found = 1;
31901 + nnode = nnode->nbranch[i].nnode;
31902 + *hght = h;
31903 + break;
31904 + }
31905 + }
31906 + if (!found)
31907 + break;
31908 + }
31909 + return nnode;
31910 +}
31911 +
31912 +/**
31913 + * ubifs_lpt_free - free resources owned by the LPT.
31914 + * @c: UBIFS file-system description object
31915 + * @wr_only: free only resources used for writing
31916 + */
31917 +void ubifs_lpt_free(struct ubifs_info *c, int wr_only)
31918 +{
31919 + struct ubifs_nnode *nnode;
31920 + int i, hght;
31921 +
31922 + /* Free write-only things first */
31923 +
31924 + free_obsolete_cnodes(c); /* Leftover from a failed commit */
31925 +
31926 + vfree(c->ltab_cmt);
31927 + c->ltab_cmt = NULL;
31928 + vfree(c->lpt_buf);
31929 + c->lpt_buf = NULL;
31930 + kfree(c->lsave);
31931 + c->lsave = NULL;
31932 +
31933 + if (wr_only)
31934 + return;
31935 +
31936 + /* Now free the rest */
31937 +
31938 + nnode = first_nnode(c, &hght);
31939 + while (nnode) {
31940 + for (i = 0; i < UBIFS_LPT_FANOUT; i++)
31941 + kfree(nnode->nbranch[i].nnode);
31942 + nnode = next_nnode(c, nnode, &hght);
31943 + }
31944 + for (i = 0; i < LPROPS_HEAP_CNT; i++)
31945 + kfree(c->lpt_heap[i].arr);
31946 + kfree(c->dirty_idx.arr);
31947 + kfree(c->nroot);
31948 + vfree(c->ltab);
31949 + kfree(c->lpt_nod_buf);
31950 +}
31951 +
31952 +#ifdef CONFIG_UBIFS_FS_DEBUG
31953 +
31954 +/**
31955 + * dbg_is_all_ff - determine if a buffer contains only 0xff bytes.
31956 + * @buf: buffer
31957 + * @len: buffer length
31958 + */
31959 +static int dbg_is_all_ff(uint8_t *buf, int len)
31960 +{
31961 + int i;
31962 +
31963 + for (i = 0; i < len; i++)
31964 + if (buf[i] != 0xff)
31965 + return 0;
31966 + return 1;
31967 +}
31968 +
31969 +/**
31970 + * dbg_is_nnode_dirty - determine if a nnode is dirty.
31971 + * @c: the UBIFS file-system description object
31972 + * @lnum: LEB number where nnode was written
31973 + * @offs: offset where nnode was written
31974 + */
31975 +static int dbg_is_nnode_dirty(struct ubifs_info *c, int lnum, int offs)
31976 +{
31977 + struct ubifs_nnode *nnode;
31978 + int hght;
31979 +
31980 + /* Entire tree is in memory so first_nnode / next_nnode are ok */
31981 + nnode = first_nnode(c, &hght);
31982 + for (; nnode; nnode = next_nnode(c, nnode, &hght)) {
31983 + struct ubifs_nbranch *branch;
31984 +
31985 + cond_resched();
31986 + if (nnode->parent) {
31987 + branch = &nnode->parent->nbranch[nnode->iip];
31988 + if (branch->lnum != lnum || branch->offs != offs)
31989 + continue;
31990 + if (test_bit(DIRTY_CNODE, &nnode->flags))
31991 + return 1;
31992 + return 0;
31993 + } else {
31994 + if (c->lpt_lnum != lnum || c->lpt_offs != offs)
31995 + continue;
31996 + if (test_bit(DIRTY_CNODE, &nnode->flags))
31997 + return 1;
31998 + return 0;
31999 + }
32000 + }
32001 + return 1;
32002 +}
32003 +
32004 +/**
32005 + * dbg_is_pnode_dirty - determine if a pnode is dirty.
32006 + * @c: the UBIFS file-system description object
32007 + * @lnum: LEB number where pnode was written
32008 + * @offs: offset where pnode was written
32009 + */
32010 +static int dbg_is_pnode_dirty(struct ubifs_info *c, int lnum, int offs)
32011 +{
32012 + int i, cnt;
32013 +
32014 + cnt = DIV_ROUND_UP(c->main_lebs, UBIFS_LPT_FANOUT);
32015 + for (i = 0; i < cnt; i++) {
32016 + struct ubifs_pnode *pnode;
32017 + struct ubifs_nbranch *branch;
32018 +
32019 + cond_resched();
32020 + pnode = pnode_lookup(c, i);
32021 + if (IS_ERR(pnode))
32022 + return PTR_ERR(pnode);
32023 + branch = &pnode->parent->nbranch[pnode->iip];
32024 + if (branch->lnum != lnum || branch->offs != offs)
32025 + continue;
32026 + if (test_bit(DIRTY_CNODE, &pnode->flags))
32027 + return 1;
32028 + return 0;
32029 + }
32030 + return 1;
32031 +}
32032 +
32033 +/**
32034 + * dbg_is_ltab_dirty - determine if a ltab node is dirty.
32035 + * @c: the UBIFS file-system description object
32036 + * @lnum: LEB number where ltab node was written
32037 + * @offs: offset where ltab node was written
32038 + */
32039 +static int dbg_is_ltab_dirty(struct ubifs_info *c, int lnum, int offs)
32040 +{
32041 + if (lnum != c->ltab_lnum || offs != c->ltab_offs)
32042 + return 1;
32043 + return (c->lpt_drty_flgs & LTAB_DIRTY) != 0;
32044 +}
32045 +
32046 +/**
32047 + * dbg_is_lsave_dirty - determine if a lsave node is dirty.
32048 + * @c: the UBIFS file-system description object
32049 + * @lnum: LEB number where lsave node was written
32050 + * @offs: offset where lsave node was written
32051 + */
32052 +static int dbg_is_lsave_dirty(struct ubifs_info *c, int lnum, int offs)
32053 +{
32054 + if (lnum != c->lsave_lnum || offs != c->lsave_offs)
32055 + return 1;
32056 + return (c->lpt_drty_flgs & LSAVE_DIRTY) != 0;
32057 +}
32058 +
32059 +/**
32060 + * dbg_is_node_dirty - determine if a node is dirty.
32061 + * @c: the UBIFS file-system description object
32062 + * @node_type: node type
32063 + * @lnum: LEB number where node was written
32064 + * @offs: offset where node was written
32065 + */
32066 +static int dbg_is_node_dirty(struct ubifs_info *c, int node_type, int lnum,
32067 + int offs)
32068 +{
32069 + switch (node_type) {
32070 + case UBIFS_LPT_NNODE:
32071 + return dbg_is_nnode_dirty(c, lnum, offs);
32072 + case UBIFS_LPT_PNODE:
32073 + return dbg_is_pnode_dirty(c, lnum, offs);
32074 + case UBIFS_LPT_LTAB:
32075 + return dbg_is_ltab_dirty(c, lnum, offs);
32076 + case UBIFS_LPT_LSAVE:
32077 + return dbg_is_lsave_dirty(c, lnum, offs);
32078 + }
32079 + return 1;
32080 +}
32081 +
32082 +/**
32083 + * dbg_check_ltab_lnum - check the ltab for a LPT LEB number.
32084 + * @c: the UBIFS file-system description object
32085 + * @lnum: LEB number where node was written
32086 + * @offs: offset where node was written
32087 + *
32088 + * This function returns %0 on success and a negative error code on failure.
32089 + */
32090 +static int dbg_check_ltab_lnum(struct ubifs_info *c, int lnum)
32091 +{
32092 + int err, len = c->leb_size, dirty = 0, node_type, node_num, node_len;
32093 + int ret;
32094 + void *buf = c->dbg_buf;
32095 +
32096 + dbg_lp("LEB %d", lnum);
32097 + err = ubi_read(c->ubi, lnum, buf, 0, c->leb_size);
32098 + if (err) {
32099 + dbg_msg("ubi_read failed, LEB %d, error %d", lnum, err);
32100 + return err;
32101 + }
32102 + while (1) {
32103 + if (!is_a_node(c, buf, len)) {
32104 + int i, pad_len;
32105 +
32106 + pad_len = get_pad_len(c, buf, len);
32107 + if (pad_len) {
32108 + buf += pad_len;
32109 + len -= pad_len;
32110 + dirty += pad_len;
32111 + continue;
32112 + }
32113 + if (!dbg_is_all_ff(buf, len)) {
32114 + dbg_msg("invalid empty space in LEB %d at %d",
32115 + lnum, c->leb_size - len);
32116 + err = -EINVAL;
32117 + }
32118 + i = lnum - c->lpt_first;
32119 + if (len != c->ltab[i].free) {
32120 + dbg_msg("invalid free space in LEB %d "
32121 + "(free %d, expected %d)",
32122 + lnum, len, c->ltab[i].free);
32123 + err = -EINVAL;
32124 + }
32125 + if (dirty != c->ltab[i].dirty) {
32126 + dbg_msg("invalid dirty space in LEB %d "
32127 + "(dirty %d, expected %d)",
32128 + lnum, dirty, c->ltab[i].dirty);
32129 + err = -EINVAL;
32130 + }
32131 + return err;
32132 + }
32133 + node_type = get_lpt_node_type(c, buf, &node_num);
32134 + node_len = get_lpt_node_len(c, node_type);
32135 + ret = dbg_is_node_dirty(c, node_type, lnum, c->leb_size - len);
32136 + if (ret == 1)
32137 + dirty += node_len;
32138 + buf += node_len;
32139 + len -= node_len;
32140 + }
32141 +}
32142 +
32143 +/**
32144 + * dbg_check_ltab - check the free and dirty space in the ltab.
32145 + * @c: the UBIFS file-system description object
32146 + *
32147 + * This function returns %0 on success and a negative error code on failure.
32148 + */
32149 +int dbg_check_ltab(struct ubifs_info *c)
32150 +{
32151 + int lnum, err, i, cnt;
32152 +
32153 + if (!(ubifs_chk_flags & UBIFS_CHK_LPROPS))
32154 + return 0;
32155 +
32156 + /* Bring the entire tree into memory */
32157 + cnt = DIV_ROUND_UP(c->main_lebs, UBIFS_LPT_FANOUT);
32158 + for (i = 0; i < cnt; i++) {
32159 + struct ubifs_pnode *pnode;
32160 +
32161 + pnode = pnode_lookup(c, i);
32162 + if (IS_ERR(pnode))
32163 + return PTR_ERR(pnode);
32164 + cond_resched();
32165 + }
32166 +
32167 + /* Check nodes */
32168 + err = dbg_check_lpt_nodes(c, (struct ubifs_cnode *)c->nroot, 0, 0);
32169 + if (err)
32170 + return err;
32171 +
32172 + /* Check each LEB */
32173 + for (lnum = c->lpt_first; lnum <= c->lpt_last; lnum++) {
32174 + err = dbg_check_ltab_lnum(c, lnum);
32175 + if (err) {
32176 + dbg_err("failed at LEB %d", lnum);
32177 + return err;
32178 + }
32179 + }
32180 +
32181 + dbg_lp("succeeded");
32182 + return 0;
32183 +}
32184 +
32185 +#endif /* CONFIG_UBIFS_FS_DEBUG */
32186 diff --exclude=.git -urN linux-2.6.25.6/fs/ubifs/Makefile avr32-2.6/fs/ubifs/Makefile
32187 --- linux-2.6.25.6/fs/ubifs/Makefile 1970-01-01 01:00:00.000000000 +0100
32188 +++ avr32-2.6/fs/ubifs/Makefile 2008-06-12 15:09:45.311815896 +0200
32189 @@ -0,0 +1,9 @@
32190 +obj-$(CONFIG_UBIFS_FS) += ubifs.o
32191 +
32192 +ubifs-y += shrinker.o journal.o file.o dir.o super.o sb.o io.o
32193 +ubifs-y += tnc.o master.o scan.o replay.o log.o commit.o gc.o orphan.o
32194 +ubifs-y += budget.o find.o tnc_commit.o compress.o lpt.o lprops.o
32195 +ubifs-y += recovery.o ioctl.o lpt_commit.o tnc_misc.o
32196 +
32197 +ubifs-$(CONFIG_UBIFS_FS_DEBUG) += debug.o
32198 +ubifs-$(CONFIG_UBIFS_FS_XATTR) += xattr.o
32199 diff --exclude=.git -urN linux-2.6.25.6/fs/ubifs/master.c avr32-2.6/fs/ubifs/master.c
32200 --- linux-2.6.25.6/fs/ubifs/master.c 1970-01-01 01:00:00.000000000 +0100
32201 +++ avr32-2.6/fs/ubifs/master.c 2008-06-12 15:09:45.475816115 +0200
32202 @@ -0,0 +1,387 @@
32203 +/*
32204 + * This file is part of UBIFS.
32205 + *
32206 + * Copyright (C) 2006-2008 Nokia Corporation.
32207 + *
32208 + * This program is free software; you can redistribute it and/or modify it
32209 + * under the terms of the GNU General Public License version 2 as published by
32210 + * the Free Software Foundation.
32211 + *
32212 + * This program is distributed in the hope that it will be useful, but WITHOUT
32213 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
32214 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
32215 + * more details.
32216 + *
32217 + * You should have received a copy of the GNU General Public License along with
32218 + * this program; if not, write to the Free Software Foundation, Inc., 51
32219 + * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
32220 + *
32221 + * Authors: Artem Bityutskiy (Битюцкий Артём)
32222 + * Adrian Hunter
32223 + */
32224 +
32225 +/* This file implements reading and writing the master node */
32226 +
32227 +#include "ubifs.h"
32228 +
32229 +/**
32230 + * scan_for_master - search the valid master node.
32231 + * @c: UBIFS file-system description object
32232 + *
32233 + * This function scans the master node LEBs and search for the latest master
32234 + * node. Returns zero in case of success and a negative error code in case of
32235 + * failure.
32236 + */
32237 +static int scan_for_master(struct ubifs_info *c)
32238 +{
32239 + struct ubifs_scan_leb *sleb;
32240 + struct ubifs_scan_node *snod;
32241 + int lnum, offs = 0, nodes_cnt;
32242 +
32243 + lnum = UBIFS_MST_LNUM;
32244 +
32245 + sleb = ubifs_scan(c, lnum, 0, c->sbuf);
32246 + if (IS_ERR(sleb))
32247 + return PTR_ERR(sleb);
32248 + nodes_cnt = sleb->nodes_cnt;
32249 + if (nodes_cnt > 0) {
32250 + snod = list_entry(sleb->nodes.prev, struct ubifs_scan_node,
32251 + list);
32252 + if (snod->type != UBIFS_MST_NODE)
32253 + goto out;
32254 + memcpy(c->mst_node, snod->node, snod->len);
32255 + offs = snod->offs;
32256 + }
32257 + ubifs_scan_destroy(sleb);
32258 +
32259 + lnum += 1;
32260 +
32261 + sleb = ubifs_scan(c, lnum, 0, c->sbuf);
32262 + if (IS_ERR(sleb))
32263 + return PTR_ERR(sleb);
32264 + if (sleb->nodes_cnt != nodes_cnt)
32265 + goto out;
32266 + if (!sleb->nodes_cnt)
32267 + goto out;
32268 + snod = list_entry(sleb->nodes.prev, struct ubifs_scan_node, list);
32269 + if (snod->type != UBIFS_MST_NODE)
32270 + goto out;
32271 + if (snod->offs != offs)
32272 + goto out;
32273 + if (memcmp((void *)c->mst_node + UBIFS_CH_SZ,
32274 + (void *)snod->node + UBIFS_CH_SZ,
32275 + UBIFS_MST_NODE_SZ - UBIFS_CH_SZ))
32276 + goto out;
32277 + c->mst_offs = offs;
32278 + ubifs_scan_destroy(sleb);
32279 + return 0;
32280 +
32281 +out:
32282 + ubifs_scan_destroy(sleb);
32283 + return -EINVAL;
32284 +}
32285 +
32286 +/**
32287 + * validate_master - validate master node.
32288 + * @c: UBIFS file-system description object
32289 + *
32290 + * This function validates data which was read from master node. Returns zero
32291 + * if the data is all right and %-EINVAL if not.
32292 + */
32293 +static int validate_master(const struct ubifs_info *c)
32294 +{
32295 + unsigned long long main_sz;
32296 + int err;
32297 +
32298 + if (c->max_sqnum >= SQNUM_WATERMARK) {
32299 + err = 1;
32300 + goto out;
32301 + }
32302 +
32303 + if (c->cmt_no >= c->max_sqnum) {
32304 + err = 2;
32305 + goto out;
32306 + }
32307 +
32308 + if (c->highest_inum >= INUM_WATERMARK) {
32309 + err = 3;
32310 + goto out;
32311 + }
32312 +
32313 + if (c->lhead_lnum < UBIFS_LOG_LNUM ||
32314 + c->lhead_lnum >= UBIFS_LOG_LNUM + c->log_lebs ||
32315 + c->lhead_offs < 0 || c->lhead_offs >= c->leb_size ||
32316 + c->lhead_offs & (c->min_io_size - 1)) {
32317 + err = 4;
32318 + goto out;
32319 + }
32320 +
32321 + if (c->zroot.lnum >= c->leb_cnt || c->zroot.lnum < c->main_first ||
32322 + c->zroot.offs >= c->leb_size || c->zroot.offs & 7) {
32323 + err = 5;
32324 + goto out;
32325 + }
32326 +
32327 + if (c->zroot.len < c->ranges[UBIFS_IDX_NODE].min_len ||
32328 + c->zroot.len > c->ranges[UBIFS_IDX_NODE].max_len) {
32329 + err = 6;
32330 + goto out;
32331 + }
32332 +
32333 + if (c->gc_lnum >= c->leb_cnt || c->gc_lnum < c->main_first) {
32334 + err = 7;
32335 + goto out;
32336 + }
32337 +
32338 + if (c->ihead_lnum >= c->leb_cnt || c->ihead_lnum < c->main_first ||
32339 + c->ihead_offs % c->min_io_size || c->ihead_offs < 0 ||
32340 + c->ihead_offs > c->leb_size || c->ihead_offs & 7) {
32341 + err = 8;
32342 + goto out;
32343 + }
32344 +
32345 + main_sz = c->main_lebs * (unsigned long long)c->leb_size;
32346 + if (c->old_idx_sz & 7 || c->old_idx_sz >= main_sz) {
32347 + err = 9;
32348 + goto out;
32349 + }
32350 +
32351 + if (c->lpt_lnum < c->lpt_first || c->lpt_lnum > c->lpt_last ||
32352 + c->lpt_offs < 0 || c->lpt_offs + c->nnode_sz > c->leb_size) {
32353 + err = 10;
32354 + goto out;
32355 + }
32356 +
32357 + if (c->nhead_lnum < c->lpt_first || c->nhead_lnum > c->lpt_last ||
32358 + c->nhead_offs < 0 || c->nhead_offs % c->min_io_size ||
32359 + c->nhead_offs > c->leb_size) {
32360 + err = 11;
32361 + goto out;
32362 + }
32363 +
32364 + if (c->ltab_lnum < c->lpt_first || c->ltab_lnum > c->lpt_last ||
32365 + c->ltab_offs < 0 ||
32366 + c->ltab_offs + c->ltab_sz > c->leb_size) {
32367 + err = 12;
32368 + goto out;
32369 + }
32370 +
32371 + if (c->big_lpt && (c->lsave_lnum < c->lpt_first ||
32372 + c->lsave_lnum > c->lpt_last || c->lsave_offs < 0 ||
32373 + c->lsave_offs + c->lsave_sz > c->leb_size)) {
32374 + err = 13;
32375 + goto out;
32376 + }
32377 +
32378 + if (c->lscan_lnum < c->main_first || c->lscan_lnum >= c->leb_cnt) {
32379 + err = 14;
32380 + goto out;
32381 + }
32382 +
32383 + if (c->lst.empty_lebs < 0 || c->lst.empty_lebs > c->main_lebs - 2) {
32384 + err = 15;
32385 + goto out;
32386 + }
32387 +
32388 + if (c->lst.idx_lebs < 0 || c->lst.idx_lebs > c->main_lebs - 1) {
32389 + err = 16;
32390 + goto out;
32391 + }
32392 +
32393 + if (c->lst.total_free < 0 || c->lst.total_free > main_sz ||
32394 + c->lst.total_free & 7) {
32395 + err = 17;
32396 + goto out;
32397 + }
32398 +
32399 + if (c->lst.total_dirty < 0 || (c->lst.total_dirty & 7)) {
32400 + err = 18;
32401 + goto out;
32402 + }
32403 +
32404 + if (c->lst.total_used < 0 || (c->lst.total_used & 7)) {
32405 + err = 19;
32406 + goto out;
32407 + }
32408 +
32409 + if (c->lst.total_free + c->lst.total_dirty +
32410 + c->lst.total_used > main_sz) {
32411 + err = 20;
32412 + goto out;
32413 + }
32414 +
32415 + if (c->lst.total_dead + c->lst.total_dark +
32416 + c->lst.total_used + c->old_idx_sz > main_sz) {
32417 + err = 21;
32418 + goto out;
32419 + }
32420 +
32421 + if (c->lst.total_dead < 0 ||
32422 + c->lst.total_dead > c->lst.total_free + c->lst.total_dirty ||
32423 + c->lst.total_dead & 7) {
32424 + err = 22;
32425 + goto out;
32426 + }
32427 +
32428 + if (c->lst.total_dark < 0 ||
32429 + c->lst.total_dark > c->lst.total_free + c->lst.total_dirty ||
32430 + c->lst.total_dark & 7) {
32431 + err = 23;
32432 + goto out;
32433 + }
32434 +
32435 + return 0;
32436 +
32437 +out:
32438 + ubifs_err("bad master node at offset %d error %d", c->mst_offs, err);
32439 + dbg_dump_node(c, c->mst_node);
32440 + return -EINVAL;
32441 +}
32442 +
32443 +/**
32444 + * ubifs_read_master - read master node.
32445 + * @c: UBIFS file-system description object
32446 + *
32447 + * This function finds and reads the master node during file-system mount. If
32448 + * the flash is empty, it creates default master node as well. Returns zero in
32449 + * case of success and a negative error code in case of failure.
32450 + */
32451 +int ubifs_read_master(struct ubifs_info *c)
32452 +{
32453 + int err, old_leb_cnt;
32454 +
32455 + c->mst_node = kzalloc(c->mst_node_alsz, GFP_KERNEL);
32456 + if (!c->mst_node)
32457 + return -ENOMEM;
32458 +
32459 + err = scan_for_master(c);
32460 + if (err) {
32461 + err = ubifs_recover_master_node(c);
32462 + if (err)
32463 + /*
32464 + * Note, we do not free 'c->mst_node' here because the
32465 + * unmount routine will take care of this.
32466 + */
32467 + return err;
32468 + }
32469 +
32470 + /* Make sure that the recovery flag is clear */
32471 + c->mst_node->flags &= cpu_to_le32(~UBIFS_MST_RCVRY);
32472 +
32473 + c->max_sqnum = le64_to_cpu(c->mst_node->ch.sqnum);
32474 + c->highest_inum = le64_to_cpu(c->mst_node->highest_inum);
32475 + c->cmt_no = le64_to_cpu(c->mst_node->cmt_no);
32476 + c->zroot.lnum = le32_to_cpu(c->mst_node->root_lnum);
32477 + c->zroot.offs = le32_to_cpu(c->mst_node->root_offs);
32478 + c->zroot.len = le32_to_cpu(c->mst_node->root_len);
32479 + c->lhead_lnum = le32_to_cpu(c->mst_node->log_lnum);
32480 + c->gc_lnum = le32_to_cpu(c->mst_node->gc_lnum);
32481 + c->ihead_lnum = le32_to_cpu(c->mst_node->ihead_lnum);
32482 + c->ihead_offs = le32_to_cpu(c->mst_node->ihead_offs);
32483 + c->old_idx_sz = le64_to_cpu(c->mst_node->index_size);
32484 + c->lpt_lnum = le32_to_cpu(c->mst_node->lpt_lnum);
32485 + c->lpt_offs = le32_to_cpu(c->mst_node->lpt_offs);
32486 + c->nhead_lnum = le32_to_cpu(c->mst_node->nhead_lnum);
32487 + c->nhead_offs = le32_to_cpu(c->mst_node->nhead_offs);
32488 + c->ltab_lnum = le32_to_cpu(c->mst_node->ltab_lnum);
32489 + c->ltab_offs = le32_to_cpu(c->mst_node->ltab_offs);
32490 + c->lsave_lnum = le32_to_cpu(c->mst_node->lsave_lnum);
32491 + c->lsave_offs = le32_to_cpu(c->mst_node->lsave_offs);
32492 + c->lscan_lnum = le32_to_cpu(c->mst_node->lscan_lnum);
32493 + c->lst.empty_lebs = le32_to_cpu(c->mst_node->empty_lebs);
32494 + c->lst.idx_lebs = le32_to_cpu(c->mst_node->idx_lebs);
32495 + old_leb_cnt = le32_to_cpu(c->mst_node->leb_cnt);
32496 + c->lst.total_free = le64_to_cpu(c->mst_node->total_free);
32497 + c->lst.total_dirty = le64_to_cpu(c->mst_node->total_dirty);
32498 + c->lst.total_used = le64_to_cpu(c->mst_node->total_used);
32499 + c->lst.total_dead = le64_to_cpu(c->mst_node->total_dead);
32500 + c->lst.total_dark = le64_to_cpu(c->mst_node->total_dark);
32501 +
32502 + c->calc_idx_sz = c->old_idx_sz;
32503 +
32504 + if (c->mst_node->flags & cpu_to_le32(UBIFS_MST_NO_ORPHS))
32505 + c->no_orphs = 1;
32506 +
32507 + if (old_leb_cnt != c->leb_cnt) {
32508 + /* The file system has been resized */
32509 + int growth = c->leb_cnt - old_leb_cnt;
32510 +
32511 + if (c->leb_cnt < old_leb_cnt ||
32512 + c->leb_cnt < UBIFS_MIN_LEB_CNT) {
32513 + ubifs_err("bad leb_cnt on master node");
32514 + dbg_dump_node(c, c->mst_node);
32515 + return -EINVAL;
32516 + }
32517 +
32518 + dbg_mnt("Auto resizing (master) from %d LEBs to %d LEBs",
32519 + old_leb_cnt, c->leb_cnt);
32520 + c->lst.empty_lebs += growth;
32521 + c->lst.total_free += growth * (long long)c->leb_size;
32522 + c->lst.total_dark += growth * (long long)c->dark_wm;
32523 +
32524 + /*
32525 + * Reflect changes back onto the master node. N.B. the master
32526 + * node gets written immediately whenever mounting (or
32527 + * remounting) in read-write mode, so we do not need to write it
32528 + * here.
32529 + */
32530 + c->mst_node->leb_cnt = cpu_to_le32(c->leb_cnt);
32531 + c->mst_node->empty_lebs = cpu_to_le32(c->lst.empty_lebs);
32532 + c->mst_node->total_free = cpu_to_le64(c->lst.total_free);
32533 + c->mst_node->total_dark = cpu_to_le64(c->lst.total_dark);
32534 + }
32535 +
32536 + err = validate_master(c);
32537 + if (err)
32538 + return err;
32539 +
32540 + err = dbg_old_index_check_init(c, &c->zroot);
32541 +
32542 + return err;
32543 +}
32544 +
32545 +/**
32546 + * ubifs_write_master - write master node.
32547 + * @c: UBIFS file-system description object
32548 + *
32549 + * This function writes the master node. The caller has to take the
32550 + * @c->mst_mutex lock before calling this function. Returns zero in case of
32551 + * success and a negative error code in case of failure. The master node is
32552 + * written twice to enable recovery.
32553 + */
32554 +int ubifs_write_master(struct ubifs_info *c)
32555 +{
32556 + int err, lnum, offs, len;
32557 +
32558 + if (c->ro_media)
32559 + return -EINVAL;
32560 +
32561 + lnum = UBIFS_MST_LNUM;
32562 + offs = c->mst_offs + c->mst_node_alsz;
32563 + len = UBIFS_MST_NODE_SZ;
32564 +
32565 + if (offs + UBIFS_MST_NODE_SZ > c->leb_size) {
32566 + err = ubifs_leb_unmap(c, lnum);
32567 + if (err)
32568 + return err;
32569 + offs = 0;
32570 + }
32571 +
32572 + c->mst_offs = offs;
32573 + c->mst_node->highest_inum = cpu_to_le64(c->highest_inum);
32574 +
32575 + err = ubifs_write_node(c, c->mst_node, len, lnum, offs, UBI_SHORTTERM);
32576 + if (err)
32577 + return err;
32578 +
32579 + lnum += 1;
32580 +
32581 + if (offs == 0) {
32582 + err = ubifs_leb_unmap(c, lnum);
32583 + if (err)
32584 + return err;
32585 + }
32586 + err = ubifs_write_node(c, c->mst_node, len, lnum, offs, UBI_SHORTTERM);
32587 +
32588 + return err;
32589 +}
32590 diff --exclude=.git -urN linux-2.6.25.6/fs/ubifs/misc.h avr32-2.6/fs/ubifs/misc.h
32591 --- linux-2.6.25.6/fs/ubifs/misc.h 1970-01-01 01:00:00.000000000 +0100
32592 +++ avr32-2.6/fs/ubifs/misc.h 2008-06-12 15:09:45.475816115 +0200
32593 @@ -0,0 +1,311 @@
32594 +/*
32595 + * This file is part of UBIFS.
32596 + *
32597 + * Copyright (C) 2006-2008 Nokia Corporation
32598 + *
32599 + * This program is free software; you can redistribute it and/or modify it
32600 + * under the terms of the GNU General Public License version 2 as published by
32601 + * the Free Software Foundation.
32602 + *
32603 + * This program is distributed in the hope that it will be useful, but WITHOUT
32604 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
32605 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
32606 + * more details.
32607 + *
32608 + * You should have received a copy of the GNU General Public License along with
32609 + * this program; if not, write to the Free Software Foundation, Inc., 51
32610 + * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
32611 + *
32612 + * Authors: Artem Bityutskiy (Битюцкий Артём)
32613 + * Adrian Hunter
32614 + */
32615 +
32616 +/*
32617 + * This file contains miscellaneous helper functions.
32618 + */
32619 +
32620 +#ifndef __UBIFS_MISC_H__
32621 +#define __UBIFS_MISC_H__
32622 +
32623 +/**
32624 + * ubifs_zn_dirty - check if znode is dirty.
32625 + * @znode: znode to check
32626 + *
32627 + * This helper function returns %1 if @znode is dirty and %0 otherwise.
32628 + */
32629 +static inline int ubifs_zn_dirty(const struct ubifs_znode *znode)
32630 +{
32631 + return !!test_bit(DIRTY_ZNODE, &znode->flags);
32632 +}
32633 +
32634 +/**
32635 + * ubifs_wake_up_bgt - wake up background thread.
32636 + * @c: UBIFS file-system description object
32637 + */
32638 +static inline void ubifs_wake_up_bgt(struct ubifs_info *c)
32639 +{
32640 + if (c->bgt && !c->need_bgt) {
32641 + c->need_bgt = 1;
32642 + wake_up_process(c->bgt);
32643 + }
32644 +}
32645 +
32646 +/**
32647 + * ubifs_tnc_find_child - find next child in znode.
32648 + * @znode: znode to search at
32649 + * @start: the zbranch index to start at
32650 + *
32651 + * This helper function looks for znode child starting at index @start. Returns
32652 + * the child or %NULL if no children were found.
32653 + */
32654 +static inline struct ubifs_znode *
32655 +ubifs_tnc_find_child(struct ubifs_znode *znode, int start)
32656 +{
32657 + while (start < znode->child_cnt) {
32658 + if (znode->zbranch[start].znode)
32659 + return znode->zbranch[start].znode;
32660 + start += 1;
32661 + }
32662 +
32663 + return NULL;
32664 +}
32665 +
32666 +/**
32667 + * ubifs_inode - get UBIFS inode information by VFS 'struct inode' object.
32668 + * @inode: the VFS 'struct inode' pointer
32669 + */
32670 +static inline struct ubifs_inode *ubifs_inode(const struct inode *inode)
32671 +{
32672 + return container_of(inode, struct ubifs_inode, vfs_inode);
32673 +}
32674 +
32675 +/**
32676 + * ubifs_ro_mode - switch UBIFS to read read-only mode.
32677 + * @c: UBIFS file-system description object
32678 + * @err: error code which is the reason of switching to R/O mode
32679 + */
32680 +static inline void ubifs_ro_mode(struct ubifs_info *c, int err)
32681 +{
32682 + if (!c->ro_media) {
32683 + c->ro_media = 1;
32684 + ubifs_warn("switched to read-only mode, error %d", err);
32685 + dbg_dump_stack();
32686 + }
32687 +}
32688 +
32689 +/**
32690 + * ubifs_compr_present - check if compressor was compiled in.
32691 + * @compr_type: compressor type to check
32692 + *
32693 + * This function returns %1 of compressor of type @compr_type is present, and
32694 + * %0 if not.
32695 + */
32696 +static inline int ubifs_compr_present(int compr_type)
32697 +{
32698 + ubifs_assert(compr_type >= 0 && compr_type < UBIFS_COMPR_TYPES_CNT);
32699 + return !!ubifs_compressors[compr_type]->capi_name;
32700 +}
32701 +
32702 +/**
32703 + * ubifs_compr_name - get compressor name string by its type.
32704 + * @compr_type: compressor type
32705 + *
32706 + * This function returns compressor type string.
32707 + */
32708 +static inline const char *ubifs_compr_name(int compr_type)
32709 +{
32710 + ubifs_assert(compr_type >= 0 && compr_type < UBIFS_COMPR_TYPES_CNT);
32711 + return ubifs_compressors[compr_type]->name;
32712 +}
32713 +
32714 +/**
32715 + * ubifs_wbuf_sync - synchronize write-buffer.
32716 + * @wbuf: write-buffer to synchronize
32717 + *
32718 + * This is the same as as 'ubifs_wbuf_sync_nolock()' but it does not assume
32719 + * that the write-buffer is already locked.
32720 + */
32721 +static inline int ubifs_wbuf_sync(struct ubifs_wbuf *wbuf)
32722 +{
32723 + int err;
32724 +
32725 + mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead);
32726 + err = ubifs_wbuf_sync_nolock(wbuf);
32727 + mutex_unlock(&wbuf->io_mutex);
32728 + return err;
32729 +}
32730 +
32731 +/**
32732 + * ubifs_leb_unmap - unmap an LEB.
32733 + * @c: UBIFS file-system description object
32734 + * @lnum: LEB number to unmap
32735 + *
32736 + * This function returns %0 on success and a negative error code on failure.
32737 + */
32738 +static inline int ubifs_leb_unmap(const struct ubifs_info *c, int lnum)
32739 +{
32740 + int err;
32741 +
32742 + err = ubi_leb_unmap(c->ubi, lnum);
32743 + if (err) {
32744 + ubifs_err("unmap LEB %d failed, error %d", lnum, err);
32745 + return err;
32746 + }
32747 +
32748 + return 0;
32749 +}
32750 +
32751 +/**
32752 + * ubifs_leb_write - write to a LEB.
32753 + * @c: UBIFS file-system description object
32754 + * @lnum: LEB number to write
32755 + * @buf: buffer to write from
32756 + * @offs: offset within LEB to write to
32757 + * @len: length to write
32758 + * @dtype: data type
32759 + *
32760 + * This function returns %0 on success and a negative error code on failure.
32761 + */
32762 +static inline int ubifs_leb_write(const struct ubifs_info *c, int lnum,
32763 + const void *buf, int offs, int len, int dtype)
32764 +{
32765 + int err;
32766 +
32767 + err = ubi_leb_write(c->ubi, lnum, buf, offs, len, dtype);
32768 + if (err) {
32769 + ubifs_err("writing %d bytes at %d:%d, error %d",
32770 + len, lnum, offs, err);
32771 + return err;
32772 + }
32773 +
32774 + return 0;
32775 +}
32776 +
32777 +/**
32778 + * ubifs_encode_dev - encode device node IDs.
32779 + * @dev: UBIFS device node information
32780 + * @rdev: device IDs to encode
32781 + *
32782 + * This is a helper function which encodes major/minor numbers of a device node
32783 + * into UBIFS device node description. We use standard Linux "new" and "huge"
32784 + * encodings.
32785 + */
32786 +static inline int ubifs_encode_dev(union ubifs_dev_desc *dev, dev_t rdev)
32787 +{
32788 + if (new_valid_dev(rdev)) {
32789 + dev->new = cpu_to_le32(new_encode_dev(rdev));
32790 + return sizeof(dev->new);
32791 + } else {
32792 + dev->huge = cpu_to_le64(huge_encode_dev(rdev));
32793 + return sizeof(dev->huge);
32794 + }
32795 +}
32796 +
32797 +/**
32798 + * ubifs_add_dirt - add dirty space to LEB properties.
32799 + * @c: the UBIFS file-system description object
32800 + * @lnum: LEB to add dirty space for
32801 + * @dirty: dirty space to add
32802 + *
32803 + * This is a helper function which increased amount of dirty LEB space. Returns
32804 + * zero in case of success and a negative error code in case of failure.
32805 + */
32806 +static inline int ubifs_add_dirt(struct ubifs_info *c, int lnum, int dirty)
32807 +{
32808 + return ubifs_update_one_lp(c, lnum, LPROPS_NC, dirty, 0, 0);
32809 +}
32810 +
32811 +/**
32812 + * ubifs_return_leb - return LEB to lprops.
32813 + * @c: the UBIFS file-system description object
32814 + * @lnum: LEB to return
32815 + *
32816 + * This helper function cleans the "taken" flag of a logical eraseblock in the
32817 + * lprops. Returns zero in case of success and a negative error code in case of
32818 + * failure.
32819 + */
32820 +static inline int ubifs_return_leb(struct ubifs_info *c, int lnum)
32821 +{
32822 + return ubifs_change_one_lp(c, lnum, LPROPS_NC, LPROPS_NC, 0,
32823 + LPROPS_TAKEN, 0);
32824 +}
32825 +
32826 +/**
32827 + * ubifs_idx_node_sz - return index node size.
32828 + * @c: the UBIFS file-system description object
32829 + * @child_cnt: number of children of this index node
32830 + */
32831 +static inline int ubifs_idx_node_sz(const struct ubifs_info *c, int child_cnt)
32832 +{
32833 + return UBIFS_IDX_NODE_SZ + (UBIFS_BRANCH_SZ + c->key_len) * child_cnt;
32834 +}
32835 +
32836 +/**
32837 + * ubifs_idx_branch - return pointer to an index branch.
32838 + * @c: the UBIFS file-system description object
32839 + * @idx: index node
32840 + * @bnum: branch number
32841 + */
32842 +static inline
32843 +struct ubifs_branch *ubifs_idx_branch(const struct ubifs_info *c,
32844 + const struct ubifs_idx_node *idx,
32845 + int bnum)
32846 +{
32847 + return (struct ubifs_branch *)((void *)idx->branches +
32848 + (UBIFS_BRANCH_SZ + c->key_len) * bnum);
32849 +}
32850 +
32851 +/**
32852 + * ubifs_idx_key - return pointer to an index key.
32853 + * @c: the UBIFS file-system description object
32854 + * @idx: index node
32855 + */
32856 +static inline void *ubifs_idx_key(const struct ubifs_info *c,
32857 + const struct ubifs_idx_node *idx)
32858 +{
32859 + return (void *)((struct ubifs_branch *)idx->branches)->key;
32860 +}
32861 +
32862 +/**
32863 + * ubifs_reported_space - calculate reported free space.
32864 + * @c: the UBIFS file-system description object
32865 + * @free: amount of free space
32866 + *
32867 + * This function calculates amount of free space which will be reported to
32868 + * user-space. User-space application tend to expect that if the file-system
32869 + * (e.g., via the 'statfs()' call) reports that it has N bytes available, they
32870 + * are able to write a file of size N. UBIFS attaches node headers to each data
32871 + * node and it has to write indexind nodes as well. This introduces additional
32872 + * overhead, and UBIFS it has to report sligtly less free space to meet the
32873 + * above expectetion.
32874 + *
32875 + * This function assumes free space is made up of uncompressed data nodes and
32876 + * full index nodes (one per data node, doubled because we always allow enough
32877 + * space to write the index twice).
32878 + *
32879 + * Note, the calculation is pessimistic, which means that most of the time
32880 + * UBIFS reports less space than it actually has.
32881 + */
32882 +static inline long long ubifs_reported_space(const struct ubifs_info *c,
32883 + uint64_t free)
32884 +{
32885 + int divisor, factor;
32886 +
32887 + divisor = UBIFS_MAX_DATA_NODE_SZ + (c->max_idx_node_sz << 1);
32888 + factor = UBIFS_MAX_DATA_NODE_SZ - UBIFS_DATA_NODE_SZ;
32889 + do_div(free, divisor);
32890 +
32891 + return free * factor;
32892 +}
32893 +
32894 +/**
32895 + * ubifs_current_time - round current time to time granularity.
32896 + * @inode: inode
32897 + */
32898 +static inline struct timespec ubifs_current_time(struct inode *inode)
32899 +{
32900 + return (inode->i_sb->s_time_gran < NSEC_PER_SEC) ?
32901 + current_fs_time(inode->i_sb) : CURRENT_TIME_SEC;
32902 +}
32903 +
32904 +#endif /* __UBIFS_MISC_H__ */
32905 diff --exclude=.git -urN linux-2.6.25.6/fs/ubifs/orphan.c avr32-2.6/fs/ubifs/orphan.c
32906 --- linux-2.6.25.6/fs/ubifs/orphan.c 1970-01-01 01:00:00.000000000 +0100
32907 +++ avr32-2.6/fs/ubifs/orphan.c 2008-06-12 15:09:45.475816115 +0200
32908 @@ -0,0 +1,958 @@
32909 +/*
32910 + * This file is part of UBIFS.
32911 + *
32912 + * Copyright (C) 2006-2008 Nokia Corporation.
32913 + *
32914 + * This program is free software; you can redistribute it and/or modify it
32915 + * under the terms of the GNU General Public License version 2 as published by
32916 + * the Free Software Foundation.
32917 + *
32918 + * This program is distributed in the hope that it will be useful, but WITHOUT
32919 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
32920 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
32921 + * more details.
32922 + *
32923 + * You should have received a copy of the GNU General Public License along with
32924 + * this program; if not, write to the Free Software Foundation, Inc., 51
32925 + * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
32926 + *
32927 + * Author: Adrian Hunter
32928 + */
32929 +
32930 +#include "ubifs.h"
32931 +
32932 +/*
32933 + * An orphan is an inode number whose inode node has been committed to the index
32934 + * with a link count of zero. That happens when an open file is deleted
32935 + * (unlinked) and then a commit is run. In the normal course of events the inode
32936 + * would be deleted when the file is closed. However in the case of an unclean
32937 + * unmount, orphans need to be accounted for. After an unclean unmount, the
32938 + * orphans' inodes must be deleted which means either scanning the entire index
32939 + * looking for them, or keeping a list on flash somewhere. This unit implements
32940 + * the latter approach.
32941 + *
32942 + * The orphan area is a fixed number of LEBs situated between the LPT area and
32943 + * the main area. The number of orphan area LEBs is specified when the file
32944 + * system is created. The minimum number is 1. The size of the orphan area
32945 + * should be so that it can hold the maximum number of orphans that are expected
32946 + * to ever exist at one time.
32947 + *
32948 + * The number of orphans that can fit in a LEB is:
32949 + *
32950 + * (c->leb_size - UBIFS_ORPH_NODE_SZ) / sizeof(__le64)
32951 + *
32952 + * For example: a 15872 byte LEB can fit 1980 orphans so 1 LEB may be enough.
32953 + *
32954 + * Orphans are accumulated in a rb-tree. When an inode's link count drops to
32955 + * zero, the inode number is added to the rb-tree. It is removed from the tree
32956 + * when the inode is deleted. Any new orphans that are in the orphan tree when
32957 + * the commit is run, are written to the orphan area in 1 or more orph nodes.
32958 + * If the orphan area is full, it is consolidated to make space. There is
32959 + * always enough space because validation prevents the user from creating more
32960 + * than the maximum number of orphans allowed.
32961 + */
32962 +
32963 +#ifdef CONFIG_UBIFS_FS_DEBUG
32964 +static int dbg_check_orphans(struct ubifs_info *c);
32965 +#else
32966 +#define dbg_check_orphans(c) 0
32967 +#endif
32968 +
32969 +/**
32970 + * ubifs_add_orphan - add an orphan.
32971 + * @c: UBIFS file-system description object
32972 + * @inum: orphan inode number
32973 + *
32974 + * Add an orphan. This function is called when an inodes link count drops to
32975 + * zero.
32976 + */
32977 +int ubifs_add_orphan(struct ubifs_info *c, ino_t inum)
32978 +{
32979 + struct ubifs_orphan *orphan, *o;
32980 + struct rb_node **p, *parent = NULL;
32981 +
32982 + orphan = kzalloc(sizeof(struct ubifs_orphan), GFP_NOFS);
32983 + if (!orphan)
32984 + return -ENOMEM;
32985 + orphan->inum = inum;
32986 + orphan->new = 1;
32987 +
32988 + spin_lock(&c->orphan_lock);
32989 + if (c->tot_orphans >= c->max_orphans) {
32990 + spin_unlock(&c->orphan_lock);
32991 + kfree(orphan);
32992 + return -ENFILE;
32993 + }
32994 + p = &c->orph_tree.rb_node;
32995 + while (*p) {
32996 + parent = *p;
32997 + o = rb_entry(parent, struct ubifs_orphan, rb);
32998 + if (inum < o->inum)
32999 + p = &(*p)->rb_left;
33000 + else if (inum > o->inum)
33001 + p = &(*p)->rb_right;
33002 + else {
33003 + dbg_err("orphaned twice");
33004 + spin_unlock(&c->orphan_lock);
33005 + kfree(orphan);
33006 + return 0;
33007 + }
33008 + }
33009 + c->tot_orphans += 1;
33010 + c->new_orphans += 1;
33011 + rb_link_node(&orphan->rb, parent, p);
33012 + rb_insert_color(&orphan->rb, &c->orph_tree);
33013 + list_add_tail(&orphan->list, &c->orph_list);
33014 + list_add_tail(&orphan->new_list, &c->orph_new);
33015 + spin_unlock(&c->orphan_lock);
33016 + dbg_gen("ino %lu", inum);
33017 + return 0;
33018 +}
33019 +
33020 +/**
33021 + * ubifs_delete_orphan - delete an orphan.
33022 + * @c: UBIFS file-system description object
33023 + * @inum: orphan inode number
33024 + *
33025 + * Delete an orphan. This function is called when an inode is deleted.
33026 + */
33027 +void ubifs_delete_orphan(struct ubifs_info *c, ino_t inum)
33028 +{
33029 + struct ubifs_orphan *o;
33030 + struct rb_node *p;
33031 +
33032 + spin_lock(&c->orphan_lock);
33033 + p = c->orph_tree.rb_node;
33034 + while (p) {
33035 + o = rb_entry(p, struct ubifs_orphan, rb);
33036 + if (inum < o->inum)
33037 + p = p->rb_left;
33038 + else if (inum > o->inum)
33039 + p = p->rb_right;
33040 + else {
33041 + if (o->dnext) {
33042 + spin_unlock(&c->orphan_lock);
33043 + dbg_gen("deleted twice ino %lu", inum);
33044 + return;
33045 + }
33046 + if (o->cnext) {
33047 + o->dnext = c->orph_dnext;
33048 + c->orph_dnext = o;
33049 + spin_unlock(&c->orphan_lock);
33050 + dbg_gen("delete later ino %lu", inum);
33051 + return;
33052 + }
33053 + rb_erase(p, &c->orph_tree);
33054 + list_del(&o->list);
33055 + c->tot_orphans -= 1;
33056 + if (o->new) {
33057 + list_del(&o->new_list);
33058 + c->new_orphans -= 1;
33059 + }
33060 + spin_unlock(&c->orphan_lock);
33061 + kfree(o);
33062 + dbg_gen("inum %lu", inum);
33063 + return;
33064 + }
33065 + }
33066 + spin_unlock(&c->orphan_lock);
33067 + dbg_err("missing orphan ino %lu", inum);
33068 + dbg_dump_stack();
33069 +}
33070 +
33071 +/**
33072 + * ubifs_orphan_start_commit - start commit of orphans.
33073 + * @c: UBIFS file-system description object
33074 + *
33075 + * Start commit of orphans.
33076 + */
33077 +int ubifs_orphan_start_commit(struct ubifs_info *c)
33078 +{
33079 + struct ubifs_orphan *orphan, **last;
33080 +
33081 + spin_lock(&c->orphan_lock);
33082 + last = &c->orph_cnext;
33083 + list_for_each_entry(orphan, &c->orph_new, new_list) {
33084 + ubifs_assert(orphan->new);
33085 + orphan->new = 0;
33086 + *last = orphan;
33087 + last = &orphan->cnext;
33088 + }
33089 + *last = orphan->cnext;
33090 + c->cmt_orphans = c->new_orphans;
33091 + c->new_orphans = 0;
33092 + dbg_cmt("%d orphans to commit", c->cmt_orphans);
33093 + INIT_LIST_HEAD(&c->orph_new);
33094 + if (c->tot_orphans == 0)
33095 + c->no_orphs = 1;
33096 + else
33097 + c->no_orphs = 0;
33098 + spin_unlock(&c->orphan_lock);
33099 + return 0;
33100 +}
33101 +
33102 +/**
33103 + * avail_orphs - calculate available space.
33104 + * @c: UBIFS file-system description object
33105 + *
33106 + * This function returns the number of orphans that can be written in the
33107 + * available space.
33108 + */
33109 +static int avail_orphs(struct ubifs_info *c)
33110 +{
33111 + int avail_lebs, avail, gap;
33112 +
33113 + avail_lebs = c->orph_lebs - (c->ohead_lnum - c->orph_first) - 1;
33114 + avail = avail_lebs *
33115 + ((c->leb_size - UBIFS_ORPH_NODE_SZ) / sizeof(__le64));
33116 + gap = c->leb_size - c->ohead_offs;
33117 + if (gap >= UBIFS_ORPH_NODE_SZ + sizeof(__le64))
33118 + avail += (gap - UBIFS_ORPH_NODE_SZ) / sizeof(__le64);
33119 + return avail;
33120 +}
33121 +
33122 +/**
33123 + * tot_avail_orphs - calculate total space.
33124 + * @c: UBIFS file-system description object
33125 + *
33126 + * This function returns the number of orphans that can be written in half
33127 + * the total space. That leaves half the space for adding new orphans.
33128 + */
33129 +static int tot_avail_orphs(struct ubifs_info *c)
33130 +{
33131 + int avail_lebs, avail;
33132 +
33133 + avail_lebs = c->orph_lebs;
33134 + avail = avail_lebs *
33135 + ((c->leb_size - UBIFS_ORPH_NODE_SZ) / sizeof(__le64));
33136 + return avail / 2;
33137 +}
33138 +
33139 +/**
33140 + * do_write_orph_node - write a node
33141 + * @c: UBIFS file-system description object
33142 + * @len: length of node
33143 + * @atomic: write atomically
33144 + *
33145 + * This function writes a node to the orphan head from the orphan buffer. If
33146 + * %atomic is not zero, then the write is done atomically. On success, %0 is
33147 + * returned, otherwise a negative error code is returned.
33148 + */
33149 +static int do_write_orph_node(struct ubifs_info *c, int len, int atomic)
33150 +{
33151 + int err = 0;
33152 +
33153 + if (atomic) {
33154 + ubifs_assert(c->ohead_offs == 0);
33155 + ubifs_prepare_node(c, c->orph_buf, len, 1);
33156 + len = ALIGN(len, c->min_io_size);
33157 + err = ubi_leb_change(c->ubi, c->ohead_lnum, c->orph_buf, len,
33158 + UBI_SHORTTERM);
33159 + } else {
33160 + if (c->ohead_offs == 0) {
33161 + /* Ensure LEB has been unmapped */
33162 + err = ubifs_leb_unmap(c, c->ohead_lnum);
33163 + if (err)
33164 + return err;
33165 + }
33166 + err = ubifs_write_node(c, c->orph_buf, len, c->ohead_lnum,
33167 + c->ohead_offs, UBI_SHORTTERM);
33168 + }
33169 + return err;
33170 +}
33171 +
33172 +/**
33173 + * write_orph_node - write an orph node
33174 + * @c: UBIFS file-system description object
33175 + * @atomic: write atomically
33176 + *
33177 + * This function builds an orph node from the cnext list and writes it to the
33178 + * orphan head. On success, %0 is returned, otherwise a negative error code
33179 + * is returned.
33180 + */
33181 +static int write_orph_node(struct ubifs_info *c, int atomic)
33182 +{
33183 + struct ubifs_orphan *orphan, *cnext;
33184 + struct ubifs_orph_node *orph;
33185 + int gap, err, len, cnt, i;
33186 +
33187 + ubifs_assert(c->cmt_orphans > 0);
33188 + gap = c->leb_size - c->ohead_offs;
33189 + if (gap < UBIFS_ORPH_NODE_SZ + sizeof(__le64)) {
33190 + c->ohead_lnum += 1;
33191 + c->ohead_offs = 0;
33192 + gap = c->leb_size;
33193 + if (c->ohead_lnum > c->orph_last) {
33194 + /*
33195 + * We limit the number of orphans so that this should
33196 + * never happen.
33197 + */
33198 + ubifs_err("out of space in orphan area");
33199 + return -EINVAL;
33200 + }
33201 + }
33202 + cnt = (gap - UBIFS_ORPH_NODE_SZ) / sizeof(__le64);
33203 + if (cnt > c->cmt_orphans)
33204 + cnt = c->cmt_orphans;
33205 + len = UBIFS_ORPH_NODE_SZ + cnt * sizeof(__le64);
33206 + ubifs_assert(c->orph_buf);
33207 + orph = c->orph_buf;
33208 + orph->ch.node_type = UBIFS_ORPH_NODE;
33209 + spin_lock(&c->orphan_lock);
33210 + cnext = c->orph_cnext;
33211 + for (i = 0; i < cnt; i++) {
33212 + orphan = cnext;
33213 + orph->inos[i] = cpu_to_le64(orphan->inum);
33214 + cnext = orphan->cnext;
33215 + orphan->cnext = NULL;
33216 + }
33217 + c->orph_cnext = cnext;
33218 + c->cmt_orphans -= cnt;
33219 + spin_unlock(&c->orphan_lock);
33220 + if (c->cmt_orphans)
33221 + orph->cmt_no = cpu_to_le64(c->cmt_no + 1);
33222 + else
33223 + /* Mark the last node of the commit */
33224 + orph->cmt_no = cpu_to_le64((c->cmt_no + 1) | (1ULL << 63));
33225 + ubifs_assert(c->ohead_offs + len <= c->leb_size);
33226 + ubifs_assert(c->ohead_lnum >= c->orph_first);
33227 + ubifs_assert(c->ohead_lnum <= c->orph_last);
33228 + err = do_write_orph_node(c, len, atomic);
33229 + c->ohead_offs += ALIGN(len, c->min_io_size);
33230 + c->ohead_offs = ALIGN(c->ohead_offs, 8);
33231 + return err;
33232 +}
33233 +
33234 +/**
33235 + * write_orph_nodes - write orph nodes until there are no more to commit
33236 + * @c: UBIFS file-system description object
33237 + * @atomic: write atomically
33238 + *
33239 + * This function writes orph nodes for all the orphans to commit. On success,
33240 + * %0 is returned, otherwise a negative error code is returned.
33241 + */
33242 +static int write_orph_nodes(struct ubifs_info *c, int atomic)
33243 +{
33244 + int err;
33245 +
33246 + while (c->cmt_orphans > 0) {
33247 + err = write_orph_node(c, atomic);
33248 + if (err)
33249 + return err;
33250 + }
33251 + if (atomic) {
33252 + int lnum;
33253 +
33254 + /* Unmap any unused LEBs after consolidation */
33255 + lnum = c->ohead_lnum + 1;
33256 + for (lnum = c->ohead_lnum + 1; lnum <= c->orph_last; lnum++) {
33257 + err = ubifs_leb_unmap(c, lnum);
33258 + if (err)
33259 + return err;
33260 + }
33261 + }
33262 + return 0;
33263 +}
33264 +
33265 +/**
33266 + * consolidate - consolidate the orphan area.
33267 + * @c: UBIFS file-system description object
33268 + *
33269 + * This function enables consolidation by putting all the orphans into the list
33270 + * to commit. The list is in the order that the orphans were added, and the
33271 + * LEBs are written atomically in order, so at no time can orphans be lost by
33272 + * an unclean unmount.
33273 + *
33274 + * This function returns %0 on success and a negative error code on failure.
33275 + */
33276 +static int consolidate(struct ubifs_info *c)
33277 +{
33278 + int tot_avail = tot_avail_orphs(c), err = 0;
33279 +
33280 + spin_lock(&c->orphan_lock);
33281 + dbg_cmt("there is space for %d orphans and there are %d",
33282 + tot_avail, c->tot_orphans);
33283 + if (c->tot_orphans - c->new_orphans <= tot_avail) {
33284 + struct ubifs_orphan *orphan, **last;
33285 + int cnt = 0;
33286 +
33287 + /* Change the cnext list to include all non-new orphans */
33288 + last = &c->orph_cnext;
33289 + list_for_each_entry(orphan, &c->orph_list, list) {
33290 + if (orphan->new)
33291 + continue;
33292 + *last = orphan;
33293 + last = &orphan->cnext;
33294 + cnt += 1;
33295 + }
33296 + *last = orphan->cnext;
33297 + ubifs_assert(cnt == c->tot_orphans - c->new_orphans);
33298 + c->cmt_orphans = cnt;
33299 + c->ohead_lnum = c->orph_first;
33300 + c->ohead_offs = 0;
33301 + } else {
33302 + /*
33303 + * We limit the number of orphans so that this should
33304 + * never happen.
33305 + */
33306 + ubifs_err("out of space in orphan area");
33307 + err = -EINVAL;
33308 + }
33309 + spin_unlock(&c->orphan_lock);
33310 + return err;
33311 +}
33312 +
33313 +/**
33314 + * commit_orphans - commit orphans.
33315 + * @c: UBIFS file-system description object
33316 + *
33317 + * This function commits orphans to flash. On success, %0 is returned,
33318 + * otherwise a negative error code is returned.
33319 + */
33320 +static int commit_orphans(struct ubifs_info *c)
33321 +{
33322 + int avail, atomic = 0, err;
33323 +
33324 + ubifs_assert(c->cmt_orphans > 0);
33325 + avail = avail_orphs(c);
33326 + if (avail < c->cmt_orphans) {
33327 + /* Not enough space to write new orphans, so consolidate */
33328 + err = consolidate(c);
33329 + if (err)
33330 + return err;
33331 + atomic = 1;
33332 + }
33333 + err = write_orph_nodes(c, atomic);
33334 + return err;
33335 +}
33336 +
33337 +/**
33338 + * erase_deleted - erase the orphans marked for deletion.
33339 + * @c: UBIFS file-system description object
33340 + *
33341 + * During commit, the orphans being committed cannot be deleted, so they are
33342 + * marked for deletion and deleted by this function. Also, the recovery
33343 + * adds killed orphans to the deletion list, and therefore they are deleted
33344 + * here too.
33345 + */
33346 +static void erase_deleted(struct ubifs_info *c)
33347 +{
33348 + struct ubifs_orphan *orphan, *dnext;
33349 +
33350 + spin_lock(&c->orphan_lock);
33351 + dnext = c->orph_dnext;
33352 + while (dnext) {
33353 + orphan = dnext;
33354 + dnext = orphan->dnext;
33355 + ubifs_assert(!orphan->new);
33356 + rb_erase(&orphan->rb, &c->orph_tree);
33357 + list_del(&orphan->list);
33358 + c->tot_orphans -= 1;
33359 + dbg_gen("deleting orphan ino %lu", orphan->inum);
33360 + kfree(orphan);
33361 + }
33362 + c->orph_dnext = NULL;
33363 + spin_unlock(&c->orphan_lock);
33364 +}
33365 +
33366 +/**
33367 + * ubifs_orphan_end_commit - end commit of orphans.
33368 + * @c: UBIFS file-system description object
33369 + *
33370 + * End commit of orphans.
33371 + */
33372 +int ubifs_orphan_end_commit(struct ubifs_info *c)
33373 +{
33374 + int err;
33375 +
33376 + if (c->cmt_orphans != 0) {
33377 + err = commit_orphans(c);
33378 + if (err)
33379 + return err;
33380 + }
33381 + erase_deleted(c);
33382 + err = dbg_check_orphans(c);
33383 + return err;
33384 +}
33385 +
33386 +/**
33387 + * clear_orphans - erase all LEBs used for orphans.
33388 + * @c: UBIFS file-system description object
33389 + *
33390 + * If recovery is not required, then the orphans from the previous session
33391 + * are not needed. This function locates the LEBs used to record
33392 + * orphans, and un-maps them.
33393 + */
33394 +static int clear_orphans(struct ubifs_info *c)
33395 +{
33396 + int lnum, err;
33397 +
33398 + for (lnum = c->orph_first; lnum <= c->orph_last; lnum++) {
33399 + err = ubifs_leb_unmap(c, lnum);
33400 + if (err)
33401 + return err;
33402 + }
33403 + c->ohead_lnum = c->orph_first;
33404 + c->ohead_offs = 0;
33405 + return 0;
33406 +}
33407 +
33408 +/**
33409 + * insert_dead_orphan - insert an orphan.
33410 + * @c: UBIFS file-system description object
33411 + * @inum: orphan inode number
33412 + *
33413 + * This function is a helper to the 'do_kill_orphans()' function. The orphan
33414 + * must be kept until the next commit, so it is added to the rb-tree and the
33415 + * deletion list.
33416 + */
33417 +static int insert_dead_orphan(struct ubifs_info *c, ino_t inum)
33418 +{
33419 + struct ubifs_orphan *orphan, *o;
33420 + struct rb_node **p, *parent = NULL;
33421 +
33422 + orphan = kzalloc(sizeof(struct ubifs_orphan), GFP_KERNEL);
33423 + if (!orphan)
33424 + return -ENOMEM;
33425 + orphan->inum = inum;
33426 +
33427 + p = &c->orph_tree.rb_node;
33428 + while (*p) {
33429 + parent = *p;
33430 + o = rb_entry(parent, struct ubifs_orphan, rb);
33431 + if (inum < o->inum)
33432 + p = &(*p)->rb_left;
33433 + else if (inum > o->inum)
33434 + p = &(*p)->rb_right;
33435 + else {
33436 + /* Already added - no problem */
33437 + kfree(orphan);
33438 + return 0;
33439 + }
33440 + }
33441 + c->tot_orphans += 1;
33442 + rb_link_node(&orphan->rb, parent, p);
33443 + rb_insert_color(&orphan->rb, &c->orph_tree);
33444 + list_add_tail(&orphan->list, &c->orph_list);
33445 + orphan->dnext = c->orph_dnext;
33446 + c->orph_dnext = orphan;
33447 + dbg_mnt("ino %lu, new %d, tot %d",
33448 + inum, c->new_orphans, c->tot_orphans);
33449 + return 0;
33450 +}
33451 +
33452 +/**
33453 + * do_kill_orphans - remove orphan inodes from the index.
33454 + * @c: UBIFS file-system description object
33455 + * @sleb: scanned LEB
33456 + * @last_cmt_no: cmt_no of last orph node read is passed and returned here
33457 + * @outofdate: whether the LEB is out of date is returned here
33458 + * @last_flagged: whether the end orph node is encountered
33459 + *
33460 + * This function is a helper to the 'kill_orphans()' function. It goes through
33461 + * every orphan node in a LEB and for every inode number recorded, removes
33462 + * all keys for that inode from the TNC.
33463 + */
33464 +static int do_kill_orphans(struct ubifs_info *c, struct ubifs_scan_leb *sleb,
33465 + unsigned long long *last_cmt_no, int *outofdate,
33466 + int *last_flagged)
33467 +{
33468 + struct ubifs_scan_node *snod;
33469 + struct ubifs_orph_node *orph;
33470 + unsigned long long cmt_no;
33471 + ino_t inum;
33472 + int i, n, err, first = 1;
33473 +
33474 + list_for_each_entry(snod, &sleb->nodes, list) {
33475 + if (snod->type != UBIFS_ORPH_NODE) {
33476 + ubifs_err("invalid node type %d in orphan area at "
33477 + "%d:%d", snod->type, sleb->lnum, snod->offs);
33478 + dbg_dump_node(c, snod->node);
33479 + return -EINVAL;
33480 + }
33481 +
33482 + orph = snod->node;
33483 +
33484 + /* Check commit number */
33485 + cmt_no = le64_to_cpu(orph->cmt_no) & LLONG_MAX;
33486 + /*
33487 + * The commit number on the master node may be less, because
33488 + * of a failed commit. If there are several failed commits in a
33489 + * row, the commit number written on orph nodes will continue to
33490 + * increase (because the commit number is adjusted here) even
33491 + * though the commit number on the master node stays the same
33492 + * because the master node has not been re-written.
33493 + */
33494 + if (cmt_no > c->cmt_no)
33495 + c->cmt_no = cmt_no;
33496 + if (cmt_no < *last_cmt_no && *last_flagged) {
33497 + /*
33498 + * The last orph node had a higher commit number and was
33499 + * flagged as the last written for that commit number.
33500 + * That makes this orph node, out of date.
33501 + */
33502 + if (!first) {
33503 + ubifs_err("out of order commit number %llu in "
33504 + "orphan node at %d:%d",
33505 + cmt_no, sleb->lnum, snod->offs);
33506 + dbg_dump_node(c, snod->node);
33507 + return -EINVAL;
33508 + }
33509 + dbg_rcvry("out of date LEB %d", sleb->lnum);
33510 + *outofdate = 1;
33511 + return 0;
33512 + }
33513 +
33514 + if (first)
33515 + first = 0;
33516 +
33517 + n = (le32_to_cpu(orph->ch.len) - UBIFS_ORPH_NODE_SZ) >> 3;
33518 + for (i = 0; i < n; i++) {
33519 + inum = le64_to_cpu(orph->inos[i]);
33520 + dbg_rcvry("deleting orphaned inode %lu", inum);
33521 + err = ubifs_tnc_remove_ino(c, inum);
33522 + if (err)
33523 + return err;
33524 + err = insert_dead_orphan(c, inum);
33525 + if (err)
33526 + return err;
33527 + }
33528 +
33529 + *last_cmt_no = cmt_no;
33530 + if (le64_to_cpu(orph->cmt_no) & (1ULL << 63)) {
33531 + dbg_rcvry("last orph node for commit %llu at %d:%d",
33532 + cmt_no, sleb->lnum, snod->offs);
33533 + *last_flagged = 1;
33534 + } else
33535 + *last_flagged = 0;
33536 + }
33537 +
33538 + return 0;
33539 +}
33540 +
33541 +/**
33542 + * kill_orphans - remove all orphan inodes from the index.
33543 + * @c: UBIFS file-system description object
33544 + *
33545 + * If recovery is required, then orphan inodes recorded during the previous
33546 + * session (which ended with an unclean unmount) must be deleted from the index.
33547 + * This is done by updating the TNC, but since the index is not updated until
33548 + * the next commit, the LEBs where the orphan information is recorded are not
33549 + * erased until the next commit.
33550 + */
33551 +static int kill_orphans(struct ubifs_info *c)
33552 +{
33553 + unsigned long long last_cmt_no = 0;
33554 + int lnum, err = 0, outofdate = 0, last_flagged = 0;
33555 +
33556 + c->ohead_lnum = c->orph_first;
33557 + c->ohead_offs = 0;
33558 + /* Check no-orphans flag and skip this if no orphans */
33559 + if (c->no_orphs) {
33560 + dbg_rcvry("no orphans");
33561 + return 0;
33562 + }
33563 + /*
33564 + * Orph nodes always start at c->orph_first and are written to each
33565 + * successive LEB in turn. Generally unused LEBs will have been unmapped
33566 + * but may contain out of date orph nodes if the unmap didn't go
33567 + * through. In addition, the last orph node written for each commit is
33568 + * marked (top bit of orph->cmt_no is set to 1). It is possible that
33569 + * there are orph nodes from the next commit (i.e. the commit did not
33570 + * complete successfully). In that case, no orphans will have been lost
33571 + * due to the way that orphans are written, and any orphans added will
33572 + * be valid orphans anyway and so can be deleted.
33573 + */
33574 + for (lnum = c->orph_first; lnum <= c->orph_last; lnum++) {
33575 + struct ubifs_scan_leb *sleb;
33576 +
33577 + dbg_rcvry("LEB %d", lnum);
33578 + sleb = ubifs_scan(c, lnum, 0, c->sbuf);
33579 + if (IS_ERR(sleb)) {
33580 + sleb = ubifs_recover_leb(c, lnum, 0, c->sbuf, 0);
33581 + if (IS_ERR(sleb)) {
33582 + err = PTR_ERR(sleb);
33583 + break;
33584 + }
33585 + }
33586 + err = do_kill_orphans(c, sleb, &last_cmt_no, &outofdate,
33587 + &last_flagged);
33588 + if (err || outofdate) {
33589 + ubifs_scan_destroy(sleb);
33590 + break;
33591 + }
33592 + if (sleb->endpt) {
33593 + c->ohead_lnum = lnum;
33594 + c->ohead_offs = sleb->endpt;
33595 + }
33596 + ubifs_scan_destroy(sleb);
33597 + }
33598 + return err;
33599 +}
33600 +
33601 +/**
33602 + * ubifs_mount_orphans - delete orphan inodes and erase LEBs that recorded them.
33603 + * @c: UBIFS file-system description object
33604 + * @unclean: indicates recovery from unclean unmount
33605 + * @read_only: indicates read only mount
33606 + *
33607 + * This function is called when mounting to erase orphans from the previous
33608 + * session. If UBIFS was not unmounted cleanly, then the inodes recorded as
33609 + * orphans are deleted.
33610 + */
33611 +int ubifs_mount_orphans(struct ubifs_info *c, int unclean, int read_only)
33612 +{
33613 + int err = 0;
33614 +
33615 + c->max_orphans = tot_avail_orphs(c);
33616 +
33617 + if (!read_only) {
33618 + c->orph_buf = vmalloc(c->leb_size);
33619 + if (!c->orph_buf)
33620 + return -ENOMEM;
33621 + }
33622 +
33623 + if (unclean)
33624 + err = kill_orphans(c);
33625 + else if (!read_only)
33626 + err = clear_orphans(c);
33627 +
33628 + return err;
33629 +}
33630 +
33631 +#ifdef CONFIG_UBIFS_FS_DEBUG
33632 +
33633 +struct check_orphan {
33634 + struct rb_node rb;
33635 + ino_t inum;
33636 +};
33637 +
33638 +struct check_info {
33639 + unsigned long last_ino;
33640 + unsigned long tot_inos;
33641 + unsigned long missing;
33642 + unsigned long long leaf_cnt;
33643 + struct ubifs_ino_node *node;
33644 + struct rb_root root;
33645 +};
33646 +
33647 +static int dbg_find_orphan(struct ubifs_info *c, ino_t inum)
33648 +{
33649 + struct ubifs_orphan *o;
33650 + struct rb_node *p;
33651 +
33652 + spin_lock(&c->orphan_lock);
33653 + p = c->orph_tree.rb_node;
33654 + while (p) {
33655 + o = rb_entry(p, struct ubifs_orphan, rb);
33656 + if (inum < o->inum)
33657 + p = p->rb_left;
33658 + else if (inum > o->inum)
33659 + p = p->rb_right;
33660 + else {
33661 + spin_unlock(&c->orphan_lock);
33662 + return 1;
33663 + }
33664 + }
33665 + spin_unlock(&c->orphan_lock);
33666 + return 0;
33667 +}
33668 +
33669 +static int dbg_ins_check_orphan(struct rb_root *root, ino_t inum)
33670 +{
33671 + struct check_orphan *orphan, *o;
33672 + struct rb_node **p, *parent = NULL;
33673 +
33674 + orphan = kzalloc(sizeof(struct check_orphan), GFP_NOFS);
33675 + if (!orphan)
33676 + return -ENOMEM;
33677 + orphan->inum = inum;
33678 +
33679 + p = &root->rb_node;
33680 + while (*p) {
33681 + parent = *p;
33682 + o = rb_entry(parent, struct check_orphan, rb);
33683 + if (inum < o->inum)
33684 + p = &(*p)->rb_left;
33685 + else if (inum > o->inum)
33686 + p = &(*p)->rb_right;
33687 + else {
33688 + kfree(orphan);
33689 + return 0;
33690 + }
33691 + }
33692 + rb_link_node(&orphan->rb, parent, p);
33693 + rb_insert_color(&orphan->rb, root);
33694 + return 0;
33695 +}
33696 +
33697 +static int dbg_find_check_orphan(struct rb_root *root, ino_t inum)
33698 +{
33699 + struct check_orphan *o;
33700 + struct rb_node *p;
33701 +
33702 + p = root->rb_node;
33703 + while (p) {
33704 + o = rb_entry(p, struct check_orphan, rb);
33705 + if (inum < o->inum)
33706 + p = p->rb_left;
33707 + else if (inum > o->inum)
33708 + p = p->rb_right;
33709 + else
33710 + return 1;
33711 + }
33712 + return 0;
33713 +}
33714 +
33715 +static void dbg_free_check_tree(struct rb_root *root)
33716 +{
33717 + struct rb_node *this = root->rb_node;
33718 + struct check_orphan *o;
33719 +
33720 + while (this) {
33721 + if (this->rb_left) {
33722 + this = this->rb_left;
33723 + continue;
33724 + } else if (this->rb_right) {
33725 + this = this->rb_right;
33726 + continue;
33727 + }
33728 + o = rb_entry(this, struct check_orphan, rb);
33729 + this = rb_parent(this);
33730 + if (this) {
33731 + if (this->rb_left == &o->rb)
33732 + this->rb_left = NULL;
33733 + else
33734 + this->rb_right = NULL;
33735 + }
33736 + kfree(o);
33737 + }
33738 +}
33739 +
33740 +static int dbg_orphan_check(struct ubifs_info *c, struct ubifs_zbranch *zbr,
33741 + void *priv)
33742 +{
33743 + struct check_info *ci = priv;
33744 + ino_t inum;
33745 + int err;
33746 +
33747 + inum = key_inum(c, &zbr->key);
33748 + if (inum != ci->last_ino) {
33749 + /* Lowest node type is the inode node, so it comes first */
33750 + if (key_type(c, &zbr->key) != UBIFS_INO_KEY)
33751 + ubifs_err("found orphan node ino %lu, type %d", inum,
33752 + key_type(c, &zbr->key));
33753 + ci->last_ino = inum;
33754 + ci->tot_inos += 1;
33755 + err = ubifs_tnc_read_node(c, zbr, ci->node);
33756 + if (err) {
33757 + ubifs_err("node read failed, error %d", err);
33758 + return err;
33759 + }
33760 + if (ci->node->nlink == 0)
33761 + /* Must be recorded as an orphan */
33762 + if (!dbg_find_check_orphan(&ci->root, inum) &&
33763 + !dbg_find_orphan(c, inum)) {
33764 + ubifs_err("missing orphan, ino %lu", inum);
33765 + ci->missing += 1;
33766 + }
33767 + }
33768 + ci->leaf_cnt += 1;
33769 + return 0;
33770 +}
33771 +
33772 +static int dbg_read_orphans(struct check_info *ci, struct ubifs_scan_leb *sleb)
33773 +{
33774 + struct ubifs_scan_node *snod;
33775 + struct ubifs_orph_node *orph;
33776 + ino_t inum;
33777 + int i, n, err;
33778 +
33779 + list_for_each_entry(snod, &sleb->nodes, list) {
33780 + cond_resched();
33781 + if (snod->type != UBIFS_ORPH_NODE)
33782 + continue;
33783 + orph = snod->node;
33784 + n = (le32_to_cpu(orph->ch.len) - UBIFS_ORPH_NODE_SZ) >> 3;
33785 + for (i = 0; i < n; i++) {
33786 + inum = le64_to_cpu(orph->inos[i]);
33787 + err = dbg_ins_check_orphan(&ci->root, inum);
33788 + if (err)
33789 + return err;
33790 + }
33791 + }
33792 + return 0;
33793 +}
33794 +
33795 +static int dbg_scan_orphans(struct ubifs_info *c, struct check_info *ci)
33796 +{
33797 + int lnum, err = 0;
33798 +
33799 + /* Check no-orphans flag and skip this if no orphans */
33800 + if (c->no_orphs)
33801 + return 0;
33802 +
33803 + for (lnum = c->orph_first; lnum <= c->orph_last; lnum++) {
33804 + struct ubifs_scan_leb *sleb;
33805 +
33806 + sleb = ubifs_scan(c, lnum, 0, c->dbg_buf);
33807 + if (IS_ERR(sleb)) {
33808 + err = PTR_ERR(sleb);
33809 + break;
33810 + }
33811 +
33812 + err = dbg_read_orphans(ci, sleb);
33813 + ubifs_scan_destroy(sleb);
33814 + if (err)
33815 + break;
33816 + }
33817 +
33818 + return err;
33819 +}
33820 +
33821 +static int dbg_check_orphans(struct ubifs_info *c)
33822 +{
33823 + struct check_info ci;
33824 + int err;
33825 +
33826 + if (!(ubifs_chk_flags & UBIFS_CHK_ORPH))
33827 + return 0;
33828 +
33829 + ci.last_ino = 0;
33830 + ci.tot_inos = 0;
33831 + ci.missing = 0;
33832 + ci.leaf_cnt = 0;
33833 + ci.root = RB_ROOT;
33834 + ci.node = kmalloc(UBIFS_MAX_INO_NODE_SZ, GFP_NOFS);
33835 + if (!ci.node) {
33836 + ubifs_err("out of memory");
33837 + return -ENOMEM;
33838 + }
33839 +
33840 + err = dbg_scan_orphans(c, &ci);
33841 + if (err)
33842 + goto out;
33843 +
33844 + err = dbg_walk_index(c, &dbg_orphan_check, NULL, &ci);
33845 + if (err) {
33846 + ubifs_err("cannot scan TNC, error %d", err);
33847 + goto out;
33848 + }
33849 +
33850 + if (ci.missing) {
33851 + ubifs_err("%lu missing orphan(s)", ci.missing);
33852 + err = -EINVAL;
33853 + goto out;
33854 + }
33855 +
33856 + dbg_cmt("last inode number is %lu", ci.last_ino);
33857 + dbg_cmt("total number of inodes is %lu", ci.tot_inos);
33858 + dbg_cmt("total number of leaf nodes is %llu", ci.leaf_cnt);
33859 +
33860 +out:
33861 + dbg_free_check_tree(&ci.root);
33862 + kfree(ci.node);
33863 + return err;
33864 +}
33865 +
33866 +#endif /* CONFIG_UBIFS_FS_DEBUG */
33867 diff --exclude=.git -urN linux-2.6.25.6/fs/ubifs/recovery.c avr32-2.6/fs/ubifs/recovery.c
33868 --- linux-2.6.25.6/fs/ubifs/recovery.c 1970-01-01 01:00:00.000000000 +0100
33869 +++ avr32-2.6/fs/ubifs/recovery.c 2008-06-12 15:09:45.475816115 +0200
33870 @@ -0,0 +1,1537 @@
33871 +/*
33872 + * This file is part of UBIFS.
33873 + *
33874 + * Copyright (C) 2006-2008 Nokia Corporation
33875 + *
33876 + * This program is free software; you can redistribute it and/or modify it
33877 + * under the terms of the GNU General Public License version 2 as published by
33878 + * the Free Software Foundation.
33879 + *
33880 + * This program is distributed in the hope that it will be useful, but WITHOUT
33881 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
33882 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
33883 + * more details.
33884 + *
33885 + * You should have received a copy of the GNU General Public License along with
33886 + * this program; if not, write to the Free Software Foundation, Inc., 51
33887 + * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
33888 + *
33889 + * Authors: Adrian Hunter
33890 + * Artem Bityutskiy (Битюцкий Артём)
33891 + */
33892 +
33893 +/*
33894 + * This file implements functions needed to recover from unclean un-mounts.
33895 + * When UBIFS is mounted, it checks a flag on the master node to determine if
33896 + * an un-mount was completed sucessfully. If not, the process of mounting
33897 + * incorparates additional checking and fixing of on-flash data structures.
33898 + * UBIFS always cleans away all remnants of an unclean un-mount, so that
33899 + * errors do not accumulate. However UBIFS defers recovery if it is mounted
33900 + * read-only, and the flash is not modified in that case.
33901 + */
33902 +
33903 +#include <linux/crc32.h>
33904 +#include "ubifs.h"
33905 +
33906 +/**
33907 + * is_empty - determine whether a buffer is empty (contains all 0xff).
33908 + * @buf: buffer to clean
33909 + * @len: length of buffer
33910 + *
33911 + * This function returns %1 if the buffer is empty (contains all 0xff) otherwise
33912 + * %0 is returned.
33913 + */
33914 +static int is_empty(void *buf, int len)
33915 +{
33916 + uint8_t *p = buf;
33917 + int i;
33918 +
33919 + for (i = 0; i < len; i++)
33920 + if (*p++ != 0xff)
33921 + return 0;
33922 + return 1;
33923 +}
33924 +
33925 +/**
33926 + * get_master_node - get the last valid master node allowing for corruption.
33927 + * @c: UBIFS file-system description object
33928 + * @lnum: LEB number
33929 + * @pbuf: buffer containing the LEB read, is returned here
33930 + * @mst: master node, if found, is returned here
33931 + * @cor: corruption, if found, is returned here
33932 + *
33933 + * This function allocates a buffer, reads the LEB into it, and finds and
33934 + * returns the last valid master node allowing for one area of corruption.
33935 + * The corrupt area, if there is one, must be consistent with the assumption
33936 + * that it is the result of an unclean unmount while the master node was being
33937 + * written. Under those circumstances, it is valid to use the previously written
33938 + * master node.
33939 + *
33940 + * This function returns %0 on success and a negative error code on failure.
33941 + */
33942 +static int get_master_node(const struct ubifs_info *c, int lnum, void **pbuf,
33943 + struct ubifs_mst_node **mst, void **cor)
33944 +{
33945 + const int sz = c->mst_node_alsz;
33946 + int err, offs, len;
33947 + void *sbuf, *buf;
33948 +
33949 + sbuf = vmalloc(c->leb_size);
33950 + if (!sbuf)
33951 + return -ENOMEM;
33952 +
33953 + err = ubi_read(c->ubi, lnum, sbuf, 0, c->leb_size);
33954 + if (err && err != -EBADMSG)
33955 + goto out_free;
33956 +
33957 + /* Find the first position that is definitely not a node */
33958 + offs = 0;
33959 + buf = sbuf;
33960 + len = c->leb_size;
33961 + while (offs + UBIFS_MST_NODE_SZ <= c->leb_size) {
33962 + struct ubifs_ch *ch = buf;
33963 +
33964 + if (le32_to_cpu(ch->magic) != UBIFS_NODE_MAGIC)
33965 + break;
33966 + offs += sz;
33967 + buf += sz;
33968 + len -= sz;
33969 + }
33970 + /* See if there was a valid master node before that */
33971 + if (offs) {
33972 + int ret;
33973 +
33974 + offs -= sz;
33975 + buf -= sz;
33976 + len += sz;
33977 + ret = ubifs_scan_a_node(c, buf, len, lnum, offs, 1);
33978 + if (ret != SCANNED_A_NODE && offs) {
33979 + /* Could have been corruption so check one place back */
33980 + offs -= sz;
33981 + buf -= sz;
33982 + len += sz;
33983 + ret = ubifs_scan_a_node(c, buf, len, lnum, offs, 1);
33984 + if (ret != SCANNED_A_NODE)
33985 + /*
33986 + * We accept only one area of corruption because
33987 + * we are assuming that it was caused while
33988 + * trying to write a master node.
33989 + */
33990 + goto out_err;
33991 + }
33992 + if (ret == SCANNED_A_NODE) {
33993 + struct ubifs_ch *ch = buf;
33994 +
33995 + if (ch->node_type != UBIFS_MST_NODE)
33996 + goto out_err;
33997 + dbg_rcvry("found a master node at %d:%d", lnum, offs);
33998 + *mst = buf;
33999 + offs += sz;
34000 + buf += sz;
34001 + len -= sz;
34002 + }
34003 + }
34004 + /* Check for corruption */
34005 + if (offs < c->leb_size) {
34006 + if (!is_empty(buf, min_t(int, len, sz))) {
34007 + *cor = buf;
34008 + dbg_rcvry("found corruption at %d:%d", lnum, offs);
34009 + }
34010 + offs += sz;
34011 + buf += sz;
34012 + len -= sz;
34013 + }
34014 + /* Check remaining empty space */
34015 + if (offs < c->leb_size)
34016 + if (!is_empty(buf, len))
34017 + goto out_err;
34018 + *pbuf = sbuf;
34019 + return 0;
34020 +
34021 +out_err:
34022 + err = -EINVAL;
34023 +out_free:
34024 + vfree(sbuf);
34025 + *mst = NULL;
34026 + *cor = NULL;
34027 + return err;
34028 +}
34029 +
34030 +/**
34031 + * write_rcvrd_mst_node - write recovered master node.
34032 + * @c: UBIFS file-system description object
34033 + * @mst: master node
34034 + *
34035 + * This function returns %0 on success and a negative error code on failure.
34036 + */
34037 +static int write_rcvrd_mst_node(struct ubifs_info *c,
34038 + struct ubifs_mst_node *mst)
34039 +{
34040 + int err = 0, lnum = UBIFS_MST_LNUM, sz = c->mst_node_alsz;
34041 + uint32_t save_flags;
34042 +
34043 + dbg_rcvry("recovery");
34044 +
34045 + save_flags = mst->flags;
34046 + mst->flags = cpu_to_le32(le32_to_cpu(mst->flags) | UBIFS_MST_RCVRY);
34047 +
34048 + ubifs_prepare_node(c, mst, UBIFS_MST_NODE_SZ, 1);
34049 + err = ubi_leb_change(c->ubi, lnum, mst, sz, UBI_SHORTTERM);
34050 + if (err)
34051 + goto out;
34052 + err = ubi_leb_change(c->ubi, lnum + 1, mst, sz, UBI_SHORTTERM);
34053 + if (err)
34054 + goto out;
34055 +out:
34056 + mst->flags = save_flags;
34057 + return err;
34058 +}
34059 +
34060 +/**
34061 + * ubifs_recover_master_node - recover the master node.
34062 + * @c: UBIFS file-system description object
34063 + *
34064 + * This function recovers the master node from corruption that may occur due to
34065 + * an unclean unmount.
34066 + *
34067 + * This function returns %0 on success and a negative error code on failure.
34068 + */
34069 +int ubifs_recover_master_node(struct ubifs_info *c)
34070 +{
34071 + void *buf1 = NULL, *buf2 = NULL, *cor1 = NULL, *cor2 = NULL;
34072 + struct ubifs_mst_node *mst1 = NULL, *mst2 = NULL, *mst;
34073 + const int sz = c->mst_node_alsz;
34074 + int err, offs1, offs2;
34075 +
34076 + dbg_rcvry("recovery");
34077 +
34078 + err = get_master_node(c, UBIFS_MST_LNUM, &buf1, &mst1, &cor1);
34079 + if (err)
34080 + goto out_free;
34081 +
34082 + err = get_master_node(c, UBIFS_MST_LNUM + 1, &buf2, &mst2, &cor2);
34083 + if (err)
34084 + goto out_free;
34085 +
34086 + if (mst1) {
34087 + offs1 = (void *)mst1 - buf1;
34088 + if ((le32_to_cpu(mst1->flags) & UBIFS_MST_RCVRY) &&
34089 + (offs1 == 0 && !cor1)) {
34090 + /*
34091 + * mst1 was written by recovery at offset 0 with no
34092 + * corruption.
34093 + */
34094 + dbg_rcvry("recovery recovery");
34095 + mst = mst1;
34096 + } else if (mst2) {
34097 + offs2 = (void *)mst2 - buf2;
34098 + if (offs1 == offs2) {
34099 + /* Same offset, so must be the same */
34100 + if (memcmp((void *)mst1 + UBIFS_CH_SZ,
34101 + (void *)mst2 + UBIFS_CH_SZ,
34102 + UBIFS_MST_NODE_SZ - UBIFS_CH_SZ))
34103 + goto out_err;
34104 + mst = mst1;
34105 + } else if (offs2 + sz == offs1) {
34106 + /* 1st LEB was written, 2nd was not */
34107 + if (cor1)
34108 + goto out_err;
34109 + mst = mst1;
34110 + } else if (offs1 == 0 && offs2 + sz >= c->leb_size) {
34111 + /* 1st LEB was unmapped and written, 2nd not */
34112 + if (cor1)
34113 + goto out_err;
34114 + mst = mst1;
34115 + } else
34116 + goto out_err;
34117 + } else {
34118 + /*
34119 + * 2nd LEB was unmapped and about to be written, so
34120 + * there must be only one master node in the first LEB
34121 + * and no corruption.
34122 + */
34123 + if (offs1 != 0 || cor1)
34124 + goto out_err;
34125 + mst = mst1;
34126 + }
34127 + } else {
34128 + if (!mst2)
34129 + goto out_err;
34130 + /*
34131 + * 1st LEB was unmapped and about to be written, so there must
34132 + * be no room left in 2nd LEB.
34133 + */
34134 + offs2 = (void *)mst2 - buf2;
34135 + if (offs2 + sz + sz <= c->leb_size)
34136 + goto out_err;
34137 + mst = mst2;
34138 + }
34139 +
34140 + dbg_rcvry("recovered master node from LEB %d",
34141 + (mst == mst1 ? UBIFS_MST_LNUM : UBIFS_MST_LNUM + 1));
34142 +
34143 + memcpy(c->mst_node, mst, UBIFS_MST_NODE_SZ);
34144 +
34145 + if ((c->vfs_sb->s_flags & MS_RDONLY)) {
34146 + /* Read-only mode. Keep a copy for switching to rw mode */
34147 + c->rcvrd_mst_node = kmalloc(sz, GFP_KERNEL);
34148 + if (!c->rcvrd_mst_node) {
34149 + err = -ENOMEM;
34150 + goto out_free;
34151 + }
34152 + memcpy(c->rcvrd_mst_node, c->mst_node, UBIFS_MST_NODE_SZ);
34153 + } else {
34154 + /* Write the recovered master node */
34155 + c->max_sqnum = le64_to_cpu(mst->ch.sqnum) - 1;
34156 + err = write_rcvrd_mst_node(c, c->mst_node);
34157 + if (err)
34158 + goto out_free;
34159 + }
34160 +
34161 + vfree(buf2);
34162 + vfree(buf1);
34163 +
34164 + return 0;
34165 +
34166 +out_err:
34167 + err = -EINVAL;
34168 +out_free:
34169 + ubifs_err("failed to recover master node");
34170 + if (mst1) {
34171 + dbg_err("dumping first master node");
34172 + dbg_dump_node(c, mst1);
34173 + }
34174 + if (mst2) {
34175 + dbg_err("dumping second master node");
34176 + dbg_dump_node(c, mst2);
34177 + }
34178 + vfree(buf2);
34179 + vfree(buf1);
34180 + return err;
34181 +}
34182 +
34183 +/**
34184 + * ubifs_write_rcvrd_mst_node - write the recovered master node.
34185 + * @c: UBIFS file-system description object
34186 + *
34187 + * This function writes the master node that was recovered during mounting in
34188 + * read-only mode and must now be written because we are remounting rw.
34189 + *
34190 + * This function returns %0 on success and a negative error code on failure.
34191 + */
34192 +int ubifs_write_rcvrd_mst_node(struct ubifs_info *c)
34193 +{
34194 + int err;
34195 +
34196 + if (!c->rcvrd_mst_node)
34197 + return 0;
34198 + c->rcvrd_mst_node->flags |= cpu_to_le32(UBIFS_MST_DIRTY);
34199 + c->mst_node->flags |= cpu_to_le32(UBIFS_MST_DIRTY);
34200 + err = write_rcvrd_mst_node(c, c->rcvrd_mst_node);
34201 + if (err)
34202 + return err;
34203 + kfree(c->rcvrd_mst_node);
34204 + c->rcvrd_mst_node = NULL;
34205 + return 0;
34206 +}
34207 +
34208 +/**
34209 + * is_last_write - determine if an offset was in the last write to a LEB.
34210 + * @c: UBIFS file-system description object
34211 + * @buf: buffer to check
34212 + * @offs: offset to check
34213 + *
34214 + * This function returns %1 if @offs was in the last write to the LEB whose data
34215 + * is in @buf, otherwise %0 is returned. The determination is made by checking
34216 + * for subsequent empty space starting from the next min_io_size boundary (or a
34217 + * bit less than the common header size if min_io_size is one).
34218 + */
34219 +static int is_last_write(const struct ubifs_info *c, void *buf, int offs)
34220 +{
34221 + int empty_offs;
34222 + int check_len;
34223 + uint8_t *p;
34224 +
34225 + if (c->min_io_size == 1) {
34226 + check_len = c->leb_size - offs;
34227 + p = buf + check_len;
34228 + for (; check_len > 0; check_len--)
34229 + if (*--p != 0xff)
34230 + break;
34231 + /*
34232 + * 'check_len' is the size of the corruption which cannot be
34233 + * more than the size of 1 node if it was caused by an unclean
34234 + * unmount.
34235 + */
34236 + if (check_len > UBIFS_MAX_NODE_SZ)
34237 + return 0;
34238 + return 1;
34239 + }
34240 +
34241 + /*
34242 + * Round up to the next c->min_io_size boundary i.e. 'offs' is in the
34243 + * last wbuf written. After that should be empty space.
34244 + */
34245 + empty_offs = ALIGN(offs + 1, c->min_io_size);
34246 + check_len = c->leb_size - empty_offs;
34247 + p = buf + empty_offs - offs;
34248 +
34249 + for (; check_len > 0; check_len--)
34250 + if (*p++ != 0xff)
34251 + return 0;
34252 + return 1;
34253 +}
34254 +
34255 +/**
34256 + * clean_buf - clean the data from an LEB sitting in a buffer.
34257 + * @c: UBIFS file-system description object
34258 + * @buf: buffer to clean
34259 + * @lnum: LEB number to clean
34260 + * @offs: offset from which to clean
34261 + * @len: length of buffer
34262 + *
34263 + * This function pads up to the next min_io_size boundary (if there is one) and
34264 + * sets empty space to all 0xff. @buf, @offs and @len are updated to the next
34265 + * min_io_size boundary (if there is one).
34266 + */
34267 +static void clean_buf(const struct ubifs_info *c, void **buf, int lnum,
34268 + int *offs, int *len)
34269 +{
34270 + int empty_offs, pad_len;
34271 +
34272 + lnum = lnum;
34273 + dbg_rcvry("cleaning corruption at %d:%d", lnum, *offs);
34274 +
34275 + if (c->min_io_size == 1) {
34276 + memset(*buf, 0xff, c->leb_size - *offs);
34277 + return;
34278 + }
34279 +
34280 + ubifs_assert(!(*offs & 7));
34281 + empty_offs = ALIGN(*offs, c->min_io_size);
34282 + pad_len = empty_offs - *offs;
34283 + ubifs_pad(c, *buf, pad_len);
34284 + *offs += pad_len;
34285 + *buf += pad_len;
34286 + *len -= pad_len;
34287 + memset(*buf, 0xff, c->leb_size - empty_offs);
34288 +}
34289 +
34290 +/**
34291 + * no_more_nodes - determine if there are no more nodes in a buffer.
34292 + * @c: UBIFS file-system description object
34293 + * @buf: buffer to check
34294 + * @len: length of buffer
34295 + * @lnum: LEB number of the LEB from which @buf was read
34296 + * @offs: offset from which @buf was read
34297 + *
34298 + * This function scans @buf for more nodes and returns %0 is a node is found and
34299 + * %1 if no more nodes are found.
34300 + */
34301 +static int no_more_nodes(const struct ubifs_info *c, void *buf, int len,
34302 + int lnum, int offs)
34303 +{
34304 + int skip, next_offs = 0;
34305 +
34306 + if (len > UBIFS_DATA_NODE_SZ) {
34307 + struct ubifs_ch *ch = buf;
34308 + int dlen = le32_to_cpu(ch->len);
34309 +
34310 + if (ch->node_type == UBIFS_DATA_NODE && dlen >= UBIFS_CH_SZ &&
34311 + dlen <= UBIFS_MAX_DATA_NODE_SZ)
34312 + /* The corrupt node looks like a data node */
34313 + next_offs = ALIGN(offs + dlen, 8);
34314 + }
34315 +
34316 + if (c->min_io_size == 1)
34317 + skip = 8;
34318 + else
34319 + skip = ALIGN(offs + 1, c->min_io_size) - offs;
34320 +
34321 + offs += skip;
34322 + buf += skip;
34323 + len -= skip;
34324 + while (len > 8) {
34325 + struct ubifs_ch *ch = buf;
34326 + uint32_t magic = le32_to_cpu(ch->magic);
34327 + int ret;
34328 +
34329 + if (magic == UBIFS_NODE_MAGIC) {
34330 + ret = ubifs_scan_a_node(c, buf, len, lnum, offs, 1);
34331 + if (ret == SCANNED_A_NODE || ret > 0) {
34332 + /*
34333 + * There is a small chance this is just data in
34334 + * a data node, so check that possibility. e.g.
34335 + * this is part of a file that itself contains
34336 + * a UBIFS image.
34337 + */
34338 + if (next_offs && offs + le32_to_cpu(ch->len) <=
34339 + next_offs)
34340 + continue;
34341 + dbg_rcvry("unexpected node at %d:%d", lnum,
34342 + offs);
34343 + return 0;
34344 + }
34345 + }
34346 + offs += 8;
34347 + buf += 8;
34348 + len -= 8;
34349 + }
34350 + return 1;
34351 +}
34352 +
34353 +/**
34354 + * fix_unclean_leb - fix an unclean LEB.
34355 + * @c: UBIFS file-system description object
34356 + * @sleb: scanned LEB information
34357 + * @start: offset where scan started
34358 + */
34359 +static int fix_unclean_leb(struct ubifs_info *c, struct ubifs_scan_leb *sleb,
34360 + int start)
34361 +{
34362 + int lnum = sleb->lnum, endpt = start;
34363 +
34364 + /* Get the end offset of the last node we are keeping */
34365 + if (!list_empty(&sleb->nodes)) {
34366 + struct ubifs_scan_node *snod;
34367 +
34368 + snod = list_entry(sleb->nodes.prev,
34369 + struct ubifs_scan_node, list);
34370 + endpt = snod->offs + snod->len;
34371 + }
34372 +
34373 + if ((c->vfs_sb->s_flags & MS_RDONLY) && !c->remounting_rw) {
34374 + /* Add to recovery list */
34375 + struct ubifs_unclean_leb *ucleb;
34376 +
34377 + dbg_rcvry("need to fix LEB %d start %d endpt %d",
34378 + lnum, start, sleb->endpt);
34379 + ucleb = kzalloc(sizeof(struct ubifs_unclean_leb), GFP_NOFS);
34380 + if (!ucleb)
34381 + return -ENOMEM;
34382 + ucleb->lnum = lnum;
34383 + ucleb->endpt = endpt;
34384 + list_add_tail(&ucleb->list, &c->unclean_leb_list);
34385 + } else {
34386 + /* Write the fixed LEB back to flash */
34387 + int err;
34388 +
34389 + dbg_rcvry("fixing LEB %d start %d endpt %d",
34390 + lnum, start, sleb->endpt);
34391 + if (endpt == 0) {
34392 + err = ubifs_leb_unmap(c, lnum);
34393 + if (err)
34394 + return err;
34395 + } else {
34396 + int len = ALIGN(endpt, c->min_io_size);
34397 +
34398 + if (start) {
34399 + err = ubi_read(c->ubi, lnum, sleb->buf, 0,
34400 + start);
34401 + if (err)
34402 + return err;
34403 + }
34404 + /* Pad to min_io_size */
34405 + if (len > endpt) {
34406 + int pad_len = len - ALIGN(endpt, 8);
34407 +
34408 + if (pad_len > 0) {
34409 + void *buf = sleb->buf + len - pad_len;
34410 +
34411 + ubifs_pad(c, buf, pad_len);
34412 + }
34413 + }
34414 + err = ubi_leb_change(c->ubi, lnum, sleb->buf, len,
34415 + UBI_UNKNOWN);
34416 + if (err)
34417 + return err;
34418 + }
34419 + }
34420 + return 0;
34421 +}
34422 +
34423 +/**
34424 + * drop_incomplete_group - drop nodes from an incomplete group.
34425 + * @sleb: scanned LEB information
34426 + * @offs: offset of dropped nodes is returned here
34427 + *
34428 + * This function returns %1 if nodes are dropped and %0 otherwise.
34429 + */
34430 +static int drop_incomplete_group(struct ubifs_scan_leb *sleb, int *offs)
34431 +{
34432 + int dropped = 0;
34433 +
34434 + while (!list_empty(&sleb->nodes)) {
34435 + struct ubifs_scan_node *snod;
34436 + struct ubifs_ch *ch;
34437 +
34438 + snod = list_entry(sleb->nodes.prev, struct ubifs_scan_node,
34439 + list);
34440 + ch = snod->node;
34441 + if (ch->group_type != UBIFS_IN_NODE_GROUP)
34442 + return dropped;
34443 + dbg_rcvry("dropping node at %d:%d", sleb->lnum, snod->offs);
34444 + *offs = snod->offs;
34445 + list_del(&snod->list);
34446 + kfree(snod);
34447 + sleb->nodes_cnt -= 1;
34448 + dropped = 1;
34449 + }
34450 + return dropped;
34451 +}
34452 +
34453 +/**
34454 + * ubifs_recover_leb - scan and recover a LEB.
34455 + * @c: UBIFS file-system description object
34456 + * @lnum: LEB number
34457 + * @offs: offset
34458 + * @sbuf: LEB-sized buffer to use
34459 + * @grouped: nodes may be grouped for recovery
34460 + *
34461 + * This function does a scan of a LEB, but caters for errors that might have
34462 + * been caused by the unclean unmount from which we are attempting to recover.
34463 + *
34464 + * This function returns %0 on success and a negative error code on failure.
34465 + */
34466 +struct ubifs_scan_leb *ubifs_recover_leb(struct ubifs_info *c, int lnum,
34467 + int offs, void *sbuf, int grouped)
34468 +{
34469 + int err, len = c->leb_size - offs, need_clean = 0, quiet = 1;
34470 + int empty_chkd = 0, start = offs;
34471 + struct ubifs_scan_leb *sleb;
34472 + void *buf = sbuf + offs;
34473 +
34474 + dbg_rcvry("%d:%d", lnum, offs);
34475 +
34476 + sleb = ubifs_start_scan(c, lnum, offs, sbuf);
34477 + if (IS_ERR(sleb))
34478 + return sleb;
34479 +
34480 + if (sleb->ecc)
34481 + need_clean = 1;
34482 +
34483 + while (len >= 8) {
34484 + int ret;
34485 +
34486 + dbg_scan("look at LEB %d:%d (%d bytes left)",
34487 + lnum, offs, len);
34488 +
34489 + cond_resched();
34490 +
34491 + /*
34492 + * Scan quietly until there is an error from which we cannot
34493 + * recover
34494 + */
34495 + ret = ubifs_scan_a_node(c, buf, len, lnum, offs, quiet);
34496 +
34497 + if (ret == SCANNED_A_NODE) {
34498 + /* A valid node, and not a padding node */
34499 + struct ubifs_ch *ch = buf;
34500 + int node_len;
34501 +
34502 + err = ubifs_add_snod(c, sleb, buf, offs);
34503 + if (err)
34504 + goto error;
34505 + node_len = ALIGN(le32_to_cpu(ch->len), 8);
34506 + offs += node_len;
34507 + buf += node_len;
34508 + len -= node_len;
34509 + continue;
34510 + }
34511 +
34512 + if (ret > 0) {
34513 + /* Padding bytes or a valid padding node */
34514 + offs += ret;
34515 + buf += ret;
34516 + len -= ret;
34517 + continue;
34518 + }
34519 +
34520 + if (ret == SCANNED_EMPTY_SPACE) {
34521 + if (!is_empty(buf, len)) {
34522 + if (!is_last_write(c, buf, offs))
34523 + break;
34524 + clean_buf(c, &buf, lnum, &offs, &len);
34525 + need_clean = 1;
34526 + }
34527 + empty_chkd = 1;
34528 + break;
34529 + }
34530 +
34531 + if (ret == SCANNED_GARBAGE || ret == SCANNED_A_BAD_PAD_NODE)
34532 + if (is_last_write(c, buf, offs)) {
34533 + clean_buf(c, &buf, lnum, &offs, &len);
34534 + need_clean = 1;
34535 + empty_chkd = 1;
34536 + break;
34537 + }
34538 +
34539 + if (ret == SCANNED_A_CORRUPT_NODE)
34540 + if (no_more_nodes(c, buf, len, lnum, offs)) {
34541 + clean_buf(c, &buf, lnum, &offs, &len);
34542 + need_clean = 1;
34543 + empty_chkd = 1;
34544 + break;
34545 + }
34546 +
34547 + if (quiet) {
34548 + /* Redo the last scan but noisily */
34549 + quiet = 0;
34550 + continue;
34551 + }
34552 +
34553 + switch (ret) {
34554 + case SCANNED_GARBAGE:
34555 + dbg_err("garbage");
34556 + goto corrupted;
34557 + case SCANNED_A_CORRUPT_NODE:
34558 + case SCANNED_A_BAD_PAD_NODE:
34559 + dbg_err("bad node");
34560 + goto corrupted;
34561 + default:
34562 + dbg_err("unknown");
34563 + goto corrupted;
34564 + }
34565 + }
34566 +
34567 + if (!empty_chkd && !is_empty(buf, len)) {
34568 + if (is_last_write(c, buf, offs)) {
34569 + clean_buf(c, &buf, lnum, &offs, &len);
34570 + need_clean = 1;
34571 + } else {
34572 + ubifs_err("corrupt empty space at LEB %d:%d",
34573 + lnum, offs);
34574 + goto corrupted;
34575 + }
34576 + }
34577 +
34578 + /* Drop nodes from incomplete group */
34579 + if (grouped && drop_incomplete_group(sleb, &offs)) {
34580 + buf = sbuf + offs;
34581 + len = c->leb_size - offs;
34582 + clean_buf(c, &buf, lnum, &offs, &len);
34583 + need_clean = 1;
34584 + }
34585 +
34586 + if (offs % c->min_io_size) {
34587 + clean_buf(c, &buf, lnum, &offs, &len);
34588 + need_clean = 1;
34589 + }
34590 +
34591 + ubifs_end_scan(c, sleb, lnum, offs);
34592 +
34593 + if (need_clean) {
34594 + err = fix_unclean_leb(c, sleb, start);
34595 + if (err)
34596 + goto error;
34597 + }
34598 +
34599 + return sleb;
34600 +
34601 +corrupted:
34602 + ubifs_scanned_corruption(c, lnum, offs, buf);
34603 + err = -EUCLEAN;
34604 +error:
34605 + ubifs_err("LEB %d scanning failed", lnum);
34606 + ubifs_scan_destroy(sleb);
34607 + return ERR_PTR(err);
34608 +}
34609 +
34610 +/**
34611 + * get_cs_sqnum - get commit start sequence number.
34612 + * @c: UBIFS file-system description object
34613 + * @lnum: LEB number of commit start node
34614 + * @offs: offset of commit start node
34615 + * @cs_sqnum: commit start sequence number is returned here
34616 + *
34617 + * This function returns %0 on success and a negative error code on failure.
34618 + */
34619 +static int get_cs_sqnum(struct ubifs_info *c, int lnum, int offs,
34620 + unsigned long long *cs_sqnum)
34621 +{
34622 + struct ubifs_cs_node *cs_node = NULL;
34623 + int err, ret;
34624 +
34625 + dbg_rcvry("at %d:%d", lnum, offs);
34626 + cs_node = kmalloc(UBIFS_CS_NODE_SZ, GFP_KERNEL);
34627 + if (!cs_node)
34628 + return -ENOMEM;
34629 + if (c->leb_size - offs < UBIFS_CS_NODE_SZ)
34630 + goto out_err;
34631 + err = ubi_read(c->ubi, lnum, (void *)cs_node, offs, UBIFS_CS_NODE_SZ);
34632 + if (err && err != -EBADMSG)
34633 + goto out_free;
34634 + ret = ubifs_scan_a_node(c, cs_node, UBIFS_CS_NODE_SZ, lnum, offs, 0);
34635 + if (ret != SCANNED_A_NODE) {
34636 + dbg_err("Not a valid node");
34637 + goto out_err;
34638 + }
34639 + if (cs_node->ch.node_type != UBIFS_CS_NODE) {
34640 + dbg_err("Node a CS node, type is %d", cs_node->ch.node_type);
34641 + goto out_err;
34642 + }
34643 + if (le64_to_cpu(cs_node->cmt_no) != c->cmt_no) {
34644 + dbg_err("CS node cmt_no %llu != current cmt_no %llu",
34645 + (unsigned long long)le64_to_cpu(cs_node->cmt_no),
34646 + c->cmt_no);
34647 + goto out_err;
34648 + }
34649 + *cs_sqnum = le64_to_cpu(cs_node->ch.sqnum);
34650 + dbg_rcvry("commit start sqnum %llu", *cs_sqnum);
34651 + kfree(cs_node);
34652 + return 0;
34653 +
34654 +out_err:
34655 + err = -EINVAL;
34656 +out_free:
34657 + ubifs_err("failed to get CS sqnum");
34658 + kfree(cs_node);
34659 + return err;
34660 +}
34661 +
34662 +/**
34663 + * ubifs_recover_log_leb - scan and recover a log LEB.
34664 + * @c: UBIFS file-system description object
34665 + * @lnum: LEB number
34666 + * @offs: offset
34667 + * @sbuf: LEB-sized buffer to use
34668 + *
34669 + * This function does a scan of a LEB, but caters for errors that might have
34670 + * been caused by the unclean unmount from which we are attempting to recover.
34671 + *
34672 + * This function returns %0 on success and a negative error code on failure.
34673 + */
34674 +struct ubifs_scan_leb *ubifs_recover_log_leb(struct ubifs_info *c, int lnum,
34675 + int offs, void *sbuf)
34676 +{
34677 + struct ubifs_scan_leb *sleb;
34678 + int next_lnum;
34679 +
34680 + dbg_rcvry("LEB %d", lnum);
34681 + next_lnum = lnum + 1;
34682 + if (next_lnum >= UBIFS_LOG_LNUM + c->log_lebs)
34683 + next_lnum = UBIFS_LOG_LNUM;
34684 + if (next_lnum != c->ltail_lnum) {
34685 + /*
34686 + * We can only recover at the end of the log, so check that the
34687 + * next log LEB is empty or out of date.
34688 + */
34689 + sleb = ubifs_scan(c, next_lnum, 0, sbuf);
34690 + if (IS_ERR(sleb))
34691 + return sleb;
34692 + if (sleb->nodes_cnt) {
34693 + struct ubifs_scan_node *snod;
34694 + unsigned long long cs_sqnum = c->cs_sqnum;
34695 +
34696 + snod = list_entry(sleb->nodes.next,
34697 + struct ubifs_scan_node, list);
34698 + if (cs_sqnum == 0) {
34699 + int err;
34700 +
34701 + err = get_cs_sqnum(c, lnum, offs, &cs_sqnum);
34702 + if (err) {
34703 + ubifs_scan_destroy(sleb);
34704 + return ERR_PTR(err);
34705 + }
34706 + }
34707 + if (snod->sqnum > cs_sqnum) {
34708 + ubifs_err("unrecoverable log corruption "
34709 + "in LEB %d", lnum);
34710 + ubifs_scan_destroy(sleb);
34711 + return ERR_PTR(-EUCLEAN);
34712 + }
34713 + }
34714 + ubifs_scan_destroy(sleb);
34715 + }
34716 + return ubifs_recover_leb(c, lnum, offs, sbuf, 0);
34717 +}
34718 +
34719 +/**
34720 + * recover_head - recover a head.
34721 + * @c: UBIFS file-system description object
34722 + * @lnum: LEB number of head to recover
34723 + * @offs: offset of head to recover
34724 + * @sbuf: LEB-sized buffer to use
34725 + *
34726 + * This function ensures that there is no data on the flash at a head location.
34727 + *
34728 + * This function returns %0 on success and a negative error code on failure.
34729 + */
34730 +static int recover_head(const struct ubifs_info *c, int lnum, int offs,
34731 + void *sbuf)
34732 +{
34733 + int len, err, need_clean = 0;
34734 +
34735 + if (c->min_io_size > 1)
34736 + len = c->min_io_size;
34737 + else
34738 + len = 512;
34739 + if (offs + len > c->leb_size)
34740 + len = c->leb_size - offs;
34741 +
34742 + if (!len)
34743 + return 0;
34744 +
34745 + /* Read at the head location and check it is empty flash */
34746 + err = ubi_read(c->ubi, lnum, sbuf, offs, len);
34747 + if (err)
34748 + need_clean = 1;
34749 + else {
34750 + uint8_t *p = sbuf;
34751 +
34752 + while (len--)
34753 + if (*p++ != 0xff) {
34754 + need_clean = 1;
34755 + break;
34756 + }
34757 + }
34758 +
34759 + if (need_clean) {
34760 + dbg_rcvry("cleaning head at %d:%d", lnum, offs);
34761 + if (offs == 0)
34762 + return ubifs_leb_unmap(c, lnum);
34763 + err = ubi_read(c->ubi, lnum, sbuf, 0, offs);
34764 + if (err)
34765 + return err;
34766 + return ubi_leb_change(c->ubi, lnum, sbuf, offs, UBI_UNKNOWN);
34767 + }
34768 +
34769 + return 0;
34770 +}
34771 +
34772 +/**
34773 + * ubifs_recover_inl_heads - recover index and LPT heads.
34774 + * @c: UBIFS file-system description object
34775 + * @sbuf: LEB-sized buffer to use
34776 + *
34777 + * This function ensures that there is no data on the flash at the index and
34778 + * LPT head locations.
34779 + *
34780 + * This deals with the recovery of a half-completed journal commit. UBIFS is
34781 + * careful never to overwrite the last version of the index or the LPT. Because
34782 + * the index and LPT are wandering trees, data from a half-completed commit will
34783 + * not be referenced anywhere in UBIFS. The data will be either in LEBs that are
34784 + * assumed to be empty and will be unmapped anyway before use, or in the index
34785 + * and LPT heads.
34786 + *
34787 + * This function returns %0 on success and a negative error code on failure.
34788 + */
34789 +int ubifs_recover_inl_heads(const struct ubifs_info *c, void *sbuf)
34790 +{
34791 + int err;
34792 +
34793 + ubifs_assert(!(c->vfs_sb->s_flags & MS_RDONLY) || c->remounting_rw);
34794 +
34795 + dbg_rcvry("checking index head at %d:%d", c->ihead_lnum, c->ihead_offs);
34796 + err = recover_head(c, c->ihead_lnum, c->ihead_offs, sbuf);
34797 + if (err)
34798 + return err;
34799 +
34800 + dbg_rcvry("checking LPT head at %d:%d", c->nhead_lnum, c->nhead_offs);
34801 + err = recover_head(c, c->nhead_lnum, c->nhead_offs, sbuf);
34802 + if (err)
34803 + return err;
34804 +
34805 + return 0;
34806 +}
34807 +
34808 +/**
34809 + * clean_an_unclean_leb - read and write a LEB to remove corruption.
34810 + * @c: UBIFS file-system description object
34811 + * @ucleb: unclean LEB information
34812 + * @sbuf: LEB-sized buffer to use
34813 + *
34814 + * This function reads a LEB up to a point pre-determined by the mount recovery,
34815 + * checks the nodes, and writes the result back to the flash, thereby cleaning
34816 + * off any following corruption, or non-fatal ECC errors.
34817 + *
34818 + * This function returns %0 on success and a negative error code on failure.
34819 + */
34820 +static int clean_an_unclean_leb(const struct ubifs_info *c,
34821 + struct ubifs_unclean_leb *ucleb, void *sbuf)
34822 +{
34823 + int err, lnum = ucleb->lnum, offs = 0, len = ucleb->endpt, quiet = 1;
34824 + void *buf = sbuf;
34825 +
34826 + dbg_rcvry("LEB %d len %d", lnum, len);
34827 +
34828 + if (len == 0) {
34829 + /* Nothing to read, just unmap it */
34830 + err = ubifs_leb_unmap(c, lnum);
34831 + if (err)
34832 + return err;
34833 + return 0;
34834 + }
34835 +
34836 + err = ubi_read(c->ubi, lnum, buf, offs, len);
34837 + if (err && err != -EBADMSG)
34838 + return err;
34839 +
34840 + while (len >= 8) {
34841 + int ret;
34842 +
34843 + cond_resched();
34844 +
34845 + /* Scan quietly until there is an error */
34846 + ret = ubifs_scan_a_node(c, buf, len, lnum, offs, quiet);
34847 +
34848 + if (ret == SCANNED_A_NODE) {
34849 + /* A valid node, and not a padding node */
34850 + struct ubifs_ch *ch = buf;
34851 + int node_len;
34852 +
34853 + node_len = ALIGN(le32_to_cpu(ch->len), 8);
34854 + offs += node_len;
34855 + buf += node_len;
34856 + len -= node_len;
34857 + continue;
34858 + }
34859 +
34860 + if (ret > 0) {
34861 + /* Padding bytes or a valid padding node */
34862 + offs += ret;
34863 + buf += ret;
34864 + len -= ret;
34865 + continue;
34866 + }
34867 +
34868 + if (ret == SCANNED_EMPTY_SPACE) {
34869 + ubifs_err("unexpected empty space at %d:%d",
34870 + lnum, offs);
34871 + return -EUCLEAN;
34872 + }
34873 +
34874 + if (quiet) {
34875 + /* Redo the last scan but noisily */
34876 + quiet = 0;
34877 + continue;
34878 + }
34879 +
34880 + ubifs_scanned_corruption(c, lnum, offs, buf);
34881 + return -EUCLEAN;
34882 + }
34883 +
34884 + /* Pad to min_io_size */
34885 + len = ALIGN(ucleb->endpt, c->min_io_size);
34886 + if (len > ucleb->endpt) {
34887 + int pad_len = len - ALIGN(ucleb->endpt, 8);
34888 +
34889 + if (pad_len > 0) {
34890 + buf = c->sbuf + len - pad_len;
34891 + ubifs_pad(c, buf, pad_len);
34892 + }
34893 + }
34894 +
34895 + /* Write back the LEB atomically */
34896 + err = ubi_leb_change(c->ubi, lnum, sbuf, len, UBI_UNKNOWN);
34897 + if (err)
34898 + return err;
34899 +
34900 + dbg_rcvry("cleaned LEB %d", lnum);
34901 +
34902 + return 0;
34903 +}
34904 +
34905 +/**
34906 + * ubifs_clean_lebs - clean LEBs recovered during read-only mount.
34907 + * @c: UBIFS file-system description object
34908 + * @sbuf: LEB-sized buffer to use
34909 + *
34910 + * This function cleans a LEB identified during recovery that needs to be
34911 + * written but was not because UBIFS was mounted read-only. This happens when
34912 + * remounting to read-write mode.
34913 + *
34914 + * This function returns %0 on success and a negative error code on failure.
34915 + */
34916 +int ubifs_clean_lebs(const struct ubifs_info *c, void *sbuf)
34917 +{
34918 + dbg_rcvry("recovery");
34919 + while (!list_empty(&c->unclean_leb_list)) {
34920 + struct ubifs_unclean_leb *ucleb;
34921 + int err;
34922 +
34923 + ucleb = list_entry(c->unclean_leb_list.next,
34924 + struct ubifs_unclean_leb, list);
34925 + err = clean_an_unclean_leb(c, ucleb, sbuf);
34926 + if (err)
34927 + return err;
34928 + list_del(&ucleb->list);
34929 + kfree(ucleb);
34930 + }
34931 + return 0;
34932 +}
34933 +
34934 +/**
34935 + * ubifs_rcvry_gc_commit - recover the GC LEB number and run the commit.
34936 + * @c: UBIFS file-system description object
34937 + *
34938 + * Out-of-place garbage collection requires always one empty LEB with which to
34939 + * start garbage collection. The LEB number is recorded in c->gc_lnum and is
34940 + * written to the master node on unmounting. In the case of an unclean unmount
34941 + * the value of gc_lnum recorded in the master node is out of date and cannot
34942 + * be used. Instead, recovery must allocate an empty LEB for this purpose.
34943 + * However, there may not be enough empty space, in which case it must be
34944 + * possible to GC the dirtiest LEB into the GC head LEB.
34945 + *
34946 + * This function also runs the commit which causes the TNC updates from
34947 + * size-recovery and orphans to be written to the flash. That is important to
34948 + * ensure correct replay order for subsequent mounts.
34949 + *
34950 + * This function returns %0 on success and a negative error code on failure.
34951 + */
34952 +int ubifs_rcvry_gc_commit(struct ubifs_info *c)
34953 +{
34954 + struct ubifs_wbuf *wbuf = &c->jheads[GCHD].wbuf;
34955 + struct ubifs_lprops lp;
34956 + int lnum, err;
34957 +
34958 + c->gc_lnum = -1;
34959 + if (wbuf->lnum == -1) {
34960 + dbg_rcvry("no GC head LEB");
34961 + goto find_free;
34962 + }
34963 + /*
34964 + * See whether the used space in the dirtiest LEB fits in the GC head
34965 + * LEB.
34966 + */
34967 + if (wbuf->offs == c->leb_size) {
34968 + dbg_rcvry("no room in GC head LEB");
34969 + goto find_free;
34970 + }
34971 + err = ubifs_find_dirty_leb(c, &lp, wbuf->offs, 2);
34972 + if (err) {
34973 + if (err == -ENOSPC)
34974 + dbg_err("could not find a dirty LEB");
34975 + return err;
34976 + }
34977 + ubifs_assert(!(lp.flags & LPROPS_INDEX));
34978 + lnum = lp.lnum;
34979 + if (lp.free + lp.dirty == c->leb_size) {
34980 + /* An empty LEB was returned */
34981 + if (lp.free != c->leb_size) {
34982 + err = ubifs_change_one_lp(c, lnum, c->leb_size,
34983 + 0, 0, 0, 0);
34984 + if (err)
34985 + return err;
34986 + }
34987 + err = ubifs_leb_unmap(c, lnum);
34988 + if (err)
34989 + return err;
34990 + c->gc_lnum = lnum;
34991 + dbg_rcvry("allocated LEB %d for GC", lnum);
34992 + /* Run the commit */
34993 + dbg_rcvry("committing");
34994 + return ubifs_run_commit(c);
34995 + }
34996 + /*
34997 + * There was no empty LEB so the used space in the dirtiest LEB must fit
34998 + * in the GC head LEB.
34999 + */
35000 + if (lp.free + lp.dirty < wbuf->offs) {
35001 + dbg_rcvry("LEB %d doesn't fit in GC head LEB %d:%d",
35002 + lnum, wbuf->lnum, wbuf->offs);
35003 + err = ubifs_return_leb(c, lnum);
35004 + if (err)
35005 + return err;
35006 + goto find_free;
35007 + }
35008 + /*
35009 + * We run the commit before garbage collection otherwise subsequent
35010 + * mounts will see the GC and orphan deletion in a different order.
35011 + */
35012 + dbg_rcvry("committing");
35013 + err = ubifs_run_commit(c);
35014 + if (err)
35015 + return err;
35016 + /*
35017 + * The data in the dirtiest LEB fits in the GC head LEB, so do the GC
35018 + * - use locking to keep 'ubifs_assert()' happy.
35019 + */
35020 + dbg_rcvry("GC'ing LEB %d", lnum);
35021 + mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead);
35022 + err = ubifs_garbage_collect_leb(c, &lp);
35023 + if (err >= 0) {
35024 + int err2 = ubifs_wbuf_sync_nolock(wbuf);
35025 +
35026 + if (err2)
35027 + err = err2;
35028 + }
35029 + mutex_unlock(&wbuf->io_mutex);
35030 + if (err < 0) {
35031 + dbg_err("GC failed, error %d", err);
35032 + if (err == -EAGAIN)
35033 + err = -EINVAL;
35034 + return err;
35035 + }
35036 + if (err != LEB_RETAINED) {
35037 + dbg_err("GC returned %d", err);
35038 + return -EINVAL;
35039 + }
35040 + err = ubifs_leb_unmap(c, c->gc_lnum);
35041 + if (err)
35042 + return err;
35043 + dbg_rcvry("allocated LEB %d for GC", lnum);
35044 + return 0;
35045 +
35046 +find_free:
35047 + /*
35048 + * There is no GC head LEB or the free space in the GC head LEB is too
35049 + * small. Allocate gc_lnum by calling 'ubifs_find_free_leb_for_idx()' so
35050 + * GC is not run.
35051 + */
35052 + lnum = ubifs_find_free_leb_for_idx(c);
35053 + if (lnum < 0) {
35054 + dbg_err("could not find an empty LEB");
35055 + return lnum;
35056 + }
35057 + /* And reset the index flag */
35058 + err = ubifs_change_one_lp(c, lnum, LPROPS_NC, LPROPS_NC, 0,
35059 + LPROPS_INDEX, 0);
35060 + if (err)
35061 + return err;
35062 + c->gc_lnum = lnum;
35063 + dbg_rcvry("allocated LEB %d for GC", lnum);
35064 + /* Run the commit */
35065 + dbg_rcvry("committing");
35066 + return ubifs_run_commit(c);
35067 +}
35068 +
35069 +/**
35070 + * struct size_entry - inode size information for recovery.
35071 + * @rb: link in the RB-tree of sizes
35072 + * @inum: inode number
35073 + * @i_size: size on inode
35074 + * @d_size: maximum size based on data nodes
35075 + * @exists: indicates whether the inode exists
35076 + * @inode: inode if pinned in memory awaiting rw mode to fix it
35077 + */
35078 +struct size_entry {
35079 + struct rb_node rb;
35080 + ino_t inum;
35081 + loff_t i_size;
35082 + loff_t d_size;
35083 + int exists;
35084 + struct inode *inode;
35085 +};
35086 +
35087 +/**
35088 + * add_ino - add an entry to the size tree.
35089 + * @c: UBIFS file-system description object
35090 + * @inum: inode number
35091 + * @i_size: size on inode
35092 + * @d_size: maximum size based on data nodes
35093 + * @exists: indicates whether the inode exists
35094 + */
35095 +static int add_ino(struct ubifs_info *c, ino_t inum, loff_t i_size,
35096 + loff_t d_size, int exists)
35097 +{
35098 + struct rb_node **p = &c->size_tree.rb_node, *parent = NULL;
35099 + struct size_entry *e;
35100 +
35101 + while (*p) {
35102 + parent = *p;
35103 + e = rb_entry(parent, struct size_entry, rb);
35104 + if (inum < e->inum)
35105 + p = &(*p)->rb_left;
35106 + else
35107 + p = &(*p)->rb_right;
35108 + }
35109 +
35110 + e = kzalloc(sizeof(struct size_entry), GFP_KERNEL);
35111 + if (!e)
35112 + return -ENOMEM;
35113 +
35114 + e->inum = inum;
35115 + e->i_size = i_size;
35116 + e->d_size = d_size;
35117 + e->exists = exists;
35118 +
35119 + rb_link_node(&e->rb, parent, p);
35120 + rb_insert_color(&e->rb, &c->size_tree);
35121 +
35122 + return 0;
35123 +}
35124 +
35125 +/**
35126 + * find_ino - find an entry on the size tree.
35127 + * @c: UBIFS file-system description object
35128 + * @inum: inode number
35129 + */
35130 +static struct size_entry *find_ino(struct ubifs_info *c, ino_t inum)
35131 +{
35132 + struct rb_node *p = c->size_tree.rb_node;
35133 + struct size_entry *e;
35134 +
35135 + while (p) {
35136 + e = rb_entry(p, struct size_entry, rb);
35137 + if (inum < e->inum)
35138 + p = p->rb_left;
35139 + else if (inum > e->inum)
35140 + p = p->rb_right;
35141 + else
35142 + return e;
35143 + }
35144 + return NULL;
35145 +}
35146 +
35147 +/**
35148 + * remove_ino - remove an entry from the size tree.
35149 + * @c: UBIFS file-system description object
35150 + * @inum: inode number
35151 + */
35152 +static void remove_ino(struct ubifs_info *c, ino_t inum)
35153 +{
35154 + struct size_entry *e = find_ino(c, inum);
35155 +
35156 + if (!e)
35157 + return;
35158 + rb_erase(&e->rb, &c->size_tree);
35159 + kfree(e);
35160 +}
35161 +
35162 +/**
35163 + * ubifs_destroy_size_tree - free resources related to the size tree.
35164 + * @c: UBIFS file-system description object
35165 + */
35166 +void ubifs_destroy_size_tree(struct ubifs_info *c)
35167 +{
35168 + struct rb_node *this = c->size_tree.rb_node;
35169 + struct size_entry *e;
35170 +
35171 + while (this) {
35172 + if (this->rb_left) {
35173 + this = this->rb_left;
35174 + continue;
35175 + } else if (this->rb_right) {
35176 + this = this->rb_right;
35177 + continue;
35178 + }
35179 + e = rb_entry(this, struct size_entry, rb);
35180 + if (e->inode)
35181 + iput(e->inode);
35182 + this = rb_parent(this);
35183 + if (this) {
35184 + if (this->rb_left == &e->rb)
35185 + this->rb_left = NULL;
35186 + else
35187 + this->rb_right = NULL;
35188 + }
35189 + kfree(e);
35190 + }
35191 + c->size_tree = RB_ROOT;
35192 +}
35193 +
35194 +/**
35195 + * ubifs_recover_size_accum - accumulate inode sizes for recovery.
35196 + * @c: UBIFS file-system description object
35197 + * @key: node key
35198 + * @deletion: node is for a deletion
35199 + * @new_size: inode size
35200 + *
35201 + * This function has two purposes:
35202 + * 1) to ensure there are no data nodes that fall outside the inode size
35203 + * 2) to ensure there are no data nodes for inodes that do not exist
35204 + * To accomplish those purposes, a rb-tree is constructed containing an entry
35205 + * for each inode number in the journal that has not been deleted, and recording
35206 + * the size from the inode node, the maximum size of any data node (also altered
35207 + * by truncations) and a flag indicating a inode number for which no inode node
35208 + * was present in the journal.
35209 + *
35210 + * Note that there is still the possibility that there are data nodes that have
35211 + * been committed that are beyond the inode size, however the only way to find
35212 + * them would be to scan the entire index. Alternatively, some provision could
35213 + * be made to record the size of inodes at the start of commit, which would seem
35214 + * very cumbersome for a scenario that is quite unlikely and the only negative
35215 + * consequence of which is wasted space.
35216 + *
35217 + * This functions returns %0 on success and a negative error code on failure.
35218 + */
35219 +int ubifs_recover_size_accum(struct ubifs_info *c, union ubifs_key *key,
35220 + int deletion, loff_t new_size)
35221 +{
35222 + ino_t inum = key_inum(c, key);
35223 + struct size_entry *e;
35224 + int err;
35225 +
35226 + switch (key_type(c, key)) {
35227 + case UBIFS_INO_KEY:
35228 + if (deletion)
35229 + remove_ino(c, inum);
35230 + else {
35231 + e = find_ino(c, inum);
35232 + if (e) {
35233 + e->i_size = new_size;
35234 + e->exists = 1;
35235 + } else {
35236 + err = add_ino(c, inum, new_size, 0, 1);
35237 + if (err)
35238 + return err;
35239 + }
35240 + }
35241 + break;
35242 + case UBIFS_DATA_KEY:
35243 + e = find_ino(c, inum);
35244 + if (e) {
35245 + if (new_size > e->d_size)
35246 + e->d_size = new_size;
35247 + } else {
35248 + err = add_ino(c, inum, 0, new_size, 0);
35249 + if (err)
35250 + return err;
35251 + }
35252 + break;
35253 + case UBIFS_TRUN_KEY:
35254 + e = find_ino(c, inum);
35255 + if (e)
35256 + e->d_size = new_size;
35257 + break;
35258 + }
35259 + return 0;
35260 +}
35261 +
35262 +/**
35263 + * fix_size_in_place - fix inode size in place on flash.
35264 + * @c: UBIFS file-system description object
35265 + * @e: inode size information for recovery
35266 + */
35267 +static int fix_size_in_place(struct ubifs_info *c, struct size_entry *e)
35268 +{
35269 + struct ubifs_ino_node *ino = c->sbuf;
35270 + unsigned char *p;
35271 + union ubifs_key key;
35272 + int err, lnum, offs, len;
35273 + loff_t i_size;
35274 + uint32_t crc;
35275 +
35276 + /* Locate the inode node LEB number and offset */
35277 + ino_key_init(c, &key, e->inum);
35278 + err = ubifs_tnc_locate(c, &key, ino, &lnum, &offs);
35279 + if (err)
35280 + goto out;
35281 + /*
35282 + * If the size recorded on the inode node is greater than the size that
35283 + * was calculated from nodes in the journal then don't change the inode.
35284 + */
35285 + i_size = le64_to_cpu(ino->size);
35286 + if (i_size >= e->d_size)
35287 + return 0;
35288 + /* Read the LEB */
35289 + err = ubi_read(c->ubi, lnum, c->sbuf, 0, c->leb_size);
35290 + if (err)
35291 + goto out;
35292 + /* Change the size field and recalculate the CRC */
35293 + ino = c->sbuf + offs;
35294 + ino->size = cpu_to_le64(e->d_size);
35295 + len = le32_to_cpu(ino->ch.len);
35296 + crc = crc32(UBIFS_CRC32_INIT, (void *)ino + 8, len - 8);
35297 + ino->ch.crc = cpu_to_le32(crc);
35298 + /* Work out where data in the LEB ends and free space begins */
35299 + p = c->sbuf;
35300 + len = c->leb_size - 1;
35301 + while (p[len] == 0xff)
35302 + len -= 1;
35303 + len = ALIGN(len + 1, c->min_io_size);
35304 + /* Atomically write the fixed LEB back again */
35305 + err = ubi_leb_change(c->ubi, lnum, c->sbuf, len, UBI_UNKNOWN);
35306 + if (err)
35307 + goto out;
35308 + dbg_rcvry("inode %lu at %d:%d size %lld -> %lld ", e->inum, lnum, offs,
35309 + i_size, e->d_size);
35310 + return 0;
35311 +
35312 +out:
35313 + ubifs_warn("inode %lu failed to fix size %lld -> %lld error %d",
35314 + e->inum, e->i_size, e->d_size, err);
35315 + return err;
35316 +}
35317 +
35318 +/**
35319 + * ubifs_recover_size - recover inode size.
35320 + * @c: UBIFS file-system description object
35321 + *
35322 + * This function attempts to fix inode size discrepancies identified by the
35323 + * 'ubifs_recover_size_accum()' function.
35324 + *
35325 + * This functions returns %0 on success and a negative error code on failure.
35326 + */
35327 +int ubifs_recover_size(struct ubifs_info *c)
35328 +{
35329 + struct rb_node *this = rb_first(&c->size_tree);
35330 +
35331 + while (this) {
35332 + struct size_entry *e;
35333 + int err;
35334 +
35335 + e = rb_entry(this, struct size_entry, rb);
35336 + if (!e->exists) {
35337 + union ubifs_key key;
35338 +
35339 + ino_key_init(c, &key, e->inum);
35340 + err = ubifs_tnc_lookup(c, &key, c->sbuf);
35341 + if (err && err != -ENOENT)
35342 + return err;
35343 + if (err == -ENOENT) {
35344 + /* Remove data nodes that have no inode */
35345 + dbg_rcvry("removing ino %lu", e->inum);
35346 + err = ubifs_tnc_remove_ino(c, e->inum);
35347 + if (err)
35348 + return err;
35349 + } else {
35350 + struct ubifs_ino_node *ino = c->sbuf;
35351 +
35352 + e->exists = 1;
35353 + e->i_size = le64_to_cpu(ino->size);
35354 + }
35355 + }
35356 + if (e->exists && e->i_size < e->d_size) {
35357 + if (!e->inode && (c->vfs_sb->s_flags & MS_RDONLY)) {
35358 + /* Fix the inode size and pin it in memory */
35359 + struct inode *inode;
35360 +
35361 + inode = ubifs_iget(c->vfs_sb, e->inum);
35362 + if (IS_ERR(inode))
35363 + return PTR_ERR(inode);
35364 + if (inode->i_size < e->d_size) {
35365 + dbg_rcvry("ino %lu size %lld -> %lld",
35366 + e->inum, e->d_size,
35367 + inode->i_size);
35368 + inode->i_size = e->d_size;
35369 + e->inode = inode;
35370 + this = rb_next(this);
35371 + continue;
35372 + }
35373 + iput(inode);
35374 + } else {
35375 + /* Fix the size in place */
35376 + err = fix_size_in_place(c, e);
35377 + if (err) {
35378 + if (e->inode)
35379 + /*
35380 + * We have changed the inode
35381 + * size in memory but failed to
35382 + * fix it on flash. Mark it
35383 + * dirty without budgeting, and
35384 + * hope we don't run out of
35385 + * space.
35386 + */
35387 + mark_inode_dirty_sync(e->inode);
35388 + /*
35389 + * We consider that failing to recover
35390 + * the size is not fatal, because it
35391 + * only affects files that were being
35392 + * written without synchronization and
35393 + * the only down side is that some space
35394 + * may be wasted.
35395 + */
35396 + err = 0;
35397 + }
35398 + if (e->inode)
35399 + iput(e->inode);
35400 + }
35401 + }
35402 + this = rb_next(this);
35403 + rb_erase(&e->rb, &c->size_tree);
35404 + kfree(e);
35405 + }
35406 + return 0;
35407 +}
35408 diff --exclude=.git -urN linux-2.6.25.6/fs/ubifs/replay.c avr32-2.6/fs/ubifs/replay.c
35409 --- linux-2.6.25.6/fs/ubifs/replay.c 1970-01-01 01:00:00.000000000 +0100
35410 +++ avr32-2.6/fs/ubifs/replay.c 2008-06-12 15:09:45.515816461 +0200
35411 @@ -0,0 +1,1075 @@
35412 +/*
35413 + * This file is part of UBIFS.
35414 + *
35415 + * Copyright (C) 2006-2008 Nokia Corporation.
35416 + *
35417 + * This program is free software; you can redistribute it and/or modify it
35418 + * under the terms of the GNU General Public License version 2 as published by
35419 + * the Free Software Foundation.
35420 + *
35421 + * This program is distributed in the hope that it will be useful, but WITHOUT
35422 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
35423 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
35424 + * more details.
35425 + *
35426 + * You should have received a copy of the GNU General Public License along with
35427 + * this program; if not, write to the Free Software Foundation, Inc., 51
35428 + * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
35429 + *
35430 + * Authors: Adrian Hunter
35431 + * Artem Bityutskiy (Битюцкий Артём)
35432 + */
35433 +
35434 +/*
35435 + * This file contains journal replay code. It runs when the file-system is being
35436 + * mounted and requires no locking.
35437 + *
35438 + * The larger is the journal, the longer it takes to scan it, so the longer it
35439 + * takes to mount UBIFS. This is why the journal has limited size which may be
35440 + * changed depending on the system requirements. But a larger journal gives
35441 + * faster I/O speed because it writes the index less frequently. So this is a
35442 + * trade-off. Also, the journal is indexed by the in-memory index (TNC), so the
35443 + * larger is the journal, the more memory its index may consume.
35444 + */
35445 +
35446 +#include "ubifs.h"
35447 +
35448 +/*
35449 + * Replay flags.
35450 + *
35451 + * REPLAY_DELETION: node was deleted
35452 + * REPLAY_REF: node is a reference node
35453 + */
35454 +enum {
35455 + REPLAY_DELETION = 1,
35456 + REPLAY_REF = 2,
35457 +};
35458 +
35459 +/**
35460 + * struct replay_entry - replay tree entry.
35461 + * @lnum: logical eraseblock number of the node
35462 + * @offs: node offset
35463 + * @len: node length
35464 + * @sqnum: node sequence number
35465 + * @flags: replay flags
35466 + * @rb: links the replay tree
35467 + * @key: node key
35468 + * @nm: directory entry name
35469 + * @old_size: truncation old size
35470 + * @new_size: truncation new size
35471 + * @free: amount of free space in a bud
35472 + * @dirty: amount of dirty space in a bud from padding and deletion nodes
35473 + *
35474 + * UBIFS journal replay must compare node sequence numbers, which means it must
35475 + * build a tree of node information to insert into the TNC.
35476 + */
35477 +struct replay_entry {
35478 + int lnum;
35479 + int offs;
35480 + int len;
35481 + unsigned long long sqnum;
35482 + int flags;
35483 + struct rb_node rb;
35484 + union ubifs_key key;
35485 + union {
35486 + struct qstr nm;
35487 + struct {
35488 + loff_t old_size;
35489 + loff_t new_size;
35490 + };
35491 + struct {
35492 + int free;
35493 + int dirty;
35494 + };
35495 + };
35496 +};
35497 +
35498 +/**
35499 + * struct bud_entry - entry in the list of buds to replay.
35500 + * @list: next bud in the list
35501 + * @bud: bud description object
35502 + * @free: free bytes in the bud
35503 + * @sqnum: reference node sequence number
35504 + */
35505 +struct bud_entry {
35506 + struct list_head list;
35507 + struct ubifs_bud *bud;
35508 + int free;
35509 + unsigned long long sqnum;
35510 +};
35511 +
35512 +/**
35513 + * set_bud_lprops - set free and dirty space used by a bud.
35514 + * @c: UBIFS file-system description object
35515 + * @r: replay entry of bud
35516 + */
35517 +static int set_bud_lprops(struct ubifs_info *c, struct replay_entry *r)
35518 +{
35519 + const struct ubifs_lprops *lp;
35520 + int err = 0, dirty;
35521 +
35522 + ubifs_get_lprops(c);
35523 +
35524 + lp = ubifs_lpt_lookup_dirty(c, r->lnum);
35525 + if (IS_ERR(lp)) {
35526 + err = PTR_ERR(lp);
35527 + goto out;
35528 + }
35529 +
35530 + dirty = lp->dirty;
35531 + if (r->offs == 0 && (lp->free != c->leb_size || lp->dirty != 0)) {
35532 + /*
35533 + * The LEB was added to the journal with a starting offset of
35534 + * zero which means the LEB must have been empty. The LEB
35535 + * property values should be lp->free == c->leb_size and
35536 + * lp->dirty == 0, but that is not the case. The reason is that
35537 + * the LEB was garbage collected. The garbage collector resets
35538 + * the free and dirty space without recording it anywhere except
35539 + * lprops, so if there is not a commit then lprops does not have
35540 + * that information next time the file system is mounted.
35541 + *
35542 + * We do not need to adjust free space because the scan has told
35543 + * us the exact value which is recorded in the replay entry as
35544 + * r->free.
35545 + *
35546 + * However we do need to subtract from the dirty space the
35547 + * amount of space that the garbage collector reclaimed, which
35548 + * is the whole LEB minus the amount of space that was free.
35549 + */
35550 + dbg_mnt("bud LEB %d was GC'd (%d free, %d dirty)", r->lnum,
35551 + lp->free, lp->dirty);
35552 + dbg_gc("bud LEB %d was GC'd (%d free, %d dirty)", r->lnum,
35553 + lp->free, lp->dirty);
35554 + dirty -= c->leb_size - lp->free;
35555 + /*
35556 + * If the replay order was perfect the dirty space would now be
35557 + * zero. The order is not perfect because the the journal heads
35558 + * race with eachother. This is not a problem but is does mean
35559 + * that the dirty space may temporarily exceed c->leb_size
35560 + * during the replay.
35561 + */
35562 + if (dirty != 0)
35563 + dbg_msg("LEB %d lp: %d free %d dirty "
35564 + "replay: %d free %d dirty", r->lnum, lp->free,
35565 + lp->dirty, r->free, r->dirty);
35566 + }
35567 + lp = ubifs_change_lp(c, lp, r->free, dirty + r->dirty,
35568 + lp->flags | LPROPS_TAKEN, 0);
35569 + if (IS_ERR(lp)) {
35570 + err = PTR_ERR(lp);
35571 + goto out;
35572 + }
35573 +out:
35574 + ubifs_release_lprops(c);
35575 + return err;
35576 +}
35577 +
35578 +/**
35579 + * trun_remove_range - apply a replay entry for a truncation to the TNC.
35580 + * @c: UBIFS file-system description object
35581 + * @r: replay entry of truncation
35582 + */
35583 +static int trun_remove_range(struct ubifs_info *c, struct replay_entry *r)
35584 +{
35585 + unsigned min_blk, max_blk;
35586 + union ubifs_key min_key, max_key;
35587 + ino_t ino;
35588 +
35589 + min_blk = r->new_size / UBIFS_BLOCK_SIZE;
35590 + if (r->new_size & (UBIFS_BLOCK_SIZE - 1))
35591 + min_blk += 1;
35592 +
35593 + max_blk = r->old_size / UBIFS_BLOCK_SIZE;
35594 + if ((r->old_size & (UBIFS_BLOCK_SIZE - 1)) == 0)
35595 + max_blk -= 1;
35596 +
35597 + ino = key_inum(c, &r->key);
35598 +
35599 + data_key_init(c, &min_key, ino, min_blk);
35600 + data_key_init(c, &max_key, ino, max_blk);
35601 +
35602 + return ubifs_tnc_remove_range(c, &min_key, &max_key);
35603 +}
35604 +
35605 +/**
35606 + * apply_replay_entry - apply a replay entry to the TNC.
35607 + * @c: UBIFS file-system description object
35608 + * @r: replay entry to apply
35609 + *
35610 + * Apply a replay entry to the TNC.
35611 + */
35612 +static int apply_replay_entry(struct ubifs_info *c, struct replay_entry *r)
35613 +{
35614 + int err, deletion = ((r->flags & REPLAY_DELETION) != 0);
35615 +
35616 + dbg_mnt("LEB %d:%d len %d flgs %d sqnum %llu %s", r->lnum,
35617 + r->offs, r->len, r->flags, r->sqnum, DBGKEY(&r->key));
35618 +
35619 + /* Set c->replay_sqnum to help deal with dangling branches. */
35620 + c->replay_sqnum = r->sqnum;
35621 +
35622 + if (r->flags & REPLAY_REF)
35623 + err = set_bud_lprops(c, r);
35624 + else if (is_hash_key(c, &r->key)) {
35625 + if (deletion)
35626 + err = ubifs_tnc_remove_nm(c, &r->key, &r->nm);
35627 + else
35628 + err = ubifs_tnc_add_nm(c, &r->key, r->lnum, r->offs,
35629 + r->len, &r->nm);
35630 + } else {
35631 + if (deletion)
35632 + switch (key_type(c, &r->key)) {
35633 + case UBIFS_INO_KEY:
35634 + {
35635 + ino_t inum = key_inum(c, &r->key);
35636 +
35637 + err = ubifs_tnc_remove_ino(c, inum);
35638 + break;
35639 + }
35640 + case UBIFS_TRUN_KEY:
35641 + err = trun_remove_range(c, r);
35642 + break;
35643 + default:
35644 + err = ubifs_tnc_remove(c, &r->key);
35645 + break;
35646 + }
35647 + else
35648 + err = ubifs_tnc_add(c, &r->key, r->lnum, r->offs,
35649 + r->len);
35650 + if (err)
35651 + return err;
35652 +
35653 + if (c->need_recovery)
35654 + err = ubifs_recover_size_accum(c, &r->key, deletion,
35655 + r->new_size);
35656 + }
35657 +
35658 + return err;
35659 +}
35660 +
35661 +/**
35662 + * destroy_replay_tree - destroy the replay.
35663 + * @c: UBIFS file-system description object
35664 + *
35665 + * Destroy the replay tree.
35666 + */
35667 +static void destroy_replay_tree(struct ubifs_info *c)
35668 +{
35669 + struct rb_node *this = c->replay_tree.rb_node;
35670 + struct replay_entry *r;
35671 +
35672 + while (this) {
35673 + if (this->rb_left) {
35674 + this = this->rb_left;
35675 + continue;
35676 + } else if (this->rb_right) {
35677 + this = this->rb_right;
35678 + continue;
35679 + }
35680 + r = rb_entry(this, struct replay_entry, rb);
35681 + this = rb_parent(this);
35682 + if (this) {
35683 + if (this->rb_left == &r->rb)
35684 + this->rb_left = NULL;
35685 + else
35686 + this->rb_right = NULL;
35687 + }
35688 + if (is_hash_key(c, &r->key))
35689 + kfree(r->nm.name);
35690 + kfree(r);
35691 + }
35692 + c->replay_tree = RB_ROOT;
35693 +}
35694 +
35695 +/**
35696 + * apply_replay_tree - apply the replay tree to the TNC.
35697 + * @c: UBIFS file-system description object
35698 + *
35699 + * Apply the replay tree.
35700 + * Returns zero in case of success and a negative error code in case of
35701 + * failure.
35702 + */
35703 +static int apply_replay_tree(struct ubifs_info *c)
35704 +{
35705 + struct rb_node *this = rb_first(&c->replay_tree);
35706 +
35707 + while (this) {
35708 + struct replay_entry *r;
35709 + int err;
35710 +
35711 + cond_resched();
35712 +
35713 + r = rb_entry(this, struct replay_entry, rb);
35714 + err = apply_replay_entry(c, r);
35715 + if (err)
35716 + return err;
35717 + this = rb_next(this);
35718 + }
35719 + return 0;
35720 +}
35721 +
35722 +/**
35723 + * insert_node - insert a node to the replay tree.
35724 + * @c: UBIFS file-system description object
35725 + * @lnum: node logical eraseblock number
35726 + * @offs: node offset
35727 + * @len: node length
35728 + * @key: node key
35729 + * @sqnum: sequence number
35730 + * @deletion: non-zero if this is a deletion
35731 + * @used: number of bytes in use in a LEB
35732 + * @old_size: truncation old size
35733 + * @new_size: truncation new size
35734 + *
35735 + * This function inserts a scanned non-direntry node to the replay tree. The
35736 + * replay tree is an RB-tree containing @struct replay_entry elements which are
35737 + * indexed by the sequence number. The replay tree is applied at the very end
35738 + * of the replay process. Since the tree is sorted in sequence number order,
35739 + * the older modifications are applied first. This function returns zero in
35740 + * case of success and a negative error code in case of failure.
35741 + */
35742 +static int insert_node(struct ubifs_info *c, int lnum, int offs, int len,
35743 + union ubifs_key *key, unsigned long long sqnum,
35744 + int deletion, int *used, loff_t old_size,
35745 + loff_t new_size)
35746 +{
35747 + struct rb_node **p = &c->replay_tree.rb_node, *parent = NULL;
35748 + struct replay_entry *r;
35749 +
35750 + if (key_inum(c, key) >= c->highest_inum)
35751 + c->highest_inum = key_inum(c, key);
35752 +
35753 + dbg_mnt("add LEB %d:%d, key %s", lnum, offs, DBGKEY(key));
35754 + while (*p) {
35755 + parent = *p;
35756 + r = rb_entry(parent, struct replay_entry, rb);
35757 + if (sqnum < r->sqnum) {
35758 + p = &(*p)->rb_left;
35759 + continue;
35760 + } else if (sqnum > r->sqnum) {
35761 + p = &(*p)->rb_right;
35762 + continue;
35763 + }
35764 + ubifs_err("duplicate sqnum in replay");
35765 + return -EINVAL;
35766 + }
35767 +
35768 + r = kzalloc(sizeof(struct replay_entry), GFP_KERNEL);
35769 + if (!r)
35770 + return -ENOMEM;
35771 +
35772 + if (!deletion)
35773 + *used += ALIGN(len, 8);
35774 + r->lnum = lnum;
35775 + r->offs = offs;
35776 + r->len = len;
35777 + r->sqnum = sqnum;
35778 + r->flags = (deletion ? REPLAY_DELETION : 0);
35779 + r->old_size = old_size;
35780 + r->new_size = new_size;
35781 + key_copy(c, key, &r->key);
35782 +
35783 + rb_link_node(&r->rb, parent, p);
35784 + rb_insert_color(&r->rb, &c->replay_tree);
35785 + return 0;
35786 +}
35787 +
35788 +/**
35789 + * insert_dent - insert a directory entry node into the replay tree.
35790 + * @c: UBIFS file-system description object
35791 + * @lnum: node logical eraseblock number
35792 + * @offs: node offset
35793 + * @len: node length
35794 + * @key: node key
35795 + * @name: directory entry name
35796 + * @nlen: directory entry name length
35797 + * @sqnum: sequence number
35798 + * @deletion: non-zero if this is a deletion
35799 + * @used: number of bytes in use in a LEB
35800 + *
35801 + * This function inserts a scanned directory entry node to the replay tree.
35802 + * Returns zero in case of success and a negative error code in case of
35803 + * failure.
35804 + *
35805 + * This function is also used for extended attribute entries because they are
35806 + * implemented as directory entry nodes.
35807 + */
35808 +static int insert_dent(struct ubifs_info *c, int lnum, int offs, int len,
35809 + union ubifs_key *key, const char *name, int nlen,
35810 + unsigned long long sqnum, int deletion, int *used)
35811 +{
35812 + struct rb_node **p = &c->replay_tree.rb_node, *parent = NULL;
35813 + struct replay_entry *r;
35814 + char *nbuf;
35815 +
35816 + if (key_inum(c, key) >= c->highest_inum)
35817 + c->highest_inum = key_inum(c, key);
35818 +
35819 + dbg_mnt("add LEB %d:%d, key %s", lnum, offs, DBGKEY(key));
35820 + while (*p) {
35821 + parent = *p;
35822 + r = rb_entry(parent, struct replay_entry, rb);
35823 + if (sqnum < r->sqnum) {
35824 + p = &(*p)->rb_left;
35825 + continue;
35826 + }
35827 + if (sqnum > r->sqnum) {
35828 + p = &(*p)->rb_right;
35829 + continue;
35830 + }
35831 + ubifs_err("duplicate sqnum in replay");
35832 + return -EINVAL;
35833 + }
35834 +
35835 + r = kzalloc(sizeof(struct replay_entry), GFP_KERNEL);
35836 + if (!r)
35837 + return -ENOMEM;
35838 + nbuf = kmalloc(nlen + 1, GFP_KERNEL);
35839 + if (!nbuf) {
35840 + kfree(r);
35841 + return -ENOMEM;
35842 + }
35843 +
35844 + if (!deletion)
35845 + *used += ALIGN(len, 8);
35846 + r->lnum = lnum;
35847 + r->offs = offs;
35848 + r->len = len;
35849 + r->sqnum = sqnum;
35850 + r->nm.len = nlen;
35851 + memcpy(nbuf, name, nlen);
35852 + nbuf[nlen] = '\0';
35853 + r->nm.name = nbuf;
35854 + r->flags = (deletion ? REPLAY_DELETION : 0);
35855 + key_copy(c, key, &r->key);
35856 +
35857 + ubifs_assert(!*p);
35858 + rb_link_node(&r->rb, parent, p);
35859 + rb_insert_color(&r->rb, &c->replay_tree);
35860 + return 0;
35861 +}
35862 +
35863 +/**
35864 + * ubifs_validate_entry - validate directory or extended attribute entry node.
35865 + * @c: UBIFS file-system description object
35866 + * @dent: the node to validate
35867 + *
35868 + * This function validates directory or extended attribute entry node @dent.
35869 + * Returns zero if the node is all right and a %-EINVAL if not.
35870 + */
35871 +int ubifs_validate_entry(struct ubifs_info *c,
35872 + const struct ubifs_dent_node *dent)
35873 +{
35874 + int key_type = key_type_flash(c, dent->key);
35875 + int nlen = le16_to_cpu(dent->nlen);
35876 +
35877 + if (le32_to_cpu(dent->ch.len) != nlen + UBIFS_DENT_NODE_SZ + 1 ||
35878 + dent->type >= UBIFS_ITYPES_CNT ||
35879 + nlen > UBIFS_MAX_NLEN || dent->name[nlen] != 0 ||
35880 + strnlen(dent->name, nlen) != nlen ||
35881 + le64_to_cpu(dent->inum) > MAX_INUM) {
35882 + ubifs_err("bad %s node", key_type == UBIFS_DENT_KEY ?
35883 + "directory entry" : "extended attribute entry");
35884 + return -EINVAL;
35885 + }
35886 +
35887 + if (key_type != UBIFS_DENT_KEY && key_type != UBIFS_XENT_KEY) {
35888 + ubifs_err("bad key type %d", key_type);
35889 + return -EINVAL;
35890 + }
35891 +
35892 + return 0;
35893 +}
35894 +
35895 +/**
35896 + * replay_bud - replay a bud logical eraseblock.
35897 + * @c: UBIFS file-system description object
35898 + * @lnum: bud logical eraseblock number to replay
35899 + * @offs: bud start offset
35900 + * @jhead: journal head to which this bud belongs
35901 + * @free: amount of free space in the bud is returned here
35902 + * @dirty: amount of dirty space from padding and deletion nodes is returned
35903 + * here
35904 + *
35905 + * This function returns zero in case of success and a negative error code in
35906 + * case of failure.
35907 + */
35908 +static int replay_bud(struct ubifs_info *c, int lnum, int offs, int jhead,
35909 + int *free, int *dirty)
35910 +{
35911 + int err = 0, used = 0;
35912 + struct ubifs_scan_leb *sleb;
35913 + struct ubifs_scan_node *snod;
35914 + struct ubifs_bud *bud;
35915 +
35916 + dbg_mnt("replay bud LEB %d, head %d", lnum, jhead);
35917 + if (c->need_recovery)
35918 + sleb = ubifs_recover_leb(c, lnum, offs, c->sbuf, jhead != GCHD);
35919 + else
35920 + sleb = ubifs_scan(c, lnum, offs, c->sbuf);
35921 + if (IS_ERR(sleb))
35922 + return PTR_ERR(sleb);
35923 +
35924 + /*
35925 + * The bud does not have to start from offset zero - the beginning of
35926 + * the 'lnum' LEB may contain previously committed data. One of the
35927 + * things we have to do in replay is to correctly update lprops with
35928 + * newer information about this LEB.
35929 + *
35930 + * At this point lprops thinks that this LEB has 'c->leb_size - offs'
35931 + * bytes of free space because it only contain information about
35932 + * committed data.
35933 + *
35934 + * But we know that real amount of free space is 'c->leb_size -
35935 + * sleb->endpt', and the space in the 'lnum' LEB between 'offs' and
35936 + * 'sleb->endpt' is used by bud data. We have to correctly calculate
35937 + * how much of these data are dirty and update lprops with this
35938 + * information.
35939 + *
35940 + * The dirt in that LEB region is comprised of padding nodes, deletion
35941 + * nodes, truncation nodes and nodes which are obsoleted by subsequent
35942 + * nodes in this LEB. So instead of calculating clean space, we
35943 + * calculate used space ('used' variable).
35944 + */
35945 +
35946 + list_for_each_entry(snod, &sleb->nodes, list) {
35947 + int deletion = 0;
35948 +
35949 + cond_resched();
35950 +
35951 + if (snod->sqnum >= SQNUM_WATERMARK) {
35952 + ubifs_err("file system's life ended");
35953 + goto out_dump;
35954 + }
35955 +
35956 + if (snod->sqnum > c->max_sqnum)
35957 + c->max_sqnum = snod->sqnum;
35958 +
35959 + switch (snod->type) {
35960 + case UBIFS_INO_NODE:
35961 + {
35962 + struct ubifs_ino_node *ino = snod->node;
35963 + loff_t new_size = le64_to_cpu(ino->size);
35964 +
35965 + if (le32_to_cpu(ino->nlink) == 0)
35966 + deletion = 1;
35967 + err = insert_node(c, lnum, snod->offs, snod->len,
35968 + &snod->key, snod->sqnum, deletion,
35969 + &used, 0, new_size);
35970 + break;
35971 + }
35972 + case UBIFS_DATA_NODE:
35973 + {
35974 + struct ubifs_data_node *dn = snod->node;
35975 + loff_t new_size = le32_to_cpu(dn->size) +
35976 + key_block(c, &snod->key) *
35977 + UBIFS_BLOCK_SIZE;
35978 +
35979 + err = insert_node(c, lnum, snod->offs, snod->len,
35980 + &snod->key, snod->sqnum, deletion,
35981 + &used, 0, new_size);
35982 + break;
35983 + }
35984 + case UBIFS_DENT_NODE:
35985 + case UBIFS_XENT_NODE:
35986 + {
35987 + struct ubifs_dent_node *dent = snod->node;
35988 +
35989 + err = ubifs_validate_entry(c, dent);
35990 + if (err)
35991 + goto out_dump;
35992 +
35993 + err = insert_dent(c, lnum, snod->offs, snod->len,
35994 + &snod->key, dent->name,
35995 + le16_to_cpu(dent->nlen), snod->sqnum,
35996 + !le64_to_cpu(dent->inum), &used);
35997 + break;
35998 + }
35999 + case UBIFS_TRUN_NODE:
36000 + {
36001 + struct ubifs_trun_node *trun = snod->node;
36002 + loff_t old_size = le64_to_cpu(trun->old_size);
36003 + loff_t new_size = le64_to_cpu(trun->new_size);
36004 + union ubifs_key key;
36005 +
36006 + /* Validate truncation node */
36007 + if (old_size < 0 || old_size > c->max_inode_sz ||
36008 + new_size < 0 || new_size > c->max_inode_sz ||
36009 + old_size <= new_size) {
36010 + ubifs_err("bad truncation node");
36011 + goto out_dump;
36012 + }
36013 +
36014 + /*
36015 + * Create a fake truncation key just to use the same
36016 + * functions which expect nodes to have keys.
36017 + */
36018 + trun_key_init(c, &key, le32_to_cpu(trun->inum));
36019 + err = insert_node(c, lnum, snod->offs, snod->len,
36020 + &key, snod->sqnum, 1, &used,
36021 + old_size, new_size);
36022 + break;
36023 + }
36024 + default:
36025 + ubifs_err("unexpected node type %d in bud LEB %d:%d",
36026 + snod->type, lnum, snod->offs);
36027 + err = -EINVAL;
36028 + goto out_dump;
36029 + }
36030 + if (err)
36031 + goto out;
36032 + }
36033 +
36034 + bud = ubifs_search_bud(c, lnum);
36035 + if (!bud)
36036 + BUG();
36037 +
36038 + ubifs_assert(sleb->endpt - offs >= used);
36039 + ubifs_assert(sleb->endpt % c->min_io_size == 0);
36040 +
36041 + if (sleb->endpt + c->min_io_size <= c->leb_size &&
36042 + !(c->vfs_sb->s_flags & MS_RDONLY))
36043 + err = ubifs_wbuf_seek_nolock(&c->jheads[jhead].wbuf, lnum,
36044 + sleb->endpt, UBI_SHORTTERM);
36045 +
36046 + *dirty = sleb->endpt - offs - used;
36047 + *free = c->leb_size - sleb->endpt;
36048 +
36049 +out:
36050 + ubifs_scan_destroy(sleb);
36051 + return err;
36052 +
36053 +out_dump:
36054 + ubifs_err("bad node is at LEB %d:%d", lnum, snod->offs);
36055 + dbg_dump_node(c, snod->node);
36056 + ubifs_scan_destroy(sleb);
36057 + return -EINVAL;
36058 +}
36059 +
36060 +/**
36061 + * insert_ref_node - insert a reference node to the replay tree.
36062 + * @c: UBIFS file-system description object
36063 + * @lnum: node logical eraseblock number
36064 + * @offs: node offset
36065 + * @sqnum: sequence number
36066 + * @free: amount of free space in bud
36067 + * @dirty: amount of dirty space from padding and deletion nodes
36068 + *
36069 + * This function inserts a reference node to the replay tree and returns zero
36070 + * in case of success ort a negative error code in case of failure.
36071 + */
36072 +static int insert_ref_node(struct ubifs_info *c, int lnum, int offs,
36073 + unsigned long long sqnum, int free, int dirty)
36074 +{
36075 + struct rb_node **p = &c->replay_tree.rb_node, *parent = NULL;
36076 + struct replay_entry *r;
36077 +
36078 + dbg_mnt("add ref LEB %d:%d", lnum, offs);
36079 + while (*p) {
36080 + parent = *p;
36081 + r = rb_entry(parent, struct replay_entry, rb);
36082 + if (sqnum < r->sqnum) {
36083 + p = &(*p)->rb_left;
36084 + continue;
36085 + } else if (sqnum > r->sqnum) {
36086 + p = &(*p)->rb_right;
36087 + continue;
36088 + }
36089 + ubifs_err("duplicate sqnum in replay tree");
36090 + return -EINVAL;
36091 + }
36092 +
36093 + r = kzalloc(sizeof(struct replay_entry), GFP_KERNEL);
36094 + if (!r)
36095 + return -ENOMEM;
36096 +
36097 + r->lnum = lnum;
36098 + r->offs = offs;
36099 + r->sqnum = sqnum;
36100 + r->flags = REPLAY_REF;
36101 + r->free = free;
36102 + r->dirty = dirty;
36103 +
36104 + rb_link_node(&r->rb, parent, p);
36105 + rb_insert_color(&r->rb, &c->replay_tree);
36106 + return 0;
36107 +}
36108 +
36109 +/**
36110 + * replay_buds - replay all buds.
36111 + * @c: UBIFS file-system description object
36112 + *
36113 + * This function returns zero in case of success and a negative error code in
36114 + * case of failure.
36115 + */
36116 +static int replay_buds(struct ubifs_info *c)
36117 +{
36118 + struct bud_entry *b;
36119 + int err, uninitialized_var(free), uninitialized_var(dirty);
36120 +
36121 + list_for_each_entry(b, &c->replay_buds, list) {
36122 + err = replay_bud(c, b->bud->lnum, b->bud->start, b->bud->jhead,
36123 + &free, &dirty);
36124 + if (err)
36125 + return err;
36126 + err = insert_ref_node(c, b->bud->lnum, b->bud->start, b->sqnum,
36127 + free, dirty);
36128 + if (err)
36129 + return err;
36130 + }
36131 +
36132 + return 0;
36133 +}
36134 +
36135 +/**
36136 + * destroy_bud_list - destroy the list of buds to replay.
36137 + * @c: UBIFS file-system description object
36138 + */
36139 +static void destroy_bud_list(struct ubifs_info *c)
36140 +{
36141 + struct bud_entry *b;
36142 +
36143 + while (!list_empty(&c->replay_buds)) {
36144 + b = list_entry(c->replay_buds.next, struct bud_entry, list);
36145 + list_del(&b->list);
36146 + kfree(b);
36147 + }
36148 +}
36149 +
36150 +/**
36151 + * add_replay_bud - add a bud to the list of buds to replay.
36152 + * @c: UBIFS file-system description object
36153 + * @lnum: bud logical eraseblock number to replay
36154 + * @offs: bud start offset
36155 + * @jhead: journal head to which this bud belongs
36156 + * @sqnum: reference node sequence number
36157 + *
36158 + * This function returns zero in case of success and a negative error code in
36159 + * case of failure.
36160 + */
36161 +static int add_replay_bud(struct ubifs_info *c, int lnum, int offs, int jhead,
36162 + unsigned long long sqnum)
36163 +{
36164 + struct ubifs_bud *bud;
36165 + struct bud_entry *b;
36166 +
36167 + dbg_mnt("add replay bud LEB %d:%d, head %d", lnum, offs, jhead);
36168 +
36169 + bud = kmalloc(sizeof(struct ubifs_bud), GFP_KERNEL);
36170 + if (!bud)
36171 + return -ENOMEM;
36172 +
36173 + b = kmalloc(sizeof(struct bud_entry), GFP_KERNEL);
36174 + if (!b) {
36175 + kfree(bud);
36176 + return -ENOMEM;
36177 + }
36178 +
36179 + bud->lnum = lnum;
36180 + bud->start = offs;
36181 + bud->jhead = jhead;
36182 + ubifs_add_bud(c, bud);
36183 +
36184 + b->bud = bud;
36185 + b->sqnum = sqnum;
36186 + list_add_tail(&b->list, &c->replay_buds);
36187 +
36188 + return 0;
36189 +}
36190 +
36191 +/**
36192 + * validate_ref - validate a reference node.
36193 + * @c: UBIFS file-system description object
36194 + * @ref: the reference node to validate
36195 + * @ref_lnum: LEB number of the reference node
36196 + * @ref_offs: reference node offset
36197 + *
36198 + * This function returns %1 if a bud reference already exists for the LEB. %0 is
36199 + * returned if the reference node is new, otherwise %-EINVAL is returned if
36200 + * validation failed.
36201 + */
36202 +static int validate_ref(struct ubifs_info *c, const struct ubifs_ref_node *ref)
36203 +{
36204 + struct ubifs_bud *bud;
36205 + int lnum = le32_to_cpu(ref->lnum);
36206 + unsigned int offs = le32_to_cpu(ref->offs);
36207 + unsigned int jhead = le32_to_cpu(ref->jhead);
36208 +
36209 + /*
36210 + * ref->offs may point to the end of LEB when the journal head points
36211 + * to the end of LEB and we write reference node for it during commit.
36212 + * So this is why we require 'offs > c->leb_size'.
36213 + */
36214 + if (jhead >= c->jhead_cnt || lnum >= c->leb_cnt ||
36215 + lnum < c->main_first || offs > c->leb_size ||
36216 + offs & (c->min_io_size - 1))
36217 + return -EINVAL;
36218 +
36219 + /* Make sure we have not already looked at this bud */
36220 + bud = ubifs_search_bud(c, lnum);
36221 + if (bud) {
36222 + if (bud->jhead == jhead && bud->start <= offs)
36223 + return 1;
36224 + ubifs_err("bud at LEB %d:%d was already referred", lnum, offs);
36225 + return -EINVAL;
36226 + }
36227 +
36228 + return 0;
36229 +}
36230 +
36231 +/**
36232 + * replay_log_leb - replay a log logical eraseblock.
36233 + * @c: UBIFS file-system description object
36234 + * @lnum: log logical eraseblock to replay
36235 + * @offs: offset to start replaying from
36236 + * @sbuf: scan buffer
36237 + *
36238 + * This function replays a log LEB and returns zero in case of success, %1 if
36239 + * this is the last LEB in the log, and a negative error code in case of
36240 + * failure.
36241 + */
36242 +static int replay_log_leb(struct ubifs_info *c, int lnum, int offs, void *sbuf)
36243 +{
36244 + int err;
36245 + struct ubifs_scan_leb *sleb;
36246 + struct ubifs_scan_node *snod;
36247 + const struct ubifs_cs_node *node;
36248 +
36249 + dbg_mnt("replay log LEB %d:%d", lnum, offs);
36250 + sleb = ubifs_scan(c, lnum, offs, sbuf);
36251 + if (IS_ERR(sleb)) {
36252 + if (c->need_recovery)
36253 + sleb = ubifs_recover_log_leb(c, lnum, offs, sbuf);
36254 + if (IS_ERR(sleb))
36255 + return PTR_ERR(sleb);
36256 + }
36257 +
36258 + if (sleb->nodes_cnt == 0) {
36259 + err = 1;
36260 + goto out;
36261 + }
36262 +
36263 + node = sleb->buf;
36264 +
36265 + snod = list_entry(sleb->nodes.next, struct ubifs_scan_node, list);
36266 + if (c->cs_sqnum == 0) {
36267 + /*
36268 + * This is the first log LEB we are looking at, make sure that
36269 + * the first node is a commit start node. Also record its
36270 + * sequence number so that UBIFS can determine where the log
36271 + * ends, because all nodes which were have higher sequence
36272 + * numbers.
36273 + */
36274 + if (snod->type != UBIFS_CS_NODE) {
36275 + dbg_err("first log node at LEB %d:%d is not CS node",
36276 + lnum, offs);
36277 + goto out_dump;
36278 + }
36279 + if (le64_to_cpu(node->cmt_no) != c->cmt_no) {
36280 + dbg_err("first CS node at LEB %d:%d has wrong "
36281 + "commit number %llu expected %llu",
36282 + lnum, offs,
36283 + (unsigned long long)le64_to_cpu(node->cmt_no),
36284 + c->cmt_no);
36285 + goto out_dump;
36286 + }
36287 +
36288 + c->cs_sqnum = le64_to_cpu(node->ch.sqnum);
36289 + dbg_mnt("commit start sqnum %llu", c->cs_sqnum);
36290 + }
36291 +
36292 + if (snod->sqnum < c->cs_sqnum) {
36293 + /*
36294 + * This means that we reached end of log and now
36295 + * look to the older log data, which was already
36296 + * committed but the eraseblock was not erased (UBIFS
36297 + * only unmaps it). So this basically means we have to
36298 + * exit with "end of log" code.
36299 + */
36300 + err = 1;
36301 + goto out;
36302 + }
36303 +
36304 + /* Make sure the first node sits at offset zero of the LEB */
36305 + if (snod->offs != 0) {
36306 + dbg_err("first node is not at zero offset");
36307 + goto out_dump;
36308 + }
36309 +
36310 + list_for_each_entry(snod, &sleb->nodes, list) {
36311 +
36312 + cond_resched();
36313 +
36314 + if (snod->sqnum >= SQNUM_WATERMARK) {
36315 + ubifs_err("file system's life ended");
36316 + goto out_dump;
36317 + }
36318 +
36319 + if (snod->sqnum < c->cs_sqnum) {
36320 + dbg_err("bad sqnum %llu, commit sqnum %llu",
36321 + snod->sqnum, c->cs_sqnum);
36322 + goto out_dump;
36323 + }
36324 +
36325 + if (snod->sqnum > c->max_sqnum)
36326 + c->max_sqnum = snod->sqnum;
36327 +
36328 + switch (snod->type) {
36329 + case UBIFS_REF_NODE: {
36330 + const struct ubifs_ref_node *ref = snod->node;
36331 +
36332 + err = validate_ref(c, ref);
36333 + if (err == 1)
36334 + break; /* Already have this bud */
36335 + if (err)
36336 + goto out_dump;
36337 +
36338 + err = add_replay_bud(c, le32_to_cpu(ref->lnum),
36339 + le32_to_cpu(ref->offs),
36340 + le32_to_cpu(ref->jhead),
36341 + snod->sqnum);
36342 + if (err)
36343 + goto out;
36344 +
36345 + break;
36346 + }
36347 + case UBIFS_CS_NODE:
36348 + /* Make sure it sits at the beginning of LEB */
36349 + if (snod->offs != 0) {
36350 + ubifs_err("unexpected node in log");
36351 + goto out_dump;
36352 + }
36353 + break;
36354 + default:
36355 + ubifs_err("unexpected node in log");
36356 + goto out_dump;
36357 + }
36358 + }
36359 +
36360 + if (sleb->endpt || c->lhead_offs >= c->leb_size) {
36361 + c->lhead_lnum = lnum;
36362 + c->lhead_offs = sleb->endpt;
36363 + }
36364 +
36365 + err = !sleb->endpt;
36366 +out:
36367 + ubifs_scan_destroy(sleb);
36368 + return err;
36369 +
36370 +out_dump:
36371 + ubifs_err("log error detected while replying the log at LEB %d:%d",
36372 + lnum, offs + snod->offs);
36373 + dbg_dump_node(c, snod->node);
36374 + ubifs_scan_destroy(sleb);
36375 + return -EINVAL;
36376 +}
36377 +
36378 +/**
36379 + * take_ihead - update the status of the index head in lprops to 'taken'.
36380 + * @c: UBIFS file-system description object
36381 + *
36382 + * This function returns the amount of free space in the index head LEB or a
36383 + * negative error code.
36384 + */
36385 +static int take_ihead(struct ubifs_info *c)
36386 +{
36387 + const struct ubifs_lprops *lp;
36388 + int err, free;
36389 +
36390 + ubifs_get_lprops(c);
36391 +
36392 + lp = ubifs_lpt_lookup_dirty(c, c->ihead_lnum);
36393 + if (IS_ERR(lp)) {
36394 + err = PTR_ERR(lp);
36395 + goto out;
36396 + }
36397 +
36398 + free = lp->free;
36399 +
36400 + lp = ubifs_change_lp(c, lp, LPROPS_NC, LPROPS_NC,
36401 + lp->flags | LPROPS_TAKEN, 0);
36402 + if (IS_ERR(lp)) {
36403 + err = PTR_ERR(lp);
36404 + goto out;
36405 + }
36406 +
36407 + err = free;
36408 +out:
36409 + ubifs_release_lprops(c);
36410 + return err;
36411 +}
36412 +
36413 +/**
36414 + * ubifs_replay_journal - replay journal.
36415 + * @c: UBIFS file-system description object
36416 + *
36417 + * This function scans the journal, replays and cleans it up. It makes sure all
36418 + * memory data structures related to uncommitted journal are built (dirty TNC
36419 + * tree, tree of buds, modified lprops, etc).
36420 + */
36421 +int ubifs_replay_journal(struct ubifs_info *c)
36422 +{
36423 + int err, i, lnum, offs, free;
36424 + void *sbuf = NULL;
36425 +
36426 + BUILD_BUG_ON(UBIFS_TRUN_KEY > 5);
36427 +
36428 + /* Update the status of the index head in lprops to 'taken' */
36429 + free = take_ihead(c);
36430 + if (free < 0)
36431 + return free; /* Error code */
36432 +
36433 + if (c->ihead_offs != c->leb_size - free) {
36434 + ubifs_err("bad index head LEB %d:%d", c->ihead_lnum,
36435 + c->ihead_offs);
36436 + return -EINVAL;
36437 + }
36438 +
36439 + sbuf = vmalloc(c->leb_size);
36440 + if (!sbuf)
36441 + return -ENOMEM;
36442 +
36443 + dbg_mnt("start replaying the journal");
36444 +
36445 + c->replaying = 1;
36446 +
36447 + lnum = c->ltail_lnum = c->lhead_lnum;
36448 + offs = c->lhead_offs;
36449 +
36450 + for (i = 0; i < c->log_lebs; i++, lnum++) {
36451 + if (lnum >= UBIFS_LOG_LNUM + c->log_lebs) {
36452 + /*
36453 + * The log is logically circular, we reached the last
36454 + * LEB, switch to the first one.
36455 + */
36456 + lnum = UBIFS_LOG_LNUM;
36457 + offs = 0;
36458 + }
36459 + err = replay_log_leb(c, lnum, offs, sbuf);
36460 + if (err == 1)
36461 + /* We hit the end of the log */
36462 + break;
36463 + if (err)
36464 + goto out;
36465 + offs = 0;
36466 + }
36467 +
36468 + err = replay_buds(c);
36469 + if (err)
36470 + goto out;
36471 +
36472 + err = apply_replay_tree(c);
36473 + if (err)
36474 + goto out;
36475 +
36476 + ubifs_assert(c->bud_bytes <= c->max_bud_bytes || c->need_recovery);
36477 + dbg_mnt("finished, log head LEB %d:%d, max_sqnum %llu, "
36478 + "highest_inum %lu", c->lhead_lnum, c->lhead_offs, c->max_sqnum,
36479 + c->highest_inum);
36480 +out:
36481 + destroy_replay_tree(c);
36482 + destroy_bud_list(c);
36483 + vfree(sbuf);
36484 + c->replaying = 0;
36485 + return err;
36486 +}
36487 diff --exclude=.git -urN linux-2.6.25.6/fs/ubifs/sb.c avr32-2.6/fs/ubifs/sb.c
36488 --- linux-2.6.25.6/fs/ubifs/sb.c 1970-01-01 01:00:00.000000000 +0100
36489 +++ avr32-2.6/fs/ubifs/sb.c 2008-06-12 15:09:45.515816461 +0200
36490 @@ -0,0 +1,618 @@
36491 +/*
36492 + * This file is part of UBIFS.
36493 + *
36494 + * Copyright (C) 2006-2008 Nokia Corporation.
36495 + *
36496 + * This program is free software; you can redistribute it and/or modify it
36497 + * under the terms of the GNU General Public License version 2 as published by
36498 + * the Free Software Foundation.
36499 + *
36500 + * This program is distributed in the hope that it will be useful, but WITHOUT
36501 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
36502 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
36503 + * more details.
36504 + *
36505 + * You should have received a copy of the GNU General Public License along with
36506 + * this program; if not, write to the Free Software Foundation, Inc., 51
36507 + * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
36508 + *
36509 + * Authors: Artem Bityutskiy (Битюцкий Артём)
36510 + * Adrian Hunter
36511 + */
36512 +
36513 +/*
36514 + * This file implements UBIFS superblock. The superblock is stored at the first
36515 + * LEB of the volume and is never changed by UBIFS. Only user-space tools may
36516 + * change it. The superblock node mostly contains geometry information.
36517 + */
36518 +
36519 +#include "ubifs.h"
36520 +#include <linux/random.h>
36521 +
36522 +/*
36523 + * Default journal size in logical eraseblocks as a percent of total
36524 + * flash size.
36525 + */
36526 +#define DEFAULT_JNL_PERCENT 5
36527 +
36528 +/* Default maximum journal size in bytes */
36529 +#define DEFAULT_MAX_JNL (32*1024*1024)
36530 +
36531 +/* Default indexing tree fanout */
36532 +#define DEFAULT_FANOUT 8
36533 +
36534 +/* Default number of LEBs for orphan information */
36535 +#ifdef CONFIG_UBIFS_FS_DEBUG
36536 +#define DEFAULT_ORPHAN_LEBS 2 /* 2 is better for testing */
36537 +#else
36538 +#define DEFAULT_ORPHAN_LEBS 1
36539 +#endif
36540 +
36541 +/* Default number of journal heads */
36542 +#define DEFAULT_JHEADS_CNT 1
36543 +
36544 +/* Default positions of different LEBs in the main area */
36545 +#define DEFAULT_IDX_LEB 0
36546 +#define DEFAULT_DATA_LEB 1
36547 +#define DEFAULT_GC_LEB 2
36548 +
36549 +/* Default number of LEB numbers in LPT's save table */
36550 +#define DEFAULT_LSAVE_CNT 256
36551 +
36552 +/* Default reserved pool size as a percent of maximum free space */
36553 +#define DEFAULT_RP_PERCENT 5
36554 +
36555 +/* The default maximum size of reserved pool in bytes */
36556 +#define DEFAULT_MAX_RP_SIZE (5*1024*1024)
36557 +
36558 +/* Default UBIFS compressor */
36559 +#define DEFAULT_COMPRESSOR UBIFS_COMPR_LZO
36560 +
36561 +/* Default time granularity in nanoseconds */
36562 +#define DEFAULT_TIME_GRAN 1000000000
36563 +
36564 +/**
36565 + * create_default_filesystem - format empty UBI volume.
36566 + * @c: UBIFS file-system description object
36567 + *
36568 + * This function creates default empty file-system. Returns zero in case of
36569 + * success and a negative error code in case of failure.
36570 + */
36571 +static int create_default_filesystem(struct ubifs_info *c)
36572 +{
36573 + struct ubifs_sb_node *sup;
36574 + struct ubifs_mst_node *mst;
36575 + struct ubifs_idx_node *idx;
36576 + struct ubifs_branch *br;
36577 + struct ubifs_ino_node *ino;
36578 + struct ubifs_cs_node *cs;
36579 + union ubifs_key key;
36580 + int err, tmp, jnl_lebs, log_lebs, max_buds, main_lebs, main_first;
36581 + int lpt_lebs, lpt_first, orph_lebs, big_lpt, ino_waste, sup_flags = 0;
36582 + uint64_t tmp64, main_bytes;
36583 +
36584 + /* Some functions called from here depend on the @c->key_len filed */
36585 + c->key_len = UBIFS_SK_LEN;
36586 +
36587 + /*
36588 + * First of all, we have to calculate default file-system geometry -
36589 + * log size, journal size, etc.
36590 + */
36591 + c->max_leb_cnt = c->leb_cnt;
36592 + if (c->leb_cnt < 0x7FFFFFFF / DEFAULT_JNL_PERCENT)
36593 + /* We can first multiply then divide and have no overflow */
36594 + jnl_lebs = c->leb_cnt * DEFAULT_JNL_PERCENT / 100;
36595 + else
36596 + jnl_lebs = (c->leb_cnt / 100) * DEFAULT_JNL_PERCENT;
36597 +
36598 + if (jnl_lebs < UBIFS_MIN_JNL_LEBS)
36599 + jnl_lebs = UBIFS_MIN_JNL_LEBS;
36600 + if (jnl_lebs * c->leb_size > DEFAULT_MAX_JNL)
36601 + jnl_lebs = DEFAULT_MAX_JNL / c->leb_size;
36602 +
36603 + /*
36604 + * The log should be large enough to fit reference nodes for all bud
36605 + * LEBs. Because buds do not have to start from the beginning of LEBs
36606 + * (half of the LEB may contain committed data), the log should
36607 + * generally be larger, make it twice as large.
36608 + */
36609 + tmp = 2 * (c->ref_node_alsz * jnl_lebs) + c->leb_size - 1;
36610 + log_lebs = tmp / c->leb_size;
36611 + /* Plus one LEB reserved for commit */
36612 + log_lebs += 1;
36613 + /* And some extra space to allow writes while committing */
36614 + log_lebs += 1;
36615 +
36616 + max_buds = jnl_lebs - log_lebs;
36617 + if (max_buds < UBIFS_MIN_BUD_LEBS)
36618 + max_buds = UBIFS_MIN_BUD_LEBS;
36619 +
36620 + /*
36621 + * Orphan nodes are stored in a separate area. One node can store a lot
36622 + * of orphan inode numbers, but when new orphan comes we just add a new
36623 + * orphan node. At some point the nodes are consolidated into one
36624 + * orphan node.
36625 + */
36626 + orph_lebs = DEFAULT_ORPHAN_LEBS;
36627 +
36628 + main_lebs = c->leb_cnt - UBIFS_SB_LEBS - UBIFS_MST_LEBS - log_lebs;
36629 + main_lebs -= orph_lebs;
36630 +
36631 + lpt_first = UBIFS_LOG_LNUM + log_lebs;
36632 + c->lsave_cnt = DEFAULT_LSAVE_CNT;
36633 + err = ubifs_create_dflt_lpt(c, &main_lebs, lpt_first, &lpt_lebs,
36634 + &big_lpt);
36635 + if (err)
36636 + return err;
36637 +
36638 + dbg_gen("LEB Properties Tree created (LEBs %d-%d)", lpt_first,
36639 + lpt_first + lpt_lebs - 1);
36640 +
36641 + main_first = c->leb_cnt - main_lebs;
36642 +
36643 + /* Create default superblock */
36644 + tmp = ALIGN(UBIFS_SB_NODE_SZ, c->min_io_size);
36645 + sup = kzalloc(tmp, GFP_KERNEL);
36646 + if (!sup)
36647 + return -ENOMEM;
36648 +
36649 + tmp64 = (uint64_t)max_buds * c->leb_size;
36650 + if (big_lpt)
36651 + sup_flags |= UBIFS_FLG_BIGLPT;
36652 +
36653 + sup->ch.node_type = UBIFS_SB_NODE;
36654 + sup->key_hash = UBIFS_KEY_HASH_R5;
36655 + sup->flags = cpu_to_le32(sup_flags);
36656 + sup->min_io_size = cpu_to_le32(c->min_io_size);
36657 + sup->leb_size = cpu_to_le32(c->leb_size);
36658 + sup->leb_cnt = cpu_to_le32(c->leb_cnt);
36659 + sup->max_leb_cnt = cpu_to_le32(c->max_leb_cnt);
36660 + sup->max_bud_bytes = cpu_to_le64(tmp64);
36661 + sup->log_lebs = cpu_to_le32(log_lebs);
36662 + sup->lpt_lebs = cpu_to_le32(lpt_lebs);
36663 + sup->orph_lebs = cpu_to_le32(orph_lebs);
36664 + sup->jhead_cnt = cpu_to_le32(DEFAULT_JHEADS_CNT);
36665 + sup->fanout = cpu_to_le32(DEFAULT_FANOUT);
36666 + sup->lsave_cnt = cpu_to_le32(c->lsave_cnt);
36667 + sup->fmt_version = cpu_to_le32(UBIFS_FORMAT_VERSION);
36668 + sup->default_compr = cpu_to_le16(DEFAULT_COMPRESSOR);
36669 + sup->time_gran = cpu_to_le32(DEFAULT_TIME_GRAN);
36670 +
36671 + generate_random_uuid(sup->uuid);
36672 +
36673 + main_bytes = (uint64_t)main_lebs * c->leb_size;
36674 + tmp64 = main_bytes * DEFAULT_RP_PERCENT;
36675 + do_div(tmp64, 100);
36676 + if (tmp64 > DEFAULT_MAX_RP_SIZE)
36677 + tmp64 = DEFAULT_MAX_RP_SIZE;
36678 + sup->rp_size = cpu_to_le64(tmp64);
36679 +
36680 + err = ubifs_write_node(c, sup, UBIFS_SB_NODE_SZ, 0, 0, UBI_LONGTERM);
36681 + kfree(sup);
36682 + if (err)
36683 + return err;
36684 +
36685 + dbg_gen("default superblock created at LEB 0:0");
36686 +
36687 + /* Create default master node */
36688 + mst = kzalloc(c->mst_node_alsz, GFP_KERNEL);
36689 + if (!mst)
36690 + return -ENOMEM;
36691 +
36692 + mst->ch.node_type = UBIFS_MST_NODE;
36693 + mst->log_lnum = cpu_to_le32(UBIFS_LOG_LNUM);
36694 + mst->highest_inum = cpu_to_le64(UBIFS_FIRST_INO);
36695 + mst->cmt_no = 0;
36696 + mst->root_lnum = cpu_to_le32(main_first + DEFAULT_IDX_LEB);
36697 + mst->root_offs = 0;
36698 + tmp = ubifs_idx_node_sz(c, 1);
36699 + mst->root_len = cpu_to_le32(tmp);
36700 + mst->gc_lnum = cpu_to_le32(main_first + DEFAULT_GC_LEB);
36701 + mst->ihead_lnum = cpu_to_le32(main_first + DEFAULT_IDX_LEB);
36702 + mst->ihead_offs = cpu_to_le32(ALIGN(tmp, c->min_io_size));
36703 + mst->index_size = cpu_to_le64(ALIGN(tmp, 8));
36704 + mst->lpt_lnum = cpu_to_le32(c->lpt_lnum);
36705 + mst->lpt_offs = cpu_to_le32(c->lpt_offs);
36706 + mst->nhead_lnum = cpu_to_le32(c->nhead_lnum);
36707 + mst->nhead_offs = cpu_to_le32(c->nhead_offs);
36708 + mst->ltab_lnum = cpu_to_le32(c->ltab_lnum);
36709 + mst->ltab_offs = cpu_to_le32(c->ltab_offs);
36710 + mst->lsave_lnum = cpu_to_le32(c->lsave_lnum);
36711 + mst->lsave_offs = cpu_to_le32(c->lsave_offs);
36712 + mst->lscan_lnum = cpu_to_le32(main_first);
36713 + mst->empty_lebs = cpu_to_le32(main_lebs - 2);
36714 + mst->idx_lebs = cpu_to_le32(1);
36715 + mst->leb_cnt = cpu_to_le32(c->leb_cnt);
36716 +
36717 + /* Calculate lprops statistics */
36718 + tmp64 = main_bytes;
36719 + tmp64 -= ALIGN(ubifs_idx_node_sz(c, 1), c->min_io_size);
36720 + tmp64 -= ALIGN(UBIFS_INO_NODE_SZ, c->min_io_size);
36721 + mst->total_free = cpu_to_le64(tmp64);
36722 +
36723 + tmp64 = ALIGN(ubifs_idx_node_sz(c, 1), c->min_io_size);
36724 + ino_waste = ALIGN(UBIFS_INO_NODE_SZ, c->min_io_size) -
36725 + UBIFS_INO_NODE_SZ;
36726 + tmp64 += ino_waste;
36727 + tmp64 -= ALIGN(ubifs_idx_node_sz(c, 1), 8);
36728 + mst->total_dirty = cpu_to_le64(tmp64);
36729 +
36730 + /* The indexing LEB does not contribute to dark space */
36731 + tmp64 = (c->main_lebs - 1) * c->dark_wm;
36732 + mst->total_dark = cpu_to_le64(tmp64);
36733 +
36734 + mst->total_used = cpu_to_le64(UBIFS_INO_NODE_SZ);
36735 +
36736 + err = ubifs_write_node(c, mst, UBIFS_MST_NODE_SZ, UBIFS_MST_LNUM, 0,
36737 + UBI_UNKNOWN);
36738 + if (err) {
36739 + kfree(mst);
36740 + return err;
36741 + }
36742 + err = ubifs_write_node(c, mst, UBIFS_MST_NODE_SZ, UBIFS_MST_LNUM + 1, 0,
36743 + UBI_UNKNOWN);
36744 + kfree(mst);
36745 + if (err)
36746 + return err;
36747 +
36748 + dbg_gen("default master node created at LEB %d:0", UBIFS_MST_LNUM);
36749 +
36750 + /* Create the root indexing node */
36751 + tmp = ubifs_idx_node_sz(c, 1);
36752 + idx = kzalloc(ALIGN(tmp, c->min_io_size), GFP_KERNEL);
36753 + if (!idx)
36754 + return -ENOMEM;
36755 +
36756 + c->key_fmt = UBIFS_SIMPLE_KEY_FMT;
36757 + c->key_hash = key_r5_hash;
36758 +
36759 + idx->ch.node_type = UBIFS_IDX_NODE;
36760 + idx->child_cnt = cpu_to_le16(1);
36761 + ino_key_init(c, &key, UBIFS_ROOT_INO);
36762 + br = ubifs_idx_branch(c, idx, 0);
36763 + key_write_idx(c, &key, &br->key);
36764 + br->lnum = cpu_to_le32(main_first + DEFAULT_DATA_LEB);
36765 + br->len = cpu_to_le32(UBIFS_INO_NODE_SZ);
36766 + err = ubifs_write_node(c, idx, tmp, main_first + DEFAULT_IDX_LEB, 0,
36767 + UBI_UNKNOWN);
36768 + kfree(idx);
36769 + if (err)
36770 + return err;
36771 +
36772 + dbg_gen("default root indexing node created LEB %d:0",
36773 + main_first + DEFAULT_IDX_LEB);
36774 +
36775 + /* Create default root inode */
36776 + tmp = ALIGN(UBIFS_INO_NODE_SZ, c->min_io_size);
36777 + ino = kzalloc(tmp, GFP_KERNEL);
36778 + if (!ino)
36779 + return -ENOMEM;
36780 +
36781 + ino_key_init_flash(c, &ino->key, UBIFS_ROOT_INO);
36782 + ino->ch.node_type = UBIFS_INO_NODE;
36783 + ino->creat_sqnum = cpu_to_le64(++c->max_sqnum);
36784 + ino->nlink = cpu_to_le32(2);
36785 + tmp = cpu_to_le64(CURRENT_TIME_SEC.tv_sec);
36786 + ino->atime_sec = tmp;
36787 + ino->ctime_sec = tmp;
36788 + ino->mtime_sec = tmp;
36789 + ino->atime_nsec = 0;
36790 + ino->ctime_nsec = 0;
36791 + ino->mtime_nsec = 0;
36792 + ino->mode = cpu_to_le32(S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO);
36793 + ino->size = cpu_to_le64(UBIFS_INO_NODE_SZ);
36794 +
36795 + /* Set compression enabled by default */
36796 + ino->flags = cpu_to_le32(UBIFS_COMPR_FL);
36797 +
36798 + err = ubifs_write_node(c, ino, UBIFS_INO_NODE_SZ,
36799 + main_first + DEFAULT_DATA_LEB, 0,
36800 + UBI_UNKNOWN);
36801 + kfree(ino);
36802 + if (err)
36803 + return err;
36804 +
36805 + dbg_gen("root inode created at LEB %d:0",
36806 + main_first + DEFAULT_DATA_LEB);
36807 +
36808 + /*
36809 + * The first node in the log has to be the commit start node. This is
36810 + * always the case during normal file-system operation. Write a fake
36811 + * commit start node to the log.
36812 + */
36813 + tmp = ALIGN(UBIFS_CS_NODE_SZ, c->min_io_size);
36814 + cs = kzalloc(tmp, GFP_KERNEL);
36815 + if (!cs)
36816 + return -ENOMEM;
36817 +
36818 + cs->ch.node_type = UBIFS_CS_NODE;
36819 + err = ubifs_write_node(c, cs, UBIFS_CS_NODE_SZ, UBIFS_LOG_LNUM,
36820 + 0, UBI_UNKNOWN);
36821 + kfree(cs);
36822 +
36823 + ubifs_msg("default file-system created");
36824 + return 0;
36825 +}
36826 +
36827 +/**
36828 + * validate_sb - validate superblock node.
36829 + * @c: UBIFS file-system description object
36830 + * @sup: superblock node
36831 + *
36832 + * This function validates superblock node @sup. Since most of data was read
36833 + * from the superblock and stored in @c, the function validates fields in @c
36834 + * instead. Returns zero in case of success and %-EINVAL in case of validation
36835 + * failure.
36836 + */
36837 +static int validate_sb(struct ubifs_info *c, struct ubifs_sb_node *sup)
36838 +{
36839 + long long max_bytes;
36840 + int err = 1;
36841 +
36842 + if (!c->key_hash) {
36843 + err = 2;
36844 + goto failed;
36845 + }
36846 +
36847 + if (sup->key_fmt != UBIFS_SIMPLE_KEY_FMT) {
36848 + err = 3;
36849 + goto failed;
36850 + }
36851 +
36852 + if (le32_to_cpu(sup->min_io_size) != c->min_io_size) {
36853 + ubifs_err("min. I/O unit mismatch: %d in superblock, %d real",
36854 + le32_to_cpu(sup->min_io_size), c->min_io_size);
36855 + goto failed;
36856 + }
36857 +
36858 + if (le32_to_cpu(sup->leb_size) != c->leb_size) {
36859 + ubifs_err("LEB size mismatch: %d in superblock, %d real",
36860 + le32_to_cpu(sup->leb_size), c->leb_size);
36861 + goto failed;
36862 + }
36863 +
36864 + if (c->leb_cnt < UBIFS_MIN_LEB_CNT || c->leb_cnt > c->vi.size) {
36865 + ubifs_err("bad LEB count: %d in superblock, %d on UBI volume, "
36866 + "%d minimum required", c->leb_cnt, c->vi.size,
36867 + UBIFS_MIN_LEB_CNT);
36868 + goto failed;
36869 + }
36870 +
36871 + if (c->max_leb_cnt < c->leb_cnt) {
36872 + ubifs_err("max. LEB count %d less than LEB count %d",
36873 + c->max_leb_cnt, c->leb_cnt);
36874 + goto failed;
36875 + }
36876 +
36877 + if (c->log_lebs < UBIFS_MIN_LOG_LEBS ||
36878 + c->lpt_lebs < UBIFS_MIN_LPT_LEBS ||
36879 + c->orph_lebs < UBIFS_MIN_ORPH_LEBS ||
36880 + c->main_lebs < UBIFS_MIN_MAIN_LEBS) {
36881 + err = 6;
36882 + goto failed;
36883 + }
36884 +
36885 + if (c->main_lebs < UBIFS_MIN_MAIN_LEBS) {
36886 + err = 7;
36887 + goto failed;
36888 + }
36889 +
36890 + if (c->max_bud_bytes < (long long)c->leb_size * UBIFS_MIN_BUD_LEBS ||
36891 + c->max_bud_bytes > (long long)c->leb_size * c->main_lebs) {
36892 + err = 8;
36893 + goto failed;
36894 + }
36895 +
36896 + if (c->jhead_cnt < NONDATA_JHEADS_CNT + 1 ||
36897 + c->jhead_cnt > NONDATA_JHEADS_CNT + UBIFS_MAX_JHEADS) {
36898 + err = 9;
36899 + goto failed;
36900 + }
36901 +
36902 + if (c->fanout < UBIFS_MIN_FANOUT ||
36903 + ubifs_idx_node_sz(c, c->fanout) > c->leb_size) {
36904 + err = 10;
36905 + goto failed;
36906 + }
36907 +
36908 + if (c->lsave_cnt < 0 || (c->lsave_cnt > DEFAULT_LSAVE_CNT &&
36909 + c->lsave_cnt > c->max_leb_cnt - UBIFS_SB_LEBS - UBIFS_MST_LEBS -
36910 + c->log_lebs - c->lpt_lebs - c->orph_lebs)) {
36911 + err = 11;
36912 + goto failed;
36913 + }
36914 +
36915 + if (UBIFS_SB_LEBS + UBIFS_MST_LEBS + c->log_lebs + c->lpt_lebs +
36916 + c->orph_lebs + c->main_lebs != c->leb_cnt) {
36917 + err = 12;
36918 + goto failed;
36919 + }
36920 +
36921 + if (c->default_compr < 0 || c->default_compr >= UBIFS_COMPR_TYPES_CNT) {
36922 + err = 13;
36923 + goto failed;
36924 + }
36925 +
36926 + max_bytes = c->main_lebs * (long long)c->leb_size;
36927 + if (c->rp_size < 0 || max_bytes < c->rp_size) {
36928 + err = 14;
36929 + goto failed;
36930 + }
36931 +
36932 + if (le32_to_cpu(sup->time_gran) > 1000000000 ||
36933 + le32_to_cpu(sup->time_gran) < 1) {
36934 + err = 15;
36935 + goto failed;
36936 + }
36937 +
36938 + return 0;
36939 +
36940 +failed:
36941 + ubifs_err("bad superblock, error %d", err);
36942 + dbg_dump_node(c, sup);
36943 + return -EINVAL;
36944 +}
36945 +
36946 +/**
36947 + * ubifs_read_sb_node - read superblock node.
36948 + * @c: UBIFS file-system description object
36949 + *
36950 + * This function returns a pointer to the superblock node or a negative error
36951 + * code.
36952 + */
36953 +struct ubifs_sb_node *ubifs_read_sb_node(struct ubifs_info *c)
36954 +{
36955 + struct ubifs_sb_node *sup;
36956 + int err;
36957 +
36958 + sup = kmalloc(ALIGN(UBIFS_SB_NODE_SZ, c->min_io_size), GFP_NOFS);
36959 + if (!sup)
36960 + return ERR_PTR(-ENOMEM);
36961 +
36962 + err = ubifs_read_node(c, sup, UBIFS_SB_NODE, UBIFS_SB_NODE_SZ,
36963 + UBIFS_SB_LNUM, 0);
36964 + if (err) {
36965 + kfree(sup);
36966 + return ERR_PTR(err);
36967 + }
36968 +
36969 + return sup;
36970 +}
36971 +
36972 +/**
36973 + * ubifs_write_sb_node - write superblock node.
36974 + * @c: UBIFS file-system description object
36975 + * @sup: superblock node read with 'ubifs_read_sb_node()'
36976 + *
36977 + * This function returns %0 on success and a negative error code on failure.
36978 + */
36979 +int ubifs_write_sb_node(struct ubifs_info *c, struct ubifs_sb_node *sup)
36980 +{
36981 + int len = ALIGN(UBIFS_SB_NODE_SZ, c->min_io_size);
36982 +
36983 + ubifs_prepare_node(c, sup, UBIFS_SB_NODE_SZ, 1);
36984 + return ubi_leb_change(c->ubi, UBIFS_SB_LNUM, sup, len, UBI_LONGTERM);
36985 +}
36986 +
36987 +/**
36988 + * ubifs_read_superblock - read superblock.
36989 + * @c: UBIFS file-system description object
36990 + *
36991 + * This function finds, reads and checks the superblock. If an empty UBI volume
36992 + * is being mounted, this function creates default superblock. Returns zero in
36993 + * case of success, and a negative error code in case of failure.
36994 + */
36995 +int ubifs_read_superblock(struct ubifs_info *c)
36996 +{
36997 + int err, sup_flags;
36998 + struct ubifs_sb_node *sup;
36999 +
37000 + if (c->empty) {
37001 + err = create_default_filesystem(c);
37002 + if (err)
37003 + return err;
37004 + }
37005 +
37006 + sup = ubifs_read_sb_node(c);
37007 + if (IS_ERR(sup))
37008 + return PTR_ERR(sup);
37009 +
37010 + /*
37011 + * The software supports all previous versions but not future versions,
37012 + * due to the unavailability of time-travelling equipment.
37013 + */
37014 + c->fmt_version = le32_to_cpu(sup->fmt_version);
37015 + if (c->fmt_version > UBIFS_FORMAT_VERSION) {
37016 + ubifs_err("on-flash format version is %d, but software only "
37017 + "supports up to version %d", c->fmt_version,
37018 + UBIFS_FORMAT_VERSION);
37019 + err = -EINVAL;
37020 + goto out;
37021 + }
37022 +
37023 + if (c->fmt_version < 3) {
37024 + ubifs_err("on-flash format version %d is not supported",
37025 + c->fmt_version);
37026 + err = -EINVAL;
37027 + goto out;
37028 + }
37029 +
37030 + switch (sup->key_hash) {
37031 + case UBIFS_KEY_HASH_R5:
37032 + c->key_hash = key_r5_hash;
37033 + c->key_hash_type = UBIFS_KEY_HASH_R5;
37034 + break;
37035 +
37036 + case UBIFS_KEY_HASH_TEST:
37037 + c->key_hash = key_test_hash;
37038 + c->key_hash_type = UBIFS_KEY_HASH_TEST;
37039 + break;
37040 + };
37041 +
37042 + c->key_fmt = sup->key_fmt;
37043 +
37044 + switch (c->key_fmt) {
37045 + case UBIFS_SIMPLE_KEY_FMT:
37046 + c->key_len = UBIFS_SK_LEN;
37047 + break;
37048 + default:
37049 + ubifs_err("unsupported key format");
37050 + err = -EINVAL;
37051 + goto out;
37052 + }
37053 +
37054 + c->leb_cnt = le32_to_cpu(sup->leb_cnt);
37055 + c->max_leb_cnt = le32_to_cpu(sup->max_leb_cnt);
37056 + c->max_bud_bytes = le64_to_cpu(sup->max_bud_bytes);
37057 + c->log_lebs = le32_to_cpu(sup->log_lebs);
37058 + c->lpt_lebs = le32_to_cpu(sup->lpt_lebs);
37059 + c->orph_lebs = le32_to_cpu(sup->orph_lebs);
37060 + c->jhead_cnt = le32_to_cpu(sup->jhead_cnt) + NONDATA_JHEADS_CNT;
37061 + c->fanout = le32_to_cpu(sup->fanout);
37062 + c->lsave_cnt = le32_to_cpu(sup->lsave_cnt);
37063 + c->default_compr = le16_to_cpu(sup->default_compr);
37064 + c->rp_size = le64_to_cpu(sup->rp_size);
37065 + c->rp_uid = le32_to_cpu(sup->rp_uid);
37066 + c->rp_gid = le32_to_cpu(sup->rp_gid);
37067 + sup_flags = le32_to_cpu(sup->flags);
37068 +
37069 + c->vfs_sb->s_time_gran = le32_to_cpu(sup->time_gran);
37070 +
37071 + memcpy(&c->uuid, &sup->uuid, 16);
37072 +
37073 + c->big_lpt = !!(sup_flags & UBIFS_FLG_BIGLPT);
37074 +
37075 + /* Automatically increase file system size to the maximum size */
37076 + c->old_leb_cnt = c->leb_cnt;
37077 + if (c->leb_cnt < c->vi.size && c->leb_cnt < c->max_leb_cnt) {
37078 + c->leb_cnt = min_t(int, c->max_leb_cnt, c->vi.size);
37079 + if (c->vfs_sb->s_flags & MS_RDONLY)
37080 + dbg_mnt("Auto resizing (ro) from %d LEBs to %d LEBs",
37081 + c->old_leb_cnt, c->leb_cnt);
37082 + else {
37083 + dbg_mnt("Auto resizing (sb) from %d LEBs to %d LEBs",
37084 + c->old_leb_cnt, c->leb_cnt);
37085 + sup->leb_cnt = cpu_to_le32(c->leb_cnt);
37086 + err = ubifs_write_sb_node(c, sup);
37087 + if (err)
37088 + goto out;
37089 + c->old_leb_cnt = c->leb_cnt;
37090 + }
37091 + }
37092 +
37093 + c->log_bytes = (long long)c->log_lebs * c->leb_size;
37094 + c->log_last = UBIFS_LOG_LNUM + c->log_lebs - 1;
37095 + c->lpt_first = UBIFS_LOG_LNUM + c->log_lebs;
37096 + c->lpt_last = c->lpt_first + c->lpt_lebs - 1;
37097 + c->orph_first = c->lpt_last + 1;
37098 + c->orph_last = c->orph_first + c->orph_lebs - 1;
37099 + c->main_lebs = c->leb_cnt - UBIFS_SB_LEBS - UBIFS_MST_LEBS;
37100 + c->main_lebs -= c->log_lebs + c->lpt_lebs + c->orph_lebs;
37101 + c->main_first = c->leb_cnt - c->main_lebs;
37102 + c->report_rp_size = ubifs_reported_space(c, c->rp_size);
37103 +
37104 + err = validate_sb(c, sup);
37105 +out:
37106 + kfree(sup);
37107 + return err;
37108 +}
37109 diff --exclude=.git -urN linux-2.6.25.6/fs/ubifs/scan.c avr32-2.6/fs/ubifs/scan.c
37110 --- linux-2.6.25.6/fs/ubifs/scan.c 1970-01-01 01:00:00.000000000 +0100
37111 +++ avr32-2.6/fs/ubifs/scan.c 2008-06-12 15:09:45.515816461 +0200
37112 @@ -0,0 +1,362 @@
37113 +/*
37114 + * This file is part of UBIFS.
37115 + *
37116 + * Copyright (C) 2006-2008 Nokia Corporation
37117 + *
37118 + * This program is free software; you can redistribute it and/or modify it
37119 + * under the terms of the GNU General Public License version 2 as published by
37120 + * the Free Software Foundation.
37121 + *
37122 + * This program is distributed in the hope that it will be useful, but WITHOUT
37123 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
37124 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
37125 + * more details.
37126 + *
37127 + * You should have received a copy of the GNU General Public License along with
37128 + * this program; if not, write to the Free Software Foundation, Inc., 51
37129 + * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
37130 + *
37131 + * Authors: Adrian Hunter
37132 + * Artem Bityutskiy (Битюцкий Артём)
37133 + */
37134 +
37135 +/*
37136 + * This file implements the scan which is a general-purpose function for
37137 + * determining what nodes are in an eraseblock. The scan is used to replay the
37138 + * journal, to do garbage collection. for the TNC in-the-gaps method, and by
37139 + * debugging functions.
37140 + */
37141 +
37142 +#include "ubifs.h"
37143 +
37144 +/**
37145 + * scan_padding_bytes - scan for padding bytes.
37146 + * @buf: buffer to scan
37147 + * @len: length of buffer
37148 + *
37149 + * This function returns the number of padding bytes on success and
37150 + * %SCANNED_GARBAGE on failure.
37151 + */
37152 +static int scan_padding_bytes(void *buf, int len)
37153 +{
37154 + int pad_len = 0, max_pad_len = min_t(int, UBIFS_PAD_NODE_SZ, len);
37155 + uint8_t *p = buf;
37156 +
37157 + dbg_scan("not a node");
37158 +
37159 + while (pad_len < max_pad_len && *p++ == UBIFS_PADDING_BYTE)
37160 + pad_len += 1;
37161 +
37162 + if (!pad_len || (pad_len & 7))
37163 + return SCANNED_GARBAGE;
37164 +
37165 + dbg_scan("%d padding bytes", pad_len);
37166 +
37167 + return pad_len;
37168 +}
37169 +
37170 +/**
37171 + * ubifs_scan_a_node - scan for a node or padding.
37172 + * @c: UBIFS file-system description object
37173 + * @buf: buffer to scan
37174 + * @len: length of buffer
37175 + * @lnum: logical eraseblock number
37176 + * @offs: offset within the logical eraseblock
37177 + * @quiet: print no messages
37178 + *
37179 + * This function returns a scanning code to indicate what was scanned.
37180 + */
37181 +int ubifs_scan_a_node(const struct ubifs_info *c, void *buf, int len, int lnum,
37182 + int offs, int quiet)
37183 +{
37184 + struct ubifs_ch *ch = buf;
37185 + uint32_t magic;
37186 +
37187 + magic = le32_to_cpu(ch->magic);
37188 +
37189 + if (magic == 0xFFFFFFFF) {
37190 + dbg_scan("hit empty space");
37191 + return SCANNED_EMPTY_SPACE;
37192 + }
37193 +
37194 + if (magic != UBIFS_NODE_MAGIC)
37195 + return scan_padding_bytes(buf, len);
37196 +
37197 + if (len < UBIFS_CH_SZ)
37198 + return SCANNED_GARBAGE;
37199 +
37200 + dbg_scan("scanning %s", dbg_ntype(ch->node_type));
37201 +
37202 + if (ubifs_check_node(c, buf, lnum, offs, quiet))
37203 + return SCANNED_A_CORRUPT_NODE;
37204 +
37205 + if (ch->node_type == UBIFS_PAD_NODE) {
37206 + struct ubifs_pad_node *pad = buf;
37207 + int pad_len = le32_to_cpu(pad->pad_len);
37208 + int node_len = le32_to_cpu(ch->len);
37209 +
37210 + /* Validate the padding node */
37211 + if (pad_len < 0 ||
37212 + offs + node_len + pad_len > c->leb_size) {
37213 + if (!quiet) {
37214 + ubifs_err("bad pad node at LEB %d:%d",
37215 + lnum, offs);
37216 + dbg_dump_node(c, pad);
37217 + }
37218 + return SCANNED_A_BAD_PAD_NODE;
37219 + }
37220 +
37221 + /* Make the node pads to 8-byte boundary */
37222 + if ((node_len + pad_len) & 7) {
37223 + if (!quiet) {
37224 + dbg_err("bad padding length %d - %d",
37225 + offs, offs + node_len + pad_len);
37226 + }
37227 + return SCANNED_A_BAD_PAD_NODE;
37228 + }
37229 +
37230 + dbg_scan("%d bytes padded, offset now %d",
37231 + pad_len, ALIGN(offs + node_len + pad_len, 8));
37232 +
37233 + return node_len + pad_len;
37234 + }
37235 +
37236 + return SCANNED_A_NODE;
37237 +}
37238 +
37239 +/**
37240 + * ubifs_start_scan - create LEB scanning information at start of scan.
37241 + * @c: UBIFS file-system description object
37242 + * @lnum: logical eraseblock number
37243 + * @offs: offset to start at (usually zero)
37244 + * @sbuf: scan buffer (must be c->leb_size)
37245 + *
37246 + * This function returns %0 on success and a negative error code on failure.
37247 + */
37248 +struct ubifs_scan_leb *ubifs_start_scan(const struct ubifs_info *c, int lnum,
37249 + int offs, void *sbuf)
37250 +{
37251 + struct ubifs_scan_leb *sleb;
37252 + int err;
37253 +
37254 + dbg_scan("scan LEB %d:%d", lnum, offs);
37255 +
37256 + sleb = kzalloc(sizeof(struct ubifs_scan_leb), GFP_NOFS);
37257 + if (!sleb)
37258 + return ERR_PTR(-ENOMEM);
37259 +
37260 + sleb->lnum = lnum;
37261 + INIT_LIST_HEAD(&sleb->nodes);
37262 + sleb->buf = sbuf;
37263 +
37264 + err = ubi_read(c->ubi, lnum, sbuf + offs, offs, c->leb_size - offs);
37265 + if (err && err != -EBADMSG) {
37266 + ubifs_err("cannot read %d bytes from LEB %d:%d,"
37267 + " error %d", c->leb_size - offs, lnum, offs, err);
37268 + kfree(sleb);
37269 + return ERR_PTR(err);
37270 + }
37271 +
37272 + if (err == -EBADMSG)
37273 + sleb->ecc = 1;
37274 +
37275 + return sleb;
37276 +}
37277 +
37278 +/**
37279 + * ubifs_end_scan - update LEB scanning information at end of scan.
37280 + * @c: UBIFS file-system description object
37281 + * @sleb: scanning information
37282 + * @lnum: logical eraseblock number
37283 + * @offs: offset to start at (usually zero)
37284 + *
37285 + * This function returns %0 on success and a negative error code on failure.
37286 + */
37287 +void ubifs_end_scan(const struct ubifs_info *c, struct ubifs_scan_leb *sleb,
37288 + int lnum, int offs)
37289 +{
37290 + lnum = lnum;
37291 + dbg_scan("stop scanning LEB %d at offset %d", lnum, offs);
37292 + ubifs_assert(offs % c->min_io_size == 0);
37293 +
37294 + sleb->endpt = ALIGN(offs, c->min_io_size);
37295 +}
37296 +
37297 +/**
37298 + * ubifs_add_snod - add a scanned node to LEB scanning information.
37299 + * @c: UBIFS file-system description object
37300 + * @sleb: scanning information
37301 + * @buf: buffer containing node
37302 + * @offs: offset of node on flash
37303 + *
37304 + * This function returns %0 on success and a negative error code on failure.
37305 + */
37306 +int ubifs_add_snod(const struct ubifs_info *c, struct ubifs_scan_leb *sleb,
37307 + void *buf, int offs)
37308 +{
37309 + struct ubifs_ch *ch = buf;
37310 + struct ubifs_ino_node *ino = buf;
37311 + struct ubifs_scan_node *snod;
37312 +
37313 + snod = kzalloc(sizeof(struct ubifs_scan_node), GFP_NOFS);
37314 + if (!snod)
37315 + return -ENOMEM;
37316 +
37317 + snod->sqnum = le64_to_cpu(ch->sqnum);
37318 + snod->type = ch->node_type;
37319 + snod->offs = offs;
37320 + snod->len = le32_to_cpu(ch->len);
37321 + snod->node = buf;
37322 +
37323 + switch (ch->node_type) {
37324 + case UBIFS_INO_NODE:
37325 + case UBIFS_DENT_NODE:
37326 + case UBIFS_XENT_NODE:
37327 + case UBIFS_DATA_NODE:
37328 + case UBIFS_TRUN_NODE:
37329 + /*
37330 + * The key is in the same place in all keyed
37331 + * nodes.
37332 + */
37333 + key_read(c, &ino->key, &snod->key);
37334 + break;
37335 + }
37336 + list_add_tail(&snod->list, &sleb->nodes);
37337 + sleb->nodes_cnt += 1;
37338 + return 0;
37339 +}
37340 +
37341 +/**
37342 + * ubifs_scanned_corruption - print information after UBIFS scanned corruption.
37343 + * @c: UBIFS file-system description object
37344 + * @lnum: LEB number of corruption
37345 + * @offs: offset of corruption
37346 + * @buf: buffer containing corruption
37347 + */
37348 +void ubifs_scanned_corruption(const struct ubifs_info *c, int lnum, int offs,
37349 + void *buf)
37350 +{
37351 + int len;
37352 +
37353 + ubifs_err("corrupted data at LEB %d:%d", lnum, offs);
37354 + if (dbg_failure_mode)
37355 + return;
37356 + len = c->leb_size - offs;
37357 + if (len > 4096)
37358 + len = 4096;
37359 + dbg_err("first %d bytes from LEB %d:%d", len, lnum, offs);
37360 + print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 4, buf, len, 1);
37361 +}
37362 +
37363 +/**
37364 + * ubifs_scan - scan a logical eraseblock.
37365 + * @c: UBIFS file-system description object
37366 + * @lnum: logical eraseblock number
37367 + * @offs: offset to start at (usually zero)
37368 + * @sbuf: scan buffer (must be c->leb_size)
37369 + *
37370 + * This function scans LEB number @lnum and returns complete information about
37371 + * its contents. Returns an error code in case of failure.
37372 + */
37373 +struct ubifs_scan_leb *ubifs_scan(const struct ubifs_info *c, int lnum,
37374 + int offs, void *sbuf)
37375 +{
37376 + void *buf = sbuf + offs;
37377 + int err, len = c->leb_size - offs;
37378 + struct ubifs_scan_leb *sleb;
37379 +
37380 + sleb = ubifs_start_scan(c, lnum, offs, sbuf);
37381 + if (IS_ERR(sleb))
37382 + return sleb;
37383 +
37384 + while (len >= 8) {
37385 + struct ubifs_ch *ch = buf;
37386 + int node_len, ret;
37387 +
37388 + dbg_scan("look at LEB %d:%d (%d bytes left)",
37389 + lnum, offs, len);
37390 +
37391 + cond_resched();
37392 +
37393 + ret = ubifs_scan_a_node(c, buf, len, lnum, offs, 0);
37394 +
37395 + if (ret > 0) {
37396 + /* Padding bytes or a valid padding node */
37397 + offs += ret;
37398 + buf += ret;
37399 + len -= ret;
37400 + continue;
37401 + }
37402 +
37403 + if (ret == SCANNED_EMPTY_SPACE)
37404 + /* Empty space is checked later */
37405 + break;
37406 +
37407 + switch (ret) {
37408 + case SCANNED_GARBAGE:
37409 + dbg_err("garbage");
37410 + goto corrupted;
37411 + case SCANNED_A_NODE:
37412 + break;
37413 + case SCANNED_A_CORRUPT_NODE:
37414 + case SCANNED_A_BAD_PAD_NODE:
37415 + dbg_err("bad node");
37416 + goto corrupted;
37417 + default:
37418 + dbg_err("unknown");
37419 + goto corrupted;
37420 + }
37421 +
37422 + err = ubifs_add_snod(c, sleb, buf, offs);
37423 + if (err)
37424 + goto error;
37425 +
37426 + node_len = ALIGN(le32_to_cpu(ch->len), 8);
37427 + offs += node_len;
37428 + buf += node_len;
37429 + len -= node_len;
37430 + }
37431 +
37432 + if (offs % c->min_io_size)
37433 + goto corrupted;
37434 +
37435 + ubifs_end_scan(c, sleb, lnum, offs);
37436 +
37437 + for (; len > 4; offs += 4, buf = buf + 4, len -= 4)
37438 + if (*(uint32_t *)buf != 0xffffffff)
37439 + break;
37440 + for (; len; offs++, buf++, len--)
37441 + if (*(uint8_t *)buf != 0xff) {
37442 + ubifs_err("corrupt empty space at LEB %d:%d",
37443 + lnum, offs);
37444 + goto corrupted;
37445 + }
37446 +
37447 + return sleb;
37448 +
37449 +corrupted:
37450 + ubifs_scanned_corruption(c, lnum, offs, buf);
37451 + err = -EUCLEAN;
37452 +error:
37453 + ubifs_err("LEB %d scanning failed", lnum);
37454 + ubifs_scan_destroy(sleb);
37455 + return ERR_PTR(err);
37456 +}
37457 +
37458 +/**
37459 + * ubifs_scan_destroy - destroy LEB scanning information.
37460 + * @sleb: scanning information to free
37461 + */
37462 +void ubifs_scan_destroy(struct ubifs_scan_leb *sleb)
37463 +{
37464 + struct ubifs_scan_node *node;
37465 + struct list_head *head;
37466 +
37467 + head = &sleb->nodes;
37468 + while (!list_empty(head)) {
37469 + node = list_entry(head->next, struct ubifs_scan_node, list);
37470 + list_del(&node->list);
37471 + kfree(node);
37472 + }
37473 + kfree(sleb);
37474 +}
37475 diff --exclude=.git -urN linux-2.6.25.6/fs/ubifs/shrinker.c avr32-2.6/fs/ubifs/shrinker.c
37476 --- linux-2.6.25.6/fs/ubifs/shrinker.c 1970-01-01 01:00:00.000000000 +0100
37477 +++ avr32-2.6/fs/ubifs/shrinker.c 2008-06-12 15:09:45.515816461 +0200
37478 @@ -0,0 +1,322 @@
37479 +/*
37480 + * This file is part of UBIFS.
37481 + *
37482 + * Copyright (C) 2006-2008 Nokia Corporation.
37483 + *
37484 + * This program is free software; you can redistribute it and/or modify it
37485 + * under the terms of the GNU General Public License version 2 as published by
37486 + * the Free Software Foundation.
37487 + *
37488 + * This program is distributed in the hope that it will be useful, but WITHOUT
37489 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
37490 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
37491 + * more details.
37492 + *
37493 + * You should have received a copy of the GNU General Public License along with
37494 + * this program; if not, write to the Free Software Foundation, Inc., 51
37495 + * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
37496 + *
37497 + * Authors: Artem Bityutskiy (Битюцкий Артём)
37498 + * Adrian Hunter
37499 + */
37500 +
37501 +/*
37502 + * This file implements UBIFS shrinker which evicts clean znodes from the TNC
37503 + * tree when Linux VM needs more RAM.
37504 + *
37505 + * We do not implement any LRU lists to find oldest znodes to free because it
37506 + * would add additional overhead to the file system fast paths. So the shrinker
37507 + * just walks the TNC tree when searching for znodes to free.
37508 + *
37509 + * If the root of a TNC sub-tree is clean and old enough, then the children are
37510 + * also clean and old enough. So the shrinker walks the TNC in level order and
37511 + * dumps entire sub-trees.
37512 + *
37513 + * The age of znodes is just the time-stamp when they were last looked at.
37514 + * The current shrinker first tries to evict old znodes, then young ones.
37515 + *
37516 + * Since the shrinker is global, it has to protect against races with FS
37517 + * un-mounts, which is done by the 'ubifs_infos_lock' and 'c->umount_mutex'.
37518 + */
37519 +
37520 +#include "ubifs.h"
37521 +
37522 +/* List of all UBIFS file-system instances */
37523 +LIST_HEAD(ubifs_infos);
37524 +
37525 +/*
37526 + * We number each shrinker run and record the number on the ubifs_info structure
37527 + * so that we can easily work out which ubifs_info structures have already been
37528 + * done by the current run.
37529 + */
37530 +static unsigned int shrinker_run_no;
37531 +
37532 +/* Protects 'ubifs_infos' list */
37533 +DEFINE_SPINLOCK(ubifs_infos_lock);
37534 +
37535 +/* Global clean znode counter (for all mounted UBIFS instances) */
37536 +atomic_long_t ubifs_clean_zn_cnt;
37537 +
37538 +/**
37539 + * shrink_tnc - shrink TNC tree.
37540 + * @c: UBIFS file-system description object
37541 + * @nr: number of znodes to free
37542 + * @age: the age of znodes to free
37543 + * @contention: if any contention, this is set to %1
37544 + *
37545 + * This function traverses TNC tree and frees clean znodes. It does not free
37546 + * clean znodes which younger then @age. Returns number of freed znodes.
37547 + */
37548 +static int shrink_tnc(struct ubifs_info *c, int nr, int age, int *contention)
37549 +{
37550 + int total_freed = 0;
37551 + struct ubifs_znode *znode, *zprev;
37552 + int time = get_seconds();
37553 +
37554 + ubifs_assert(mutex_is_locked(&c->umount_mutex));
37555 + ubifs_assert(mutex_is_locked(&c->tnc_mutex));
37556 +
37557 + if (!c->zroot.znode || atomic_long_read(&c->clean_zn_cnt) == 0)
37558 + return 0;
37559 +
37560 + /*
37561 + * Traverse the TNC tree in levelorder manner, so that it is possible
37562 + * to destroy large sub-trees. Indeed, if a znode is old, then all its
37563 + * children are older or of the same age.
37564 + *
37565 + * Note, we are holding 'c->tnc_mutex', so we do not have to lock the
37566 + * 'c->space_lock' when _reading_ 'c->clean_zn_cnt', because it is
37567 + * changed only when the 'c->tnc_mutex' is held.
37568 + */
37569 + zprev = NULL;
37570 + znode = ubifs_tnc_levelorder_next(c->zroot.znode, NULL);
37571 + while (znode && total_freed < nr &&
37572 + atomic_long_read(&c->clean_zn_cnt) > 0) {
37573 + int freed;
37574 +
37575 + /*
37576 + * If the znode is clean, but it is in the 'c->cnext' list, this
37577 + * means that this znode has just been written to flash as a
37578 + * part of commit and was marked clean. They will be removed
37579 + * from the list at end commit. We cannot change the list,
37580 + * because it is not protected by any mutex (design decision to
37581 + * make commit really independent and parallel to main I/O). So
37582 + * we just skip these znodes.
37583 + *
37584 + * Note, the 'clean_zn_cnt' counters are not updated until
37585 + * after the commit, so the UBIFS shrinker does not report
37586 + * the znodes which are in the 'c->cnext' list as freeable.
37587 + *
37588 + * Also note, if the root of a sub-tree is not in 'c->cnext',
37589 + * then the whole sub-tree is not in 'c->cnext' as well, so it
37590 + * is safe to dump whole sub-tree.
37591 + */
37592 +
37593 + if (znode->cnext) {
37594 + /*
37595 + * Very soon these znodes will be removed from the list
37596 + * and become freeable.
37597 + */
37598 + *contention = 1;
37599 + } else if (!ubifs_zn_dirty(znode) &&
37600 + abs(time - znode->time) >= age) {
37601 + if (znode->parent)
37602 + znode->parent->zbranch[znode->iip].znode = NULL;
37603 + else
37604 + c->zroot.znode = NULL;
37605 +
37606 + freed = ubifs_destroy_tnc_subtree(znode);
37607 + atomic_long_sub(freed, &ubifs_clean_zn_cnt);
37608 + atomic_long_sub(freed, &c->clean_zn_cnt);
37609 + ubifs_assert(atomic_long_read(&c->clean_zn_cnt) >= 0);
37610 + total_freed += freed;
37611 + znode = zprev;
37612 + }
37613 +
37614 + if (unlikely(!c->zroot.znode))
37615 + break;
37616 +
37617 + zprev = znode;
37618 + znode = ubifs_tnc_levelorder_next(c->zroot.znode, znode);
37619 + cond_resched();
37620 + }
37621 +
37622 + return total_freed;
37623 +}
37624 +
37625 +/**
37626 + * shrink_tnc_trees - shrink UBIFS TNC trees.
37627 + * @nr: number of znodes to free
37628 + * @age: the age of znodes to free
37629 + * @contention: if any contention, this is set to %1
37630 + *
37631 + * This function walks the list of mounted UBIFS file-systems and frees clean
37632 + * znodes which are older then @age, until at least @nr znodes are freed.
37633 + * Returns the number of freed znodes.
37634 + */
37635 +static int shrink_tnc_trees(int nr, int age, int *contention)
37636 +{
37637 + struct ubifs_info *c;
37638 + struct list_head *p;
37639 + unsigned int run_no;
37640 + int freed = 0;
37641 +
37642 + spin_lock(&ubifs_infos_lock);
37643 + do
37644 + run_no = ++shrinker_run_no;
37645 + while (run_no == 0);
37646 + /* Iterate over all mounted UBIFS file-systems and try to shrink them */
37647 + p = ubifs_infos.next;
37648 + while (p != &ubifs_infos) {
37649 + c = list_entry(p, struct ubifs_info, infos_list);
37650 + /*
37651 + * We move the ones we do to the end of the list, so we stop
37652 + * when we see one we have already done.
37653 + */
37654 + if (c->shrinker_run_no == run_no)
37655 + break;
37656 + if (!mutex_trylock(&c->umount_mutex)) {
37657 + /* Some un-mount is in progress, try next FS */
37658 + *contention = 1;
37659 + p = p->next;
37660 + continue;
37661 + }
37662 + /*
37663 + * We're holding 'c->umount_mutex', so the file-system won't go
37664 + * away.
37665 + */
37666 + if (!mutex_trylock(&c->tnc_mutex)) {
37667 + mutex_unlock(&c->umount_mutex);
37668 + *contention = 1;
37669 + p = p->next;
37670 + continue;
37671 + }
37672 + spin_unlock(&ubifs_infos_lock);
37673 + /*
37674 + * OK, now we have TNC locked, the file-system cannot go away -
37675 + * it is safe to reap the cache.
37676 + */
37677 + c->shrinker_run_no = run_no;
37678 + freed += shrink_tnc(c, nr, age, contention);
37679 + mutex_unlock(&c->tnc_mutex);
37680 + spin_lock(&ubifs_infos_lock);
37681 + /* Get the next list element before we move this one */
37682 + p = p->next;
37683 + /*
37684 + * Move this one to the end of the list to provide some
37685 + * fairness.
37686 + */
37687 + list_del(&c->infos_list);
37688 + list_add_tail(&c->infos_list, &ubifs_infos);
37689 + mutex_unlock(&c->umount_mutex);
37690 + if (freed >= nr)
37691 + break;
37692 + }
37693 + spin_unlock(&ubifs_infos_lock);
37694 + return freed;
37695 +}
37696 +
37697 +/**
37698 + * kick_a_thread - kick a background thread to start commit.
37699 + *
37700 + * This function kicks a background thread to start background commit. Returns
37701 + * %-1 if a thread was kicked or there is another reason to assume the memory
37702 + * will soon be freed or become freeable. If there are no dirty znodes, returns
37703 + * %0.
37704 + */
37705 +static int kick_a_thread(void)
37706 +{
37707 + int i;
37708 + struct ubifs_info *c;
37709 +
37710 + /*
37711 + * Iterate over all mounted UBIFS file-systems and find out if there is
37712 + * already an ongoing commit operation there. If no, then iterate for
37713 + * the second time and initiate background commit.
37714 + */
37715 + spin_lock(&ubifs_infos_lock);
37716 + for (i = 0; i < 2; i++) {
37717 + list_for_each_entry(c, &ubifs_infos, infos_list) {
37718 + long dirty_zn_cnt;
37719 +
37720 + if (!mutex_trylock(&c->umount_mutex)) {
37721 + /*
37722 + * Some un-mount is in progress, it will
37723 + * certainly free memory, so just return.
37724 + */
37725 + spin_unlock(&ubifs_infos_lock);
37726 + return -1;
37727 + }
37728 +
37729 + dirty_zn_cnt = atomic_long_read(&c->dirty_zn_cnt);
37730 +
37731 + if (!dirty_zn_cnt || c->cmt_state == COMMIT_BROKEN ||
37732 + c->ro_media) {
37733 + mutex_unlock(&c->umount_mutex);
37734 + continue;
37735 + }
37736 +
37737 + if (c->cmt_state != COMMIT_RESTING) {
37738 + spin_unlock(&ubifs_infos_lock);
37739 + mutex_unlock(&c->umount_mutex);
37740 + return -1;
37741 + }
37742 +
37743 + if (i == 1) {
37744 + list_del(&c->infos_list);
37745 + list_add_tail(&c->infos_list, &ubifs_infos);
37746 + spin_unlock(&ubifs_infos_lock);
37747 +
37748 + ubifs_request_bg_commit(c);
37749 + mutex_unlock(&c->umount_mutex);
37750 + return -1;
37751 + }
37752 + mutex_unlock(&c->umount_mutex);
37753 + }
37754 + }
37755 + spin_unlock(&ubifs_infos_lock);
37756 +
37757 + return 0;
37758 +}
37759 +
37760 +int ubifs_shrinker(int nr, gfp_t gfp_mask)
37761 +{
37762 + int freed, contention = 0;
37763 + long clean_zn_cnt = atomic_long_read(&ubifs_clean_zn_cnt);
37764 +
37765 + if (nr == 0)
37766 + return clean_zn_cnt;
37767 +
37768 + if (!clean_zn_cnt) {
37769 + /*
37770 + * No clean znodes, nothing to reap. All we can do in this case
37771 + * is to kick background threads to start commit, which will
37772 + * probably make clean znodes which, in turn, will be freeable.
37773 + * And we return -1 which means will make VM call us again
37774 + * later.
37775 + */
37776 + dbg_tnc("no clean znodes, kick a thread");
37777 + return kick_a_thread();
37778 + }
37779 +
37780 + freed = shrink_tnc_trees(nr, OLD_ZNODE_AGE, &contention);
37781 + if (freed >= nr)
37782 + goto out;
37783 +
37784 + dbg_tnc("not enough old znodes, try to free young ones");
37785 + freed += shrink_tnc_trees(nr - freed, YOUNG_ZNODE_AGE, &contention);
37786 + if (freed >= nr)
37787 + goto out;
37788 +
37789 + dbg_tnc("not enough young znodes, free all");
37790 + freed += shrink_tnc_trees(nr - freed, 0, &contention);
37791 +
37792 + if (!freed && contention) {
37793 + dbg_tnc("freed nothing, but contention");
37794 + return -1;
37795 + }
37796 +
37797 +out:
37798 + dbg_tnc("%d znodes were freed, requested %d", freed, nr);
37799 + return freed;
37800 +}
37801 diff --exclude=.git -urN linux-2.6.25.6/fs/ubifs/super.c avr32-2.6/fs/ubifs/super.c
37802 --- linux-2.6.25.6/fs/ubifs/super.c 1970-01-01 01:00:00.000000000 +0100
37803 +++ avr32-2.6/fs/ubifs/super.c 2008-06-12 15:09:45.600758286 +0200
37804 @@ -0,0 +1,1969 @@
37805 +/*
37806 + * This file is part of UBIFS.
37807 + *
37808 + * Copyright (C) 2006-2008 Nokia Corporation.
37809 + *
37810 + * This program is free software; you can redistribute it and/or modify it
37811 + * under the terms of the GNU General Public License version 2 as published by
37812 + * the Free Software Foundation.
37813 + *
37814 + * This program is distributed in the hope that it will be useful, but WITHOUT
37815 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
37816 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
37817 + * more details.
37818 + *
37819 + * You should have received a copy of the GNU General Public License along with
37820 + * this program; if not, write to the Free Software Foundation, Inc., 51
37821 + * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
37822 + *
37823 + * Authors: Artem Bityutskiy (Битюцкий Артём)
37824 + * Adrian Hunter
37825 + */
37826 +
37827 +/*
37828 + * This file implements UBIFS initialization and VFS superblock operations. Some
37829 + * initialization stuff which is rather large and complex is placed at
37830 + * corresponding subsystems, but most of it is here.
37831 + */
37832 +
37833 +#include <linux/init.h>
37834 +#include <linux/slab.h>
37835 +#include <linux/module.h>
37836 +#include <linux/ctype.h>
37837 +#include <linux/random.h>
37838 +#include <linux/kthread.h>
37839 +#include <linux/parser.h>
37840 +#include <linux/seq_file.h>
37841 +#include <linux/mount.h>
37842 +#include "ubifs.h"
37843 +
37844 +/* Slab cache for UBIFS inodes */
37845 +struct kmem_cache *ubifs_inode_slab;
37846 +
37847 +/* UBIFS TNC shrinker description */
37848 +static struct shrinker ubifs_shrinker_info = {
37849 + .shrink = ubifs_shrinker,
37850 + .seeks = DEFAULT_SEEKS,
37851 +};
37852 +
37853 +/**
37854 + * validate_inode - validate inode.
37855 + * @c: UBIFS file-system description object
37856 + * @inode: the inode to validate
37857 + *
37858 + * This is a helper function for 'ubifs_iget()' which validates various fields
37859 + * of a newly built inode to make sure they contain sane values and prevent
37860 + * possible vulnerabilities. Returns zero if the inode is all right and
37861 + * a non-zero error code if not.
37862 + */
37863 +static int validate_inode(struct ubifs_info *c, const struct inode *inode)
37864 +{
37865 + int err;
37866 + const struct ubifs_inode *ui = ubifs_inode(inode);
37867 +
37868 + if (inode->i_size > c->max_inode_sz) {
37869 + ubifs_err("inode is too large (%lld)",
37870 + (long long)inode->i_size);
37871 + return 1;
37872 + }
37873 +
37874 + if (ui->compr_type < 0 || ui->compr_type >= UBIFS_COMPR_TYPES_CNT) {
37875 + ubifs_err("unknown compression type %d", ui->compr_type);
37876 + return 2;
37877 + }
37878 +
37879 + if (ui->xattr_cnt < 0)
37880 + return 3;
37881 +
37882 + if (ui->xattr_size < 0)
37883 + return 4;
37884 +
37885 + if (ui->xattr_names < 0 ||
37886 + ui->xattr_names + ui->xattr_cnt > XATTR_LIST_MAX)
37887 + return 5;
37888 +
37889 + if (ui->data_len < 0 || ui->data_len > UBIFS_MAX_INO_DATA)
37890 + return 6;
37891 +
37892 + if (!ubifs_compr_present(ui->compr_type)) {
37893 + ubifs_warn("inode %lu uses '%s' compression, but it was not "
37894 + "compiled in", inode->i_ino,
37895 + ubifs_compr_name(ui->compr_type));
37896 + }
37897 +
37898 + err = dbg_check_dir_size(c, inode);
37899 + return err;
37900 +}
37901 +
37902 +struct inode *ubifs_iget(struct super_block *sb, unsigned long inum)
37903 +{
37904 + int err;
37905 + union ubifs_key key;
37906 + struct ubifs_ino_node *ino;
37907 + struct ubifs_info *c = sb->s_fs_info;
37908 + struct inode *inode;
37909 + struct ubifs_inode *ui;
37910 +
37911 + dbg_gen("inode %lu", inum);
37912 +
37913 + inode = iget_locked(sb, inum);
37914 + if (!inode)
37915 + return ERR_PTR(-ENOMEM);
37916 + if (!(inode->i_state & I_NEW))
37917 + return inode;
37918 + ui = ubifs_inode(inode);
37919 +
37920 + ino = kmalloc(UBIFS_MAX_INO_NODE_SZ, GFP_NOFS);
37921 + if (!ino) {
37922 + err = -ENOMEM;
37923 + goto out;
37924 + }
37925 +
37926 + ino_key_init(c, &key, inode->i_ino);
37927 +
37928 + err = ubifs_tnc_lookup(c, &key, ino);
37929 + if (err)
37930 + goto out_ino;
37931 +
37932 + inode->i_flags |= (S_NOCMTIME | S_NOATIME);
37933 + inode->i_nlink = le32_to_cpu(ino->nlink);
37934 + inode->i_uid = le32_to_cpu(ino->uid);
37935 + inode->i_gid = le32_to_cpu(ino->gid);
37936 + inode->i_atime.tv_sec = (int64_t)le64_to_cpu(ino->atime_sec);
37937 + inode->i_atime.tv_nsec = le32_to_cpu(ino->atime_nsec);
37938 + inode->i_mtime.tv_sec = (int64_t)le64_to_cpu(ino->mtime_sec);
37939 + inode->i_mtime.tv_nsec = le32_to_cpu(ino->mtime_nsec);
37940 + inode->i_ctime.tv_sec = (int64_t)le64_to_cpu(ino->ctime_sec);
37941 + inode->i_ctime.tv_nsec = le32_to_cpu(ino->ctime_nsec);
37942 + inode->i_mode = le32_to_cpu(ino->mode);
37943 + inode->i_size = le64_to_cpu(ino->size);
37944 +
37945 + ui->data_len = le32_to_cpu(ino->data_len);
37946 + ui->flags = le32_to_cpu(ino->flags);
37947 + ui->compr_type = le16_to_cpu(ino->compr_type);
37948 + ui->creat_sqnum = le64_to_cpu(ino->creat_sqnum);
37949 + ui->xattr_cnt = le32_to_cpu(ino->xattr_cnt);
37950 + ui->xattr_size = le64_to_cpu(ino->xattr_size);
37951 + ui->xattr_names = le32_to_cpu(ino->xattr_names);
37952 +
37953 + err = validate_inode(c, inode);
37954 + if (err)
37955 + goto out_invalid;
37956 +
37957 + /* Disable readahead */
37958 + inode->i_mapping->backing_dev_info = &ubifs_backing_dev_info;
37959 +
37960 + switch (inode->i_mode & S_IFMT) {
37961 + case S_IFREG:
37962 + inode->i_mapping->a_ops = &ubifs_file_address_operations;
37963 + inode->i_op = &ubifs_file_inode_operations;
37964 + inode->i_fop = &ubifs_file_operations;
37965 + if (ui->data_len != 0) {
37966 + err = 10;
37967 + goto out_invalid;
37968 + }
37969 + break;
37970 + case S_IFDIR:
37971 + inode->i_op = &ubifs_dir_inode_operations;
37972 + inode->i_fop = &ubifs_dir_operations;
37973 + if (ui->data_len != 0) {
37974 + err = 11;
37975 + goto out_invalid;
37976 + }
37977 + break;
37978 + case S_IFLNK:
37979 + inode->i_op = &ubifs_symlink_inode_operations;
37980 + if (ui->data_len <= 0 || ui->data_len > UBIFS_MAX_INO_DATA) {
37981 + err = 12;
37982 + goto out_invalid;
37983 + }
37984 + ui->data = kmalloc(ui->data_len + 1, GFP_NOFS);
37985 + if (!ui->data) {
37986 + err = -ENOMEM;
37987 + goto out_ino;
37988 + }
37989 + memcpy(ui->data, ino->data, ui->data_len);
37990 + ((char *)ui->data)[ui->data_len] = '\0';
37991 + break;
37992 + case S_IFBLK:
37993 + case S_IFCHR:
37994 + {
37995 + dev_t rdev;
37996 + union ubifs_dev_desc *dev;
37997 +
37998 + ui->data = kmalloc(sizeof(union ubifs_dev_desc), GFP_NOFS);
37999 + if (!ui->data) {
38000 + err = -ENOMEM;
38001 + goto out_ino;
38002 + }
38003 +
38004 + dev = (union ubifs_dev_desc *)ino->data;
38005 + if (ui->data_len == sizeof(dev->new))
38006 + rdev = new_decode_dev(le32_to_cpu(dev->new));
38007 + else if (ui->data_len == sizeof(dev->huge))
38008 + rdev = huge_decode_dev(le64_to_cpu(dev->huge));
38009 + else {
38010 + err = 13;
38011 + goto out_invalid;
38012 + }
38013 + memcpy(ui->data, ino->data, ui->data_len);
38014 + inode->i_op = &ubifs_file_inode_operations;
38015 + init_special_inode(inode, inode->i_mode, rdev);
38016 + break;
38017 + }
38018 + case S_IFSOCK:
38019 + case S_IFIFO:
38020 + inode->i_op = &ubifs_file_inode_operations;
38021 + init_special_inode(inode, inode->i_mode, 0);
38022 + if (ui->data_len != 0) {
38023 + err = 14;
38024 + goto out_invalid;
38025 + }
38026 + break;
38027 + default:
38028 + err = 15;
38029 + goto out_invalid;
38030 + }
38031 +
38032 + kfree(ino);
38033 + ubifs_set_inode_flags(inode);
38034 + unlock_new_inode(inode);
38035 + return inode;
38036 +
38037 +out_invalid:
38038 + ubifs_err("inode %lu validation failed, error %d", inode->i_ino, err);
38039 + dbg_dump_node(c, ino);
38040 + dbg_dump_inode(c, inode);
38041 + err = -EINVAL;
38042 +out_ino:
38043 + kfree(ino);
38044 +out:
38045 + ubifs_err("failed to read inode %lu, error %d", inode->i_ino, err);
38046 + iget_failed(inode);
38047 + return ERR_PTR(err);
38048 +}
38049 +
38050 +static struct inode *ubifs_alloc_inode(struct super_block *sb)
38051 +{
38052 + struct ubifs_inode *ui;
38053 +
38054 + ui = kmem_cache_alloc(ubifs_inode_slab, GFP_NOFS);
38055 + if (!ui)
38056 + return NULL;
38057 +
38058 + memset((void *)ui + sizeof(struct inode), 0,
38059 + sizeof(struct ubifs_inode) - sizeof(struct inode));
38060 + mutex_init(&ui->budg_mutex);
38061 + return &ui->vfs_inode;
38062 +};
38063 +
38064 +static void ubifs_destroy_inode(struct inode *inode)
38065 +{
38066 + struct ubifs_inode *ui = ubifs_inode(inode);
38067 +
38068 + kfree(ui->data);
38069 + kmem_cache_free(ubifs_inode_slab, inode);
38070 +}
38071 +
38072 +/*
38073 + * Note, Linux write-back code calls this without 'i_mutex'.
38074 + */
38075 +static int ubifs_write_inode(struct inode *inode, int wait)
38076 +{
38077 + int err;
38078 + struct ubifs_info *c = inode->i_sb->s_fs_info;
38079 + struct ubifs_inode *ui = ubifs_inode(inode);
38080 + struct ubifs_budget_req req = {.dd_growth = c->inode_budget,
38081 + .dirtied_ino_d = ui->data_len};
38082 +
38083 + ubifs_assert(!ui->xattr);
38084 + if (is_bad_inode(inode))
38085 + return 0;
38086 +
38087 + mutex_lock(&ui->budg_mutex);
38088 +
38089 + /*
38090 + * Due to races between write-back forced by budgeting
38091 + * (see 'sync_some_inodes()') and pdflush write-back, the inode may
38092 + * have already been synchronized, do not do this again.
38093 + *
38094 + * This might also happen if it was synchronized in e.g. ubifs_link()',
38095 + * etc.
38096 + */
38097 + if (!ui->dirty) {
38098 + mutex_unlock(&ui->budg_mutex);
38099 + return 0;
38100 + }
38101 +
38102 + ubifs_assert(ui->budgeted);
38103 + dbg_gen("inode %lu", inode->i_ino);
38104 +
38105 + err = ubifs_jnl_write_inode(c, inode, 0, IS_SYNC(inode));
38106 + if (err)
38107 + ubifs_err("can't write inode %lu, error %d", inode->i_ino, err);
38108 +
38109 + ui->dirty = 0;
38110 + UBIFS_DBG(ui->budgeted = 0);
38111 + atomic_long_dec(&c->dirty_ino_cnt);
38112 +
38113 + ubifs_release_budget(c, &req);
38114 + mutex_unlock(&ui->budg_mutex);
38115 +
38116 + return err;
38117 +}
38118 +
38119 +static void ubifs_delete_inode(struct inode *inode)
38120 +{
38121 + struct ubifs_info *c = inode->i_sb->s_fs_info;
38122 + struct ubifs_inode *ui = ubifs_inode(inode);
38123 + struct ubifs_budget_req req = {.dd_growth = c->inode_budget,
38124 + .dirtied_ino_d = ui->data_len};
38125 + int err;
38126 +
38127 + if (ui->xattr) {
38128 + /*
38129 + * Extended attribute inode deletions are fully handled in
38130 + * 'ubifs_removexattr()'. These inodes are special and have
38131 + * limited usage, so there is nothing to do here.
38132 + */
38133 + ubifs_assert(!ui->dirty);
38134 + goto out;
38135 + }
38136 +
38137 + dbg_gen("inode %lu", inode->i_ino);
38138 + ubifs_assert(!atomic_read(&inode->i_count));
38139 + ubifs_assert(inode->i_nlink == 0);
38140 +
38141 + truncate_inode_pages(&inode->i_data, 0);
38142 + if (is_bad_inode(inode))
38143 + goto out;
38144 +
38145 + mutex_lock(&ui->budg_mutex);
38146 +
38147 + inode->i_size = 0;
38148 +
38149 + err = ubifs_jnl_write_inode(c, inode, 1, IS_SYNC(inode));
38150 + if (err)
38151 + /*
38152 + * Worst case we have a lost orphan inode wasting space, so a
38153 + * simple error message is ok here.
38154 + */
38155 + ubifs_err("can't write inode %lu, error %d", inode->i_ino, err);
38156 +
38157 + if (ui->dirty) {
38158 + ubifs_assert(ui->budgeted);
38159 + atomic_long_dec(&c->dirty_ino_cnt);
38160 + ui->dirty = 0;
38161 + UBIFS_DBG(ui->budgeted = 0);
38162 + ubifs_release_budget(c, &req);
38163 + }
38164 +
38165 + mutex_unlock(&ui->budg_mutex);
38166 +out:
38167 + clear_inode(inode);
38168 +}
38169 +
38170 +static void ubifs_dirty_inode(struct inode *inode)
38171 +{
38172 + struct ubifs_inode *ui = ubifs_inode(inode);
38173 +
38174 + ubifs_assert(mutex_is_locked(&ui->budg_mutex));
38175 + if (!ui->dirty) {
38176 + struct ubifs_info *c = inode->i_sb->s_fs_info;
38177 +
38178 + ui->dirty = 1;
38179 + atomic_long_inc(&c->dirty_ino_cnt);
38180 + dbg_gen("inode %lu", inode->i_ino);
38181 + }
38182 +}
38183 +
38184 +static int ubifs_statfs(struct dentry *dentry, struct kstatfs *buf)
38185 +{
38186 + struct ubifs_info *c = dentry->d_sb->s_fs_info;
38187 + unsigned long long free;
38188 +
38189 + free = ubifs_budg_get_free_space(c);
38190 + dbg_gen("free space %lld bytes (%lld blocks)",
38191 + free, free >> UBIFS_BLOCK_SHIFT);
38192 +
38193 + buf->f_type = UBIFS_SUPER_MAGIC;
38194 + buf->f_bsize = UBIFS_BLOCK_SIZE;
38195 + buf->f_blocks = c->block_cnt;
38196 + buf->f_bfree = free >> UBIFS_BLOCK_SHIFT;
38197 + if (free > c->report_rp_size)
38198 + buf->f_bavail = (free - c->report_rp_size) >> UBIFS_BLOCK_SHIFT;
38199 + else
38200 + buf->f_bavail = 0;
38201 + buf->f_files = 0;
38202 + buf->f_ffree = 0;
38203 + buf->f_namelen = UBIFS_MAX_NLEN;
38204 +
38205 + return 0;
38206 +}
38207 +
38208 +static int ubifs_show_options(struct seq_file *s, struct vfsmount *mnt)
38209 +{
38210 + struct ubifs_info *c = mnt->mnt_sb->s_fs_info;
38211 +
38212 + if (c->mount_opts.unmount_mode == 2)
38213 + seq_printf(s, ",fast_unmount");
38214 + else if (c->mount_opts.unmount_mode == 1)
38215 + seq_printf(s, ",norm_unmount");
38216 +
38217 + return 0;
38218 +}
38219 +
38220 +static int ubifs_sync_fs(struct super_block *sb, int wait)
38221 +{
38222 + struct ubifs_info *c = sb->s_fs_info;
38223 + int i, ret = 0, err;
38224 +
38225 + if (c->jheads)
38226 + for (i = 0; i < c->jhead_cnt; i++) {
38227 + err = ubifs_wbuf_sync(&c->jheads[i].wbuf);
38228 + if (err && !ret)
38229 + ret = err;
38230 + }
38231 + /*
38232 + * We ought to call sync for c->ubi but it does not have one. If it had
38233 + * it would in turn call mtd->sync, however mtd operations are
38234 + * synchronous anyway, so we don't lose any sleep here.
38235 + */
38236 + return ret;
38237 +}
38238 +
38239 +/**
38240 + * init_constants_early - initialize UBIFS constants.
38241 + * @c: UBIFS file-system description object
38242 + *
38243 + * This function initialize UBIFS constants which do not need the superblock to
38244 + * be read. It also checks that the UBI volume satisfies basic UBIFS
38245 + * requirements. Returns zero in case of success and a negative error code in
38246 + * case of failure.
38247 + */
38248 +static int init_constants_early(struct ubifs_info *c)
38249 +{
38250 + if (c->vi.corrupted) {
38251 + ubifs_warn("UBI volume is corrupted - read-only mode");
38252 + c->ro_media = 1;
38253 + }
38254 +
38255 + if (c->di.ro_mode) {
38256 + ubifs_msg("read-only UBI device");
38257 + c->ro_media = 1;
38258 + }
38259 +
38260 + if (c->vi.vol_type == UBI_STATIC_VOLUME) {
38261 + ubifs_msg("static UBI volume - read-only mode");
38262 + c->ro_media = 1;
38263 + }
38264 +
38265 + c->leb_cnt = c->vi.size;
38266 + c->leb_size = c->vi.usable_leb_size;
38267 + c->half_leb_size = c->leb_size / 2;
38268 + c->min_io_size = c->di.min_io_size;
38269 + c->min_io_shift = fls(c->min_io_size) - 1;
38270 +
38271 + if (c->leb_size < UBIFS_MIN_LEB_SZ) {
38272 + ubifs_err("too small LEBs (%d bytes), min. is %d bytes",
38273 + c->leb_size, UBIFS_MIN_LEB_SZ);
38274 + return -EINVAL;
38275 + }
38276 +
38277 + if (c->leb_cnt < UBIFS_MIN_LEB_CNT) {
38278 + ubifs_err("too few LEBs (%d), min. is %d",
38279 + c->leb_cnt, UBIFS_MIN_LEB_CNT);
38280 + return -EINVAL;
38281 + }
38282 +
38283 + if (!is_power_of_2(c->min_io_size)) {
38284 + ubifs_err("bad min. I/O size %d", c->min_io_size);
38285 + return -EINVAL;
38286 + }
38287 +
38288 + /*
38289 + * UBIFS aligns all node to 8-byte boundary, so to make function in
38290 + * io.c simpler, assume minimum I/O unit size to be 8 bytes if it is
38291 + * less than 8.
38292 + */
38293 + if (c->min_io_size < 8) {
38294 + c->min_io_size = 8;
38295 + c->min_io_shift = 3;
38296 + }
38297 +
38298 + c->ref_node_alsz = ALIGN(UBIFS_REF_NODE_SZ, c->min_io_size);
38299 + c->mst_node_alsz = ALIGN(UBIFS_MST_NODE_SZ, c->min_io_size);
38300 +
38301 + /*
38302 + * Initialize node length ranges which are mostly needed for node
38303 + * length validation.
38304 + */
38305 + c->ranges[UBIFS_PAD_NODE].len = UBIFS_PAD_NODE_SZ;
38306 + c->ranges[UBIFS_SB_NODE].len = UBIFS_SB_NODE_SZ;
38307 + c->ranges[UBIFS_MST_NODE].len = UBIFS_MST_NODE_SZ;
38308 + c->ranges[UBIFS_REF_NODE].len = UBIFS_REF_NODE_SZ;
38309 + c->ranges[UBIFS_TRUN_NODE].len = UBIFS_TRUN_NODE_SZ;
38310 + c->ranges[UBIFS_CS_NODE].len = UBIFS_CS_NODE_SZ;
38311 +
38312 + c->ranges[UBIFS_INO_NODE].min_len = UBIFS_INO_NODE_SZ;
38313 + c->ranges[UBIFS_INO_NODE].max_len = UBIFS_MAX_INO_NODE_SZ;
38314 + c->ranges[UBIFS_ORPH_NODE].min_len =
38315 + UBIFS_ORPH_NODE_SZ + sizeof(__le64);
38316 + c->ranges[UBIFS_ORPH_NODE].max_len = c->leb_size;
38317 + c->ranges[UBIFS_DENT_NODE].min_len = UBIFS_DENT_NODE_SZ;
38318 + c->ranges[UBIFS_DENT_NODE].max_len = UBIFS_MAX_DENT_NODE_SZ;
38319 + c->ranges[UBIFS_XENT_NODE].min_len = UBIFS_XENT_NODE_SZ;
38320 + c->ranges[UBIFS_XENT_NODE].max_len = UBIFS_MAX_XENT_NODE_SZ;
38321 + c->ranges[UBIFS_DATA_NODE].min_len = UBIFS_DATA_NODE_SZ;
38322 + c->ranges[UBIFS_DATA_NODE].max_len = UBIFS_MAX_DATA_NODE_SZ;
38323 + /*
38324 + * Minimum indexing node size is amended later when superblock is
38325 + * read and the key length is known.
38326 + */
38327 + c->ranges[UBIFS_IDX_NODE].min_len = UBIFS_IDX_NODE_SZ + UBIFS_BRANCH_SZ;
38328 + /*
38329 + * Maximum indexing node size is amended later when superblock is
38330 + * read and the fanout is known.
38331 + */
38332 + c->ranges[UBIFS_IDX_NODE].max_len = INT_MAX;
38333 +
38334 + /*
38335 + * Initialize dead and dark LEB space watermarks.
38336 + *
38337 + * Dead space is the space which cannot be used. Its watermark is
38338 + * equivalent to min. I/O unit or minimum node size if it is greater
38339 + * then min. I/O unit.
38340 + *
38341 + * Dark space is the space which might be used, or might not, depending
38342 + * on which node should be written to the LEB. Its watermark is
38343 + * equivalent to maximum UBIFS node size.
38344 + */
38345 + c->dead_wm = ALIGN(MIN_WRITE_SZ, c->min_io_size);
38346 + c->dark_wm = ALIGN(UBIFS_MAX_NODE_SZ, c->min_io_size);
38347 +
38348 + return 0;
38349 +}
38350 +
38351 +/**
38352 + * bud_wbuf_callback - bud LEB write-buffer synchronization call-back.
38353 + * @c: UBIFS file-system description object
38354 + * @lnum: LEB the write-buffer was synchronized to
38355 + * @free: how many free bytes left in this LEB
38356 + * @pad: how many bytes were padded
38357 + *
38358 + * This is a callback function which is called by the I/O unit when the
38359 + * write-buffer is synchronized. We need this to correctly maintain space
38360 + * accounting in bud logical eraseblocks. This function returns zero in case of
38361 + * success and a negative error code in case of failure.
38362 + *
38363 + * This function actually belongs to the journal, but we keep it here because
38364 + * we want to keep it static.
38365 + */
38366 +static int bud_wbuf_callback(struct ubifs_info *c, int lnum, int free, int pad)
38367 +{
38368 + return ubifs_update_one_lp(c, lnum, free, pad, 0, 0);
38369 +}
38370 +
38371 +/*
38372 + * init_constants_late - initialize UBIFS constants.
38373 + * @c: UBIFS file-system description object
38374 + *
38375 + * This is a helper function which initializes various UBIFS constants after
38376 + * the superblock has been read. It also checks various UBIFS parameters and
38377 + * makes sure they are all right. Returns zero in case of success and a
38378 + * negative error code in case of failure.
38379 + */
38380 +static int init_constants_late(struct ubifs_info *c)
38381 +{
38382 + int tmp, err;
38383 + uint64_t tmp64;
38384 +
38385 + c->main_bytes = c->main_lebs * c->leb_size;
38386 +
38387 + c->max_znode_sz = sizeof(struct ubifs_znode) +
38388 + c->fanout * sizeof(struct ubifs_zbranch);
38389 +
38390 + tmp = ubifs_idx_node_sz(c, 1);
38391 + c->ranges[UBIFS_IDX_NODE].min_len = tmp;
38392 + c->min_idx_node_sz = ALIGN(tmp, 8);
38393 +
38394 + tmp = ubifs_idx_node_sz(c, c->fanout);
38395 + c->ranges[UBIFS_IDX_NODE].max_len = tmp;
38396 + c->max_idx_node_sz = ALIGN(tmp, 8);
38397 +
38398 + /* Make sure LEB size is large enough to fit full commit */
38399 + tmp = UBIFS_CS_NODE_SZ + UBIFS_REF_NODE_SZ * c->jhead_cnt;
38400 + tmp = ALIGN(tmp, c->min_io_size);
38401 + if (tmp > c->leb_size) {
38402 + dbg_err("too small LEB size %d, at least %d needed",
38403 + c->leb_size, tmp);
38404 + return -EINVAL;
38405 + }
38406 +
38407 + /*
38408 + * Make sure that the log is large enough to fit reference nodes for
38409 + * all buds plus one reserved LEB.
38410 + */
38411 + tmp64 = c->max_bud_bytes;
38412 + tmp = do_div(tmp64, c->leb_size);
38413 + c->max_bud_cnt = tmp64 + !!tmp;
38414 + tmp = (c->ref_node_alsz * c->max_bud_cnt + c->leb_size - 1);
38415 + tmp /= c->leb_size;
38416 + tmp += 1;
38417 + if (c->log_lebs < tmp) {
38418 + dbg_err("too small log %d LEBs, required min. %d LEBs",
38419 + c->log_lebs, tmp);
38420 + return -EINVAL;
38421 + }
38422 +
38423 + /*
38424 + * When budgeting we assume worst-case scenarios when the pages are not
38425 + * be compressed and direntries are of the maximum size.
38426 + *
38427 + * Note, data, which may be stored in inodes is budgeted separately, so
38428 + * it is not included into 'c->inode_budget'.
38429 + */
38430 + c->page_budget = UBIFS_MAX_DATA_NODE_SZ * UBIFS_BLOCKS_PER_PAGE;
38431 + c->inode_budget = UBIFS_INO_NODE_SZ;
38432 + c->dent_budget = UBIFS_MAX_DENT_NODE_SZ;
38433 +
38434 + /*
38435 + * When the amount of flash space used by buds becomes
38436 + * 'c->max_bud_bytes', UBIFS just blocks all writers and starts commit.
38437 + * The writers are unblocked when the commit is finished. To avoid
38438 + * writers to be blocked UBIFS initiates background commit in advance,
38439 + * when number of bud bytes becomes above the limit defined below.
38440 + */
38441 + c->bg_bud_bytes = (c->max_bud_bytes * 13) >> 4;
38442 +
38443 + /*
38444 + * Ensure minimum journal size. All the bytes in the journal heads are
38445 + * considered to be used, when calculating the current journal usage.
38446 + * Consequently, if the journal is too small, UBIFS will treat it as
38447 + * always full.
38448 + */
38449 + tmp64 = (uint64_t)(c->jhead_cnt + 1) * c->leb_size + 1;
38450 + if (c->bg_bud_bytes < tmp64)
38451 + c->bg_bud_bytes = tmp64;
38452 + if (c->max_bud_bytes < tmp64 + c->leb_size)
38453 + c->max_bud_bytes = tmp64 + c->leb_size;
38454 +
38455 + err = ubifs_calc_lpt_geom(c);
38456 + if (err)
38457 + return err;
38458 +
38459 + c->min_idx_lebs = ubifs_calc_min_idx_lebs(c);
38460 +
38461 + /*
38462 + * Calculate total amount of FS blocks. This number is not used
38463 + * internally because it does not make much sense for UBIFS, but it is
38464 + * necessary to report something for the 'statfs()' call.
38465 + *
38466 + * Subtract the LEB reserved for GC and the LEB which is reserved for
38467 + * deletions.
38468 + *
38469 + * Review 'ubifs_calc_available()' if changing this calculation.
38470 + */
38471 + tmp64 = c->main_lebs - 2;
38472 + tmp64 *= c->leb_size - c->dark_wm;
38473 + tmp64 = ubifs_reported_space(c, tmp64);
38474 + c->block_cnt = tmp64 >> UBIFS_BLOCK_SHIFT;
38475 +
38476 + return 0;
38477 +}
38478 +
38479 +/**
38480 + * take_gc_lnum - reserve GC LEB.
38481 + * @c: UBIFS file-system description object
38482 + *
38483 + * This function ensures that the LEB reserved for garbage collection is
38484 + * unmapped and is marked as "taken" in lprops. We also have to set free space
38485 + * to LEB size and dirty space to zero, because lprops may contain out-of-date
38486 + * information if the file-system was un-mounted before it has been committed.
38487 + * This function returns zero in case of success and a negative error code in
38488 + * case of failure.
38489 + */
38490 +static int take_gc_lnum(struct ubifs_info *c)
38491 +{
38492 + int err;
38493 +
38494 + if (c->gc_lnum == -1) {
38495 + ubifs_err("no LEB for GC");
38496 + return -EINVAL;
38497 + }
38498 +
38499 + err = ubifs_leb_unmap(c, c->gc_lnum);
38500 + if (err)
38501 + return err;
38502 +
38503 + /* And we have to tell lprops that this LEB is taken */
38504 + err = ubifs_change_one_lp(c, c->gc_lnum, c->leb_size, 0,
38505 + LPROPS_TAKEN, 0, 0);
38506 + return err;
38507 +}
38508 +
38509 +/**
38510 + * alloc_wbufs - allocate write-buffers.
38511 + * @c: UBIFS file-system description object
38512 + *
38513 + * This helper function allocates and initializes UBIFS write-buffers. Returns
38514 + * zero in case of success and %-ENOMEM in case of failure.
38515 + */
38516 +static int alloc_wbufs(struct ubifs_info *c)
38517 +{
38518 + int i, err;
38519 +
38520 + c->jheads = kzalloc(c->jhead_cnt * sizeof(struct ubifs_jhead),
38521 + GFP_KERNEL);
38522 + if (!c->jheads)
38523 + return -ENOMEM;
38524 +
38525 + /* Initialize journal heads */
38526 + for (i = 0; i < c->jhead_cnt; i++) {
38527 + INIT_LIST_HEAD(&c->jheads[i].buds_list);
38528 + err = ubifs_wbuf_init(c, &c->jheads[i].wbuf);
38529 + if (err)
38530 + return err;
38531 +
38532 + c->jheads[i].wbuf.sync_callback = &bud_wbuf_callback;
38533 + c->jheads[i].wbuf.jhead = i;
38534 + }
38535 +
38536 + c->jheads[BASEHD].wbuf.dtype = UBI_SHORTTERM;
38537 + /*
38538 + * Garbage Collector head likely contains long-term data and
38539 + * does not need to be synchronized by timer.
38540 + */
38541 + c->jheads[GCHD].wbuf.dtype = UBI_LONGTERM;
38542 + c->jheads[GCHD].wbuf.timeout = 0;
38543 +
38544 + return 0;
38545 +}
38546 +
38547 +/**
38548 + * free_wbufs - free write-buffers.
38549 + * @c: UBIFS file-system description object
38550 + */
38551 +static void free_wbufs(struct ubifs_info *c)
38552 +{
38553 + int i;
38554 +
38555 + if (c->jheads) {
38556 + for (i = 0; i < c->jhead_cnt; i++) {
38557 + kfree(c->jheads[i].wbuf.buf);
38558 + kfree(c->jheads[i].wbuf.inodes);
38559 + }
38560 + kfree(c->jheads);
38561 + c->jheads = NULL;
38562 + }
38563 +}
38564 +
38565 +/**
38566 + * free_orphans - free orphans.
38567 + * @c: UBIFS file-system description object
38568 + */
38569 +static void free_orphans(struct ubifs_info *c)
38570 +{
38571 + struct ubifs_orphan *orph;
38572 +
38573 + while (c->orph_dnext) {
38574 + orph = c->orph_dnext;
38575 + c->orph_dnext = orph->dnext;
38576 + list_del(&orph->list);
38577 + kfree(orph);
38578 + }
38579 +
38580 + while (!list_empty(&c->orph_list)) {
38581 + orph = list_entry(c->orph_list.next, struct ubifs_orphan, list);
38582 + list_del(&orph->list);
38583 + kfree(orph);
38584 + dbg_err("orphan list not empty at unmount");
38585 + }
38586 +
38587 + vfree(c->orph_buf);
38588 + c->orph_buf = NULL;
38589 +}
38590 +
38591 +/**
38592 + * free_buds - free per-bud objects.
38593 + * @c: UBIFS file-system description object
38594 + */
38595 +static void free_buds(struct ubifs_info *c)
38596 +{
38597 + struct rb_node *this = c->buds.rb_node;
38598 + struct ubifs_bud *bud;
38599 +
38600 + while (this) {
38601 + if (this->rb_left)
38602 + this = this->rb_left;
38603 + else if (this->rb_right)
38604 + this = this->rb_right;
38605 + else {
38606 + bud = rb_entry(this, struct ubifs_bud, rb);
38607 + this = rb_parent(this);
38608 + if (this) {
38609 + if (this->rb_left == &bud->rb)
38610 + this->rb_left = NULL;
38611 + else
38612 + this->rb_right = NULL;
38613 + }
38614 + kfree(bud);
38615 + }
38616 + }
38617 +}
38618 +
38619 +/**
38620 + * check_volume_empty - check if the UBI volume is empty.
38621 + * @c: UBIFS file-system description object
38622 + *
38623 + * This function checks if the UBIFS volume is empty by looking if its LEBs are
38624 + * mapped or not. The result of checking is stored in the @c->empty variable.
38625 + * Returns zero in case of success and a negative error code in case of
38626 + * failure.
38627 + */
38628 +static int check_volume_empty(struct ubifs_info *c)
38629 +{
38630 + int lnum, err;
38631 +
38632 + c->empty = 1;
38633 + for (lnum = 0; lnum < c->leb_cnt; lnum++) {
38634 + err = ubi_is_mapped(c->ubi, lnum);
38635 + if (unlikely(err < 0))
38636 + return err;
38637 + if (err == 1) {
38638 + c->empty = 0;
38639 + break;
38640 + }
38641 +
38642 + cond_resched();
38643 + }
38644 +
38645 + return 0;
38646 +}
38647 +
38648 +/*
38649 + * UBIFS mount options.
38650 + *
38651 + * Opt_fast_unmount: do not run a journal commit before un-mounting
38652 + * Opt_norm_unmount: run a journal commit before un-mounting
38653 + * Opt_err: just end of array marker
38654 + */
38655 +enum {
38656 + Opt_fast_unmount,
38657 + Opt_norm_unmount,
38658 + Opt_err,
38659 +};
38660 +
38661 +static match_table_t tokens = {
38662 + {Opt_fast_unmount, "fast_unmount"},
38663 + {Opt_norm_unmount, "norm_unmount"},
38664 + {Opt_err, NULL},
38665 +};
38666 +
38667 +/**
38668 + * ubifs_parse_options - parse mount parameters.
38669 + * @c: UBIFS file-system description object
38670 + * @options: parameters to parse
38671 + * @is_remount: non-zero if this is FS re-mount
38672 + *
38673 + * This function parses UBIFS mount options and returns zero in case success
38674 + * and a negative error code in case of failure.
38675 + */
38676 +static int ubifs_parse_options(struct ubifs_info *c, char *options,
38677 + int is_remount)
38678 +{
38679 + char *p;
38680 + substring_t args[MAX_OPT_ARGS];
38681 +
38682 + if (!options)
38683 + return 0;
38684 +
38685 + while ((p = strsep(&options, ","))) {
38686 + int token;
38687 +
38688 + if (!*p)
38689 + continue;
38690 +
38691 + token = match_token(p, tokens, args);
38692 + switch (token) {
38693 + case Opt_fast_unmount:
38694 + c->mount_opts.unmount_mode = 2;
38695 + c->fast_unmount = 1;
38696 + break;
38697 + case Opt_norm_unmount:
38698 + c->mount_opts.unmount_mode = 1;
38699 + c->fast_unmount = 0;
38700 + break;
38701 + default:
38702 + ubifs_err("unrecognized mount option \"%s\" "
38703 + "or missing value", p);
38704 + return -EINVAL;
38705 + }
38706 + }
38707 +
38708 + return 0;
38709 +}
38710 +
38711 +/**
38712 + * destroy_journal - destroy journal data structures.
38713 + * @c: UBIFS file-system description object
38714 + *
38715 + * This function destroys journal data structures including those that may have
38716 + * been created by recovery functions.
38717 + */
38718 +static void destroy_journal(struct ubifs_info *c)
38719 +{
38720 + while (!list_empty(&c->unclean_leb_list)) {
38721 + struct ubifs_unclean_leb *ucleb;
38722 +
38723 + ucleb = list_entry(c->unclean_leb_list.next,
38724 + struct ubifs_unclean_leb, list);
38725 + list_del(&ucleb->list);
38726 + kfree(ucleb);
38727 + }
38728 + while (!list_empty(&c->old_buds)) {
38729 + struct ubifs_bud *bud;
38730 +
38731 + bud = list_entry(c->old_buds.next, struct ubifs_bud, list);
38732 + list_del(&bud->list);
38733 + kfree(bud);
38734 + }
38735 + ubifs_destroy_idx_gc(c);
38736 + ubifs_destroy_size_tree(c);
38737 + ubifs_tnc_close(c);
38738 + free_buds(c);
38739 +}
38740 +
38741 +/**
38742 + * mount_ubifs - mount UBIFS file-system.
38743 + * @c: UBIFS file-system description object
38744 + *
38745 + * This function mounts UBIFS file system. Returns zero in case of success and
38746 + * a negative error code in case of failure.
38747 + *
38748 + * Note, the function does not de-allocate resources it it fails half way
38749 + * through, and the caller has to do this instead.
38750 + */
38751 +static int mount_ubifs(struct ubifs_info *c)
38752 +{
38753 + struct super_block *sb = c->vfs_sb;
38754 + int err, mounted_read_only = (sb->s_flags & MS_RDONLY);
38755 + unsigned long long x;
38756 + size_t sz;
38757 +
38758 + err = init_constants_early(c);
38759 + if (err)
38760 + return err;
38761 +
38762 +#ifdef CONFIG_UBIFS_FS_DEBUG
38763 + c->dbg_buf = vmalloc(c->leb_size);
38764 + if (!c->dbg_buf)
38765 + return -ENOMEM;
38766 +#endif
38767 +
38768 + err = check_volume_empty(c);
38769 + if (err)
38770 + goto out_free;
38771 +
38772 + if (c->empty && (mounted_read_only || c->ro_media)) {
38773 + /*
38774 + * This UBI volume is empty, and read-only, or the file system
38775 + * is mounted read-only - we cannot format it.
38776 + */
38777 + ubifs_err("can't format empty UBI volume: read-only %s",
38778 + c->ro_media ? "UBI volume" : "mount");
38779 + err = -EROFS;
38780 + goto out_free;
38781 + }
38782 +
38783 + if (c->ro_media && !mounted_read_only) {
38784 + ubifs_err("cannot mount read-write - read-only media");
38785 + err = -EROFS;
38786 + goto out_free;
38787 + }
38788 +
38789 + /*
38790 + * The requirement for the buffer is that it should fit indexing B-tree
38791 + * height amount of integers. We assume the height if the TNC tree will
38792 + * never exceed 64.
38793 + */
38794 + err = -ENOMEM;
38795 + c->bottom_up_buf = kmalloc(BOTTOM_UP_HEIGHT * sizeof(int), GFP_KERNEL);
38796 + if (!c->bottom_up_buf)
38797 + goto out_free;
38798 +
38799 + c->sbuf = vmalloc(c->leb_size);
38800 + if (!c->sbuf)
38801 + goto out_free;
38802 +
38803 + if (!mounted_read_only) {
38804 + c->ileb_buf = vmalloc(c->leb_size);
38805 + if (!c->ileb_buf)
38806 + goto out_free;
38807 + }
38808 +
38809 + err = ubifs_read_superblock(c);
38810 + if (err)
38811 + goto out_free;
38812 +
38813 + /*
38814 + * Make sure the compressor which is set as the default on in the
38815 + * superblock was actually compiled in.
38816 + */
38817 + if (!ubifs_compr_present(c->default_compr)) {
38818 + ubifs_warn("'%s' compressor is set by superblock, but not "
38819 + "compiled in", ubifs_compr_name(c->default_compr));
38820 + c->default_compr = UBIFS_COMPR_NONE;
38821 + }
38822 +
38823 + dbg_failure_mode_registration(c);
38824 +
38825 + err = init_constants_late(c);
38826 + if (err)
38827 + goto out_dereg;
38828 +
38829 + sz = ALIGN(c->max_idx_node_sz, c->min_io_size);
38830 + sz = ALIGN(sz + c->max_idx_node_sz, c->min_io_size);
38831 + c->cbuf = kmalloc(sz, GFP_NOFS);
38832 + if (!c->cbuf) {
38833 + err = -ENOMEM;
38834 + goto out_dereg;
38835 + }
38836 +
38837 + if (!mounted_read_only) {
38838 + err = alloc_wbufs(c);
38839 + if (err)
38840 + goto out_cbuf;
38841 +
38842 + /* Create background thread */
38843 + sprintf(c->bgt_name, BGT_NAME_PATTERN, c->vi.ubi_num,
38844 + c->vi.vol_id);
38845 + c->bgt = kthread_create(ubifs_bg_thread, c, c->bgt_name);
38846 + if (!c->bgt)
38847 + c->bgt = ERR_PTR(-EINVAL);
38848 + if (IS_ERR(c->bgt)) {
38849 + err = PTR_ERR(c->bgt);
38850 + c->bgt = NULL;
38851 + ubifs_err("cannot spawn \"%s\", error %d",
38852 + c->bgt_name, err);
38853 + goto out_wbufs;
38854 + }
38855 + wake_up_process(c->bgt);
38856 + }
38857 +
38858 + err = ubifs_read_master(c);
38859 + if (err)
38860 + goto out_stop;
38861 +
38862 + if ((c->mst_node->flags & cpu_to_le32(UBIFS_MST_DIRTY)) != 0) {
38863 + ubifs_msg("recovery needed");
38864 + c->need_recovery = 1;
38865 + if (!mounted_read_only) {
38866 + err = ubifs_recover_inl_heads(c, c->sbuf);
38867 + if (err)
38868 + goto out_master;
38869 + }
38870 + } else if (!mounted_read_only) {
38871 + /*
38872 + * Set the "dirty" flag so that if we reboot uncleanly we
38873 + * will notice this immediately on the next mount.
38874 + */
38875 + c->mst_node->flags |= cpu_to_le32(UBIFS_MST_DIRTY);
38876 + err = ubifs_write_master(c);
38877 + if (err)
38878 + goto out_master;
38879 + }
38880 +
38881 + err = ubifs_lpt_init(c, 1, !mounted_read_only);
38882 + if (err)
38883 + goto out_lpt;
38884 +
38885 + err = dbg_check_idx_size(c, c->old_idx_sz);
38886 + if (err)
38887 + goto out_lpt;
38888 +
38889 + err = ubifs_replay_journal(c);
38890 + if (err)
38891 + goto out_journal;
38892 +
38893 + err = ubifs_mount_orphans(c, c->need_recovery, mounted_read_only);
38894 + if (err)
38895 + goto out_orphans;
38896 +
38897 + if (!mounted_read_only) {
38898 + int lnum;
38899 +
38900 + /* Check for enough free space */
38901 + if (ubifs_calc_available(c) <= 0) {
38902 + ubifs_err("insufficient available space");
38903 + err = -EINVAL;
38904 + goto out_orphans;
38905 + }
38906 +
38907 + /* Check for enough log space */
38908 + lnum = c->lhead_lnum + 1;
38909 + if (lnum >= UBIFS_LOG_LNUM + c->log_lebs)
38910 + lnum = UBIFS_LOG_LNUM;
38911 + if (lnum == c->ltail_lnum) {
38912 + err = ubifs_consolidate_log(c);
38913 + if (err)
38914 + goto out_orphans;
38915 + }
38916 +
38917 + if (c->need_recovery) {
38918 + err = ubifs_recover_size(c);
38919 + if (err)
38920 + goto out_orphans;
38921 + err = ubifs_rcvry_gc_commit(c);
38922 + } else
38923 + err = take_gc_lnum(c);
38924 + if (err)
38925 + goto out_orphans;
38926 +
38927 + err = dbg_check_lprops(c);
38928 + if (err)
38929 + goto out_orphans;
38930 + } else if (c->need_recovery) {
38931 + err = ubifs_recover_size(c);
38932 + if (err)
38933 + goto out_orphans;
38934 + }
38935 +
38936 + spin_lock(&ubifs_infos_lock);
38937 + list_add_tail(&c->infos_list, &ubifs_infos);
38938 + spin_unlock(&ubifs_infos_lock);
38939 +
38940 + if (c->need_recovery) {
38941 + if (mounted_read_only)
38942 + ubifs_msg("recovery deferred");
38943 + else {
38944 + c->need_recovery = 0;
38945 + ubifs_msg("recovery completed");
38946 + }
38947 + }
38948 +
38949 + err = dbg_check_filesystem(c);
38950 + if (err)
38951 + goto out_infos;
38952 +
38953 + ubifs_msg("mounted UBI device %d, volume %d", c->vi.ubi_num,
38954 + c->vi.vol_id);
38955 + if (mounted_read_only)
38956 + ubifs_msg("mounted read-only");
38957 + ubifs_msg("minimal I/O unit size: %d bytes", c->min_io_size);
38958 + ubifs_msg("logical eraseblock size: %d bytes (%d KiB)",
38959 + c->leb_size, c->leb_size / 1024);
38960 + x = (unsigned long long)c->main_lebs * c->leb_size;
38961 + ubifs_msg("file system size: %lld bytes (%lld KiB, %lld MiB, "
38962 + "%d LEBs)", x, x >> 10, x >> 20, c->main_lebs);
38963 + x = (unsigned long long)c->log_lebs * c->leb_size + c->max_bud_bytes;
38964 + ubifs_msg("journal size: %lld bytes (%lld KiB, %lld MiB, "
38965 + "%d LEBs)", x, x >> 10, x >> 20,
38966 + c->log_lebs + c->max_bud_cnt);
38967 + ubifs_msg("data journal heads: %d",
38968 + c->jhead_cnt - NONDATA_JHEADS_CNT);
38969 + ubifs_msg("default compressor: %s",
38970 + ubifs_compr_name(c->default_compr));
38971 + ubifs_msg("media format %d, latest format %d",
38972 + c->fmt_version, UBIFS_FORMAT_VERSION);
38973 +
38974 + dbg_msg("compiled on: " __DATE__ " at " __TIME__);
38975 + dbg_msg("UUID: %02X%02X%02X%02X-%02X%02X"
38976 + "-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X",
38977 + c->uuid[0], c->uuid[1], c->uuid[2], c->uuid[3],
38978 + c->uuid[4], c->uuid[5], c->uuid[6], c->uuid[7],
38979 + c->uuid[8], c->uuid[9], c->uuid[10], c->uuid[11],
38980 + c->uuid[12], c->uuid[13], c->uuid[14], c->uuid[15]);
38981 + dbg_msg("fast unmount: %d", c->fast_unmount);
38982 + dbg_msg("big_lpt %d", c->big_lpt);
38983 + dbg_msg("log LEBs: %d (%d - %d)",
38984 + c->log_lebs, UBIFS_LOG_LNUM, c->log_last);
38985 + dbg_msg("LPT area LEBs: %d (%d - %d)",
38986 + c->lpt_lebs, c->lpt_first, c->lpt_last);
38987 + dbg_msg("orphan area LEBs: %d (%d - %d)",
38988 + c->orph_lebs, c->orph_first, c->orph_last);
38989 + dbg_msg("main area LEBs: %d (%d - %d)",
38990 + c->main_lebs, c->main_first, c->leb_cnt - 1);
38991 + dbg_msg("index LEBs: %d", c->lst.idx_lebs);
38992 + dbg_msg("total index bytes: %lld (%lld KiB, %lld MiB)",
38993 + c->old_idx_sz, c->old_idx_sz >> 10, c->old_idx_sz >> 20);
38994 + dbg_msg("key hash type: %d", c->key_hash_type);
38995 + dbg_msg("tree fanout: %d", c->fanout);
38996 + dbg_msg("reserved GC LEB: %d", c->gc_lnum);
38997 + dbg_msg("first main LEB: %d", c->main_first);
38998 + dbg_msg("dead watermark: %d", c->dead_wm);
38999 + dbg_msg("dark watermark: %d", c->dark_wm);
39000 + x = c->main_lebs * c->dark_wm;
39001 + dbg_msg("max. dark space: %lld (%lld KiB, %lld MiB)",
39002 + x, x >> 10, x >> 20);
39003 + dbg_msg("maximum bud bytes: %lld (%lld KiB, %lld MiB)",
39004 + c->max_bud_bytes, c->max_bud_bytes >> 10,
39005 + c->max_bud_bytes >> 20);
39006 + dbg_msg("BG commit bud bytes: %lld (%lld KiB, %lld MiB)",
39007 + c->bg_bud_bytes, c->bg_bud_bytes >> 10,
39008 + c->bg_bud_bytes >> 20);
39009 + dbg_msg("current bud bytes %lld (%lld KiB, %lld MiB)",
39010 + c->bud_bytes, c->bud_bytes >> 10, c->bud_bytes >> 20);
39011 + dbg_msg("max. seq. number: %llu", c->max_sqnum);
39012 + dbg_msg("commit number: %llu", c->cmt_no);
39013 +
39014 + return 0;
39015 +
39016 +out_infos:
39017 + spin_lock(&ubifs_infos_lock);
39018 + list_del(&c->infos_list);
39019 + spin_unlock(&ubifs_infos_lock);
39020 +out_orphans:
39021 + free_orphans(c);
39022 +out_journal:
39023 + destroy_journal(c);
39024 +out_lpt:
39025 + ubifs_lpt_free(c, 0);
39026 +out_master:
39027 + kfree(c->mst_node);
39028 + kfree(c->rcvrd_mst_node);
39029 +out_stop:
39030 + if (c->bgt)
39031 + kthread_stop(c->bgt);
39032 +out_wbufs:
39033 + free_wbufs(c);
39034 +out_cbuf:
39035 + kfree(c->cbuf);
39036 +out_dereg:
39037 + dbg_failure_mode_deregistration(c);
39038 +out_free:
39039 + vfree(c->ileb_buf);
39040 + vfree(c->sbuf);
39041 + kfree(c->bottom_up_buf);
39042 + UBIFS_DBG(vfree(c->dbg_buf));
39043 + return err;
39044 +}
39045 +
39046 +/**
39047 + * ubifs_umount - un-mount UBIFS file-system.
39048 + * @c: UBIFS file-system description object
39049 + *
39050 + * Note, this function is called to free allocated resourced when un-mounting,
39051 + * as well as free resources when an error occurred while we were half way
39052 + * through mounting (error path cleanup function). So it has to make sure the
39053 + * resource was actually allocated before freeing it.
39054 + */
39055 +static void ubifs_umount(struct ubifs_info *c)
39056 +{
39057 + dbg_gen("un-mounting UBI device %d, volume %d", c->vi.ubi_num,
39058 + c->vi.vol_id);
39059 +
39060 + spin_lock(&ubifs_infos_lock);
39061 + list_del(&c->infos_list);
39062 + spin_unlock(&ubifs_infos_lock);
39063 +
39064 + if (c->bgt)
39065 + kthread_stop(c->bgt);
39066 +
39067 + destroy_journal(c);
39068 + free_wbufs(c);
39069 + free_orphans(c);
39070 + ubifs_lpt_free(c, 0);
39071 +
39072 + kfree(c->cbuf);
39073 + kfree(c->rcvrd_mst_node);
39074 + kfree(c->mst_node);
39075 + vfree(c->sbuf);
39076 + kfree(c->bottom_up_buf);
39077 + UBIFS_DBG(vfree(c->dbg_buf));
39078 + vfree(c->ileb_buf);
39079 + dbg_failure_mode_deregistration(c);
39080 +}
39081 +
39082 +/**
39083 + * ubifs_remount_rw - re-mount in read-write mode.
39084 + * @c: UBIFS file-system description object
39085 + *
39086 + * UBIFS avoids allocating many unnecessary resources when mounted in read-only
39087 + * mode. This function allocates the needed resources and re-mounts UBIFS in
39088 + * read-write mode.
39089 + */
39090 +static int ubifs_remount_rw(struct ubifs_info *c)
39091 +{
39092 + int err, lnum;
39093 +
39094 + if (c->ro_media)
39095 + return -EINVAL;
39096 +
39097 + mutex_lock(&c->umount_mutex);
39098 + c->remounting_rw = 1;
39099 +
39100 + /* Check for enough free space */
39101 + if (ubifs_calc_available(c) <= 0) {
39102 + ubifs_err("insufficient available space");
39103 + err = -EINVAL;
39104 + goto out;
39105 + }
39106 +
39107 + if (c->old_leb_cnt != c->leb_cnt) {
39108 + struct ubifs_sb_node *sup;
39109 +
39110 + sup = ubifs_read_sb_node(c);
39111 + if (IS_ERR(sup)) {
39112 + err = PTR_ERR(sup);
39113 + goto out;
39114 + }
39115 + sup->leb_cnt = cpu_to_le32(c->leb_cnt);
39116 + err = ubifs_write_sb_node(c, sup);
39117 + if (err)
39118 + goto out;
39119 + }
39120 +
39121 + if (c->need_recovery) {
39122 + ubifs_msg("completing deferred recovery");
39123 + err = ubifs_write_rcvrd_mst_node(c);
39124 + if (err)
39125 + goto out;
39126 + err = ubifs_recover_size(c);
39127 + if (err)
39128 + goto out;
39129 + err = ubifs_clean_lebs(c, c->sbuf);
39130 + if (err)
39131 + goto out;
39132 + err = ubifs_recover_inl_heads(c, c->sbuf);
39133 + if (err)
39134 + goto out;
39135 + }
39136 +
39137 + if (!(c->mst_node->flags & cpu_to_le32(UBIFS_MST_DIRTY))) {
39138 + c->mst_node->flags |= cpu_to_le32(UBIFS_MST_DIRTY);
39139 + err = ubifs_write_master(c);
39140 + if (err)
39141 + goto out;
39142 + }
39143 +
39144 + c->ileb_buf = vmalloc(c->leb_size);
39145 + if (!c->ileb_buf) {
39146 + err = -ENOMEM;
39147 + goto out;
39148 + }
39149 +
39150 + err = ubifs_lpt_init(c, 0, 1);
39151 + if (err)
39152 + goto out;
39153 +
39154 + err = alloc_wbufs(c);
39155 + if (err)
39156 + goto out;
39157 +
39158 + ubifs_create_buds_lists(c);
39159 +
39160 + /* Create background thread */
39161 + c->bgt = kthread_create(ubifs_bg_thread, c, c->bgt_name);
39162 + if (!c->bgt)
39163 + c->bgt = ERR_PTR(-EINVAL);
39164 + if (IS_ERR(c->bgt)) {
39165 + err = PTR_ERR(c->bgt);
39166 + c->bgt = NULL;
39167 + ubifs_err("cannot spawn \"%s\", error %d",
39168 + c->bgt_name, err);
39169 + return err;
39170 + }
39171 + wake_up_process(c->bgt);
39172 +
39173 + c->orph_buf = vmalloc(c->leb_size);
39174 + if (!c->orph_buf)
39175 + return -ENOMEM;
39176 +
39177 + /* Check for enough log space */
39178 + lnum = c->lhead_lnum + 1;
39179 + if (lnum >= UBIFS_LOG_LNUM + c->log_lebs)
39180 + lnum = UBIFS_LOG_LNUM;
39181 + if (lnum == c->ltail_lnum) {
39182 + err = ubifs_consolidate_log(c);
39183 + if (err)
39184 + goto out;
39185 + }
39186 +
39187 + if (c->need_recovery)
39188 + err = ubifs_rcvry_gc_commit(c);
39189 + else
39190 + err = take_gc_lnum(c);
39191 + if (err)
39192 + goto out;
39193 +
39194 + if (c->need_recovery) {
39195 + c->need_recovery = 0;
39196 + ubifs_msg("deferred recovery completed");
39197 + }
39198 +
39199 + dbg_gen("re-mounted read-write");
39200 + c->vfs_sb->s_flags &= ~MS_RDONLY;
39201 + c->remounting_rw = 0;
39202 + mutex_unlock(&c->umount_mutex);
39203 + return 0;
39204 +
39205 +out:
39206 + vfree(c->orph_buf);
39207 + c->orph_buf = NULL;
39208 + if (c->bgt) {
39209 + kthread_stop(c->bgt);
39210 + c->bgt = NULL;
39211 + }
39212 + free_wbufs(c);
39213 + vfree(c->ileb_buf);
39214 + c->ileb_buf = NULL;
39215 + ubifs_lpt_free(c, 1);
39216 + c->remounting_rw = 0;
39217 + mutex_unlock(&c->umount_mutex);
39218 + return err;
39219 +}
39220 +
39221 +/**
39222 + * commit_on_unmount - commit the journal when un-mounting.
39223 + * @c: UBIFS file-system description object
39224 + *
39225 + * This function is called during un-mounting and it commits the journal unless
39226 + * the "fast unmount" mode is enabled. It also avoids committing the journal if
39227 + * it contains too few data.
39228 + *
39229 + * Sometimes recovery requires the journal to be committed at least once, and
39230 + * this function takes care about this.
39231 + */
39232 +static void commit_on_unmount(struct ubifs_info *c)
39233 +{
39234 + if (!c->fast_unmount) {
39235 + long long bud_bytes;
39236 +
39237 + spin_lock(&c->buds_lock);
39238 + bud_bytes = c->bud_bytes;
39239 + spin_unlock(&c->buds_lock);
39240 + if (bud_bytes > c->leb_size)
39241 + ubifs_run_commit(c);
39242 + }
39243 +}
39244 +
39245 +/**
39246 + * ubifs_remount_ro - re-mount in read-only mode.
39247 + * @c: UBIFS file-system description object
39248 + *
39249 + * We rely on VFS to have stopped writing. Possibly the background thread could
39250 + * be running a commit, however kthread_stop will wait in that case.
39251 + */
39252 +static void ubifs_remount_ro(struct ubifs_info *c)
39253 +{
39254 + int i, err;
39255 +
39256 + ubifs_assert(!c->need_recovery);
39257 + commit_on_unmount(c);
39258 +
39259 + mutex_lock(&c->umount_mutex);
39260 + if (c->bgt) {
39261 + kthread_stop(c->bgt);
39262 + c->bgt = NULL;
39263 + }
39264 +
39265 + for (i = 0; i < c->jhead_cnt; i++) {
39266 + ubifs_wbuf_sync(&c->jheads[i].wbuf);
39267 + del_timer_sync(&c->jheads[i].wbuf.timer);
39268 + }
39269 +
39270 + if (!c->ro_media) {
39271 + c->mst_node->flags &= ~cpu_to_le32(UBIFS_MST_DIRTY);
39272 + c->mst_node->flags |= cpu_to_le32(UBIFS_MST_NO_ORPHS);
39273 + c->mst_node->gc_lnum = cpu_to_le32(c->gc_lnum);
39274 + err = ubifs_write_master(c);
39275 + if (err)
39276 + ubifs_ro_mode(c, err);
39277 + }
39278 +
39279 + ubifs_destroy_idx_gc(c);
39280 + free_wbufs(c);
39281 + vfree(c->orph_buf);
39282 + c->orph_buf = NULL;
39283 + vfree(c->ileb_buf);
39284 + c->ileb_buf = NULL;
39285 + ubifs_lpt_free(c, 1);
39286 + mutex_unlock(&c->umount_mutex);
39287 +}
39288 +
39289 +static void ubifs_put_super(struct super_block *sb)
39290 +{
39291 + int i;
39292 + struct ubifs_info *c = sb->s_fs_info;
39293 +
39294 + ubifs_msg("un-mount UBI device %d, volume %d", c->vi.ubi_num,
39295 + c->vi.vol_id);
39296 + /*
39297 + * The following asserts are only valid if there has not been a failure
39298 + * of the media. For example, there will be dirty inodes if we failed
39299 + * to write them back because of I/O errors.
39300 + */
39301 + ubifs_assert(atomic_long_read(&c->dirty_pg_cnt) == 0);
39302 + ubifs_assert(atomic_long_read(&c->dirty_ino_cnt) == 0);
39303 + ubifs_assert(c->budg_idx_growth == 0);
39304 + ubifs_assert(c->budg_data_growth == 0);
39305 +
39306 + /*
39307 + * The 'c->umount_lock' prevents races between UBIFS memory shrinker
39308 + * and file system un-mount. Namely, it prevents the shrinker from
39309 + * picking this superblock for shrinking - it will be just skipped if
39310 + * the mutex is locked.
39311 + */
39312 + mutex_lock(&c->umount_mutex);
39313 + if (!(c->vfs_sb->s_flags & MS_RDONLY)) {
39314 + /*
39315 + * First of all kill the background thread to make sure it does
39316 + * not interfere with un-mounting and freeing resources.
39317 + */
39318 + if (c->bgt) {
39319 + kthread_stop(c->bgt);
39320 + c->bgt = NULL;
39321 + }
39322 +
39323 + /* Synchronize write-buffers */
39324 + if (c->jheads)
39325 + for (i = 0; i < c->jhead_cnt; i++) {
39326 + ubifs_wbuf_sync(&c->jheads[i].wbuf);
39327 + del_timer_sync(&c->jheads[i].wbuf.timer);
39328 + }
39329 +
39330 + /*
39331 + * On fatal errors c->ro_media is set to 1, in which case we do
39332 + * not write the master node.
39333 + */
39334 + if (!c->ro_media) {
39335 + /*
39336 + * We are being cleanly unmounted which means the
39337 + * orphans were killed - indicate this in the master
39338 + * node. Also save the reserved GC LEB number.
39339 + */
39340 + int err;
39341 +
39342 + c->mst_node->flags &= ~cpu_to_le32(UBIFS_MST_DIRTY);
39343 + c->mst_node->flags |= cpu_to_le32(UBIFS_MST_NO_ORPHS);
39344 + c->mst_node->gc_lnum = cpu_to_le32(c->gc_lnum);
39345 + err = ubifs_write_master(c);
39346 + if (err)
39347 + /*
39348 + * Recovery will attempt to fix the master area
39349 + * next mount, so we just print a message and
39350 + * continue to unmount normally.
39351 + */
39352 + ubifs_err("failed to write master node, "
39353 + "error %d", err);
39354 + }
39355 + }
39356 +
39357 + ubifs_umount(c);
39358 + ubi_close_volume(c->ubi);
39359 + mutex_unlock(&c->umount_mutex);
39360 + kfree(c);
39361 +}
39362 +
39363 +static int ubifs_remount_fs(struct super_block *sb, int *flags, char *data)
39364 +{
39365 + int err;
39366 + struct ubifs_info *c = sb->s_fs_info;
39367 +
39368 + dbg_gen("old flags %#lx, new flags %#x", sb->s_flags, *flags);
39369 +
39370 + err = ubifs_parse_options(c, data, 1);
39371 + if (err) {
39372 + ubifs_err("invalid or unknown remount parameter");
39373 + return err;
39374 + }
39375 + if ((sb->s_flags & MS_RDONLY) && !(*flags & MS_RDONLY)) {
39376 + err = ubifs_remount_rw(c);
39377 + if (err)
39378 + return err;
39379 + } else if (!(sb->s_flags & MS_RDONLY) && (*flags & MS_RDONLY))
39380 + ubifs_remount_ro(c);
39381 +
39382 + return 0;
39383 +}
39384 +
39385 +struct super_operations ubifs_super_operations = {
39386 + .alloc_inode = ubifs_alloc_inode,
39387 + .destroy_inode = ubifs_destroy_inode,
39388 + .put_super = ubifs_put_super,
39389 + .write_inode = ubifs_write_inode,
39390 + .delete_inode = ubifs_delete_inode,
39391 + .statfs = ubifs_statfs,
39392 + .dirty_inode = ubifs_dirty_inode,
39393 + .remount_fs = ubifs_remount_fs,
39394 + .show_options = ubifs_show_options,
39395 + .sync_fs = ubifs_sync_fs,
39396 +};
39397 +
39398 +/**
39399 + * open_ubi - parse UBI device name string and open the UBI device.
39400 + * @name: UBI volume name
39401 + * @mode: UBI volume open mode
39402 + *
39403 + * There are several ways to specify UBI volumes when mounting UBIFS:
39404 + * o ubiX_Y - UBI device number X, volume Y;
39405 + * o ubiY - UBI device number 0, volume Y;
39406 + * o ubiX:NAME - mount UBI device X, volume with name NAME;
39407 + * o ubi:NAME - mount UBI device 0, volume with name NAME.
39408 + *
39409 + * Alternative '!' separator may be used instead of ':' (because some shells
39410 + * like busybox may interpret ':' as an NFS host name separator). This function
39411 + * returns ubi volume object in case of success and a negative error code in
39412 + * case of failure.
39413 + */
39414 +static struct ubi_volume_desc *open_ubi(const char *name, int mode)
39415 +{
39416 + int dev, vol;
39417 + char *endptr;
39418 +
39419 + if (name[0] != 'u' || name[1] != 'b' || name[2] != 'i')
39420 + return ERR_PTR(-EINVAL);
39421 +
39422 + /* ubi:NAME method */
39423 + if ((name[3] == ':' || name[3] == '!') && name[4] != '\0')
39424 + return ubi_open_volume_nm(0, name + 4, mode);
39425 +
39426 + if (!isdigit(name[3]))
39427 + return ERR_PTR(-EINVAL);
39428 +
39429 + dev = simple_strtoul(name + 3, &endptr, 0);
39430 +
39431 + /* ubiY method */
39432 + if (*endptr == '\0')
39433 + return ubi_open_volume(0, dev, mode);
39434 +
39435 + /* ubiX_Y method */
39436 + if (*endptr == '_' && isdigit(endptr[1])) {
39437 + vol = simple_strtoul(endptr + 1, &endptr, 0);
39438 + if (*endptr != '\0')
39439 + return ERR_PTR(-EINVAL);
39440 + return ubi_open_volume(dev, vol, mode);
39441 + }
39442 +
39443 + /* ubiX:NAME method */
39444 + if ((*endptr == ':' || *endptr == '!') && endptr[1] != '\0')
39445 + return ubi_open_volume_nm(dev, ++endptr, mode);
39446 +
39447 + return ERR_PTR(-EINVAL);
39448 +}
39449 +
39450 +static int ubifs_fill_super(struct super_block *sb, void *data, int silent)
39451 +{
39452 + struct ubi_volume_desc *ubi = sb->s_fs_info;
39453 + struct ubifs_info *c;
39454 + struct inode *root;
39455 + int err;
39456 +
39457 + c = kzalloc(sizeof(struct ubifs_info), GFP_KERNEL);
39458 + if (!c)
39459 + return -ENOMEM;
39460 +
39461 + spin_lock_init(&c->cnt_lock);
39462 + spin_lock_init(&c->cs_lock);
39463 + spin_lock_init(&c->buds_lock);
39464 + spin_lock_init(&c->space_lock);
39465 + spin_lock_init(&c->orphan_lock);
39466 + init_rwsem(&c->commit_sem);
39467 + mutex_init(&c->lp_mutex);
39468 + mutex_init(&c->tnc_mutex);
39469 + mutex_init(&c->log_mutex);
39470 + mutex_init(&c->mst_mutex);
39471 + mutex_init(&c->umount_mutex);
39472 + init_waitqueue_head(&c->cmt_wq);
39473 + c->buds = RB_ROOT;
39474 + c->old_idx = RB_ROOT;
39475 + c->size_tree = RB_ROOT;
39476 + c->orph_tree = RB_ROOT;
39477 + INIT_LIST_HEAD(&c->infos_list);
39478 + INIT_LIST_HEAD(&c->idx_gc);
39479 + INIT_LIST_HEAD(&c->replay_list);
39480 + INIT_LIST_HEAD(&c->replay_buds);
39481 + INIT_LIST_HEAD(&c->uncat_list);
39482 + INIT_LIST_HEAD(&c->empty_list);
39483 + INIT_LIST_HEAD(&c->freeable_list);
39484 + INIT_LIST_HEAD(&c->frdi_idx_list);
39485 + INIT_LIST_HEAD(&c->unclean_leb_list);
39486 + INIT_LIST_HEAD(&c->old_buds);
39487 + INIT_LIST_HEAD(&c->orph_list);
39488 + INIT_LIST_HEAD(&c->orph_new);
39489 +
39490 + c->highest_inum = UBIFS_FIRST_INO;
39491 + get_random_bytes(&c->vfs_gen, sizeof(int));
39492 + c->lhead_lnum = c->ltail_lnum = UBIFS_LOG_LNUM;
39493 +
39494 + ubi_get_volume_info(ubi, &c->vi);
39495 + ubi_get_device_info(c->vi.ubi_num, &c->di);
39496 +
39497 + /* Re-open the UBI device in read-write mode */
39498 + c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READWRITE);
39499 + if (IS_ERR(c->ubi)) {
39500 + err = PTR_ERR(c->ubi);
39501 + goto out_free;
39502 + }
39503 +
39504 + err = ubifs_parse_options(c, data, 0);
39505 + if (err)
39506 + goto out_close;
39507 +
39508 + c->vfs_sb = sb;
39509 +
39510 + sb->s_fs_info = c;
39511 + sb->s_magic = UBIFS_SUPER_MAGIC;
39512 + sb->s_blocksize = UBIFS_BLOCK_SIZE;
39513 + sb->s_blocksize_bits = UBIFS_BLOCK_SHIFT;
39514 + sb->s_dev = c->vi.cdev;
39515 + sb->s_maxbytes = c->max_inode_sz = key_max_inode_size(c);
39516 + if (c->max_inode_sz > MAX_LFS_FILESIZE)
39517 + sb->s_maxbytes = c->max_inode_sz = MAX_LFS_FILESIZE;
39518 + sb->s_op = &ubifs_super_operations;
39519 +
39520 + mutex_lock(&c->umount_mutex);
39521 + err = mount_ubifs(c);
39522 + if (err) {
39523 + ubifs_assert(err < 0);
39524 + goto out_unlock;
39525 + }
39526 +
39527 + /* Read the root inode */
39528 + root = ubifs_iget(sb, UBIFS_ROOT_INO);
39529 + if (IS_ERR(root)) {
39530 + err = PTR_ERR(root);
39531 + goto out_umount;
39532 + }
39533 +
39534 + sb->s_root = d_alloc_root(root);
39535 + if (!sb->s_root)
39536 + goto out_iput;
39537 +
39538 + mutex_unlock(&c->umount_mutex);
39539 +
39540 + return 0;
39541 +
39542 +out_iput:
39543 + iput(root);
39544 +out_umount:
39545 + ubifs_umount(c);
39546 +out_unlock:
39547 + mutex_unlock(&c->umount_mutex);
39548 +out_close:
39549 + ubi_close_volume(c->ubi);
39550 +out_free:
39551 + kfree(c);
39552 + return err;
39553 +}
39554 +
39555 +static int sb_test(struct super_block *sb, void *data)
39556 +{
39557 + dev_t *dev = data;
39558 +
39559 + return sb->s_dev == *dev;
39560 +}
39561 +
39562 +static int sb_set(struct super_block *sb, void *data)
39563 +{
39564 + dev_t *dev = data;
39565 +
39566 + sb->s_dev = *dev;
39567 + return 0;
39568 +}
39569 +
39570 +static int ubifs_get_sb(struct file_system_type *fs_type, int flags,
39571 + const char *name, void *data, struct vfsmount *mnt)
39572 +{
39573 + struct ubi_volume_desc *ubi;
39574 + struct ubi_volume_info vi;
39575 + struct super_block *sb;
39576 + int err;
39577 +
39578 + dbg_gen("name %s, flags %#x", name, flags);
39579 +
39580 + /*
39581 + * Get UBI device number and volume ID. Mount it read-only so far
39582 + * because this might be a new mount point, and UBI allows only one
39583 + * read-write user at a time.
39584 + */
39585 + ubi = open_ubi(name, UBI_READONLY);
39586 + if (IS_ERR(ubi)) {
39587 + ubifs_err("cannot open \"%s\", error %d",
39588 + name, (int)PTR_ERR(ubi));
39589 + return PTR_ERR(ubi);
39590 + }
39591 + ubi_get_volume_info(ubi, &vi);
39592 +
39593 + dbg_gen("opened ubi%d_%d", vi.ubi_num, vi.vol_id);
39594 +
39595 + sb = sget(fs_type, &sb_test, &sb_set, &vi.cdev);
39596 + if (IS_ERR(sb)) {
39597 + err = PTR_ERR(sb);
39598 + goto out_close;
39599 + }
39600 +
39601 + if (sb->s_root) {
39602 + /* A new mount point for already mounted UBIFS */
39603 + dbg_gen("this ubi volume is already mounted");
39604 + if ((flags ^ sb->s_flags) & MS_RDONLY) {
39605 + err = -EBUSY;
39606 + goto out_deact;
39607 + }
39608 + } else {
39609 + sb->s_flags = flags;
39610 + /*
39611 + * Pass 'ubi' to 'fill_super()' in sb->s_fs_info where it is
39612 + * replaced by 'c'.
39613 + */
39614 + sb->s_fs_info = ubi;
39615 + err = ubifs_fill_super(sb, data, flags & MS_SILENT ? 1 : 0);
39616 + if (err)
39617 + goto out_deact;
39618 + /* We do not support atime */
39619 + sb->s_flags |= MS_ACTIVE | MS_NOATIME;
39620 + }
39621 +
39622 + /* 'fill_super()' opens ubi again so we must close it here */
39623 + ubi_close_volume(ubi);
39624 +
39625 + return simple_set_mnt(mnt, sb);
39626 +
39627 +out_deact:
39628 + up_write(&sb->s_umount);
39629 + deactivate_super(sb);
39630 +out_close:
39631 + ubi_close_volume(ubi);
39632 + return err;
39633 +}
39634 +
39635 +static void ubifs_kill_sb(struct super_block *sb)
39636 +{
39637 + struct ubifs_info *c = sb->s_fs_info;
39638 +
39639 + /*
39640 + * We do 'commit_on_unmount()' here instead of 'ubifs_put_super()'
39641 + * in order to be outside BKL.
39642 + */
39643 + if (sb->s_root && !(sb->s_flags & MS_RDONLY))
39644 + commit_on_unmount(c);
39645 + /* The un-mount routine is actually done in put_super() */
39646 + generic_shutdown_super(sb);
39647 +}
39648 +
39649 +static struct file_system_type ubifs_fs_type = {
39650 + .name = "ubifs",
39651 + .owner = THIS_MODULE,
39652 + .get_sb = ubifs_get_sb,
39653 + .kill_sb = ubifs_kill_sb
39654 +};
39655 +
39656 +/*
39657 + * Inode slab cache constructor.
39658 + */
39659 +static void inode_slab_ctor(struct kmem_cache *cachep, void *obj)
39660 +{
39661 + struct ubifs_inode *inode = obj;
39662 + inode_init_once(&inode->vfs_inode);
39663 +}
39664 +
39665 +static int __init ubifs_init(void)
39666 +{
39667 + int err;
39668 +
39669 + BUILD_BUG_ON(sizeof(struct ubifs_ch) != 24);
39670 +
39671 + /* Make sure node sizes are 8-byte aligned */
39672 + BUILD_BUG_ON(UBIFS_CH_SZ & 7);
39673 + BUILD_BUG_ON(UBIFS_INO_NODE_SZ & 7);
39674 + BUILD_BUG_ON(UBIFS_DENT_NODE_SZ & 7);
39675 + BUILD_BUG_ON(UBIFS_XENT_NODE_SZ & 7);
39676 + BUILD_BUG_ON(UBIFS_DATA_NODE_SZ & 7);
39677 + BUILD_BUG_ON(UBIFS_TRUN_NODE_SZ & 7);
39678 + BUILD_BUG_ON(UBIFS_SB_NODE_SZ & 7);
39679 + BUILD_BUG_ON(UBIFS_MST_NODE_SZ & 7);
39680 + BUILD_BUG_ON(UBIFS_REF_NODE_SZ & 7);
39681 + BUILD_BUG_ON(UBIFS_CS_NODE_SZ & 7);
39682 + BUILD_BUG_ON(UBIFS_ORPH_NODE_SZ & 7);
39683 +
39684 + BUILD_BUG_ON(UBIFS_MAX_DENT_NODE_SZ & 7);
39685 + BUILD_BUG_ON(UBIFS_MAX_XENT_NODE_SZ & 7);
39686 + BUILD_BUG_ON(UBIFS_MAX_DATA_NODE_SZ & 7);
39687 + BUILD_BUG_ON(UBIFS_MAX_INO_NODE_SZ & 7);
39688 + BUILD_BUG_ON(UBIFS_MAX_NODE_SZ & 7);
39689 + BUILD_BUG_ON(MIN_WRITE_SZ & 7);
39690 +
39691 + /* Check min. node size */
39692 + BUILD_BUG_ON(UBIFS_INO_NODE_SZ < MIN_WRITE_SZ);
39693 + BUILD_BUG_ON(UBIFS_DENT_NODE_SZ < MIN_WRITE_SZ);
39694 + BUILD_BUG_ON(UBIFS_XENT_NODE_SZ < MIN_WRITE_SZ);
39695 + BUILD_BUG_ON(UBIFS_TRUN_NODE_SZ < MIN_WRITE_SZ);
39696 +
39697 + BUILD_BUG_ON(UBIFS_MAX_DENT_NODE_SZ > UBIFS_MAX_NODE_SZ);
39698 + BUILD_BUG_ON(UBIFS_MAX_XENT_NODE_SZ > UBIFS_MAX_NODE_SZ);
39699 + BUILD_BUG_ON(UBIFS_MAX_DATA_NODE_SZ > UBIFS_MAX_NODE_SZ);
39700 + BUILD_BUG_ON(UBIFS_MAX_INO_NODE_SZ > UBIFS_MAX_NODE_SZ);
39701 +
39702 + /* Defined node sizes */
39703 + BUILD_BUG_ON(UBIFS_SB_NODE_SZ != 4096);
39704 + BUILD_BUG_ON(UBIFS_MST_NODE_SZ != 512);
39705 + BUILD_BUG_ON(UBIFS_INO_NODE_SZ != 160);
39706 + BUILD_BUG_ON(UBIFS_REF_NODE_SZ != 64);
39707 +
39708 + /*
39709 + * We require that PAGE_CACHE_SIZE is greater-than-or-equal-to
39710 + * UBIFS_BLOCK_SIZE. It is assumed that both are powers of 2.
39711 + */
39712 + if (PAGE_CACHE_SIZE < UBIFS_BLOCK_SIZE) {
39713 + ubifs_err("VFS page cache size is %u bytes, but UBIFS requires"
39714 + " at least 4096 bytes",
39715 + (unsigned int)PAGE_CACHE_SIZE);
39716 + return -EINVAL;
39717 + }
39718 +
39719 + err = bdi_init(&ubifs_backing_dev_info);
39720 + if (err)
39721 + return err;
39722 +
39723 + err = register_filesystem(&ubifs_fs_type);
39724 + if (err) {
39725 + ubifs_err("cannot register file system, error %d", err);
39726 + goto out;
39727 + }
39728 +
39729 + err = -ENOMEM;
39730 + ubifs_inode_slab = kmem_cache_create("ubifs_inode_slab",
39731 + sizeof(struct ubifs_inode), 0,
39732 + SLAB_MEM_SPREAD | SLAB_RECLAIM_ACCOUNT,
39733 + &inode_slab_ctor);
39734 + if (!ubifs_inode_slab)
39735 + goto out_reg;
39736 +
39737 + register_shrinker(&ubifs_shrinker_info);
39738 +
39739 + err = ubifs_compressors_init();
39740 + if (err)
39741 + goto out_compr;
39742 +
39743 + return 0;
39744 +
39745 +out_compr:
39746 + unregister_shrinker(&ubifs_shrinker_info);
39747 + kmem_cache_destroy(ubifs_inode_slab);
39748 +out_reg:
39749 + unregister_filesystem(&ubifs_fs_type);
39750 +out:
39751 + bdi_destroy(&ubifs_backing_dev_info);
39752 + return err;
39753 +}
39754 +/* late_initcall to let compressors initialize first */
39755 +late_initcall(ubifs_init);
39756 +
39757 +static void __exit ubifs_exit(void)
39758 +{
39759 + ubifs_assert(list_empty(&ubifs_infos));
39760 + ubifs_assert(atomic_long_read(&ubifs_clean_zn_cnt) == 0);
39761 +
39762 + ubifs_compressors_exit();
39763 + unregister_shrinker(&ubifs_shrinker_info);
39764 + kmem_cache_destroy(ubifs_inode_slab);
39765 + unregister_filesystem(&ubifs_fs_type);
39766 + bdi_destroy(&ubifs_backing_dev_info);
39767 +}
39768 +module_exit(ubifs_exit);
39769 +
39770 +MODULE_LICENSE("GPL");
39771 +MODULE_VERSION(__stringify(UBIFS_VERSION));
39772 +MODULE_AUTHOR("Artem Bityutskiy, Adrian Hunter");
39773 +MODULE_DESCRIPTION("UBIFS - UBI File System");
39774 diff --exclude=.git -urN linux-2.6.25.6/fs/ubifs/tnc.c avr32-2.6/fs/ubifs/tnc.c
39775 --- linux-2.6.25.6/fs/ubifs/tnc.c 1970-01-01 01:00:00.000000000 +0100
39776 +++ avr32-2.6/fs/ubifs/tnc.c 2008-06-12 15:09:45.600758286 +0200
39777 @@ -0,0 +1,2961 @@
39778 +/*
39779 + * This file is part of UBIFS.
39780 + *
39781 + * Copyright (C) 2006-2008 Nokia Corporation.
39782 + *
39783 + * This program is free software; you can redistribute it and/or modify it
39784 + * under the terms of the GNU General Public License version 2 as published by
39785 + * the Free Software Foundation.
39786 + *
39787 + * This program is distributed in the hope that it will be useful, but WITHOUT
39788 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
39789 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
39790 + * more details.
39791 + *
39792 + * You should have received a copy of the GNU General Public License along with
39793 + * this program; if not, write to the Free Software Foundation, Inc., 51
39794 + * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
39795 + *
39796 + * Authors: Adrian Hunter
39797 + * Artem Bityutskiy (Битюцкий Артём)
39798 + */
39799 +
39800 +/*
39801 + * This file implements TNC (Tree Node Cache) which caches indexing nodes of
39802 + * the UBIFS B-tree.
39803 + *
39804 + * At the moment the locking rules of the TNC tree are quite simple and
39805 + * straightforward. We just have a mutex and lock it when we traverse the
39806 + * tree. If a znode is not in memory, we read it from flash while still having
39807 + * the mutex locked.
39808 + */
39809 +
39810 +#include <linux/crc32.h>
39811 +#include "ubifs.h"
39812 +
39813 +/*
39814 + * Returned codes of 'matches_name()' and 'fallible_matches_name()' functions.
39815 + * @NAME_LESS: name corresponding to the first argument is less than second
39816 + * @NAME_MATCHES: names match
39817 + * @NAME_GREATER: name corresponding to the second argument is greater than
39818 + * first
39819 + * @NOT_ON_MEDIA: node referred by zbranch does not exist on the media
39820 + *
39821 + * These constants were introduce to improve readability.
39822 + */
39823 +enum {
39824 + NAME_LESS = 0,
39825 + NAME_MATCHES = 1,
39826 + NAME_GREATER = 2,
39827 + NOT_ON_MEDIA = 3,
39828 +};
39829 +
39830 +/**
39831 + * insert_old_idx - record an index node obsoleted since the last commit start.
39832 + * @c: UBIFS file-system description object
39833 + * @lnum: LEB number of obsoleted index node
39834 + * @offs: offset of obsoleted index node
39835 + *
39836 + * Returns %0 on success, and a negative error code on failure.
39837 + *
39838 + * For recovery, there must always be a complete intact version of the index on
39839 + * flash at all times. That is called the "old index". It is the index as at the
39840 + * time of the last successful commit. Many of the index nodes in the old index
39841 + * may be dirty, but they must not be erased until the next successful commit
39842 + * (at which point that index becomes the old index).
39843 + *
39844 + * That means that the garbage collection and the in-the-gaps method of
39845 + * committing must be able to determine if an index node is in the old index.
39846 + * Most of the old index nodes can be found by looking up the TNC using the
39847 + * 'lookup_znode()' function. However, some of the old index nodes may have
39848 + * been deleted from the current index or may have been changed so much that
39849 + * they cannot be easily found. In those cases, an entry is added to an RB-tree.
39850 + * That is what this function does. The RB-tree is ordered by LEB number and
39851 + * offset because they uniquely identify the old index node.
39852 + */
39853 +static int insert_old_idx(struct ubifs_info *c, int lnum, int offs)
39854 +{
39855 + struct ubifs_old_idx *old_idx, *o;
39856 + struct rb_node **p, *parent = NULL;
39857 +
39858 + old_idx = kmalloc(sizeof(struct ubifs_old_idx), GFP_NOFS);
39859 + if (unlikely(!old_idx))
39860 + return -ENOMEM;
39861 + old_idx->lnum = lnum;
39862 + old_idx->offs = offs;
39863 +
39864 + p = &c->old_idx.rb_node;
39865 + while (*p) {
39866 + parent = *p;
39867 + o = rb_entry(parent, struct ubifs_old_idx, rb);
39868 + if (lnum < o->lnum)
39869 + p = &(*p)->rb_left;
39870 + else if (lnum > o->lnum)
39871 + p = &(*p)->rb_right;
39872 + else if (offs < o->offs)
39873 + p = &(*p)->rb_left;
39874 + else if (offs > o->offs)
39875 + p = &(*p)->rb_right;
39876 + else {
39877 + ubifs_err("old idx added twice!");
39878 + kfree(old_idx);
39879 + return 0;
39880 + }
39881 + }
39882 + rb_link_node(&old_idx->rb, parent, p);
39883 + rb_insert_color(&old_idx->rb, &c->old_idx);
39884 + return 0;
39885 +}
39886 +
39887 +/**
39888 + * insert_old_idx_znode - record a znode obsoleted since last commit start.
39889 + * @c: UBIFS file-system description object
39890 + * @znode: znode of obsoleted index node
39891 + *
39892 + * Returns %0 on success, and a negative error code on failure.
39893 + */
39894 +int insert_old_idx_znode(struct ubifs_info *c, struct ubifs_znode *znode)
39895 +{
39896 + if (znode->parent) {
39897 + struct ubifs_zbranch *zbr;
39898 +
39899 + zbr = &znode->parent->zbranch[znode->iip];
39900 + if (zbr->len)
39901 + return insert_old_idx(c, zbr->lnum, zbr->offs);
39902 + } else
39903 + if (c->zroot.len)
39904 + return insert_old_idx(c, c->zroot.lnum,
39905 + c->zroot.offs);
39906 + return 0;
39907 +}
39908 +
39909 +/**
39910 + * ins_clr_old_idx_znode - record a znode obsoleted since last commit start.
39911 + * @c: UBIFS file-system description object
39912 + * @znode: znode of obsoleted index node
39913 + *
39914 + * Returns %0 on success, and a negative error code on failure.
39915 + */
39916 +static int ins_clr_old_idx_znode(struct ubifs_info *c,
39917 + struct ubifs_znode *znode)
39918 +{
39919 + int err;
39920 +
39921 + if (znode->parent) {
39922 + struct ubifs_zbranch *zbr;
39923 +
39924 + zbr = &znode->parent->zbranch[znode->iip];
39925 + if (zbr->len) {
39926 + err = insert_old_idx(c, zbr->lnum, zbr->offs);
39927 + if (err)
39928 + return err;
39929 + zbr->lnum = 0;
39930 + zbr->offs = 0;
39931 + zbr->len = 0;
39932 + }
39933 + } else
39934 + if (c->zroot.len) {
39935 + err = insert_old_idx(c, c->zroot.lnum, c->zroot.offs);
39936 + if (err)
39937 + return err;
39938 + c->zroot.lnum = 0;
39939 + c->zroot.offs = 0;
39940 + c->zroot.len = 0;
39941 + }
39942 + return 0;
39943 +}
39944 +
39945 +/**
39946 + * destroy_old_idx - destroy the old_idx RB-tree.
39947 + * @c: UBIFS file-system description object
39948 + *
39949 + * During start commit, the old_idx RB-tree is used to avoid overwriting index
39950 + * nodes that were in the index last commit but have since been deleted. This
39951 + * is necessary for recovery i.e. the old index must be kept intact until the
39952 + * new index is successfully written. The old-idx RB-tree is used for the
39953 + * in-the-gaps method of writing index nodes and is destroyed every commit.
39954 + */
39955 +void destroy_old_idx(struct ubifs_info *c)
39956 +{
39957 + struct rb_node *this = c->old_idx.rb_node;
39958 + struct ubifs_old_idx *old_idx;
39959 +
39960 + while (this) {
39961 + if (this->rb_left) {
39962 + this = this->rb_left;
39963 + continue;
39964 + } else if (this->rb_right) {
39965 + this = this->rb_right;
39966 + continue;
39967 + }
39968 + old_idx = rb_entry(this, struct ubifs_old_idx, rb);
39969 + this = rb_parent(this);
39970 + if (this) {
39971 + if (this->rb_left == &old_idx->rb)
39972 + this->rb_left = NULL;
39973 + else
39974 + this->rb_right = NULL;
39975 + }
39976 + kfree(old_idx);
39977 + }
39978 + c->old_idx = RB_ROOT;
39979 +}
39980 +
39981 +/**
39982 + * copy_znode - copy a dirty znode.
39983 + * @c: UBIFS file-system description object
39984 + * @znode: znode to copy
39985 + *
39986 + * A dirty znode being committed may not be changed, so it is copied.
39987 + */
39988 +static struct ubifs_znode *copy_znode(struct ubifs_info *c,
39989 + struct ubifs_znode *znode)
39990 +{
39991 + struct ubifs_znode *zn;
39992 +
39993 + zn = kmalloc(c->max_znode_sz, GFP_NOFS);
39994 + if (unlikely(!zn))
39995 + return ERR_PTR(-ENOMEM);
39996 +
39997 + memcpy(zn, znode, c->max_znode_sz);
39998 + zn->cnext = NULL;
39999 + __set_bit(DIRTY_ZNODE, &zn->flags);
40000 + __clear_bit(COW_ZNODE, &zn->flags);
40001 +
40002 + ubifs_assert(!test_bit(OBSOLETE_ZNODE, &znode->flags));
40003 + __set_bit(OBSOLETE_ZNODE, &znode->flags);
40004 +
40005 + if (znode->level != 0) {
40006 + int i;
40007 + const int n = zn->child_cnt;
40008 +
40009 + /* The children now have new parent */
40010 + for (i = 0; i < n; i++) {
40011 + struct ubifs_zbranch *zbr = &zn->zbranch[i];
40012 +
40013 + if (zbr->znode)
40014 + zbr->znode->parent = zn;
40015 + }
40016 + }
40017 +
40018 + atomic_long_inc(&c->dirty_zn_cnt);
40019 + return zn;
40020 +}
40021 +
40022 +/**
40023 + * add_idx_dirt - add dirt due to a dirty znode.
40024 + * @c: UBIFS file-system description object
40025 + * @lnum: LEB number of index node
40026 + * @dirt: size of index node
40027 + *
40028 + * This function updates lprops dirty space and the new size of the index.
40029 + */
40030 +static int add_idx_dirt(struct ubifs_info *c, int lnum, int dirt)
40031 +{
40032 + c->calc_idx_sz -= ALIGN(dirt, 8);
40033 + return ubifs_add_dirt(c, lnum, dirt);
40034 +}
40035 +
40036 +/**
40037 + * dirty_cow_znode - ensure a znode is not being committed.
40038 + * @c: UBIFS file-system description object
40039 + * @zbr: branch of znode to check
40040 + *
40041 + * Returns dirtied znode on success or negative error code on failure.
40042 + */
40043 +static struct ubifs_znode *dirty_cow_znode(struct ubifs_info *c,
40044 + struct ubifs_zbranch *zbr)
40045 +{
40046 + struct ubifs_znode *znode = zbr->znode;
40047 + struct ubifs_znode *zn;
40048 + int err;
40049 +
40050 + if (!test_bit(COW_ZNODE, &znode->flags)) {
40051 + /* znode is not being committed */
40052 + if (!test_and_set_bit(DIRTY_ZNODE, &znode->flags)) {
40053 + atomic_long_inc(&c->dirty_zn_cnt);
40054 + atomic_long_dec(&c->clean_zn_cnt);
40055 + atomic_long_dec(&ubifs_clean_zn_cnt);
40056 + err = add_idx_dirt(c, zbr->lnum, zbr->len);
40057 + if (unlikely(err))
40058 + return ERR_PTR(err);
40059 + }
40060 + return znode;
40061 + }
40062 +
40063 + zn = copy_znode(c, znode);
40064 + if (unlikely(IS_ERR(zn)))
40065 + return zn;
40066 +
40067 + if (zbr->len) {
40068 + err = insert_old_idx(c, zbr->lnum, zbr->offs);
40069 + if (unlikely(err))
40070 + return ERR_PTR(err);
40071 + err = add_idx_dirt(c, zbr->lnum, zbr->len);
40072 + } else
40073 + err = 0;
40074 +
40075 + zbr->znode = zn;
40076 + zbr->lnum = 0;
40077 + zbr->offs = 0;
40078 + zbr->len = 0;
40079 +
40080 + if (unlikely(err))
40081 + return ERR_PTR(err);
40082 + return zn;
40083 +}
40084 +
40085 +/**
40086 + * lnc_add - add a leaf node to the leaf node cache.
40087 + * @c: UBIFS file-system description object
40088 + * @zbr: zbranch of leaf node
40089 + * @node: leaf node
40090 + *
40091 + * Leaf nodes are non-index nodes directory entry nodes or data nodes. The
40092 + * purpose of the leaf node cache is to save re-reading the same leaf node over
40093 + * and over again. Most things are cached by VFS, however the file system must
40094 + * cache directory entries for readdir and for resolving hash collisions. The
40095 + * present implementation of the leaf node cache is extremely simple, and
40096 + * allows for error returns that are not used but that may be needed if a more
40097 + * complex implementation is created.
40098 + *
40099 + * Note, this function does not add the @node object to LNC directly, but
40100 + * allocates a copy of the object and adds the copy to LNC. The reason for this
40101 + * is that @node has been allocated outside of the TNC subsystem and will be
40102 + * used with @c->tnc_mutex unlock upon return from the TNC subsystem. But LNC
40103 + * may be changed at any time, e.g. freed by the shrinker.
40104 + */
40105 +static int lnc_add(struct ubifs_info *c, struct ubifs_zbranch *zbr,
40106 + const void *node)
40107 +{
40108 + int err;
40109 + void *lnc_node;
40110 + const struct ubifs_dent_node *dent = node;
40111 +
40112 + ubifs_assert(!zbr->leaf);
40113 + ubifs_assert(zbr->len != 0);
40114 + ubifs_assert(is_hash_key(c, &zbr->key));
40115 +
40116 + err = ubifs_validate_entry(c, dent);
40117 + if (err) {
40118 + dbg_dump_stack();
40119 + dbg_dump_node(c, dent);
40120 + return err;
40121 + }
40122 +
40123 + lnc_node = kmalloc(zbr->len, GFP_NOFS);
40124 + if (!lnc_node)
40125 + /* We don't have to have the cache, so no error */
40126 + return 0;
40127 +
40128 + memcpy(lnc_node, node, zbr->len);
40129 + zbr->leaf = lnc_node;
40130 + return 0;
40131 +}
40132 +
40133 + /**
40134 + * lnc_add_directly - add a leaf node to the leaf-node-cache.
40135 + * @c: UBIFS file-system description object
40136 + * @zbr: zbranch of leaf node
40137 + * @node: leaf node
40138 + *
40139 + * This function is similar to 'lnc_add()', but it does not create a copy of
40140 + * @node but inserts @node to TNC directly.
40141 + */
40142 +static int lnc_add_directly(struct ubifs_info *c, struct ubifs_zbranch *zbr,
40143 + void *node)
40144 +{
40145 + int err;
40146 +
40147 + ubifs_assert(!zbr->leaf);
40148 + ubifs_assert(zbr->len != 0);
40149 +
40150 + err = ubifs_validate_entry(c, node);
40151 + if (err) {
40152 + dbg_dump_stack();
40153 + dbg_dump_node(c, node);
40154 + return err;
40155 + }
40156 +
40157 + zbr->leaf = node;
40158 + return 0;
40159 +}
40160 +
40161 +/**
40162 + * lnc_free - remove a leaf node from the leaf node cache.
40163 + * @zbr: zbranch of leaf node
40164 + * @node: leaf node
40165 + */
40166 +static void lnc_free(struct ubifs_zbranch *zbr)
40167 +{
40168 + if (!zbr->leaf)
40169 + return;
40170 + kfree(zbr->leaf);
40171 + zbr->leaf = NULL;
40172 +}
40173 +
40174 +/**
40175 + * tnc_read_node_nm - read a "hashed" leaf node.
40176 + * @c: UBIFS file-system description object
40177 + * @zbr: key and position of the node
40178 + * @node: node is returned here
40179 + *
40180 + * This function reads a "hashed" node defined by @zbr from the leaf node cache
40181 + * (in it is there) or from the hash media, in which case the node is also
40182 + * added to LNC. Returns zero in case of success or a negative negative error
40183 + * code in case of failure.
40184 + */
40185 +static int tnc_read_node_nm(struct ubifs_info *c, struct ubifs_zbranch *zbr,
40186 + void *node)
40187 +{
40188 + int err;
40189 +
40190 + ubifs_assert(is_hash_key(c, &zbr->key));
40191 +
40192 + if (zbr->leaf) {
40193 + /* Read from the leaf node cache */
40194 + ubifs_assert(zbr->len != 0);
40195 + memcpy(node, zbr->leaf, zbr->len);
40196 + return 0;
40197 + }
40198 +
40199 + err = ubifs_tnc_read_node(c, zbr, node);
40200 + if (err)
40201 + return err;
40202 +
40203 + /* Add the node to the leaf node cache */
40204 + err = lnc_add(c, zbr, node);
40205 + return err;
40206 +}
40207 +
40208 +/**
40209 + * try_read_node - read a node if it is a node.
40210 + * @c: UBIFS file-system description object
40211 + * @buf: buffer to read to
40212 + * @type: node type
40213 + * @len: node length (not aligned)
40214 + * @lnum: LEB number of node to read
40215 + * @offs: offset of node to read
40216 + *
40217 + * This function tries to read a node of known type and length, checks it and
40218 + * stores it in @buf. This function returns %1 if a node is present and %0 if
40219 + * a node is not present. A negative error code is returned for I/O errors.
40220 + * This function performs that same function as ubifs_read_node except that
40221 + * it does not require that there is actually a node present and instead
40222 + * the return code indicates if a node was read.
40223 + */
40224 +static int try_read_node(const struct ubifs_info *c, void *buf, int type,
40225 + int len, int lnum, int offs)
40226 +{
40227 + int err, node_len;
40228 + struct ubifs_ch *ch = buf;
40229 + uint32_t crc, node_crc;
40230 +
40231 + dbg_io("LEB %d:%d, %s, length %d", lnum, offs, dbg_ntype(type), len);
40232 +
40233 + err = ubi_read(c->ubi, lnum, buf, offs, len);
40234 + if (err) {
40235 + ubifs_err("cannot read node type %d from LEB %d:%d, error %d",
40236 + type, lnum, offs, err);
40237 + return err;
40238 + }
40239 +
40240 + if (le32_to_cpu(ch->magic) != UBIFS_NODE_MAGIC)
40241 + return 0;
40242 +
40243 + if (ch->node_type != type)
40244 + return 0;
40245 +
40246 + node_len = le32_to_cpu(ch->len);
40247 + if (node_len != len)
40248 + return 0;
40249 +
40250 + crc = crc32(UBIFS_CRC32_INIT, buf + 8, node_len - 8);
40251 + node_crc = le32_to_cpu(ch->crc);
40252 + if (crc != node_crc)
40253 + return 0;
40254 +
40255 + return 1;
40256 +}
40257 +
40258 +/**
40259 + * fallible_read_node - try to read a leaf node.
40260 + * @c: UBIFS file-system description object
40261 + * @key: key of node to read
40262 + * @zbr: position of node
40263 + * @node: node returned
40264 + *
40265 + * This function tries to read a node and returns %1 if the node is read, %0
40266 + * if the node is not present, and a negative error code in the case of error.
40267 + */
40268 +static int fallible_read_node(struct ubifs_info *c, const union ubifs_key *key,
40269 + struct ubifs_zbranch *zbr, void *node)
40270 +{
40271 + int ret;
40272 +
40273 + dbg_tnc("LEB %d:%d, key %s", zbr->lnum, zbr->offs, DBGKEY(key));
40274 +
40275 + ret = try_read_node(c, node, key_type(c, key), zbr->len, zbr->lnum,
40276 + zbr->offs);
40277 + if (ret == 1) {
40278 + union ubifs_key node_key;
40279 + struct ubifs_dent_node *dent = node;
40280 +
40281 + /* All nodes have key in the same place */
40282 + key_read(c, &dent->key, &node_key);
40283 + if (keys_cmp(c, key, &node_key) != 0)
40284 + ret = 0;
40285 + }
40286 + if (ret == 0)
40287 + dbg_mnt("dangling branch LEB %d:%d len %d, key %s",
40288 + zbr->lnum, zbr->offs, zbr->len, DBGKEY(key));
40289 + return ret;
40290 +}
40291 +
40292 +/**
40293 + * matches_name - determine if a directory or extended attribute entry matches
40294 + * a given name.
40295 + * @c: UBIFS file-system description object
40296 + * @zbr: zbranch of dent
40297 + * @nm: name to match
40298 + *
40299 + * This function checks if xentry/direntry referred by zbranch @zbr matches name
40300 + * @nm. Returns %NAME_MATCHES if it does, %NAME_LESS if the name referred by
40301 + * @zbr is less than @nm, and %NAME_GREATER if it is greater than @nm. In case
40302 + * of failure, a negative error code is returned.
40303 + */
40304 +static int matches_name(struct ubifs_info *c, struct ubifs_zbranch *zbr,
40305 + const struct qstr *nm)
40306 +{
40307 + struct ubifs_dent_node *dent;
40308 + int nlen, err;
40309 +
40310 + /* If possible, match against the dent in the leaf node cache */
40311 + if (!zbr->leaf) {
40312 + dent = kmalloc(zbr->len, GFP_NOFS);
40313 + if (!dent)
40314 + return -ENOMEM;
40315 +
40316 + err = ubifs_tnc_read_node(c, zbr, dent);
40317 + if (err)
40318 + goto out_free;
40319 +
40320 + /* Add the node to the leaf node cache */
40321 + err = lnc_add_directly(c, zbr, dent);
40322 + if (err)
40323 + goto out_free;
40324 + } else
40325 + dent = zbr->leaf;
40326 +
40327 + nlen = le16_to_cpu(dent->nlen);
40328 + err = memcmp(dent->name, nm->name, min_t(int, nlen, nm->len));
40329 + if (err == 0) {
40330 + if (nlen == nm->len)
40331 + return NAME_MATCHES;
40332 + else if (nlen < nm->len)
40333 + return NAME_LESS;
40334 + else
40335 + return NAME_GREATER;
40336 + } else if (err < 0)
40337 + return NAME_LESS;
40338 + else
40339 + return NAME_GREATER;
40340 +
40341 +out_free:
40342 + kfree(dent);
40343 + return err;
40344 +}
40345 +
40346 +/**
40347 + * get_znode - get a TNC znode that may not be loaded yet.
40348 + * @c: UBIFS file-system description object
40349 + * @znode: parent znode
40350 + * @n: znode branch slot number
40351 + *
40352 + * This function returns the znode or a negative error code.
40353 + */
40354 +static struct ubifs_znode *get_znode(struct ubifs_info *c,
40355 + struct ubifs_znode *znode, int n)
40356 +{
40357 + struct ubifs_zbranch *zbr;
40358 +
40359 + zbr = &znode->zbranch[n];
40360 + if (zbr->znode)
40361 + znode = zbr->znode;
40362 + else
40363 + znode = ubifs_load_znode(c, zbr, znode, n);
40364 + return znode;
40365 +}
40366 +
40367 +/**
40368 + * tnc_next - find next TNC entry.
40369 + * @c: UBIFS file-system description object
40370 + * @zn: znode is passed and returned here
40371 + * @n: znode branch slot number is passed and returned here
40372 + *
40373 + * This function returns %0 if the next TNC entry is found, %-ENOENT if there is
40374 + * no next entry, or a negative error code otherwise.
40375 + */
40376 +static int tnc_next(struct ubifs_info *c, struct ubifs_znode **zn, int *n)
40377 +{
40378 + struct ubifs_znode *znode = *zn;
40379 + int nn = *n;
40380 +
40381 + nn += 1;
40382 + if (nn < znode->child_cnt) {
40383 + *n = nn;
40384 + return 0;
40385 + }
40386 + while (1) {
40387 + struct ubifs_znode *zp;
40388 +
40389 + zp = znode->parent;
40390 + if (!zp)
40391 + return -ENOENT;
40392 + nn = znode->iip + 1;
40393 + znode = zp;
40394 + if (nn < znode->child_cnt) {
40395 + znode = get_znode(c, znode, nn);
40396 + if (IS_ERR(znode))
40397 + return PTR_ERR(znode);
40398 + while (znode->level != 0) {
40399 + znode = get_znode(c, znode, 0);
40400 + if (IS_ERR(znode))
40401 + return PTR_ERR(znode);
40402 + }
40403 + nn = 0;
40404 + break;
40405 + }
40406 + }
40407 + *zn = znode;
40408 + *n = nn;
40409 + return 0;
40410 +}
40411 +
40412 +/**
40413 + * tnc_prev - find previous TNC entry.
40414 + * @c: UBIFS file-system description object
40415 + * @zn: znode is returned here
40416 + * @n: znode branch slot number is passed and returned here
40417 + *
40418 + * This function returns %0 if the previous TNC entry is found, %-ENOENT if
40419 + * there is no next entry, or a negative error code otherwise.
40420 + */
40421 +static int tnc_prev(struct ubifs_info *c, struct ubifs_znode **zn, int *n)
40422 +{
40423 + struct ubifs_znode *znode = *zn;
40424 + int nn = *n;
40425 +
40426 + if (nn > 0) {
40427 + *n = nn - 1;
40428 + return 0;
40429 + }
40430 + while (1) {
40431 + struct ubifs_znode *zp;
40432 +
40433 + zp = znode->parent;
40434 + if (!zp)
40435 + return -ENOENT;
40436 + nn = znode->iip - 1;
40437 + znode = zp;
40438 + if (nn >= 0) {
40439 + znode = get_znode(c, znode, nn);
40440 + if (IS_ERR(znode))
40441 + return PTR_ERR(znode);
40442 + while (znode->level != 0) {
40443 + nn = znode->child_cnt - 1;
40444 + znode = get_znode(c, znode, nn);
40445 + if (IS_ERR(znode))
40446 + return PTR_ERR(znode);
40447 + }
40448 + nn = znode->child_cnt - 1;
40449 + break;
40450 + }
40451 + }
40452 + *zn = znode;
40453 + *n = nn;
40454 + return 0;
40455 +}
40456 +
40457 +/**
40458 + * resolve_collision - resolve a collision.
40459 + * @c: UBIFS file-system description object
40460 + * @key: key of a directory or extended attribute entry
40461 + * @zn: znode is returned here
40462 + * @n: zbranch number is passed and returned here
40463 + * @nm: name of the entry
40464 + *
40465 + * This function is called for "hashed" keys to make sure that the found key
40466 + * really corresponds to the looked up node (directory or extended attribute
40467 + * entry). It returns %1 and sets @zn and @n if the collision is resolved.
40468 + * %0 is returned if @nm is not found and @zn and @n are set to the previous
40469 + * entry, i.e. to the entry after which @nm could follow if it were in TNC.
40470 + * This means that @n may be set to %-1 if the leftmost key in @zn is the
40471 + * previous one. A negative error code is returned on failures.
40472 + */
40473 +static int resolve_collision(struct ubifs_info *c, const union ubifs_key *key,
40474 + struct ubifs_znode **zn, int *n,
40475 + const struct qstr *nm)
40476 +{
40477 + int err;
40478 +
40479 + err = matches_name(c, &(*zn)->zbranch[*n], nm);
40480 + if (unlikely(err < 0))
40481 + return err;
40482 + if (err == NAME_MATCHES)
40483 + return 1;
40484 +
40485 + if (err == NAME_GREATER) {
40486 + /* Look left */
40487 + while (1) {
40488 + err = tnc_prev(c, zn, n);
40489 + if (err == -ENOENT) {
40490 + ubifs_assert(*n == 0);
40491 + *n = -1;
40492 + return 0;
40493 + }
40494 + if (err < 0)
40495 + return err;
40496 + if (keys_cmp(c, &(*zn)->zbranch[*n].key, key)) {
40497 + /*
40498 + * We have found the branch after which we would
40499 + * like to insert, but inserting in this znode
40500 + * may still be wrong. Consider the following 3
40501 + * znodes, in the case where we are resolving a
40502 + * collision with Key2.
40503 + *
40504 + * znode zp
40505 + * ----------------------
40506 + * level 1 | Key0 | Key1 |
40507 + * -----------------------
40508 + * | |
40509 + * znode za | | znode zb
40510 + * ------------ ------------
40511 + * level 0 | Key0 | | Key2 |
40512 + * ------------ ------------
40513 + *
40514 + * The lookup finds Key2 in znode zb. Lets say
40515 + * there is no match and the name is greater so
40516 + * we look left. When we find Key0, we end up
40517 + * here. If we return now, we will insert into
40518 + * znode za at slot n = 1. But that is invalid
40519 + * according to the parent's keys. Key2 must
40520 + * be inserted into znode zb.
40521 + *
40522 + * Note, this problem is not relevant for the
40523 + * case when we go right, because
40524 + * 'tnc_insert()' would correct the parent key.
40525 + */
40526 + if (*n == (*zn)->child_cnt - 1) {
40527 + err = tnc_next(c, zn, n);
40528 + if (err) {
40529 + /* Should be impossible */
40530 + ubifs_assert(0);
40531 + if (err == -ENOENT)
40532 + err = -EINVAL;
40533 + return err;
40534 + }
40535 + ubifs_assert(*n == 0);
40536 + *n = -1;
40537 + }
40538 + return 0;
40539 + }
40540 + err = matches_name(c, &(*zn)->zbranch[*n], nm);
40541 + if (err < 0)
40542 + return err;
40543 + if (err == NAME_LESS)
40544 + return 0;
40545 + if (err == NAME_MATCHES)
40546 + return 1;
40547 + ubifs_assert(err == NAME_GREATER);
40548 + }
40549 + } else {
40550 + int nn = *n;
40551 + struct ubifs_znode *znode = *zn;
40552 +
40553 + /* Look right */
40554 + while (1) {
40555 + err = tnc_next(c, &znode, &nn);
40556 + if (err == -ENOENT)
40557 + return 0;
40558 + if (err < 0)
40559 + return err;
40560 + if (keys_cmp(c, &znode->zbranch[nn].key, key))
40561 + return 0;
40562 + err = matches_name(c, &znode->zbranch[nn], nm);
40563 + if (err < 0)
40564 + return err;
40565 + if (err == NAME_GREATER)
40566 + return 0;
40567 + *zn = znode;
40568 + *n = nn;
40569 + if (err == NAME_MATCHES)
40570 + return 1;
40571 + ubifs_assert(err == NAME_LESS);
40572 + }
40573 + }
40574 +}
40575 +
40576 +/**
40577 + * fallible_matches_name - determine if a dent matches a given name.
40578 + * @c: UBIFS file-system description object
40579 + * @zbr: zbranch of dent
40580 + * @nm: name to match
40581 + *
40582 + * This is a "fallible" version of 'matches_name()' function which does not
40583 + * panic if the direntry/xentry referred by @zbr does not exist on the media.
40584 + *
40585 + * This function checks if xentry/direntry referred by zbranch @zbr matches name
40586 + * @nm. Returns %NAME_MATCHES it does, %NAME_LESS if the name referred by @zbr
40587 + * is less than @nm, %NAME_GREATER if it is greater than @nm, and @NOT_ON_MEDIA
40588 + * if xentry/direntry referred by @zbr does not exist on the media. A negative
40589 + * error code is returned in case of failure.
40590 + */
40591 +static int fallible_matches_name(struct ubifs_info *c,
40592 + struct ubifs_zbranch *zbr,
40593 + const struct qstr *nm)
40594 +{
40595 + struct ubifs_dent_node *dent;
40596 + int nlen, err;
40597 +
40598 + /* If possible, match against the dent in the leaf node cache */
40599 + if (!zbr->leaf) {
40600 + dent = kmalloc(zbr->len, GFP_NOFS);
40601 + if (!dent)
40602 + return -ENOMEM;
40603 +
40604 + err = fallible_read_node(c, &zbr->key, zbr, dent);
40605 + if (err < 0)
40606 + goto out_free;
40607 + if (err == 0) {
40608 + /* The node was not present */
40609 + err = NOT_ON_MEDIA;
40610 + goto out_free;
40611 + }
40612 + ubifs_assert(err == 1);
40613 +
40614 + err = lnc_add_directly(c, zbr, dent);
40615 + if (err)
40616 + goto out_free;
40617 + } else
40618 + dent = zbr->leaf;
40619 +
40620 + nlen = le16_to_cpu(dent->nlen);
40621 + err = memcmp(dent->name, nm->name, min_t(int, nlen, nm->len));
40622 + if (err == 0) {
40623 + if (nlen == nm->len)
40624 + return NAME_MATCHES;
40625 + else if (nlen < nm->len)
40626 + return NAME_LESS;
40627 + else
40628 + return NAME_GREATER;
40629 + } else if (err < 0)
40630 + return NAME_LESS;
40631 + else
40632 + return NAME_GREATER;
40633 +
40634 +out_free:
40635 + kfree(dent);
40636 + return err;
40637 +}
40638 +
40639 +/**
40640 + * fallible_resolve_collision - resolve a collision even if nodes are missing.
40641 + * @c: UBIFS file-system description object
40642 + * @key: key
40643 + * @zn: znode is returned here
40644 + * @n: branch number is passed and returned here
40645 + * @nm: name of directory entry
40646 + * @adding: indicates caller is adding a key to the TNC
40647 + *
40648 + * This is a "fallible" version of the 'resolve_collision()' function which
40649 + * does not panic if one of the nodes referred to by TNC does not exist on the
40650 + * media. This may happen when replaying the journal if a deleted node was
40651 + * Garbage-collected and the commit was not done. A branch that refers to a node
40652 + * that is not present is called a dangling branch. The following are the return
40653 + * codes for this function:
40654 + * o if @nm was found, %1 is returned and @zn and @n are set to the found
40655 + * branch;
40656 + * o if we are @adding and @nm was not found, %0 is returned;
40657 + * o if we are not @adding and @nm was not found, but a dangling branch was
40658 + * found, then %1 is returned and @zn and @n are set to the dangling branch;
40659 + * o a negative error code is returned in case of failure.
40660 + */
40661 +static int fallible_resolve_collision(struct ubifs_info *c,
40662 + const union ubifs_key *key,
40663 + struct ubifs_znode **zn, int *n,
40664 + const struct qstr *nm, int adding)
40665 +{
40666 + struct ubifs_znode *o_znode = NULL, *znode = *zn;
40667 + int uninitialized_var(o_n), err, cmp, unsure = 0, nn = *n;
40668 +
40669 + cmp = fallible_matches_name(c, &znode->zbranch[nn], nm);
40670 + if (unlikely(cmp < 0))
40671 + return cmp;
40672 + if (cmp == NAME_MATCHES)
40673 + return 1;
40674 + if (cmp == NOT_ON_MEDIA) {
40675 + o_znode = znode;
40676 + o_n = nn;
40677 + /*
40678 + * We are unlucky and hit a dangling branch straight away.
40679 + * Now we do not really know where to go to find the needed
40680 + * branch - to the left or to the right. Well, let's try left.
40681 + */
40682 + unsure = 1;
40683 + } else if (!adding)
40684 + unsure = 1; /* Remove a dangling branch wherever it is */
40685 +
40686 + if (cmp == NAME_GREATER || unsure) {
40687 + /* Look left */
40688 + while (1) {
40689 + err = tnc_prev(c, zn, n);
40690 + if (err == -ENOENT) {
40691 + ubifs_assert(*n == 0);
40692 + *n = -1;
40693 + break;
40694 + }
40695 + if (err < 0)
40696 + return err;
40697 + if (keys_cmp(c, &(*zn)->zbranch[*n].key, key)) {
40698 + /* See comments in 'resolve_collision()' */
40699 + if (*n == (*zn)->child_cnt - 1) {
40700 + err = tnc_next(c, zn, n);
40701 + if (err) {
40702 + /* Should be impossible */
40703 + ubifs_assert(0);
40704 + if (err == -ENOENT)
40705 + err = -EINVAL;
40706 + return err;
40707 + }
40708 + ubifs_assert(*n == 0);
40709 + *n = -1;
40710 + }
40711 + break;
40712 + }
40713 + err = fallible_matches_name(c, &(*zn)->zbranch[*n], nm);
40714 + if (err < 0)
40715 + return err;
40716 + if (err == NAME_MATCHES)
40717 + return 1;
40718 + if (err == NOT_ON_MEDIA) {
40719 + o_znode = *zn;
40720 + o_n = *n;
40721 + continue;
40722 + }
40723 + if (!adding)
40724 + continue;
40725 + if (err == NAME_LESS)
40726 + break;
40727 + else
40728 + unsure = 0;
40729 + }
40730 + }
40731 +
40732 + if (cmp == NAME_LESS || unsure) {
40733 + /* Look right */
40734 + *zn = znode;
40735 + *n = nn;
40736 + while (1) {
40737 + err = tnc_next(c, &znode, &nn);
40738 + if (err == -ENOENT)
40739 + break;
40740 + if (err < 0)
40741 + return err;
40742 + if (keys_cmp(c, &znode->zbranch[nn].key, key))
40743 + break;
40744 + err = fallible_matches_name(c, &znode->zbranch[nn], nm);
40745 + if (err < 0)
40746 + return err;
40747 + if (err == NAME_GREATER)
40748 + break;
40749 + *zn = znode;
40750 + *n = nn;
40751 + if (err == NAME_MATCHES)
40752 + return 1;
40753 + if (err == NOT_ON_MEDIA) {
40754 + o_znode = znode;
40755 + o_n = nn;
40756 + }
40757 + }
40758 + }
40759 +
40760 + /* Never match a dangling branch when adding */
40761 + if (adding || !o_znode)
40762 + return 0;
40763 +
40764 + dbg_mnt("dangling match LEB %d:%d len %d %s",
40765 + o_znode->zbranch[o_n].lnum, o_znode->zbranch[o_n].offs,
40766 + o_znode->zbranch[o_n].len, DBGKEY(key));
40767 + *zn = o_znode;
40768 + *n = o_n;
40769 + return 1;
40770 +}
40771 +
40772 +/**
40773 + * matches_position - determine if a zbranch matches a given position.
40774 + * @zbr: zbranch of dent
40775 + * @lnum: LEB number of dent to match
40776 + * @offs: offset of dent to match
40777 + *
40778 + * This function returns %1 if @lnum:@offs matches, and %0 otherwise.
40779 + */
40780 +static int matches_position(struct ubifs_zbranch *zbr, int lnum, int offs)
40781 +{
40782 + if (zbr->lnum == lnum && zbr->offs == offs)
40783 + return 1;
40784 + else
40785 + return 0;
40786 +}
40787 +
40788 +/**
40789 + * resolve_collision_directly - resolve a collision directly.
40790 + * @c: UBIFS file-system description object
40791 + * @key: key of directory entry
40792 + * @zn: znode is passed and returned here
40793 + * @n: zbranch number is passed and returned here
40794 + * @lnum: LEB number of dent node to match
40795 + * @offs: offset of dent node to match
40796 + *
40797 + * This function is used for "hashed" keys to make sure the found directory or
40798 + * extended attribute entry node is what was looked for. It is used when the
40799 + * flash address of the right node is known (@lnum:@offs) which makes it much
40800 + * easier to resolve collisions (no need to read entries and match full
40801 + * names). This function returns %1 and sets @zn and @n if the collision is
40802 + * resolved, %0 if @lnum:@offs is not found and @zn and @n are set to the
40803 + * previous directory entry. Otherwise a negative error code is returned.
40804 + */
40805 +static int resolve_collision_directly(struct ubifs_info *c,
40806 + const union ubifs_key *key,
40807 + struct ubifs_znode **zn, int *n,
40808 + int lnum, int offs)
40809 +{
40810 + struct ubifs_znode *znode;
40811 + int nn, err;
40812 +
40813 + znode = *zn;
40814 + nn = *n;
40815 + if (matches_position(&znode->zbranch[nn], lnum, offs))
40816 + return 1;
40817 +
40818 + /* Look left */
40819 + while (1) {
40820 + err = tnc_prev(c, &znode, &nn);
40821 + if (err == -ENOENT)
40822 + break;
40823 + if (err < 0)
40824 + return err;
40825 + if (keys_cmp(c, &znode->zbranch[nn].key, key))
40826 + break;
40827 + if (matches_position(&znode->zbranch[nn], lnum, offs)) {
40828 + *zn = znode;
40829 + *n = nn;
40830 + return 1;
40831 + }
40832 + }
40833 +
40834 + /* Look right */
40835 + znode = *zn;
40836 + nn = *n;
40837 + while (1) {
40838 + err = tnc_next(c, &znode, &nn);
40839 + if (err == -ENOENT)
40840 + return 0;
40841 + if (err < 0)
40842 + return err;
40843 + if (keys_cmp(c, &znode->zbranch[nn].key, key))
40844 + return 0;
40845 + *zn = znode;
40846 + *n = nn;
40847 + if (matches_position(&znode->zbranch[nn], lnum, offs))
40848 + return 1;
40849 + }
40850 +}
40851 +
40852 +/**
40853 + * dirty_cow_bottom_up - dirty a znode and its ancestors.
40854 + * @c: UBIFS file-system description object
40855 + * @znode: znode to dirty
40856 + *
40857 + * If we do not have a unique key that resides in a znode, then we cannot
40858 + * dirty that znode from the top down (i.e. by using lookup_level0_dirty)
40859 + * This function records the path back to the last dirty ancestor, and then
40860 + * dirties the znodes on that path.
40861 + */
40862 +static struct ubifs_znode *dirty_cow_bottom_up(struct ubifs_info *c,
40863 + struct ubifs_znode *znode)
40864 +{
40865 + struct ubifs_znode *zp;
40866 + int *path = c->bottom_up_buf, p = 0;
40867 +
40868 + ubifs_assert(c->zroot.znode);
40869 + ubifs_assert(znode);
40870 + if (c->zroot.znode->level > BOTTOM_UP_HEIGHT) {
40871 + kfree(c->bottom_up_buf);
40872 + c->bottom_up_buf = kmalloc(c->zroot.znode->level * sizeof(int),
40873 + GFP_NOFS);
40874 + if (!c->bottom_up_buf)
40875 + return ERR_PTR(-ENOMEM);
40876 + path = c->bottom_up_buf;
40877 + }
40878 + if (c->zroot.znode->level) {
40879 + /* Go up until parent is dirty */
40880 + while (1) {
40881 + int n;
40882 +
40883 + zp = znode->parent;
40884 + if (!zp)
40885 + break;
40886 + n = znode->iip;
40887 + ubifs_assert(p < c->zroot.znode->level);
40888 + path[p++] = n;
40889 + if (!zp->cnext && ubifs_zn_dirty(znode))
40890 + break;
40891 + znode = zp;
40892 + }
40893 + }
40894 +
40895 + /* Come back down, dirtying as we go */
40896 + while (1) {
40897 + struct ubifs_zbranch *zbr;
40898 +
40899 + zp = znode->parent;
40900 + if (zp) {
40901 + ubifs_assert(path[p - 1] >= 0);
40902 + ubifs_assert(path[p - 1] < zp->child_cnt);
40903 + zbr = &zp->zbranch[path[--p]];
40904 + znode = dirty_cow_znode(c, zbr);
40905 + } else {
40906 + ubifs_assert(znode == c->zroot.znode);
40907 + znode = dirty_cow_znode(c, &c->zroot);
40908 + }
40909 + if (unlikely(IS_ERR(znode)) || !p)
40910 + break;
40911 + ubifs_assert(path[p - 1] >= 0);
40912 + ubifs_assert(path[p - 1] < znode->child_cnt);
40913 + znode = znode->zbranch[path[p - 1]].znode;
40914 + }
40915 +
40916 + return znode;
40917 +}
40918 +
40919 +/**
40920 + * ubifs_lookup_level0 - search for zero-level znode.
40921 + * @c: UBIFS file-system description object
40922 + * @key: key to lookup
40923 + * @zn: znode is returned here
40924 + * @n: znode branch slot number is returned here
40925 + *
40926 + * This function looks up the TNC tree and search for zero-level znode which
40927 + * refers key @key. The found zero-level znode is returned in @zn. There are 3
40928 + * cases:
40929 + * o exact match, i.e. the found zero-level znode contains key @key, then %1
40930 + * is returned and slot number of the matched branch is stored in @n;
40931 + * o not exact match, which means that zero-level znode does not contain
40932 + * @key, then %0 is returned and slot number of the closed branch is stored
40933 + * in @n;
40934 + * o @key is so small that it is even less than the lowest key of the
40935 + * leftmost zero-level node, then %0 is returned and %0 is stored in @n.
40936 + *
40937 + * Note, when the TNC tree is traversed, some znodes may be absent, then this
40938 + * function reads corresponding indexing nodes and inserts them to TNC. In
40939 + * case of failure, a negative error code is returned.
40940 + */
40941 +int ubifs_lookup_level0(struct ubifs_info *c, const union ubifs_key *key,
40942 + struct ubifs_znode **zn, int *n)
40943 +{
40944 + int err, exact;
40945 + struct ubifs_znode *znode;
40946 + unsigned long time = get_seconds();
40947 +
40948 + dbg_tnc("search key %s", DBGKEY(key));
40949 +
40950 + znode = c->zroot.znode;
40951 + if (unlikely(!znode)) {
40952 + znode = ubifs_load_znode(c, &c->zroot, NULL, 0);
40953 + if (IS_ERR(znode))
40954 + return PTR_ERR(znode);
40955 + }
40956 +
40957 + znode->time = time;
40958 +
40959 + while (1) {
40960 + struct ubifs_zbranch *zbr;
40961 +
40962 + exact = ubifs_search_zbranch(c, znode, key, n);
40963 +
40964 + if (znode->level == 0)
40965 + break;
40966 +
40967 + if (*n < 0)
40968 + *n = 0;
40969 + zbr = &znode->zbranch[*n];
40970 +
40971 + if (zbr->znode) {
40972 + znode->time = time;
40973 + znode = zbr->znode;
40974 + continue;
40975 + }
40976 +
40977 + /* znode is not in TNC cache, load it from the media */
40978 + znode = ubifs_load_znode(c, zbr, znode, *n);
40979 + if (IS_ERR(znode))
40980 + return PTR_ERR(znode);
40981 + }
40982 +
40983 + *zn = znode;
40984 + if (exact || !is_hash_key(c, key) || *n != -1) {
40985 + dbg_tnc("found %d, lvl %d, n %d", exact, znode->level, *n);
40986 + return exact;
40987 + }
40988 +
40989 + /*
40990 + * Here is a tricky place. We have not found the key and this is a
40991 + * "hashed" key, which may collide. The rest of the code deals with
40992 + * situations like this:
40993 + *
40994 + * | 3 | 5 |
40995 + * / \
40996 + * | 3 | 5 | | 6 | 7 | (x)
40997 + *
40998 + * Or more a complex example:
40999 + *
41000 + * | 1 | 5 |
41001 + * / \
41002 + * | 1 | 3 | | 5 | 8 |
41003 + * \ /
41004 + * | 5 | 5 | | 6 | 7 | (x)
41005 + *
41006 + * In the examples, if we are looking for key "5", we may reach nodes
41007 + * marked with "(x)". In this case what we have do is to look at the
41008 + * left and see if there is "5" key there. If there is, we have to
41009 + * return it.
41010 + *
41011 + * Note, this whole situation is possible because we allow to have
41012 + * elements which are equivalent to the next key in the parent in the
41013 + * children of current znode. For example, this happens if we split a
41014 + * znode like this: | 3 | 5 | 5 | 6 | 7 |, which results in something
41015 + * like this:
41016 + * | 3 | 5 |
41017 + * / \
41018 + * | 3 | 5 | | 5 | 6 | 7 |
41019 + * ^
41020 + * And this becomes what is at the first "picture" after key "5" marked
41021 + * with "^" is removed. What could be done is we could prohibit
41022 + * splitting in the middle of the colliding sequence. Also, when
41023 + * removing the leftmost key, we would have to correct the key of the
41024 + * parent node, which would introduce additional complications. Namely,
41025 + * if we changed the the leftmost key of the parent znode, the garbage
41026 + * collector would be unable to find it (GC is doing this when GC'ing
41027 + * indexing LEBs). Although we already have an additional RB-tree where
41028 + * we save such changed znodes (see 'ins_clr_old_idx_znode()') until
41029 + * after the commit. But anyway, this does not look easy to implement
41030 + * so we did not try this.
41031 + */
41032 + err = tnc_prev(c, &znode, n);
41033 + if (err == -ENOENT) {
41034 + dbg_tnc("found 0, lvl %d, n -1", znode->level);
41035 + *n = -1;
41036 + return 0;
41037 + }
41038 + if (unlikely(err < 0))
41039 + return err;
41040 + if (keys_cmp(c, key, &znode->zbranch[*n].key)) {
41041 + dbg_tnc("found 0, lvl %d, n -1", znode->level);
41042 + *n = -1;
41043 + return 0;
41044 + }
41045 +
41046 + dbg_tnc("found 1, lvl %d, n %d", znode->level, *n);
41047 + *zn = znode;
41048 + return 1;
41049 +}
41050 +
41051 +/**
41052 + * lookup_level0_dirty - search for zero-level znode dirtying.
41053 + * @c: UBIFS file-system description object
41054 + * @key: key to lookup
41055 + * @zn: znode is returned here
41056 + * @n: znode branch slot number is returned here
41057 + *
41058 + * This function looks up the TNC tree and search for zero-level znode which
41059 + * refers key @key. The found zero-level znode is returned in @zn. There are 3
41060 + * cases:
41061 + * o exact match, i.e. the found zero-level znode contains key @key, then %1
41062 + * is returned and slot number of the matched branch is stored in @n;
41063 + * o not exact match, which means that zero-level znode does not contain @key
41064 + * then %0 is returned and slot number of the closed branch is stored in
41065 + * @n;
41066 + * o @key is so small that it is even less than the lowest key of the
41067 + * leftmost zero-level node, then %0 is returned and %-1 is stored in @n.
41068 + *
41069 + * Additionally all znodes in the path from the root to the located zero-level
41070 + * znode are marked as dirty.
41071 + *
41072 + * Note, when the TNC tree is traversed, some znodes may be absent, then this
41073 + * function reads corresponding indexing nodes and inserts them to TNC. In
41074 + * case of failure, a negative error code is returned.
41075 + */
41076 +static int lookup_level0_dirty(struct ubifs_info *c, const union ubifs_key *key,
41077 + struct ubifs_znode **zn, int *n)
41078 +{
41079 + int err, exact;
41080 + struct ubifs_znode *znode;
41081 + unsigned long time = get_seconds();
41082 +
41083 + dbg_tnc("search and dirty key %s", DBGKEY(key));
41084 +
41085 + znode = c->zroot.znode;
41086 + if (unlikely(!znode)) {
41087 + znode = ubifs_load_znode(c, &c->zroot, NULL, 0);
41088 + if (IS_ERR(znode))
41089 + return PTR_ERR(znode);
41090 + }
41091 +
41092 + znode = dirty_cow_znode(c, &c->zroot);
41093 + if (IS_ERR(znode))
41094 + return PTR_ERR(znode);
41095 +
41096 + znode->time = time;
41097 +
41098 + while (1) {
41099 + struct ubifs_zbranch *zbr;
41100 +
41101 + exact = ubifs_search_zbranch(c, znode, key, n);
41102 +
41103 + if (znode->level == 0)
41104 + break;
41105 +
41106 + if (*n < 0)
41107 + *n = 0;
41108 + zbr = &znode->zbranch[*n];
41109 +
41110 + if (zbr->znode) {
41111 + znode->time = time;
41112 + znode = dirty_cow_znode(c, zbr);
41113 + if (IS_ERR(znode))
41114 + return PTR_ERR(znode);
41115 + continue;
41116 + }
41117 +
41118 + /* znode is not in TNC cache, load it from the media */
41119 + znode = ubifs_load_znode(c, zbr, znode, *n);
41120 + if (IS_ERR(znode))
41121 + return PTR_ERR(znode);
41122 + znode = dirty_cow_znode(c, zbr);
41123 + if (IS_ERR(znode))
41124 + return PTR_ERR(znode);
41125 + }
41126 +
41127 + *zn = znode;
41128 + if (exact || !is_hash_key(c, key) || *n != -1) {
41129 + dbg_tnc("found %d, lvl %d, n %d", exact, znode->level, *n);
41130 + return exact;
41131 + }
41132 +
41133 + /*
41134 + * See huge comment at 'lookup_level0_dirty()' what is the rest of the
41135 + * code.
41136 + */
41137 + err = tnc_prev(c, &znode, n);
41138 + if (err == -ENOENT) {
41139 + *n = -1;
41140 + dbg_tnc("found 0, lvl %d, n -1", znode->level);
41141 + return 0;
41142 + }
41143 + if (unlikely(err < 0))
41144 + return err;
41145 + if (keys_cmp(c, key, &znode->zbranch[*n].key)) {
41146 + *n = -1;
41147 + dbg_tnc("found 0, lvl %d, n -1", znode->level);
41148 + return 0;
41149 + }
41150 +
41151 + if (znode->cnext || !ubifs_zn_dirty(znode)) {
41152 + znode = dirty_cow_bottom_up(c, znode);
41153 + if (IS_ERR(znode))
41154 + return PTR_ERR(znode);
41155 + }
41156 +
41157 + dbg_tnc("found 1, lvl %d, n %d", znode->level, *n);
41158 + *zn = znode;
41159 + return 1;
41160 +}
41161 +
41162 +/**
41163 + * ubifs_tnc_lookup - look up a file-system node.
41164 + * @c: UBIFS file-system description object
41165 + * @key: node key to lookup
41166 + * @node: the node is returned here
41167 + *
41168 + * This function look up and reads node with key @key. The caller has to make
41169 + * sure the @node buffer is large enough to fit the node. Returns zero in case
41170 + * of success, %-ENOENT if the node was not found, and a negative error code in
41171 + * case of failure.
41172 + */
41173 +int ubifs_tnc_lookup(struct ubifs_info *c, const union ubifs_key *key,
41174 + void *node)
41175 +{
41176 + int found, n, err;
41177 + struct ubifs_znode *znode;
41178 + struct ubifs_zbranch zbr, *zt;
41179 +
41180 + mutex_lock(&c->tnc_mutex);
41181 + found = ubifs_lookup_level0(c, key, &znode, &n);
41182 + if (!found) {
41183 + err = -ENOENT;
41184 + goto out;
41185 + } else if (found < 0) {
41186 + err = found;
41187 + goto out;
41188 + }
41189 + zt = &znode->zbranch[n];
41190 + if (is_hash_key(c, key)) {
41191 + /*
41192 + * In this case the leaf node cache gets used, so we pass the
41193 + * address of the zbranch and keep the mutex locked
41194 + */
41195 + err = tnc_read_node_nm(c, zt, node);
41196 + goto out;
41197 + }
41198 + zbr = znode->zbranch[n];
41199 + mutex_unlock(&c->tnc_mutex);
41200 +
41201 + err = ubifs_tnc_read_node(c, &zbr, node);
41202 + return err;
41203 +
41204 +out:
41205 + mutex_unlock(&c->tnc_mutex);
41206 + return err;
41207 +}
41208 +
41209 +/**
41210 + * ubifs_tnc_locate - look up a file-system node and return it and its location.
41211 + * @c: UBIFS file-system description object
41212 + * @key: node key to lookup
41213 + * @node: the node is returned here
41214 + * @lnum: LEB number is returned here
41215 + * @offs: offset is returned here
41216 + *
41217 + * This function is the same as 'ubifs_tnc_lookup()' but it returns the node
41218 + * location also. See 'ubifs_tnc_lookup()'.
41219 + */
41220 +int ubifs_tnc_locate(struct ubifs_info *c, const union ubifs_key *key,
41221 + void *node, int *lnum, int *offs)
41222 +{
41223 + int found, n, err;
41224 + struct ubifs_znode *znode;
41225 + struct ubifs_zbranch zbr, *zt;
41226 +
41227 + mutex_lock(&c->tnc_mutex);
41228 + found = ubifs_lookup_level0(c, key, &znode, &n);
41229 + if (!found) {
41230 + err = -ENOENT;
41231 + goto out;
41232 + } else if (found < 0) {
41233 + err = found;
41234 + goto out;
41235 + }
41236 + zt = &znode->zbranch[n];
41237 + if (is_hash_key(c, key)) {
41238 + /*
41239 + * In this case the leaf node cache gets used, so we pass the
41240 + * address of the zbranch and keep the mutex locked
41241 + */
41242 + *lnum = zt->lnum;
41243 + *offs = zt->offs;
41244 + err = tnc_read_node_nm(c, zt, node);
41245 + goto out;
41246 + }
41247 + zbr = znode->zbranch[n];
41248 + mutex_unlock(&c->tnc_mutex);
41249 +
41250 + *lnum = zbr.lnum;
41251 + *offs = zbr.offs;
41252 +
41253 + err = ubifs_tnc_read_node(c, &zbr, node);
41254 + return err;
41255 +
41256 +out:
41257 + mutex_unlock(&c->tnc_mutex);
41258 + return err;
41259 +}
41260 +
41261 +/**
41262 + * do_lookup_nm- look up a "hashed" node.
41263 + * directory entry file-system node.
41264 + * @c: UBIFS file-system description object
41265 + * @key: node key to lookup
41266 + * @node: the node is returned here
41267 + * @nm: node name
41268 + *
41269 + * This function look up and reads a node which contains name hash in the key.
41270 + * Since the hash may have collisions, there may be many nodes with the same
41271 + * key, so we have to sequentially look to all of them until the needed one is
41272 + * found. This function returns zero in case of success, %-ENOENT if the node
41273 + * was not found, and a negative error code in case of failure.
41274 + */
41275 +static int do_lookup_nm(struct ubifs_info *c, const union ubifs_key *key,
41276 + void *node, const struct qstr *nm)
41277 +{
41278 + int found, n, err;
41279 + struct ubifs_znode *znode;
41280 + struct ubifs_zbranch zbr;
41281 +
41282 + dbg_tnc("name '%.*s' key %s", nm->len, nm->name, DBGKEY(key));
41283 + mutex_lock(&c->tnc_mutex);
41284 + found = ubifs_lookup_level0(c, key, &znode, &n);
41285 + if (!found) {
41286 + err = -ENOENT;
41287 + goto out_unlock;
41288 + } else if (found < 0) {
41289 + err = found;
41290 + goto out_unlock;
41291 + }
41292 +
41293 + ubifs_assert(n >= 0);
41294 +
41295 + err = resolve_collision(c, key, &znode, &n, nm);
41296 + dbg_tnc("rc returned %d, znode %p, n %d", err, znode, n);
41297 + if (unlikely(err < 0))
41298 + goto out_unlock;
41299 + if (err == 0) {
41300 + err = -ENOENT;
41301 + goto out_unlock;
41302 + }
41303 +
41304 + zbr = znode->zbranch[n];
41305 + mutex_unlock(&c->tnc_mutex);
41306 +
41307 + err = tnc_read_node_nm(c, &zbr, node);
41308 + return err;
41309 +
41310 +out_unlock:
41311 + mutex_unlock(&c->tnc_mutex);
41312 + return err;
41313 +}
41314 +
41315 +/**
41316 + * ubifs_tnc_lookup_nm- look up a "hashed" node.
41317 + * directory entry file-system node.
41318 + * @c: UBIFS file-system description object
41319 + * @key: node key to lookup
41320 + * @node: the node is returned here
41321 + * @nm: node name
41322 + *
41323 + * This function look up and reads a node which contains name hash in the key.
41324 + * Since the hash may have collisions, there may be many nodes with the same
41325 + * key, so we have to sequentially look to all of them until the needed one is
41326 + * found. This function returns zero in case of success, %-ENOENT if the node
41327 + * was not found, and a negative error code in case of failure.
41328 + */
41329 +int ubifs_tnc_lookup_nm(struct ubifs_info *c, const union ubifs_key *key,
41330 + void *node, const struct qstr *nm)
41331 +{
41332 + int err, len;
41333 + const struct ubifs_dent_node *dent = node;
41334 +
41335 + /*
41336 + * We assume that in most of the cases there are no name collisions and
41337 + * 'ubifs_tnc_lookup()' returns us the right direntry.
41338 + */
41339 + err = ubifs_tnc_lookup(c, key, node);
41340 + if (err)
41341 + return err;
41342 +
41343 + len = le16_to_cpu(dent->nlen);
41344 + if (nm->len == len && !memcmp(dent->name, nm->name, len))
41345 + return 0;
41346 +
41347 + /*
41348 + * Unluckily, there are hash collisions and we have to iterate over
41349 + * them look at each direntry with colliding name hash sequentially.
41350 + */
41351 + return do_lookup_nm(c, key, node, nm);
41352 +}
41353 +
41354 +/**
41355 + * correct_parent_keys - correct parent znodes' keys.
41356 + * @c: UBIFS file-system description object
41357 + * @znode: znode to correct parent znodes for
41358 + *
41359 + * This is a helper function for 'tnc_insert()'. When the key of the leftmost
41360 + * zbranch changes, keys of parent znodes have to be corrected. This helper
41361 + * function is called in such situations and corrects the keys if needed.
41362 + */
41363 +static void correct_parent_keys(const struct ubifs_info *c,
41364 + struct ubifs_znode *znode)
41365 +{
41366 + union ubifs_key *key, *key1;
41367 +
41368 + ubifs_assert(znode->parent);
41369 + ubifs_assert(znode->iip == 0);
41370 +
41371 + key = &znode->zbranch[0].key;
41372 + key1 = &znode->parent->zbranch[0].key;
41373 +
41374 + while (keys_cmp(c, key, key1) < 0) {
41375 + key_copy(c, key, key1);
41376 + znode = znode->parent;
41377 + if (!znode->parent || znode->iip)
41378 + break;
41379 + key1 = &znode->parent->zbranch[0].key;
41380 + }
41381 +}
41382 +
41383 +/**
41384 + * insert_zbranch - insert a zbranch into a znode.
41385 + * @znode: znode into which to insert
41386 + * @zbr: zbranch to insert
41387 + * @n: slot number to insert to
41388 + *
41389 + * This is a helper function for 'tnc_insert()'. UBIFS does not allow "gaps" in
41390 + * znode's array of zbranches and keeps zbranches consolidated, so when a new
41391 + * zbranch has to be inserted to the @znode->zbranches[]' array at the @n-th
41392 + * slot, zbranches starting from @n have to be moved right.
41393 + */
41394 +static void insert_zbranch(struct ubifs_znode *znode,
41395 + const struct ubifs_zbranch *zbr, int n)
41396 +{
41397 + int i;
41398 +
41399 + ubifs_assert(ubifs_zn_dirty(znode));
41400 +
41401 + if (znode->level) {
41402 + for (i = znode->child_cnt; i > n; i--) {
41403 + znode->zbranch[i] = znode->zbranch[i - 1];
41404 + if (znode->zbranch[i].znode)
41405 + znode->zbranch[i].znode->iip = i;
41406 + }
41407 + if (zbr->znode)
41408 + zbr->znode->iip = n;
41409 + } else
41410 + for (i = znode->child_cnt; i > n; i--)
41411 + znode->zbranch[i] = znode->zbranch[i - 1];
41412 +
41413 + znode->zbranch[n] = *zbr;
41414 + znode->child_cnt += 1;
41415 +
41416 + /*
41417 + * After inserting at slot zero, the lower bound of the key range of
41418 + * this znode may have changed. If this znode is subsequently split
41419 + * then the upper bound of the key range may change, and furthermore
41420 + * it could change to be lower than the original lower bound. If that
41421 + * happens, then it will no longer be possible to find this znode in the
41422 + * TNC using the key from the index node on flash. That is bad because
41423 + * if it is not found, we will assume it is obsolete and may overwrite
41424 + * it. Then if there is an unclean unmount, we will start using the
41425 + * old index which will be broken.
41426 + *
41427 + * So we first mark znodes that have insertions at slot zero, and then
41428 + * if they are split we add their lnum/offs to the old_idx tree.
41429 + */
41430 + if (n == 0)
41431 + znode->alt = 1;
41432 +}
41433 +
41434 +/**
41435 + * tnc_insert - insert a node into TNC.
41436 + * @c: UBIFS file-system description object
41437 + * @znode: znode to insert into
41438 + * @zbr: branch to insert
41439 + * @n: slot number to insert new zbranch to
41440 + *
41441 + * This function inserts a new node described by @zbr into znode @znode. If
41442 + * znode does not have a free slot for new zbranch, it is split. Parent znodes
41443 + * are splat as well if needed. Returns zero in case of success or a negative
41444 + * error code in case of failure.
41445 + */
41446 +static int tnc_insert(struct ubifs_info *c, struct ubifs_znode *znode,
41447 + struct ubifs_zbranch *zbr, int n)
41448 +{
41449 + struct ubifs_znode *zn, *zi, *zp;
41450 + int i, keep, move, appending = 0;
41451 + union ubifs_key *key = &zbr->key;
41452 +
41453 + ubifs_assert(n >= 0 && n <= c->fanout);
41454 +
41455 + /* Implement naive insert for now */
41456 +again:
41457 + zp = znode->parent;
41458 + if (znode->child_cnt < c->fanout) {
41459 + ubifs_assert(n != c->fanout);
41460 + dbg_tnc("inserted at %d level %d, key %s", n, znode->level,
41461 + DBGKEY(key));
41462 +
41463 + insert_zbranch(znode, zbr, n);
41464 +
41465 + /* Ensure parent's key is correct */
41466 + if (n == 0 && zp && znode->iip == 0)
41467 + correct_parent_keys(c, znode);
41468 +
41469 + return 0;
41470 + }
41471 +
41472 + /*
41473 + * Unfortunately, @znode does not have more empty slots and we have to
41474 + * split it.
41475 + */
41476 + dbg_tnc("splitting level %d, key %s", znode->level, DBGKEY(key));
41477 +
41478 + if (znode->alt)
41479 + /*
41480 + * We can no longer be sure of finding this znode by key, so we
41481 + * record it in the old_idx tree.
41482 + */
41483 + ins_clr_old_idx_znode(c, znode);
41484 +
41485 + zn = kzalloc(c->max_znode_sz, GFP_NOFS);
41486 + if (!zn)
41487 + return -ENOMEM;
41488 + zn->parent = zp;
41489 + zn->level = znode->level;
41490 +
41491 + /* Decide where to split */
41492 + if (znode->level == 0 && n == c->fanout &&
41493 + key_type(c, key) == UBIFS_DATA_KEY) {
41494 + union ubifs_key *key1;
41495 +
41496 + /*
41497 + * If this is an inode which is being appended - do not split
41498 + * it because no other zbranches can be inserted between
41499 + * zbranches of consecutive data nodes anyway.
41500 + */
41501 + key1 = &znode->zbranch[n - 1].key;
41502 + if (key_inum(c, key1) == key_inum(c, key) &&
41503 + key_type(c, key1) == UBIFS_DATA_KEY &&
41504 + key_block(c, key1) == key_block(c, key) - 1)
41505 + appending = 1;
41506 + }
41507 +
41508 + if (appending) {
41509 + keep = c->fanout;
41510 + move = 0;
41511 + } else {
41512 + keep = (c->fanout + 1) / 2;
41513 + move = c->fanout - keep;
41514 + }
41515 +
41516 + /*
41517 + * Although we don't at present, we could look at the neighbors and see
41518 + * if we can move some zbranches there.
41519 + */
41520 +
41521 + if (n < keep) {
41522 + /* Insert into existing znode */
41523 + zi = znode;
41524 + move += 1;
41525 + keep -= 1;
41526 + } else {
41527 + /* Insert into new znode */
41528 + zi = zn;
41529 + n -= keep;
41530 + /* Re-parent */
41531 + if (zn->level != 0)
41532 + zbr->znode->parent = zn;
41533 + }
41534 +
41535 + __set_bit(DIRTY_ZNODE, &zn->flags);
41536 + atomic_long_inc(&c->dirty_zn_cnt);
41537 +
41538 + zn->child_cnt = move;
41539 + znode->child_cnt = keep;
41540 +
41541 + dbg_tnc("moving %d, keeping %d", move, keep);
41542 +
41543 + /* Move zbranch */
41544 + for (i = 0; i < move; i++) {
41545 + zn->zbranch[i] = znode->zbranch[keep + i];
41546 + /* Re-parent */
41547 + if (zn->level != 0)
41548 + if (zn->zbranch[i].znode) {
41549 + zn->zbranch[i].znode->parent = zn;
41550 + zn->zbranch[i].znode->iip = i;
41551 + }
41552 + }
41553 +
41554 + /* Insert new key and branch */
41555 + dbg_tnc("inserting at %d level %d, key %s", n, zn->level, DBGKEY(key));
41556 +
41557 + insert_zbranch(zi, zbr, n);
41558 +
41559 + /* Insert new znode (produced by spitting) into the parent */
41560 + if (zp) {
41561 + i = n;
41562 + /* Locate insertion point */
41563 + n = znode->iip + 1;
41564 + if (appending && n != c->fanout)
41565 + appending = 0;
41566 +
41567 + if (i == 0 && zi == znode && znode->iip == 0)
41568 + correct_parent_keys(c, znode);
41569 +
41570 + /* Tail recursion */
41571 + zbr->key = zn->zbranch[0].key;
41572 + zbr->znode = zn;
41573 + zbr->lnum = 0;
41574 + zbr->offs = 0;
41575 + zbr->len = 0;
41576 + znode = zp;
41577 +
41578 + goto again;
41579 + }
41580 +
41581 + /* We have to split root znode */
41582 + dbg_tnc("creating new zroot at level %d", znode->level + 1);
41583 +
41584 + zi = kzalloc(c->max_znode_sz, GFP_NOFS);
41585 + if (!zi)
41586 + return -ENOMEM;
41587 +
41588 + zi->child_cnt = 2;
41589 + zi->level = znode->level + 1;
41590 +
41591 + __set_bit(DIRTY_ZNODE, &zi->flags);
41592 + atomic_long_inc(&c->dirty_zn_cnt);
41593 +
41594 + zi->zbranch[0].key = znode->zbranch[0].key;
41595 + zi->zbranch[0].znode = znode;
41596 + zi->zbranch[0].lnum = c->zroot.lnum;
41597 + zi->zbranch[0].offs = c->zroot.offs;
41598 + zi->zbranch[0].len = c->zroot.len;
41599 + zi->zbranch[1].key = zn->zbranch[0].key;
41600 + zi->zbranch[1].znode = zn;
41601 +
41602 + c->zroot.lnum = 0;
41603 + c->zroot.offs = 0;
41604 + c->zroot.len = 0;
41605 + c->zroot.znode = zi;
41606 +
41607 + zn->parent = zi;
41608 + zn->iip = 1;
41609 + znode->parent = zi;
41610 + znode->iip = 0;
41611 +
41612 + return 0;
41613 +}
41614 +
41615 +/**
41616 + * ubifs_tnc_add - add a node to TNC.
41617 + * @c: UBIFS file-system description object
41618 + * @key: key to add
41619 + * @lnum: LEB number of node
41620 + * @offs: node offset
41621 + * @len: node length
41622 + *
41623 + * This function adds a node with key @key to TNC. The node may be new or it may
41624 + * obsolete some existing one. Returns %0 on success or negative error code on
41625 + * failure.
41626 + */
41627 +int ubifs_tnc_add(struct ubifs_info *c, const union ubifs_key *key, int lnum,
41628 + int offs, int len)
41629 +{
41630 + int found, n, err = 0;
41631 + struct ubifs_znode *znode;
41632 +
41633 + mutex_lock(&c->tnc_mutex);
41634 + dbg_tnc("%d:%d, len %d, key %s", lnum, offs, len, DBGKEY(key));
41635 + found = lookup_level0_dirty(c, key, &znode, &n);
41636 + if (!found) {
41637 + struct ubifs_zbranch zbr;
41638 +
41639 + zbr.znode = NULL;
41640 + zbr.lnum = lnum;
41641 + zbr.offs = offs;
41642 + zbr.len = len;
41643 + key_copy(c, key, &zbr.key);
41644 + err = tnc_insert(c, znode, &zbr, n + 1);
41645 + } else if (found == 1) {
41646 + struct ubifs_zbranch *zbr = &znode->zbranch[n];
41647 +
41648 + lnc_free(zbr);
41649 + err = ubifs_add_dirt(c, zbr->lnum, zbr->len);
41650 + zbr->lnum = lnum;
41651 + zbr->offs = offs;
41652 + zbr->len = len;
41653 + } else
41654 + err = found;
41655 + if (!err)
41656 + err = dbg_check_tnc(c, 0);
41657 + mutex_unlock(&c->tnc_mutex);
41658 +
41659 + return err;
41660 +}
41661 +
41662 +/**
41663 + * ubifs_tnc_replace - replace a node in the TNC only if the old node is found.
41664 + * @c: UBIFS file-system description object
41665 + * @key: key to add
41666 + * @old_lnum: LEB number of old node
41667 + * @old_offs: old node offset
41668 + * @lnum: LEB number of node
41669 + * @offs: node offset
41670 + * @len: node length
41671 + *
41672 + * This function replaces a node with key @key in the TNC only if the old node
41673 + * is found. This function is called by garbage collection when node are moved.
41674 + * Returns %0 on success or negative error code on failure.
41675 + */
41676 +int ubifs_tnc_replace(struct ubifs_info *c, const union ubifs_key *key,
41677 + int old_lnum, int old_offs, int lnum, int offs, int len)
41678 +{
41679 + int found, n, err = 0;
41680 + struct ubifs_znode *znode;
41681 +
41682 + mutex_lock(&c->tnc_mutex);
41683 + dbg_tnc("old LEB %d:%d, new LEB %d:%d, len %d, key %s", old_lnum,
41684 + old_offs, lnum, offs, len, DBGKEY(key));
41685 + found = lookup_level0_dirty(c, key, &znode, &n);
41686 + if (found < 0) {
41687 + err = found;
41688 + goto out_unlock;
41689 + }
41690 +
41691 + if (found == 1) {
41692 + struct ubifs_zbranch *zbr = &znode->zbranch[n];
41693 +
41694 + found = 0;
41695 + if (zbr->lnum == old_lnum && zbr->offs == old_offs) {
41696 + lnc_free(zbr);
41697 + err = ubifs_add_dirt(c, zbr->lnum, zbr->len);
41698 + if (err)
41699 + goto out_unlock;
41700 + zbr->lnum = lnum;
41701 + zbr->offs = offs;
41702 + zbr->len = len;
41703 + found = 1;
41704 + } else if (is_hash_key(c, key)) {
41705 + found = resolve_collision_directly(c, key, &znode, &n,
41706 + old_lnum, old_offs);
41707 + dbg_tnc("rc returned %d, znode %p, n %d, LEB %d:%d",
41708 + found, znode, n, old_lnum, old_offs);
41709 + if (found < 0) {
41710 + err = found;
41711 + goto out_unlock;
41712 + }
41713 +
41714 + if (found) {
41715 + /* Ensure the znode is dirtied */
41716 + if (znode->cnext || !ubifs_zn_dirty(znode)) {
41717 + znode = dirty_cow_bottom_up(c,
41718 + znode);
41719 + if (IS_ERR(znode)) {
41720 + err = PTR_ERR(znode);
41721 + goto out_unlock;
41722 + }
41723 + }
41724 + zbr = &znode->zbranch[n];
41725 + lnc_free(zbr);
41726 + err = ubifs_add_dirt(c, zbr->lnum,
41727 + zbr->len);
41728 + if (err)
41729 + goto out_unlock;
41730 + zbr->lnum = lnum;
41731 + zbr->offs = offs;
41732 + zbr->len = len;
41733 + }
41734 + }
41735 + }
41736 +
41737 + if (!found)
41738 + err = ubifs_add_dirt(c, lnum, len);
41739 +
41740 + if (!err)
41741 + err = dbg_check_tnc(c, 0);
41742 +
41743 +out_unlock:
41744 + mutex_unlock(&c->tnc_mutex);
41745 + return err;
41746 +}
41747 +
41748 +/**
41749 + * ubifs_tnc_add_nm - add a "hashed" node to TNC.
41750 + * @c: UBIFS file-system description object
41751 + * @key: key to add
41752 + * @lnum: LEB number of node
41753 + * @offs: node offset
41754 + * @len: node length
41755 + * @nm: node name
41756 + *
41757 + * This is the same as 'ubifs_tnc_add()' but it should be used with keys which
41758 + * may have collisions, like directory entry keys.
41759 + */
41760 +int ubifs_tnc_add_nm(struct ubifs_info *c, const union ubifs_key *key,
41761 + int lnum, int offs, int len, const struct qstr *nm)
41762 +{
41763 + int found, n, err = 0;
41764 + struct ubifs_znode *znode;
41765 +
41766 + mutex_lock(&c->tnc_mutex);
41767 + dbg_tnc("LEB %d:%d, name '%.*s', key %s", lnum, offs, nm->len, nm->name,
41768 + DBGKEY(key));
41769 + found = lookup_level0_dirty(c, key, &znode, &n);
41770 + if (found < 0) {
41771 + err = found;
41772 + goto out_unlock;
41773 + }
41774 +
41775 + if (found == 1) {
41776 + if (c->replaying)
41777 + found = fallible_resolve_collision(c, key, &znode, &n,
41778 + nm, 1);
41779 + else
41780 + found = resolve_collision(c, key, &znode, &n, nm);
41781 + dbg_tnc("rc returned %d, znode %p, n %d", found, znode, n);
41782 + if (found < 0) {
41783 + err = found;
41784 + goto out_unlock;
41785 + }
41786 +
41787 + /* Ensure the znode is dirtied */
41788 + if (znode->cnext || !ubifs_zn_dirty(znode)) {
41789 + znode = dirty_cow_bottom_up(c, znode);
41790 + if (IS_ERR(znode)) {
41791 + err = PTR_ERR(znode);
41792 + goto out_unlock;
41793 + }
41794 + }
41795 +
41796 + if (found == 1) {
41797 + struct ubifs_zbranch *zbr = &znode->zbranch[n];
41798 +
41799 + lnc_free(zbr);
41800 + err = ubifs_add_dirt(c, zbr->lnum, zbr->len);
41801 + zbr->lnum = lnum;
41802 + zbr->offs = offs;
41803 + zbr->len = len;
41804 + goto out_unlock;
41805 + }
41806 + }
41807 +
41808 + if (!found) {
41809 + struct ubifs_zbranch zbr;
41810 +
41811 + zbr.znode = NULL;
41812 + zbr.lnum = lnum;
41813 + zbr.offs = offs;
41814 + zbr.len = len;
41815 + key_copy(c, key, &zbr.key);
41816 + err = tnc_insert(c, znode, &zbr, n + 1);
41817 + if (err)
41818 + goto out_unlock;
41819 + if (c->replaying && c->replay_sqnum < c->cs_sqnum) {
41820 + /*
41821 + * This node was moved by garbage collection. We can
41822 + * tell because it is in the journal but it has a
41823 + * sequence number earlier than the last commit-start.
41824 + * We did not find it in the index so there may be a
41825 + * dangling branch still in the index. So we remove it
41826 + * by passing 'ubifs_tnc_remove_nm()' the same key but
41827 + * an unmatchable name.
41828 + */
41829 + struct qstr noname = { .len = 0, .name = "" };
41830 +
41831 + err = dbg_check_tnc(c, 0);
41832 + mutex_unlock(&c->tnc_mutex);
41833 + if (err)
41834 + return err;
41835 + return ubifs_tnc_remove_nm(c, key, &noname);
41836 + }
41837 + }
41838 +
41839 +out_unlock:
41840 + if (!err)
41841 + err = dbg_check_tnc(c, 0);
41842 + mutex_unlock(&c->tnc_mutex);
41843 + return err;
41844 +}
41845 +
41846 +/**
41847 + * tnc_delete - delete a znode form TNC.
41848 + * @c: UBIFS file-system description object
41849 + * @znode: znode to delete from
41850 + * @n: zbranch slot number to delete
41851 + *
41852 + * This function deletes a leaf node from @n-th slot of @znode. Returns zero in
41853 + * case of success and a negative error code in case of failure.
41854 + */
41855 +static int tnc_delete(struct ubifs_info *c, struct ubifs_znode *znode, int n)
41856 +{
41857 + struct ubifs_zbranch *zbr;
41858 + struct ubifs_znode *zp;
41859 + int i, err;
41860 +
41861 + /* Delete without merge for now */
41862 + ubifs_assert(znode->level == 0);
41863 + ubifs_assert(n >= 0 && n < c->fanout);
41864 + dbg_tnc("deleting %s", DBGKEY(&znode->zbranch[n].key));
41865 +
41866 + zbr = &znode->zbranch[n];
41867 + lnc_free(zbr);
41868 +
41869 + err = ubifs_add_dirt(c, zbr->lnum, zbr->len);
41870 + if (err) {
41871 + dbg_dump_znode(c, znode);
41872 + return err;
41873 + }
41874 +
41875 + /* We do not "gap" zbranch slots */
41876 + for (i = n; i < znode->child_cnt - 1; i++)
41877 + znode->zbranch[i] = znode->zbranch[i + 1];
41878 + znode->child_cnt -= 1;
41879 +
41880 + if (znode->child_cnt > 0)
41881 + return 0;
41882 +
41883 + /*
41884 + * This was the last zbranch, we have to delete this znode from the
41885 + * parent.
41886 + */
41887 +
41888 + do {
41889 + ubifs_assert(!test_bit(OBSOLETE_ZNODE, &znode->flags));
41890 + ubifs_assert(ubifs_zn_dirty(znode));
41891 +
41892 + zp = znode->parent;
41893 + n = znode->iip;
41894 +
41895 + atomic_long_dec(&c->dirty_zn_cnt);
41896 +
41897 + err = insert_old_idx_znode(c, znode);
41898 + if (err)
41899 + return err;
41900 +
41901 + if (znode->cnext) {
41902 + __set_bit(OBSOLETE_ZNODE, &znode->flags);
41903 + atomic_long_inc(&c->clean_zn_cnt);
41904 + atomic_long_inc(&ubifs_clean_zn_cnt);
41905 + } else
41906 + kfree(znode);
41907 + znode = zp;
41908 + } while (znode->child_cnt == 1); /* while removing last child */
41909 +
41910 + /* Remove from znode, entry n - 1 */
41911 + znode->child_cnt -= 1;
41912 + ubifs_assert(znode->level != 0);
41913 + for (i = n; i < znode->child_cnt; i++) {
41914 + znode->zbranch[i] = znode->zbranch[i + 1];
41915 + if (znode->zbranch[i].znode)
41916 + znode->zbranch[i].znode->iip = i;
41917 + }
41918 +
41919 + /*
41920 + * If this is the root and it has only 1 child then
41921 + * collapse the tree.
41922 + */
41923 + if (!znode->parent) {
41924 + while (znode->child_cnt == 1 && znode->level != 0) {
41925 + zp = znode;
41926 + zbr = &znode->zbranch[0];
41927 + znode = get_znode(c, znode, 0);
41928 + if (IS_ERR(znode))
41929 + return PTR_ERR(znode);
41930 + znode = dirty_cow_znode(c, zbr);
41931 + if (IS_ERR(znode))
41932 + return PTR_ERR(znode);
41933 + znode->parent = NULL;
41934 + znode->iip = 0;
41935 + if (c->zroot.len) {
41936 + err = insert_old_idx(c, c->zroot.lnum,
41937 + c->zroot.offs);
41938 + if (err)
41939 + return err;
41940 + }
41941 + c->zroot.lnum = zbr->lnum;
41942 + c->zroot.offs = zbr->offs;
41943 + c->zroot.len = zbr->len;
41944 + c->zroot.znode = znode;
41945 + ubifs_assert(!test_bit(OBSOLETE_ZNODE,
41946 + &zp->flags));
41947 + ubifs_assert(test_bit(DIRTY_ZNODE, &zp->flags));
41948 + atomic_long_dec(&c->dirty_zn_cnt);
41949 +
41950 + if (zp->cnext) {
41951 + __set_bit(OBSOLETE_ZNODE, &zp->flags);
41952 + atomic_long_inc(&c->clean_zn_cnt);
41953 + atomic_long_inc(&ubifs_clean_zn_cnt);
41954 + } else
41955 + kfree(zp);
41956 + }
41957 + }
41958 +
41959 + return 0;
41960 +}
41961 +
41962 +/**
41963 + * ubifs_tnc_remove - remove an index entry of a node.
41964 + * @c: UBIFS file-system description object
41965 + * @key: key of node
41966 + *
41967 + * Returns %0 on success or negative error code on failure.
41968 + */
41969 +int ubifs_tnc_remove(struct ubifs_info *c, const union ubifs_key *key)
41970 +{
41971 + int found, n, err = 0;
41972 + struct ubifs_znode *znode;
41973 +
41974 + mutex_lock(&c->tnc_mutex);
41975 + dbg_tnc("key %s", DBGKEY(key));
41976 + found = lookup_level0_dirty(c, key, &znode, &n);
41977 + if (found < 0) {
41978 + err = found;
41979 + goto out_unlock;
41980 + }
41981 + if (found == 1)
41982 + err = tnc_delete(c, znode, n);
41983 + if (!err)
41984 + err = dbg_check_tnc(c, 0);
41985 +
41986 +out_unlock:
41987 + mutex_unlock(&c->tnc_mutex);
41988 + return err;
41989 +}
41990 +
41991 +/**
41992 + * ubifs_tnc_remove_nm - remove an index entry for a "hashed" node.
41993 + * @c: UBIFS file-system description object
41994 + * @key: key of node
41995 + * @nm: directory entry name
41996 + *
41997 + * Returns %0 on success or negative error code on failure.
41998 + */
41999 +int ubifs_tnc_remove_nm(struct ubifs_info *c, const union ubifs_key *key,
42000 + const struct qstr *nm)
42001 +{
42002 + int n, err;
42003 + struct ubifs_znode *znode;
42004 +
42005 + mutex_lock(&c->tnc_mutex);
42006 + dbg_tnc("%.*s, key %s", nm->len, nm->name, DBGKEY(key));
42007 + err = lookup_level0_dirty(c, key, &znode, &n);
42008 + if (err < 0)
42009 + goto out_unlock;
42010 +
42011 + if (err) {
42012 + if (c->replaying)
42013 + err = fallible_resolve_collision(c, key, &znode, &n,
42014 + nm, 0);
42015 + else
42016 + err = resolve_collision(c, key, &znode, &n, nm);
42017 + dbg_tnc("rc returned %d, znode %p, n %d", err, znode, n);
42018 + if (err < 0)
42019 + goto out_unlock;
42020 + if (err) {
42021 + /* Ensure the znode is dirtied */
42022 + if (znode->cnext || !ubifs_zn_dirty(znode)) {
42023 + znode = dirty_cow_bottom_up(c, znode);
42024 + if (IS_ERR(znode)) {
42025 + err = PTR_ERR(znode);
42026 + goto out_unlock;
42027 + }
42028 + }
42029 + err = tnc_delete(c, znode, n);
42030 + }
42031 + }
42032 +
42033 +out_unlock:
42034 + if (!err)
42035 + err = dbg_check_tnc(c, 0);
42036 + mutex_unlock(&c->tnc_mutex);
42037 + return err;
42038 +}
42039 +
42040 +/**
42041 + * key_in_range - determine if a key falls within a range of keys.
42042 + * @c: UBIFS file-system description object
42043 + * @key: key to check
42044 + * @from_key: lowest key in range
42045 + * @to_key: highest key in range
42046 + *
42047 + * This function returns %1 if the key is in range and %0 otherwise.
42048 + */
42049 +static int key_in_range(struct ubifs_info *c, union ubifs_key *key,
42050 + union ubifs_key *from_key, union ubifs_key *to_key)
42051 +{
42052 + if (keys_cmp(c, key, from_key) < 0)
42053 + return 0;
42054 + if (keys_cmp(c, key, to_key) > 0)
42055 + return 0;
42056 + return 1;
42057 +}
42058 +
42059 +/**
42060 + * ubifs_tnc_remove_range - remove index entries in range.
42061 + * @c: UBIFS file-system description object
42062 + * @from_key: lowest key to remove
42063 + * @to_key: highest key to remove
42064 + *
42065 + * This function removes index entries starting at @from_key and ending at
42066 + * @to_key. This function returns zero in case of success and a negative error
42067 + * code in case of failure.
42068 + */
42069 +int ubifs_tnc_remove_range(struct ubifs_info *c, union ubifs_key *from_key,
42070 + union ubifs_key *to_key)
42071 +{
42072 + int i, n, k, err = 0;
42073 + struct ubifs_znode *znode;
42074 + union ubifs_key *key;
42075 +
42076 + mutex_lock(&c->tnc_mutex);
42077 + while (1) {
42078 + /* Find first level 0 znode that contains keys to remove */
42079 + err = ubifs_lookup_level0(c, from_key, &znode, &n);
42080 + if (err < 0)
42081 + goto out_unlock;
42082 +
42083 + if (err)
42084 + key = from_key;
42085 + else {
42086 + err = tnc_next(c, &znode, &n);
42087 + if (err == -ENOENT) {
42088 + err = 0;
42089 + goto out_unlock;
42090 + }
42091 + if (err < 0)
42092 + goto out_unlock;
42093 + key = &znode->zbranch[n].key;
42094 + if (!key_in_range(c, key, from_key, to_key)) {
42095 + err = 0;
42096 + goto out_unlock;
42097 + }
42098 + }
42099 +
42100 + /* Ensure the znode is dirtied */
42101 + if (znode->cnext || !ubifs_zn_dirty(znode)) {
42102 + znode = dirty_cow_bottom_up(c, znode);
42103 + if (IS_ERR(znode)) {
42104 + err = PTR_ERR(znode);
42105 + goto out_unlock;
42106 + }
42107 + }
42108 +
42109 + /* Remove all keys in range except the first */
42110 + for (i = n + 1, k = 0; i < znode->child_cnt; i++, k++) {
42111 + key = &znode->zbranch[i].key;
42112 + if (!key_in_range(c, key, from_key, to_key))
42113 + break;
42114 + lnc_free(&znode->zbranch[i]);
42115 + err = ubifs_add_dirt(c, znode->zbranch[i].lnum,
42116 + znode->zbranch[i].len);
42117 + if (err) {
42118 + dbg_dump_znode(c, znode);
42119 + goto out_unlock;
42120 + }
42121 + dbg_tnc("removing %s", DBGKEY(key));
42122 + }
42123 + if (k) {
42124 + for (i = n + 1 + k; i < znode->child_cnt; i++)
42125 + znode->zbranch[i - k] = znode->zbranch[i];
42126 + znode->child_cnt -= k;
42127 + }
42128 +
42129 + /* Now delete the first */
42130 + err = tnc_delete(c, znode, n);
42131 + if (err)
42132 + goto out_unlock;
42133 + }
42134 +
42135 +out_unlock:
42136 + if (!err)
42137 + err = dbg_check_tnc(c, 0);
42138 + mutex_unlock(&c->tnc_mutex);
42139 + return err;
42140 +}
42141 +
42142 +/**
42143 + * ubifs_tnc_remove_ino - remove an inode from TNC.
42144 + * @c: UBIFS file-system description object
42145 + * @inum: inode number to remove
42146 + *
42147 + * This function remove inode @inum and all the extended attributes associated
42148 + * with the anode from TNC and returns zero in case of success or a negative
42149 + * error code in case of failure.
42150 + */
42151 +int ubifs_tnc_remove_ino(struct ubifs_info *c, ino_t inum)
42152 +{
42153 + union ubifs_key key1, key2;
42154 + struct ubifs_dent_node *xent, *pxent = NULL;
42155 + struct qstr nm = { .name = NULL };
42156 +
42157 + dbg_tnc("ino %lu", inum);
42158 +
42159 + /*
42160 + * Walk all extended attribute entries and remove them together with
42161 + * corresponding extended attribute inodes.
42162 + */
42163 + lowest_xent_key(c, &key1, inum);
42164 + while (1) {
42165 + ino_t xattr_inum;
42166 + int err;
42167 +
42168 + xent = ubifs_tnc_next_ent(c, &key1, &nm);
42169 + if (IS_ERR(xent)) {
42170 + err = PTR_ERR(xent);
42171 + if (err == -ENOENT)
42172 + break;
42173 + return err;
42174 + }
42175 +
42176 + xattr_inum = le64_to_cpu(xent->inum);
42177 + dbg_tnc("xent '%s', ino %lu", xent->name, xattr_inum);
42178 +
42179 + nm.name = xent->name;
42180 + nm.len = le16_to_cpu(xent->nlen);
42181 + err = ubifs_tnc_remove_nm(c, &key1, &nm);
42182 + if (err) {
42183 + kfree(xent);
42184 + return err;
42185 + }
42186 +
42187 + lowest_ino_key(c, &key1, xattr_inum);
42188 + highest_ino_key(c, &key2, xattr_inum);
42189 + err = ubifs_tnc_remove_range(c, &key1, &key2);
42190 + if (err) {
42191 + kfree(xent);
42192 + return err;
42193 + }
42194 +
42195 + kfree(pxent);
42196 + pxent = xent;
42197 + key_read(c, &xent->key, &key1);
42198 + }
42199 +
42200 + kfree(pxent);
42201 + lowest_ino_key(c, &key1, inum);
42202 + highest_ino_key(c, &key2, inum);
42203 +
42204 + return ubifs_tnc_remove_range(c, &key1, &key2);
42205 +}
42206 +
42207 +/**
42208 + * ubifs_tnc_next_ent - walk directory or extended attribute entries.
42209 + * @c: UBIFS file-system description object
42210 + * @key: key of last entry
42211 + * @nm: name of last entry found or %NULL
42212 + *
42213 + * This function finds and reads the next directory or extended attribute entry
42214 + * after the given key (@key) if there is one. @nm is used to resolve
42215 + * collisions.
42216 + *
42217 + * If the name of the current entry is not known and only the key is known,
42218 + * @nm->name has to be %NULL. In this case the semantics of this function is a
42219 + * little bit different and it returns the entry corresponding to this key, not
42220 + * the next one. If the key was not found, the closest "right" entry is
42221 + * returned.
42222 + *
42223 + * If the fist entry has to be found, @key has to contain the lowest possible
42224 + * key value for this inode and @name has to be %NULL.
42225 + *
42226 + * This function returns the found directory or extended attribute entry node
42227 + * in case of success, %-ENOENT is returned if no entry was found, and a
42228 + * negative error code is returned in case of failure.
42229 + */
42230 +struct ubifs_dent_node *ubifs_tnc_next_ent(struct ubifs_info *c,
42231 + union ubifs_key *key,
42232 + const struct qstr *nm)
42233 +{
42234 + int n, err, type = key_type(c, key);
42235 + struct ubifs_znode *znode;
42236 + struct ubifs_dent_node *dent;
42237 + struct ubifs_zbranch *zbr;
42238 + union ubifs_key *dkey;
42239 +
42240 + dbg_tnc("%s %s", nm->name ? (char *)nm->name : "(lowest)", DBGKEY(key));
42241 + ubifs_assert(is_hash_key(c, key));
42242 +
42243 + mutex_lock(&c->tnc_mutex);
42244 + err = ubifs_lookup_level0(c, key, &znode, &n);
42245 + if (unlikely(err < 0))
42246 + goto out_unlock;
42247 +
42248 + if (nm->name) {
42249 + if (err) {
42250 + /* Handle collisions */
42251 + err = resolve_collision(c, key, &znode, &n, nm);
42252 + dbg_tnc("rc returned %d, znode %p, n %d",
42253 + err, znode, n);
42254 + if (unlikely(err < 0))
42255 + goto out_unlock;
42256 + }
42257 +
42258 + /* Now find next entry */
42259 + err = tnc_next(c, &znode, &n);
42260 + if (unlikely(err))
42261 + goto out_unlock;
42262 + } else {
42263 + /*
42264 + * The full name of the entry was not given, in which case the
42265 + * behavior of this function is a little different and it
42266 + * returns current entry, not the next one.
42267 + */
42268 + if (!err) {
42269 + /*
42270 + * However, the given key does not exist in the TNC
42271 + * tree and @znode/@n variables contain the closest
42272 + * "preceding" element. Switch to the next one.
42273 + */
42274 + err = tnc_next(c, &znode, &n);
42275 + if (err)
42276 + goto out_unlock;
42277 + }
42278 + }
42279 +
42280 + zbr = &znode->zbranch[n];
42281 + dent = kmalloc(zbr->len, GFP_NOFS);
42282 + if (unlikely(!dent)) {
42283 + err = -ENOMEM;
42284 + goto out_unlock;
42285 + }
42286 +
42287 + /*
42288 + * The above 'tnc_next()' call could lead us to the next inode, check
42289 + * this.
42290 + */
42291 + dkey = &zbr->key;
42292 + if (key_inum(c, dkey) != key_inum(c, key) ||
42293 + key_type(c, dkey) != type) {
42294 + err = -ENOENT;
42295 + goto out_free;
42296 + }
42297 +
42298 + err = tnc_read_node_nm(c, zbr, dent);
42299 + if (unlikely(err))
42300 + goto out_free;
42301 +
42302 + mutex_unlock(&c->tnc_mutex);
42303 + return dent;
42304 +
42305 +out_free:
42306 + kfree(dent);
42307 +out_unlock:
42308 + mutex_unlock(&c->tnc_mutex);
42309 + return ERR_PTR(err);
42310 +}
42311 +
42312 +/**
42313 + * tnc_destroy_cnext - destroy left-over obsolete znodes from a failed commit.
42314 + * @c: UBIFS file-system description object
42315 + *
42316 + * Destroy left-over obsolete znodes from a failed commit.
42317 + */
42318 +static void tnc_destroy_cnext(struct ubifs_info *c)
42319 +{
42320 + struct ubifs_znode *cnext;
42321 +
42322 + if (!c->cnext)
42323 + return;
42324 + ubifs_assert(c->cmt_state == COMMIT_BROKEN);
42325 + cnext = c->cnext;
42326 + do {
42327 + struct ubifs_znode *znode = cnext;
42328 +
42329 + cnext = cnext->cnext;
42330 + if (test_bit(OBSOLETE_ZNODE, &znode->flags))
42331 + kfree(znode);
42332 + } while (cnext && cnext != c->cnext);
42333 +}
42334 +
42335 +/**
42336 + * ubifs_tnc_close - close TNC subsystem and free all related resources.
42337 + * @c: UBIFS file-system description object
42338 + */
42339 +void ubifs_tnc_close(struct ubifs_info *c)
42340 +{
42341 + long clean_freed;
42342 +
42343 + tnc_destroy_cnext(c);
42344 + if (c->zroot.znode) {
42345 + clean_freed = ubifs_destroy_tnc_subtree(c->zroot.znode);
42346 + atomic_long_sub(clean_freed, &ubifs_clean_zn_cnt);
42347 + }
42348 + kfree(c->gap_lebs);
42349 + kfree(c->ilebs);
42350 + destroy_old_idx(c);
42351 +}
42352 +
42353 +/**
42354 + * left_znode - get the znode to the left.
42355 + * @c: UBIFS file-system description object
42356 + * @znode: znode
42357 + *
42358 + * This function returns a pointer to the znode to the left of @znode or NULL if
42359 + * there is not one. A negative error code is returned on failure.
42360 + */
42361 +static struct ubifs_znode *left_znode(struct ubifs_info *c,
42362 + struct ubifs_znode *znode)
42363 +{
42364 + int level = znode->level;
42365 +
42366 + while (1) {
42367 + int n = znode->iip - 1;
42368 +
42369 + /* Go up until we can go left */
42370 + znode = znode->parent;
42371 + if (!znode)
42372 + return NULL;
42373 + if (n >= 0) {
42374 + /* Now go down the rightmost branch to 'level' */
42375 + znode = get_znode(c, znode, n);
42376 + if (IS_ERR(znode))
42377 + return znode;
42378 + while (znode->level != level) {
42379 + n = znode->child_cnt - 1;
42380 + znode = get_znode(c, znode, n);
42381 + if (IS_ERR(znode))
42382 + return znode;
42383 + }
42384 + break;
42385 + }
42386 + }
42387 + return znode;
42388 +}
42389 +
42390 +/**
42391 + * right_znode - get the znode to the right.
42392 + * @c: UBIFS file-system description object
42393 + * @znode: znode
42394 + *
42395 + * This function returns a pointer to the znode to the right of @znode or NULL
42396 + * if there is not one. A negative error code is returned on failure.
42397 + */
42398 +static struct ubifs_znode *right_znode(struct ubifs_info *c,
42399 + struct ubifs_znode *znode)
42400 +{
42401 + int level = znode->level;
42402 +
42403 + while (1) {
42404 + int n = znode->iip + 1;
42405 +
42406 + /* Go up until we can go right */
42407 + znode = znode->parent;
42408 + if (!znode)
42409 + return NULL;
42410 + if (n < znode->child_cnt) {
42411 + /* Now go down the leftmost branch to 'level' */
42412 + znode = get_znode(c, znode, n);
42413 + if (IS_ERR(znode))
42414 + return znode;
42415 + while (znode->level != level) {
42416 + znode = get_znode(c, znode, 0);
42417 + if (IS_ERR(znode))
42418 + return znode;
42419 + }
42420 + break;
42421 + }
42422 + }
42423 + return znode;
42424 +}
42425 +
42426 +/**
42427 + * lookup_znode - find a particular indexing node from TNC.
42428 + * @c: UBIFS file-system description object
42429 + * @key: index node key to lookup
42430 + * @level: index node level
42431 + * @lnum: index node LEB number
42432 + * @offs: index node offset
42433 + *
42434 + * This function searches an indexing node by its first key @key and its
42435 + * address @lnum:@offs. It looks up the indexing tree by pulling all indexing
42436 + * nodes it traverses to TNC. This function is called fro indexing nodes which
42437 + * were found on the media by scanning, for example when garbage-collecting or
42438 + * when doing in-the-gaps commit. This means that the indexing node which is
42439 + * looked for does not have to have exactly the same leftmost key @key, because
42440 + * the leftmost key may have been changed, in which case TNC will contain a
42441 + * dirty znode which still refers the same @lnum:@offs. This function is clever
42442 + * enough to recognize such indexing nodes.
42443 + *
42444 + * Note, if a znode was deleted or changed too much, then this function will
42445 + * not find it. For situations like this UBIFS has the old index RB-tree
42446 + * (indexed by @lnum:@offs).
42447 + *
42448 + * This function returns a pointer to the znode found or %NULL if it is not
42449 + * found. A negative error code is returned on failure.
42450 + */
42451 +static struct ubifs_znode *lookup_znode(struct ubifs_info *c,
42452 + union ubifs_key *key, int level,
42453 + int lnum, int offs)
42454 +{
42455 + struct ubifs_znode *znode, *zn;
42456 + int n, nn;
42457 +
42458 + /*
42459 + * The arguments have probably been read off flash, so don't assume
42460 + * they are valid.
42461 + */
42462 + if (level < 0)
42463 + return ERR_PTR(-EINVAL);
42464 +
42465 + /* Get the root znode */
42466 + znode = c->zroot.znode;
42467 + if (!znode) {
42468 + znode = ubifs_load_znode(c, &c->zroot, NULL, 0);
42469 + if (IS_ERR(znode))
42470 + return znode;
42471 + }
42472 + /* Check if it is the one we are looking for */
42473 + if (c->zroot.lnum == lnum && c->zroot.offs == offs)
42474 + return znode;
42475 + /* Descend to the parent level i.e. (level + 1) */
42476 + if (level >= znode->level)
42477 + return NULL;
42478 + while (1) {
42479 + ubifs_search_zbranch(c, znode, key, &n);
42480 + if (n < 0) {
42481 + /*
42482 + * We reached a znode where the leftmost key is greater
42483 + * than the key we are searching for. This is the same
42484 + * situation as the one described in a huge comment at
42485 + * the end of the 'ubifs_lookup_level0()' function. And
42486 + * for exactly the same reasons we have to try to look
42487 + * left before giving up.
42488 + */
42489 + znode = left_znode(c, znode);
42490 + if (!znode)
42491 + return NULL;
42492 + if (IS_ERR(znode))
42493 + return znode;
42494 + ubifs_search_zbranch(c, znode, key, &n);
42495 + ubifs_assert(n >= 0);
42496 + }
42497 + if (znode->level == level + 1)
42498 + break;
42499 + znode = get_znode(c, znode, n);
42500 + if (IS_ERR(znode))
42501 + return znode;
42502 + }
42503 + /* Check if the child is the one we are looking for */
42504 + if (znode->zbranch[n].lnum == lnum && znode->zbranch[n].offs == offs)
42505 + return get_znode(c, znode, n);
42506 + /* If the key is unique, there is nowhere else to look */
42507 + if (!is_hash_key(c, key))
42508 + return NULL;
42509 + /*
42510 + * The key is not unique and so may be also in the znodes to either
42511 + * side.
42512 + */
42513 + zn = znode;
42514 + nn = n;
42515 + /* Look left */
42516 + while (1) {
42517 + /* Move one branch to the left */
42518 + if (n)
42519 + n -= 1;
42520 + else {
42521 + znode = left_znode(c, znode);
42522 + if (!znode)
42523 + break;
42524 + if (IS_ERR(znode))
42525 + return znode;
42526 + n = znode->child_cnt - 1;
42527 + }
42528 + /* Check it */
42529 + if (znode->zbranch[n].lnum == lnum &&
42530 + znode->zbranch[n].offs == offs)
42531 + return get_znode(c, znode, n);
42532 + /* Stop if the key is less than the one we are looking for */
42533 + if (keys_cmp(c, &znode->zbranch[n].key, key) < 0)
42534 + break;
42535 + }
42536 + /* Back to the middle */
42537 + znode = zn;
42538 + n = nn;
42539 + /* Look right */
42540 + while (1) {
42541 + /* Move one branch to the right */
42542 + if (++n >= znode->child_cnt) {
42543 + znode = right_znode(c, znode);
42544 + if (!znode)
42545 + break;
42546 + if (IS_ERR(znode))
42547 + return znode;
42548 + n = 0;
42549 + }
42550 + /* Check it */
42551 + if (znode->zbranch[n].lnum == lnum &&
42552 + znode->zbranch[n].offs == offs)
42553 + return get_znode(c, znode, n);
42554 + /* Stop if the key is greater than the one we are looking for */
42555 + if (keys_cmp(c, &znode->zbranch[n].key, key) > 0)
42556 + break;
42557 + }
42558 + return NULL;
42559 +}
42560 +
42561 +/**
42562 + * is_idx_node_in_tnc - determine if an index node is in the TNC.
42563 + * @c: UBIFS file-system description object
42564 + * @key: key of index node
42565 + * @level: index node level
42566 + * @lnum: LEB number of index node
42567 + * @offs: offset of index node
42568 + *
42569 + * This function returns %0 if the index node is not referred to in the TNC, %1
42570 + * if the index node is referred to in the TNC and the corresponding znode is
42571 + * dirty, %2 if an index node is referred to in the TNC and the corresponding
42572 + * znode is clean, and a negative error code in case of failure.
42573 + *
42574 + * Note, the @key argument has to be the key of the first child. Also note,
42575 + * this function relies on the fact that 0:0 is never a valid LEB number and
42576 + * offset for a main-area node.
42577 + */
42578 +int is_idx_node_in_tnc(struct ubifs_info *c, union ubifs_key *key, int level,
42579 + int lnum, int offs)
42580 +{
42581 + struct ubifs_znode *znode;
42582 +
42583 + znode = lookup_znode(c, key, level, lnum, offs);
42584 + if (!znode)
42585 + return 0;
42586 + if (IS_ERR(znode))
42587 + return PTR_ERR(znode);
42588 +
42589 + return ubifs_zn_dirty(znode) ? 1 : 2;
42590 +}
42591 +
42592 +/**
42593 + * is_leaf_node_in_tnc - determine if a non-indexing not is in the TNC.
42594 + * @c: UBIFS file-system description object
42595 + * @key: node key
42596 + * @lnum: node LEB number
42597 + * @offs: node offset
42598 + *
42599 + * This function returns %1 if the node is referred to in the TNC, %0 if it is
42600 + * not, and a negative error code in case of failure.
42601 + *
42602 + * Note, this function relies on the fact that 0:0 is never a valid LEB number
42603 + * and offset for a main-area node.
42604 + */
42605 +static int is_leaf_node_in_tnc(struct ubifs_info *c, union ubifs_key *key,
42606 + int lnum, int offs)
42607 +{
42608 + struct ubifs_zbranch *zbr;
42609 + struct ubifs_znode *znode, *zn;
42610 + int n, found, err, nn;
42611 + const int unique = !is_hash_key(c, key);
42612 +
42613 + found = ubifs_lookup_level0(c, key, &znode, &n);
42614 + if (found < 0)
42615 + return found; /* Error code */
42616 + if (!found)
42617 + return 0;
42618 + zbr = &znode->zbranch[n];
42619 + if (lnum == zbr->lnum && offs == zbr->offs)
42620 + return 1; /* Found it */
42621 + if (unique)
42622 + return 0;
42623 + /*
42624 + * Because the key is not unique, we have to look left
42625 + * and right as well
42626 + */
42627 + zn = znode;
42628 + nn = n;
42629 + /* Look left */
42630 + while (1) {
42631 + err = tnc_prev(c, &znode, &n);
42632 + if (err == -ENOENT)
42633 + break;
42634 + if (err)
42635 + return err;
42636 + if (keys_cmp(c, key, &znode->zbranch[n].key))
42637 + break;
42638 + zbr = &znode->zbranch[n];
42639 + if (lnum == zbr->lnum && offs == zbr->offs)
42640 + return 1; /* Found it */
42641 + }
42642 + /* Look right */
42643 + znode = zn;
42644 + n = nn;
42645 + while (1) {
42646 + err = tnc_next(c, &znode, &n);
42647 + if (err) {
42648 + if (err == -ENOENT)
42649 + return 0;
42650 + return err;
42651 + }
42652 + if (keys_cmp(c, key, &znode->zbranch[n].key))
42653 + break;
42654 + zbr = &znode->zbranch[n];
42655 + if (lnum == zbr->lnum && offs == zbr->offs)
42656 + return 1; /* Found it */
42657 + }
42658 + return 0;
42659 +}
42660 +
42661 +/**
42662 + * ubifs_tnc_has_node - determine whether a node is in the TNC.
42663 + * @c: UBIFS file-system description object
42664 + * @key: node key
42665 + * @level: index node level (if it is an index node)
42666 + * @lnum: node LEB number
42667 + * @offs: node offset
42668 + * @is_idx: non-zero if the node is an index node
42669 + *
42670 + * This function returns %1 if the node is in the TNC, %0 if it is not, and a
42671 + * negative error code in case of failure. For index nodes, @key has to be the
42672 + * key of the first child. An index node is considered to be in the TNC only if
42673 + * the corresponding znode is clean or has not been loaded.
42674 + */
42675 +int ubifs_tnc_has_node(struct ubifs_info *c, union ubifs_key *key, int level,
42676 + int lnum, int offs, int is_idx)
42677 +{
42678 + int err;
42679 +
42680 + mutex_lock(&c->tnc_mutex);
42681 + if (is_idx) {
42682 + err = is_idx_node_in_tnc(c, key, level, lnum, offs);
42683 + if (err < 0)
42684 + goto out_unlock;
42685 + if (err == 1)
42686 + /* The index node was found but it was dirty */
42687 + err = 0;
42688 + else if (err == 2)
42689 + /* The index node was found and it was clean */
42690 + err = 1;
42691 + else
42692 + BUG_ON(err != 0);
42693 + } else
42694 + err = is_leaf_node_in_tnc(c, key, lnum, offs);
42695 +
42696 +out_unlock:
42697 + mutex_unlock(&c->tnc_mutex);
42698 + return err;
42699 +}
42700 +
42701 +/**
42702 + * ubifs_dirty_idx_node - dirty an index node.
42703 + * @c: UBIFS file-system description object
42704 + * @key: index node key
42705 + * @level: index node level
42706 + * @lnum: index node LEB number
42707 + * @offs: index node offset
42708 + *
42709 + * This function loads and dirties an index node so that it can be garbage
42710 + * collected. The @key argument has to be the key of the first child. This
42711 + * function relies on the fact that 0:0 is never a valid LEB number and offset
42712 + * for a main-area node. Returns %0 on success and a negative error code on
42713 + * failure.
42714 + */
42715 +int ubifs_dirty_idx_node(struct ubifs_info *c, union ubifs_key *key, int level,
42716 + int lnum, int offs)
42717 +{
42718 + struct ubifs_znode *znode;
42719 + int err = 0;
42720 +
42721 + mutex_lock(&c->tnc_mutex);
42722 + znode = lookup_znode(c, key, level, lnum, offs);
42723 + if (!znode)
42724 + goto out_unlock;
42725 + if (IS_ERR(znode)) {
42726 + err = PTR_ERR(znode);
42727 + goto out_unlock;
42728 + }
42729 + znode = dirty_cow_bottom_up(c, znode);
42730 + if (IS_ERR(znode)) {
42731 + err = PTR_ERR(znode);
42732 + goto out_unlock;
42733 + }
42734 +
42735 +out_unlock:
42736 + mutex_unlock(&c->tnc_mutex);
42737 + return err;
42738 +}
42739 diff --exclude=.git -urN linux-2.6.25.6/fs/ubifs/tnc_commit.c avr32-2.6/fs/ubifs/tnc_commit.c
42740 --- linux-2.6.25.6/fs/ubifs/tnc_commit.c 1970-01-01 01:00:00.000000000 +0100
42741 +++ avr32-2.6/fs/ubifs/tnc_commit.c 2008-06-12 15:09:45.603817614 +0200
42742 @@ -0,0 +1,1105 @@
42743 +/*
42744 + * This file is part of UBIFS.
42745 + *
42746 + * Copyright (C) 2006-2008 Nokia Corporation.
42747 + *
42748 + * This program is free software; you can redistribute it and/or modify it
42749 + * under the terms of the GNU General Public License version 2 as published by
42750 + * the Free Software Foundation.
42751 + *
42752 + * This program is distributed in the hope that it will be useful, but WITHOUT
42753 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
42754 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
42755 + * more details.
42756 + *
42757 + * You should have received a copy of the GNU General Public License along with
42758 + * this program; if not, write to the Free Software Foundation, Inc., 51
42759 + * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
42760 + *
42761 + * Authors: Adrian Hunter
42762 + * Artem Bityutskiy (Битюцкий Артём)
42763 + */
42764 +
42765 +/* This file implements TNC functions for committing */
42766 +
42767 +#include "ubifs.h"
42768 +
42769 +/**
42770 + * make_idx_node - make an index node for fill-the-gaps method of TNC commit.
42771 + * @c: UBIFS file-system description object
42772 + * @idx: buffer in which to place new index node
42773 + * @znode: znode from which to make new index node
42774 + * @lnum: LEB number where new index node will be written
42775 + * @offs: offset where new index node will be written
42776 + * @len: length of new index node
42777 + */
42778 +static int make_idx_node(struct ubifs_info *c, struct ubifs_idx_node *idx,
42779 + struct ubifs_znode *znode, int lnum, int offs, int len)
42780 +{
42781 + struct ubifs_znode *zp;
42782 + int i, err;
42783 +
42784 + /* Make index node */
42785 + idx->ch.node_type = UBIFS_IDX_NODE;
42786 + idx->child_cnt = cpu_to_le16(znode->child_cnt);
42787 + idx->level = cpu_to_le16(znode->level);
42788 + for (i = 0; i < znode->child_cnt; i++) {
42789 + struct ubifs_branch *br = ubifs_idx_branch(c, idx, i);
42790 + struct ubifs_zbranch *zbr = &znode->zbranch[i];
42791 +
42792 + key_write_idx(c, &zbr->key, &br->key);
42793 + br->lnum = cpu_to_le32(zbr->lnum);
42794 + br->offs = cpu_to_le32(zbr->offs);
42795 + br->len = cpu_to_le32(zbr->len);
42796 + if (!zbr->lnum || !zbr->len) {
42797 + ubifs_err("bad ref in znode");
42798 + dbg_dump_znode(c, znode);
42799 + if (zbr->znode)
42800 + dbg_dump_znode(c, zbr->znode);
42801 + }
42802 + }
42803 + ubifs_prepare_node(c, idx, len, 0);
42804 +
42805 +#ifdef CONFIG_UBIFS_FS_DEBUG
42806 + znode->lnum = lnum;
42807 + znode->offs = offs;
42808 + znode->len = len;
42809 +#endif
42810 +
42811 + err = insert_old_idx_znode(c, znode);
42812 +
42813 + /* Update the parent */
42814 + zp = znode->parent;
42815 + if (zp) {
42816 + struct ubifs_zbranch *zbr;
42817 +
42818 + zbr = &zp->zbranch[znode->iip];
42819 + zbr->lnum = lnum;
42820 + zbr->offs = offs;
42821 + zbr->len = len;
42822 + } else {
42823 + c->zroot.lnum = lnum;
42824 + c->zroot.offs = offs;
42825 + c->zroot.len = len;
42826 + }
42827 + c->calc_idx_sz += ALIGN(len, 8);
42828 +
42829 + atomic_long_dec(&c->dirty_zn_cnt);
42830 +
42831 + ubifs_assert(ubifs_zn_dirty(znode));
42832 + ubifs_assert(test_bit(COW_ZNODE, &znode->flags));
42833 +
42834 + __clear_bit(DIRTY_ZNODE, &znode->flags);
42835 + __clear_bit(COW_ZNODE, &znode->flags);
42836 +
42837 + return err;
42838 +}
42839 +
42840 +/**
42841 + * fill_gap - make index nodes in gaps in dirty index LEBs.
42842 + * @c: UBIFS file-system description object
42843 + * @lnum: LEB number that gap appears in
42844 + * @gap_start: offset of start of gap
42845 + * @gap_end: offset of end of gap
42846 + * @dirt: adds dirty space to this
42847 + *
42848 + * This function returns the number of index nodes written into the gap.
42849 + */
42850 +static int fill_gap(struct ubifs_info *c, int lnum, int gap_start, int gap_end,
42851 + int *dirt)
42852 +{
42853 + int len, gap_remains, gap_pos, written, pad_len;
42854 +
42855 + ubifs_assert((gap_start & 7) == 0);
42856 + ubifs_assert((gap_end & 7) == 0);
42857 + ubifs_assert(gap_end >= gap_start);
42858 +
42859 + gap_remains = gap_end - gap_start;
42860 + if (!gap_remains)
42861 + return 0;
42862 + gap_pos = gap_start;
42863 + written = 0;
42864 + while (c->enext) {
42865 + len = ubifs_idx_node_sz(c, c->enext->child_cnt);
42866 + if (len < gap_remains) {
42867 + struct ubifs_znode *znode = c->enext;
42868 + const int alen = ALIGN(len, 8);
42869 + int err;
42870 +
42871 + ubifs_assert(alen <= gap_remains);
42872 + err = make_idx_node(c, c->ileb_buf + gap_pos, znode,
42873 + lnum, gap_pos, len);
42874 + if (err)
42875 + return err;
42876 + gap_remains -= alen;
42877 + gap_pos += alen;
42878 + c->enext = znode->cnext;
42879 + if (c->enext == c->cnext)
42880 + c->enext = NULL;
42881 + written += 1;
42882 + } else
42883 + break;
42884 + }
42885 + if (gap_end == c->leb_size) {
42886 + c->ileb_len = ALIGN(gap_pos, c->min_io_size);
42887 + /* Pad to end of min_io_size */
42888 + pad_len = c->ileb_len - gap_pos;
42889 + } else
42890 + /* Pad to end of gap */
42891 + pad_len = gap_remains;
42892 + dbg_gc("LEB %d:%d to %d len %d nodes written %d wasted bytes %d",
42893 + lnum, gap_start, gap_end, gap_end - gap_start, written, pad_len);
42894 + ubifs_pad(c, c->ileb_buf + gap_pos, pad_len);
42895 + *dirt += pad_len;
42896 + return written;
42897 +}
42898 +
42899 +/**
42900 + * find_old_idx - find an index node obsoleted since the last commit start.
42901 + * @c: UBIFS file-system description object
42902 + * @lnum: LEB number of obsoleted index node
42903 + * @offs: offset of obsoleted index node
42904 + *
42905 + * Returns %1 if found and %0 otherwise.
42906 + */
42907 +static int find_old_idx(struct ubifs_info *c, int lnum, int offs)
42908 +{
42909 + struct ubifs_old_idx *o;
42910 + struct rb_node *p;
42911 +
42912 + p = c->old_idx.rb_node;
42913 + while (p) {
42914 + o = rb_entry(p, struct ubifs_old_idx, rb);
42915 + if (lnum < o->lnum)
42916 + p = p->rb_left;
42917 + else if (lnum > o->lnum)
42918 + p = p->rb_right;
42919 + else if (offs < o->offs)
42920 + p = p->rb_left;
42921 + else if (offs > o->offs)
42922 + p = p->rb_right;
42923 + else
42924 + return 1;
42925 + }
42926 + return 0;
42927 +}
42928 +
42929 +/**
42930 + * is_idx_node_in_use - determine if an index node can be overwritten.
42931 + * @c: UBIFS file-system description object
42932 + * @key: key of index node
42933 + * @level: index node level
42934 + * @lnum: LEB number of index node
42935 + * @offs: offset of index node
42936 + *
42937 + * If @key / @lnum / @offs identify an index node that was not part of the old
42938 + * index, then this function returns %0 (obsolete). Else if the index node was
42939 + * part of the old index but is now dirty %1 is returned, else if it is clean %2
42940 + * is returned. A negative error code is returned on failure.
42941 + */
42942 +static int is_idx_node_in_use(struct ubifs_info *c, union ubifs_key *key,
42943 + int level, int lnum, int offs)
42944 +{
42945 + int ret;
42946 +
42947 + ret = is_idx_node_in_tnc(c, key, level, lnum, offs);
42948 + if (ret < 0)
42949 + return ret; /* Error code */
42950 + if (ret == 0)
42951 + if (find_old_idx(c, lnum, offs))
42952 + return 1;
42953 + return ret;
42954 +}
42955 +
42956 +/**
42957 + * layout_leb_in_gaps - layout index nodes using in-the-gaps method.
42958 + * @c: UBIFS file-system description object
42959 + * @p: return LEB number here
42960 + *
42961 + * This function lays out new index nodes for dirty znodes using in-the-gaps
42962 + * method of TNC commit.
42963 + * This function merely puts the next znode into the next gap, making no attempt
42964 + * to try to maximise the number of znodes that fit.
42965 + * This function returns the number of index nodes written into the gaps, or a
42966 + * negative error code on failure.
42967 + */
42968 +static int layout_leb_in_gaps(struct ubifs_info *c, int *p)
42969 +{
42970 + struct ubifs_scan_leb *sleb;
42971 + struct ubifs_scan_node *snod;
42972 + int lnum, dirt = 0, gap_start, gap_end, err, written, tot_written;
42973 +
42974 + tot_written = 0;
42975 + /* Get an index LEB with lots of obsolete index nodes */
42976 + lnum = ubifs_find_dirty_idx_leb(c);
42977 + if (lnum < 0)
42978 + /*
42979 + * There also may be dirt in the index head that could be
42980 + * filled, however we do not check there at present.
42981 + */
42982 + return lnum; /* Error code */
42983 + *p = lnum;
42984 + dbg_gc("LEB %d", lnum);
42985 + /*
42986 + * Scan the index LEB. We use the generic scan for this even though
42987 + * it is more comprehensive and less efficient than is needed for this
42988 + * purpose.
42989 + */
42990 + sleb = ubifs_scan(c, lnum, 0, c->ileb_buf);
42991 + c->ileb_len = 0;
42992 + if (IS_ERR(sleb))
42993 + return PTR_ERR(sleb);
42994 + gap_start = 0;
42995 + list_for_each_entry(snod, &sleb->nodes, list) {
42996 + struct ubifs_idx_node *idx;
42997 + int in_use, level;
42998 +
42999 + ubifs_assert(snod->type == UBIFS_IDX_NODE);
43000 + idx = snod->node;
43001 + key_read(c, ubifs_idx_key(c, idx), &snod->key);
43002 + level = le16_to_cpu(idx->level);
43003 + /* Determine if the index node is in use (not obsolete) */
43004 + in_use = is_idx_node_in_use(c, &snod->key, level, lnum,
43005 + snod->offs);
43006 + if (in_use < 0) {
43007 + ubifs_scan_destroy(sleb);
43008 + return in_use; /* Error code */
43009 + }
43010 + if (in_use) {
43011 + if (in_use == 1)
43012 + dirt += ALIGN(snod->len, 8);
43013 + /*
43014 + * The obsolete index nodes form gaps that can be
43015 + * overwritten. This gap has ended because we have
43016 + * found an index node that is still in use
43017 + * i.e. not obsolete
43018 + */
43019 + gap_end = snod->offs;
43020 + /* Try to fill gap */
43021 + written = fill_gap(c, lnum, gap_start, gap_end, &dirt);
43022 + if (written < 0) {
43023 + ubifs_scan_destroy(sleb);
43024 + return written; /* Error code */
43025 + }
43026 + tot_written += written;
43027 + gap_start = ALIGN(snod->offs + snod->len, 8);
43028 + }
43029 + }
43030 + ubifs_scan_destroy(sleb);
43031 + c->ileb_len = c->leb_size;
43032 + gap_end = c->leb_size;
43033 + /* Try to fill gap */
43034 + written = fill_gap(c, lnum, gap_start, gap_end, &dirt);
43035 + if (written < 0)
43036 + return written; /* Error code */
43037 + tot_written += written;
43038 + if (tot_written == 0) {
43039 + struct ubifs_lprops lp;
43040 +
43041 + dbg_gc("LEB %d wrote %d index nodes", lnum, tot_written);
43042 + err = ubifs_read_one_lp(c, lnum, &lp);
43043 + if (err)
43044 + return err;
43045 + if (lp.free == c->leb_size) {
43046 + /*
43047 + * We must have snatched this LEB from the idx_gc list
43048 + * so we need to correct the free and dirty space.
43049 + */
43050 + err = ubifs_change_one_lp(c, lnum,
43051 + c->leb_size - c->ileb_len,
43052 + dirt, 0, 0, 0);
43053 + if (err)
43054 + return err;
43055 + }
43056 + return 0;
43057 + }
43058 + err = ubifs_change_one_lp(c, lnum, c->leb_size - c->ileb_len, dirt,
43059 + 0, 0, 0);
43060 + if (err)
43061 + return err;
43062 + err = ubi_leb_change(c->ubi, lnum, c->ileb_buf, c->ileb_len,
43063 + UBI_SHORTTERM);
43064 + if (err) {
43065 + ubifs_err("ubi_leb_change failed, error %d", err);
43066 + return err;
43067 + }
43068 + dbg_gc("LEB %d wrote %d index nodes", lnum, tot_written);
43069 + return tot_written;
43070 +}
43071 +
43072 +/**
43073 + * get_leb_cnt - calculate the number of empty LEBs needed to commit.
43074 + * @c: UBIFS file-system description object
43075 + * @cnt: number of znodes to commit
43076 + *
43077 + * This function returns the number of empty LEBs needed to commit @cnt znodes
43078 + * to the current index head. The number is not exact and may be more than
43079 + * needed.
43080 + */
43081 +static int get_leb_cnt(struct ubifs_info *c, int cnt)
43082 +{
43083 + int d;
43084 +
43085 + /* Assume maximum index node size (i.e. overestimate space needed) */
43086 + cnt -= (c->leb_size - c->ihead_offs) / c->max_idx_node_sz;
43087 + if (cnt < 0)
43088 + cnt = 0;
43089 + d = c->leb_size / c->max_idx_node_sz;
43090 + return DIV_ROUND_UP(cnt, d);
43091 +}
43092 +
43093 +/**
43094 + * layout_in_gaps - in-the-gaps method of committing TNC.
43095 + * @c: UBIFS file-system description object
43096 + * @cnt: number of dirty znodes to commit.
43097 + *
43098 + * This function lays out new index nodes for dirty znodes using in-the-gaps
43099 + * method of TNC commit.
43100 + *
43101 + * This function returns %0 on success and a negative error code on failure.
43102 + */
43103 +static int layout_in_gaps(struct ubifs_info *c, int cnt)
43104 +{
43105 + int err, leb_needed_cnt, written, *p;
43106 +
43107 + dbg_gc("%d znodes to write", cnt);
43108 +
43109 + c->gap_lebs = kmalloc(sizeof(int) * (c->lst.idx_lebs + 1), GFP_NOFS);
43110 + if (!c->gap_lebs)
43111 + return -ENOMEM;
43112 +
43113 + p = c->gap_lebs;
43114 + do {
43115 + ubifs_assert(p < c->gap_lebs + sizeof(int) * c->lst.idx_lebs);
43116 + written = layout_leb_in_gaps(c, p);
43117 + if (written < 0) {
43118 + err = written;
43119 + if (err == -ENOSPC) {
43120 + if (!dbg_force_in_the_gaps_enabled) {
43121 + /*
43122 + * Do not print scary warnings if the
43123 + * debugging option which forces
43124 + * in-the-gaps is enabled.
43125 + */
43126 + ubifs_err("out of space");
43127 + spin_lock(&c->space_lock);
43128 + dbg_dump_budg(c);
43129 + spin_unlock(&c->space_lock);
43130 + dbg_dump_lprops(c);
43131 + }
43132 + /* Try to commit anyway */
43133 + err = 0;
43134 + break;
43135 + }
43136 + kfree(c->gap_lebs);
43137 + c->gap_lebs = NULL;
43138 + return err;
43139 + }
43140 + p++;
43141 + cnt -= written;
43142 + leb_needed_cnt = get_leb_cnt(c, cnt);
43143 + dbg_gc("%d znodes remaining, need %d LEBs, have %d", cnt,
43144 + leb_needed_cnt, c->ileb_cnt);
43145 + } while (leb_needed_cnt > c->ileb_cnt);
43146 +
43147 + *p = -1;
43148 + return 0;
43149 +}
43150 +
43151 +/**
43152 + * layout_in_empty_space - layout index nodes in empty space.
43153 + * @c: UBIFS file-system description object
43154 + *
43155 + * This function lays out new index nodes for dirty znodes using empty LEBs.
43156 + *
43157 + * This function returns %0 on success and a negative error code on failure.
43158 + */
43159 +static int layout_in_empty_space(struct ubifs_info *c)
43160 +{
43161 + struct ubifs_znode *znode, *cnext, *zp;
43162 + int lnum, offs, len, next_len, buf_len, buf_offs, used, avail;
43163 + int wlen, blen, err;
43164 +
43165 + cnext = c->enext;
43166 + if (!cnext)
43167 + return 0;
43168 +
43169 + lnum = c->ihead_lnum;
43170 + buf_offs = c->ihead_offs;
43171 +
43172 + buf_len = ubifs_idx_node_sz(c, c->fanout);
43173 + buf_len = ALIGN(buf_len, c->min_io_size);
43174 + used = 0;
43175 + avail = buf_len;
43176 +
43177 + /* Ensure there is enough room for first write */
43178 + next_len = ubifs_idx_node_sz(c, cnext->child_cnt);
43179 + if (buf_offs + next_len > c->leb_size)
43180 + lnum = -1;
43181 +
43182 + while (1) {
43183 + znode = cnext;
43184 +
43185 + len = ubifs_idx_node_sz(c, znode->child_cnt);
43186 +
43187 + /* Determine the index node position */
43188 + if (lnum == -1) {
43189 + if (c->ileb_nxt >= c->ileb_cnt) {
43190 + ubifs_err("out of space");
43191 + return -ENOSPC;
43192 + }
43193 + lnum = c->ilebs[c->ileb_nxt++];
43194 + buf_offs = 0;
43195 + used = 0;
43196 + avail = buf_len;
43197 + }
43198 +
43199 + offs = buf_offs + used;
43200 +
43201 +#ifdef CONFIG_UBIFS_FS_DEBUG
43202 + znode->lnum = lnum;
43203 + znode->offs = offs;
43204 + znode->len = len;
43205 +#endif
43206 +
43207 + /* Update the parent */
43208 + zp = znode->parent;
43209 + if (zp) {
43210 + struct ubifs_zbranch *zbr;
43211 + int i;
43212 +
43213 + i = znode->iip;
43214 + zbr = &zp->zbranch[i];
43215 + zbr->lnum = lnum;
43216 + zbr->offs = offs;
43217 + zbr->len = len;
43218 + } else {
43219 + c->zroot.lnum = lnum;
43220 + c->zroot.offs = offs;
43221 + c->zroot.len = len;
43222 + }
43223 + c->calc_idx_sz += ALIGN(len, 8);
43224 +
43225 + /*
43226 + * Once lprops is updated, we can decrease the dirty znode count
43227 + * but it is easier to just do it here.
43228 + */
43229 + atomic_long_dec(&c->dirty_zn_cnt);
43230 +
43231 + /*
43232 + * Calculate the next index node length to see if there is
43233 + * enough room for it
43234 + */
43235 + cnext = znode->cnext;
43236 + if (cnext == c->cnext)
43237 + next_len = 0;
43238 + else
43239 + next_len = ubifs_idx_node_sz(c, cnext->child_cnt);
43240 +
43241 + if (c->min_io_size == 1) {
43242 + buf_offs += ALIGN(len, 8);
43243 + if (next_len) {
43244 + if (buf_offs + next_len <= c->leb_size)
43245 + continue;
43246 + err = ubifs_update_one_lp(c, lnum, 0,
43247 + c->leb_size - buf_offs, 0, 0);
43248 + if (err)
43249 + return err;
43250 + lnum = -1;
43251 + continue;
43252 + }
43253 + err = ubifs_update_one_lp(c, lnum,
43254 + c->leb_size - buf_offs, 0, 0, 0);
43255 + if (err)
43256 + return err;
43257 + break;
43258 + }
43259 +
43260 + /* Update buffer positions */
43261 + wlen = used + len;
43262 + used += ALIGN(len, 8);
43263 + avail -= ALIGN(len, 8);
43264 +
43265 + if (next_len != 0 &&
43266 + buf_offs + used + next_len <= c->leb_size &&
43267 + avail > 0)
43268 + continue;
43269 +
43270 + if (avail <= 0 && next_len &&
43271 + buf_offs + used + next_len <= c->leb_size)
43272 + blen = buf_len;
43273 + else
43274 + blen = ALIGN(wlen, c->min_io_size);
43275 +
43276 + /* The buffer is full or there are no more znodes to do */
43277 + buf_offs += blen;
43278 + if (next_len) {
43279 + if (buf_offs + next_len > c->leb_size) {
43280 + err = ubifs_update_one_lp(c, lnum,
43281 + c->leb_size - buf_offs, blen - used,
43282 + 0, 0);
43283 + if (err)
43284 + return err;
43285 + lnum = -1;
43286 + }
43287 + used -= blen;
43288 + if (used < 0)
43289 + used = 0;
43290 + avail = buf_len - used;
43291 + continue;
43292 + }
43293 + err = ubifs_update_one_lp(c, lnum, c->leb_size - buf_offs,
43294 + blen - used, 0, 0);
43295 + if (err)
43296 + return err;
43297 + break;
43298 + }
43299 +
43300 +#ifdef CONFIG_UBIFS_FS_DEBUG
43301 + c->new_ihead_lnum = lnum;
43302 + c->new_ihead_offs = buf_offs;
43303 +#endif
43304 +
43305 + return 0;
43306 +}
43307 +
43308 +/**
43309 + * layout_commit - determine positions of index nodes to commit.
43310 + * @c: UBIFS file-system description object
43311 + * @no_space: indicates that insufficient empty LEBs were allocated
43312 + * @cnt: number of znodes to commit
43313 + *
43314 + * Calculate and update the positions of index nodes to commit. If there were
43315 + * an insufficient number of empty LEBs allocated, then index nodes are placed
43316 + * into the gaps created by obsolete index nodes in non-empty index LEBs. For
43317 + * this purpose, an obsolete index node is one that was not in the index as at
43318 + * the end of the last commit. To write "in-the-gaps" requires that those index
43319 + * LEBs are updated atomically in-place.
43320 + */
43321 +static int layout_commit(struct ubifs_info *c, int no_space, int cnt)
43322 +{
43323 + int err;
43324 +
43325 + if (no_space) {
43326 + err = layout_in_gaps(c, cnt);
43327 + if (err)
43328 + return err;
43329 + }
43330 + err = layout_in_empty_space(c);
43331 + return err;
43332 +}
43333 +
43334 +/**
43335 + * find_first_dirty - find first dirty znode.
43336 + * @znode: znode to begin searching from
43337 + */
43338 +static struct ubifs_znode *find_first_dirty(struct ubifs_znode *znode)
43339 +{
43340 + int i, cont;
43341 +
43342 + if (!znode)
43343 + return NULL;
43344 +
43345 + while (1) {
43346 + if (znode->level == 0) {
43347 + if (ubifs_zn_dirty(znode))
43348 + return znode;
43349 + return NULL;
43350 + }
43351 + cont = 0;
43352 + for (i = 0; i < znode->child_cnt; i++) {
43353 + struct ubifs_zbranch *zbr = &znode->zbranch[i];
43354 +
43355 + if (zbr->znode && ubifs_zn_dirty(zbr->znode)) {
43356 + znode = zbr->znode;
43357 + cont = 1;
43358 + break;
43359 + }
43360 + }
43361 + if (!cont) {
43362 + if (ubifs_zn_dirty(znode))
43363 + return znode;
43364 + return NULL;
43365 + }
43366 + }
43367 +}
43368 +
43369 +/**
43370 + * find_next_dirty - find next dirty znode.
43371 + * @znode: znode to begin searching from
43372 + */
43373 +static struct ubifs_znode *find_next_dirty(struct ubifs_znode *znode)
43374 +{
43375 + int n = znode->iip + 1;
43376 +
43377 + znode = znode->parent;
43378 + if (!znode)
43379 + return NULL;
43380 + for (; n < znode->child_cnt; n++) {
43381 + struct ubifs_zbranch *zbr = &znode->zbranch[n];
43382 +
43383 + if (zbr->znode && ubifs_zn_dirty(zbr->znode))
43384 + return find_first_dirty(zbr->znode);
43385 + }
43386 + return znode;
43387 +}
43388 +
43389 +/**
43390 + * get_znodes_to_commit - create list of dirty znodes to commit.
43391 + * @c: UBIFS file-system description object
43392 + *
43393 + * This function returns the number of znodes to commit.
43394 + */
43395 +static int get_znodes_to_commit(struct ubifs_info *c)
43396 +{
43397 + struct ubifs_znode *znode, *cnext;
43398 + int cnt = 0;
43399 +
43400 + c->cnext = find_first_dirty(c->zroot.znode);
43401 + znode = c->enext = c->cnext;
43402 + if (!znode) {
43403 + dbg_cmt("no znodes to commit");
43404 + return 0;
43405 + }
43406 + cnt += 1;
43407 + while (1) {
43408 + ubifs_assert(!test_bit(COW_ZNODE, &znode->flags));
43409 + __set_bit(COW_ZNODE, &znode->flags);
43410 + znode->alt = 0;
43411 + cnext = find_next_dirty(znode);
43412 + if (!cnext) {
43413 + znode->cnext = c->cnext;
43414 + break;
43415 + }
43416 + znode->cnext = cnext;
43417 + znode = cnext;
43418 + cnt += 1;
43419 + }
43420 + dbg_cmt("committing %d znodes", cnt);
43421 + ubifs_assert(cnt == atomic_long_read(&c->dirty_zn_cnt));
43422 + return cnt;
43423 +}
43424 +
43425 +/**
43426 + * alloc_idx_lebs - allocate empty LEBs to be used to commit.
43427 + * @c: UBIFS file-system description object
43428 + * @cnt: number of znodes to commit
43429 + *
43430 + * This function returns %-ENOSPC if it cannot allocate a sufficient number of
43431 + * empty LEBs. %0 is returned on success, otherwise a negative error code
43432 + * is returned.
43433 + */
43434 +static int alloc_idx_lebs(struct ubifs_info *c, int cnt)
43435 +{
43436 + int i, leb_cnt, lnum;
43437 +
43438 + c->ileb_cnt = 0;
43439 + c->ileb_nxt = 0;
43440 + leb_cnt = get_leb_cnt(c, cnt);
43441 + dbg_cmt("need about %d empty LEBS for TNC commit", leb_cnt);
43442 + if (!leb_cnt)
43443 + return 0;
43444 + c->ilebs = kmalloc(leb_cnt * sizeof(int), GFP_NOFS);
43445 + if (!c->ilebs)
43446 + return -ENOMEM;
43447 + for (i = 0; i < leb_cnt; i++) {
43448 + lnum = ubifs_find_free_leb_for_idx(c);
43449 + if (lnum < 0)
43450 + return lnum;
43451 + c->ilebs[c->ileb_cnt++] = lnum;
43452 + dbg_cmt("LEB %d", lnum);
43453 + }
43454 + if (dbg_force_in_the_gaps())
43455 + return -ENOSPC;
43456 + return 0;
43457 +}
43458 +
43459 +/**
43460 + * free_unused_idx_lebs - free unused LEBs that were allocated for the commit.
43461 + * @c: UBIFS file-system description object
43462 + *
43463 + * It is possible that we allocate more empty LEBs for the commit than we need.
43464 + * This functions frees the surplus.
43465 + *
43466 + * This function returns %0 on success and a negative error code on failure.
43467 + */
43468 +static int free_unused_idx_lebs(struct ubifs_info *c)
43469 +{
43470 + int i, err = 0, lnum, er;
43471 +
43472 + for (i = c->ileb_nxt; i < c->ileb_cnt; i++) {
43473 + lnum = c->ilebs[i];
43474 + dbg_cmt("LEB %d", lnum);
43475 + er = ubifs_change_one_lp(c, lnum, LPROPS_NC, LPROPS_NC, 0,
43476 + LPROPS_INDEX | LPROPS_TAKEN, 0);
43477 + if (!err)
43478 + err = er;
43479 + }
43480 + return err;
43481 +}
43482 +
43483 +/**
43484 + * free_idx_lebs - free unused LEBs after commit end.
43485 + * @c: UBIFS file-system description object
43486 + *
43487 + * This function returns %0 on success and a negative error code on failure.
43488 + */
43489 +static int free_idx_lebs(struct ubifs_info *c)
43490 +{
43491 + int err;
43492 +
43493 + err = free_unused_idx_lebs(c);
43494 + kfree(c->ilebs);
43495 + c->ilebs = NULL;
43496 + return err;
43497 +}
43498 +
43499 +/**
43500 + * ubifs_tnc_start_commit - start TNC commit.
43501 + * @c: UBIFS file-system description object
43502 + * @zroot: new index root position is returned here
43503 + *
43504 + * This function prepares the list of indexing nodes to commit and lays out
43505 + * their positions on flash. If there is not enough free space it uses the
43506 + * in-gap commit method. Returns zero in case of success and a negative error
43507 + * code in case of failure.
43508 + */
43509 +int ubifs_tnc_start_commit(struct ubifs_info *c, struct ubifs_zbranch *zroot)
43510 +{
43511 + int err = 0, cnt;
43512 +
43513 + mutex_lock(&c->tnc_mutex);
43514 + err = dbg_check_tnc(c, 1);
43515 + if (err)
43516 + goto out;
43517 + cnt = get_znodes_to_commit(c);
43518 + if (cnt != 0) {
43519 + int no_space = 0;
43520 +
43521 + err = alloc_idx_lebs(c, cnt);
43522 + if (err == -ENOSPC)
43523 + no_space = 1;
43524 + else if (err)
43525 + goto out_free;
43526 + err = layout_commit(c, no_space, cnt);
43527 + if (err)
43528 + goto out_free;
43529 + ubifs_assert(atomic_long_read(&c->dirty_zn_cnt) == 0);
43530 + err = free_unused_idx_lebs(c);
43531 + if (err)
43532 + goto out;
43533 + }
43534 + destroy_old_idx(c);
43535 + memcpy(zroot, &c->zroot, sizeof(struct ubifs_zbranch));
43536 +
43537 + err = ubifs_save_dirty_idx_lnums(c);
43538 + if (err)
43539 + goto out;
43540 +
43541 + spin_lock(&c->space_lock);
43542 + /*
43543 + * Although we have not finished committing yet, update size of the
43544 + * committed index ('c->old_idx_sz') and zero out the index growth
43545 + * budget. It is OK to do this now, because we've reserved all the
43546 + * space which is needed to commit the index, and it is save for the
43547 + * budgeting subsystem to assume the index is already committed,
43548 + * even though it is not.
43549 + */
43550 + c->old_idx_sz = c->calc_idx_sz;
43551 + c->budg_uncommitted_idx = 0;
43552 + spin_unlock(&c->space_lock);
43553 + mutex_unlock(&c->tnc_mutex);
43554 +
43555 + dbg_cmt("number of index LEBs %d", c->lst.idx_lebs);
43556 + dbg_cmt("size of index %llu", c->calc_idx_sz);
43557 + return err;
43558 +
43559 +out_free:
43560 + free_idx_lebs(c);
43561 +out:
43562 + mutex_unlock(&c->tnc_mutex);
43563 + return err;
43564 +}
43565 +
43566 +/**
43567 + * write_index - write index nodes.
43568 + * @c: UBIFS file-system description object
43569 + *
43570 + * This function writes the index nodes whose positions were laid out in the
43571 + * layout_in_empty_space function.
43572 + */
43573 +static int write_index(struct ubifs_info *c)
43574 +{
43575 + struct ubifs_idx_node *idx;
43576 + struct ubifs_znode *znode, *cnext;
43577 + int i, lnum, offs, len, next_len, buf_len, buf_offs, used;
43578 + int avail, wlen, err, lnum_pos = 0;
43579 +
43580 + cnext = c->enext;
43581 + if (!cnext)
43582 + return 0;
43583 +
43584 + /*
43585 + * Always write index nodes to the index head so that index nodes and
43586 + * other types of nodes are never mixed in the same erase block.
43587 + */
43588 + lnum = c->ihead_lnum;
43589 + buf_offs = c->ihead_offs;
43590 +
43591 + /* Allocate commit buffer */
43592 + buf_len = ALIGN(c->max_idx_node_sz, c->min_io_size);
43593 + used = 0;
43594 + avail = buf_len;
43595 +
43596 + /* Ensure there is enough room for first write */
43597 + next_len = ubifs_idx_node_sz(c, cnext->child_cnt);
43598 + if (buf_offs + next_len > c->leb_size) {
43599 + err = ubifs_update_one_lp(c, lnum, LPROPS_NC, 0, 0,
43600 + LPROPS_TAKEN);
43601 + if (err)
43602 + return err;
43603 + lnum = -1;
43604 + }
43605 +
43606 + while (1) {
43607 + cond_resched();
43608 +
43609 + znode = cnext;
43610 + idx = c->cbuf + used;
43611 +
43612 + /* Make index node */
43613 + idx->ch.node_type = UBIFS_IDX_NODE;
43614 + idx->child_cnt = cpu_to_le16(znode->child_cnt);
43615 + idx->level = cpu_to_le16(znode->level);
43616 + for (i = 0; i < znode->child_cnt; i++) {
43617 + struct ubifs_branch *br = ubifs_idx_branch(c, idx, i);
43618 + struct ubifs_zbranch *zbr = &znode->zbranch[i];
43619 +
43620 + key_write_idx(c, &zbr->key, &br->key);
43621 + br->lnum = cpu_to_le32(zbr->lnum);
43622 + br->offs = cpu_to_le32(zbr->offs);
43623 + br->len = cpu_to_le32(zbr->len);
43624 + if (!zbr->lnum || !zbr->len) {
43625 + ubifs_err("bad ref in znode");
43626 + dbg_dump_znode(c, znode);
43627 + if (zbr->znode)
43628 + dbg_dump_znode(c, zbr->znode);
43629 + }
43630 + }
43631 + len = ubifs_idx_node_sz(c, znode->child_cnt);
43632 + ubifs_prepare_node(c, idx, len, 0);
43633 +
43634 + /* Determine the index node position */
43635 + if (lnum == -1) {
43636 + lnum = c->ilebs[lnum_pos++];
43637 + buf_offs = 0;
43638 + used = 0;
43639 + avail = buf_len;
43640 + }
43641 + offs = buf_offs + used;
43642 +
43643 +#ifdef CONFIG_UBIFS_FS_DEBUG
43644 + if (lnum != znode->lnum || offs != znode->offs ||
43645 + len != znode->len) {
43646 + ubifs_err("inconsistent znode posn");
43647 + return -EINVAL;
43648 + }
43649 +#endif
43650 +
43651 + /* Grab some stuff from znode while we still can */
43652 + cnext = znode->cnext;
43653 +
43654 + ubifs_assert(ubifs_zn_dirty(znode));
43655 + ubifs_assert(test_bit(COW_ZNODE, &znode->flags));
43656 +
43657 + /*
43658 + * It is important that other threads should see %DIRTY_ZNODE
43659 + * flag cleared before %COW_ZNODE. Specifically, it matters in
43660 + * the 'dirty_cow_znode()' function. This is the reason for the
43661 + * first barrier. Also, we want the bit changes to be seen to
43662 + * other threads ASAP, to avoid unnecesarry copying, which is
43663 + * the reason for the second barrier.
43664 + */
43665 + clear_bit(DIRTY_ZNODE, &znode->flags);
43666 + smp_mb__before_clear_bit();
43667 + clear_bit(COW_ZNODE, &znode->flags);
43668 + smp_mb__after_clear_bit();
43669 +
43670 + /* Do not access znode from this point on */
43671 +
43672 + /* Update buffer positions */
43673 + wlen = used + len;
43674 + used += ALIGN(len, 8);
43675 + avail -= ALIGN(len, 8);
43676 +
43677 + /*
43678 + * Calculate the next index node length to see if there is
43679 + * enough room for it
43680 + */
43681 + if (cnext == c->cnext)
43682 + next_len = 0;
43683 + else
43684 + next_len = ubifs_idx_node_sz(c, cnext->child_cnt);
43685 +
43686 + if (c->min_io_size == 1) {
43687 + /*
43688 + * Write the prepared index node immediately if there is
43689 + * no minimum IO size
43690 + */
43691 + err = ubifs_leb_write(c, lnum, c->cbuf, buf_offs,
43692 + wlen, UBI_SHORTTERM);
43693 + if (err)
43694 + return err;
43695 + buf_offs += ALIGN(wlen, 8);
43696 + if (next_len) {
43697 + used = 0;
43698 + avail = buf_len;
43699 + if (buf_offs + next_len > c->leb_size) {
43700 + err = ubifs_update_one_lp(c, lnum,
43701 + LPROPS_NC, 0, 0, LPROPS_TAKEN);
43702 + if (err)
43703 + return err;
43704 + lnum = -1;
43705 + }
43706 + continue;
43707 + }
43708 + } else {
43709 + int blen, nxt_offs = buf_offs + used + next_len;
43710 +
43711 + if (next_len && nxt_offs <= c->leb_size) {
43712 + if (avail > 0)
43713 + continue;
43714 + else
43715 + blen = buf_len;
43716 + } else {
43717 + wlen = ALIGN(wlen, 8);
43718 + blen = ALIGN(wlen, c->min_io_size);
43719 + ubifs_pad(c, c->cbuf + wlen, blen - wlen);
43720 + }
43721 + /*
43722 + * The buffer is full or there are no more znodes
43723 + * to do
43724 + */
43725 + err = ubifs_leb_write(c, lnum, c->cbuf, buf_offs,
43726 + blen, UBI_SHORTTERM);
43727 + if (err)
43728 + return err;
43729 + buf_offs += blen;
43730 + if (next_len) {
43731 + if (nxt_offs > c->leb_size) {
43732 + err = ubifs_update_one_lp(c, lnum,
43733 + LPROPS_NC, 0, 0, LPROPS_TAKEN);
43734 + if (err)
43735 + return err;
43736 + lnum = -1;
43737 + }
43738 + used -= blen;
43739 + if (used < 0)
43740 + used = 0;
43741 + avail = buf_len - used;
43742 + memmove(c->cbuf, c->cbuf + blen, used);
43743 + continue;
43744 + }
43745 + }
43746 + break;
43747 + }
43748 +
43749 +#ifdef CONFIG_UBIFS_FS_DEBUG
43750 + if (lnum != c->new_ihead_lnum || buf_offs != c->new_ihead_offs) {
43751 + ubifs_err("inconsistent ihead");
43752 + return -EINVAL;
43753 + }
43754 +#endif
43755 +
43756 + c->ihead_lnum = lnum;
43757 + c->ihead_offs = buf_offs;
43758 +
43759 + return 0;
43760 +}
43761 +
43762 +/**
43763 + * free_obsolete_znodes - free obsolete znodes.
43764 + * @c: UBIFS file-system description object
43765 + *
43766 + * At the end of commit end, obsolete znodes are freed.
43767 + */
43768 +static void free_obsolete_znodes(struct ubifs_info *c)
43769 +{
43770 + struct ubifs_znode *znode, *cnext;
43771 +
43772 + cnext = c->cnext;
43773 + do {
43774 + znode = cnext;
43775 + cnext = znode->cnext;
43776 + if (test_bit(OBSOLETE_ZNODE, &znode->flags))
43777 + kfree(znode);
43778 + else {
43779 + znode->cnext = NULL;
43780 + atomic_long_inc(&c->clean_zn_cnt);
43781 + atomic_long_inc(&ubifs_clean_zn_cnt);
43782 + }
43783 + } while (cnext != c->cnext);
43784 +}
43785 +
43786 +/**
43787 + * return_gap_lebs - return LEBs used by the in-gap commit method.
43788 + * @c: UBIFS file-system description object
43789 + *
43790 + * This function clears the "taken" flag for the LEBs which were used by the
43791 + * "commit in-the-gaps" method.
43792 + */
43793 +static int return_gap_lebs(struct ubifs_info *c)
43794 +{
43795 + int *p, err;
43796 +
43797 + if (!c->gap_lebs)
43798 + return 0;
43799 +
43800 + dbg_cmt("");
43801 + for (p = c->gap_lebs; *p != -1; p++) {
43802 + err = ubifs_change_one_lp(c, *p, LPROPS_NC, LPROPS_NC, 0,
43803 + LPROPS_TAKEN, 0);
43804 + if (err)
43805 + return err;
43806 + }
43807 +
43808 + kfree(c->gap_lebs);
43809 + c->gap_lebs = NULL;
43810 + return 0;
43811 +}
43812 +
43813 +/**
43814 + * ubifs_tnc_end_commit - update the TNC for commit end.
43815 + * @c: UBIFS file-system description object
43816 + *
43817 + * Write the dirty znodes.
43818 + */
43819 +int ubifs_tnc_end_commit(struct ubifs_info *c)
43820 +{
43821 + int err;
43822 +
43823 + if (!c->cnext)
43824 + return 0;
43825 +
43826 + err = return_gap_lebs(c);
43827 + if (err)
43828 + return err;
43829 +
43830 + err = write_index(c);
43831 + if (err)
43832 + return err;
43833 +
43834 + mutex_lock(&c->tnc_mutex);
43835 +
43836 + dbg_cmt("TNC height is %d", c->zroot.znode->level + 1);
43837 +
43838 + free_obsolete_znodes(c);
43839 +
43840 + c->cnext = NULL;
43841 + kfree(c->ilebs);
43842 + c->ilebs = NULL;
43843 +
43844 + mutex_unlock(&c->tnc_mutex);
43845 +
43846 + return 0;
43847 +}
43848 diff --exclude=.git -urN linux-2.6.25.6/fs/ubifs/tnc_misc.c avr32-2.6/fs/ubifs/tnc_misc.c
43849 --- linux-2.6.25.6/fs/ubifs/tnc_misc.c 1970-01-01 01:00:00.000000000 +0100
43850 +++ avr32-2.6/fs/ubifs/tnc_misc.c 2008-06-12 15:09:45.603817614 +0200
43851 @@ -0,0 +1,496 @@
43852 +/*
43853 + * This file is part of UBIFS.
43854 + *
43855 + * Copyright (C) 2006-2008 Nokia Corporation.
43856 + *
43857 + * This program is free software; you can redistribute it and/or modify it
43858 + * under the terms of the GNU General Public License version 2 as published by
43859 + * the Free Software Foundation.
43860 + *
43861 + * This program is distributed in the hope that it will be useful, but WITHOUT
43862 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
43863 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
43864 + * more details.
43865 + *
43866 + * You should have received a copy of the GNU General Public License along with
43867 + * this program; if not, write to the Free Software Foundation, Inc., 51
43868 + * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
43869 + *
43870 + * Authors: Adrian Hunter
43871 + * Artem Bityutskiy (Битюцкий Артём)
43872 + */
43873 +
43874 +/*
43875 + * This file contains miscelanious TNC-related functions shared betweend
43876 + * different files. This file does not form any logically separate TNC
43877 + * sub-system. The file was created because there is a lot of TNC code and
43878 + * putting it all in one file would make that file too big and unreadable.
43879 + */
43880 +
43881 +#include "ubifs.h"
43882 +
43883 +/**
43884 + * ubifs_tnc_levelorder_next - next TNC tree element in levelorder traversal.
43885 + * @zr: root of the subtree to traverse
43886 + * @znode: previous znode
43887 + *
43888 + * This function implements levelorder TNC traversal. The LNC is ignored.
43889 + * Returns the next element or %NULL if @znode is already the last one.
43890 + */
43891 +struct ubifs_znode *ubifs_tnc_levelorder_next(struct ubifs_znode *zr,
43892 + struct ubifs_znode *znode)
43893 +{
43894 + int level, iip, level_search = 0;
43895 + struct ubifs_znode *zn;
43896 +
43897 + ubifs_assert(zr);
43898 +
43899 + if (unlikely(!znode))
43900 + return zr;
43901 +
43902 + if (unlikely(znode == zr)) {
43903 + if (znode->level == 0)
43904 + return NULL;
43905 + return ubifs_tnc_find_child(zr, 0);
43906 + }
43907 +
43908 + level = znode->level;
43909 +
43910 + iip = znode->iip;
43911 + while (1) {
43912 + ubifs_assert(znode->level <= zr->level);
43913 +
43914 + /*
43915 + * First walk up until there is a znode with next branch to
43916 + * look at.
43917 + */
43918 + while (znode->parent != zr && iip >= znode->parent->child_cnt) {
43919 + znode = znode->parent;
43920 + iip = znode->iip;
43921 + }
43922 +
43923 + if (unlikely(znode->parent == zr &&
43924 + iip >= znode->parent->child_cnt)) {
43925 + /* This level is done, switch to the lower one */
43926 + level -= 1;
43927 + if (level_search || level < 0)
43928 + /*
43929 + * We were already looking for znode at lower
43930 + * level ('level_search'). As we are here
43931 + * again, it just does not exist. Or all levels
43932 + * were finished ('level < 0').
43933 + */
43934 + return NULL;
43935 +
43936 + level_search = 1;
43937 + iip = -1;
43938 + znode = ubifs_tnc_find_child(zr, 0);
43939 + ubifs_assert(znode);
43940 + }
43941 +
43942 + /* Switch to the next index */
43943 + zn = ubifs_tnc_find_child(znode->parent, iip + 1);
43944 + if (!zn) {
43945 + /* No more children to look at, we have walk up */
43946 + iip = znode->parent->child_cnt;
43947 + continue;
43948 + }
43949 +
43950 + /* Walk back down to the level we came from ('level') */
43951 + while (zn->level != level) {
43952 + znode = zn;
43953 + zn = ubifs_tnc_find_child(zn, 0);
43954 + if (!zn) {
43955 + /*
43956 + * This path is not too deep so it does not
43957 + * reach 'level'. Try next path.
43958 + */
43959 + iip = znode->iip;
43960 + break;
43961 + }
43962 + }
43963 +
43964 + if (zn) {
43965 + ubifs_assert(zn->level >= 0);
43966 + return zn;
43967 + }
43968 + }
43969 +}
43970 +
43971 +/**
43972 + * ubifs_search_zbranch - search znode branch.
43973 + * @c: UBIFS file-system description object
43974 + * @znode: znode to search in
43975 + * @key: key to search for
43976 + * @n: znode branch slot number is returned here
43977 + *
43978 + * This is a helper function which search branch with key @key in @znode using
43979 + * binary search. The result of the search may be:
43980 + * o exact match, then %1 is returned, and the slot number of the branch is
43981 + * stored in @n;
43982 + * o no exact match, then %0 is returned and the slot number of the left
43983 + * closest branch is returned in @n; the slot if all keys in this znode are
43984 + * greater than @key, then %-1 is returned in @n.
43985 + */
43986 +int ubifs_search_zbranch(const struct ubifs_info *c,
43987 + const struct ubifs_znode *znode,
43988 + const union ubifs_key *key, int *n)
43989 +{
43990 + int beg = 0, end = znode->child_cnt, uninitialized_var(mid);
43991 + int uninitialized_var(cmp);
43992 + const struct ubifs_zbranch *zbr = &znode->zbranch[0];
43993 +
43994 + ubifs_assert(end > beg);
43995 +
43996 + while (end > beg) {
43997 + mid = (beg + end) >> 1;
43998 + cmp = keys_cmp(c, key, &zbr[mid].key);
43999 + if (cmp > 0)
44000 + beg = mid + 1;
44001 + else if (cmp < 0)
44002 + end = mid;
44003 + else {
44004 + *n = mid;
44005 + return 1;
44006 + }
44007 + }
44008 +
44009 + *n = end - 1;
44010 +
44011 + /* The insert point is after *n */
44012 + ubifs_assert(*n >= -1 && *n < znode->child_cnt);
44013 + if (*n == -1)
44014 + ubifs_assert(keys_cmp(c, key, &zbr[0].key) < 0);
44015 + else
44016 + ubifs_assert(keys_cmp(c, key, &zbr[*n].key) > 0);
44017 + if (*n + 1 < znode->child_cnt)
44018 + ubifs_assert(keys_cmp(c, key, &zbr[*n + 1].key) < 0);
44019 +
44020 + return 0;
44021 +}
44022 +
44023 +/**
44024 + * ubifs_tnc_postorder_first - find first znode to do postorder tree traversal.
44025 + * @znode: znode to start at (root of the sub-tree to traverse)
44026 + *
44027 + * Find the lowest leftmost znode in a subtree of the TNC tree. The LNC is
44028 + * ignored.
44029 + */
44030 +struct ubifs_znode *ubifs_tnc_postorder_first(struct ubifs_znode *znode)
44031 +{
44032 + if (unlikely(!znode))
44033 + return NULL;
44034 +
44035 + while (znode->level > 0) {
44036 + struct ubifs_znode *child;
44037 +
44038 + child = ubifs_tnc_find_child(znode, 0);
44039 + if (!child)
44040 + return znode;
44041 + znode = child;
44042 + }
44043 +
44044 + return znode;
44045 +}
44046 +
44047 +/**
44048 + * ubifs_tnc_postorder_next - next TNC tree element in postorder traversal.
44049 + * @znode: previous znode
44050 + *
44051 + * This function implements postorder TNC traversal. The LNC is ignored.
44052 + * Returns the next element or %NULL if @znode is already the last one.
44053 + */
44054 +struct ubifs_znode *ubifs_tnc_postorder_next(struct ubifs_znode *znode)
44055 +{
44056 + struct ubifs_znode *zn;
44057 +
44058 + ubifs_assert(znode);
44059 + if (unlikely(!znode->parent))
44060 + return NULL;
44061 +
44062 + /* Switch to the next index in the parent */
44063 + zn = ubifs_tnc_find_child(znode->parent, znode->iip + 1);
44064 + if (!zn)
44065 + /* This is in fact the last child, return parent */
44066 + return znode->parent;
44067 +
44068 + /* Go to the first znode in this new subtree */
44069 + return ubifs_tnc_postorder_first(zn);
44070 +}
44071 +
44072 +/**
44073 + * ubifs_destroy_tnc_subtree - destroy all znodes connected to a subtree.
44074 + * @znode: znode defining subtree to destroy
44075 + *
44076 + * This function destroys subtree of the TNC tree. Returns number of clean
44077 + * znodes in the subtree.
44078 + */
44079 +long ubifs_destroy_tnc_subtree(struct ubifs_znode *znode)
44080 +{
44081 + struct ubifs_znode *zn = ubifs_tnc_postorder_first(znode);
44082 + long clean_freed = 0;
44083 + int n;
44084 +
44085 + ubifs_assert(zn);
44086 + while (1) {
44087 + for (n = 0; n < zn->child_cnt; n++) {
44088 + if (!zn->zbranch[n].znode)
44089 + continue;
44090 +
44091 + if (zn->level > 0 &&
44092 + !ubifs_zn_dirty(zn->zbranch[n].znode))
44093 + clean_freed += 1;
44094 +
44095 + cond_resched();
44096 + kfree(zn->zbranch[n].znode);
44097 + }
44098 +
44099 + if (zn == znode) {
44100 + if (!ubifs_zn_dirty(zn))
44101 + clean_freed += 1;
44102 + kfree(zn);
44103 + return clean_freed;
44104 + }
44105 +
44106 + zn = ubifs_tnc_postorder_next(zn);
44107 + }
44108 +}
44109 +
44110 +/**
44111 + * read_znode - read an indexing node from flash and fill znode.
44112 + * @c: UBIFS file-system description object
44113 + * @lnum: LEB of the indexing node to read
44114 + * @offs: node offset
44115 + * @len: node length
44116 + * @znode: znode to read to
44117 + *
44118 + * This function reads an indexing node from the flash media and fills znode
44119 + * with the read data. Returns zero in case of success and a negative error
44120 + * code in case of failure. The read indexing node is validated and if anything
44121 + * is wrong with it, this function prints complaint messages and returns
44122 + * %-EINVAL.
44123 + */
44124 +static int read_znode(struct ubifs_info *c, int lnum, int offs, int len,
44125 + struct ubifs_znode *znode)
44126 +{
44127 + int i, err, type, cmp;
44128 + struct ubifs_idx_node *idx;
44129 +
44130 + idx = kmalloc(c->max_idx_node_sz, GFP_NOFS);
44131 + if (!idx)
44132 + return -ENOMEM;
44133 +
44134 + err = ubifs_read_node(c, idx, UBIFS_IDX_NODE, len, lnum, offs);
44135 + if (err < 0) {
44136 + kfree(idx);
44137 + return err;
44138 + }
44139 +
44140 + znode->child_cnt = le16_to_cpu(idx->child_cnt);
44141 + znode->level = le16_to_cpu(idx->level);
44142 +
44143 + dbg_tnc("LEB %d:%d, level %d, %d branch",
44144 + lnum, offs, znode->level, znode->child_cnt);
44145 +
44146 + if (znode->child_cnt > c->fanout || znode->level > UBIFS_MAX_LEVELS) {
44147 + dbg_err("current fanout %d, branch count %d",
44148 + c->fanout, znode->child_cnt);
44149 + dbg_err("max levels %d, znode level %d",
44150 + UBIFS_MAX_LEVELS, znode->level);
44151 + err = 1;
44152 + goto out_dump;
44153 + }
44154 +
44155 + for (i = 0; i < znode->child_cnt; i++) {
44156 + const struct ubifs_branch *br = ubifs_idx_branch(c, idx, i);
44157 + struct ubifs_zbranch *zbr = &znode->zbranch[i];
44158 +
44159 + key_read(c, &br->key, &zbr->key);
44160 + zbr->lnum = le32_to_cpu(br->lnum);
44161 + zbr->offs = le32_to_cpu(br->offs);
44162 + zbr->len = le32_to_cpu(br->len);
44163 + zbr->znode = NULL;
44164 +
44165 + /* Validate branch */
44166 +
44167 + if (zbr->lnum < c->main_first ||
44168 + zbr->lnum >= c->leb_cnt || zbr->offs < 0 ||
44169 + zbr->offs + zbr->len > c->leb_size || zbr->offs & 7) {
44170 + dbg_err("bad branch %d", i);
44171 + err = 2;
44172 + goto out_dump;
44173 + }
44174 +
44175 + switch (key_type(c, &zbr->key)) {
44176 + case UBIFS_INO_KEY:
44177 + case UBIFS_DATA_KEY:
44178 + case UBIFS_DENT_KEY:
44179 + case UBIFS_XENT_KEY:
44180 + break;
44181 + default:
44182 + dbg_msg("bad key type at slot %d: %s", i,
44183 + DBGKEY(&zbr->key));
44184 + err = 3;
44185 + goto out_dump;
44186 + }
44187 +
44188 + if (znode->level)
44189 + continue;
44190 +
44191 + type = key_type(c, &zbr->key);
44192 + if (c->ranges[type].max_len == 0) {
44193 + if (zbr->len != c->ranges[type].len) {
44194 + dbg_err("bad target node (type %d) length (%d)",
44195 + type, zbr->len);
44196 + dbg_err("have to be %d", c->ranges[type].len);
44197 + err = 4;
44198 + goto out_dump;
44199 + }
44200 + } else if (zbr->len < c->ranges[type].min_len ||
44201 + zbr->len > c->ranges[type].max_len) {
44202 + dbg_err("bad target node (type %d) length (%d)",
44203 + type, zbr->len);
44204 + dbg_err("have to be in range of %d-%d",
44205 + c->ranges[type].min_len,
44206 + c->ranges[type].max_len);
44207 + err = 5;
44208 + goto out_dump;
44209 + }
44210 + }
44211 +
44212 + /*
44213 + * Ensure that the next key is greater or equivalent to the
44214 + * previous one.
44215 + */
44216 + for (i = 0; i < znode->child_cnt - 1; i++) {
44217 + const union ubifs_key *key1, *key2;
44218 +
44219 + key1 = &znode->zbranch[i].key;
44220 + key2 = &znode->zbranch[i + 1].key;
44221 +
44222 + cmp = keys_cmp(c, key1, key2);
44223 + if (cmp > 0) {
44224 + dbg_err("bad key order (keys %d and %d)", i, i + 1);
44225 + err = 6;
44226 + goto out_dump;
44227 + } else if (cmp == 0 && !is_hash_key(c, key1)) {
44228 + /* These can only be keys with colliding hash */
44229 + dbg_err("keys %d and %d are not hashed but equivalent",
44230 + i, i + 1);
44231 + err = 7;
44232 + goto out_dump;
44233 + }
44234 + }
44235 +
44236 + kfree(idx);
44237 + return 0;
44238 +
44239 +out_dump:
44240 + ubifs_err("bad indexing node at LEB %d:%d, error %d", lnum, offs, err);
44241 + dbg_dump_node(c, idx);
44242 + kfree(idx);
44243 + return -EINVAL;
44244 +}
44245 +
44246 +/**
44247 + * ubifs_load_znode - load znode to TNC cache.
44248 + * @c: UBIFS file-system description object
44249 + * @zbr: znode branch
44250 + * @parent: znode's parent
44251 + * @iip: index in parent
44252 + *
44253 + * This function loads znode pointed to by @zbr into the TNC cache and
44254 + * returns pointer to it in case of success and a negative error code in case
44255 + * of failure.
44256 + */
44257 +struct ubifs_znode *ubifs_load_znode(struct ubifs_info *c,
44258 + struct ubifs_zbranch *zbr,
44259 + struct ubifs_znode *parent, int iip)
44260 +{
44261 + int err;
44262 + struct ubifs_znode *znode;
44263 +
44264 + ubifs_assert(!zbr->znode);
44265 + /*
44266 + * A slab cache is not presently used for znodes because the znode size
44267 + * depends on the fanout which is stored in the superblock.
44268 + */
44269 + znode = kzalloc(c->max_znode_sz, GFP_NOFS);
44270 + if (!znode)
44271 + return ERR_PTR(-ENOMEM);
44272 +
44273 + err = read_znode(c, zbr->lnum, zbr->offs, zbr->len, znode);
44274 + if (err)
44275 + goto out;
44276 +
44277 + atomic_long_inc(&c->clean_zn_cnt);
44278 +
44279 + /*
44280 + * Increment the global clean znode counter as well. It is OK that
44281 + * global and per-FS clean znode counters may be inconsistent for some
44282 + * short time (because we might be preempted at this point), the global
44283 + * one is only used in shrinker.
44284 + */
44285 + atomic_long_inc(&ubifs_clean_zn_cnt);
44286 +
44287 + zbr->znode = znode;
44288 + znode->parent = parent;
44289 + znode->time = get_seconds();
44290 + znode->iip = iip;
44291 +
44292 + return znode;
44293 +
44294 +out:
44295 + kfree(znode);
44296 + return ERR_PTR(err);
44297 +}
44298 +
44299 +/**
44300 + * ubifs_tnc_read_node - read a leaf node from the flash media.
44301 + * @c: UBIFS file-system description object
44302 + * @zbr: key and position of the node
44303 + * @node: node is returned here
44304 + *
44305 + * This function reads a node defined by @zbr from the flash media. Returns
44306 + * zero in case of success or a negative negative error code in case of
44307 + * failure.
44308 + */
44309 +int ubifs_tnc_read_node(struct ubifs_info *c, struct ubifs_zbranch *zbr,
44310 + void *node)
44311 +{
44312 + union ubifs_key key1, *key = &zbr->key;
44313 + int err, type = key_type(c, key);
44314 + struct ubifs_wbuf *wbuf;
44315 +
44316 + ubifs_assert(!zbr->leaf);
44317 +
44318 + /*
44319 + * 'zbr' has to point to on-flash node. The node may sit in a bud and
44320 + * may even be in a write buffer, so we have to take care about this.
44321 + */
44322 + wbuf = ubifs_get_wbuf(c, zbr->lnum);
44323 + if (wbuf)
44324 + err = ubifs_read_node_wbuf(wbuf, node, type, zbr->len,
44325 + zbr->lnum, zbr->offs);
44326 + else
44327 + err = ubifs_read_node(c, node, type, zbr->len, zbr->lnum,
44328 + zbr->offs);
44329 +
44330 + if (err) {
44331 + dbg_tnc("key %s", DBGKEY(key));
44332 + return err;
44333 + }
44334 +
44335 + /* Make sure the key of the read node is correct */
44336 + key_read(c, key, &key1);
44337 + if (memcmp(node + UBIFS_KEY_OFFSET, &key1, c->key_len)) {
44338 + ubifs_err("bad key in node at LEB %d:%d",
44339 + zbr->lnum, zbr->offs);
44340 + dbg_tnc("looked for key %s found node's key %s",
44341 + DBGKEY(key), DBGKEY1(&key1));
44342 + dbg_dump_node(c, node);
44343 + return -EINVAL;
44344 + }
44345 +
44346 + return 0;
44347 +}
44348 diff --exclude=.git -urN linux-2.6.25.6/fs/ubifs/ubifs.h avr32-2.6/fs/ubifs/ubifs.h
44349 --- linux-2.6.25.6/fs/ubifs/ubifs.h 1970-01-01 01:00:00.000000000 +0100
44350 +++ avr32-2.6/fs/ubifs/ubifs.h 2008-06-12 15:09:45.603817614 +0200
44351 @@ -0,0 +1,1605 @@
44352 +/*
44353 + * This file is part of UBIFS.
44354 + *
44355 + * Copyright (C) 2006-2008 Nokia Corporation
44356 + *
44357 + * This program is free software; you can redistribute it and/or modify it
44358 + * under the terms of the GNU General Public License version 2 as published by
44359 + * the Free Software Foundation.
44360 + *
44361 + * This program is distributed in the hope that it will be useful, but WITHOUT
44362 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
44363 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
44364 + * more details.
44365 + *
44366 + * You should have received a copy of the GNU General Public License along with
44367 + * this program; if not, write to the Free Software Foundation, Inc., 51
44368 + * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
44369 + *
44370 + * Authors: Artem Bityutskiy (Битюцкий Артём)
44371 + * Adrian Hunter
44372 + */
44373 +
44374 +/* Implementation version 0.7 */
44375 +
44376 +#ifndef __UBIFS_H__
44377 +#define __UBIFS_H__
44378 +
44379 +#include <asm/div64.h>
44380 +#include <linux/statfs.h>
44381 +#include <linux/fs.h>
44382 +#include <linux/err.h>
44383 +#include <linux/sched.h>
44384 +#include <linux/vmalloc.h>
44385 +#include <linux/spinlock.h>
44386 +#include <linux/mutex.h>
44387 +#include <linux/rwsem.h>
44388 +#include <linux/mtd/ubi.h>
44389 +#include <linux/pagemap.h>
44390 +#include <linux/backing-dev.h>
44391 +#include "ubifs-media.h"
44392 +
44393 +/* Version of this UBIFS implementation */
44394 +#define UBIFS_VERSION 1
44395 +
44396 +/* Normal UBIFS messages */
44397 +#define ubifs_msg(fmt, ...) \
44398 + printk(KERN_NOTICE "UBIFS: " fmt "\n", ##__VA_ARGS__)
44399 +/* UBIFS error messages */
44400 +#define ubifs_err(fmt, ...) \
44401 + printk(KERN_ERR "UBIFS error (pid %d): %s: " fmt "\n", current->pid, \
44402 + __func__, ##__VA_ARGS__)
44403 +/* UBIFS warning messages */
44404 +#define ubifs_warn(fmt, ...) \
44405 + printk(KERN_WARNING "UBIFS warning (pid %d): %s: " fmt "\n", \
44406 + current->pid, __func__, ##__VA_ARGS__)
44407 +
44408 +/* UBIFS file system VFS magic number */
44409 +#define UBIFS_SUPER_MAGIC 0x24051905
44410 +
44411 +/* Number of UBIFS blocks per VFS page */
44412 +#define UBIFS_BLOCKS_PER_PAGE (PAGE_CACHE_SIZE / UBIFS_BLOCK_SIZE)
44413 +#define UBIFS_BLOCKS_PER_PAGE_SHIFT (PAGE_CACHE_SHIFT - UBIFS_BLOCK_SHIFT)
44414 +
44415 +/* "File system end of life" sequence number watermark */
44416 +#define SQNUM_WARN_WATERMARK 0xFFFFFFFF00000000ULL
44417 +#define SQNUM_WATERMARK 0xFFFFFFFFFF000000ULL
44418 +
44419 +/* Minimum amount of data UBIFS writes to the flash */
44420 +#define MIN_WRITE_SZ (UBIFS_DATA_NODE_SZ + 8)
44421 +
44422 +/*
44423 + * Currently we do not support inode number overlapping and re-using, so this
44424 + * watermark defines dangerous inode number level. This should be fixed later,
44425 + * although it is difficult to exceed current limit. Another option is to use
44426 + * 64-bit inode numbers, but this means more overhead.
44427 + */
44428 +#define INUM_WARN_WATERMARK 0xFFF00000
44429 +#define INUM_WATERMARK 0xFFFFFF00
44430 +
44431 +/* Largest key size supported in this implementation */
44432 +#define CUR_MAX_KEY_LEN UBIFS_SK_LEN
44433 +
44434 +/* Maximum number of entries in each LPT (LEB category) heap */
44435 +#define LPT_HEAP_SZ 256
44436 +
44437 +/*
44438 + * Background thread name pattern. The numbers are UBI device and volume
44439 + * numbers.
44440 + */
44441 +#define BGT_NAME_PATTERN "ubifs_bgt%d_%d"
44442 +
44443 +/* Default write-buffer synchronization timeout (5 secs) */
44444 +#define DEFAULT_WBUF_TIMEOUT (5 * HZ)
44445 +
44446 +/* Maximum possible inode number (only 32-bit inodes are supported now) */
44447 +#define MAX_INUM 0xFFFFFFFF
44448 +
44449 +/* Number of non-data journal heads */
44450 +#define NONDATA_JHEADS_CNT 2
44451 +
44452 +/* Garbage collector head */
44453 +#define GCHD 0
44454 +/* Base journal head number */
44455 +#define BASEHD 1
44456 +/* First "general purpose" journal head */
44457 +#define DATAHD 2
44458 +
44459 +/* 'No change' value for 'ubifs_change_lp()' */
44460 +#define LPROPS_NC 0x80000001
44461 +
44462 +/*
44463 + * There is no notion of truncation key because truncation nodes do not exist
44464 + * in TNC. However, when replaying, it is handy to introduce fake "truncation"
44465 + * keys for truncation nodes because the code becomes simpler. So we define
44466 + * %UBIFS_TRUN_KEY type.
44467 + */
44468 +#define UBIFS_TRUN_KEY UBIFS_KEY_TYPES_CNT
44469 +
44470 +/*
44471 + * How much a directory entry/extended attribute entry adds to the parent/host
44472 + * inode.
44473 + */
44474 +#define CALC_DENT_SIZE(name_len) ALIGN(UBIFS_DENT_NODE_SZ + (name_len) + 1, 8)
44475 +
44476 +/* How much an extended attribute adds to the host inode */
44477 +#define CALC_XATTR_BYTES(data_len) ALIGN(UBIFS_INO_NODE_SZ + (data_len) + 1, 8)
44478 +
44479 +/*
44480 + * Znodes which were not touched for 'OLD_ZNODE_AGE' seconds are considered
44481 + * "old", and znode which were touched last 'YOUNG_ZNODE_AGE' seconds ago are
44482 + * considered "young". This is used by shrinker when selecting znode to trim
44483 + * off.
44484 + */
44485 +#define OLD_ZNODE_AGE 20
44486 +#define YOUNG_ZNODE_AGE 5
44487 +
44488 +/*
44489 + * Some compressors, like LZO, may end up with more data then the input buffer.
44490 + * So UBIFS always allocates larger output buffer, to be sure the compressor
44491 + * will not corrupt memory in case of worst case compression.
44492 + */
44493 +#define WORST_COMPR_FACTOR 2
44494 +
44495 +/* Maximum expected tree height for use by bottom_up_buf */
44496 +#define BOTTOM_UP_HEIGHT 64
44497 +
44498 +/*
44499 + * Znode flags (actually, bit numbers which store the flags).
44500 + *
44501 + * DIRTY_ZNODE: znode is dirty
44502 + * COW_ZNODE: znode is being committed and a new instance of this znode has to
44503 + * be created before changing this znode
44504 + * OBSOLETE_ZNODE: znode is obsolete, which means it was deleted, but it is
44505 + * still in the commit list and the ongoing commit operation
44506 + * will commit it, and delete this znode after it is done
44507 + */
44508 +enum {
44509 + DIRTY_ZNODE = 0,
44510 + COW_ZNODE = 1,
44511 + OBSOLETE_ZNODE = 2
44512 +};
44513 +
44514 +/*
44515 + * Commit states.
44516 + *
44517 + * COMMIT_RESTING: commit is not wanted
44518 + * COMMIT_BACKGROUND: background commit has been requested
44519 + * COMMIT_REQUIRED: commit is required
44520 + * COMMIT_RUNNING_BACKGROUND: background commit is running
44521 + * COMMIT_RUNNING_REQUIRED: commit is running and it is required
44522 + * COMMIT_BROKEN: commit failed
44523 + */
44524 +enum {
44525 + COMMIT_RESTING = 0,
44526 + COMMIT_BACKGROUND,
44527 + COMMIT_REQUIRED,
44528 + COMMIT_RUNNING_BACKGROUND,
44529 + COMMIT_RUNNING_REQUIRED,
44530 + COMMIT_BROKEN,
44531 +};
44532 +
44533 +/*
44534 + * 'ubifs_scan_a_node()' return values.
44535 + *
44536 + * SCANNED_GARBAGE: scanned garbage
44537 + * SCANNED_EMPTY_SPACE: scanned empty space
44538 + * SCANNED_A_NODE: scanned a valid node
44539 + * SCANNED_A_CORRUPT_NODE: scanned a corrupted node
44540 + * SCANNED_A_BAD_PAD_NODE: scanned a padding node with invalid pad length
44541 + *
44542 + * Greater than zero means: 'scanned that number of padding bytes'
44543 + */
44544 +enum {
44545 + SCANNED_GARBAGE = 0,
44546 + SCANNED_EMPTY_SPACE = -1,
44547 + SCANNED_A_NODE = -2,
44548 + SCANNED_A_CORRUPT_NODE = -3,
44549 + SCANNED_A_BAD_PAD_NODE = -4,
44550 +};
44551 +
44552 +/*
44553 + * LPT cnode flag bits.
44554 + *
44555 + * DIRTY_CNODE: cnode is dirty
44556 + * COW_CNODE: cnode is being committed and must be copied before writing
44557 + * OBSOLETE_CNODE: cnode is being committed and has been copied (or deleted),
44558 + * so it can (and must) be freed when the commit is finished
44559 + */
44560 +enum {
44561 + DIRTY_CNODE = 0,
44562 + COW_CNODE = 1,
44563 + OBSOLETE_CNODE = 2,
44564 +};
44565 +
44566 +/*
44567 + * Dirty flag bits (lpt_drty_flgs) for LPT special nodes.
44568 + *
44569 + * LTAB_DIRTY: ltab node is dirty
44570 + * LSAVE_DIRTY: lsave node is dirty
44571 + */
44572 +enum {
44573 + LTAB_DIRTY = 1,
44574 + LSAVE_DIRTY = 2,
44575 +};
44576 +
44577 +/*
44578 + * Return codes used by the garbage collector.
44579 + * @LEB_FREED: the logical eraseblock was freed and is ready to use
44580 + * @LEB_FREED_IDX: indexing LEB was freed and can be used only after the commit
44581 + * @LEB_RETAINED: the logical eraseblock was freed and retained for GC purposes
44582 + */
44583 +enum {
44584 + LEB_FREED,
44585 + LEB_FREED_IDX,
44586 + LEB_RETAINED,
44587 +};
44588 +
44589 +/**
44590 + * struct ubifs_old_idx - index node obsoleted since last commit start.
44591 + * @rb: rb-tree node
44592 + * @lnum: LEB number of obsoleted index node
44593 + * @offs: offset of obsoleted index node
44594 + */
44595 +struct ubifs_old_idx {
44596 + struct rb_node rb;
44597 + int lnum;
44598 + int offs;
44599 +};
44600 +
44601 +/* The below union makes it easier to deal with keys */
44602 +union ubifs_key {
44603 + uint8_t u8[CUR_MAX_KEY_LEN];
44604 + uint32_t u32[CUR_MAX_KEY_LEN/4];
44605 + uint64_t u64[CUR_MAX_KEY_LEN/8];
44606 + __le32 j32[CUR_MAX_KEY_LEN/4];
44607 +};
44608 +
44609 +/**
44610 + * struct ubifs_scan_node - UBIFS scanned node information.
44611 + * @list: list of scanned nodes
44612 + * @key: key of node scanned (if it has one)
44613 + * @sqnum: sequence number
44614 + * @type: type of node scanned
44615 + * @offs: offset with LEB of node scanned
44616 + * @len: length of node scanned
44617 + * @node: raw node
44618 + */
44619 +struct ubifs_scan_node {
44620 + struct list_head list;
44621 + union ubifs_key key;
44622 + unsigned long long sqnum;
44623 + int type;
44624 + int offs;
44625 + int len;
44626 + void *node;
44627 +};
44628 +
44629 +/**
44630 + * struct ubifs_scan_leb - UBIFS scanned LEB information.
44631 + * @lnum: logical eraseblock number
44632 + * @nodes_cnt: number of nodes scanned
44633 + * @nodes: list of struct ubifs_scan_node
44634 + * @endpt: end point (and therefore the start of empty space)
44635 + * @ecc: read returned -EBADMSG
44636 + * @buf: buffer containing entire LEB scanned
44637 + */
44638 +struct ubifs_scan_leb {
44639 + int lnum;
44640 + int nodes_cnt;
44641 + struct list_head nodes;
44642 + int endpt;
44643 + int ecc;
44644 + void *buf;
44645 +};
44646 +
44647 +/**
44648 + * struct ubifs_gced_idx_leb - garbage-collected indexing LEB.
44649 + * @list: list
44650 + * @lnum: LEB number
44651 + * @unmap: OK to unmap this LEB
44652 + *
44653 + * This data structure is used to temporary store garbage-collected indexing
44654 + * LEBs - they are not released immediately, but only after the next commit.
44655 + * This is needed to guarantee recoverability.
44656 + */
44657 +struct ubifs_gced_idx_leb {
44658 + struct list_head list;
44659 + int lnum;
44660 + int unmap;
44661 +};
44662 +
44663 +/**
44664 + * struct ubifs_inode - UBIFS in-memory inode description.
44665 + * @vfs_inode: VFS inode description object
44666 + * @creat_sqnum: sequence number at time of creation
44667 + * @xattr_size: summarized size of all extended attributes in bytes, protected
44668 + * by @inode->i_lock
44669 + * @xattr_cnt: count of extended attributes this inode has
44670 + * @xattr_names: sum of lengths of all extended attribute names belonging to
44671 + * this inode
44672 + * @dirty: non-zero if the inode is dirty
44673 + * @xattr: non-zero if this is an extended attribute inode
44674 + * @budgeted: non-zero if the inode has been budgeted (used for debugging)
44675 + * @budg_mutex: serializes inode budgeting and write-back
44676 + * @flags: inode flags (@UBIFS_COMPR_FL, etc)
44677 + * @compr_type: default compression type used for this inode
44678 + * @data_len: length of the data attached to the inode
44679 + * @data: inode's data
44680 + *
44681 + * UBIFS has its own inode mutex, besides the VFS 'i_mutex'. The reason for
44682 + * this is budgeting - UBIFS has to budget each operation. So, if an operation
44683 + * is going to mark an inode dirty, it has to allocate budget for this. It
44684 + * cannot just mark it dirty because there is no guarantee there will be enough
44685 + * flash space when it is time to write the inode back. This means that UBIFS
44686 + * has to have full control over "clean <-> dirty" transitions of inodes (and
44687 + * pages actually, but it is easy for pages, because we have
44688 + * 'ubifs_prepare_write()' which is called _before_ every page change). But
44689 + * unfortunately, VFS marks inodes dirty in many places, and it does not ask
44690 + * the file-system if it is allowed to do so (there is a notifier, but it is
44691 + * not enough), i.e., there is no mechanism to synchronize with this. So we
44692 + * introduce our own dirty flag to UBIFS inodes and our own inode mutex to
44693 + * serialize "clean <-> dirty" transitions.
44694 + */
44695 +struct ubifs_inode {
44696 + struct inode vfs_inode;
44697 + unsigned long long creat_sqnum;
44698 + long long xattr_size;
44699 + int xattr_cnt;
44700 + int xattr_names;
44701 + unsigned int dirty:1;
44702 + unsigned int xattr:1;
44703 +#ifdef CONFIG_UBIFS_FS_DEBUG
44704 + unsigned int budgeted:1;
44705 +#endif
44706 + struct mutex budg_mutex;
44707 + int flags;
44708 + int compr_type;
44709 + int data_len;
44710 + void *data;
44711 +};
44712 +
44713 +/**
44714 + * struct ubifs_unclean_leb - records a LEB recovered under read-only mode.
44715 + * @list: list
44716 + * @lnum: LEB number of recovered LEB
44717 + * @endpt: offset where recovery ended
44718 + *
44719 + * This structure records a LEB identified during recovery that needs to be
44720 + * cleaned but was not because UBIFS was mounted read-only. The information
44721 + * is used to clean the LEB when remounting to read-write mode.
44722 + */
44723 +struct ubifs_unclean_leb {
44724 + struct list_head list;
44725 + int lnum;
44726 + int endpt;
44727 +};
44728 +
44729 +/*
44730 + * LEB properties flags.
44731 + *
44732 + * LPROPS_UNCAT: not categorized
44733 + * LPROPS_DIRTY: dirty > 0, not index
44734 + * LPROPS_DIRTY_IDX: dirty + free > UBIFS_CH_SZ and index
44735 + * LPROPS_FREE: free > 0, not empty, not index
44736 + * LPROPS_HEAP_CNT: number of heaps used for storing categorized LEBs
44737 + * LPROPS_EMPTY: LEB is empty, not taken
44738 + * LPROPS_FREEABLE: free + dirty == leb_size, not index, not taken
44739 + * LPROPS_FRDI_IDX: free + dirty == leb_size and index, may be taken
44740 + * LPROPS_CAT_MASK: mask for the LEB categories above
44741 + * LPROPS_TAKEN: LEB was taken (this flag is not saved on the media)
44742 + * LPROPS_INDEX: LEB contains indexing nodes (this flag also exists on flash)
44743 + */
44744 +enum {
44745 + LPROPS_UNCAT = 0,
44746 + LPROPS_DIRTY = 1,
44747 + LPROPS_DIRTY_IDX = 2,
44748 + LPROPS_FREE = 3,
44749 + LPROPS_HEAP_CNT = 3,
44750 + LPROPS_EMPTY = 4,
44751 + LPROPS_FREEABLE = 5,
44752 + LPROPS_FRDI_IDX = 6,
44753 + LPROPS_CAT_MASK = 15,
44754 + LPROPS_TAKEN = 16,
44755 + LPROPS_INDEX = 32,
44756 +};
44757 +
44758 +/**
44759 + * struct ubifs_lprops - logical eraseblock properties.
44760 + * @free: amount of free space in bytes
44761 + * @dirty: amount of dirty space in bytes
44762 + * @flags: LEB properties flags (see above)
44763 + * @lnum: LEB number
44764 + * @list: list of same-category lprops (for LPROPS_EMPTY and LPROPS_FREEABLE)
44765 + * @hpos: heap position in heap of same-category lprops (other categories)
44766 + */
44767 +struct ubifs_lprops {
44768 + int free;
44769 + int dirty;
44770 + int flags;
44771 + int lnum;
44772 + union {
44773 + struct list_head list;
44774 + int hpos;
44775 + };
44776 +};
44777 +
44778 +/**
44779 + * struct ubifs_lpt_lprops - LPT logical eraseblock properties.
44780 + * @free: amount of free space in bytes
44781 + * @dirty: amount of dirty space in bytes
44782 + * @tgc: trivial GC flag (1 => unmap after commit end)
44783 + * @cmt: commit flag (1 => reserved for commit)
44784 + */
44785 +struct ubifs_lpt_lprops {
44786 + int free;
44787 + int dirty;
44788 + unsigned tgc : 1;
44789 + unsigned cmt : 1;
44790 +};
44791 +
44792 +/**
44793 + * struct ubifs_lp_stats - statistics of eraseblocks in the main area.
44794 + * @empty_lebs: number of empty LEBs
44795 + * @taken_empty_lebs: number of taken LEBs
44796 + * @idx_lebs: number of indexing LEBs
44797 + * @total_free: total free space in bytes
44798 + * @total_dirty: total dirty space in bytes
44799 + * @total_used: total used space in bytes (includes only data LEBs)
44800 + * @total_dead: total dead space in bytes (includes only data LEBs)
44801 + * @total_dark: total dark space in bytes (includes only data LEBs)
44802 + *
44803 + * N.B. total_dirty and total_used are different to other total_* fields,
44804 + * because they account _all_ LEBs, not just data LEBs.
44805 + *
44806 + * 'taken_empty_lebs' counts the LEBs that are in the transient state of having
44807 + * been 'taken' for use but not yet written to. 'taken_empty_lebs' is needed
44808 + * to account correctly for gc_lnum, otherwise 'empty_lebs' could be used
44809 + * by itself (in which case 'unused_lebs' would be a better name). In the case
44810 + * of gc_lnum, it is 'taken' at mount time or whenever a LEB is retained by GC,
44811 + * but unlike other empty LEBs that are 'taken', it may not be written straight
44812 + * away (i.e. before the next commit start or unmount), so either gc_lnum must
44813 + * be specially accounted for, or the current approach followed i.e. count it
44814 + * under 'taken_empty_lebs'.
44815 + */
44816 +struct ubifs_lp_stats {
44817 + int empty_lebs;
44818 + int taken_empty_lebs;
44819 + int idx_lebs;
44820 + long long total_free;
44821 + long long total_dirty;
44822 + long long total_used;
44823 + long long total_dead;
44824 + long long total_dark;
44825 +};
44826 +
44827 +struct ubifs_nnode;
44828 +
44829 +/**
44830 + * struct ubifs_cnode - LEB Properties Tree common node.
44831 + * @parent: parent nnode
44832 + * @cnext: next cnode to commit
44833 + * @flags: flags (%DIRTY_LPT_NODE or %OBSOLETE_LPT_NODE)
44834 + * @iip: index in parent
44835 + * @level: level in the tree (zero for pnodes, greater than zero for nnodes)
44836 + * @num: node number
44837 + */
44838 +struct ubifs_cnode {
44839 + struct ubifs_nnode *parent;
44840 + struct ubifs_cnode *cnext;
44841 + unsigned long flags;
44842 + int iip;
44843 + int level;
44844 + int num;
44845 +};
44846 +
44847 +/**
44848 + * struct ubifs_pnode - LEB Properties Tree leaf node.
44849 + * @parent: parent nnode
44850 + * @cnext: next cnode to commit
44851 + * @flags: flags (%DIRTY_LPT_NODE or %OBSOLETE_LPT_NODE)
44852 + * @iip: index in parent
44853 + * @level: level in the tree (always zero for pnodes)
44854 + * @num: node number
44855 + * @lprops: LEB properties array
44856 + */
44857 +struct ubifs_pnode {
44858 + struct ubifs_nnode *parent;
44859 + struct ubifs_cnode *cnext;
44860 + unsigned long flags;
44861 + int iip;
44862 + int level;
44863 + int num;
44864 + struct ubifs_lprops lprops[UBIFS_LPT_FANOUT];
44865 +};
44866 +
44867 +/**
44868 + * struct ubifs_nbranch - LEB Properties Tree internal node branch.
44869 + * @lnum: LEB number of child
44870 + * @offs: offset of child
44871 + * @nnode: nnode child
44872 + * @pnode: pnode child
44873 + * @cnode: cnode child
44874 + */
44875 +struct ubifs_nbranch {
44876 + int lnum;
44877 + int offs;
44878 + union {
44879 + struct ubifs_nnode *nnode;
44880 + struct ubifs_pnode *pnode;
44881 + struct ubifs_cnode *cnode;
44882 + };
44883 +};
44884 +
44885 +/**
44886 + * struct ubifs_nnode - LEB Properties Tree internal node.
44887 + * @parent: parent nnode
44888 + * @cnext: next cnode to commit
44889 + * @flags: flags (%DIRTY_LPT_NODE or %OBSOLETE_LPT_NODE)
44890 + * @iip: index in parent
44891 + * @level: level in the tree (always greater than zero for nnodes)
44892 + * @num: node number
44893 + * @nbranch: branches to child nodes
44894 + */
44895 +struct ubifs_nnode {
44896 + struct ubifs_nnode *parent;
44897 + struct ubifs_cnode *cnext;
44898 + unsigned long flags;
44899 + int iip;
44900 + int level;
44901 + int num;
44902 + struct ubifs_nbranch nbranch[UBIFS_LPT_FANOUT];
44903 +};
44904 +
44905 +/**
44906 + * struct ubifs_lpt_heap - heap of categorized lprops.
44907 + * @arr: heap array
44908 + * @cnt: number in heap
44909 + * @max_cnt: maximum number allowed in heap
44910 + *
44911 + * There are %LPROPS_HEAP_CNT heaps.
44912 + */
44913 +struct ubifs_lpt_heap {
44914 + struct ubifs_lprops **arr;
44915 + int cnt;
44916 + int max_cnt;
44917 +};
44918 +
44919 +/*
44920 + * Return codes for LPT scan callback function.
44921 + *
44922 + * LPT_SCAN_CONTINUE: continue scanning
44923 + * LPT_SCAN_ADD: add the LEB properties scanned to the tree in memory
44924 + * LPT_SCAN_STOP: stop scanning
44925 + */
44926 +enum {
44927 + LPT_SCAN_CONTINUE = 0,
44928 + LPT_SCAN_ADD = 1,
44929 + LPT_SCAN_STOP = 2,
44930 +};
44931 +
44932 +struct ubifs_info;
44933 +
44934 +/* Callback used by the 'ubifs_lpt_scan_nolock()' function */
44935 +typedef int (*ubifs_lpt_scan_callback)(struct ubifs_info *c,
44936 + const struct ubifs_lprops *lprops,
44937 + int in_tree, void *data);
44938 +
44939 +/**
44940 + * struct ubifs_wbuf - UBIFS write-buffer.
44941 + * @c: UBIFS file-system description object
44942 + * @buf: write-buffer (of min. flash I/O unit size)
44943 + * @lnum: logical eraseblock number the write-buffer points to
44944 + * @offs: write-buffer offset in this logical eraseblock
44945 + * @avail: number of bytes available in the write-buffer
44946 + * @used: number of used bytes in the write-buffer
44947 + * @dtype: type of data stored in this LEB (%UBI_LONGTERM, %UBI_SHORTTERM,
44948 + * %UBI_UNKNOWN)
44949 + * @jhead: journal head the mutex belongs to (note, needed only to shut lockdep
44950 + * up by 'mutex_lock_nested()).
44951 + * @sync_callback: write-buffer synchronization callback
44952 + * @io_mutex: serializes write-buffer I/O
44953 + * @lock: serializes @buf, @lnum, @offs, @avail, @used, @next_ino and @inodes
44954 + * fields
44955 + * @timer: write-buffer timer
44956 + * @timeout: timer expire interval in jiffies
44957 + * @need_sync: it is set if its timer expired and needs sync
44958 + * @next_ino: points to the next position of the following inode number
44959 + * @inodes: stores the inode numbers of the nodes which are in wbuf
44960 + *
44961 + * The write-buffer synchronization callback is called when the write-buffer is
44962 + * synchronized in order to notify how much space was wasted due to
44963 + * write-buffer padding and how much free space is left in the LEB.
44964 + *
44965 + * Note: the fields @buf, @lnum, @offs, @avail and @used can be read under
44966 + * spin-lock or mutex because they are written under both mutex and spin-lock.
44967 + * @buf is appended to under mutex but overwritten under both mutex and
44968 + * spin-lock. Thus the data between @buf and @buf + @used can be read under
44969 + * spinlock.
44970 + */
44971 +struct ubifs_wbuf {
44972 + struct ubifs_info *c;
44973 + void *buf;
44974 + int lnum;
44975 + int offs;
44976 + int avail;
44977 + int used;
44978 + int dtype;
44979 + int jhead;
44980 + int (*sync_callback)(struct ubifs_info *c, int lnum, int free, int pad);
44981 + struct mutex io_mutex;
44982 + spinlock_t lock;
44983 + struct timer_list timer;
44984 + int timeout;
44985 + int need_sync;
44986 + int next_ino;
44987 + ino_t *inodes;
44988 +};
44989 +
44990 +/**
44991 + * struct ubifs_bud - bud logical eraseblock.
44992 + * @lnum: logical eraseblock number
44993 + * @start: where the (uncommitted) bud data starts
44994 + * @jhead: journal head number this bud belongs to
44995 + * @list: link in the list buds belonging to the same journal head
44996 + * @rb: link in the tree of all buds
44997 + */
44998 +struct ubifs_bud {
44999 + int lnum;
45000 + int start;
45001 + int jhead;
45002 + struct list_head list;
45003 + struct rb_node rb;
45004 +};
45005 +
45006 +/**
45007 + * struct ubifs_jhead - journal head.
45008 + * @wbuf: head's write-buffer
45009 + * @buds_list: list of bud LEBs belonging to this journal head
45010 + *
45011 + * Note, the @buds list is protected by the @c->buds_lock.
45012 + */
45013 +struct ubifs_jhead {
45014 + struct ubifs_wbuf wbuf;
45015 + struct list_head buds_list;
45016 +};
45017 +
45018 +/**
45019 + * struct ubifs_zbranch - key/coordinate/length branch stored in znodes.
45020 + * @key: key
45021 + * @znode: znode address in memory
45022 + * @lnum: LEB number of the indexing node
45023 + * @offs: offset of the indexing node within @lnum
45024 + * @len: target node length
45025 + */
45026 +struct ubifs_zbranch {
45027 + union ubifs_key key;
45028 + union {
45029 + struct ubifs_znode *znode;
45030 + void *leaf;
45031 + };
45032 + int lnum;
45033 + int offs;
45034 + int len;
45035 +};
45036 +
45037 +/**
45038 + * struct ubifs_znode - in-memory representation of an indexing node.
45039 + * @parent: parent znode or NULL if it is the root
45040 + * @cnext: next znode to commit
45041 + * @flags: znode flags (%DIRTY_ZNODE, %COW_ZNODE or %OBSOLETE_ZNODE)
45042 + * @time: last access time (seconds)
45043 + * @level: level of the entry in the TNC tree
45044 + * @child_cnt: count of child znodes
45045 + * @iip: index in parent's zbranch array
45046 + * @alt: lower bound of key range has altered i.e. child inserted at slot 0
45047 + * @lnum: LEB number of the corresponding indexing node
45048 + * @offs: offset of the corresponding indexing node
45049 + * @len: length of the corresponding indexing node
45050 + * @zbranch: array of znode branches (@c->fanout elements)
45051 + */
45052 +struct ubifs_znode {
45053 + struct ubifs_znode *parent;
45054 + struct ubifs_znode *cnext;
45055 + unsigned long flags;
45056 + unsigned long time;
45057 + int level;
45058 + int child_cnt;
45059 + int iip;
45060 + int alt;
45061 +#ifdef CONFIG_UBIFS_FS_DEBUG
45062 + int lnum, offs, len;
45063 +#endif
45064 + struct ubifs_zbranch zbranch[];
45065 +};
45066 +
45067 +/**
45068 + * struct ubifs_node_range - node length range description data structure.
45069 + * @len: fixed node length
45070 + * @min_len: minimum possible node length
45071 + * @max_len: maximum possible node length
45072 + *
45073 + * If @max_len is %0, the node has fixed length @len.
45074 + */
45075 +struct ubifs_node_range {
45076 + union {
45077 + int len;
45078 + int min_len;
45079 + };
45080 + int max_len;
45081 +};
45082 +
45083 +/**
45084 + * struct ubifs_compressor - UBIFS compressor description structure.
45085 + * @compr_type: compressor type (%UBIFS_COMPR_LZO, etc)
45086 + * @cc: cryptoapi compressor handle
45087 + * @comp_mutex: mutex used during compression
45088 + * @decomp_mutex: mutex used during decompression
45089 + * @name: compressor name
45090 + * @capi_name: cryptoapi compressor name
45091 + */
45092 +struct ubifs_compressor {
45093 + int compr_type;
45094 + struct crypto_comp *cc;
45095 + struct mutex *comp_mutex;
45096 + struct mutex *decomp_mutex;
45097 + const char *name;
45098 + const char *capi_name;
45099 +};
45100 +
45101 +/**
45102 + * struct ubifs_budget_req - budget requirements of an operation.
45103 + *
45104 + * @new_ino: non-zero if the operation adds a new inode
45105 + * @dirtied_ino: how many inodes the operation makes dirty
45106 + * @new_page: non-zero if the operation adds a new page
45107 + * @dirtied_page: non-zero if the operation makes a page dirty
45108 + * @new_dent: non-zero if the operation adds a new directory entry
45109 + * @mod_dent: non-zero if the operation removes or modifies an existing
45110 + * directory entry
45111 + * @new_ino_d: now much data newly created inode contains
45112 + * @dirtied_ino_d: now much data dirtied inode contains
45113 + * @idx_growth: how much the index will supposedly grow
45114 + * @data_growth: how much new data the operation will supposedly add
45115 + * @dd_growth: how much data that makes other data dirty the operation will
45116 + * supposedly add
45117 + *
45118 + * @idx_growth, @data_growth and @dd_growth are not used in budget request. The
45119 + * budgeting subsystem caches index and data growth values there to avoid
45120 + * re-calculating them when the budget is released. However, if @idx_growth is
45121 + * %-1, it is calculated by the release function using other fields.
45122 + *
45123 + * An inode may contain 4KiB of data at max., thus the widths of @new_ino_d
45124 + * is 13 bits, and @dirtied_ino_d - 15, because up to 4 inodes may be made
45125 + * dirty by the re-name operation.
45126 + */
45127 +struct ubifs_budget_req {
45128 + unsigned int new_ino:1;
45129 + unsigned int dirtied_ino:4;
45130 + unsigned int new_page:1;
45131 + unsigned int dirtied_page:1;
45132 + unsigned int new_dent:1;
45133 + unsigned int mod_dent:1;
45134 + unsigned int new_ino_d:13;
45135 + unsigned int dirtied_ino_d:15;
45136 + int idx_growth;
45137 + int data_growth;
45138 + int dd_growth;
45139 +};
45140 +
45141 +/**
45142 + * struct ubifs_orphan - stores the inode number of an orphan.
45143 + * @rb: rb-tree node of rb-tree of orphans sorted by inode number
45144 + * @list: list head of list of orphans in order added
45145 + * @new_list: list head of list of orphans added since the last commit
45146 + * @cnext: next orphan to commit
45147 + * @dnext: next orphan to delete
45148 + * @inum: inode number
45149 + * @new: %1 => added since the last commit, otherwise %0
45150 + */
45151 +struct ubifs_orphan {
45152 + struct rb_node rb;
45153 + struct list_head list;
45154 + struct list_head new_list;
45155 + struct ubifs_orphan *cnext;
45156 + struct ubifs_orphan *dnext;
45157 + ino_t inum;
45158 + int new;
45159 +};
45160 +
45161 +/**
45162 + * struct ubifs_mount_opts - UBIFS-specific mount options information.
45163 + * @unmount_mode: selected unmount mode (%0 default, %1 normal, %2 fast)
45164 + */
45165 +struct ubifs_mount_opts {
45166 + unsigned int unmount_mode:2;
45167 +};
45168 +
45169 +/**
45170 + * struct ubifs_info - UBIFS file-system description data structure
45171 + * (per-superblock).
45172 + * @vfs_sb: VFS @struct super_block object
45173 + *
45174 + * @highest_inum: highest used inode number
45175 + * @vfs_gen: VFS inode generation counter
45176 + * @max_sqnum: current global sequence number
45177 + * @cmt_no: commit number (last successfully completed commit)
45178 + * @cnt_lock: protects @highest_inum, @vfs_gen, and @max_sqnum counters
45179 + * @fmt_version: UBIFS on-flash format version
45180 + * @uuid: UUID from super block
45181 + *
45182 + * @lhead_lnum: log head logical eraseblock number
45183 + * @lhead_offs: log head offset
45184 + * @ltail_lnum: log tail logical eraseblock number (offset is always 0)
45185 + * @log_mutex: protects the log, @lhead_lnum, @lhead_offs, @ltail_lnum, and
45186 + * @bud_bytes
45187 + * @min_log_bytes: minimum required number of bytes in the log
45188 + * @cmt_bud_bytes: used during commit to temporarily amount of bytes in
45189 + * committed buds
45190 + *
45191 + * @buds: tree of all buds indexed by bud LEB number
45192 + * @bud_bytes: how many bytes of flash is used by buds
45193 + * @buds_lock: protects the @buds tree, @bud_bytes, and per-journal head bud
45194 + * lists
45195 + * @jhead_cnt: count of journal heads
45196 + * @jheads: journal heads (head zero is base head)
45197 + * @max_bud_bytes: maximum number of bytes allowed in buds
45198 + * @bg_bud_bytes: number of bud bytes when background commit is initiated
45199 + * @old_buds: buds to be released after commit ends
45200 + * @max_bud_cnt: maximum number of buds
45201 + *
45202 + * @commit_sem: synchronizes committer with other processes
45203 + * @cmt_state: commit state
45204 + * @cs_lock: commit state lock
45205 + * @cmt_wq: wait queue to sleep on if the log is full and a commit is running
45206 + * @fast_unmount: do not run journal commit before unmounting
45207 + * @big_lpt: flag that LPT is too big to write whole during commit
45208 + *
45209 + * @tnc_mutex: protects the Tree Node Cache (TNC), @zroot, @cnext, @enext, and
45210 + * @calc_idx_sz
45211 + * @zroot: zbranch which points to the root index node and znode
45212 + * @cnext: next znode to commit
45213 + * @enext: next znode to commit to empty space
45214 + * @gap_lebs: array of LEBs used by the in-gaps commit method
45215 + * @cbuf: commit buffer
45216 + * @ileb_buf: buffer for commit in-the-gaps method
45217 + * @ileb_len: length of data in ileb_buf
45218 + * @ihead_lnum: LEB number of index head
45219 + * @ihead_offs: offset of index head
45220 + * @ilebs: pre-allocated index LEBs
45221 + * @ileb_cnt: number of pre-allocated index LEBs
45222 + * @ileb_nxt: next pre-allocated index LEBs
45223 + * @old_idx: tree of index nodes obsoleted since the last commit start
45224 + * @bottom_up_buf: a buffer which is used by 'dirty_cow_bottom_up()' in tnc.c
45225 + * @new_ihead_lnum: used by debugging to check ihead_lnum
45226 + * @new_ihead_offs: used by debugging to check ihead_offs
45227 + *
45228 + * @mst_node: master node
45229 + * @mst_offs: offset of valid master node
45230 + * @mst_mutex: protects the master node area, @mst_node, and @mst_offs
45231 + *
45232 + * @log_lebs: number of logical eraseblocks in the log
45233 + * @log_bytes: log size in bytes
45234 + * @log_last: last LEB of the log
45235 + * @lpt_lebs: number of LEBs used for lprops table
45236 + * @lpt_first: first LEB of the lprops table area
45237 + * @lpt_last: last LEB of the lprops table area
45238 + * @orph_lebs: number of LEBs used for the orphan area
45239 + * @orph_first: first LEB of the orphan area
45240 + * @orph_last: last LEB of the orphan area
45241 + * @main_lebs: count of LEBs in the main area
45242 + * @main_first: first LEB of the main area
45243 + * @main_bytes: main area size in bytes
45244 + * @default_compr: default compression type
45245 + *
45246 + * @key_hash_type: type of the key hash
45247 + * @key_hash: direntry key hash function
45248 + * @key_fmt: key format
45249 + * @key_len: key length
45250 + * @fanout: fanout of the index tree (number of links per indexing node)
45251 + *
45252 + * @min_io_size: minimal input/output unit size
45253 + * @min_io_shift: number of bits in @min_io_size minus one
45254 + * @leb_size: logical eraseblock size in bytes
45255 + * @half_leb_size: half LEB size
45256 + * @leb_cnt: count of logical eraseblocks
45257 + * @max_leb_cnt: maximum count of logical eraseblocks
45258 + * @old_leb_cnt: count of logical eraseblocks before resize
45259 + * @ro_media: the underlying UBI volume is read-only
45260 + *
45261 + * @dirty_pg_cnt: number of dirty pages (not used)
45262 + * @dirty_ino_cnt: number of dirty inodes (not used)
45263 + * @dirty_zn_cnt: number of dirty znodes
45264 + * @clean_zn_cnt: number of clean znodes
45265 + *
45266 + * @budg_idx_growth: amount of bytes budgeted for index growth
45267 + * @budg_data_growth: amount of bytes budgeted for cached data
45268 + * @budg_dd_growth: amount of bytes budgeted for cached data that will make
45269 + * other data dirty
45270 + * @budg_uncommitted_idx: amount of bytes were budgeted for growth of the index,
45271 + * but which still have to be taken into account because
45272 + * the index has not been committed so far
45273 + * @space_lock: protects @budg_idx_growth, @budg_data_growth, @budg_dd_growth,
45274 + * @budg_uncommited_idx, @min_idx_lebs, @old_idx_sz, and @lst;
45275 + * @min_idx_lebs: minimum number of LEBs required for the index
45276 + * @old_idx_sz: size of index on flash
45277 + * @calc_idx_sz: temporary variable which is used to calculate new index size
45278 + * (contains accurate new index size at end of TNC commit start)
45279 + * @lst: lprops statistics
45280 + *
45281 + * @page_budget: budget for a page
45282 + * @inode_budget: budget for an inode
45283 + * @dent_budget: budget for a directory entry
45284 + *
45285 + * @ref_node_alsz: size of the LEB reference node aligned to the min. flash
45286 + * I/O unit
45287 + * @mst_node_alsz: master node aligned size
45288 + * @min_idx_node_sz: minimum indexing node aligned on 8-bytes boundary
45289 + * @max_idx_node_sz: maximum indexing node aligned on 8-bytes boundary
45290 + * @max_inode_sz: maximum possible inode size in bytes
45291 + * @max_znode_sz: size of znode in bytes
45292 + * @dead_wm: LEB dead space watermark
45293 + * @dark_wm: LEB dark space watermark
45294 + * @block_cnt: count of 4KiB blocks on the FS
45295 + *
45296 + * @ranges: UBIFS node length ranges
45297 + * @ubi: UBI volume descriptor
45298 + * @di: UBI device information
45299 + * @vi: UBI volume information
45300 + *
45301 + * @orph_tree: rb-tree of orphan inode numbers
45302 + * @orph_list: list of orphan inode numbers in order added
45303 + * @orph_new: list of orphan inode numbers added since last commit
45304 + * @orph_cnext: next orphan to commit
45305 + * @orph_dnext: next orphan to delete
45306 + * @orphan_lock: lock for orph_tree and orph_new
45307 + * @orph_buf: buffer for orphan nodes
45308 + * @new_orphans: number of orphans since last commit
45309 + * @cmt_orphans: number of orphans being committed
45310 + * @tot_orphans: number of orphans in the rb_tree
45311 + * @max_orphans: maximum number of orphans allowed
45312 + * @ohead_lnum: orphan head LEB number
45313 + * @ohead_offs: orphan head offset
45314 + * @no_orphs: non-zero if there are no orphans
45315 + *
45316 + * @bgt: UBIFS background thread
45317 + * @bgt_name: background thread name
45318 + * @need_bgt: if background thread should run
45319 + * @need_wbuf_sync: if write-buffers have to be synchronized
45320 + *
45321 + * @gc_lnum: LEB number used for garbage collection
45322 + * @sbuf: a buffer of LEB size used by GC and replay for scanning
45323 + * @idx_gc: list of index LEBs that have been garbage collected
45324 + * @idx_gc_cnt: number of elements on the idx_gc list
45325 + *
45326 + * @infos_list: links all 'ubifs_info' objects
45327 + * @umount_mutex: serializes shrinker and un-mount
45328 + * @shrinker_run_no: shrinker run number
45329 + *
45330 + * @space_bits: number of bits needed to record free or dirty space
45331 + * @lpt_lnum_bits: number of bits needed to record a LEB number in the LPT
45332 + * @lpt_offs_bits: number of bits needed to record an offset in the LPT
45333 + * @lpt_spc_bits: number of bits needed to space in the LPT
45334 + * @pcnt_bits: number of bits needed to record pnode or nnode number
45335 + * @lnum_bits: number of bits needed to record LEB number
45336 + * @nnode_sz: size of on-flash nnode
45337 + * @pnode_sz: size of on-flash pnode
45338 + * @ltab_sz: size of on-flash LPT lprops table
45339 + * @lsave_sz: size of on-flash LPT save table
45340 + * @pnode_cnt: number of pnodes
45341 + * @nnode_cnt: number of nnodes
45342 + * @lpt_hght: height of the LPT
45343 + * @pnodes_have: number of pnodes in memory
45344 + *
45345 + * @lp_mutex: protects lprops table and all the other lprops-related fields
45346 + * @lpt_lnum: LEB number of the root nnode of the LPT
45347 + * @lpt_offs: offset of the root nnode of the LPT
45348 + * @nhead_lnum: LEB number of LPT head
45349 + * @nhead_offs: offset of LPT head
45350 + * @lpt_drty_flgs: dirty flags for LPT special nodes e.g. ltab
45351 + * @dirty_nn_cnt: number of dirty nnodes
45352 + * @dirty_pn_cnt: number of dirty pnodes
45353 + * @lpt_sz: LPT size
45354 + * @lpt_nod_buf: buffer for an on-flash nnode or pnode
45355 + * @lpt_buf: buffer of LEB size used by LPT
45356 + * @nroot: address in memory of the root nnode of the LPT
45357 + * @lpt_cnext: next LPT node to commit
45358 + * @lpt_heap: array of heaps of categorized lprops
45359 + * @dirty_idx: a (reverse sorted) copy of the LPROPS_DIRTY_IDX heap as at
45360 + * previous commit start
45361 + * @uncat_list: list of un-categorized LEBs
45362 + * @empty_list: list of empty LEBs
45363 + * @freeable_list: list of freeable non-index LEBs (free + dirty == leb_size)
45364 + * @frdi_idx_list: list of freeable index LEBs (free + dirty == leb_size)
45365 + * @freeable_cnt: number of freeable LEBs in @freeable_list
45366 + *
45367 + * @ltab_lnum: LEB number of LPT's own lprops table
45368 + * @ltab_offs: offset of LPT's own lprops table
45369 + * @ltab: LPT's own lprops table
45370 + * @ltab_cmt: LPT's own lprops table (commit copy)
45371 + * @lsave_cnt: number of LEB numbers in LPT's save table
45372 + * @lsave_lnum: LEB number of LPT's save table
45373 + * @lsave_offs: offset of LPT's save table
45374 + * @lsave: LPT's save table
45375 + * @lscan_lnum: LEB number of last LPT scan
45376 + *
45377 + * @rp_size: size of the reserved pool in bytes
45378 + * @report_rp_size: size of the reserved pool reported to userspace
45379 + * @rp_uid: reserved pool user ID
45380 + * @rp_gid: reserved pool group ID
45381 + *
45382 + * @empty: if the UBI device is empty
45383 + * @replay_tree: temporary tree used during journal replay
45384 + * @replay_list: temporary list used during journal replay
45385 + * @replay_buds: list of buds to replay
45386 + * @cs_sqnum: sequence number of first node in the log (commit start node)
45387 + * @replay_sqnum: sequence number of node currently being replayed
45388 + * @need_recovery: file-system needs recovery
45389 + * @replaying: set to %1 during journal replay
45390 + * @unclean_leb_list: LEBs to recover when mounting ro to rw
45391 + * @rcvrd_mst_node: recovered master node to write when mounting ro to rw
45392 + * @size_tree: inode size information for recovery
45393 + * @remounting_rw: set while remounting from ro to rw (sb flags have MS_RDONLY)
45394 + * @mount_opts: UBIFS-specific mount options
45395 + *
45396 + * @dbg_buf: a buffer of LEB size used for debugging purposes
45397 + * @old_zroot: old index root - used by 'dbg_check_old_index()'
45398 + * @old_zroot_level: old index root level - used by 'dbg_check_old_index()'
45399 + * @old_zroot_sqnum: old index root sqnum - used by 'dbg_check_old_index()'
45400 + * @failure_mode: failure mode for recovery testing
45401 + * @fail_delay: 0=>don't delay, 1=>delay a time, 2=>delay a number of calls
45402 + * @fail_timeout: time in jiffies when delay of failure mode expires
45403 + * @fail_cnt: current number of calls to failure mode I/O functions
45404 + * @fail_cnt_max: number of calls by which to delay failure mode
45405 + */
45406 +struct ubifs_info {
45407 + struct super_block *vfs_sb;
45408 +
45409 + ino_t highest_inum;
45410 + unsigned int vfs_gen;
45411 + unsigned long long max_sqnum;
45412 + unsigned long long cmt_no;
45413 + spinlock_t cnt_lock;
45414 + int fmt_version;
45415 + unsigned char uuid[16];
45416 +
45417 + int lhead_lnum;
45418 + int lhead_offs;
45419 + int ltail_lnum;
45420 + struct mutex log_mutex;
45421 + int min_log_bytes;
45422 + long long cmt_bud_bytes;
45423 +
45424 + struct rb_root buds;
45425 + long long bud_bytes;
45426 + spinlock_t buds_lock;
45427 + int jhead_cnt;
45428 + struct ubifs_jhead *jheads;
45429 + long long max_bud_bytes;
45430 + long long bg_bud_bytes;
45431 + struct list_head old_buds;
45432 + int max_bud_cnt;
45433 +
45434 + struct rw_semaphore commit_sem;
45435 + int cmt_state;
45436 + spinlock_t cs_lock;
45437 + wait_queue_head_t cmt_wq;
45438 + unsigned int fast_unmount:1;
45439 + unsigned int big_lpt:1;
45440 +
45441 + struct mutex tnc_mutex;
45442 + struct ubifs_zbranch zroot;
45443 + struct ubifs_znode *cnext;
45444 + struct ubifs_znode *enext;
45445 + int *gap_lebs;
45446 + void *cbuf;
45447 + void *ileb_buf;
45448 + int ileb_len;
45449 + int ihead_lnum;
45450 + int ihead_offs;
45451 + int *ilebs;
45452 + int ileb_cnt;
45453 + int ileb_nxt;
45454 + struct rb_root old_idx;
45455 + int *bottom_up_buf;
45456 +#ifdef CONFIG_UBIFS_FS_DEBUG
45457 + int new_ihead_lnum;
45458 + int new_ihead_offs;
45459 +#endif
45460 +
45461 + struct ubifs_mst_node *mst_node;
45462 + int mst_offs;
45463 + struct mutex mst_mutex;
45464 +
45465 + int log_lebs;
45466 + long long log_bytes;
45467 + int log_last;
45468 + int lpt_lebs;
45469 + int lpt_first;
45470 + int lpt_last;
45471 + int orph_lebs;
45472 + int orph_first;
45473 + int orph_last;
45474 + int main_lebs;
45475 + int main_first;
45476 + long long main_bytes;
45477 + int default_compr;
45478 +
45479 + uint8_t key_hash_type;
45480 + uint32_t (*key_hash)(const char *str, int len);
45481 + int key_fmt;
45482 + int key_len;
45483 + int fanout;
45484 +
45485 + int min_io_size;
45486 + int min_io_shift;
45487 + int leb_size;
45488 + int half_leb_size;
45489 + int leb_cnt;
45490 + int max_leb_cnt;
45491 + int old_leb_cnt;
45492 + int ro_media;
45493 +
45494 + atomic_long_t dirty_pg_cnt;
45495 + atomic_long_t dirty_ino_cnt;
45496 + atomic_long_t dirty_zn_cnt;
45497 + atomic_long_t clean_zn_cnt;
45498 +
45499 + long long budg_idx_growth;
45500 + long long budg_data_growth;
45501 + long long budg_dd_growth;
45502 + long long budg_uncommitted_idx;
45503 + spinlock_t space_lock;
45504 + int min_idx_lebs;
45505 + unsigned long long old_idx_sz;
45506 + unsigned long long calc_idx_sz;
45507 + struct ubifs_lp_stats lst;
45508 +
45509 + int page_budget;
45510 + int inode_budget;
45511 + int dent_budget;
45512 +
45513 + int ref_node_alsz;
45514 + int mst_node_alsz;
45515 + int min_idx_node_sz;
45516 + int max_idx_node_sz;
45517 + long long max_inode_sz;
45518 + int max_znode_sz;
45519 + int dead_wm;
45520 + int dark_wm;
45521 + int block_cnt;
45522 +
45523 + struct ubifs_node_range ranges[UBIFS_NODE_TYPES_CNT];
45524 + struct ubi_volume_desc *ubi;
45525 + struct ubi_device_info di;
45526 + struct ubi_volume_info vi;
45527 +
45528 + struct rb_root orph_tree;
45529 + struct list_head orph_list;
45530 + struct list_head orph_new;
45531 + struct ubifs_orphan *orph_cnext;
45532 + struct ubifs_orphan *orph_dnext;
45533 + spinlock_t orphan_lock;
45534 + void *orph_buf;
45535 + int new_orphans;
45536 + int cmt_orphans;
45537 + int tot_orphans;
45538 + int max_orphans;
45539 + int ohead_lnum;
45540 + int ohead_offs;
45541 + int no_orphs;
45542 +
45543 + struct task_struct *bgt;
45544 + char bgt_name[sizeof(BGT_NAME_PATTERN) + 9];
45545 + int need_bgt;
45546 + int need_wbuf_sync;
45547 +
45548 + int gc_lnum;
45549 + void *sbuf;
45550 + struct list_head idx_gc;
45551 + int idx_gc_cnt;
45552 +
45553 + struct list_head infos_list;
45554 + struct mutex umount_mutex;
45555 + unsigned int shrinker_run_no;
45556 +
45557 + int space_bits;
45558 + int lpt_lnum_bits;
45559 + int lpt_offs_bits;
45560 + int lpt_spc_bits;
45561 + int pcnt_bits;
45562 + int lnum_bits;
45563 + int nnode_sz;
45564 + int pnode_sz;
45565 + int ltab_sz;
45566 + int lsave_sz;
45567 + int pnode_cnt;
45568 + int nnode_cnt;
45569 + int lpt_hght;
45570 + int pnodes_have;
45571 +
45572 + struct mutex lp_mutex;
45573 + int lpt_lnum;
45574 + int lpt_offs;
45575 + int nhead_lnum;
45576 + int nhead_offs;
45577 + int lpt_drty_flgs;
45578 + int dirty_nn_cnt;
45579 + int dirty_pn_cnt;
45580 + long long lpt_sz;
45581 + void *lpt_nod_buf;
45582 + void *lpt_buf;
45583 + struct ubifs_nnode *nroot;
45584 + struct ubifs_cnode *lpt_cnext;
45585 + struct ubifs_lpt_heap lpt_heap[LPROPS_HEAP_CNT];
45586 + struct ubifs_lpt_heap dirty_idx;
45587 + struct list_head uncat_list;
45588 + struct list_head empty_list;
45589 + struct list_head freeable_list;
45590 + struct list_head frdi_idx_list;
45591 + int freeable_cnt;
45592 +
45593 + int ltab_lnum;
45594 + int ltab_offs;
45595 + struct ubifs_lpt_lprops *ltab;
45596 + struct ubifs_lpt_lprops *ltab_cmt;
45597 + int lsave_cnt;
45598 + int lsave_lnum;
45599 + int lsave_offs;
45600 + int *lsave;
45601 + int lscan_lnum;
45602 +
45603 + long long rp_size;
45604 + long long report_rp_size;
45605 + uid_t rp_uid;
45606 + gid_t rp_gid;
45607 +
45608 + /* The below fields are used only during mounting and re-mounting */
45609 + int empty;
45610 + struct rb_root replay_tree;
45611 + struct list_head replay_list;
45612 + struct list_head replay_buds;
45613 + unsigned long long cs_sqnum;
45614 + unsigned long long replay_sqnum;
45615 + int need_recovery;
45616 + int replaying;
45617 + struct list_head unclean_leb_list;
45618 + struct ubifs_mst_node *rcvrd_mst_node;
45619 + struct rb_root size_tree;
45620 + int remounting_rw;
45621 + struct ubifs_mount_opts mount_opts;
45622 +
45623 +#ifdef CONFIG_UBIFS_FS_DEBUG
45624 + void *dbg_buf;
45625 + struct ubifs_zbranch old_zroot;
45626 + int old_zroot_level;
45627 + unsigned long long old_zroot_sqnum;
45628 + int failure_mode;
45629 + int fail_delay;
45630 + unsigned long fail_timeout;
45631 + unsigned int fail_cnt;
45632 + unsigned int fail_cnt_max;
45633 +#endif
45634 +};
45635 +
45636 +extern struct list_head ubifs_infos;
45637 +extern spinlock_t ubifs_infos_lock;
45638 +extern atomic_long_t ubifs_clean_zn_cnt;
45639 +extern struct kmem_cache *ubifs_inode_slab;
45640 +extern struct super_operations ubifs_super_operations;
45641 +extern struct address_space_operations ubifs_file_address_operations;
45642 +extern struct file_operations ubifs_file_operations;
45643 +extern struct inode_operations ubifs_file_inode_operations;
45644 +extern struct file_operations ubifs_dir_operations;
45645 +extern struct inode_operations ubifs_dir_inode_operations;
45646 +extern struct inode_operations ubifs_symlink_inode_operations;
45647 +extern struct backing_dev_info ubifs_backing_dev_info;
45648 +extern struct ubifs_compressor *ubifs_compressors[UBIFS_COMPR_TYPES_CNT];
45649 +
45650 +/* io.c */
45651 +int ubifs_wbuf_write_nolock(struct ubifs_wbuf *wbuf, void *buf, int len);
45652 +int ubifs_wbuf_seek_nolock(struct ubifs_wbuf *wbuf, int lnum, int offs,
45653 + int dtype);
45654 +int ubifs_wbuf_init(struct ubifs_info *c, struct ubifs_wbuf *wbuf);
45655 +int ubifs_read_node(const struct ubifs_info *c, void *buf, int type, int len,
45656 + int lnum, int offs);
45657 +int ubifs_read_node_wbuf(struct ubifs_wbuf *wbuf, void *buf, int type, int len,
45658 + int lnum, int offs);
45659 +int ubifs_write_node(struct ubifs_info *c, void *node, int len, int lnum,
45660 + int offs, int dtype);
45661 +int ubifs_check_node(const struct ubifs_info *c, const void *buf, int lnum,
45662 + int offs, int quiet);
45663 +void ubifs_prepare_node(struct ubifs_info *c, void *buf, int len, int pad);
45664 +void ubifs_prep_grp_node(struct ubifs_info *c, void *node, int len, int last);
45665 +int ubifs_io_init(struct ubifs_info *c);
45666 +void ubifs_pad(const struct ubifs_info *c, void *buf, int pad);
45667 +int ubifs_wbuf_sync_nolock(struct ubifs_wbuf *wbuf);
45668 +int ubifs_bg_wbufs_sync(struct ubifs_info *c);
45669 +void ubifs_wbuf_add_ino_nolock(struct ubifs_wbuf *wbuf, ino_t inum);
45670 +int ubifs_sync_wbufs_by_inodes(struct ubifs_info *c,
45671 + struct inode * const *inodes, int count);
45672 +
45673 +/* scan.c */
45674 +struct ubifs_scan_leb *ubifs_scan(const struct ubifs_info *c, int lnum,
45675 + int offs, void *sbuf);
45676 +void ubifs_scan_destroy(struct ubifs_scan_leb *sleb);
45677 +int ubifs_scan_a_node(const struct ubifs_info *c, void *buf, int len, int lnum,
45678 + int offs, int quiet);
45679 +struct ubifs_scan_leb *ubifs_start_scan(const struct ubifs_info *c, int lnum,
45680 + int offs, void *sbuf);
45681 +void ubifs_end_scan(const struct ubifs_info *c, struct ubifs_scan_leb *sleb,
45682 + int lnum, int offs);
45683 +int ubifs_add_snod(const struct ubifs_info *c, struct ubifs_scan_leb *sleb,
45684 + void *buf, int offs);
45685 +void ubifs_scanned_corruption(const struct ubifs_info *c, int lnum, int offs,
45686 + void *buf);
45687 +
45688 +/* log.c */
45689 +void ubifs_add_bud(struct ubifs_info *c, struct ubifs_bud *bud);
45690 +void ubifs_create_buds_lists(struct ubifs_info *c);
45691 +int ubifs_add_bud_to_log(struct ubifs_info *c, int jhead, int lnum, int offs);
45692 +struct ubifs_bud *ubifs_search_bud(struct ubifs_info *c, int lnum);
45693 +struct ubifs_wbuf *ubifs_get_wbuf(struct ubifs_info *c, int lnum);
45694 +int ubifs_log_start_commit(struct ubifs_info *c, int *ltail_lnum);
45695 +int ubifs_log_end_commit(struct ubifs_info *c, int new_ltail_lnum);
45696 +int ubifs_log_post_commit(struct ubifs_info *c, int old_ltail_lnum);
45697 +int ubifs_consolidate_log(struct ubifs_info *c);
45698 +
45699 +/* journal.c */
45700 +int ubifs_jnl_update(struct ubifs_info *c, const struct inode *dir,
45701 + const struct qstr *nm, const struct inode *inode,
45702 + int deletion, int sync, int xent);
45703 +int ubifs_jnl_write_data(struct ubifs_info *c, const struct inode *inode,
45704 + const union ubifs_key *key, const void *buf, int len);
45705 +int ubifs_jnl_write_inode(struct ubifs_info *c, const struct inode *inode,
45706 + int last_reference, int sync);
45707 +int ubifs_jnl_rename(struct ubifs_info *c, const struct inode *old_dir,
45708 + const struct dentry *old_dentry,
45709 + const struct inode *new_dir,
45710 + const struct dentry *new_dentry, int sync);
45711 +int ubifs_jnl_truncate(struct ubifs_info *c, ino_t inum,
45712 + loff_t old_size, loff_t new_size);
45713 +int ubifs_jnl_delete_xattr(struct ubifs_info *c, const struct inode *host,
45714 + const struct inode *inode, const struct qstr *nm,
45715 + int sync);
45716 +int ubifs_jnl_write_2_inodes(struct ubifs_info *c, const struct inode *inode1,
45717 + const struct inode *inode2, int sync);
45718 +
45719 +/* budget.c */
45720 +int ubifs_budget_space(struct ubifs_info *c, struct ubifs_budget_req *req);
45721 +void ubifs_release_budget(struct ubifs_info *c, struct ubifs_budget_req *req);
45722 +int ubifs_budget_inode_op(struct ubifs_info *c, struct inode *inode,
45723 + struct ubifs_budget_req *req);
45724 +void ubifs_release_ino_dirty(struct ubifs_info *c, struct inode *inode,
45725 + struct ubifs_budget_req *req);
45726 +void ubifs_cancel_ino_op(struct ubifs_info *c, struct inode *inode,
45727 + struct ubifs_budget_req *req);
45728 +int ubifs_budget_ino_cleaning(struct ubifs_info *c, struct inode *inode,
45729 + struct ubifs_budget_req *req);
45730 +void ubifs_release_ino_clean(struct ubifs_info *c, struct inode *inode,
45731 + struct ubifs_budget_req *req);
45732 +long long ubifs_budg_get_free_space(struct ubifs_info *c);
45733 +int ubifs_calc_min_idx_lebs(struct ubifs_info *c);
45734 +void ubifs_convert_page_budget(struct ubifs_info *c);
45735 +void ubifs_release_new_page_budget(struct ubifs_info *c);
45736 +long long ubifs_calc_available(const struct ubifs_info *c);
45737 +
45738 +/* find.c */
45739 +int ubifs_find_free_space(struct ubifs_info *c, int min_space, int *free,
45740 + int squeeze);
45741 +int ubifs_find_free_leb_for_idx(struct ubifs_info *c);
45742 +int ubifs_find_dirty_leb(struct ubifs_info *c, struct ubifs_lprops *ret_lp,
45743 + int min_space, int pick_free);
45744 +int ubifs_find_dirty_idx_leb(struct ubifs_info *c);
45745 +int ubifs_save_dirty_idx_lnums(struct ubifs_info *c);
45746 +
45747 +/* tnc.c */
45748 +int ubifs_lookup_level0(struct ubifs_info *c, const union ubifs_key *key,
45749 + struct ubifs_znode **zn, int *n);
45750 +int ubifs_tnc_lookup(struct ubifs_info *c, const union ubifs_key *key,
45751 + void *node);
45752 +int ubifs_tnc_lookup_nm(struct ubifs_info *c, const union ubifs_key *key,
45753 + void *node, const struct qstr *nm);
45754 +int ubifs_tnc_locate(struct ubifs_info *c, const union ubifs_key *key,
45755 + void *node, int *lnum, int *offs);
45756 +int ubifs_tnc_add(struct ubifs_info *c, const union ubifs_key *key, int lnum,
45757 + int offs, int len);
45758 +int ubifs_tnc_replace(struct ubifs_info *c, const union ubifs_key *key,
45759 + int old_lnum, int old_offs, int lnum, int offs, int len);
45760 +int ubifs_tnc_add_nm(struct ubifs_info *c, const union ubifs_key *key,
45761 + int lnum, int offs, int len, const struct qstr *nm);
45762 +int ubifs_tnc_remove(struct ubifs_info *c, const union ubifs_key *key);
45763 +int ubifs_tnc_remove_nm(struct ubifs_info *c, const union ubifs_key *key,
45764 + const struct qstr *nm);
45765 +int ubifs_tnc_remove_range(struct ubifs_info *c, union ubifs_key *from_key,
45766 + union ubifs_key *to_key);
45767 +int ubifs_tnc_remove_ino(struct ubifs_info *c, ino_t inum);
45768 +struct ubifs_dent_node *ubifs_tnc_next_ent(struct ubifs_info *c,
45769 + union ubifs_key *key,
45770 + const struct qstr *nm);
45771 +void ubifs_tnc_close(struct ubifs_info *c);
45772 +int ubifs_tnc_has_node(struct ubifs_info *c, union ubifs_key *key, int level,
45773 + int lnum, int offs, int is_idx);
45774 +int ubifs_dirty_idx_node(struct ubifs_info *c, union ubifs_key *key, int level,
45775 + int lnum, int offs);
45776 +/* Shared by tnc.c for tnc_commit.c */
45777 +void destroy_old_idx(struct ubifs_info *c);
45778 +int is_idx_node_in_tnc(struct ubifs_info *c, union ubifs_key *key, int level,
45779 + int lnum, int offs);
45780 +int insert_old_idx_znode(struct ubifs_info *c, struct ubifs_znode *znode);
45781 +
45782 +/* tnc_misc.c */
45783 +struct ubifs_znode *ubifs_tnc_levelorder_next(struct ubifs_znode *zr,
45784 + struct ubifs_znode *znode);
45785 +int ubifs_search_zbranch(const struct ubifs_info *c,
45786 + const struct ubifs_znode *znode,
45787 + const union ubifs_key *key, int *n);
45788 +struct ubifs_znode *ubifs_tnc_postorder_first(struct ubifs_znode *znode);
45789 +struct ubifs_znode *ubifs_tnc_postorder_next(struct ubifs_znode *znode);
45790 +long ubifs_destroy_tnc_subtree(struct ubifs_znode *zr);
45791 +struct ubifs_znode *ubifs_load_znode(struct ubifs_info *c,
45792 + struct ubifs_zbranch *zbr,
45793 + struct ubifs_znode *parent, int iip);
45794 +int ubifs_tnc_read_node(struct ubifs_info *c, struct ubifs_zbranch *zbr,
45795 + void *node);
45796 +
45797 +/* tnc_commit.c */
45798 +int ubifs_tnc_start_commit(struct ubifs_info *c, struct ubifs_zbranch *zroot);
45799 +int ubifs_tnc_end_commit(struct ubifs_info *c);
45800 +
45801 +/* shrinker.c */
45802 +int ubifs_shrinker(int nr_to_scan, gfp_t gfp_mask);
45803 +
45804 +/* commit.c */
45805 +int ubifs_bg_thread(void *info);
45806 +void ubifs_commit_required(struct ubifs_info *c);
45807 +void ubifs_request_bg_commit(struct ubifs_info *c);
45808 +int ubifs_run_commit(struct ubifs_info *c);
45809 +void ubifs_recovery_commit(struct ubifs_info *c);
45810 +int ubifs_gc_should_commit(struct ubifs_info *c);
45811 +void ubifs_wait_for_commit(struct ubifs_info *c);
45812 +
45813 +/* master.c */
45814 +int ubifs_read_master(struct ubifs_info *c);
45815 +int ubifs_write_master(struct ubifs_info *c);
45816 +
45817 +/* sb.c */
45818 +int ubifs_read_superblock(struct ubifs_info *c);
45819 +struct ubifs_sb_node *ubifs_read_sb_node(struct ubifs_info *c);
45820 +int ubifs_write_sb_node(struct ubifs_info *c, struct ubifs_sb_node *sup);
45821 +
45822 +/* replay.c */
45823 +int ubifs_validate_entry(struct ubifs_info *c,
45824 + const struct ubifs_dent_node *dent);
45825 +int ubifs_replay_journal(struct ubifs_info *c);
45826 +
45827 +/* gc.c */
45828 +int ubifs_garbage_collect(struct ubifs_info *c, int anyway);
45829 +int ubifs_gc_start_commit(struct ubifs_info *c);
45830 +int ubifs_gc_end_commit(struct ubifs_info *c);
45831 +void ubifs_destroy_idx_gc(struct ubifs_info *c);
45832 +int ubifs_get_idx_gc_leb(struct ubifs_info *c);
45833 +int ubifs_garbage_collect_leb(struct ubifs_info *c, struct ubifs_lprops *lp);
45834 +
45835 +/* orphan.c */
45836 +int ubifs_add_orphan(struct ubifs_info *c, ino_t inum);
45837 +void ubifs_delete_orphan(struct ubifs_info *c, ino_t inum);
45838 +int ubifs_orphan_start_commit(struct ubifs_info *c);
45839 +int ubifs_orphan_end_commit(struct ubifs_info *c);
45840 +int ubifs_mount_orphans(struct ubifs_info *c, int unclean, int read_only);
45841 +
45842 +/* lpt.c */
45843 +int ubifs_calc_lpt_geom(struct ubifs_info *c);
45844 +int ubifs_create_dflt_lpt(struct ubifs_info *c, int *main_lebs, int lpt_first,
45845 + int *lpt_lebs, int *big_lpt);
45846 +int ubifs_lpt_init(struct ubifs_info *c, int rd, int wr);
45847 +struct ubifs_lprops *ubifs_lpt_lookup(struct ubifs_info *c, int lnum);
45848 +struct ubifs_lprops *ubifs_lpt_lookup_dirty(struct ubifs_info *c, int lnum);
45849 +int ubifs_lpt_scan_nolock(struct ubifs_info *c, int start_lnum, int end_lnum,
45850 + ubifs_lpt_scan_callback scan_cb, void *data);
45851 +
45852 +/* Shared by lpt.c for lpt_commit.c */
45853 +void ubifs_pack_lsave(struct ubifs_info *c, void *buf, int *lsave);
45854 +void ubifs_pack_ltab(struct ubifs_info *c, void *buf,
45855 + struct ubifs_lpt_lprops *ltab);
45856 +void ubifs_pack_pnode(struct ubifs_info *c, void *buf,
45857 + struct ubifs_pnode *pnode);
45858 +void ubifs_pack_nnode(struct ubifs_info *c, void *buf,
45859 + struct ubifs_nnode *nnode);
45860 +struct ubifs_pnode *ubifs_get_pnode(struct ubifs_info *c,
45861 + struct ubifs_nnode *parent, int iip);
45862 +struct ubifs_nnode *ubifs_get_nnode(struct ubifs_info *c,
45863 + struct ubifs_nnode *parent, int iip);
45864 +int ubifs_read_nnode(struct ubifs_info *c, struct ubifs_nnode *parent, int iip);
45865 +void ubifs_add_lpt_dirt(struct ubifs_info *c, int lnum, int dirty);
45866 +void ubifs_add_nnode_dirt(struct ubifs_info *c, struct ubifs_nnode *nnode);
45867 +uint32_t ubifs_unpack_bits(uint8_t **addr, int *pos, int nrbits);
45868 +struct ubifs_nnode *ubifs_first_nnode(struct ubifs_info *c, int *hght);
45869 +
45870 +/* lpt_commit.c */
45871 +int ubifs_lpt_start_commit(struct ubifs_info *c);
45872 +int ubifs_lpt_end_commit(struct ubifs_info *c);
45873 +int ubifs_lpt_post_commit(struct ubifs_info *c);
45874 +void ubifs_lpt_free(struct ubifs_info *c, int wr_only);
45875 +
45876 +/* lprops.c */
45877 +void ubifs_get_lprops(struct ubifs_info *c);
45878 +const struct ubifs_lprops *ubifs_change_lp(struct ubifs_info *c,
45879 + const struct ubifs_lprops *lp,
45880 + int free, int dirty, int flags,
45881 + int idx_gc_cnt);
45882 +void ubifs_release_lprops(struct ubifs_info *c);
45883 +void ubifs_get_lp_stats(struct ubifs_info *c, struct ubifs_lp_stats *stats);
45884 +void ubifs_add_to_cat(struct ubifs_info *c, struct ubifs_lprops *lprops,
45885 + int cat);
45886 +void ubifs_replace_cat(struct ubifs_info *c, struct ubifs_lprops *old_lprops,
45887 + struct ubifs_lprops *new_lprops);
45888 +void ubifs_ensure_cat(struct ubifs_info *c, struct ubifs_lprops *lprops);
45889 +int ubifs_categorize_lprops(const struct ubifs_info *c,
45890 + const struct ubifs_lprops *lprops);
45891 +int ubifs_change_one_lp(struct ubifs_info *c, int lnum, int free, int dirty,
45892 + int flags_set, int flags_clean, int idx_gc_cnt);
45893 +int ubifs_update_one_lp(struct ubifs_info *c, int lnum, int free, int dirty,
45894 + int flags_set, int flags_clean);
45895 +int ubifs_read_one_lp(struct ubifs_info *c, int lnum, struct ubifs_lprops *lp);
45896 +const struct ubifs_lprops *ubifs_fast_find_free(struct ubifs_info *c);
45897 +const struct ubifs_lprops *ubifs_fast_find_empty(struct ubifs_info *c);
45898 +const struct ubifs_lprops *ubifs_fast_find_freeable(struct ubifs_info *c);
45899 +const struct ubifs_lprops *ubifs_fast_find_frdi_idx(struct ubifs_info *c);
45900 +
45901 +/* file.c */
45902 +int ubifs_fsync(struct file *file, struct dentry *dentry, int datasync);
45903 +int ubifs_setattr(struct dentry *dentry, struct iattr *attr);
45904 +
45905 +/* dir.c */
45906 +struct inode *ubifs_new_inode(struct ubifs_info *c, const struct inode *dir,
45907 + int mode);
45908 +int ubifs_getattr(struct vfsmount *mnt, struct dentry *dentry,
45909 + struct kstat *stat);
45910 +
45911 +/* xattr.c */
45912 +int ubifs_setxattr(struct dentry *dentry, const char *name,
45913 + const void *value, size_t size, int flags);
45914 +ssize_t ubifs_getxattr(struct dentry *dentry, const char *name, void *buf,
45915 + size_t size);
45916 +ssize_t ubifs_listxattr(struct dentry *dentry, char *buffer, size_t size);
45917 +int ubifs_removexattr(struct dentry *dentry, const char *name);
45918 +
45919 +/* super.c */
45920 +struct inode *ubifs_iget(struct super_block *sb, unsigned long inum);
45921 +
45922 +/* recovery.c */
45923 +int ubifs_recover_master_node(struct ubifs_info *c);
45924 +int ubifs_write_rcvrd_mst_node(struct ubifs_info *c);
45925 +struct ubifs_scan_leb *ubifs_recover_leb(struct ubifs_info *c, int lnum,
45926 + int offs, void *sbuf, int grouped);
45927 +struct ubifs_scan_leb *ubifs_recover_log_leb(struct ubifs_info *c, int lnum,
45928 + int offs, void *sbuf);
45929 +int ubifs_recover_inl_heads(const struct ubifs_info *c, void *sbuf);
45930 +int ubifs_clean_lebs(const struct ubifs_info *c, void *sbuf);
45931 +int ubifs_rcvry_gc_commit(struct ubifs_info *c);
45932 +int ubifs_recover_size_accum(struct ubifs_info *c, union ubifs_key *key,
45933 + int deletion, loff_t new_size);
45934 +int ubifs_recover_size(struct ubifs_info *c);
45935 +void ubifs_destroy_size_tree(struct ubifs_info *c);
45936 +
45937 +/* ioctl.c */
45938 +long ubifs_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
45939 +void ubifs_set_inode_flags(struct inode *inode);
45940 +#ifdef CONFIG_COMPAT
45941 +long ubifs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
45942 +#endif
45943 +
45944 +/* compressor.c */
45945 +int __init ubifs_compressors_init(void);
45946 +void __exit ubifs_compressors_exit(void);
45947 +void ubifs_compress(const void *in_buf, int in_len, void *out_buf, int *out_len,
45948 + int *compr_type);
45949 +int ubifs_decompress(const void *buf, int len, void *out, int *out_len,
45950 + int compr_type);
45951 +
45952 +#include "debug.h"
45953 +#include "misc.h"
45954 +#include "key.h"
45955 +
45956 +#endif /* !__UBIFS_H__ */
45957 diff --exclude=.git -urN linux-2.6.25.6/fs/ubifs/ubifs-media.h avr32-2.6/fs/ubifs/ubifs-media.h
45958 --- linux-2.6.25.6/fs/ubifs/ubifs-media.h 1970-01-01 01:00:00.000000000 +0100
45959 +++ avr32-2.6/fs/ubifs/ubifs-media.h 2008-06-12 15:09:45.603817614 +0200
45960 @@ -0,0 +1,729 @@
45961 +/*
45962 + * This file is part of UBIFS.
45963 + *
45964 + * Copyright (C) 2006-2008 Nokia Corporation.
45965 + *
45966 + * This program is free software; you can redistribute it and/or modify it
45967 + * under the terms of the GNU General Public License version 2 as published by
45968 + * the Free Software Foundation.
45969 + *
45970 + * This program is distributed in the hope that it will be useful, but WITHOUT
45971 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
45972 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
45973 + * more details.
45974 + *
45975 + * You should have received a copy of the GNU General Public License along with
45976 + * this program; if not, write to the Free Software Foundation, Inc., 51
45977 + * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
45978 + *
45979 + * Authors: Artem Bityutskiy (Битюцкий Артём)
45980 + * Adrian Hunter
45981 + */
45982 +
45983 +/*
45984 + * This file describes UBIFS on-flash format and contains definitions of all the
45985 + * relevant data structures and constants.
45986 + *
45987 + * All UBIFS on-flash objects are stored in the form of nodes. All nodes start
45988 + * with the UBIFS node magic number and have the same common header. Nodes
45989 + * always sit at 8-byte aligned positions on the media and node header sizes are
45990 + * also 8-byte aligned (except for the indexing node and the padding node).
45991 + */
45992 +
45993 +#ifndef __UBIFS_MEDIA_H__
45994 +#define __UBIFS_MEDIA_H__
45995 +
45996 +/* UBIFS node magic number (must not have the padding byte first or last) */
45997 +#define UBIFS_NODE_MAGIC 0x06101831
45998 +
45999 +/* UBIFS on-flash format version */
46000 +#define UBIFS_FORMAT_VERSION 4
46001 +
46002 +/* Minimum logical eraseblock size in bytes */
46003 +#define UBIFS_MIN_LEB_SZ (15*1024)
46004 +
46005 +/* Initial CRC32 value used when calculating CRC checksums */
46006 +#define UBIFS_CRC32_INIT 0xFFFFFFFFU
46007 +
46008 +/*
46009 + * UBIFS does not try to compress data if its length is less than the below
46010 + * constant.
46011 + */
46012 +#define UBIFS_MIN_COMPR_LEN 128
46013 +
46014 +/* Root inode number */
46015 +#define UBIFS_ROOT_INO 1
46016 +
46017 +/* Lowest inode number used for regular inodes (not UBIFS-only internal ones) */
46018 +#define UBIFS_FIRST_INO 64
46019 +
46020 +/*
46021 + * Maximum file name and extended attribute length (must be a multiple of 8,
46022 + * minus 1).
46023 + */
46024 +#define UBIFS_MAX_NLEN 255
46025 +
46026 +/* Maximum number of data journal heads */
46027 +#define UBIFS_MAX_JHEADS 1
46028 +
46029 +/*
46030 + * Size of UBIFS data block. Note, UBIFS is not a block oriented file-system,
46031 + * which means that it does not treat the underlying media as consisting of
46032 + * blocks like in case of hard drives. Do not be confused. UBIFS block is just
46033 + * the maximum amount of data which one data node can have or which can be
46034 + * attached to an inode node.
46035 + */
46036 +#define UBIFS_BLOCK_SIZE 4096
46037 +#define UBIFS_BLOCK_SHIFT 12
46038 +#define UBIFS_BLOCK_MASK 0x00000FFF
46039 +
46040 +/* UBIFS padding byte pattern (must not be first or last byte of node magic) */
46041 +#define UBIFS_PADDING_BYTE 0xCE
46042 +
46043 +/* Maximum possible key length */
46044 +#define UBIFS_MAX_KEY_LEN 16
46045 +
46046 +/* Key length ("simple" format) */
46047 +#define UBIFS_SK_LEN 8
46048 +
46049 +/* Minimum index tree fanout */
46050 +#define UBIFS_MIN_FANOUT 2
46051 +
46052 +/* Maximum number of levels in UBIFS indexing B-tree */
46053 +#define UBIFS_MAX_LEVELS 512
46054 +
46055 +/* Maximum amount of data attached to an inode in bytes */
46056 +#define UBIFS_MAX_INO_DATA UBIFS_BLOCK_SIZE
46057 +
46058 +/* LEB Properties Tree fanout (must be power of 2) and fanout shift */
46059 +#define UBIFS_LPT_FANOUT 4
46060 +#define UBIFS_LPT_FANOUT_SHIFT 2
46061 +
46062 +/* LEB Properties Tree bit field sizes */
46063 +#define UBIFS_LPT_CRC_BITS 16
46064 +#define UBIFS_LPT_CRC_BYTES 2
46065 +#define UBIFS_LPT_TYPE_BITS 4
46066 +
46067 +/* The key is always at the same position in all keyed nodes */
46068 +#define UBIFS_KEY_OFFSET offsetof(struct ubifs_ino_node, key)
46069 +
46070 +/*
46071 + * LEB Properties Tree node types.
46072 + *
46073 + * UBIFS_LPT_PNODE: LPT leaf node (contains LEB properties)
46074 + * UBIFS_LPT_NNODE: LPT internal node
46075 + * UBIFS_LPT_LTAB: LPT's own lprops table
46076 + * UBIFS_LPT_LSAVE: LPT's save table (big model only)
46077 + * UBIFS_LPT_NODE_CNT: count of LPT node types
46078 + * UBIFS_LPT_NOT_A_NODE: all ones (15 for 4 bits) is never a valid node type
46079 + */
46080 +enum {
46081 + UBIFS_LPT_PNODE,
46082 + UBIFS_LPT_NNODE,
46083 + UBIFS_LPT_LTAB,
46084 + UBIFS_LPT_LSAVE,
46085 + UBIFS_LPT_NODE_CNT,
46086 + UBIFS_LPT_NOT_A_NODE = (1 << UBIFS_LPT_TYPE_BITS) - 1,
46087 +};
46088 +
46089 +/*
46090 + * UBIFS inode types.
46091 + *
46092 + * UBIFS_ITYPE_REG: regular file
46093 + * UBIFS_ITYPE_DIR: directory
46094 + * UBIFS_ITYPE_LNK: soft link
46095 + * UBIFS_ITYPE_BLK: block device node
46096 + * UBIFS_ITYPE_CHR: character device node
46097 + * UBIFS_ITYPE_FIFO: fifo
46098 + * UBIFS_ITYPE_SOCK: socket
46099 + * UBIFS_ITYPES_CNT: count of supported file types
46100 + */
46101 +enum {
46102 + UBIFS_ITYPE_REG,
46103 + UBIFS_ITYPE_DIR,
46104 + UBIFS_ITYPE_LNK,
46105 + UBIFS_ITYPE_BLK,
46106 + UBIFS_ITYPE_CHR,
46107 + UBIFS_ITYPE_FIFO,
46108 + UBIFS_ITYPE_SOCK,
46109 + UBIFS_ITYPES_CNT,
46110 +};
46111 +
46112 +/*
46113 + * Supported key hash functions.
46114 + *
46115 + * UBIFS_KEY_HASH_R5: R5 hash
46116 + * UBIFS_KEY_HASH_TEST: test hash which just returns first 4 bytes of the name
46117 + */
46118 +enum {
46119 + UBIFS_KEY_HASH_R5,
46120 + UBIFS_KEY_HASH_TEST,
46121 +};
46122 +
46123 +/*
46124 + * Supported key formats.
46125 + *
46126 + * UBIFS_SIMPLE_KEY_FMT: simple key format
46127 + */
46128 +enum {
46129 + UBIFS_SIMPLE_KEY_FMT,
46130 +};
46131 +
46132 +/*
46133 + * The simple key format uses 29 bits for storing UBIFS block number and hash
46134 + * value.
46135 + */
46136 +#define UBIFS_S_KEY_BLOCK_BITS 29
46137 +#define UBIFS_S_KEY_BLOCK_MASK 0x1FFFFFFF
46138 +#define UBIFS_S_KEY_HASH_BITS UBIFS_S_KEY_BLOCK_BITS
46139 +#define UBIFS_S_KEY_HASH_MASK UBIFS_S_KEY_BLOCK_MASK
46140 +
46141 +/*
46142 + * Key types.
46143 + *
46144 + * UBIFS_INO_KEY: inode node key
46145 + * UBIFS_DATA_KEY: data node key
46146 + * UBIFS_DENT_KEY: directory entry node key
46147 + * UBIFS_XENT_KEY: extended attribute entry key
46148 + * UBIFS_KEY_TYPES_CNT: number of supported key types
46149 + */
46150 +enum {
46151 + UBIFS_INO_KEY,
46152 + UBIFS_DATA_KEY,
46153 + UBIFS_DENT_KEY,
46154 + UBIFS_XENT_KEY,
46155 + UBIFS_KEY_TYPES_CNT,
46156 +};
46157 +
46158 +/* Count of LEBs reserved for the superblock area */
46159 +#define UBIFS_SB_LEBS 1
46160 +/* Count of LEBs reserved for the master area */
46161 +#define UBIFS_MST_LEBS 2
46162 +
46163 +/* First LEB of the superblock area */
46164 +#define UBIFS_SB_LNUM 0
46165 +/* First LEB of the master area */
46166 +#define UBIFS_MST_LNUM (UBIFS_SB_LNUM + UBIFS_SB_LEBS)
46167 +/* First LEB of the log area */
46168 +#define UBIFS_LOG_LNUM (UBIFS_MST_LNUM + UBIFS_MST_LEBS)
46169 +
46170 +/* Minimum number of logical eraseblocks in the log */
46171 +#define UBIFS_MIN_LOG_LEBS 2
46172 +/* Minimum number of bud logical eraseblocks */
46173 +#define UBIFS_MIN_BUD_LEBS 2
46174 +/* Minimum number of journal logical eraseblocks */
46175 +#define UBIFS_MIN_JNL_LEBS (UBIFS_MIN_LOG_LEBS + UBIFS_MIN_BUD_LEBS)
46176 +/* Minimum number of LPT area logical eraseblocks */
46177 +#define UBIFS_MIN_LPT_LEBS 2
46178 +/* Minimum number of orphan area logical eraseblocks */
46179 +#define UBIFS_MIN_ORPH_LEBS 1
46180 +/* Minimum number of main area logical eraseblocks */
46181 +#define UBIFS_MIN_MAIN_LEBS 8
46182 +
46183 +/* Minimum number of logical eraseblocks */
46184 +#define UBIFS_MIN_LEB_CNT (UBIFS_SB_LEBS + UBIFS_MST_LEBS + \
46185 + UBIFS_MIN_LOG_LEBS + UBIFS_MIN_BUD_LEBS + \
46186 + UBIFS_MIN_LPT_LEBS + UBIFS_MIN_ORPH_LEBS + \
46187 + UBIFS_MIN_MAIN_LEBS)
46188 +
46189 +/* Node sizes (N.B. these are guaranteed to be multiples of 8) */
46190 +#define UBIFS_CH_SZ sizeof(struct ubifs_ch)
46191 +#define UBIFS_INO_NODE_SZ sizeof(struct ubifs_ino_node)
46192 +#define UBIFS_DATA_NODE_SZ sizeof(struct ubifs_data_node)
46193 +#define UBIFS_DENT_NODE_SZ sizeof(struct ubifs_dent_node)
46194 +#define UBIFS_TRUN_NODE_SZ sizeof(struct ubifs_trun_node)
46195 +#define UBIFS_PAD_NODE_SZ sizeof(struct ubifs_pad_node)
46196 +#define UBIFS_SB_NODE_SZ sizeof(struct ubifs_sb_node)
46197 +#define UBIFS_MST_NODE_SZ sizeof(struct ubifs_mst_node)
46198 +#define UBIFS_REF_NODE_SZ sizeof(struct ubifs_ref_node)
46199 +#define UBIFS_IDX_NODE_SZ sizeof(struct ubifs_idx_node)
46200 +#define UBIFS_CS_NODE_SZ sizeof(struct ubifs_cs_node)
46201 +#define UBIFS_ORPH_NODE_SZ sizeof(struct ubifs_orph_node)
46202 +/* Extended attribute entry nodes are identical to directory entry nodes */
46203 +#define UBIFS_XENT_NODE_SZ UBIFS_DENT_NODE_SZ
46204 +/* Only this does not have to be multiple of 8 bytes */
46205 +#define UBIFS_BRANCH_SZ sizeof(struct ubifs_branch)
46206 +
46207 +/* Maximum node sizes (N.B. these are guaranteed to be multiples of 8) */
46208 +#define UBIFS_MAX_DATA_NODE_SZ (UBIFS_DATA_NODE_SZ + UBIFS_BLOCK_SIZE)
46209 +#define UBIFS_MAX_INO_NODE_SZ (UBIFS_INO_NODE_SZ + UBIFS_MAX_INO_DATA)
46210 +#define UBIFS_MAX_DENT_NODE_SZ (UBIFS_DENT_NODE_SZ + UBIFS_MAX_NLEN + 1)
46211 +#define UBIFS_MAX_XENT_NODE_SZ UBIFS_MAX_DENT_NODE_SZ
46212 +
46213 +/* The largest UBIFS node */
46214 +#define UBIFS_MAX_NODE_SZ UBIFS_MAX_INO_NODE_SZ
46215 +
46216 +/*
46217 + * On-flash inode flags.
46218 + *
46219 + * UBIFS_COMPR_FL: use compression for this inode
46220 + * UBIFS_SYNC_FL: I/O on this inode has to be synchronous
46221 + * UBIFS_IMMUTABLE_FL: inode is immutable
46222 + * UBIFS_APPEND_FL: writes to the inode may only append data
46223 + * UBIFS_DIRSYNC_FL: I/O on this directory inode has to be synchronous
46224 + *
46225 + * Note, these are on-flash flags which correspond to ioctl flags
46226 + * (@FS_COMPR_FL, etc). They have the same values now, but generally, do not
46227 + * have to be the same.
46228 + */
46229 +enum {
46230 + UBIFS_COMPR_FL = 0x01,
46231 + UBIFS_SYNC_FL = 0x02,
46232 + UBIFS_IMMUTABLE_FL = 0x04,
46233 + UBIFS_APPEND_FL = 0x08,
46234 + UBIFS_DIRSYNC_FL = 0x10,
46235 +};
46236 +
46237 +/* Inode flag bits used by UBIFS */
46238 +#define UBIFS_FL_MASK 0x0000001F
46239 +
46240 +/*
46241 + * UBIFS compression types.
46242 + *
46243 + * UBIFS_COMPR_NONE: no compression
46244 + * UBIFS_COMPR_LZO: LZO compression
46245 + * UBIFS_COMPR_ZLIB: ZLIB compression
46246 + * UBIFS_COMPR_TYPES_CNT: count of supported compression types
46247 + */
46248 +enum {
46249 + UBIFS_COMPR_NONE,
46250 + UBIFS_COMPR_LZO,
46251 + UBIFS_COMPR_ZLIB,
46252 + UBIFS_COMPR_TYPES_CNT,
46253 +};
46254 +
46255 +/*
46256 + * UBIFS node types.
46257 + *
46258 + * UBIFS_INO_NODE: inode node
46259 + * UBIFS_DATA_NODE: data node
46260 + * UBIFS_DENT_NODE: directory entry node
46261 + * UBIFS_XENT_NODE: extended attribute node
46262 + * UBIFS_TRUN_NODE: truncation node
46263 + * UBIFS_PAD_NODE: padding node
46264 + * UBIFS_SB_NODE: superblock node
46265 + * UBIFS_MST_NODE: master node
46266 + * UBIFS_REF_NODE: LEB reference node
46267 + * UBIFS_IDX_NODE: index node
46268 + * UBIFS_CS_NODE: commit start node
46269 + * UBIFS_ORPH_NODE: orphan node
46270 + * UBIFS_NODE_TYPES_CNT: count of supported node types
46271 + *
46272 + * Note, we index arrays by these numbers, so keep them low and contiguous.
46273 + * Node type constants for inodes, direntries and so on have to be the same as
46274 + * corresponding key type constants.
46275 + */
46276 +enum {
46277 + UBIFS_INO_NODE,
46278 + UBIFS_DATA_NODE,
46279 + UBIFS_DENT_NODE,
46280 + UBIFS_XENT_NODE,
46281 + UBIFS_TRUN_NODE,
46282 + UBIFS_PAD_NODE,
46283 + UBIFS_SB_NODE,
46284 + UBIFS_MST_NODE,
46285 + UBIFS_REF_NODE,
46286 + UBIFS_IDX_NODE,
46287 + UBIFS_CS_NODE,
46288 + UBIFS_ORPH_NODE,
46289 + UBIFS_NODE_TYPES_CNT,
46290 +};
46291 +
46292 +/*
46293 + * Master node flags.
46294 + *
46295 + * UBIFS_MST_DIRTY: rebooted uncleanly - master node is dirty
46296 + * UBIFS_MST_NO_ORPHS: no orphan inodes present
46297 + * UBIFS_MST_RCVRY: written by recovery
46298 + */
46299 +enum {
46300 + UBIFS_MST_DIRTY = 1,
46301 + UBIFS_MST_NO_ORPHS = 2,
46302 + UBIFS_MST_RCVRY = 4,
46303 +};
46304 +
46305 +/*
46306 + * Node group type (used by recovery to recover whole group or none).
46307 + *
46308 + * UBIFS_NO_NODE_GROUP: this node is not part of a group
46309 + * UBIFS_IN_NODE_GROUP: this node is a part of a group
46310 + * UBIFS_LAST_OF_NODE_GROUP: this node is the last in a group
46311 + */
46312 +enum {
46313 + UBIFS_NO_NODE_GROUP = 0,
46314 + UBIFS_IN_NODE_GROUP,
46315 + UBIFS_LAST_OF_NODE_GROUP,
46316 +};
46317 +
46318 +/*
46319 + * Superblock flags.
46320 + *
46321 + * UBIFS_FLG_BIGLPT: if "big" LPT model is used if set
46322 + */
46323 +enum {
46324 + UBIFS_FLG_BIGLPT = 0x02,
46325 +};
46326 +
46327 +/**
46328 + * struct ubifs_ch - common header node.
46329 + * @magic: UBIFS node magic number (%UBIFS_NODE_MAGIC)
46330 + * @crc: CRC-32 checksum of the node header
46331 + * @sqnum: sequence number
46332 + * @len: full node length
46333 + * @node_type: node type
46334 + * @group_type: node group type
46335 + * @padding: reserved for future, zeroes
46336 + *
46337 + * Every UBIFS node starts with this common part. If the node has a key, the
46338 + * key always goes next.
46339 + */
46340 +struct ubifs_ch {
46341 + __le32 magic;
46342 + __le32 crc;
46343 + __le64 sqnum;
46344 + __le32 len;
46345 + __u8 node_type;
46346 + __u8 group_type;
46347 + __u8 padding[2];
46348 +} __attribute__ ((packed));
46349 +
46350 +/**
46351 + * union ubifs_dev_desc - device node descriptor.
46352 + * @new: new type device descriptor
46353 + * @huge: huge type device descriptor
46354 + *
46355 + * This data structure describes major/minor numbers of a device node. In an
46356 + * inode is a device node then its data contains an object of this type. UBIFS
46357 + * uses standard Linux "new" and "huge" device node encodings.
46358 + */
46359 +union ubifs_dev_desc {
46360 + __le32 new;
46361 + __le64 huge;
46362 +} __attribute__ ((packed));
46363 +
46364 +/**
46365 + * struct ubifs_ino_node - inode node.
46366 + * @ch: common header
46367 + * @key: node key
46368 + * @creat_sqnum: sequence number at time of creation
46369 + * @size: inode size in bytes (amount of uncompressed data)
46370 + * @atime_sec: access time seconds
46371 + * @ctime_sec: creation time seconds
46372 + * @mtime_sec: modification time seconds
46373 + * @atime_nsec: access time nanoseconds
46374 + * @ctime_nsec: creation time nanoseconds
46375 + * @mtime_nsec: modification time nanoseconds
46376 + * @nlink: number of hard links
46377 + * @uid: owner ID
46378 + * @gid: group ID
46379 + * @mode: access flags
46380 + * @flags: per-inode flags (%UBIFS_COMPR_FL, %UBIFS_SYNC_FL, etc)
46381 + * @data_len: inode data length
46382 + * @xattr_cnt: count of extended attributes this inode has
46383 + * @xattr_size: summarized size of all extended attributes in bytes
46384 + * @xattr_names: sum of lengths of all extended attribute names belonging to
46385 + * this inode
46386 + * @compr_type: compression type used for this inode
46387 + * @padding: reserved for future, zeroes
46388 + * @data: data attached to the inode
46389 + *
46390 + * Note, even though inode compression type is defined by @compr_type, some
46391 + * nodes of this inode may be compressed with different compressor - this
46392 + * happens if compression type is changed while the inode already has data
46393 + * nodes. But @compr_type will be use for further writes to the inode.
46394 + *
46395 + * Note, do not forget to amend 'zero_ino_node_unused()' function when changing
46396 + * the padding fields.
46397 + */
46398 +struct ubifs_ino_node {
46399 + struct ubifs_ch ch;
46400 + __u8 key[UBIFS_MAX_KEY_LEN];
46401 + __le64 creat_sqnum;
46402 + __le64 size;
46403 + __le64 atime_sec;
46404 + __le64 ctime_sec;
46405 + __le64 mtime_sec;
46406 + __le32 atime_nsec;
46407 + __le32 ctime_nsec;
46408 + __le32 mtime_nsec;
46409 + __le32 nlink;
46410 + __le32 uid;
46411 + __le32 gid;
46412 + __le32 mode;
46413 + __le32 flags;
46414 + __le32 data_len;
46415 + __le32 xattr_cnt;
46416 + __le64 xattr_size;
46417 + __le32 xattr_names;
46418 + __le16 compr_type;
46419 + __u8 padding[26]; /* Watch 'zero_ino_node_unused()' if changing! */
46420 + __u8 data[];
46421 +} __attribute__ ((packed));
46422 +
46423 +/**
46424 + * struct ubifs_dent_node - directory entry node.
46425 + * @ch: common header
46426 + * @key: node key
46427 + * @inum: target inode number
46428 + * @padding1: reserved for future, zeroes
46429 + * @type: type of the target inode (%UBIFS_ITYPE_REG, %UBIFS_ITYPE_DIR, etc)
46430 + * @nlen: name length
46431 + * @padding2: reserved for future, zeroes
46432 + * @name: zero-terminated name
46433 + *
46434 + * Note, do not forget to amend 'zero_dent_node_unused()' function when
46435 + * changing the padding fields.
46436 + */
46437 +struct ubifs_dent_node {
46438 + struct ubifs_ch ch;
46439 + __u8 key[UBIFS_MAX_KEY_LEN];
46440 + __le64 inum;
46441 + __u8 padding1;
46442 + __u8 type;
46443 + __le16 nlen;
46444 + __u8 padding2[4]; /* Watch 'zero_dent_node_unused()' if changing! */
46445 + __u8 name[];
46446 +} __attribute__ ((packed));
46447 +
46448 +/**
46449 + * struct ubifs_data_node - data node.
46450 + * @ch: common header
46451 + * @key: node key
46452 + * @size: uncompressed data size in bytes
46453 + * @compr_type: compression type (%UBIFS_COMPR_NONE, %UBIFS_COMPR_LZO, etc)
46454 + * @padding: reserved for future, zeroes
46455 + * @data: data
46456 + *
46457 + * Note, do not forget to amend 'zero_data_node_unused()' function when
46458 + * changing the padding fields.
46459 + */
46460 +struct ubifs_data_node {
46461 + struct ubifs_ch ch;
46462 + __u8 key[UBIFS_MAX_KEY_LEN];
46463 + __le32 size;
46464 + __le16 compr_type;
46465 + __u8 padding[2]; /* Watch 'zero_data_node_unused()' if changing! */
46466 + __u8 data[];
46467 +} __attribute__ ((packed));
46468 +
46469 +/**
46470 + * struct ubifs_trun_node - truncation node.
46471 + * @ch: common header
46472 + * @inum: truncated inode number
46473 + * @padding: reserved for future, zeroes
46474 + * @old_size: size before truncation
46475 + * @new_size: size after truncation
46476 + *
46477 + * This node exists only in the journal and never goes to the main area. Note,
46478 + * do not forget to amend 'zero_trun_node_unused()' function when changing the
46479 + * padding fields.
46480 + */
46481 +struct ubifs_trun_node {
46482 + struct ubifs_ch ch;
46483 + __le32 inum;
46484 + __u8 padding[12]; /* Watch 'zero_trun_node_unused()' if changing! */
46485 + __le64 old_size;
46486 + __le64 new_size;
46487 +} __attribute__ ((packed));
46488 +
46489 +/**
46490 + * struct ubifs_pad_node - padding node.
46491 + * @ch: common header
46492 + * @pad_len: how many bytes after this node are unused (because padded)
46493 + * @padding: reserved for future, zeroes
46494 + */
46495 +struct ubifs_pad_node {
46496 + struct ubifs_ch ch;
46497 + __le32 pad_len;
46498 +} __attribute__ ((packed));
46499 +
46500 +/**
46501 + * struct ubifs_sb_node - superblock node.
46502 + * @ch: common header
46503 + * @padding: reserved for future, zeroes
46504 + * @key_hash: type of hash function used in keys
46505 + * @key_fmt: format of the key
46506 + * @flags: file-system flags (%UBIFS_FLG_BIGLPT, etc)
46507 + * @min_io_size: minimal input/output unit size
46508 + * @leb_size: logical eraseblock size in bytes
46509 + * @leb_cnt: count of LEBs used by filesystem
46510 + * @max_leb_cnt: maximum count of LEBs used by filesystem
46511 + * @max_bud_bytes: maximum amount of data stored in buds
46512 + * @log_lebs: log size in logical eraseblocks
46513 + * @lpt_lebs: number of LEBs used for lprops table
46514 + * @orph_lebs: number of LEBs used for recording orphans
46515 + * @jhead_cnt: count of journal heads
46516 + * @fanout: tree fanout (max. number of links per indexing node)
46517 + * @lsave_cnt: number of LEB numbers in LPT's save table
46518 + * @fmt_version: UBIFS on-flash format version
46519 + * @default_compr: default compression
46520 + * @padding1: reserved for future, zeroes
46521 + * @rp_uid: reserve pool UID
46522 + * @rp_gid: reserve pool GID
46523 + * @rp_size: size of the reserved pool in bytes
46524 + * @padding2: reserved for future, zeroes
46525 + * @time_gran: time granularity in nanoseconds
46526 + * @uuid: UUID generated when the file system image was created
46527 + */
46528 +struct ubifs_sb_node {
46529 + struct ubifs_ch ch;
46530 + __u8 padding[2];
46531 + __u8 key_hash;
46532 + __u8 key_fmt;
46533 + __le32 flags;
46534 + __le32 min_io_size;
46535 + __le32 leb_size;
46536 + __le32 leb_cnt;
46537 + __le32 max_leb_cnt;
46538 + __le64 max_bud_bytes;
46539 + __le32 log_lebs;
46540 + __le32 lpt_lebs;
46541 + __le32 orph_lebs;
46542 + __le32 jhead_cnt;
46543 + __le32 fanout;
46544 + __le32 lsave_cnt;
46545 + __le32 fmt_version;
46546 + __le16 default_compr;
46547 + __u8 padding1[2];
46548 + __le32 rp_uid;
46549 + __le32 rp_gid;
46550 + __le64 rp_size;
46551 + __le32 time_gran;
46552 + __u8 uuid[16];
46553 + __u8 padding2[3972];
46554 +} __attribute__ ((packed));
46555 +
46556 +/**
46557 + * struct ubifs_mst_node - master node.
46558 + * @ch: common header
46559 + * @highest_inum: highest inode number in the committed index
46560 + * @cmt_no: commit number
46561 + * @flags: various flags (%UBIFS_MST_DIRTY, etc)
46562 + * @log_lnum: start of the log
46563 + * @root_lnum: LEB number of the root indexing node
46564 + * @root_offs: offset within @root_lnum
46565 + * @root_len: root indexing node length
46566 + * @gc_lnum: LEB reserved for garbage collection (%-1 value means the LEB was
46567 + * not reserved and should be reserved on mount)
46568 + * @ihead_lnum: LEB number of index head
46569 + * @ihead_offs: offset of index head
46570 + * @index_size: size of index on flash
46571 + * @total_free: total free space in bytes
46572 + * @total_dirty: total dirty space in bytes
46573 + * @total_used: total used space in bytes (includes only data LEBs)
46574 + * @total_dead: total dead space in bytes (includes only data LEBs)
46575 + * @total_dark: total dark space in bytes (includes only data LEBs)
46576 + * @lpt_lnum: LEB number of LPT root nnode
46577 + * @lpt_offs: offset of LPT root nnode
46578 + * @nhead_lnum: LEB number of LPT head
46579 + * @nhead_offs: offset of LPT head
46580 + * @ltab_lnum: LEB number of LPT's own lprops table
46581 + * @ltab_offs: offset of LPT's own lprops table
46582 + * @lsave_lnum: LEB number of LPT's save table (big model only)
46583 + * @lsave_offs: offset of LPT's save table (big model only)
46584 + * @lscan_lnum: LEB number of last LPT scan
46585 + * @empty_lebs: number of empty logical eraseblocks
46586 + * @idx_lebs: number of indexing logical eraseblocks
46587 + * @leb_cnt: count of LEBs used by filesystem
46588 + * @padding: reserved for future, zeroes
46589 + */
46590 +struct ubifs_mst_node {
46591 + struct ubifs_ch ch;
46592 + __le64 highest_inum;
46593 + __le64 cmt_no;
46594 + __le32 flags;
46595 + __le32 log_lnum;
46596 + __le32 root_lnum;
46597 + __le32 root_offs;
46598 + __le32 root_len;
46599 + __le32 gc_lnum;
46600 + __le32 ihead_lnum;
46601 + __le32 ihead_offs;
46602 + __le64 index_size;
46603 + __le64 total_free;
46604 + __le64 total_dirty;
46605 + __le64 total_used;
46606 + __le64 total_dead;
46607 + __le64 total_dark;
46608 + __le32 lpt_lnum;
46609 + __le32 lpt_offs;
46610 + __le32 nhead_lnum;
46611 + __le32 nhead_offs;
46612 + __le32 ltab_lnum;
46613 + __le32 ltab_offs;
46614 + __le32 lsave_lnum;
46615 + __le32 lsave_offs;
46616 + __le32 lscan_lnum;
46617 + __le32 empty_lebs;
46618 + __le32 idx_lebs;
46619 + __le32 leb_cnt;
46620 + __u8 padding[344];
46621 +} __attribute__ ((packed));
46622 +
46623 +/**
46624 + * struct ubifs_ref_node - logical eraseblock reference node.
46625 + * @ch: common header
46626 + * @lnum: the referred logical eraseblock number
46627 + * @offs: start offset in the referred LEB
46628 + * @jhead: journal head number
46629 + * @padding: reserved for future, zeroes
46630 + */
46631 +struct ubifs_ref_node {
46632 + struct ubifs_ch ch;
46633 + __le32 lnum;
46634 + __le32 offs;
46635 + __le32 jhead;
46636 + __u8 padding[28];
46637 +} __attribute__ ((packed));
46638 +
46639 +/**
46640 + * struct ubifs_branch - key/reference/length branch
46641 + * @lnum: LEB number of the target node
46642 + * @offs: offset within @lnum
46643 + * @len: target node length
46644 + * @key: key
46645 + */
46646 +struct ubifs_branch {
46647 + __le32 lnum;
46648 + __le32 offs;
46649 + __le32 len;
46650 + __u8 key[];
46651 +} __attribute__ ((packed));
46652 +
46653 +/**
46654 + * struct ubifs_idx_node - indexing node.
46655 + * @ch: common header
46656 + * @child_cnt: number of child index nodes
46657 + * @level: tree level
46658 + * @branches: LEB number / offset / length / key branches
46659 + */
46660 +struct ubifs_idx_node {
46661 + struct ubifs_ch ch;
46662 + __le16 child_cnt;
46663 + __le16 level;
46664 + __u8 branches[];
46665 +} __attribute__ ((packed));
46666 +
46667 +/**
46668 + * struct ubifs_cs_node - commit start node.
46669 + * @ch: common header
46670 + * @cmt_no: commit number
46671 + */
46672 +struct ubifs_cs_node {
46673 + struct ubifs_ch ch;
46674 + __le64 cmt_no;
46675 +} __attribute__ ((packed));
46676 +
46677 +/**
46678 + * struct ubifs_orph_node - orphan node.
46679 + * @ch: common header
46680 + * @cmt_no: commit number (also top bit is set on the last node of the commit)
46681 + * @inos: inode numbers of orphans
46682 + */
46683 +struct ubifs_orph_node {
46684 + struct ubifs_ch ch;
46685 + __le64 cmt_no;
46686 + __le64 inos[];
46687 +} __attribute__ ((packed));
46688 +
46689 +#endif /* __UBIFS_MEDIA_H__ */
46690 diff --exclude=.git -urN linux-2.6.25.6/fs/ubifs/xattr.c avr32-2.6/fs/ubifs/xattr.c
46691 --- linux-2.6.25.6/fs/ubifs/xattr.c 1970-01-01 01:00:00.000000000 +0100
46692 +++ avr32-2.6/fs/ubifs/xattr.c 2008-06-12 15:09:45.603817614 +0200
46693 @@ -0,0 +1,581 @@
46694 +/*
46695 + * This file is part of UBIFS.
46696 + *
46697 + * Copyright (C) 2006-2008 Nokia Corporation.
46698 + *
46699 + * This program is free software; you can redistribute it and/or modify it
46700 + * under the terms of the GNU General Public License version 2 as published by
46701 + * the Free Software Foundation.
46702 + *
46703 + * This program is distributed in the hope that it will be useful, but WITHOUT
46704 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
46705 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
46706 + * more details.
46707 + *
46708 + * You should have received a copy of the GNU General Public License along with
46709 + * this program; if not, write to the Free Software Foundation, Inc., 51
46710 + * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
46711 + *
46712 + * Authors: Artem Bityutskiy (Битюцкий Артём)
46713 + * Adrian Hunter
46714 + */
46715 +
46716 +/*
46717 + * This file implements UBIFS extended attributes support.
46718 + *
46719 + * Extended attributes are implemented as regular inodes with attached data,
46720 + * which limits extended attribute size to UBIFS block size (4KiB). Names of
46721 + * extended attributes are described by extended attribute entries (xentries),
46722 + * which are almost identical to directory entries, but have different key type.
46723 + *
46724 + * In other words, the situation with extended attributes is very similar to
46725 + * directories. Indeed, any inode (but of course not xattr inodes) may have a
46726 + * number of associated xentries, just like directory inodes have associated
46727 + * directory entries. Extended attribute entries store the name of the extended
46728 + * attribute, the host inode number, and the extended attribute inode number.
46729 + * Similarly, direntries store the name, the parent and the target inode
46730 + * numbers. Thus, most of the common UBIFS mechanisms may be re-used for
46731 + * extended attributes.
46732 + *
46733 + * The number of extended attributes is not limited, but there is Linux
46734 + * limitation on the maximum possible size of the list of all extended
46735 + * attributes associated with an inode (%XATTR_LIST_MAX), so UBIFS makes sure
46736 + * the sum of all extended attribute names of the inode does not exceed that
46737 + * limit.
46738 + *
46739 + * Extended attributes are synchronous, which means they are written to the
46740 + * flash media synchronously and there is no write-back for extended attribute
46741 + * inodes. The extended attribute values are not stored in compressed form on
46742 + * the media.
46743 + *
46744 + * Since extended attributes are represented by regular inodes, they are cached
46745 + * in the VFS inode cache. The xentries are cached in the LNC cache (see
46746 + * tnc.c).
46747 + *
46748 + * ACL support is not implemented.
46749 + */
46750 +
46751 +#include <linux/xattr.h>
46752 +#include <linux/posix_acl_xattr.h>
46753 +#include "ubifs.h"
46754 +
46755 +/*
46756 + * Extended attribute type constants.
46757 + *
46758 + * USER_XATTR: user extended attribute ("user.*")
46759 + * TRUSTED_XATTR: trusted extended attribute ("trusted.*)
46760 + * SECURITY_XATTR: security extended attribute ("security.*")
46761 + */
46762 +enum {
46763 + USER_XATTR,
46764 + TRUSTED_XATTR,
46765 + SECURITY_XATTR,
46766 +};
46767 +
46768 +static struct inode_operations none_inode_operations;
46769 +static struct address_space_operations none_address_operations;
46770 +static struct file_operations none_file_operations;
46771 +
46772 +/**
46773 + * create_xattr - create an extended attribute.
46774 + * @c: UBIFS file-system description object
46775 + * @host: host inode
46776 + * @nm: extended attribute name
46777 + * @value: extended attribute value
46778 + * @size: size of extended attribute value
46779 + *
46780 + * This is a helper function which creates an extended attribute of name @nm
46781 + * and value @value for inode @host. The host inode is also updated on flash
46782 + * because the ctime and extended attribute accounting data changes. This
46783 + * function returns zero in case of success and a negative error code in case
46784 + * of failure.
46785 + */
46786 +static int create_xattr(struct ubifs_info *c, struct inode *host,
46787 + const struct qstr *nm, const void *value, int size)
46788 +{
46789 + struct ubifs_inode *ui, *host_ui = ubifs_inode(host);
46790 + struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1,
46791 + .new_ino_d = size };
46792 + struct inode *inode;
46793 + int err;
46794 +
46795 + /*
46796 + * Linux limits the maximum size of the extended attribute names list
46797 + * to %XATTR_LIST_MAX. This means we should not allow creating more*
46798 + * extended attributes if the name list becomes larger. This limitation
46799 + * is artificial for UBIFS, though.
46800 + */
46801 + if (host_ui->xattr_names + host_ui->xattr_cnt +
46802 + nm->len + 1 > XATTR_LIST_MAX)
46803 + return -ENOSPC;
46804 +
46805 + err = ubifs_budget_inode_op(c, host, &req);
46806 + if (err)
46807 + return err;
46808 +
46809 + inode = ubifs_new_inode(c, host, S_IFREG | S_IRWXUGO);
46810 + if (IS_ERR(inode)) {
46811 + err = PTR_ERR(inode);
46812 + goto out_budg;
46813 + }
46814 +
46815 + /* Re-define all operations to be "nothing" */
46816 + inode->i_mapping->a_ops = &none_address_operations;
46817 + inode->i_op = &none_inode_operations;
46818 + inode->i_fop = &none_file_operations;
46819 +
46820 + inode->i_flags |= S_SYNC | S_NOATIME | S_NOCMTIME | S_NOQUOTA;
46821 + ui = ubifs_inode(inode);
46822 + ui->xattr = 1;
46823 + ui->data = kmalloc(size, GFP_NOFS);
46824 + if (!ui->data) {
46825 + err = -ENOMEM;
46826 + goto out_inode;
46827 + }
46828 +
46829 + memcpy(ui->data, value, size);
46830 + host->i_ctime = ubifs_current_time(host);
46831 + host_ui->xattr_cnt += 1;
46832 + spin_lock(&host->i_lock);
46833 + host_ui->xattr_size += CALC_DENT_SIZE(nm->len);
46834 + host_ui->xattr_size += CALC_XATTR_BYTES(size);
46835 + spin_unlock(&host->i_lock);
46836 + host_ui->xattr_names += nm->len;
46837 +
46838 + /*
46839 + * We do not use i_size_write() because nobody can race with us as we
46840 + * are holding host @host->i_mutex - every xattr operation for this
46841 + * inode is serialized by it.
46842 + */
46843 + inode->i_size = size;
46844 + ui->data_len = size;
46845 +
46846 + /*
46847 + * Note, it is important that 'ubifs_jnl_update()' writes the @host
46848 + * inode last, so when it gets synchronized and the write-buffer is
46849 + * flushed, the extended attribute is flushed as well.
46850 + */
46851 + err = ubifs_jnl_update(c, host, nm, inode, 0, IS_DIRSYNC(host), 1);
46852 + if (err)
46853 + goto out_cancel;
46854 +
46855 + ubifs_release_ino_clean(c, host, &req);
46856 + insert_inode_hash(inode);
46857 + iput(inode);
46858 + return 0;
46859 +
46860 +out_cancel:
46861 + host_ui->xattr_cnt -= 1;
46862 + spin_lock(&host->i_lock);
46863 + host_ui->xattr_size -= CALC_DENT_SIZE(nm->len);
46864 + host_ui->xattr_size -= CALC_XATTR_BYTES(size);
46865 + spin_unlock(&host->i_lock);
46866 +out_inode:
46867 + make_bad_inode(inode);
46868 + iput(inode);
46869 +out_budg:
46870 + ubifs_cancel_ino_op(c, host, &req);
46871 + return err;
46872 +}
46873 +
46874 +/**
46875 + * change_xattr - change an extended attribute.
46876 + * @c: UBIFS file-system description object
46877 + * @host: host inode
46878 + * @inode: extended attribute inode
46879 + * @value: extended attribute value
46880 + * @size: size of extended attribute value
46881 + *
46882 + * This helper function changes the value of extended attribute @inode with new
46883 + * data from @value. Returns zero in case of success and a negative error code
46884 + * in case of failure.
46885 + */
46886 +static int change_xattr(struct ubifs_info *c, struct inode *host,
46887 + struct inode *inode, const void *value, int size)
46888 +{
46889 + struct ubifs_inode *host_ui = ubifs_inode(host);
46890 + struct ubifs_inode *ui = ubifs_inode(inode);
46891 + struct ubifs_budget_req req = { .dirtied_ino = 1,
46892 + .dirtied_ino_d = ui->data_len };
46893 + int err;
46894 +
46895 + ubifs_assert(ui->data_len == inode->i_size);
46896 +
46897 + err = ubifs_budget_inode_op(c, host, &req);
46898 + if (err)
46899 + return err;
46900 +
46901 + host->i_ctime = ubifs_current_time(host);
46902 + spin_lock(&host->i_lock);
46903 + host_ui->xattr_size -= CALC_XATTR_BYTES(ui->data_len);
46904 + host_ui->xattr_size += CALC_XATTR_BYTES(size);
46905 + spin_unlock(&host->i_lock);
46906 +
46907 + kfree(ui->data);
46908 + ui->data = kmalloc(size, GFP_NOFS);
46909 + if (!ui->data) {
46910 + err = -ENOMEM;
46911 + goto out_budg;
46912 + }
46913 +
46914 + memcpy(ui->data, value, size);
46915 + inode->i_size = size;
46916 + ui->data_len = size;
46917 +
46918 + /*
46919 + * It is important to write the host inode after the xattr inode
46920 + * because if the host inode gets synchronized, then the extended
46921 + * attribute inode gets synchronized, because it goes before the host
46922 + * inode in the write-buffer.
46923 + */
46924 + err = ubifs_jnl_write_2_inodes(c, inode, host, IS_DIRSYNC(host));
46925 + if (err)
46926 + goto out_cancel;
46927 +
46928 + ubifs_release_ino_clean(c, host, &req);
46929 + return 0;
46930 +
46931 +out_cancel:
46932 + spin_lock(&host->i_lock);
46933 + host_ui->xattr_size -= CALC_XATTR_BYTES(size);
46934 + host_ui->xattr_size += CALC_XATTR_BYTES(ui->data_len);
46935 + spin_unlock(&host->i_lock);
46936 + make_bad_inode(inode);
46937 +out_budg:
46938 + ubifs_cancel_ino_op(c, host, &req);
46939 + return err;
46940 +}
46941 +
46942 +/**
46943 + * check_namespace - check extended attribute name-space.
46944 + * @nm: extended attribute name
46945 + *
46946 + * This function makes sure the extended attribute name belongs to one of the
46947 + * supported extended attribute name-spaces. Returns name-space index in case
46948 + * of success and a negative error code in case of failure.
46949 + */
46950 +static int check_namespace(const struct qstr *nm)
46951 +{
46952 + int type;
46953 +
46954 + if (nm->len > UBIFS_MAX_NLEN)
46955 + return -ENAMETOOLONG;
46956 +
46957 + if (!strncmp(nm->name, XATTR_TRUSTED_PREFIX,
46958 + XATTR_TRUSTED_PREFIX_LEN)) {
46959 + if (nm->name[sizeof(XATTR_TRUSTED_PREFIX) - 1] == '\0')
46960 + return -EINVAL;
46961 + type = TRUSTED_XATTR;
46962 + } else if (!strncmp(nm->name, XATTR_USER_PREFIX,
46963 + XATTR_USER_PREFIX_LEN)) {
46964 + if (nm->name[XATTR_USER_PREFIX_LEN] == '\0')
46965 + return -EINVAL;
46966 + type = USER_XATTR;
46967 + } else if (!strncmp(nm->name, XATTR_SECURITY_PREFIX,
46968 + XATTR_SECURITY_PREFIX_LEN)) {
46969 + if (nm->name[sizeof(XATTR_SECURITY_PREFIX) - 1] == '\0')
46970 + return -EINVAL;
46971 + type = SECURITY_XATTR;
46972 + } else
46973 + return -EOPNOTSUPP;
46974 +
46975 + return type;
46976 +}
46977 +
46978 +int ubifs_setxattr(struct dentry *dentry, const char *name,
46979 + const void *value, size_t size, int flags)
46980 +{
46981 + struct inode *inode, *host = dentry->d_inode;
46982 + struct ubifs_info *c = host->i_sb->s_fs_info;
46983 + struct qstr nm = { .name = name, .len = strlen(name) };
46984 + struct ubifs_dent_node *xent;
46985 + union ubifs_key key;
46986 + int err, type;
46987 +
46988 + dbg_gen("xattr '%s', host ino %lu ('%.*s'), size %zd", name,
46989 + host->i_ino, dentry->d_name.len, dentry->d_name.name, size);
46990 + ubifs_assert(ubifs_inode(host)->xattr_cnt >= 0);
46991 + ubifs_assert(ubifs_inode(host)->xattr_size >= 0);
46992 + ubifs_assert(ubifs_inode(host)->xattr_names >= 0);
46993 +
46994 + if (size > UBIFS_MAX_INO_DATA)
46995 + return -ERANGE;
46996 +
46997 + type = check_namespace(&nm);
46998 + if (type < 0)
46999 + return type;
47000 +
47001 + xent = kmalloc(UBIFS_MAX_XENT_NODE_SZ, GFP_NOFS);
47002 + if (!xent)
47003 + return -ENOMEM;
47004 +
47005 + /*
47006 + * The extended attribute entries are stored in LNC, so multiple
47007 + * look-ups do not involve reading the flash.
47008 + */
47009 + xent_key_init(c, &key, host->i_ino, &nm);
47010 + err = ubifs_tnc_lookup_nm(c, &key, xent, &nm);
47011 + if (err) {
47012 + if (err != -ENOENT)
47013 + goto out_free;
47014 +
47015 + if (flags & XATTR_REPLACE)
47016 + /* We are asked not to create the xattr */
47017 + err = -ENODATA;
47018 + else
47019 + err = create_xattr(c, host, &nm, value, size);
47020 + goto out_free;
47021 + }
47022 +
47023 + if (flags & XATTR_CREATE) {
47024 + /* We are asked not to replace the xattr */
47025 + err = -EEXIST;
47026 + goto out_free;
47027 + }
47028 +
47029 + inode = ubifs_iget(c->vfs_sb, le64_to_cpu(xent->inum));
47030 + if (IS_ERR(inode)) {
47031 + ubifs_err("dead extended attribute entry, error %d", err);
47032 + ubifs_ro_mode(c, err);
47033 + err = PTR_ERR(inode);
47034 + goto out_free;
47035 + }
47036 +
47037 + err = change_xattr(c, host, inode, value, size);
47038 + iput(inode);
47039 +
47040 +out_free:
47041 + kfree(xent);
47042 + return err;
47043 +}
47044 +
47045 +ssize_t ubifs_getxattr(struct dentry *dentry, const char *name, void *buf,
47046 + size_t size)
47047 +{
47048 + struct inode *inode, *host = dentry->d_inode;
47049 + struct ubifs_info *c = host->i_sb->s_fs_info;
47050 + struct qstr nm = { .name = name, .len = strlen(name) };
47051 + struct ubifs_inode *ui;
47052 + struct ubifs_dent_node *xent;
47053 + union ubifs_key key;
47054 + int err;
47055 +
47056 + dbg_gen("xattr '%s', ino %lu ('%.*s'), buf size %zd", name,
47057 + host->i_ino, dentry->d_name.len, dentry->d_name.name, size);
47058 + ubifs_assert(ubifs_inode(host)->xattr_cnt >= 0);
47059 + ubifs_assert(ubifs_inode(host)->xattr_size >= 0);
47060 + ubifs_assert(ubifs_inode(host)->xattr_names >= 0);
47061 +
47062 + err = check_namespace(&nm);
47063 + if (err < 0)
47064 + return err;
47065 +
47066 + xent = kmalloc(UBIFS_MAX_XENT_NODE_SZ, GFP_NOFS);
47067 + if (!xent)
47068 + return -ENOMEM;
47069 +
47070 + mutex_lock(&host->i_mutex);
47071 + xent_key_init(c, &key, host->i_ino, &nm);
47072 + err = ubifs_tnc_lookup_nm(c, &key, xent, &nm);
47073 + if (err) {
47074 + if (err == -ENOENT)
47075 + err = -ENODATA;
47076 + goto out_unlock;
47077 + }
47078 +
47079 + inode = ubifs_iget(c->vfs_sb, le64_to_cpu(xent->inum));
47080 + if (IS_ERR(inode)) {
47081 + ubifs_err("dead extended attribute entry, error %d", err);
47082 + ubifs_ro_mode(c, err);
47083 + err = PTR_ERR(inode);
47084 + goto out_unlock;
47085 + }
47086 +
47087 + ui = ubifs_inode(inode);
47088 + ubifs_assert(inode->i_size == ui->data_len);
47089 + ubifs_assert(ubifs_inode(host)->xattr_size > ui->data_len);
47090 +
47091 + if (buf) {
47092 + /* If @buf is %NULL we are supposed to return the length */
47093 + if (ui->data_len > size) {
47094 + dbg_err("buffer size %zd, xattr len %d",
47095 + size, ui->data_len);
47096 + err = -ERANGE;
47097 + goto out_iput;
47098 + }
47099 +
47100 + memcpy(buf, ui->data, ui->data_len);
47101 + }
47102 + err = ui->data_len;
47103 +
47104 +out_iput:
47105 + iput(inode);
47106 +out_unlock:
47107 + mutex_unlock(&host->i_mutex);
47108 + kfree(xent);
47109 + return err;
47110 +}
47111 +
47112 +ssize_t ubifs_listxattr(struct dentry *dentry, char *buffer, size_t size)
47113 +{
47114 + struct inode *host = dentry->d_inode;
47115 + struct ubifs_info *c = host->i_sb->s_fs_info;
47116 + struct ubifs_inode *host_ui = ubifs_inode(host);
47117 + union ubifs_key key;
47118 + struct ubifs_dent_node *xent, *pxent = NULL;
47119 + int err, len, written = 0;
47120 + struct qstr nm = { .name = NULL };
47121 +
47122 + dbg_gen("ino %lu ('%.*s'), buffer size %zd", host->i_ino,
47123 + dentry->d_name.len, dentry->d_name.name, size);
47124 + ubifs_assert(host_ui->xattr_cnt >= 0);
47125 + ubifs_assert(host_ui->xattr_size >= 0);
47126 + ubifs_assert(host_ui->xattr_names >= 0);
47127 +
47128 + len = host_ui->xattr_names + host_ui->xattr_cnt;
47129 + if (!buffer)
47130 + /*
47131 + * We should return the minimum buffer size which will fit a
47132 + * null-terminated list of all the extended attribute names.
47133 + */
47134 + return len;
47135 +
47136 + if (len > size)
47137 + return -ERANGE;
47138 +
47139 + lowest_xent_key(c, &key, host->i_ino);
47140 +
47141 + mutex_lock(&host->i_mutex);
47142 + while (1) {
47143 + int type;
47144 +
47145 + xent = ubifs_tnc_next_ent(c, &key, &nm);
47146 + if (unlikely(IS_ERR(xent))) {
47147 + err = PTR_ERR(xent);
47148 + break;
47149 + }
47150 +
47151 + nm.name = xent->name;
47152 + nm.len = le16_to_cpu(xent->nlen);
47153 +
47154 + type = check_namespace(&nm);
47155 + if (unlikely(type < 0)) {
47156 + err = type;
47157 + break;
47158 + }
47159 +
47160 + /* Show trusted namespace only for "power" users */
47161 + if (type != TRUSTED_XATTR || capable(CAP_SYS_ADMIN)) {
47162 + memcpy(buffer + written, nm.name, nm.len + 1);
47163 + written += nm.len + 1;
47164 + }
47165 +
47166 + kfree(pxent);
47167 + pxent = xent;
47168 + key_read(c, &xent->key, &key);
47169 + }
47170 + mutex_unlock(&host->i_mutex);
47171 +
47172 + kfree(pxent);
47173 + if (err != -ENOENT) {
47174 + ubifs_err("cannot find next direntry, error %d", err);
47175 + return err;
47176 + }
47177 +
47178 + ubifs_assert(written <= size);
47179 + return written;
47180 +}
47181 +
47182 +static int remove_xattr(struct ubifs_info *c, struct inode *host,
47183 + struct inode *inode, const struct qstr *nm)
47184 +{
47185 + struct ubifs_inode *host_ui = ubifs_inode(host);
47186 + struct ubifs_inode *ui = ubifs_inode(inode);
47187 + struct ubifs_budget_req req = { .dirtied_ino = 1, .mod_dent = 1 };
47188 + int err;
47189 +
47190 + ubifs_assert(ui->data_len == inode->i_size);
47191 +
47192 + err = ubifs_budget_inode_op(c, host, &req);
47193 + if (err)
47194 + return err;
47195 +
47196 + host->i_ctime = ubifs_current_time(host);
47197 + host_ui->xattr_cnt -= 1;
47198 + spin_lock(&host->i_lock);
47199 + host_ui->xattr_size -= CALC_DENT_SIZE(nm->len);
47200 + host_ui->xattr_size -= CALC_XATTR_BYTES(ui->data_len);
47201 + spin_unlock(&host->i_lock);
47202 + host_ui->xattr_names -= nm->len;
47203 +
47204 + err = ubifs_jnl_delete_xattr(c, host, inode, nm, IS_DIRSYNC(host));
47205 + if (err)
47206 + goto out_cancel;
47207 +
47208 + ubifs_release_ino_clean(c, host, &req);
47209 + return 0;
47210 +
47211 +out_cancel:
47212 + ubifs_cancel_ino_op(c, host, &req);
47213 + host_ui->xattr_cnt += 1;
47214 + spin_lock(&host->i_lock);
47215 + host_ui->xattr_size += CALC_DENT_SIZE(nm->len);
47216 + host_ui->xattr_size += CALC_XATTR_BYTES(ui->data_len);
47217 + spin_unlock(&host->i_lock);
47218 + make_bad_inode(inode);
47219 + return err;
47220 +}
47221 +
47222 +int ubifs_removexattr(struct dentry *dentry, const char *name)
47223 +{
47224 + struct inode *inode, *host = dentry->d_inode;
47225 + struct ubifs_info *c = host->i_sb->s_fs_info;
47226 + struct qstr nm = { .name = name, .len = strlen(name) };
47227 + struct ubifs_dent_node *xent;
47228 + union ubifs_key key;
47229 + int err;
47230 +
47231 + dbg_gen("xattr '%s', ino %lu ('%.*s')", name,
47232 + host->i_ino, dentry->d_name.len, dentry->d_name.name);
47233 + ubifs_assert(mutex_is_locked(&host->i_mutex));
47234 + ubifs_assert(ubifs_inode(host)->xattr_cnt >= 0);
47235 + ubifs_assert(ubifs_inode(host)->xattr_size >= 0);
47236 + ubifs_assert(ubifs_inode(host)->xattr_names >= 0);
47237 +
47238 + err = check_namespace(&nm);
47239 + if (err < 0)
47240 + return err;
47241 +
47242 + xent = kmalloc(UBIFS_MAX_XENT_NODE_SZ, GFP_NOFS);
47243 + if (!xent)
47244 + return -ENOMEM;
47245 +
47246 + xent_key_init(c, &key, host->i_ino, &nm);
47247 + err = ubifs_tnc_lookup_nm(c, &key, xent, &nm);
47248 + if (err) {
47249 + if (err == -ENOENT)
47250 + err = -ENODATA;
47251 + goto out_free;
47252 + }
47253 +
47254 + inode = ubifs_iget(c->vfs_sb, le64_to_cpu(xent->inum));
47255 + if (IS_ERR(inode)) {
47256 + ubifs_err("dead extended attribute node entry");
47257 + ubifs_ro_mode(c, err);
47258 + err = PTR_ERR(inode);
47259 + goto out_free;
47260 + }
47261 +
47262 + ubifs_assert(inode->i_nlink == 1);
47263 + inode->i_nlink = 0;
47264 + err = remove_xattr(c, host, inode, &nm);
47265 + if (err)
47266 + inode->i_nlink = 1;
47267 +
47268 + /* If @i_nlink is 0, 'iput()' will delete the inode */
47269 + iput(inode);
47270 +
47271 +out_free:
47272 + kfree(xent);
47273 + return err;
47274 +}
47275 diff --exclude=.git -urN linux-2.6.25.6/include/asm-arm/arch-at91/at91_ecc.h avr32-2.6/include/asm-arm/arch-at91/at91_ecc.h
47276 --- linux-2.6.25.6/include/asm-arm/arch-at91/at91_ecc.h 2008-06-09 20:27:19.000000000 +0200
47277 +++ avr32-2.6/include/asm-arm/arch-at91/at91_ecc.h 1970-01-01 01:00:00.000000000 +0100
47278 @@ -1,38 +0,0 @@
47279 -/*
47280 - * include/asm-arm/arch-at91/at91_ecc.h
47281 - *
47282 - * Error Corrected Code Controller (ECC) - System peripherals regsters.
47283 - * Based on AT91SAM9260 datasheet revision B.
47284 - *
47285 - * This program is free software; you can redistribute it and/or modify it
47286 - * under the terms of the GNU General Public License as published by the
47287 - * Free Software Foundation; either version 2 of the License, or (at your
47288 - * option) any later version.
47289 - */
47290 -
47291 -#ifndef AT91_ECC_H
47292 -#define AT91_ECC_H
47293 -
47294 -#define AT91_ECC_CR (AT91_ECC + 0x00) /* Control register */
47295 -#define AT91_ECC_RST (1 << 0) /* Reset parity */
47296 -
47297 -#define AT91_ECC_MR (AT91_ECC + 0x04) /* Mode register */
47298 -#define AT91_ECC_PAGESIZE (3 << 0) /* Page Size */
47299 -#define AT91_ECC_PAGESIZE_528 (0)
47300 -#define AT91_ECC_PAGESIZE_1056 (1)
47301 -#define AT91_ECC_PAGESIZE_2112 (2)
47302 -#define AT91_ECC_PAGESIZE_4224 (3)
47303 -
47304 -#define AT91_ECC_SR (AT91_ECC + 0x08) /* Status register */
47305 -#define AT91_ECC_RECERR (1 << 0) /* Recoverable Error */
47306 -#define AT91_ECC_ECCERR (1 << 1) /* ECC Single Bit Error */
47307 -#define AT91_ECC_MULERR (1 << 2) /* Multiple Errors */
47308 -
47309 -#define AT91_ECC_PR (AT91_ECC + 0x0c) /* Parity register */
47310 -#define AT91_ECC_BITADDR (0xf << 0) /* Bit Error Address */
47311 -#define AT91_ECC_WORDADDR (0xfff << 4) /* Word Error Address */
47312 -
47313 -#define AT91_ECC_NPR (AT91_ECC + 0x10) /* NParity register */
47314 -#define AT91_ECC_NPARITY (0xffff << 0) /* NParity */
47315 -
47316 -#endif
47317 diff --exclude=.git -urN linux-2.6.25.6/include/asm-arm/arch-at91/board.h avr32-2.6/include/asm-arm/arch-at91/board.h
47318 --- linux-2.6.25.6/include/asm-arm/arch-at91/board.h 2008-06-09 20:27:19.000000000 +0200
47319 +++ avr32-2.6/include/asm-arm/arch-at91/board.h 2008-06-12 15:09:45.803815435 +0200
47320 @@ -85,7 +85,7 @@
47321 extern void __init at91_add_device_usbh(struct at91_usbh_data *data);
47322
47323 /* NAND / SmartMedia */
47324 -struct at91_nand_data {
47325 +struct atmel_nand_data {
47326 u8 enable_pin; /* chip enable */
47327 u8 det_pin; /* card detect */
47328 u8 rdy_pin; /* ready/busy */
47329 @@ -94,7 +94,7 @@
47330 u8 bus_width_16; /* buswidth is 16 bit */
47331 struct mtd_partition* (*partition_info)(int, int*);
47332 };
47333 -extern void __init at91_add_device_nand(struct at91_nand_data *data);
47334 +extern void __init at91_add_device_nand(struct atmel_nand_data *data);
47335
47336 /* I2C*/
47337 extern void __init at91_add_device_i2c(struct i2c_board_info *devices, int nr_devices);
47338 diff --exclude=.git -urN linux-2.6.25.6/include/asm-avr32/arch-at32ap/board.h avr32-2.6/include/asm-avr32/arch-at32ap/board.h
47339 --- linux-2.6.25.6/include/asm-avr32/arch-at32ap/board.h 2008-06-09 20:27:19.000000000 +0200
47340 +++ avr32-2.6/include/asm-avr32/arch-at32ap/board.h 2008-06-12 15:09:45.855816193 +0200
47341 @@ -8,6 +8,12 @@
47342
47343 #define GPIO_PIN_NONE (-1)
47344
47345 +/*
47346 + * Clock rates for various on-board oscillators. The number of entries
47347 + * in this array is chip-dependent.
47348 + */
47349 +extern unsigned long at32_board_osc_rates[];
47350 +
47351 /* Add basic devices: system manager, interrupt controller, portmuxes, etc. */
47352 void at32_add_system_devices(void);
47353
47354 @@ -36,11 +42,10 @@
47355 struct atmel_lcdfb_info;
47356 struct platform_device *
47357 at32_add_device_lcdc(unsigned int id, struct atmel_lcdfb_info *data,
47358 - unsigned long fbmem_start, unsigned long fbmem_len);
47359 + unsigned long fbmem_start, unsigned long fbmem_len,
47360 + unsigned int pin_config);
47361
47362 -struct usba_platform_data {
47363 - int vbus_pin;
47364 -};
47365 +struct usba_platform_data;
47366 struct platform_device *
47367 at32_add_device_usba(unsigned int id, struct usba_platform_data *data);
47368
47369 @@ -68,8 +73,17 @@
47370 struct platform_device *
47371 at32_add_device_ssc(unsigned int id, unsigned int flags);
47372
47373 -struct platform_device *at32_add_device_twi(unsigned int id);
47374 -struct platform_device *at32_add_device_mci(unsigned int id);
47375 +struct i2c_board_info;
47376 +struct platform_device *at32_add_device_twi(unsigned int id,
47377 + struct i2c_board_info *b,
47378 + unsigned int n);
47379 +
47380 +struct mci_platform_data {
47381 + int detect_pin;
47382 + int wp_pin;
47383 +};
47384 +struct platform_device *
47385 +at32_add_device_mci(unsigned int id, struct mci_platform_data *data);
47386 struct platform_device *at32_add_device_ac97c(unsigned int id);
47387 struct platform_device *at32_add_device_abdac(unsigned int id);
47388
47389 @@ -84,4 +98,20 @@
47390 at32_add_device_cf(unsigned int id, unsigned int extint,
47391 struct cf_platform_data *data);
47392
47393 +struct platform_device *
47394 +at32_add_device_psif(unsigned int id);
47395 +
47396 +/* NAND / SmartMedia */
47397 +struct atmel_nand_data {
47398 + int enable_pin; /* chip enable */
47399 + int det_pin; /* card detect */
47400 + int rdy_pin; /* ready/busy */
47401 + u8 ale; /* address line number connected to ALE */
47402 + u8 cle; /* address line number connected to CLE */
47403 + u8 bus_width_16; /* buswidth is 16 bit */
47404 + struct mtd_partition *(*partition_info)(int size, int *num_partitions);
47405 +};
47406 +struct platform_device *
47407 +at32_add_device_nand(unsigned int id, struct atmel_nand_data *data);
47408 +
47409 #endif /* __ASM_ARCH_BOARD_H */
47410 diff --exclude=.git -urN linux-2.6.25.6/include/asm-avr32/arch-at32ap/init.h avr32-2.6/include/asm-avr32/arch-at32ap/init.h
47411 --- linux-2.6.25.6/include/asm-avr32/arch-at32ap/init.h 2008-06-09 20:27:19.000000000 +0200
47412 +++ avr32-2.6/include/asm-avr32/arch-at32ap/init.h 2008-06-12 15:09:45.855816193 +0200
47413 @@ -13,10 +13,6 @@
47414 void setup_platform(void);
47415 void setup_board(void);
47416
47417 -/* Called by setup_platform */
47418 -void at32_clock_init(void);
47419 -void at32_portmux_init(void);
47420 -
47421 void at32_setup_serial_console(unsigned int usart_id);
47422
47423 #endif /* __ASM_AVR32_AT32AP_INIT_H__ */
47424 diff --exclude=.git -urN linux-2.6.25.6/include/asm-avr32/arch-at32ap/pm.h avr32-2.6/include/asm-avr32/arch-at32ap/pm.h
47425 --- linux-2.6.25.6/include/asm-avr32/arch-at32ap/pm.h 1970-01-01 01:00:00.000000000 +0100
47426 +++ avr32-2.6/include/asm-avr32/arch-at32ap/pm.h 2008-06-12 15:09:45.855816193 +0200
47427 @@ -0,0 +1,51 @@
47428 +/*
47429 + * AVR32 AP Power Management.
47430 + *
47431 + * Copyright (C) 2008 Atmel Corporation
47432 + *
47433 + * This program is free software; you can redistribute it and/or modify
47434 + * it under the terms of the GNU General Public License version 2 as
47435 + * published by the Free Software Foundation.
47436 + */
47437 +#ifndef __ASM_AVR32_ARCH_PM_H
47438 +#define __ASM_AVR32_ARCH_PM_H
47439 +
47440 +/* Possible arguments to the "sleep" instruction */
47441 +#define CPU_SLEEP_IDLE 0
47442 +#define CPU_SLEEP_FROZEN 1
47443 +#define CPU_SLEEP_STANDBY 2
47444 +#define CPU_SLEEP_STOP 3
47445 +#define CPU_SLEEP_STATIC 5
47446 +
47447 +#ifndef __ASSEMBLY__
47448 +extern void cpu_enter_idle(void);
47449 +extern void cpu_enter_standby(unsigned long sdramc_base);
47450 +
47451 +extern bool disable_idle_sleep;
47452 +
47453 +static inline void cpu_disable_idle_sleep(void)
47454 +{
47455 + disable_idle_sleep = true;
47456 +}
47457 +
47458 +static inline void cpu_enable_idle_sleep(void)
47459 +{
47460 + disable_idle_sleep = false;
47461 +}
47462 +
47463 +static inline void cpu_idle_sleep(void)
47464 +{
47465 + /*
47466 + * If we're using the COUNT and COMPARE registers for
47467 + * timekeeping, we can't use the IDLE state.
47468 + */
47469 + if (disable_idle_sleep)
47470 + cpu_relax();
47471 + else
47472 + cpu_enter_idle();
47473 +}
47474 +
47475 +void intc_set_suspend_handler(unsigned long offset);
47476 +#endif
47477 +
47478 +#endif /* __ASM_AVR32_ARCH_PM_H */
47479 diff --exclude=.git -urN linux-2.6.25.6/include/asm-avr32/arch-at32ap/portmux.h avr32-2.6/include/asm-avr32/arch-at32ap/portmux.h
47480 --- linux-2.6.25.6/include/asm-avr32/arch-at32ap/portmux.h 2008-06-09 20:27:19.000000000 +0200
47481 +++ avr32-2.6/include/asm-avr32/arch-at32ap/portmux.h 2008-06-12 15:09:45.859816144 +0200
47482 @@ -26,4 +26,16 @@
47483 void at32_select_gpio(unsigned int pin, unsigned long flags);
47484 void at32_reserve_pin(unsigned int pin);
47485
47486 +#ifdef CONFIG_GPIO_DEV
47487 +
47488 +/* Gang allocators and accessors; used by the GPIO /dev driver */
47489 +int at32_gpio_port_is_valid(unsigned int port);
47490 +int at32_select_gpio_pins(unsigned int port, u32 pins, u32 oe_mask);
47491 +void at32_deselect_pins(unsigned int port, u32 pins);
47492 +
47493 +u32 at32_gpio_get_value_multiple(unsigned int port, u32 pins);
47494 +void at32_gpio_set_value_multiple(unsigned int port, u32 value, u32 mask);
47495 +
47496 +#endif /* CONFIG_GPIO_DEV */
47497 +
47498 #endif /* __ASM_ARCH_PORTMUX_H__ */
47499 diff --exclude=.git -urN linux-2.6.25.6/include/asm-avr32/arch-at32ap/sram.h avr32-2.6/include/asm-avr32/arch-at32ap/sram.h
47500 --- linux-2.6.25.6/include/asm-avr32/arch-at32ap/sram.h 1970-01-01 01:00:00.000000000 +0100
47501 +++ avr32-2.6/include/asm-avr32/arch-at32ap/sram.h 2008-06-12 15:09:45.859816144 +0200
47502 @@ -0,0 +1,30 @@
47503 +/*
47504 + * Simple SRAM allocator
47505 + *
47506 + * Copyright (C) 2008 Atmel Corporation
47507 + *
47508 + * This program is free software; you can redistribute it and/or modify
47509 + * it under the terms of the GNU General Public License version 2 as
47510 + * published by the Free Software Foundation.
47511 + */
47512 +#ifndef __ASM_AVR32_ARCH_SRAM_H
47513 +#define __ASM_AVR32_ARCH_SRAM_H
47514 +
47515 +#include <linux/genalloc.h>
47516 +
47517 +extern struct gen_pool *sram_pool;
47518 +
47519 +static inline unsigned long sram_alloc(size_t len)
47520 +{
47521 + if (!sram_pool)
47522 + return 0UL;
47523 +
47524 + return gen_pool_alloc(sram_pool, len);
47525 +}
47526 +
47527 +static inline void sram_free(unsigned long addr, size_t len)
47528 +{
47529 + return gen_pool_free(sram_pool, addr, len);
47530 +}
47531 +
47532 +#endif /* __ASM_AVR32_ARCH_SRAM_H */
47533 diff --exclude=.git -urN linux-2.6.25.6/include/asm-avr32/arch-at32ap/time.h avr32-2.6/include/asm-avr32/arch-at32ap/time.h
47534 --- linux-2.6.25.6/include/asm-avr32/arch-at32ap/time.h 2008-06-09 20:27:19.000000000 +0200
47535 +++ avr32-2.6/include/asm-avr32/arch-at32ap/time.h 1970-01-01 01:00:00.000000000 +0100
47536 @@ -1,112 +0,0 @@
47537 -/*
47538 - * Copyright (C) 2007 Atmel Corporation
47539 - *
47540 - * This program is free software; you can redistribute it and/or modify
47541 - * it under the terms of the GNU General Public License version 2 as
47542 - * published by the Free Software Foundation.
47543 - */
47544 -
47545 -#ifndef _ASM_AVR32_ARCH_AT32AP_TIME_H
47546 -#define _ASM_AVR32_ARCH_AT32AP_TIME_H
47547 -
47548 -#include <linux/platform_device.h>
47549 -
47550 -extern struct irqaction timer_irqaction;
47551 -extern struct platform_device at32_systc0_device;
47552 -extern void local_timer_interrupt(int irq, void *dev_id);
47553 -
47554 -#define TIMER_BCR 0x000000c0
47555 -#define TIMER_BCR_SYNC 0
47556 -#define TIMER_BMR 0x000000c4
47557 -#define TIMER_BMR_TC0XC0S 0
47558 -#define TIMER_BMR_TC1XC1S 2
47559 -#define TIMER_BMR_TC2XC2S 4
47560 -#define TIMER_CCR 0x00000000
47561 -#define TIMER_CCR_CLKDIS 1
47562 -#define TIMER_CCR_CLKEN 0
47563 -#define TIMER_CCR_SWTRG 2
47564 -#define TIMER_CMR 0x00000004
47565 -#define TIMER_CMR_ABETRG 10
47566 -#define TIMER_CMR_ACPA 16
47567 -#define TIMER_CMR_ACPC 18
47568 -#define TIMER_CMR_AEEVT 20
47569 -#define TIMER_CMR_ASWTRG 22
47570 -#define TIMER_CMR_BCPB 24
47571 -#define TIMER_CMR_BCPC 26
47572 -#define TIMER_CMR_BEEVT 28
47573 -#define TIMER_CMR_BSWTRG 30
47574 -#define TIMER_CMR_BURST 4
47575 -#define TIMER_CMR_CLKI 3
47576 -#define TIMER_CMR_CPCDIS 7
47577 -#define TIMER_CMR_CPCSTOP 6
47578 -#define TIMER_CMR_CPCTRG 14
47579 -#define TIMER_CMR_EEVT 10
47580 -#define TIMER_CMR_EEVTEDG 8
47581 -#define TIMER_CMR_ENETRG 12
47582 -#define TIMER_CMR_ETRGEDG 8
47583 -#define TIMER_CMR_LDBDIS 7
47584 -#define TIMER_CMR_LDBSTOP 6
47585 -#define TIMER_CMR_LDRA 16
47586 -#define TIMER_CMR_LDRB 18
47587 -#define TIMER_CMR_TCCLKS 0
47588 -#define TIMER_CMR_WAVE 15
47589 -#define TIMER_CMR_WAVSEL 13
47590 -#define TIMER_CV 0x00000010
47591 -#define TIMER_CV_CV 0
47592 -#define TIMER_IDR 0x00000028
47593 -#define TIMER_IDR_COVFS 0
47594 -#define TIMER_IDR_CPAS 2
47595 -#define TIMER_IDR_CPBS 3
47596 -#define TIMER_IDR_CPCS 4
47597 -#define TIMER_IDR_ETRGS 7
47598 -#define TIMER_IDR_LDRAS 5
47599 -#define TIMER_IDR_LDRBS 6
47600 -#define TIMER_IDR_LOVRS 1
47601 -#define TIMER_IER 0x00000024
47602 -#define TIMER_IER_COVFS 0
47603 -#define TIMER_IER_CPAS 2
47604 -#define TIMER_IER_CPBS 3
47605 -#define TIMER_IER_CPCS 4
47606 -#define TIMER_IER_ETRGS 7
47607 -#define TIMER_IER_LDRAS 5
47608 -#define TIMER_IER_LDRBS 6
47609 -#define TIMER_IER_LOVRS 1
47610 -#define TIMER_IMR 0x0000002c
47611 -#define TIMER_IMR_COVFS 0
47612 -#define TIMER_IMR_CPAS 2
47613 -#define TIMER_IMR_CPBS 3
47614 -#define TIMER_IMR_CPCS 4
47615 -#define TIMER_IMR_ETRGS 7
47616 -#define TIMER_IMR_LDRAS 5
47617 -#define TIMER_IMR_LDRBS 6
47618 -#define TIMER_IMR_LOVRS 1
47619 -#define TIMER_RA 0x00000014
47620 -#define TIMER_RA_RA 0
47621 -#define TIMER_RB 0x00000018
47622 -#define TIMER_RB_RB 0
47623 -#define TIMER_RC 0x0000001c
47624 -#define TIMER_RC_RC 0
47625 -#define TIMER_SR 0x00000020
47626 -#define TIMER_SR_CLKSTA 16
47627 -#define TIMER_SR_COVFS 0
47628 -#define TIMER_SR_CPAS 2
47629 -#define TIMER_SR_CPBS 3
47630 -#define TIMER_SR_CPCS 4
47631 -#define TIMER_SR_ETRGS 7
47632 -#define TIMER_SR_LDRAS 5
47633 -#define TIMER_SR_LDRBS 6
47634 -#define TIMER_SR_LOVRS 1
47635 -#define TIMER_SR_MTIOA 17
47636 -#define TIMER_SR_MTIOB 18
47637 -
47638 -/* Bit manipulation macros */
47639 -#define TIMER_BIT(name) (1 << TIMER_##name)
47640 -#define TIMER_BF(name,value) ((value) << TIMER_##name)
47641 -
47642 -/* Register access macros */
47643 -#define timer_read(port,instance,reg) \
47644 - __raw_readl(port + (0x40 * instance) + TIMER_##reg)
47645 -#define timer_write(port,instance,reg,value) \
47646 - __raw_writel((value), port + (0x40 * instance) + TIMER_##reg)
47647 -
47648 -#endif /* _ASM_AVR32_ARCH_AT32AP_TIME_H */
47649 diff --exclude=.git -urN linux-2.6.25.6/include/asm-avr32/asm.h avr32-2.6/include/asm-avr32/asm.h
47650 --- linux-2.6.25.6/include/asm-avr32/asm.h 2008-06-09 20:27:19.000000000 +0200
47651 +++ avr32-2.6/include/asm-avr32/asm.h 2008-06-12 15:04:04.683816631 +0200
47652 @@ -12,10 +12,10 @@
47653 #include <asm/asm-offsets.h>
47654 #include <asm/thread_info.h>
47655
47656 -#define mask_interrupts ssrf SR_GM_BIT
47657 -#define mask_exceptions ssrf SR_EM_BIT
47658 -#define unmask_interrupts csrf SR_GM_BIT
47659 -#define unmask_exceptions csrf SR_EM_BIT
47660 +#define mask_interrupts ssrf SYSREG_GM_OFFSET
47661 +#define mask_exceptions ssrf SYSREG_EM_OFFSET
47662 +#define unmask_interrupts csrf SYSREG_GM_OFFSET
47663 +#define unmask_exceptions csrf SYSREG_EM_OFFSET
47664
47665 #ifdef CONFIG_FRAME_POINTER
47666 .macro save_fp
47667 diff --exclude=.git -urN linux-2.6.25.6/include/asm-avr32/dma-controller.h avr32-2.6/include/asm-avr32/dma-controller.h
47668 --- linux-2.6.25.6/include/asm-avr32/dma-controller.h 1970-01-01 01:00:00.000000000 +0100
47669 +++ avr32-2.6/include/asm-avr32/dma-controller.h 2008-06-12 15:09:45.859816144 +0200
47670 @@ -0,0 +1,166 @@
47671 +/*
47672 + * Copyright (C) 2005-2006 Atmel Corporation
47673 + *
47674 + * This program is free software; you can redistribute it and/or modify
47675 + * it under the terms of the GNU General Public License version 2 as
47676 + * published by the Free Software Foundation.
47677 + */
47678 +#ifndef __ASM_AVR32_DMA_CONTROLLER_H
47679 +#define __ASM_AVR32_DMA_CONTROLLER_H
47680 +
47681 +#include <linux/device.h>
47682 +
47683 +#define DMA_DIR_MEM_TO_MEM 0x0000
47684 +#define DMA_DIR_MEM_TO_PERIPH 0x0001
47685 +#define DMA_DIR_PERIPH_TO_MEM 0x0002
47686 +#define DMA_DIR_PERIPH_TO_PERIPH 0x0003
47687 +
47688 +#define DMA_WIDTH_8BIT 0
47689 +#define DMA_WIDTH_16BIT 1
47690 +#define DMA_WIDTH_32BIT 2
47691 +
47692 +struct dma_request {
47693 + struct dma_controller *dmac;
47694 + struct list_head list;
47695 +
47696 + unsigned short channel;
47697 +
47698 + void (*xfer_complete)(struct dma_request *req);
47699 + void (*block_complete)(struct dma_request *req);
47700 + void (*error)(struct dma_request *req);
47701 +};
47702 +
47703 +struct dma_request_sg {
47704 + struct dma_request req;
47705 +
47706 + int nr_sg;
47707 + struct scatterlist *sg;
47708 + unsigned long block_size;
47709 + unsigned int nr_blocks;
47710 +
47711 + dma_addr_t data_reg;
47712 + unsigned short periph_id;
47713 +
47714 + unsigned char direction;
47715 + unsigned char width;
47716 +};
47717 +#define to_dma_request_sg(_req) \
47718 + container_of(_req, struct dma_request_sg, req)
47719 +
47720 +struct dma_request_cyclic {
47721 + struct dma_request req;
47722 +
47723 + int periods;
47724 + unsigned long buffer_size;
47725 +
47726 + dma_addr_t buffer_start;
47727 + dma_addr_t data_reg;
47728 +
47729 + unsigned short periph_id;
47730 + unsigned char direction;
47731 + unsigned char width;
47732 +
47733 + void *dev_id;
47734 +};
47735 +#define to_dma_request_cyclic(_req) \
47736 + container_of(_req, struct dma_request_cyclic, req)
47737 +
47738 +struct dma_request_memcpy {
47739 + struct dma_request req;
47740 +
47741 + dma_addr_t src_addr;
47742 + unsigned int src_width;
47743 + unsigned int src_stride;
47744 +
47745 + dma_addr_t dst_addr;
47746 + unsigned int dst_width;
47747 + unsigned int dst_stride;
47748 +
47749 + size_t length;
47750 +
47751 + unsigned short src_reverse:1;
47752 + unsigned short dst_reverse:1;
47753 +};
47754 +#define to_dma_request_memcpy(_req) \
47755 + container_of(_req, struct dma_request_memcpy, req)
47756 +
47757 +struct dma_controller {
47758 + struct list_head list;
47759 + int id;
47760 + struct device *dev;
47761 +
47762 + int (*alloc_channel)(struct dma_controller *dmac);
47763 + void (*release_channel)(struct dma_controller *dmac,
47764 + int channel);
47765 + int (*prepare_request_sg)(struct dma_controller *dmac,
47766 + struct dma_request_sg *req);
47767 + int (*prepare_request_cyclic)(struct dma_controller *dmac,
47768 + struct dma_request_cyclic *req);
47769 + int (*prepare_request_memcpy)(struct dma_controller *dmac,
47770 + struct dma_request_memcpy *req);
47771 + int (*start_request)(struct dma_controller *dmac,
47772 + unsigned int channel);
47773 + int (*stop_request)(struct dma_controller *dmac,
47774 + unsigned int channel);
47775 + dma_addr_t (*get_current_pos)(struct dma_controller *dmac,
47776 + unsigned int channel);
47777 +};
47778 +
47779 +static inline int
47780 +dma_alloc_channel(struct dma_controller *dmac)
47781 +{
47782 + return dmac->alloc_channel(dmac);
47783 +}
47784 +
47785 +static inline void
47786 +dma_release_channel(struct dma_controller *dmac, int chan)
47787 +{
47788 + dmac->release_channel(dmac, chan);
47789 +}
47790 +
47791 +static inline int
47792 +dma_prepare_request_sg(struct dma_controller *dmac,
47793 + struct dma_request_sg *req)
47794 +{
47795 + return dmac->prepare_request_sg(dmac, req);
47796 +}
47797 +
47798 +static inline int
47799 +dma_prepare_request_cyclic(struct dma_controller *dmac,
47800 + struct dma_request_cyclic *req)
47801 +{
47802 + return dmac->prepare_request_cyclic(dmac, req);
47803 +}
47804 +
47805 +static inline int
47806 +dma_prepare_request_memcpy(struct dma_controller *dmac,
47807 + struct dma_request_memcpy *req)
47808 +{
47809 + return dmac->prepare_request_memcpy(dmac, req);
47810 +}
47811 +
47812 +static inline int
47813 +dma_start_request(struct dma_controller *dmac,
47814 + unsigned int channel)
47815 +{
47816 + return dmac->start_request(dmac, channel);
47817 +}
47818 +
47819 +static inline int
47820 +dma_stop_request(struct dma_controller *dmac,
47821 + unsigned int channel)
47822 +{
47823 + return dmac->stop_request(dmac, channel);
47824 +}
47825 +
47826 +static inline dma_addr_t
47827 +dma_get_current_pos(struct dma_controller *dmac,
47828 + unsigned int channel)
47829 +{
47830 + return dmac->get_current_pos(dmac, channel);
47831 +}
47832 +
47833 +extern int register_dma_controller(struct dma_controller *dmac);
47834 +extern struct dma_controller *find_dma_controller(int id);
47835 +
47836 +#endif /* __ASM_AVR32_DMA_CONTROLLER_H */
47837 diff --exclude=.git -urN linux-2.6.25.6/include/asm-avr32/intc.h avr32-2.6/include/asm-avr32/intc.h
47838 --- linux-2.6.25.6/include/asm-avr32/intc.h 2008-06-09 20:27:19.000000000 +0200
47839 +++ avr32-2.6/include/asm-avr32/intc.h 1970-01-01 01:00:00.000000000 +0100
47840 @@ -1,128 +0,0 @@
47841 -#ifndef __ASM_AVR32_INTC_H
47842 -#define __ASM_AVR32_INTC_H
47843 -
47844 -#include <linux/sysdev.h>
47845 -#include <linux/interrupt.h>
47846 -
47847 -struct irq_controller;
47848 -struct irqaction;
47849 -struct pt_regs;
47850 -
47851 -struct platform_device;
47852 -
47853 -/* Information about the internal interrupt controller */
47854 -struct intc_device {
47855 - /* ioremapped address of configuration block */
47856 - void __iomem *regs;
47857 -
47858 - /* the physical device */
47859 - struct platform_device *pdev;
47860 -
47861 - /* Number of interrupt lines per group. */
47862 - unsigned int irqs_per_group;
47863 -
47864 - /* The highest group ID + 1 */
47865 - unsigned int nr_groups;
47866 -
47867 - /*
47868 - * Bitfield indicating which groups are actually in use. The
47869 - * size of the array is
47870 - * ceil(group_max / (8 * sizeof(unsigned int))).
47871 - */
47872 - unsigned int group_mask[];
47873 -};
47874 -
47875 -struct irq_controller_class {
47876 - /*
47877 - * A short name identifying this kind of controller.
47878 - */
47879 - const char *typename;
47880 - /*
47881 - * Handle the IRQ. Must do any necessary acking and masking.
47882 - */
47883 - irqreturn_t (*handle)(int irq, void *dev_id, struct pt_regs *regs);
47884 - /*
47885 - * Register a new IRQ handler.
47886 - */
47887 - int (*setup)(struct irq_controller *ctrl, unsigned int irq,
47888 - struct irqaction *action);
47889 - /*
47890 - * Unregister a IRQ handler.
47891 - */
47892 - void (*free)(struct irq_controller *ctrl, unsigned int irq,
47893 - void *dev_id);
47894 - /*
47895 - * Mask the IRQ in the interrupt controller.
47896 - */
47897 - void (*mask)(struct irq_controller *ctrl, unsigned int irq);
47898 - /*
47899 - * Unmask the IRQ in the interrupt controller.
47900 - */
47901 - void (*unmask)(struct irq_controller *ctrl, unsigned int irq);
47902 - /*
47903 - * Set the type of the IRQ. See below for possible types.
47904 - * Return -EINVAL if a given type is not supported
47905 - */
47906 - int (*set_type)(struct irq_controller *ctrl, unsigned int irq,
47907 - unsigned int type);
47908 - /*
47909 - * Return the IRQ type currently set
47910 - */
47911 - unsigned int (*get_type)(struct irq_controller *ctrl, unsigned int irq);
47912 -};
47913 -
47914 -struct irq_controller {
47915 - struct irq_controller_class *class;
47916 - unsigned int irq_group;
47917 - unsigned int first_irq;
47918 - unsigned int nr_irqs;
47919 - struct list_head list;
47920 -};
47921 -
47922 -struct intc_group_desc {
47923 - struct irq_controller *ctrl;
47924 - irqreturn_t (*handle)(int, void *, struct pt_regs *);
47925 - unsigned long flags;
47926 - void *dev_id;
47927 - const char *devname;
47928 -};
47929 -
47930 -/*
47931 - * The internal interrupt controller. Defined in board/part-specific
47932 - * devices.c.
47933 - * TODO: Should probably be defined per-cpu.
47934 - */
47935 -extern struct intc_device intc;
47936 -
47937 -extern int request_internal_irq(unsigned int irq,
47938 - irqreturn_t (*handler)(int, void *, struct pt_regs *),
47939 - unsigned long irqflags,
47940 - const char *devname, void *dev_id);
47941 -extern void free_internal_irq(unsigned int irq);
47942 -
47943 -/* Only used by time_init() */
47944 -extern int setup_internal_irq(unsigned int irq, struct intc_group_desc *desc);
47945 -
47946 -/*
47947 - * Set interrupt priority for a given group. `group' can be found by
47948 - * using irq_to_group(irq). Priority can be from 0 (lowest) to 3
47949 - * (highest). Higher-priority interrupts will preempt lower-priority
47950 - * interrupts (unless interrupts are masked globally).
47951 - *
47952 - * This function does not check for conflicts within a group.
47953 - */
47954 -extern int intc_set_priority(unsigned int group,
47955 - unsigned int priority);
47956 -
47957 -/*
47958 - * Returns a bitmask of pending interrupts in a group.
47959 - */
47960 -extern unsigned long intc_get_pending(unsigned int group);
47961 -
47962 -/*
47963 - * Register a new external interrupt controller. Returns the first
47964 - * external IRQ number that is assigned to the new controller.
47965 - */
47966 -extern int intc_register_controller(struct irq_controller *ctrl);
47967 -
47968 -#endif /* __ASM_AVR32_INTC_H */
47969 diff --exclude=.git -urN linux-2.6.25.6/include/asm-avr32/irq.h avr32-2.6/include/asm-avr32/irq.h
47970 --- linux-2.6.25.6/include/asm-avr32/irq.h 2008-06-09 20:27:19.000000000 +0200
47971 +++ avr32-2.6/include/asm-avr32/irq.h 2008-06-12 15:04:04.687816302 +0200
47972 @@ -14,6 +14,11 @@
47973 #ifndef __ASSEMBLER__
47974 int nmi_enable(void);
47975 void nmi_disable(void);
47976 +
47977 +/*
47978 + * Returns a bitmask of pending interrupts in a group.
47979 + */
47980 +extern unsigned long intc_get_pending(unsigned int group);
47981 #endif
47982
47983 #endif /* __ASM_AVR32_IOCTLS_H */
47984 diff --exclude=.git -urN linux-2.6.25.6/include/asm-avr32/page.h avr32-2.6/include/asm-avr32/page.h
47985 --- linux-2.6.25.6/include/asm-avr32/page.h 2008-06-09 20:27:19.000000000 +0200
47986 +++ avr32-2.6/include/asm-avr32/page.h 2008-06-12 15:04:04.691816253 +0200
47987 @@ -8,13 +8,11 @@
47988 #ifndef __ASM_AVR32_PAGE_H
47989 #define __ASM_AVR32_PAGE_H
47990
47991 +#include <linux/const.h>
47992 +
47993 /* PAGE_SHIFT determines the page size */
47994 #define PAGE_SHIFT 12
47995 -#ifdef __ASSEMBLY__
47996 -#define PAGE_SIZE (1 << PAGE_SHIFT)
47997 -#else
47998 -#define PAGE_SIZE (1UL << PAGE_SHIFT)
47999 -#endif
48000 +#define PAGE_SIZE (_AC(1, UL) << PAGE_SHIFT)
48001 #define PAGE_MASK (~(PAGE_SIZE-1))
48002 #define PTE_MASK PAGE_MASK
48003
48004 diff --exclude=.git -urN linux-2.6.25.6/include/asm-avr32/pci.h avr32-2.6/include/asm-avr32/pci.h
48005 --- linux-2.6.25.6/include/asm-avr32/pci.h 2008-06-09 20:27:19.000000000 +0200
48006 +++ avr32-2.6/include/asm-avr32/pci.h 2008-06-12 15:09:45.859816144 +0200
48007 @@ -5,4 +5,6 @@
48008
48009 #define PCI_DMA_BUS_IS_PHYS (1)
48010
48011 +#include <asm-generic/pci-dma-compat.h>
48012 +
48013 #endif /* __ASM_AVR32_PCI_H__ */
48014 diff --exclude=.git -urN linux-2.6.25.6/include/asm-avr32/serial.h avr32-2.6/include/asm-avr32/serial.h
48015 --- linux-2.6.25.6/include/asm-avr32/serial.h 1970-01-01 01:00:00.000000000 +0100
48016 +++ avr32-2.6/include/asm-avr32/serial.h 2008-06-12 15:04:04.695816483 +0200
48017 @@ -0,0 +1,13 @@
48018 +#ifndef _ASM_SERIAL_H
48019 +#define _ASM_SERIAL_H
48020 +
48021 +/*
48022 + * This assumes you have a 1.8432 MHz clock for your UART.
48023 + *
48024 + * It'd be nice if someone built a serial card with a 24.576 MHz
48025 + * clock, since the 16550A is capable of handling a top speed of 1.5
48026 + * megabits/second; but this requires the faster clock.
48027 + */
48028 +#define BASE_BAUD (1843200 / 16)
48029 +
48030 +#endif /* _ASM_SERIAL_H */
48031 diff --exclude=.git -urN linux-2.6.25.6/include/asm-avr32/thread_info.h avr32-2.6/include/asm-avr32/thread_info.h
48032 --- linux-2.6.25.6/include/asm-avr32/thread_info.h 2008-06-09 20:27:19.000000000 +0200
48033 +++ avr32-2.6/include/asm-avr32/thread_info.h 2008-06-12 15:09:45.859816144 +0200
48034 @@ -88,6 +88,7 @@
48035 #define TIF_MEMDIE 6
48036 #define TIF_RESTORE_SIGMASK 7 /* restore signal mask in do_signal */
48037 #define TIF_CPU_GOING_TO_SLEEP 8 /* CPU is entering sleep 0 mode */
48038 +#define TIF_FREEZE 29
48039 #define TIF_DEBUG 30 /* debugging enabled */
48040 #define TIF_USERSPACE 31 /* true if FS sets userspace */
48041
48042 diff --exclude=.git -urN linux-2.6.25.6/include/asm-avr32/xor.h avr32-2.6/include/asm-avr32/xor.h
48043 --- linux-2.6.25.6/include/asm-avr32/xor.h 1970-01-01 01:00:00.000000000 +0100
48044 +++ avr32-2.6/include/asm-avr32/xor.h 2008-06-12 15:04:04.707817453 +0200
48045 @@ -0,0 +1,6 @@
48046 +#ifndef _ASM_XOR_H
48047 +#define _ASM_XOR_H
48048 +
48049 +#include <asm-generic/xor.h>
48050 +
48051 +#endif
48052 diff --exclude=.git -urN linux-2.6.25.6/include/linux/atmel_tc.h avr32-2.6/include/linux/atmel_tc.h
48053 --- linux-2.6.25.6/include/linux/atmel_tc.h 1970-01-01 01:00:00.000000000 +0100
48054 +++ avr32-2.6/include/linux/atmel_tc.h 2008-06-12 15:04:07.586819080 +0200
48055 @@ -0,0 +1,252 @@
48056 +/*
48057 + * Timer/Counter Unit (TC) registers.
48058 + *
48059 + * This program is free software; you can redistribute it and/or modify
48060 + * it under the terms of the GNU General Public License as published by
48061 + * the Free Software Foundation; either version 2 of the License, or
48062 + * (at your option) any later version.
48063 + */
48064 +
48065 +#ifndef ATMEL_TC_H
48066 +#define ATMEL_TC_H
48067 +
48068 +#include <linux/compiler.h>
48069 +#include <linux/list.h>
48070 +
48071 +/*
48072 + * Many 32-bit Atmel SOCs include one or more TC blocks, each of which holds
48073 + * three general-purpose 16-bit timers. These timers share one register bank.
48074 + * Depending on the SOC, each timer may have its own clock and IRQ, or those
48075 + * may be shared by the whole TC block.
48076 + *
48077 + * These TC blocks may have up to nine external pins: TCLK0..2 signals for
48078 + * clocks or clock gates, and per-timer TIOA and TIOB signals used for PWM
48079 + * or triggering. Those pins need to be set up for use with the TC block,
48080 + * else they will be used as GPIOs or for a different controller.
48081 + *
48082 + * Although we expect each TC block to have a platform_device node, those
48083 + * nodes are not what drivers bind to. Instead, they ask for a specific
48084 + * TC block, by number ... which is a common approach on systems with many
48085 + * timers. Then they use clk_get() and platform_get_irq() to get clock and
48086 + * IRQ resources.
48087 + */
48088 +
48089 +struct clk;
48090 +
48091 +/**
48092 + * struct atmel_tc - information about a Timer/Counter Block
48093 + * @pdev: physical device
48094 + * @iomem: resource associated with the I/O register
48095 + * @regs: mapping through which the I/O registers can be accessed
48096 + * @irq: irq for each of the three channels
48097 + * @clk: internal clock source for each of the three channels
48098 + * @node: list node, for tclib internal use
48099 + *
48100 + * On some platforms, each TC channel has its own clocks and IRQs,
48101 + * while on others, all TC channels share the same clock and IRQ.
48102 + * Drivers should clk_enable() all the clocks they need even though
48103 + * all the entries in @clk may point to the same physical clock.
48104 + * Likewise, drivers should request irqs independently for each
48105 + * channel, but they must use IRQF_SHARED in case some of the entries
48106 + * in @irq are actually the same IRQ.
48107 + */
48108 +struct atmel_tc {
48109 + struct platform_device *pdev;
48110 + struct resource *iomem;
48111 + void __iomem *regs;
48112 + int irq[3];
48113 + struct clk *clk[3];
48114 + struct list_head node;
48115 +};
48116 +
48117 +extern struct atmel_tc *atmel_tc_alloc(unsigned block, const char *name);
48118 +extern void atmel_tc_free(struct atmel_tc *tc);
48119 +
48120 +/* platform-specific ATMEL_TC_TIMER_CLOCKx divisors (0 means 32KiHz) */
48121 +extern const u8 atmel_tc_divisors[5];
48122 +
48123 +
48124 +/*
48125 + * Two registers have block-wide controls. These are: configuring the three
48126 + * "external" clocks (or event sources) used by the timer channels; and
48127 + * synchronizing the timers by resetting them all at once.
48128 + *
48129 + * "External" can mean "external to chip" using the TCLK0, TCLK1, or TCLK2
48130 + * signals. Or, it can mean "external to timer", using the TIOA output from
48131 + * one of the other two timers that's being run in waveform mode.
48132 + */
48133 +
48134 +#define ATMEL_TC_BCR 0xc0 /* TC Block Control Register */
48135 +#define ATMEL_TC_SYNC (1 << 0) /* synchronize timers */
48136 +
48137 +#define ATMEL_TC_BMR 0xc4 /* TC Block Mode Register */
48138 +#define ATMEL_TC_TC0XC0S (3 << 0) /* external clock 0 source */
48139 +#define ATMEL_TC_TC0XC0S_TCLK0 (0 << 0)
48140 +#define ATMEL_TC_TC0XC0S_NONE (1 << 0)
48141 +#define ATMEL_TC_TC0XC0S_TIOA1 (2 << 0)
48142 +#define ATMEL_TC_TC0XC0S_TIOA2 (3 << 0)
48143 +#define ATMEL_TC_TC1XC1S (3 << 2) /* external clock 1 source */
48144 +#define ATMEL_TC_TC1XC1S_TCLK1 (0 << 2)
48145 +#define ATMEL_TC_TC1XC1S_NONE (1 << 2)
48146 +#define ATMEL_TC_TC1XC1S_TIOA0 (2 << 2)
48147 +#define ATMEL_TC_TC1XC1S_TIOA2 (3 << 2)
48148 +#define ATMEL_TC_TC2XC2S (3 << 4) /* external clock 2 source */
48149 +#define ATMEL_TC_TC2XC2S_TCLK2 (0 << 4)
48150 +#define ATMEL_TC_TC2XC2S_NONE (1 << 4)
48151 +#define ATMEL_TC_TC2XC2S_TIOA0 (2 << 4)
48152 +#define ATMEL_TC_TC2XC2S_TIOA1 (3 << 4)
48153 +
48154 +
48155 +/*
48156 + * Each TC block has three "channels", each with one counter and controls.
48157 + *
48158 + * Note that the semantics of ATMEL_TC_TIMER_CLOCKx (input clock selection
48159 + * when it's not "external") is silicon-specific. AT91 platforms use one
48160 + * set of definitions; AVR32 platforms use a different set. Don't hard-wire
48161 + * such knowledge into your code, use the global "atmel_tc_divisors" ...
48162 + * where index N is the divisor for clock N+1, else zero to indicate it uses
48163 + * the 32 KiHz clock.
48164 + *
48165 + * The timers can be chained in various ways, and operated in "waveform"
48166 + * generation mode (including PWM) or "capture" mode (to time events). In
48167 + * both modes, behavior can be configured in many ways.
48168 + *
48169 + * Each timer has two I/O pins, TIOA and TIOB. Waveform mode uses TIOA as a
48170 + * PWM output, and TIOB as either another PWM or as a trigger. Capture mode
48171 + * uses them only as inputs.
48172 + */
48173 +#define ATMEL_TC_CHAN(idx) ((idx)*0x40)
48174 +#define ATMEL_TC_REG(idx, reg) (ATMEL_TC_CHAN(idx) + ATMEL_TC_ ## reg)
48175 +
48176 +#define ATMEL_TC_CCR 0x00 /* Channel Control Register */
48177 +#define ATMEL_TC_CLKEN (1 << 0) /* clock enable */
48178 +#define ATMEL_TC_CLKDIS (1 << 1) /* clock disable */
48179 +#define ATMEL_TC_SWTRG (1 << 2) /* software trigger */
48180 +
48181 +#define ATMEL_TC_CMR 0x04 /* Channel Mode Register */
48182 +
48183 +/* Both modes share some CMR bits */
48184 +#define ATMEL_TC_TCCLKS (7 << 0) /* clock source */
48185 +#define ATMEL_TC_TIMER_CLOCK1 (0 << 0)
48186 +#define ATMEL_TC_TIMER_CLOCK2 (1 << 0)
48187 +#define ATMEL_TC_TIMER_CLOCK3 (2 << 0)
48188 +#define ATMEL_TC_TIMER_CLOCK4 (3 << 0)
48189 +#define ATMEL_TC_TIMER_CLOCK5 (4 << 0)
48190 +#define ATMEL_TC_XC0 (5 << 0)
48191 +#define ATMEL_TC_XC1 (6 << 0)
48192 +#define ATMEL_TC_XC2 (7 << 0)
48193 +#define ATMEL_TC_CLKI (1 << 3) /* clock invert */
48194 +#define ATMEL_TC_BURST (3 << 4) /* clock gating */
48195 +#define ATMEL_TC_GATE_NONE (0 << 4)
48196 +#define ATMEL_TC_GATE_XC0 (1 << 4)
48197 +#define ATMEL_TC_GATE_XC1 (2 << 4)
48198 +#define ATMEL_TC_GATE_XC2 (3 << 4)
48199 +#define ATMEL_TC_WAVE (1 << 15) /* true = Waveform mode */
48200 +
48201 +/* CAPTURE mode CMR bits */
48202 +#define ATMEL_TC_LDBSTOP (1 << 6) /* counter stops on RB load */
48203 +#define ATMEL_TC_LDBDIS (1 << 7) /* counter disable on RB load */
48204 +#define ATMEL_TC_ETRGEDG (3 << 8) /* external trigger edge */
48205 +#define ATMEL_TC_ETRGEDG_NONE (0 << 8)
48206 +#define ATMEL_TC_ETRGEDG_RISING (1 << 8)
48207 +#define ATMEL_TC_ETRGEDG_FALLING (2 << 8)
48208 +#define ATMEL_TC_ETRGEDG_BOTH (3 << 8)
48209 +#define ATMEL_TC_ABETRG (1 << 10) /* external trigger is TIOA? */
48210 +#define ATMEL_TC_CPCTRG (1 << 14) /* RC compare trigger enable */
48211 +#define ATMEL_TC_LDRA (3 << 16) /* RA loading edge (of TIOA) */
48212 +#define ATMEL_TC_LDRA_NONE (0 << 16)
48213 +#define ATMEL_TC_LDRA_RISING (1 << 16)
48214 +#define ATMEL_TC_LDRA_FALLING (2 << 16)
48215 +#define ATMEL_TC_LDRA_BOTH (3 << 16)
48216 +#define ATMEL_TC_LDRB (3 << 18) /* RB loading edge (of TIOA) */
48217 +#define ATMEL_TC_LDRB_NONE (0 << 18)
48218 +#define ATMEL_TC_LDRB_RISING (1 << 18)
48219 +#define ATMEL_TC_LDRB_FALLING (2 << 18)
48220 +#define ATMEL_TC_LDRB_BOTH (3 << 18)
48221 +
48222 +/* WAVEFORM mode CMR bits */
48223 +#define ATMEL_TC_CPCSTOP (1 << 6) /* RC compare stops counter */
48224 +#define ATMEL_TC_CPCDIS (1 << 7) /* RC compare disables counter */
48225 +#define ATMEL_TC_EEVTEDG (3 << 8) /* external event edge */
48226 +#define ATMEL_TC_EEVTEDG_NONE (0 << 8)
48227 +#define ATMEL_TC_EEVTEDG_RISING (1 << 8)
48228 +#define ATMEL_TC_EEVTEDG_FALLING (2 << 8)
48229 +#define ATMEL_TC_EEVTEDG_BOTH (3 << 8)
48230 +#define ATMEL_TC_EEVT (3 << 10) /* external event source */
48231 +#define ATMEL_TC_EEVT_TIOB (0 << 10)
48232 +#define ATMEL_TC_EEVT_XC0 (1 << 10)
48233 +#define ATMEL_TC_EEVT_XC1 (2 << 10)
48234 +#define ATMEL_TC_EEVT_XC2 (3 << 10)
48235 +#define ATMEL_TC_ENETRG (1 << 12) /* external event is trigger */
48236 +#define ATMEL_TC_WAVESEL (3 << 13) /* waveform type */
48237 +#define ATMEL_TC_WAVESEL_UP (0 << 13)
48238 +#define ATMEL_TC_WAVESEL_UPDOWN (1 << 13)
48239 +#define ATMEL_TC_WAVESEL_UP_AUTO (2 << 13)
48240 +#define ATMEL_TC_WAVESEL_UPDOWN_AUTO (3 << 13)
48241 +#define ATMEL_TC_ACPA (3 << 16) /* RA compare changes TIOA */
48242 +#define ATMEL_TC_ACPA_NONE (0 << 16)
48243 +#define ATMEL_TC_ACPA_SET (1 << 16)
48244 +#define ATMEL_TC_ACPA_CLEAR (2 << 16)
48245 +#define ATMEL_TC_ACPA_TOGGLE (3 << 16)
48246 +#define ATMEL_TC_ACPC (3 << 18) /* RC compare changes TIOA */
48247 +#define ATMEL_TC_ACPC_NONE (0 << 18)
48248 +#define ATMEL_TC_ACPC_SET (1 << 18)
48249 +#define ATMEL_TC_ACPC_CLEAR (2 << 18)
48250 +#define ATMEL_TC_ACPC_TOGGLE (3 << 18)
48251 +#define ATMEL_TC_AEEVT (3 << 20) /* external event changes TIOA */
48252 +#define ATMEL_TC_AEEVT_NONE (0 << 20)
48253 +#define ATMEL_TC_AEEVT_SET (1 << 20)
48254 +#define ATMEL_TC_AEEVT_CLEAR (2 << 20)
48255 +#define ATMEL_TC_AEEVT_TOGGLE (3 << 20)
48256 +#define ATMEL_TC_ASWTRG (3 << 22) /* software trigger changes TIOA */
48257 +#define ATMEL_TC_ASWTRG_NONE (0 << 22)
48258 +#define ATMEL_TC_ASWTRG_SET (1 << 22)
48259 +#define ATMEL_TC_ASWTRG_CLEAR (2 << 22)
48260 +#define ATMEL_TC_ASWTRG_TOGGLE (3 << 22)
48261 +#define ATMEL_TC_BCPB (3 << 24) /* RB compare changes TIOB */
48262 +#define ATMEL_TC_BCPB_NONE (0 << 24)
48263 +#define ATMEL_TC_BCPB_SET (1 << 24)
48264 +#define ATMEL_TC_BCPB_CLEAR (2 << 24)
48265 +#define ATMEL_TC_BCPB_TOGGLE (3 << 24)
48266 +#define ATMEL_TC_BCPC (3 << 26) /* RC compare changes TIOB */
48267 +#define ATMEL_TC_BCPC_NONE (0 << 26)
48268 +#define ATMEL_TC_BCPC_SET (1 << 26)
48269 +#define ATMEL_TC_BCPC_CLEAR (2 << 26)
48270 +#define ATMEL_TC_BCPC_TOGGLE (3 << 26)
48271 +#define ATMEL_TC_BEEVT (3 << 28) /* external event changes TIOB */
48272 +#define ATMEL_TC_BEEVT_NONE (0 << 28)
48273 +#define ATMEL_TC_BEEVT_SET (1 << 28)
48274 +#define ATMEL_TC_BEEVT_CLEAR (2 << 28)
48275 +#define ATMEL_TC_BEEVT_TOGGLE (3 << 28)
48276 +#define ATMEL_TC_BSWTRG (3 << 30) /* software trigger changes TIOB */
48277 +#define ATMEL_TC_BSWTRG_NONE (0 << 30)
48278 +#define ATMEL_TC_BSWTRG_SET (1 << 30)
48279 +#define ATMEL_TC_BSWTRG_CLEAR (2 << 30)
48280 +#define ATMEL_TC_BSWTRG_TOGGLE (3 << 30)
48281 +
48282 +#define ATMEL_TC_CV 0x10 /* counter Value */
48283 +#define ATMEL_TC_RA 0x14 /* register A */
48284 +#define ATMEL_TC_RB 0x18 /* register B */
48285 +#define ATMEL_TC_RC 0x1c /* register C */
48286 +
48287 +#define ATMEL_TC_SR 0x20 /* status (read-only) */
48288 +/* Status-only flags */
48289 +#define ATMEL_TC_CLKSTA (1 << 16) /* clock enabled */
48290 +#define ATMEL_TC_MTIOA (1 << 17) /* TIOA mirror */
48291 +#define ATMEL_TC_MTIOB (1 << 18) /* TIOB mirror */
48292 +
48293 +#define ATMEL_TC_IER 0x24 /* interrupt enable (write-only) */
48294 +#define ATMEL_TC_IDR 0x28 /* interrupt disable (write-only) */
48295 +#define ATMEL_TC_IMR 0x2c /* interrupt mask (read-only) */
48296 +
48297 +/* Status and IRQ flags */
48298 +#define ATMEL_TC_COVFS (1 << 0) /* counter overflow */
48299 +#define ATMEL_TC_LOVRS (1 << 1) /* load overrun */
48300 +#define ATMEL_TC_CPAS (1 << 2) /* RA compare */
48301 +#define ATMEL_TC_CPBS (1 << 3) /* RB compare */
48302 +#define ATMEL_TC_CPCS (1 << 4) /* RC compare */
48303 +#define ATMEL_TC_LDRAS (1 << 5) /* RA loading */
48304 +#define ATMEL_TC_LDRBS (1 << 6) /* RB loading */
48305 +#define ATMEL_TC_ETRGS (1 << 7) /* external trigger */
48306 +
48307 +#endif
48308 diff --exclude=.git -urN linux-2.6.25.6/include/linux/fs.h avr32-2.6/include/linux/fs.h
48309 --- linux-2.6.25.6/include/linux/fs.h 2008-06-09 20:27:19.000000000 +0200
48310 +++ avr32-2.6/include/linux/fs.h 2008-06-12 15:09:46.255815187 +0200
48311 @@ -1691,6 +1691,8 @@
48312 extern int invalidate_inode_pages2(struct address_space *mapping);
48313 extern int invalidate_inode_pages2_range(struct address_space *mapping,
48314 pgoff_t start, pgoff_t end);
48315 +extern void generic_sync_sb_inodes(struct super_block *sb,
48316 + struct writeback_control *wbc);
48317 extern int write_inode_now(struct inode *, int);
48318 extern int filemap_fdatawrite(struct address_space *);
48319 extern int filemap_flush(struct address_space *);
48320 diff --exclude=.git -urN linux-2.6.25.6/include/linux/usb/atmel_usba_udc.h avr32-2.6/include/linux/usb/atmel_usba_udc.h
48321 --- linux-2.6.25.6/include/linux/usb/atmel_usba_udc.h 1970-01-01 01:00:00.000000000 +0100
48322 +++ avr32-2.6/include/linux/usb/atmel_usba_udc.h 2008-06-12 15:04:07.986816119 +0200
48323 @@ -0,0 +1,22 @@
48324 +/*
48325 + * Platform data definitions for Atmel USBA gadget driver.
48326 + */
48327 +#ifndef __LINUX_USB_USBA_H
48328 +#define __LINUX_USB_USBA_H
48329 +
48330 +struct usba_ep_data {
48331 + char *name;
48332 + int index;
48333 + int fifo_size;
48334 + int nr_banks;
48335 + int can_dma;
48336 + int can_isoc;
48337 +};
48338 +
48339 +struct usba_platform_data {
48340 + int vbus_pin;
48341 + int num_ep;
48342 + struct usba_ep_data ep[0];
48343 +};
48344 +
48345 +#endif /* __LINUX_USB_USBA_H */
48346 diff --exclude=.git -urN linux-2.6.25.6/include/mtd/Kbuild avr32-2.6/include/mtd/Kbuild
48347 --- linux-2.6.25.6/include/mtd/Kbuild 2008-06-09 20:27:19.000000000 +0200
48348 +++ avr32-2.6/include/mtd/Kbuild 2008-06-12 15:04:08.018816005 +0200
48349 @@ -3,5 +3,4 @@
48350 header-y += mtd-abi.h
48351 header-y += mtd-user.h
48352 header-y += nftl-user.h
48353 -header-y += ubi-header.h
48354 header-y += ubi-user.h
48355 diff --exclude=.git -urN linux-2.6.25.6/include/mtd/ubi-header.h avr32-2.6/include/mtd/ubi-header.h
48356 --- linux-2.6.25.6/include/mtd/ubi-header.h 2008-06-09 20:27:19.000000000 +0200
48357 +++ avr32-2.6/include/mtd/ubi-header.h 1970-01-01 01:00:00.000000000 +0100
48358 @@ -1,372 +0,0 @@
48359 -/*
48360 - * Copyright (c) International Business Machines Corp., 2006
48361 - *
48362 - * This program is free software; you can redistribute it and/or modify
48363 - * it under the terms of the GNU General Public License as published by
48364 - * the Free Software Foundation; either version 2 of the License, or
48365 - * (at your option) any later version.
48366 - *
48367 - * This program is distributed in the hope that it will be useful,
48368 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
48369 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
48370 - * the GNU General Public License for more details.
48371 - *
48372 - * You should have received a copy of the GNU General Public License
48373 - * along with this program; if not, write to the Free Software
48374 - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
48375 - *
48376 - * Authors: Artem Bityutskiy (Битюцкий Артём)
48377 - * Thomas Gleixner
48378 - * Frank Haverkamp
48379 - * Oliver Lohmann
48380 - * Andreas Arnez
48381 - */
48382 -
48383 -/*
48384 - * This file defines the layout of UBI headers and all the other UBI on-flash
48385 - * data structures. May be included by user-space.
48386 - */
48387 -
48388 -#ifndef __UBI_HEADER_H__
48389 -#define __UBI_HEADER_H__
48390 -
48391 -#include <asm/byteorder.h>
48392 -
48393 -/* The version of UBI images supported by this implementation */
48394 -#define UBI_VERSION 1
48395 -
48396 -/* The highest erase counter value supported by this implementation */
48397 -#define UBI_MAX_ERASECOUNTER 0x7FFFFFFF
48398 -
48399 -/* The initial CRC32 value used when calculating CRC checksums */
48400 -#define UBI_CRC32_INIT 0xFFFFFFFFU
48401 -
48402 -/* Erase counter header magic number (ASCII "UBI#") */
48403 -#define UBI_EC_HDR_MAGIC 0x55424923
48404 -/* Volume identifier header magic number (ASCII "UBI!") */
48405 -#define UBI_VID_HDR_MAGIC 0x55424921
48406 -
48407 -/*
48408 - * Volume type constants used in the volume identifier header.
48409 - *
48410 - * @UBI_VID_DYNAMIC: dynamic volume
48411 - * @UBI_VID_STATIC: static volume
48412 - */
48413 -enum {
48414 - UBI_VID_DYNAMIC = 1,
48415 - UBI_VID_STATIC = 2
48416 -};
48417 -
48418 -/*
48419 - * Volume flags used in the volume table record.
48420 - *
48421 - * @UBI_VTBL_AUTORESIZE_FLG: auto-resize this volume
48422 - *
48423 - * %UBI_VTBL_AUTORESIZE_FLG flag can be set only for one volume in the volume
48424 - * table. UBI automatically re-sizes the volume which has this flag and makes
48425 - * the volume to be of largest possible size. This means that if after the
48426 - * initialization UBI finds out that there are available physical eraseblocks
48427 - * present on the device, it automatically appends all of them to the volume
48428 - * (the physical eraseblocks reserved for bad eraseblocks handling and other
48429 - * reserved physical eraseblocks are not taken). So, if there is a volume with
48430 - * the %UBI_VTBL_AUTORESIZE_FLG flag set, the amount of available logical
48431 - * eraseblocks will be zero after UBI is loaded, because all of them will be
48432 - * reserved for this volume. Note, the %UBI_VTBL_AUTORESIZE_FLG bit is cleared
48433 - * after the volume had been initialized.
48434 - *
48435 - * The auto-resize feature is useful for device production purposes. For
48436 - * example, different NAND flash chips may have different amount of initial bad
48437 - * eraseblocks, depending of particular chip instance. Manufacturers of NAND
48438 - * chips usually guarantee that the amount of initial bad eraseblocks does not
48439 - * exceed certain percent, e.g. 2%. When one creates an UBI image which will be
48440 - * flashed to the end devices in production, he does not know the exact amount
48441 - * of good physical eraseblocks the NAND chip on the device will have, but this
48442 - * number is required to calculate the volume sized and put them to the volume
48443 - * table of the UBI image. In this case, one of the volumes (e.g., the one
48444 - * which will store the root file system) is marked as "auto-resizable", and
48445 - * UBI will adjust its size on the first boot if needed.
48446 - *
48447 - * Note, first UBI reserves some amount of physical eraseblocks for bad
48448 - * eraseblock handling, and then re-sizes the volume, not vice-versa. This
48449 - * means that the pool of reserved physical eraseblocks will always be present.
48450 - */
48451 -enum {
48452 - UBI_VTBL_AUTORESIZE_FLG = 0x01,
48453 -};
48454 -
48455 -/*
48456 - * Compatibility constants used by internal volumes.
48457 - *
48458 - * @UBI_COMPAT_DELETE: delete this internal volume before anything is written
48459 - * to the flash
48460 - * @UBI_COMPAT_RO: attach this device in read-only mode
48461 - * @UBI_COMPAT_PRESERVE: preserve this internal volume - do not touch its
48462 - * physical eraseblocks, don't allow the wear-leveling unit to move them
48463 - * @UBI_COMPAT_REJECT: reject this UBI image
48464 - */
48465 -enum {
48466 - UBI_COMPAT_DELETE = 1,
48467 - UBI_COMPAT_RO = 2,
48468 - UBI_COMPAT_PRESERVE = 4,
48469 - UBI_COMPAT_REJECT = 5
48470 -};
48471 -
48472 -/* Sizes of UBI headers */
48473 -#define UBI_EC_HDR_SIZE sizeof(struct ubi_ec_hdr)
48474 -#define UBI_VID_HDR_SIZE sizeof(struct ubi_vid_hdr)
48475 -
48476 -/* Sizes of UBI headers without the ending CRC */
48477 -#define UBI_EC_HDR_SIZE_CRC (UBI_EC_HDR_SIZE - sizeof(__be32))
48478 -#define UBI_VID_HDR_SIZE_CRC (UBI_VID_HDR_SIZE - sizeof(__be32))
48479 -
48480 -/**
48481 - * struct ubi_ec_hdr - UBI erase counter header.
48482 - * @magic: erase counter header magic number (%UBI_EC_HDR_MAGIC)
48483 - * @version: version of UBI implementation which is supposed to accept this
48484 - * UBI image
48485 - * @padding1: reserved for future, zeroes
48486 - * @ec: the erase counter
48487 - * @vid_hdr_offset: where the VID header starts
48488 - * @data_offset: where the user data start
48489 - * @padding2: reserved for future, zeroes
48490 - * @hdr_crc: erase counter header CRC checksum
48491 - *
48492 - * The erase counter header takes 64 bytes and has a plenty of unused space for
48493 - * future usage. The unused fields are zeroed. The @version field is used to
48494 - * indicate the version of UBI implementation which is supposed to be able to
48495 - * work with this UBI image. If @version is greater then the current UBI
48496 - * version, the image is rejected. This may be useful in future if something
48497 - * is changed radically. This field is duplicated in the volume identifier
48498 - * header.
48499 - *
48500 - * The @vid_hdr_offset and @data_offset fields contain the offset of the the
48501 - * volume identifier header and user data, relative to the beginning of the
48502 - * physical eraseblock. These values have to be the same for all physical
48503 - * eraseblocks.
48504 - */
48505 -struct ubi_ec_hdr {
48506 - __be32 magic;
48507 - __u8 version;
48508 - __u8 padding1[3];
48509 - __be64 ec; /* Warning: the current limit is 31-bit anyway! */
48510 - __be32 vid_hdr_offset;
48511 - __be32 data_offset;
48512 - __u8 padding2[36];
48513 - __be32 hdr_crc;
48514 -} __attribute__ ((packed));
48515 -
48516 -/**
48517 - * struct ubi_vid_hdr - on-flash UBI volume identifier header.
48518 - * @magic: volume identifier header magic number (%UBI_VID_HDR_MAGIC)
48519 - * @version: UBI implementation version which is supposed to accept this UBI
48520 - * image (%UBI_VERSION)
48521 - * @vol_type: volume type (%UBI_VID_DYNAMIC or %UBI_VID_STATIC)
48522 - * @copy_flag: if this logical eraseblock was copied from another physical
48523 - * eraseblock (for wear-leveling reasons)
48524 - * @compat: compatibility of this volume (%0, %UBI_COMPAT_DELETE,
48525 - * %UBI_COMPAT_IGNORE, %UBI_COMPAT_PRESERVE, or %UBI_COMPAT_REJECT)
48526 - * @vol_id: ID of this volume
48527 - * @lnum: logical eraseblock number
48528 - * @leb_ver: version of this logical eraseblock (IMPORTANT: obsolete, to be
48529 - * removed, kept only for not breaking older UBI users)
48530 - * @data_size: how many bytes of data this logical eraseblock contains
48531 - * @used_ebs: total number of used logical eraseblocks in this volume
48532 - * @data_pad: how many bytes at the end of this physical eraseblock are not
48533 - * used
48534 - * @data_crc: CRC checksum of the data stored in this logical eraseblock
48535 - * @padding1: reserved for future, zeroes
48536 - * @sqnum: sequence number
48537 - * @padding2: reserved for future, zeroes
48538 - * @hdr_crc: volume identifier header CRC checksum
48539 - *
48540 - * The @sqnum is the value of the global sequence counter at the time when this
48541 - * VID header was created. The global sequence counter is incremented each time
48542 - * UBI writes a new VID header to the flash, i.e. when it maps a logical
48543 - * eraseblock to a new physical eraseblock. The global sequence counter is an
48544 - * unsigned 64-bit integer and we assume it never overflows. The @sqnum
48545 - * (sequence number) is used to distinguish between older and newer versions of
48546 - * logical eraseblocks.
48547 - *
48548 - * There are 2 situations when there may be more then one physical eraseblock
48549 - * corresponding to the same logical eraseblock, i.e., having the same @vol_id
48550 - * and @lnum values in the volume identifier header. Suppose we have a logical
48551 - * eraseblock L and it is mapped to the physical eraseblock P.
48552 - *
48553 - * 1. Because UBI may erase physical eraseblocks asynchronously, the following
48554 - * situation is possible: L is asynchronously erased, so P is scheduled for
48555 - * erasure, then L is written to,i.e. mapped to another physical eraseblock P1,
48556 - * so P1 is written to, then an unclean reboot happens. Result - there are 2
48557 - * physical eraseblocks P and P1 corresponding to the same logical eraseblock
48558 - * L. But P1 has greater sequence number, so UBI picks P1 when it attaches the
48559 - * flash.
48560 - *
48561 - * 2. From time to time UBI moves logical eraseblocks to other physical
48562 - * eraseblocks for wear-leveling reasons. If, for example, UBI moves L from P
48563 - * to P1, and an unclean reboot happens before P is physically erased, there
48564 - * are two physical eraseblocks P and P1 corresponding to L and UBI has to
48565 - * select one of them when the flash is attached. The @sqnum field says which
48566 - * PEB is the original (obviously P will have lower @sqnum) and the copy. But
48567 - * it is not enough to select the physical eraseblock with the higher sequence
48568 - * number, because the unclean reboot could have happen in the middle of the
48569 - * copying process, so the data in P is corrupted. It is also not enough to
48570 - * just select the physical eraseblock with lower sequence number, because the
48571 - * data there may be old (consider a case if more data was added to P1 after
48572 - * the copying). Moreover, the unclean reboot may happen when the erasure of P
48573 - * was just started, so it result in unstable P, which is "mostly" OK, but
48574 - * still has unstable bits.
48575 - *
48576 - * UBI uses the @copy_flag field to indicate that this logical eraseblock is a
48577 - * copy. UBI also calculates data CRC when the data is moved and stores it at
48578 - * the @data_crc field of the copy (P1). So when UBI needs to pick one physical
48579 - * eraseblock of two (P or P1), the @copy_flag of the newer one (P1) is
48580 - * examined. If it is cleared, the situation* is simple and the newer one is
48581 - * picked. If it is set, the data CRC of the copy (P1) is examined. If the CRC
48582 - * checksum is correct, this physical eraseblock is selected (P1). Otherwise
48583 - * the older one (P) is selected.
48584 - *
48585 - * Note, there is an obsolete @leb_ver field which was used instead of @sqnum
48586 - * in the past. But it is not used anymore and we keep it in order to be able
48587 - * to deal with old UBI images. It will be removed at some point.
48588 - *
48589 - * There are 2 sorts of volumes in UBI: user volumes and internal volumes.
48590 - * Internal volumes are not seen from outside and are used for various internal
48591 - * UBI purposes. In this implementation there is only one internal volume - the
48592 - * layout volume. Internal volumes are the main mechanism of UBI extensions.
48593 - * For example, in future one may introduce a journal internal volume. Internal
48594 - * volumes have their own reserved range of IDs.
48595 - *
48596 - * The @compat field is only used for internal volumes and contains the "degree
48597 - * of their compatibility". It is always zero for user volumes. This field
48598 - * provides a mechanism to introduce UBI extensions and to be still compatible
48599 - * with older UBI binaries. For example, if someone introduced a journal in
48600 - * future, he would probably use %UBI_COMPAT_DELETE compatibility for the
48601 - * journal volume. And in this case, older UBI binaries, which know nothing
48602 - * about the journal volume, would just delete this volume and work perfectly
48603 - * fine. This is similar to what Ext2fs does when it is fed by an Ext3fs image
48604 - * - it just ignores the Ext3fs journal.
48605 - *
48606 - * The @data_crc field contains the CRC checksum of the contents of the logical
48607 - * eraseblock if this is a static volume. In case of dynamic volumes, it does
48608 - * not contain the CRC checksum as a rule. The only exception is when the
48609 - * data of the physical eraseblock was moved by the wear-leveling unit, then
48610 - * the wear-leveling unit calculates the data CRC and stores it in the
48611 - * @data_crc field. And of course, the @copy_flag is %in this case.
48612 - *
48613 - * The @data_size field is used only for static volumes because UBI has to know
48614 - * how many bytes of data are stored in this eraseblock. For dynamic volumes,
48615 - * this field usually contains zero. The only exception is when the data of the
48616 - * physical eraseblock was moved to another physical eraseblock for
48617 - * wear-leveling reasons. In this case, UBI calculates CRC checksum of the
48618 - * contents and uses both @data_crc and @data_size fields. In this case, the
48619 - * @data_size field contains data size.
48620 - *
48621 - * The @used_ebs field is used only for static volumes and indicates how many
48622 - * eraseblocks the data of the volume takes. For dynamic volumes this field is
48623 - * not used and always contains zero.
48624 - *
48625 - * The @data_pad is calculated when volumes are created using the alignment
48626 - * parameter. So, effectively, the @data_pad field reduces the size of logical
48627 - * eraseblocks of this volume. This is very handy when one uses block-oriented
48628 - * software (say, cramfs) on top of the UBI volume.
48629 - */
48630 -struct ubi_vid_hdr {
48631 - __be32 magic;
48632 - __u8 version;
48633 - __u8 vol_type;
48634 - __u8 copy_flag;
48635 - __u8 compat;
48636 - __be32 vol_id;
48637 - __be32 lnum;
48638 - __be32 leb_ver; /* obsolete, to be removed, don't use */
48639 - __be32 data_size;
48640 - __be32 used_ebs;
48641 - __be32 data_pad;
48642 - __be32 data_crc;
48643 - __u8 padding1[4];
48644 - __be64 sqnum;
48645 - __u8 padding2[12];
48646 - __be32 hdr_crc;
48647 -} __attribute__ ((packed));
48648 -
48649 -/* Internal UBI volumes count */
48650 -#define UBI_INT_VOL_COUNT 1
48651 -
48652 -/*
48653 - * Starting ID of internal volumes. There is reserved room for 4096 internal
48654 - * volumes.
48655 - */
48656 -#define UBI_INTERNAL_VOL_START (0x7FFFFFFF - 4096)
48657 -
48658 -/* The layout volume contains the volume table */
48659 -
48660 -#define UBI_LAYOUT_VOLUME_ID UBI_INTERNAL_VOL_START
48661 -#define UBI_LAYOUT_VOLUME_TYPE UBI_VID_DYNAMIC
48662 -#define UBI_LAYOUT_VOLUME_ALIGN 1
48663 -#define UBI_LAYOUT_VOLUME_EBS 2
48664 -#define UBI_LAYOUT_VOLUME_NAME "layout volume"
48665 -#define UBI_LAYOUT_VOLUME_COMPAT UBI_COMPAT_REJECT
48666 -
48667 -/* The maximum number of volumes per one UBI device */
48668 -#define UBI_MAX_VOLUMES 128
48669 -
48670 -/* The maximum volume name length */
48671 -#define UBI_VOL_NAME_MAX 127
48672 -
48673 -/* Size of the volume table record */
48674 -#define UBI_VTBL_RECORD_SIZE sizeof(struct ubi_vtbl_record)
48675 -
48676 -/* Size of the volume table record without the ending CRC */
48677 -#define UBI_VTBL_RECORD_SIZE_CRC (UBI_VTBL_RECORD_SIZE - sizeof(__be32))
48678 -
48679 -/**
48680 - * struct ubi_vtbl_record - a record in the volume table.
48681 - * @reserved_pebs: how many physical eraseblocks are reserved for this volume
48682 - * @alignment: volume alignment
48683 - * @data_pad: how many bytes are unused at the end of the each physical
48684 - * eraseblock to satisfy the requested alignment
48685 - * @vol_type: volume type (%UBI_DYNAMIC_VOLUME or %UBI_STATIC_VOLUME)
48686 - * @upd_marker: if volume update was started but not finished
48687 - * @name_len: volume name length
48688 - * @name: the volume name
48689 - * @flags: volume flags (%UBI_VTBL_AUTORESIZE_FLG)
48690 - * @padding: reserved, zeroes
48691 - * @crc: a CRC32 checksum of the record
48692 - *
48693 - * The volume table records are stored in the volume table, which is stored in
48694 - * the layout volume. The layout volume consists of 2 logical eraseblock, each
48695 - * of which contains a copy of the volume table (i.e., the volume table is
48696 - * duplicated). The volume table is an array of &struct ubi_vtbl_record
48697 - * objects indexed by the volume ID.
48698 - *
48699 - * If the size of the logical eraseblock is large enough to fit
48700 - * %UBI_MAX_VOLUMES records, the volume table contains %UBI_MAX_VOLUMES
48701 - * records. Otherwise, it contains as many records as it can fit (i.e., size of
48702 - * logical eraseblock divided by sizeof(struct ubi_vtbl_record)).
48703 - *
48704 - * The @upd_marker flag is used to implement volume update. It is set to %1
48705 - * before update and set to %0 after the update. So if the update operation was
48706 - * interrupted, UBI knows that the volume is corrupted.
48707 - *
48708 - * The @alignment field is specified when the volume is created and cannot be
48709 - * later changed. It may be useful, for example, when a block-oriented file
48710 - * system works on top of UBI. The @data_pad field is calculated using the
48711 - * logical eraseblock size and @alignment. The alignment must be multiple to the
48712 - * minimal flash I/O unit. If @alignment is 1, all the available space of
48713 - * the physical eraseblocks is used.
48714 - *
48715 - * Empty records contain all zeroes and the CRC checksum of those zeroes.
48716 - */
48717 -struct ubi_vtbl_record {
48718 - __be32 reserved_pebs;
48719 - __be32 alignment;
48720 - __be32 data_pad;
48721 - __u8 vol_type;
48722 - __u8 upd_marker;
48723 - __be16 name_len;
48724 - __u8 name[UBI_VOL_NAME_MAX+1];
48725 - __u8 flags;
48726 - __u8 padding[23];
48727 - __be32 crc;
48728 -} __attribute__ ((packed));
48729 -
48730 -#endif /* !__UBI_HEADER_H__ */
48731 diff --exclude=.git -urN linux-2.6.25.6/init/do_mounts.c avr32-2.6/init/do_mounts.c
48732 --- linux-2.6.25.6/init/do_mounts.c 2008-06-09 20:27:19.000000000 +0200
48733 +++ avr32-2.6/init/do_mounts.c 2008-06-12 15:09:46.451815572 +0200
48734 @@ -126,8 +126,14 @@
48735
48736 static int __init rootwait_setup(char *str)
48737 {
48738 - if (*str)
48739 + if (*str && *str != '=')
48740 return 0;
48741 +
48742 + if (*str)
48743 + printk(KERN_WARNING
48744 + "WARNING: \"rootwait=1\" is deprecated, "
48745 + "use \"rootwait\" instead.\n");
48746 +
48747 root_wait = 1;
48748 return 1;
48749 }
48750 @@ -347,7 +353,8 @@
48751
48752 if (saved_root_name[0]) {
48753 root_device_name = saved_root_name;
48754 - if (!strncmp(root_device_name, "mtd", 3)) {
48755 + if (!strncmp(root_device_name, "mtd", 3) ||
48756 + !strncmp(root_device_name, "ubi", 3)) {
48757 mount_block_root(root_device_name, root_mountflags);
48758 goto out;
48759 }
48760 diff --exclude=.git -urN linux-2.6.25.6/sound/avr32/ac97c.c avr32-2.6/sound/avr32/ac97c.c
48761 --- linux-2.6.25.6/sound/avr32/ac97c.c 1970-01-01 01:00:00.000000000 +0100
48762 +++ avr32-2.6/sound/avr32/ac97c.c 2008-06-12 15:09:47.011815952 +0200
48763 @@ -0,0 +1,914 @@
48764 +/*
48765 + * Driver for the Atmel AC97 controller
48766 + *
48767 + * Copyright (C) 2005-2007 Atmel Corporation
48768 + *
48769 + * This program is free software; you can redistribute it and/or modify it
48770 + * under the terms of the GNU General Public License version 2 as published by
48771 + * the Free Software Foundation.
48772 + */
48773 +#include <linux/clk.h>
48774 +#include <linux/delay.h>
48775 +#include <linux/dma-mapping.h>
48776 +#include <linux/init.h>
48777 +#include <linux/interrupt.h>
48778 +#include <linux/module.h>
48779 +#include <linux/platform_device.h>
48780 +#include <linux/mutex.h>
48781 +#include <linux/io.h>
48782 +
48783 +#include <sound/driver.h>
48784 +#include <sound/core.h>
48785 +#include <sound/initval.h>
48786 +#include <sound/pcm.h>
48787 +#include <sound/pcm_params.h>
48788 +#include <sound/ac97_codec.h>
48789 +#include <sound/memalloc.h>
48790 +
48791 +#include <asm/dma-controller.h>
48792 +
48793 +#include "ac97c.h"
48794 +
48795 +/* Serialize access to opened */
48796 +static DEFINE_MUTEX(opened_mutex);
48797 +
48798 +struct atmel_ac97_dma_info {
48799 + struct dma_request_cyclic req_tx;
48800 + struct dma_request_cyclic req_rx;
48801 + unsigned short rx_periph_id;
48802 + unsigned short tx_periph_id;
48803 +};
48804 +
48805 +struct atmel_ac97 {
48806 + /* Serialize access to opened */
48807 + spinlock_t lock;
48808 + void __iomem *regs;
48809 + struct snd_pcm_substream *playback_substream;
48810 + struct snd_pcm_substream *capture_substream;
48811 + struct snd_card *card;
48812 + struct snd_pcm *pcm;
48813 + struct snd_ac97 *ac97;
48814 + struct snd_ac97_bus *ac97_bus;
48815 + int opened;
48816 + int period;
48817 + u64 cur_format;
48818 + unsigned int cur_rate;
48819 + struct clk *mck;
48820 + struct platform_device *pdev;
48821 + struct atmel_ac97_dma_info dma;
48822 +};
48823 +
48824 +#define get_chip(card) ((struct atmel_ac97 *)(card)->private_data)
48825 +
48826 +#define ac97c_writel(chip, reg, val) \
48827 + __raw_writel((val), (chip)->regs + AC97C_##reg)
48828 +#define ac97c_readl(chip, reg) \
48829 + __raw_readl((chip)->regs + AC97C_##reg)
48830 +
48831 +/*
48832 + * PCM part
48833 + */
48834 +static struct snd_pcm_hardware snd_atmel_ac97_playback_hw = {
48835 + .info = (SNDRV_PCM_INFO_INTERLEAVED
48836 + | SNDRV_PCM_INFO_MMAP
48837 + | SNDRV_PCM_INFO_MMAP_VALID
48838 + | SNDRV_PCM_INFO_BLOCK_TRANSFER
48839 + | SNDRV_PCM_INFO_JOINT_DUPLEX),
48840 + .formats = (SNDRV_PCM_FMTBIT_S16_BE
48841 + | SNDRV_PCM_FMTBIT_S16_LE),
48842 + .rates = (SNDRV_PCM_RATE_CONTINUOUS),
48843 + .rate_min = 4000,
48844 + .rate_max = 48000,
48845 + .channels_min = 1,
48846 + .channels_max = 6,
48847 + .buffer_bytes_max = 64*1024,
48848 + .period_bytes_min = 512,
48849 + .period_bytes_max = 4095,
48850 + .periods_min = 8,
48851 + .periods_max = 1024,
48852 +};
48853 +
48854 +static struct snd_pcm_hardware snd_atmel_ac97_capture_hw = {
48855 + .info = (SNDRV_PCM_INFO_INTERLEAVED
48856 + | SNDRV_PCM_INFO_MMAP
48857 + | SNDRV_PCM_INFO_MMAP_VALID
48858 + | SNDRV_PCM_INFO_BLOCK_TRANSFER
48859 + | SNDRV_PCM_INFO_JOINT_DUPLEX),
48860 + .formats = (SNDRV_PCM_FMTBIT_S16_BE
48861 + | SNDRV_PCM_FMTBIT_S16_LE),
48862 + .rates = (SNDRV_PCM_RATE_CONTINUOUS),
48863 + .rate_min = 4000,
48864 + .rate_max = 48000,
48865 + .channels_min = 1,
48866 + .channels_max = 2,
48867 + .buffer_bytes_max = 64*1024,
48868 + .period_bytes_min = 512,
48869 + .period_bytes_max = 4095,
48870 + .periods_min = 8,
48871 + .periods_max = 1024,
48872 +};
48873 +
48874 +/*
48875 + * PCM functions
48876 + */
48877 +static int
48878 +snd_atmel_ac97_playback_open(struct snd_pcm_substream *substream)
48879 +{
48880 + struct atmel_ac97 *chip = snd_pcm_substream_chip(substream);
48881 + struct snd_pcm_runtime *runtime = substream->runtime;
48882 +
48883 + mutex_lock(&opened_mutex);
48884 + chip->opened++;
48885 + runtime->hw = snd_atmel_ac97_playback_hw;
48886 + if (chip->cur_rate) {
48887 + runtime->hw.rate_min = chip->cur_rate;
48888 + runtime->hw.rate_max = chip->cur_rate;
48889 + }
48890 + if (chip->cur_format)
48891 + runtime->hw.formats = (1ULL << chip->cur_format);
48892 + mutex_unlock(&opened_mutex);
48893 + chip->playback_substream = substream;
48894 + chip->period = 0;
48895 + return 0;
48896 +}
48897 +
48898 +static int
48899 +snd_atmel_ac97_capture_open(struct snd_pcm_substream *substream)
48900 +{
48901 + struct atmel_ac97 *chip = snd_pcm_substream_chip(substream);
48902 + struct snd_pcm_runtime *runtime = substream->runtime;
48903 +
48904 + mutex_lock(&opened_mutex);
48905 + chip->opened++;
48906 + runtime->hw = snd_atmel_ac97_capture_hw;
48907 + if (chip->cur_rate) {
48908 + runtime->hw.rate_min = chip->cur_rate;
48909 + runtime->hw.rate_max = chip->cur_rate;
48910 + }
48911 + if (chip->cur_format)
48912 + runtime->hw.formats = (1ULL << chip->cur_format);
48913 + mutex_unlock(&opened_mutex);
48914 + chip->capture_substream = substream;
48915 + chip->period = 0;
48916 + return 0;
48917 +}
48918 +
48919 +static int snd_atmel_ac97_playback_close(struct snd_pcm_substream *substream)
48920 +{
48921 + struct atmel_ac97 *chip = snd_pcm_substream_chip(substream);
48922 + mutex_lock(&opened_mutex);
48923 + chip->opened--;
48924 + if (!chip->opened) {
48925 + chip->cur_rate = 0;
48926 + chip->cur_format = 0;
48927 + }
48928 + mutex_unlock(&opened_mutex);
48929 + return 0;
48930 +}
48931 +
48932 +static int snd_atmel_ac97_capture_close(struct snd_pcm_substream *substream)
48933 +{
48934 + struct atmel_ac97 *chip = snd_pcm_substream_chip(substream);
48935 + mutex_lock(&opened_mutex);
48936 + chip->opened--;
48937 + if (!chip->opened) {
48938 + chip->cur_rate = 0;
48939 + chip->cur_format = 0;
48940 + }
48941 + mutex_unlock(&opened_mutex);
48942 + return 0;
48943 +}
48944 +
48945 +static int
48946 +snd_atmel_ac97_playback_hw_params(struct snd_pcm_substream *substream,
48947 + struct snd_pcm_hw_params *hw_params)
48948 +{
48949 + struct atmel_ac97 *chip = snd_pcm_substream_chip(substream);
48950 + int err;
48951 +
48952 + err = snd_pcm_lib_malloc_pages(substream,
48953 + params_buffer_bytes(hw_params));
48954 + if (err < 0)
48955 + return err;
48956 +
48957 + /* Set restrictions to params */
48958 + mutex_lock(&opened_mutex);
48959 + chip->cur_rate = params_rate(hw_params);
48960 + chip->cur_format = params_format(hw_params);
48961 + mutex_unlock(&opened_mutex);
48962 +
48963 + return 0;
48964 +}
48965 +
48966 +static int
48967 +snd_atmel_ac97_capture_hw_params(struct snd_pcm_substream *substream,
48968 + struct snd_pcm_hw_params *hw_params)
48969 +{
48970 + struct atmel_ac97 *chip = snd_pcm_substream_chip(substream);
48971 + int err;
48972 +
48973 + err = snd_pcm_lib_malloc_pages(substream,
48974 + params_buffer_bytes(hw_params));
48975 + if (err < 0)
48976 + return err;
48977 +
48978 + /* Set restrictions to params */
48979 + mutex_lock(&opened_mutex);
48980 + chip->cur_rate = params_rate(hw_params);
48981 + chip->cur_format = params_format(hw_params);
48982 + mutex_unlock(&opened_mutex);
48983 +
48984 + return 0;
48985 +}
48986 +
48987 +static int snd_atmel_ac97_playback_hw_free(struct snd_pcm_substream *substream)
48988 +{
48989 + return snd_pcm_lib_free_pages(substream);
48990 +}
48991 +
48992 +static int snd_atmel_ac97_capture_hw_free(struct snd_pcm_substream *substream)
48993 +{
48994 +
48995 + return snd_pcm_lib_free_pages(substream);
48996 +}
48997 +
48998 +static int snd_atmel_ac97_playback_prepare(struct snd_pcm_substream *substream)
48999 +{
49000 + struct atmel_ac97 *chip = snd_pcm_substream_chip(substream);
49001 + struct platform_device *pdev = chip->pdev;
49002 + struct snd_pcm_runtime *runtime = substream->runtime;
49003 + int block_size = frames_to_bytes(runtime, runtime->period_size);
49004 + unsigned long word = 0;
49005 + unsigned long buffer_size = 0;
49006 +
49007 + dma_sync_single_for_device(&pdev->dev, runtime->dma_addr,
49008 + block_size * 2, DMA_TO_DEVICE);
49009 +
49010 + /* Assign slots to channels */
49011 + switch (substream->runtime->channels) {
49012 + case 1:
49013 + word |= AC97C_CH_ASSIGN(PCM_LEFT, A);
49014 + break;
49015 + case 2:
49016 + /* Assign Left and Right slot to Channel A */
49017 + word |= AC97C_CH_ASSIGN(PCM_LEFT, A)
49018 + | AC97C_CH_ASSIGN(PCM_RIGHT, A);
49019 + break;
49020 + default:
49021 + /* TODO: support more than two channels */
49022 + return -EINVAL;
49023 + break;
49024 + }
49025 + ac97c_writel(chip, OCA, word);
49026 +
49027 + /* Configure sample format and size */
49028 + word = AC97C_CMR_PDCEN | AC97C_CMR_SIZE_16;
49029 +
49030 + switch (runtime->format) {
49031 + case SNDRV_PCM_FORMAT_S16_LE:
49032 + word |= AC97C_CMR_CEM_LITTLE;
49033 + break;
49034 + case SNDRV_PCM_FORMAT_S16_BE: /* fall through */
49035 + default:
49036 + word &= ~AC97C_CMR_CEM_LITTLE;
49037 + break;
49038 + }
49039 +
49040 + ac97c_writel(chip, CAMR, word);
49041 +
49042 + /* Set variable rate if needed */
49043 + if (runtime->rate != 48000) {
49044 + word = ac97c_readl(chip, MR);
49045 + word |= AC97C_MR_VRA;
49046 + ac97c_writel(chip, MR, word);
49047 + } else {
49048 + /* Clear Variable Rate Bit */
49049 + word = ac97c_readl(chip, MR);
49050 + word &= ~AC97C_MR_VRA;
49051 + ac97c_writel(chip, MR, word);
49052 + }
49053 +
49054 + /* Set rate */
49055 + snd_ac97_set_rate(chip->ac97, AC97_PCM_FRONT_DAC_RATE, runtime->rate);
49056 +
49057 + buffer_size = frames_to_bytes(runtime, runtime->period_size) *
49058 + runtime->periods;
49059 +
49060 + chip->dma.req_tx.buffer_size = buffer_size;
49061 + chip->dma.req_tx.periods = runtime->periods;
49062 +
49063 + BUG_ON(chip->dma.req_tx.buffer_size !=
49064 + (chip->dma.req_tx.periods *
49065 + frames_to_bytes(runtime, runtime->period_size)));
49066 +
49067 + chip->dma.req_tx.buffer_start = runtime->dma_addr;
49068 + chip->dma.req_tx.data_reg = (dma_addr_t)(chip->regs + AC97C_CATHR + 2);
49069 + chip->dma.req_tx.periph_id = chip->dma.tx_periph_id;
49070 + chip->dma.req_tx.direction = DMA_DIR_MEM_TO_PERIPH;
49071 + chip->dma.req_tx.width = DMA_WIDTH_16BIT;
49072 + chip->dma.req_tx.dev_id = chip;
49073 +
49074 + return 0;
49075 +}
49076 +
49077 +static int snd_atmel_ac97_capture_prepare(struct snd_pcm_substream *substream)
49078 +{
49079 + struct atmel_ac97 *chip = snd_pcm_substream_chip(substream);
49080 + struct platform_device *pdev = chip->pdev;
49081 + struct snd_pcm_runtime *runtime = substream->runtime;
49082 + int block_size = frames_to_bytes(runtime, runtime->period_size);
49083 + unsigned long word = 0;
49084 + unsigned long buffer_size = 0;
49085 +
49086 + dma_sync_single_for_device(&pdev->dev, runtime->dma_addr,
49087 + block_size * 2, DMA_FROM_DEVICE);
49088 +
49089 + /* Assign slots to channels */
49090 + switch (substream->runtime->channels) {
49091 + case 1:
49092 + word |= AC97C_CH_ASSIGN(PCM_LEFT, A);
49093 + break;
49094 + case 2:
49095 + /* Assign Left and Right slot to Channel A */
49096 + word |= AC97C_CH_ASSIGN(PCM_LEFT, A)
49097 + | AC97C_CH_ASSIGN(PCM_RIGHT, A);
49098 + break;
49099 + default:
49100 + /* TODO: support more than two channels */
49101 + return -EINVAL;
49102 + break;
49103 + }
49104 + ac97c_writel(chip, ICA, word);
49105 +
49106 + /* Configure sample format and size */
49107 + word = AC97C_CMR_PDCEN | AC97C_CMR_SIZE_16;
49108 +
49109 + switch (runtime->format) {
49110 + case SNDRV_PCM_FORMAT_S16_LE:
49111 + word |= AC97C_CMR_CEM_LITTLE;
49112 + break;
49113 + case SNDRV_PCM_FORMAT_S16_BE:
49114 + default:
49115 + word &= ~(AC97C_CMR_CEM_LITTLE);
49116 + break;
49117 + }
49118 +
49119 + ac97c_writel(chip, CAMR, word);
49120 +
49121 + /* Set variable rate if needed */
49122 + if (runtime->rate != 48000) {
49123 + word = ac97c_readl(chip, MR);
49124 + word |= AC97C_MR_VRA;
49125 + ac97c_writel(chip, MR, word);
49126 + } else {
49127 + /* Clear Variable Rate Bit */
49128 + word = ac97c_readl(chip, MR);
49129 + word &= ~(AC97C_MR_VRA);
49130 + ac97c_writel(chip, MR, word);
49131 + }
49132 +
49133 + /* Set rate */
49134 + snd_ac97_set_rate(chip->ac97, AC97_PCM_LR_ADC_RATE, runtime->rate);
49135 +
49136 + buffer_size = frames_to_bytes(runtime, runtime->period_size) *
49137 + runtime->periods;
49138 +
49139 + chip->dma.req_rx.buffer_size = buffer_size;
49140 + chip->dma.req_rx.periods = runtime->periods;
49141 +
49142 + BUG_ON(chip->dma.req_rx.buffer_size !=
49143 + (chip->dma.req_rx.periods *
49144 + frames_to_bytes(runtime, runtime->period_size)));
49145 +
49146 + chip->dma.req_rx.buffer_start = runtime->dma_addr;
49147 + chip->dma.req_rx.data_reg = (dma_addr_t)(chip->regs + AC97C_CARHR + 2);
49148 + chip->dma.req_rx.periph_id = chip->dma.rx_periph_id;
49149 + chip->dma.req_rx.direction = DMA_DIR_PERIPH_TO_MEM;
49150 + chip->dma.req_rx.width = DMA_WIDTH_16BIT;
49151 + chip->dma.req_rx.dev_id = chip;
49152 +
49153 + return 0;
49154 +}
49155 +
49156 + static int
49157 +snd_atmel_ac97_playback_trigger(struct snd_pcm_substream *substream, int cmd)
49158 +{
49159 + struct atmel_ac97 *chip = snd_pcm_substream_chip(substream);
49160 + unsigned long camr;
49161 + int flags, err = 0;
49162 +
49163 + spin_lock_irqsave(&chip->lock, flags);
49164 + camr = ac97c_readl(chip, CAMR);
49165 +
49166 + switch (cmd) {
49167 + case SNDRV_PCM_TRIGGER_START:
49168 + err = dma_prepare_request_cyclic(chip->dma.req_tx.req.dmac,
49169 + &chip->dma.req_tx);
49170 + dma_start_request(chip->dma.req_tx.req.dmac,
49171 + chip->dma.req_tx.req.channel);
49172 + camr |= AC97C_CMR_CENA;
49173 + break;
49174 + case SNDRV_PCM_TRIGGER_STOP:
49175 + err = dma_stop_request(chip->dma.req_tx.req.dmac,
49176 + chip->dma.req_tx.req.channel);
49177 + if (chip->opened <= 1)
49178 + camr &= ~AC97C_CMR_CENA;
49179 + break;
49180 + default:
49181 + err = -EINVAL;
49182 + break;
49183 + }
49184 +
49185 + ac97c_writel(chip, CAMR, camr);
49186 +
49187 + spin_unlock_irqrestore(&chip->lock, flags);
49188 + return err;
49189 +}
49190 +
49191 + static int
49192 +snd_atmel_ac97_capture_trigger(struct snd_pcm_substream *substream, int cmd)
49193 +{
49194 + struct atmel_ac97 *chip = snd_pcm_substream_chip(substream);
49195 + unsigned long camr;
49196 + int flags, err = 0;
49197 +
49198 + spin_lock_irqsave(&chip->lock, flags);
49199 + camr = ac97c_readl(chip, CAMR);
49200 +
49201 + switch (cmd) {
49202 + case SNDRV_PCM_TRIGGER_START:
49203 + err = dma_prepare_request_cyclic(chip->dma.req_rx.req.dmac,
49204 + &chip->dma.req_rx);
49205 + dma_start_request(chip->dma.req_rx.req.dmac,
49206 + chip->dma.req_rx.req.channel);
49207 + camr |= AC97C_CMR_CENA;
49208 + break;
49209 + case SNDRV_PCM_TRIGGER_STOP:
49210 + err = dma_stop_request(chip->dma.req_rx.req.dmac,
49211 + chip->dma.req_rx.req.channel);
49212 + mutex_lock(&opened_mutex);
49213 + if (chip->opened <= 1)
49214 + camr &= ~AC97C_CMR_CENA;
49215 + mutex_unlock(&opened_mutex);
49216 + break;
49217 + default:
49218 + err = -EINVAL;
49219 + break;
49220 + }
49221 +
49222 + ac97c_writel(chip, CAMR, camr);
49223 +
49224 + spin_unlock_irqrestore(&chip->lock, flags);
49225 + return err;
49226 +}
49227 +
49228 + static snd_pcm_uframes_t
49229 +snd_atmel_ac97_playback_pointer(struct snd_pcm_substream *substream)
49230 +{
49231 + struct atmel_ac97 *chip = snd_pcm_substream_chip(substream);
49232 + struct snd_pcm_runtime *runtime = substream->runtime;
49233 + snd_pcm_uframes_t pos;
49234 + unsigned long bytes;
49235 +
49236 + bytes = (dma_get_current_pos
49237 + (chip->dma.req_tx.req.dmac,
49238 + chip->dma.req_tx.req.channel) - runtime->dma_addr);
49239 + pos = bytes_to_frames(runtime, bytes);
49240 + if (pos >= runtime->buffer_size)
49241 + pos -= runtime->buffer_size;
49242 +
49243 + return pos;
49244 +}
49245 +
49246 + static snd_pcm_uframes_t
49247 +snd_atmel_ac97_capture_pointer(struct snd_pcm_substream *substream)
49248 +{
49249 + struct atmel_ac97 *chip = snd_pcm_substream_chip(substream);
49250 + struct snd_pcm_runtime *runtime = substream->runtime;
49251 + snd_pcm_uframes_t pos;
49252 + unsigned long bytes;
49253 +
49254 + bytes = (dma_get_current_pos
49255 + (chip->dma.req_rx.req.dmac,
49256 + chip->dma.req_rx.req.channel)
49257 + - runtime->dma_addr);
49258 + pos = bytes_to_frames(runtime, bytes);
49259 + if (pos >= runtime->buffer_size)
49260 + pos -= runtime->buffer_size;
49261 +
49262 +
49263 + return pos;
49264 +}
49265 +
49266 +static struct snd_pcm_ops atmel_ac97_playback_ops = {
49267 + .open = snd_atmel_ac97_playback_open,
49268 + .close = snd_atmel_ac97_playback_close,
49269 + .ioctl = snd_pcm_lib_ioctl,
49270 + .hw_params = snd_atmel_ac97_playback_hw_params,
49271 + .hw_free = snd_atmel_ac97_playback_hw_free,
49272 + .prepare = snd_atmel_ac97_playback_prepare,
49273 + .trigger = snd_atmel_ac97_playback_trigger,
49274 + .pointer = snd_atmel_ac97_playback_pointer,
49275 +};
49276 +
49277 +static struct snd_pcm_ops atmel_ac97_capture_ops = {
49278 + .open = snd_atmel_ac97_capture_open,
49279 + .close = snd_atmel_ac97_capture_close,
49280 + .ioctl = snd_pcm_lib_ioctl,
49281 + .hw_params = snd_atmel_ac97_capture_hw_params,
49282 + .hw_free = snd_atmel_ac97_capture_hw_free,
49283 + .prepare = snd_atmel_ac97_capture_prepare,
49284 + .trigger = snd_atmel_ac97_capture_trigger,
49285 + .pointer = snd_atmel_ac97_capture_pointer,
49286 +};
49287 +
49288 +static struct ac97_pcm atmel_ac97_pcm_defs[] __devinitdata = {
49289 + /* Playback */
49290 + {
49291 + .exclusive = 1,
49292 + .r = { {
49293 + .slots = ((1 << AC97_SLOT_PCM_LEFT)
49294 + | (1 << AC97_SLOT_PCM_RIGHT)
49295 + | (1 << AC97_SLOT_PCM_CENTER)
49296 + | (1 << AC97_SLOT_PCM_SLEFT)
49297 + | (1 << AC97_SLOT_PCM_SRIGHT)
49298 + | (1 << AC97_SLOT_LFE)),
49299 + } }
49300 + },
49301 + /* PCM in */
49302 + {
49303 + .stream = 1,
49304 + .exclusive = 1,
49305 + .r = { {
49306 + .slots = ((1 << AC97_SLOT_PCM_LEFT)
49307 + | (1 << AC97_SLOT_PCM_RIGHT)),
49308 + } }
49309 + },
49310 + /* Mic in */
49311 + {
49312 + .stream = 1,
49313 + .exclusive = 1,
49314 + .r = { {
49315 + .slots = (1<<AC97_SLOT_MIC),
49316 + } }
49317 + },
49318 +};
49319 +
49320 +static int __devinit snd_atmel_ac97_pcm_new(struct atmel_ac97 *chip)
49321 +{
49322 + struct snd_pcm *pcm;
49323 + int err;
49324 +
49325 + err = snd_ac97_pcm_assign(chip->ac97_bus,
49326 + ARRAY_SIZE(atmel_ac97_pcm_defs),
49327 + atmel_ac97_pcm_defs);
49328 + if (err)
49329 + return err;
49330 +
49331 + err = snd_pcm_new(chip->card, "Atmel-AC97", 0, 1, 1, &pcm);
49332 + if (err)
49333 + return err;
49334 +
49335 + snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
49336 + &atmel_ac97_playback_ops);
49337 +
49338 + snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
49339 + &atmel_ac97_capture_ops);
49340 +
49341 + snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
49342 + &chip->pdev->dev,
49343 + 128 * 1024, 128 * 1024);
49344 +
49345 + pcm->private_data = chip;
49346 + pcm->info_flags = 0;
49347 + strcpy(pcm->name, "Atmel-AC97");
49348 + chip->pcm = pcm;
49349 +
49350 + return 0;
49351 +}
49352 +
49353 +/*
49354 + * Mixer part.
49355 + */
49356 +static int snd_atmel_ac97_mixer_new(struct atmel_ac97 *chip)
49357 +{
49358 + int err;
49359 + struct snd_ac97_template template;
49360 +
49361 + memset(&template, 0, sizeof(template));
49362 + template.private_data = chip;
49363 + err = snd_ac97_mixer(chip->ac97_bus, &template, &chip->ac97);
49364 +
49365 + return err;
49366 +}
49367 +
49368 +static void atmel_ac97_error(struct dma_request *_req)
49369 +{
49370 + struct dma_request_cyclic *req = to_dma_request_cyclic(_req);
49371 + struct atmel_ac97 *chip = req->dev_id;
49372 +
49373 + dev_dbg(&chip->pdev->dev, "DMA Controller error, channel %d\n",
49374 + req->req.channel);
49375 +}
49376 +
49377 +static void atmel_ac97_block_complete(struct dma_request *_req)
49378 +{
49379 + struct dma_request_cyclic *req = to_dma_request_cyclic(_req);
49380 + struct atmel_ac97 *chip = req->dev_id;
49381 + if (req->periph_id == chip->dma.tx_periph_id)
49382 + snd_pcm_period_elapsed(chip->playback_substream);
49383 + else
49384 + snd_pcm_period_elapsed(chip->capture_substream);
49385 +}
49386 +
49387 +/*
49388 + * Codec part.
49389 + */
49390 +static void snd_atmel_ac97_write(struct snd_ac97 *ac97, unsigned short reg,
49391 + unsigned short val)
49392 +{
49393 + struct atmel_ac97 *chip = get_chip(ac97);
49394 + unsigned long word;
49395 + int timeout = 40;
49396 +
49397 + word = (reg & 0x7f) << 16 | val;
49398 +
49399 + do {
49400 + if (ac97c_readl(chip, COSR) & AC97C_CSR_TXRDY) {
49401 + ac97c_writel(chip, COTHR, word);
49402 + return;
49403 + }
49404 + udelay(1);
49405 + } while (--timeout);
49406 +
49407 + dev_dbg(&chip->pdev->dev, "codec write timeout\n");
49408 +}
49409 +
49410 +static unsigned short snd_atmel_ac97_read(struct snd_ac97 *ac97,
49411 + unsigned short reg)
49412 +{
49413 + struct atmel_ac97 *chip = get_chip(ac97);
49414 + unsigned long word;
49415 + int timeout = 40;
49416 + int write = 10;
49417 +
49418 + word = (0x80 | (reg & 0x7f)) << 16;
49419 +
49420 + if ((ac97c_readl(chip, COSR) & AC97C_CSR_RXRDY) != 0)
49421 + ac97c_readl(chip, CORHR);
49422 +
49423 +retry_write:
49424 + timeout = 40;
49425 +
49426 + do {
49427 + if ((ac97c_readl(chip, COSR) & AC97C_CSR_TXRDY) != 0) {
49428 + ac97c_writel(chip, COTHR, word);
49429 + goto read_reg;
49430 + }
49431 + udelay(10);
49432 + } while (--timeout);
49433 +
49434 + if (!--write)
49435 + goto timed_out;
49436 + goto retry_write;
49437 +
49438 +read_reg:
49439 + do {
49440 + if ((ac97c_readl(chip, COSR) & AC97C_CSR_RXRDY) != 0) {
49441 + unsigned short val = ac97c_readl(chip, CORHR);
49442 + return val;
49443 + }
49444 + udelay(10);
49445 + } while (--timeout);
49446 +
49447 + if (!--write)
49448 + goto timed_out;
49449 + goto retry_write;
49450 +
49451 +timed_out:
49452 + dev_dbg(&chip->pdev->dev, "codec read timeout\n");
49453 + return 0xffff;
49454 +}
49455 +
49456 +static void snd_atmel_ac97_reset(struct atmel_ac97 *chip)
49457 +{
49458 + ac97c_writel(chip, MR, AC97C_MR_WRST);
49459 + mdelay(1);
49460 + ac97c_writel(chip, MR, AC97C_MR_ENA);
49461 +}
49462 +
49463 +static void snd_atmel_ac97_destroy(struct snd_card *card)
49464 +{
49465 + struct atmel_ac97 *chip = get_chip(card);
49466 +
49467 + if (chip->regs)
49468 + iounmap(chip->regs);
49469 +
49470 + if (chip->mck) {
49471 + clk_disable(chip->mck);
49472 + clk_put(chip->mck);
49473 + }
49474 +
49475 + if (chip->dma.req_tx.req.dmac) {
49476 + dma_release_channel(chip->dma.req_tx.req.dmac,
49477 + chip->dma.req_tx.req.channel);
49478 + }
49479 + if (chip->dma.req_rx.req.dmac) {
49480 + dma_release_channel(chip->dma.req_rx.req.dmac,
49481 + chip->dma.req_rx.req.channel);
49482 + }
49483 +}
49484 +
49485 +static int __devinit snd_atmel_ac97_create(struct snd_card *card,
49486 + struct platform_device *pdev)
49487 +{
49488 + static struct snd_ac97_bus_ops ops = {
49489 + .write = snd_atmel_ac97_write,
49490 + .read = snd_atmel_ac97_read,
49491 + };
49492 + struct atmel_ac97 *chip = get_chip(card);
49493 + struct resource *regs;
49494 + struct clk *mck;
49495 + int err;
49496 +
49497 + regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
49498 + if (!regs)
49499 + return -ENXIO;
49500 +
49501 + mck = clk_get(&pdev->dev, "pclk");
49502 + if (IS_ERR(mck))
49503 + return PTR_ERR(mck);
49504 + clk_enable(mck);
49505 + chip->mck = mck;
49506 +
49507 + card->private_free = snd_atmel_ac97_destroy;
49508 +
49509 + spin_lock_init(&chip->lock);
49510 + chip->card = card;
49511 + chip->pdev = pdev;
49512 +
49513 + chip->regs = ioremap(regs->start, regs->end - regs->start + 1);
49514 + if (!chip->regs)
49515 + return -ENOMEM;
49516 +
49517 + snd_card_set_dev(card, &pdev->dev);
49518 +
49519 + err = snd_ac97_bus(card, 0, &ops, chip, &chip->ac97_bus);
49520 +
49521 + return err;
49522 +}
49523 +
49524 +static int __devinit snd_atmel_ac97_probe(struct platform_device *pdev)
49525 +{
49526 + static int dev;
49527 + struct snd_card *card;
49528 + struct atmel_ac97 *chip;
49529 + int err;
49530 + int ch;
49531 +
49532 + mutex_init(&opened_mutex);
49533 +
49534 + err = -ENOMEM;
49535 + card = snd_card_new(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
49536 + THIS_MODULE, sizeof(struct atmel_ac97));
49537 + if (!card)
49538 + goto out;
49539 + chip = get_chip(card);
49540 +
49541 + err = snd_atmel_ac97_create(card, pdev);
49542 + if (err)
49543 + goto out_free_card;
49544 +
49545 + snd_atmel_ac97_reset(chip);
49546 +
49547 + err = snd_atmel_ac97_mixer_new(chip);
49548 + if (err)
49549 + goto out_free_card;
49550 +
49551 + err = snd_atmel_ac97_pcm_new(chip);
49552 + if (err)
49553 + goto out_free_card;
49554 +
49555 + /* TODO: Get this information from the platform device */
49556 + chip->dma.req_tx.req.dmac = find_dma_controller(0);
49557 + if (!chip->dma.req_tx.req.dmac) {
49558 + dev_dbg(&chip->pdev->dev, "DMA controller for TX missing\n");
49559 + err = -ENODEV;
49560 + goto out_free_card;
49561 + }
49562 + chip->dma.req_rx.req.dmac = find_dma_controller(0);
49563 + if (!chip->dma.req_rx.req.dmac) {
49564 + dev_dbg(&chip->pdev->dev, "DMA controller for RX missing\n");
49565 + err = -ENODEV;
49566 + goto out_free_card;
49567 + }
49568 +
49569 + chip->dma.rx_periph_id = 3;
49570 + chip->dma.tx_periph_id = 4;
49571 +
49572 + ch = dma_alloc_channel(chip->dma.req_tx.req.dmac);
49573 + if (ch < 0) {
49574 + dev_dbg(&chip->pdev->dev,
49575 + "could not allocate TX DMA channel\n");
49576 + err = ch;
49577 + goto out_free_card;
49578 + }
49579 + chip->dma.req_tx.req.channel = ch;
49580 + chip->dma.req_tx.width = DMA_WIDTH_16BIT;
49581 + chip->dma.req_tx.req.block_complete = atmel_ac97_block_complete;
49582 + chip->dma.req_tx.req.error = atmel_ac97_error;
49583 +
49584 + ch = dma_alloc_channel(chip->dma.req_rx.req.dmac);
49585 + if (ch < 0) {
49586 + dev_dbg(&chip->pdev->dev,
49587 + "could not allocate RX DMA channel\n");
49588 + err = ch;
49589 + goto out_free_card;
49590 + }
49591 + chip->dma.req_rx.req.channel = ch;
49592 + chip->dma.req_rx.width = DMA_WIDTH_16BIT;
49593 + chip->dma.req_rx.req.block_complete = atmel_ac97_block_complete;
49594 + chip->dma.req_rx.req.error = atmel_ac97_error;
49595 +
49596 + strcpy(card->driver, "atmel_ac97c");
49597 + strcpy(card->shortname, "atmel_ac97c");
49598 + sprintf(card->longname, "Atmel AVR32 AC97 controller");
49599 +
49600 + err = snd_card_register(card);
49601 + if (err)
49602 + goto out_free_card;
49603 +
49604 + platform_set_drvdata(pdev, card);
49605 + dev++;
49606 +
49607 + dev_info(&pdev->dev, "Atmel AVR32 AC97 controller at 0x%p\n",
49608 + chip->regs);
49609 +
49610 + return 0;
49611 +
49612 +out_free_card:
49613 + snd_card_free(card);
49614 +out:
49615 + return err;
49616 +}
49617 +
49618 +#ifdef CONFIG_PM
49619 + static int
49620 +snd_atmel_ac97_suspend(struct platform_device *pdev, pm_message_t msg)
49621 +{
49622 + struct snd_card *card = platform_get_drvdata(pdev);
49623 + struct atmel_ac97 *chip = card->private_data;
49624 +
49625 + clk_disable(chip->mck);
49626 +
49627 + return 0;
49628 +}
49629 +
49630 +static int snd_atmel_ac97_resume(struct platform_device *pdev)
49631 +{
49632 + struct snd_card *card = dev_get_drvdata(pdev);
49633 + struct atmel_ac97 *chip = card->private_data;
49634 +
49635 + clk_enable(chip->mck);
49636 +
49637 + return 0;
49638 +}
49639 +#else
49640 +#define snd_atmel_ac97_suspend NULL
49641 +#define snd_atmel_ac97_resume NULL
49642 +#endif
49643 +
49644 +static int __devexit snd_atmel_ac97_remove(struct platform_device *pdev)
49645 +{
49646 + struct snd_card *card = platform_get_drvdata(pdev);
49647 +
49648 + snd_card_free(card);
49649 + platform_set_drvdata(pdev, NULL);
49650 + return 0;
49651 +}
49652 +
49653 +static struct platform_driver atmel_ac97_driver = {
49654 + .remove = __devexit_p(snd_atmel_ac97_remove),
49655 + .driver = {
49656 + .name = "atmel_ac97c",
49657 + },
49658 + .suspend = snd_atmel_ac97_suspend,
49659 + .resume = snd_atmel_ac97_resume,
49660 +};
49661 +
49662 +static int __init atmel_ac97_init(void)
49663 +{
49664 + return platform_driver_probe(&atmel_ac97_driver,
49665 + snd_atmel_ac97_probe);
49666 +}
49667 +module_init(atmel_ac97_init);
49668 +
49669 +static void __exit atmel_ac97_exit(void)
49670 +{
49671 + platform_driver_unregister(&atmel_ac97_driver);
49672 +}
49673 +module_exit(atmel_ac97_exit);
49674 +
49675 +MODULE_LICENSE("GPL");
49676 +MODULE_DESCRIPTION("Driver for Atmel AC97 Controller");
49677 +MODULE_AUTHOR("Haavard Skinnemoen <hskinnemoen@atmel.com>");
49678 diff --exclude=.git -urN linux-2.6.25.6/sound/avr32/ac97c.h avr32-2.6/sound/avr32/ac97c.h
49679 --- linux-2.6.25.6/sound/avr32/ac97c.h 1970-01-01 01:00:00.000000000 +0100
49680 +++ avr32-2.6/sound/avr32/ac97c.h 2008-06-12 15:09:47.011815952 +0200
49681 @@ -0,0 +1,71 @@
49682 +/*
49683 + * Register definitions for the Atmel AC97 Controller.
49684 + *
49685 + * Copyright (C) 2005-2006 Atmel Corporation
49686 + *
49687 + * This program is free software; you can redistribute it and/or modify
49688 + * it under the terms of the GNU General Public License version 2 as
49689 + * published by the Free Software Foundation.
49690 + */
49691 +#ifndef __SOUND_AVR32_AC97C_H
49692 +#define __SOUND_AVR32_AC97C_H
49693 +
49694 +#define AC97C_MR 0x08
49695 +#define AC97C_ICA 0x10
49696 +#define AC97C_OCA 0x14
49697 +#define AC97C_CARHR 0x20
49698 +#define AC97C_CATHR 0x24
49699 +#define AC97C_CASR 0x28
49700 +#define AC97C_CAMR 0x2c
49701 +#define AC97C_CBRHR 0x30
49702 +#define AC97C_CBTHR 0x34
49703 +#define AC97C_CBSR 0x38
49704 +#define AC97C_CBMR 0x3c
49705 +#define AC97C_CORHR 0x40
49706 +#define AC97C_COTHR 0x44
49707 +#define AC97C_COSR 0x48
49708 +#define AC97C_COMR 0x4c
49709 +#define AC97C_SR 0x50
49710 +#define AC97C_IER 0x54
49711 +#define AC97C_IDR 0x58
49712 +#define AC97C_IMR 0x5c
49713 +#define AC97C_VERSION 0xfc
49714 +
49715 +#define AC97C_CATPR PDC_TPR
49716 +#define AC97C_CATCR PDC_TCR
49717 +#define AC97C_CATNPR PDC_TNPR
49718 +#define AC97C_CATNCR PDC_TNCR
49719 +#define AC97C_CARPR PDC_RPR
49720 +#define AC97C_CARCR PDC_RCR
49721 +#define AC97C_CARNPR PDC_RNPR
49722 +#define AC97C_CARNCR PDC_RNCR
49723 +#define AC97C_PTCR PDC_PTCR
49724 +
49725 +#define AC97C_MR_ENA (1 << 0)
49726 +#define AC97C_MR_WRST (1 << 1)
49727 +#define AC97C_MR_VRA (1 << 2)
49728 +
49729 +#define AC97C_CSR_TXRDY (1 << 0)
49730 +#define AC97C_CSR_UNRUN (1 << 2)
49731 +#define AC97C_CSR_RXRDY (1 << 4)
49732 +#define AC97C_CSR_ENDTX (1 << 10)
49733 +#define AC97C_CSR_ENDRX (1 << 14)
49734 +
49735 +#define AC97C_CMR_SIZE_20 (0 << 16)
49736 +#define AC97C_CMR_SIZE_18 (1 << 16)
49737 +#define AC97C_CMR_SIZE_16 (2 << 16)
49738 +#define AC97C_CMR_SIZE_10 (3 << 16)
49739 +#define AC97C_CMR_CEM_LITTLE (1 << 18)
49740 +#define AC97C_CMR_CEM_BIG (0 << 18)
49741 +#define AC97C_CMR_CENA (1 << 21)
49742 +#define AC97C_CMR_PDCEN (1 << 22)
49743 +
49744 +#define AC97C_SR_CAEVT (1 << 3)
49745 +
49746 +#define AC97C_CH_ASSIGN(slot, channel) \
49747 + (AC97C_CHANNEL_##channel << (3 * (AC97_SLOT_##slot - 3)))
49748 +#define AC97C_CHANNEL_NONE 0x0
49749 +#define AC97C_CHANNEL_A 0x1
49750 +#define AC97C_CHANNEL_B 0x2
49751 +
49752 +#endif /* __SOUND_AVR32_AC97C_H */
49753 diff --exclude=.git -urN linux-2.6.25.6/sound/avr32/Kconfig avr32-2.6/sound/avr32/Kconfig
49754 --- linux-2.6.25.6/sound/avr32/Kconfig 1970-01-01 01:00:00.000000000 +0100
49755 +++ avr32-2.6/sound/avr32/Kconfig 2008-06-12 15:09:47.011815952 +0200
49756 @@ -0,0 +1,11 @@
49757 +menu "AVR32 devices"
49758 + depends on SND != n && AVR32
49759 +
49760 +config SND_ATMEL_AC97
49761 + tristate "Atmel AC97 Controller Driver"
49762 + select SND_PCM
49763 + select SND_AC97_CODEC
49764 + help
49765 + ALSA sound driver for the Atmel AC97 controller.
49766 +
49767 +endmenu
49768 diff --exclude=.git -urN linux-2.6.25.6/sound/avr32/Makefile avr32-2.6/sound/avr32/Makefile
49769 --- linux-2.6.25.6/sound/avr32/Makefile 1970-01-01 01:00:00.000000000 +0100
49770 +++ avr32-2.6/sound/avr32/Makefile 2008-06-12 15:09:47.011815952 +0200
49771 @@ -0,0 +1,3 @@
49772 +snd-atmel-ac97-objs := ac97c.o
49773 +
49774 +obj-$(CONFIG_SND_ATMEL_AC97) += snd-atmel-ac97.o
49775 diff --exclude=.git -urN linux-2.6.25.6/sound/Kconfig avr32-2.6/sound/Kconfig
49776 --- linux-2.6.25.6/sound/Kconfig 2008-06-09 20:27:19.000000000 +0200
49777 +++ avr32-2.6/sound/Kconfig 2008-06-12 15:09:47.011815952 +0200
49778 @@ -63,6 +63,8 @@
49779
49780 source "sound/arm/Kconfig"
49781
49782 +source "sound/avr32/Kconfig"
49783 +
49784 if SPI
49785 source "sound/spi/Kconfig"
49786 endif
49787 diff --exclude=.git -urN linux-2.6.25.6/sound/Makefile avr32-2.6/sound/Makefile
49788 --- linux-2.6.25.6/sound/Makefile 2008-06-09 20:27:19.000000000 +0200
49789 +++ avr32-2.6/sound/Makefile 2008-06-12 15:09:47.011815952 +0200
49790 @@ -6,7 +6,7 @@
49791 obj-$(CONFIG_SOUND_PRIME) += oss/
49792 obj-$(CONFIG_DMASOUND) += oss/
49793 obj-$(CONFIG_SND) += core/ i2c/ drivers/ isa/ pci/ ppc/ arm/ sh/ synth/ usb/ \
49794 - sparc/ spi/ parisc/ pcmcia/ mips/ soc/
49795 + sparc/ spi/ parisc/ pcmcia/ mips/ soc/ avr32/
49796 obj-$(CONFIG_SND_AOA) += aoa/
49797
49798 # This one must be compilable even if sound is configured out
49799 diff --exclude=.git -urN linux-2.6.25.6/sound/oss/at32_abdac.c avr32-2.6/sound/oss/at32_abdac.c
49800 --- linux-2.6.25.6/sound/oss/at32_abdac.c 1970-01-01 01:00:00.000000000 +0100
49801 +++ avr32-2.6/sound/oss/at32_abdac.c 2008-06-12 15:09:47.027815755 +0200
49802 @@ -0,0 +1,722 @@
49803 +/*
49804 + * OSS Sound Driver for the Atmel AT32 on-chip DAC.
49805 + *
49806 + * Copyright (C) 2006 Atmel Corporation
49807 + *
49808 + * This program is free software; you can redistribute it and/or modify
49809 + * it under the terms of the GNU General Public License version 2 as
49810 + * published by the Free Software Foundation.
49811 + */
49812 +#include <linux/clk.h>
49813 +#include <linux/dma-mapping.h>
49814 +#include <linux/fs.h>
49815 +#include <linux/init.h>
49816 +#include <linux/interrupt.h>
49817 +#include <linux/kernel.h>
49818 +#include <linux/module.h>
49819 +#include <linux/platform_device.h>
49820 +#include <linux/sound.h>
49821 +#include <linux/soundcard.h>
49822 +
49823 +#include <asm/byteorder.h>
49824 +#include <asm/dma-controller.h>
49825 +#include <asm/io.h>
49826 +#include <asm/uaccess.h>
49827 +
49828 +/* We want to use the "bizarre" swap-bytes-in-each-halfword macro */
49829 +#include <linux/byteorder/swabb.h>
49830 +
49831 +#include "at32_abdac.h"
49832 +
49833 +#define DMA_BUFFER_SIZE 32768
49834 +#define DMA_PERIOD_SHIFT 10
49835 +#define DMA_PERIOD_SIZE (1 << DMA_PERIOD_SHIFT)
49836 +#define DMA_WRITE_THRESHOLD DMA_PERIOD_SIZE
49837 +
49838 +struct sound_settings {
49839 + unsigned int format;
49840 + unsigned int channels;
49841 + unsigned int sample_rate;
49842 + /* log2(bytes per sample) */
49843 + unsigned int input_order;
49844 +};
49845 +
49846 +struct at32_dac {
49847 + spinlock_t lock;
49848 + void __iomem *regs;
49849 +
49850 + /* head and tail refer to number of words */
49851 + struct {
49852 + u32 *buf;
49853 + int head;
49854 + int tail;
49855 + } dma;
49856 +
49857 + struct semaphore sem;
49858 + wait_queue_head_t write_wait;
49859 +
49860 + /*
49861 + * Read at most ucount bytes from ubuf, translate to 2-channel
49862 + * signed 16-bit big endian format and write to the DMA buffer
49863 + * as long as there is room left. Return the number of bytes
49864 + * successfully copied from ubuf, or -EFAULT if the first
49865 + * sample from ubuf couldn't be read. This function is not
49866 + * called unless there is room for at least one sample (4
49867 + * bytes) in the DMA buffer.
49868 + */
49869 + ssize_t (*trans)(struct at32_dac *dac, const char __user *ubuf,
49870 + size_t ucount);
49871 +
49872 + struct sound_settings dsp_settings;
49873 + struct dma_request_cyclic req;
49874 +
49875 + struct clk *mck;
49876 + struct clk *sample_clk;
49877 + struct platform_device *pdev;
49878 + int busy;
49879 + int playing;
49880 + int dev_dsp;
49881 +};
49882 +static struct at32_dac *the_dac;
49883 +
49884 +static inline unsigned int abdac_get_head(struct at32_dac *dac)
49885 +{
49886 + return dac->dma.head & ((DMA_BUFFER_SIZE / 4) - 1);
49887 +}
49888 +
49889 +static inline unsigned int abdac_get_tail(struct at32_dac *dac)
49890 +{
49891 + return dac->dma.tail & ((DMA_BUFFER_SIZE / 4) - 1);
49892 +}
49893 +
49894 +static inline unsigned int abdac_dma_space(struct at32_dac *dac)
49895 +{
49896 + unsigned int space;
49897 +
49898 + space = ((dac->dma.tail - dac->dma.head - 1)
49899 + & ((DMA_BUFFER_SIZE / 4) - 1));
49900 + return space;
49901 +}
49902 +
49903 +static void abdac_update_dma_tail(struct at32_dac *dac)
49904 +{
49905 + dma_addr_t dma_addr;
49906 + unsigned int new_tail;
49907 +
49908 + if (dac->playing) {
49909 + dma_addr = dma_get_current_pos(dac->req.req.dmac,
49910 + dac->req.req.channel);
49911 + new_tail = (dma_addr - dac->req.buffer_start) / 4;
49912 + if (new_tail >= dac->dma.head
49913 + && (dac->dma.tail < dac->dma.head
49914 + || dac->dma.tail > new_tail))
49915 + dev_notice(&dac->pdev->dev, "DMA underrun detected!\n");
49916 + dac->dma.tail = new_tail;
49917 + dev_dbg(&dac->pdev->dev, "update tail: 0x%x - 0x%x = %u\n",
49918 + dma_addr, dac->req.buffer_start, dac->dma.tail);
49919 + }
49920 +}
49921 +
49922 +static int abdac_start(struct at32_dac *dac)
49923 +{
49924 + int ret;
49925 +
49926 + if (dac->playing)
49927 + return 0;
49928 +
49929 + memset(dac->dma.buf, 0, DMA_BUFFER_SIZE);
49930 +
49931 + clk_enable(dac->sample_clk);
49932 +
49933 + ret = dma_prepare_request_cyclic(dac->req.req.dmac, &dac->req);
49934 + if (ret)
49935 + goto out_stop_clock;
49936 +
49937 + dev_dbg(&dac->pdev->dev, "starting DMA...\n");
49938 + ret = dma_start_request(dac->req.req.dmac, dac->req.req.channel);
49939 + if (ret)
49940 + goto out_stop_request;
49941 +
49942 + dac_writel(dac, CTRL, DAC_BIT(EN));
49943 + dac->playing = 1;
49944 +
49945 + return 0;
49946 +
49947 +out_stop_request:
49948 + dma_stop_request(dac->req.req.dmac,
49949 + dac->req.req.channel);
49950 +out_stop_clock:
49951 + clk_disable(dac->sample_clk);
49952 + return ret;
49953 +}
49954 +
49955 +static int abdac_stop(struct at32_dac *dac)
49956 +{
49957 + if (dac->playing) {
49958 + dma_stop_request(dac->req.req.dmac, dac->req.req.channel);
49959 + dac_writel(dac, DATA, 0);
49960 + dac_writel(dac, CTRL, 0);
49961 + dac->playing = 0;
49962 + clk_disable(dac->sample_clk);
49963 + }
49964 +
49965 + return 0;
49966 +}
49967 +
49968 +static int abdac_dma_prepare(struct at32_dac *dac)
49969 +{
49970 + dac->dma.buf = dma_alloc_coherent(&dac->pdev->dev, DMA_BUFFER_SIZE,
49971 + &dac->req.buffer_start, GFP_KERNEL);
49972 + if (!dac->dma.buf)
49973 + return -ENOMEM;
49974 +
49975 + dac->dma.head = dac->dma.tail = 0;
49976 + dac->req.periods = DMA_BUFFER_SIZE / DMA_PERIOD_SIZE;
49977 + dac->req.buffer_size = DMA_BUFFER_SIZE;
49978 +
49979 + return 0;
49980 +}
49981 +
49982 +static void abdac_dma_cleanup(struct at32_dac *dac)
49983 +{
49984 + if (dac->dma.buf)
49985 + dma_free_coherent(&dac->pdev->dev, DMA_BUFFER_SIZE,
49986 + dac->dma.buf, dac->req.buffer_start);
49987 + dac->dma.buf = NULL;
49988 +}
49989 +
49990 +static void abdac_dma_block_complete(struct dma_request *req)
49991 +{
49992 + struct dma_request_cyclic *creq = to_dma_request_cyclic(req);
49993 + struct at32_dac *dac = container_of(creq, struct at32_dac, req);
49994 +
49995 + wake_up(&dac->write_wait);
49996 +}
49997 +
49998 +static void abdac_dma_error(struct dma_request *req)
49999 +{
50000 + struct dma_request_cyclic *creq = to_dma_request_cyclic(req);
50001 + struct at32_dac *dac = container_of(creq, struct at32_dac, req);
50002 +
50003 + dev_err(&dac->pdev->dev, "DMA error\n");
50004 +}
50005 +
50006 +static irqreturn_t abdac_interrupt(int irq, void *dev_id)
50007 +{
50008 + struct at32_dac *dac = dev_id;
50009 + u32 status;
50010 +
50011 + status = dac_readl(dac, INT_STATUS);
50012 + if (status & DAC_BIT(UNDERRUN)) {
50013 + dev_err(&dac->pdev->dev, "Underrun detected!\n");
50014 + dac_writel(dac, INT_CLR, DAC_BIT(UNDERRUN));
50015 + } else {
50016 + dev_err(&dac->pdev->dev, "Spurious interrupt (status=0x%x)\n",
50017 + status);
50018 + dac_writel(dac, INT_CLR, status);
50019 + }
50020 +
50021 + return IRQ_HANDLED;
50022 +}
50023 +
50024 +static ssize_t trans_s16be(struct at32_dac *dac, const char __user *ubuf,
50025 + size_t ucount)
50026 +{
50027 + ssize_t ret;
50028 +
50029 + if (dac->dsp_settings.channels == 2) {
50030 + const u32 __user *up = (const u32 __user *)ubuf;
50031 + u32 sample;
50032 +
50033 + for (ret = 0; ret < (ssize_t)(ucount - 3); ret += 4) {
50034 + if (!abdac_dma_space(dac))
50035 + break;
50036 +
50037 + if (unlikely(__get_user(sample, up++))) {
50038 + if (ret == 0)
50039 + ret = -EFAULT;
50040 + break;
50041 + }
50042 + dac->dma.buf[abdac_get_head(dac)] = sample;
50043 + dac->dma.head++;
50044 + }
50045 + } else {
50046 + const u16 __user *up = (const u16 __user *)ubuf;
50047 + u16 sample;
50048 +
50049 + for (ret = 0; ret < (ssize_t)(ucount - 1); ret += 2) {
50050 + if (!abdac_dma_space(dac))
50051 + break;
50052 +
50053 + if (unlikely(__get_user(sample, up++))) {
50054 + if (ret == 0)
50055 + ret = -EFAULT;
50056 + break;
50057 + }
50058 + dac->dma.buf[abdac_get_head(dac)]
50059 + = (sample << 16) | sample;
50060 + dac->dma.head++;
50061 + }
50062 + }
50063 +
50064 + return ret;
50065 +}
50066 +
50067 +static ssize_t trans_s16le(struct at32_dac *dac, const char __user *ubuf,
50068 + size_t ucount)
50069 +{
50070 + ssize_t ret;
50071 +
50072 + if (dac->dsp_settings.channels == 2) {
50073 + const u32 __user *up = (const u32 __user *)ubuf;
50074 + u32 sample;
50075 +
50076 + for (ret = 0; ret < (ssize_t)(ucount - 3); ret += 4) {
50077 + if (!abdac_dma_space(dac))
50078 + break;
50079 +
50080 + if (unlikely(__get_user(sample, up++))) {
50081 + if (ret == 0)
50082 + ret = -EFAULT;
50083 + break;
50084 + }
50085 + /* Swap bytes in each halfword */
50086 + dac->dma.buf[abdac_get_head(dac)] = swahb32(sample);
50087 + dac->dma.head++;
50088 + }
50089 + } else {
50090 + const u16 __user *up = (const u16 __user *)ubuf;
50091 + u16 sample;
50092 +
50093 + for (ret = 0; ret < (ssize_t)(ucount - 1); ret += 2) {
50094 + if (!abdac_dma_space(dac))
50095 + break;
50096 +
50097 + if (unlikely(__get_user(sample, up++))) {
50098 + if (ret == 0)
50099 + ret = -EFAULT;
50100 + break;
50101 + }
50102 + sample = swab16(sample);
50103 + dac->dma.buf[abdac_get_head(dac)]
50104 + = (sample << 16) | sample;
50105 + dac->dma.head++;
50106 + }
50107 + }
50108 +
50109 + return ret;
50110 +}
50111 +
50112 +static ssize_t abdac_dma_translate_from_user(struct at32_dac *dac,
50113 + const char __user *buffer,
50114 + size_t count)
50115 +{
50116 + /* At least one buffer must be available at this point */
50117 + dev_dbg(&dac->pdev->dev, "copying %zu bytes from user...\n", count);
50118 +
50119 + return dac->trans(dac, buffer, count);
50120 +}
50121 +
50122 +static int abdac_set_format(struct at32_dac *dac, int format)
50123 +{
50124 + unsigned int order;
50125 +
50126 + switch (format) {
50127 + case AFMT_S16_BE:
50128 + order = 1;
50129 + dac->trans = trans_s16be;
50130 + break;
50131 + case AFMT_S16_LE:
50132 + order = 1;
50133 + dac->trans = trans_s16le;
50134 + break;
50135 + default:
50136 + dev_dbg(&dac->pdev->dev, "unsupported format: %d\n", format);
50137 + return -EINVAL;
50138 + }
50139 +
50140 + if (dac->dsp_settings.channels == 2)
50141 + order++;
50142 +
50143 + dac->dsp_settings.input_order = order;
50144 + dac->dsp_settings.format = format;
50145 + return 0;
50146 +}
50147 +
50148 +static int abdac_set_sample_rate(struct at32_dac *dac, unsigned long rate)
50149 +{
50150 + unsigned long new_rate;
50151 + int ret;
50152 +
50153 + ret = clk_set_rate(dac->sample_clk, 256 * rate);
50154 + if (ret < 0)
50155 + return ret;
50156 +
50157 + /* TODO: mplayer seems to have a problem with this */
50158 +#if 0
50159 + new_rate = clk_get_rate(dac->sample_clk);
50160 + dac->dsp_settings.sample_rate = new_rate / 256;
50161 +#else
50162 + dac->dsp_settings.sample_rate = rate;
50163 +#endif
50164 +
50165 + return 0;
50166 +}
50167 +
50168 +static ssize_t abdac_dsp_write(struct file *file,
50169 + const char __user *buffer,
50170 + size_t count, loff_t *ppos)
50171 +{
50172 + struct at32_dac *dac = file->private_data;
50173 + DECLARE_WAITQUEUE(wait, current);
50174 + unsigned int avail;
50175 + ssize_t copied;
50176 + ssize_t ret;
50177 +
50178 + /* Avoid address space checking in the translation functions */
50179 + if (!access_ok(buffer, count, VERIFY_READ))
50180 + return -EFAULT;
50181 +
50182 + down(&dac->sem);
50183 +
50184 + if (!dac->dma.buf) {
50185 + ret = abdac_dma_prepare(dac);
50186 + if (ret)
50187 + goto out;
50188 + }
50189 +
50190 + add_wait_queue(&dac->write_wait, &wait);
50191 + ret = 0;
50192 + while (count > 0) {
50193 + do {
50194 + abdac_update_dma_tail(dac);
50195 + avail = abdac_dma_space(dac);
50196 + set_current_state(TASK_INTERRUPTIBLE);
50197 + if (avail >= DMA_WRITE_THRESHOLD)
50198 + break;
50199 +
50200 + if (file->f_flags & O_NONBLOCK) {
50201 + if (!ret)
50202 + ret = -EAGAIN;
50203 + goto out;
50204 + }
50205 +
50206 + pr_debug("Going to wait (avail = %u, count = %zu)\n",
50207 + avail, count);
50208 +
50209 + up(&dac->sem);
50210 + schedule();
50211 + if (signal_pending(current)) {
50212 + if (!ret)
50213 + ret = -ERESTARTSYS;
50214 + goto out_nosem;
50215 + }
50216 + down(&dac->sem);
50217 + } while (1);
50218 +
50219 + copied = abdac_dma_translate_from_user(dac, buffer, count);
50220 + if (copied < 0) {
50221 + if (!ret)
50222 + ret = -EFAULT;
50223 + goto out;
50224 + }
50225 +
50226 + abdac_start(dac);
50227 +
50228 + count -= copied;
50229 + ret += copied;
50230 + }
50231 +
50232 +out:
50233 + up(&dac->sem);
50234 +out_nosem:
50235 + remove_wait_queue(&dac->write_wait, &wait);
50236 + set_current_state(TASK_RUNNING);
50237 + return ret;
50238 +}
50239 +
50240 +static int abdac_dsp_ioctl(struct inode *inode, struct file *file,
50241 + unsigned int cmd, unsigned long arg)
50242 +{
50243 + struct at32_dac *dac = file->private_data;
50244 + int __user *up = (int __user *)arg;
50245 + struct audio_buf_info abinfo;
50246 + int val, ret;
50247 +
50248 + switch (cmd) {
50249 + case OSS_GETVERSION:
50250 + return put_user(SOUND_VERSION, up);
50251 +
50252 + case SNDCTL_DSP_SPEED:
50253 + if (get_user(val, up))
50254 + return -EFAULT;
50255 + if (val >= 0) {
50256 + abdac_stop(dac);
50257 + ret = abdac_set_sample_rate(dac, val);
50258 + if (ret)
50259 + return ret;
50260 + }
50261 + return put_user(dac->dsp_settings.sample_rate, up);
50262 +
50263 + case SNDCTL_DSP_STEREO:
50264 + if (get_user(val, up))
50265 + return -EFAULT;
50266 + abdac_stop(dac);
50267 + if (val && dac->dsp_settings.channels == 1)
50268 + dac->dsp_settings.input_order++;
50269 + else if (!val && dac->dsp_settings.channels != 1)
50270 + dac->dsp_settings.input_order--;
50271 + dac->dsp_settings.channels = val ? 2 : 1;
50272 + return 0;
50273 +
50274 + case SNDCTL_DSP_CHANNELS:
50275 + if (get_user(val, up))
50276 + return -EFAULT;
50277 +
50278 + if (val) {
50279 + if (val < 0 || val > 2)
50280 + return -EINVAL;
50281 +
50282 + abdac_stop(dac);
50283 + dac->dsp_settings.input_order
50284 + += val - dac->dsp_settings.channels;
50285 + dac->dsp_settings.channels = val;
50286 + }
50287 + return put_user(val, (int *)arg);
50288 +
50289 + case SNDCTL_DSP_GETFMTS:
50290 + return put_user(AFMT_S16_BE | AFMT_S16_BE, up);
50291 +
50292 + case SNDCTL_DSP_SETFMT:
50293 + if (get_user(val, up))
50294 + return -EFAULT;
50295 +
50296 + if (val == AFMT_QUERY) {
50297 + val = dac->dsp_settings.format;
50298 + } else {
50299 + ret = abdac_set_format(dac, val);
50300 + if (ret)
50301 + return ret;
50302 + }
50303 + return put_user(val, up);
50304 +
50305 + case SNDCTL_DSP_GETOSPACE:
50306 + abdac_update_dma_tail(dac);
50307 + abinfo.fragsize = ((1 << dac->dsp_settings.input_order)
50308 + * (DMA_PERIOD_SIZE / 4));
50309 + abinfo.bytes = (abdac_dma_space(dac)
50310 + << dac->dsp_settings.input_order);
50311 + abinfo.fragstotal = ((DMA_BUFFER_SIZE * 4)
50312 + >> (DMA_PERIOD_SHIFT
50313 + + dac->dsp_settings.input_order));
50314 + abinfo.fragments = ((abinfo.bytes
50315 + >> dac->dsp_settings.input_order)
50316 + / (DMA_PERIOD_SIZE / 4));
50317 + pr_debug("fragments=%d fragstotal=%d fragsize=%d bytes=%d\n",
50318 + abinfo.fragments, abinfo.fragstotal, abinfo.fragsize,
50319 + abinfo.bytes);
50320 + return copy_to_user(up, &abinfo, sizeof(abinfo)) ? -EFAULT : 0;
50321 +
50322 + default:
50323 + dev_dbg(&dac->pdev->dev, "Unimplemented ioctl cmd: 0x%x\n", cmd);
50324 + return -EINVAL;
50325 + }
50326 +}
50327 +
50328 +static int abdac_dsp_open(struct inode *inode, struct file *file)
50329 +{
50330 + struct at32_dac *dac = the_dac;
50331 + int ret;
50332 +
50333 + if (file->f_mode & FMODE_READ)
50334 + return -ENXIO;
50335 +
50336 + down(&dac->sem);
50337 + ret = -EBUSY;
50338 + if (dac->busy)
50339 + goto out;
50340 +
50341 + dac->dma.head = dac->dma.tail = 0;
50342 +
50343 + /* FIXME: What are the correct defaults? */
50344 + dac->dsp_settings.channels = 2;
50345 + abdac_set_format(dac, AFMT_S16_BE);
50346 + ret = abdac_set_sample_rate(dac, 8000);
50347 + if (ret)
50348 + goto out;
50349 +
50350 + file->private_data = dac;
50351 + dac->busy = 1;
50352 +
50353 + ret = 0;
50354 +
50355 +out:
50356 + up(&dac->sem);
50357 + return ret;
50358 +}
50359 +
50360 +static int abdac_dsp_release(struct inode *inode, struct file *file)
50361 +{
50362 + struct at32_dac *dac = file->private_data;
50363 +
50364 + down(&dac->sem);
50365 +
50366 + abdac_stop(dac);
50367 + abdac_dma_cleanup(dac);
50368 + dac->busy = 0;
50369 +
50370 + up(&dac->sem);
50371 +
50372 + return 0;
50373 +}
50374 +
50375 +static struct file_operations abdac_dsp_fops = {
50376 + .owner = THIS_MODULE,
50377 + .llseek = no_llseek,
50378 + .write = abdac_dsp_write,
50379 + .ioctl = abdac_dsp_ioctl,
50380 + .open = abdac_dsp_open,
50381 + .release = abdac_dsp_release,
50382 +};
50383 +
50384 +static int __init abdac_probe(struct platform_device *pdev)
50385 +{
50386 + struct at32_dac *dac;
50387 + struct resource *regs;
50388 + struct clk *mck;
50389 + struct clk *sample_clk;
50390 + int irq;
50391 + int ret;
50392 +
50393 + if (the_dac)
50394 + return -EBUSY;
50395 +
50396 + regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
50397 + if (!regs)
50398 + return -ENXIO;
50399 + irq = platform_get_irq(pdev, 0);
50400 + if (irq < 0)
50401 + return irq;
50402 +
50403 + mck = clk_get(&pdev->dev, "pclk");
50404 + if (IS_ERR(mck))
50405 + return PTR_ERR(mck);
50406 + sample_clk = clk_get(&pdev->dev, "sample_clk");
50407 + if (IS_ERR(sample_clk)) {
50408 + ret = PTR_ERR(sample_clk);
50409 + goto out_put_mck;
50410 + }
50411 + clk_enable(mck);
50412 +
50413 + ret = -ENOMEM;
50414 + dac = kzalloc(sizeof(struct at32_dac), GFP_KERNEL);
50415 + if (!dac)
50416 + goto out_disable_clk;
50417 +
50418 + spin_lock_init(&dac->lock);
50419 + init_MUTEX(&dac->sem);
50420 + init_waitqueue_head(&dac->write_wait);
50421 + dac->pdev = pdev;
50422 + dac->mck = mck;
50423 + dac->sample_clk = sample_clk;
50424 +
50425 + dac->regs = ioremap(regs->start, regs->end - regs->start + 1);
50426 + if (!dac->regs)
50427 + goto out_free_dac;
50428 +
50429 + ret = request_irq(irq, abdac_interrupt, 0, "dac", dac);
50430 + if (ret)
50431 + goto out_unmap_regs;
50432 +
50433 + /* FIXME */
50434 + dac->req.req.dmac = find_dma_controller(0);
50435 + if (!dac->req.req.dmac)
50436 + goto out_free_irq;
50437 +
50438 + ret = dma_alloc_channel(dac->req.req.dmac);
50439 + if (ret < 0)
50440 + goto out_free_irq;
50441 +
50442 + dac->req.req.channel = ret;
50443 + dac->req.req.block_complete = abdac_dma_block_complete;
50444 + dac->req.req.error = abdac_dma_error;
50445 + dac->req.data_reg = regs->start + DAC_DATA;
50446 + dac->req.periph_id = 2; /* FIXME */
50447 + dac->req.direction = DMA_DIR_MEM_TO_PERIPH;
50448 + dac->req.width = DMA_WIDTH_32BIT;
50449 +
50450 + /* Make sure the DAC is silent and disabled */
50451 + dac_writel(dac, DATA, 0);
50452 + dac_writel(dac, CTRL, 0);
50453 +
50454 + ret = register_sound_dsp(&abdac_dsp_fops, -1);
50455 + if (ret < 0)
50456 + goto out_free_dma;
50457 + dac->dev_dsp = ret;
50458 +
50459 + /* TODO: Register mixer */
50460 +
50461 + the_dac = dac;
50462 + platform_set_drvdata(pdev, dac);
50463 +
50464 + return 0;
50465 +
50466 +out_free_dma:
50467 + dma_release_channel(dac->req.req.dmac, dac->req.req.channel);
50468 +out_free_irq:
50469 + free_irq(irq, dac);
50470 +out_unmap_regs:
50471 + iounmap(dac->regs);
50472 +out_free_dac:
50473 + kfree(dac);
50474 +out_disable_clk:
50475 + clk_disable(mck);
50476 + clk_put(sample_clk);
50477 +out_put_mck:
50478 + clk_put(mck);
50479 + return ret;
50480 +}
50481 +
50482 +static int __exit abdac_remove(struct platform_device *pdev)
50483 +{
50484 + struct at32_dac *dac;
50485 +
50486 + dac = platform_get_drvdata(pdev);
50487 + if (dac) {
50488 + unregister_sound_dsp(dac->dev_dsp);
50489 + dma_release_channel(dac->req.req.dmac, dac->req.req.channel);
50490 + free_irq(platform_get_irq(pdev, 0), dac);
50491 + iounmap(dac->regs);
50492 + clk_disable(dac->mck);
50493 + clk_put(dac->sample_clk);
50494 + clk_put(dac->mck);
50495 + kfree(dac);
50496 + platform_set_drvdata(pdev, NULL);
50497 + the_dac = NULL;
50498 + }
50499 +
50500 + return 0;
50501 +}
50502 +
50503 +static struct platform_driver abdac_driver = {
50504 + .remove = __exit_p(abdac_remove),
50505 + .driver = {
50506 + .name = "abdac",
50507 + },
50508 +};
50509 +
50510 +static int __init abdac_init(void)
50511 +{
50512 + return platform_driver_probe(&abdac_driver, abdac_probe);
50513 +}
50514 +module_init(abdac_init);
50515 +
50516 +static void __exit abdac_exit(void)
50517 +{
50518 + platform_driver_unregister(&abdac_driver);
50519 +}
50520 +module_exit(abdac_exit);
50521 +
50522 +MODULE_AUTHOR("Haavard Skinnemoen <hskinnemoen@atmel.com>");
50523 +MODULE_DESCRIPTION("Sound Driver for the Atmel AT32 ABDAC");
50524 +MODULE_LICENSE("GPL");
50525 diff --exclude=.git -urN linux-2.6.25.6/sound/oss/at32_abdac.h avr32-2.6/sound/oss/at32_abdac.h
50526 --- linux-2.6.25.6/sound/oss/at32_abdac.h 1970-01-01 01:00:00.000000000 +0100
50527 +++ avr32-2.6/sound/oss/at32_abdac.h 2008-06-12 15:09:47.027815755 +0200
50528 @@ -0,0 +1,59 @@
50529 +/*
50530 + * Register definitions for the Atmel AT32 on-chip DAC.
50531 + *
50532 + * Copyright (C) 2006 Atmel Corporation
50533 + *
50534 + * This program is free software; you can redistribute it and/or modify
50535 + * it under the terms of the GNU General Public License version 2 as
50536 + * published by the Free Software Foundation.
50537 + */
50538 +#ifndef __SOUND_OSS_AT32_ABDAC_H__
50539 +#define __SOUND_OSS_AT32_ABDAC_H__
50540 +
50541 +/* DAC register offsets */
50542 +#define DAC_DATA 0x0000
50543 +#define DAC_CTRL 0x0008
50544 +#define DAC_INT_MASK 0x000c
50545 +#define DAC_INT_EN 0x0010
50546 +#define DAC_INT_DIS 0x0014
50547 +#define DAC_INT_CLR 0x0018
50548 +#define DAC_INT_STATUS 0x001c
50549 +#define DAC_PDC_DATA 0x0020
50550 +
50551 +/* Bitfields in CTRL */
50552 +#define DAC_SWAP_OFFSET 30
50553 +#define DAC_SWAP_SIZE 1
50554 +#define DAC_EN_OFFSET 31
50555 +#define DAC_EN_SIZE 1
50556 +
50557 +/* Bitfields in INT_MASK/INT_EN/INT_DIS/INT_STATUS/INT_CLR */
50558 +#define DAC_UNDERRUN_OFFSET 28
50559 +#define DAC_UNDERRUN_SIZE 1
50560 +#define DAC_TX_READY_OFFSET 29
50561 +#define DAC_TX_READY_SIZE 1
50562 +#define DAC_TX_BUFFER_EMPTY_OFFSET 30
50563 +#define DAC_TX_BUFFER_EMPTY_SIZE 1
50564 +#define DAC_CHANNEL_TX_END_OFFSET 31
50565 +#define DAC_CHANNEL_TX_END_SIZE 1
50566 +
50567 +/* Bit manipulation macros */
50568 +#define DAC_BIT(name) \
50569 + (1 << DAC_##name##_OFFSET)
50570 +#define DAC_BF(name, value) \
50571 + (((value) & ((1 << DAC_##name##_SIZE) - 1)) \
50572 + << DAC_##name##_OFFSET)
50573 +#define DAC_BFEXT(name, value) \
50574 + (((value) >> DAC_##name##_OFFSET) \
50575 + & ((1 << DAC_##name##_SIZE) - 1))
50576 +#define DAC_BFINS(name, value, old) \
50577 + (((old) & ~(((1 << DAC_##name##_SIZE) - 1) \
50578 + << DAC_##name##_OFFSET)) \
50579 + | DAC_BF(name,value))
50580 +
50581 +/* Register access macros */
50582 +#define dac_readl(port, reg) \
50583 + __raw_readl((port)->regs + DAC_##reg)
50584 +#define dac_writel(port, reg, value) \
50585 + __raw_writel((value), (port)->regs + DAC_##reg)
50586 +
50587 +#endif /* __SOUND_OSS_AT32_ABDAC_H__ */
50588 diff --exclude=.git -urN linux-2.6.25.6/sound/oss/Kconfig avr32-2.6/sound/oss/Kconfig
50589 --- linux-2.6.25.6/sound/oss/Kconfig 2008-06-09 20:27:19.000000000 +0200
50590 +++ avr32-2.6/sound/oss/Kconfig 2008-06-12 15:09:47.023815804 +0200
50591 @@ -654,3 +654,7 @@
50592 int "DAC channel"
50593 default "1"
50594 depends on SOUND_SH_DAC_AUDIO
50595 +
50596 +config SOUND_AT32_ABDAC
50597 + tristate "Atmel AT32 Audio Bitstream DAC (ABDAC) support"
50598 + depends on SOUND_PRIME && AVR32
50599 diff --exclude=.git -urN linux-2.6.25.6/sound/oss/Makefile avr32-2.6/sound/oss/Makefile
50600 --- linux-2.6.25.6/sound/oss/Makefile 2008-06-09 20:27:19.000000000 +0200
50601 +++ avr32-2.6/sound/oss/Makefile 2008-06-12 15:09:47.023815804 +0200
50602 @@ -9,6 +9,7 @@
50603
50604 # Please leave it as is, cause the link order is significant !
50605
50606 +obj-$(CONFIG_SOUND_AT32_ABDAC) += at32_abdac.o
50607 obj-$(CONFIG_SOUND_SH_DAC_AUDIO) += sh_dac_audio.o
50608 obj-$(CONFIG_SOUND_HAL2) += hal2.o
50609 obj-$(CONFIG_SOUND_AEDSP16) += aedsp16.o
50610 diff --exclude=.git -urN linux-2.6.25.6/sound/spi/at73c213.c avr32-2.6/sound/spi/at73c213.c
50611 --- linux-2.6.25.6/sound/spi/at73c213.c 2008-06-09 20:27:19.000000000 +0200
50612 +++ avr32-2.6/sound/spi/at73c213.c 2008-06-12 15:09:47.247815006 +0200
50613 @@ -737,7 +737,7 @@
50614 /*
50615 * Device functions
50616 */
50617 -static int snd_at73c213_ssc_init(struct snd_at73c213 *chip)
50618 +static int __devinit snd_at73c213_ssc_init(struct snd_at73c213 *chip)
50619 {
50620 /*
50621 * Continuous clock output.
50622 @@ -767,7 +767,7 @@
50623 return 0;
50624 }
50625
50626 -static int snd_at73c213_chip_init(struct snd_at73c213 *chip)
50627 +static int __devinit snd_at73c213_chip_init(struct snd_at73c213 *chip)
50628 {
50629 int retval;
50630 unsigned char dac_ctrl = 0;
50631 @@ -933,7 +933,7 @@
50632 return retval;
50633 }
50634
50635 -static int snd_at73c213_probe(struct spi_device *spi)
50636 +static int __devinit snd_at73c213_probe(struct spi_device *spi)
50637 {
50638 struct snd_card *card;
50639 struct snd_at73c213 *chip;