Commit | Line | Data |
---|---|---|
1e51764a AB |
1 | /* |
2 | * This file is part of UBIFS. | |
3 | * | |
4 | * Copyright (C) 2006-2008 Nokia Corporation. | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify it | |
7 | * under the terms of the GNU General Public License version 2 as published by | |
8 | * the Free Software Foundation. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, but WITHOUT | |
11 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
12 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
13 | * more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License along with | |
16 | * this program; if not, write to the Free Software Foundation, Inc., 51 | |
17 | * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
18 | * | |
19 | * Authors: Adrian Hunter | |
20 | * Artem Bityutskiy (Битюцкий Артём) | |
21 | */ | |
22 | ||
23 | /* | |
24 | * This file implements the LEB properties tree (LPT) area. The LPT area | |
25 | * contains the LEB properties tree, a table of LPT area eraseblocks (ltab), and | |
26 | * (for the "big" model) a table of saved LEB numbers (lsave). The LPT area sits | |
27 | * between the log and the orphan area. | |
28 | * | |
29 | * The LPT area is like a miniature self-contained file system. It is required | |
30 | * that it never runs out of space, is fast to access and update, and scales | |
31 | * logarithmically. The LEB properties tree is implemented as a wandering tree | |
32 | * much like the TNC, and the LPT area has its own garbage collection. | |
33 | * | |
34 | * The LPT has two slightly different forms called the "small model" and the | |
35 | * "big model". The small model is used when the entire LEB properties table | |
36 | * can be written into a single eraseblock. In that case, garbage collection | |
37 | * consists of just writing the whole table, which therefore makes all other | |
38 | * eraseblocks reusable. In the case of the big model, dirty eraseblocks are | |
39 | * selected for garbage collection, which consists are marking the nodes in | |
40 | * that LEB as dirty, and then only the dirty nodes are written out. Also, in | |
41 | * the case of the big model, a table of LEB numbers is saved so that the entire | |
42 | * LPT does not to be scanned looking for empty eraseblocks when UBIFS is first | |
43 | * mounted. | |
44 | */ | |
45 | ||
46 | #include <linux/crc16.h> | |
47 | #include "ubifs.h" | |
48 | ||
49 | /** | |
50 | * do_calc_lpt_geom - calculate sizes for the LPT area. | |
51 | * @c: the UBIFS file-system description object | |
52 | * | |
53 | * Calculate the sizes of LPT bit fields, nodes, and tree, based on the | |
54 | * properties of the flash and whether LPT is "big" (c->big_lpt). | |
55 | */ | |
56 | static void do_calc_lpt_geom(struct ubifs_info *c) | |
57 | { | |
58 | int i, n, bits, per_leb_wastage, max_pnode_cnt; | |
59 | long long sz, tot_wastage; | |
60 | ||
61 | n = c->main_lebs + c->max_leb_cnt - c->leb_cnt; | |
62 | max_pnode_cnt = DIV_ROUND_UP(n, UBIFS_LPT_FANOUT); | |
63 | ||
64 | c->lpt_hght = 1; | |
65 | n = UBIFS_LPT_FANOUT; | |
66 | while (n < max_pnode_cnt) { | |
67 | c->lpt_hght += 1; | |
68 | n <<= UBIFS_LPT_FANOUT_SHIFT; | |
69 | } | |
70 | ||
71 | c->pnode_cnt = DIV_ROUND_UP(c->main_lebs, UBIFS_LPT_FANOUT); | |
72 | ||
73 | n = DIV_ROUND_UP(c->pnode_cnt, UBIFS_LPT_FANOUT); | |
74 | c->nnode_cnt = n; | |
75 | for (i = 1; i < c->lpt_hght; i++) { | |
76 | n = DIV_ROUND_UP(n, UBIFS_LPT_FANOUT); | |
77 | c->nnode_cnt += n; | |
78 | } | |
79 | ||
80 | c->space_bits = fls(c->leb_size) - 3; | |
81 | c->lpt_lnum_bits = fls(c->lpt_lebs); | |
82 | c->lpt_offs_bits = fls(c->leb_size - 1); | |
83 | c->lpt_spc_bits = fls(c->leb_size); | |
84 | ||
85 | n = DIV_ROUND_UP(c->max_leb_cnt, UBIFS_LPT_FANOUT); | |
86 | c->pcnt_bits = fls(n - 1); | |
87 | ||
88 | c->lnum_bits = fls(c->max_leb_cnt - 1); | |
89 | ||
90 | bits = UBIFS_LPT_CRC_BITS + UBIFS_LPT_TYPE_BITS + | |
91 | (c->big_lpt ? c->pcnt_bits : 0) + | |
92 | (c->space_bits * 2 + 1) * UBIFS_LPT_FANOUT; | |
93 | c->pnode_sz = (bits + 7) / 8; | |
94 | ||
95 | bits = UBIFS_LPT_CRC_BITS + UBIFS_LPT_TYPE_BITS + | |
96 | (c->big_lpt ? c->pcnt_bits : 0) + | |
97 | (c->lpt_lnum_bits + c->lpt_offs_bits) * UBIFS_LPT_FANOUT; | |
98 | c->nnode_sz = (bits + 7) / 8; | |
99 | ||
100 | bits = UBIFS_LPT_CRC_BITS + UBIFS_LPT_TYPE_BITS + | |
101 | c->lpt_lebs * c->lpt_spc_bits * 2; | |
102 | c->ltab_sz = (bits + 7) / 8; | |
103 | ||
104 | bits = UBIFS_LPT_CRC_BITS + UBIFS_LPT_TYPE_BITS + | |
105 | c->lnum_bits * c->lsave_cnt; | |
106 | c->lsave_sz = (bits + 7) / 8; | |
107 | ||
108 | /* Calculate the minimum LPT size */ | |
109 | c->lpt_sz = (long long)c->pnode_cnt * c->pnode_sz; | |
110 | c->lpt_sz += (long long)c->nnode_cnt * c->nnode_sz; | |
111 | c->lpt_sz += c->ltab_sz; | |
73944a6d AH |
112 | if (c->big_lpt) |
113 | c->lpt_sz += c->lsave_sz; | |
1e51764a AB |
114 | |
115 | /* Add wastage */ | |
116 | sz = c->lpt_sz; | |
117 | per_leb_wastage = max_t(int, c->pnode_sz, c->nnode_sz); | |
118 | sz += per_leb_wastage; | |
119 | tot_wastage = per_leb_wastage; | |
120 | while (sz > c->leb_size) { | |
121 | sz += per_leb_wastage; | |
122 | sz -= c->leb_size; | |
123 | tot_wastage += per_leb_wastage; | |
124 | } | |
125 | tot_wastage += ALIGN(sz, c->min_io_size) - sz; | |
126 | c->lpt_sz += tot_wastage; | |
127 | } | |
128 | ||
129 | /** | |
130 | * ubifs_calc_lpt_geom - calculate and check sizes for the LPT area. | |
131 | * @c: the UBIFS file-system description object | |
132 | * | |
133 | * This function returns %0 on success and a negative error code on failure. | |
134 | */ | |
135 | int ubifs_calc_lpt_geom(struct ubifs_info *c) | |
136 | { | |
137 | int lebs_needed; | |
138 | uint64_t sz; | |
139 | ||
140 | do_calc_lpt_geom(c); | |
141 | ||
142 | /* Verify that lpt_lebs is big enough */ | |
143 | sz = c->lpt_sz * 2; /* Must have at least 2 times the size */ | |
144 | sz += c->leb_size - 1; | |
145 | do_div(sz, c->leb_size); | |
146 | lebs_needed = sz; | |
147 | if (lebs_needed > c->lpt_lebs) { | |
148 | ubifs_err("too few LPT LEBs"); | |
149 | return -EINVAL; | |
150 | } | |
151 | ||
152 | /* Verify that ltab fits in a single LEB (since ltab is a single node */ | |
153 | if (c->ltab_sz > c->leb_size) { | |
154 | ubifs_err("LPT ltab too big"); | |
155 | return -EINVAL; | |
156 | } | |
157 | ||
158 | c->check_lpt_free = c->big_lpt; | |
159 | ||
160 | return 0; | |
161 | } | |
162 | ||
163 | /** | |
164 | * calc_dflt_lpt_geom - calculate default LPT geometry. | |
165 | * @c: the UBIFS file-system description object | |
166 | * @main_lebs: number of main area LEBs is passed and returned here | |
167 | * @big_lpt: whether the LPT area is "big" is returned here | |
168 | * | |
169 | * The size of the LPT area depends on parameters that themselves are dependent | |
170 | * on the size of the LPT area. This function, successively recalculates the LPT | |
171 | * area geometry until the parameters and resultant geometry are consistent. | |
172 | * | |
173 | * This function returns %0 on success and a negative error code on failure. | |
174 | */ | |
175 | static int calc_dflt_lpt_geom(struct ubifs_info *c, int *main_lebs, | |
176 | int *big_lpt) | |
177 | { | |
178 | int i, lebs_needed; | |
179 | uint64_t sz; | |
180 | ||
181 | /* Start by assuming the minimum number of LPT LEBs */ | |
182 | c->lpt_lebs = UBIFS_MIN_LPT_LEBS; | |
183 | c->main_lebs = *main_lebs - c->lpt_lebs; | |
184 | if (c->main_lebs <= 0) | |
185 | return -EINVAL; | |
186 | ||
187 | /* And assume we will use the small LPT model */ | |
188 | c->big_lpt = 0; | |
189 | ||
190 | /* | |
191 | * Calculate the geometry based on assumptions above and then see if it | |
192 | * makes sense | |
193 | */ | |
194 | do_calc_lpt_geom(c); | |
195 | ||
196 | /* Small LPT model must have lpt_sz < leb_size */ | |
197 | if (c->lpt_sz > c->leb_size) { | |
198 | /* Nope, so try again using big LPT model */ | |
199 | c->big_lpt = 1; | |
200 | do_calc_lpt_geom(c); | |
201 | } | |
202 | ||
203 | /* Now check there are enough LPT LEBs */ | |
204 | for (i = 0; i < 64 ; i++) { | |
205 | sz = c->lpt_sz * 4; /* Allow 4 times the size */ | |
206 | sz += c->leb_size - 1; | |
207 | do_div(sz, c->leb_size); | |
208 | lebs_needed = sz; | |
209 | if (lebs_needed > c->lpt_lebs) { | |
210 | /* Not enough LPT LEBs so try again with more */ | |
211 | c->lpt_lebs = lebs_needed; | |
212 | c->main_lebs = *main_lebs - c->lpt_lebs; | |
213 | if (c->main_lebs <= 0) | |
214 | return -EINVAL; | |
215 | do_calc_lpt_geom(c); | |
216 | continue; | |
217 | } | |
218 | if (c->ltab_sz > c->leb_size) { | |
219 | ubifs_err("LPT ltab too big"); | |
220 | return -EINVAL; | |
221 | } | |
222 | *main_lebs = c->main_lebs; | |
223 | *big_lpt = c->big_lpt; | |
224 | return 0; | |
225 | } | |
226 | return -EINVAL; | |
227 | } | |
228 | ||
229 | /** | |
230 | * pack_bits - pack bit fields end-to-end. | |
231 | * @addr: address at which to pack (passed and next address returned) | |
232 | * @pos: bit position at which to pack (passed and next position returned) | |
233 | * @val: value to pack | |
234 | * @nrbits: number of bits of value to pack (1-32) | |
235 | */ | |
236 | static void pack_bits(uint8_t **addr, int *pos, uint32_t val, int nrbits) | |
237 | { | |
238 | uint8_t *p = *addr; | |
239 | int b = *pos; | |
240 | ||
241 | ubifs_assert(nrbits > 0); | |
242 | ubifs_assert(nrbits <= 32); | |
243 | ubifs_assert(*pos >= 0); | |
244 | ubifs_assert(*pos < 8); | |
245 | ubifs_assert((val >> nrbits) == 0 || nrbits == 32); | |
246 | if (b) { | |
247 | *p |= ((uint8_t)val) << b; | |
248 | nrbits += b; | |
249 | if (nrbits > 8) { | |
250 | *++p = (uint8_t)(val >>= (8 - b)); | |
251 | if (nrbits > 16) { | |
252 | *++p = (uint8_t)(val >>= 8); | |
253 | if (nrbits > 24) { | |
254 | *++p = (uint8_t)(val >>= 8); | |
255 | if (nrbits > 32) | |
256 | *++p = (uint8_t)(val >>= 8); | |
257 | } | |
258 | } | |
259 | } | |
260 | } else { | |
261 | *p = (uint8_t)val; | |
262 | if (nrbits > 8) { | |
263 | *++p = (uint8_t)(val >>= 8); | |
264 | if (nrbits > 16) { | |
265 | *++p = (uint8_t)(val >>= 8); | |
266 | if (nrbits > 24) | |
267 | *++p = (uint8_t)(val >>= 8); | |
268 | } | |
269 | } | |
270 | } | |
271 | b = nrbits & 7; | |
272 | if (b == 0) | |
273 | p++; | |
274 | *addr = p; | |
275 | *pos = b; | |
276 | } | |
277 | ||
278 | /** | |
279 | * ubifs_unpack_bits - unpack bit fields. | |
280 | * @addr: address at which to unpack (passed and next address returned) | |
281 | * @pos: bit position at which to unpack (passed and next position returned) | |
282 | * @nrbits: number of bits of value to unpack (1-32) | |
283 | * | |
284 | * This functions returns the value unpacked. | |
285 | */ | |
286 | uint32_t ubifs_unpack_bits(uint8_t **addr, int *pos, int nrbits) | |
287 | { | |
288 | const int k = 32 - nrbits; | |
289 | uint8_t *p = *addr; | |
290 | int b = *pos; | |
291 | uint32_t val; | |
292 | ||
293 | ubifs_assert(nrbits > 0); | |
294 | ubifs_assert(nrbits <= 32); | |
295 | ubifs_assert(*pos >= 0); | |
296 | ubifs_assert(*pos < 8); | |
297 | if (b) { | |
298 | val = p[1] | ((uint32_t)p[2] << 8) | ((uint32_t)p[3] << 16) | | |
299 | ((uint32_t)p[4] << 24); | |
300 | val <<= (8 - b); | |
301 | val |= *p >> b; | |
302 | nrbits += b; | |
303 | } else | |
304 | val = p[0] | ((uint32_t)p[1] << 8) | ((uint32_t)p[2] << 16) | | |
305 | ((uint32_t)p[3] << 24); | |
306 | val <<= k; | |
307 | val >>= k; | |
308 | b = nrbits & 7; | |
309 | p += nrbits / 8; | |
310 | *addr = p; | |
311 | *pos = b; | |
312 | ubifs_assert((val >> nrbits) == 0 || nrbits - b == 32); | |
313 | return val; | |
314 | } | |
315 | ||
316 | /** | |
317 | * ubifs_pack_pnode - pack all the bit fields of a pnode. | |
318 | * @c: UBIFS file-system description object | |
319 | * @buf: buffer into which to pack | |
320 | * @pnode: pnode to pack | |
321 | */ | |
322 | void ubifs_pack_pnode(struct ubifs_info *c, void *buf, | |
323 | struct ubifs_pnode *pnode) | |
324 | { | |
325 | uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES; | |
326 | int i, pos = 0; | |
327 | uint16_t crc; | |
328 | ||
329 | pack_bits(&addr, &pos, UBIFS_LPT_PNODE, UBIFS_LPT_TYPE_BITS); | |
330 | if (c->big_lpt) | |
331 | pack_bits(&addr, &pos, pnode->num, c->pcnt_bits); | |
332 | for (i = 0; i < UBIFS_LPT_FANOUT; i++) { | |
333 | pack_bits(&addr, &pos, pnode->lprops[i].free >> 3, | |
334 | c->space_bits); | |
335 | pack_bits(&addr, &pos, pnode->lprops[i].dirty >> 3, | |
336 | c->space_bits); | |
337 | if (pnode->lprops[i].flags & LPROPS_INDEX) | |
338 | pack_bits(&addr, &pos, 1, 1); | |
339 | else | |
340 | pack_bits(&addr, &pos, 0, 1); | |
341 | } | |
342 | crc = crc16(-1, buf + UBIFS_LPT_CRC_BYTES, | |
343 | c->pnode_sz - UBIFS_LPT_CRC_BYTES); | |
344 | addr = buf; | |
345 | pos = 0; | |
346 | pack_bits(&addr, &pos, crc, UBIFS_LPT_CRC_BITS); | |
347 | } | |
348 | ||
349 | /** | |
350 | * ubifs_pack_nnode - pack all the bit fields of a nnode. | |
351 | * @c: UBIFS file-system description object | |
352 | * @buf: buffer into which to pack | |
353 | * @nnode: nnode to pack | |
354 | */ | |
355 | void ubifs_pack_nnode(struct ubifs_info *c, void *buf, | |
356 | struct ubifs_nnode *nnode) | |
357 | { | |
358 | uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES; | |
359 | int i, pos = 0; | |
360 | uint16_t crc; | |
361 | ||
362 | pack_bits(&addr, &pos, UBIFS_LPT_NNODE, UBIFS_LPT_TYPE_BITS); | |
363 | if (c->big_lpt) | |
364 | pack_bits(&addr, &pos, nnode->num, c->pcnt_bits); | |
365 | for (i = 0; i < UBIFS_LPT_FANOUT; i++) { | |
366 | int lnum = nnode->nbranch[i].lnum; | |
367 | ||
368 | if (lnum == 0) | |
369 | lnum = c->lpt_last + 1; | |
370 | pack_bits(&addr, &pos, lnum - c->lpt_first, c->lpt_lnum_bits); | |
371 | pack_bits(&addr, &pos, nnode->nbranch[i].offs, | |
372 | c->lpt_offs_bits); | |
373 | } | |
374 | crc = crc16(-1, buf + UBIFS_LPT_CRC_BYTES, | |
375 | c->nnode_sz - UBIFS_LPT_CRC_BYTES); | |
376 | addr = buf; | |
377 | pos = 0; | |
378 | pack_bits(&addr, &pos, crc, UBIFS_LPT_CRC_BITS); | |
379 | } | |
380 | ||
381 | /** | |
382 | * ubifs_pack_ltab - pack the LPT's own lprops table. | |
383 | * @c: UBIFS file-system description object | |
384 | * @buf: buffer into which to pack | |
385 | * @ltab: LPT's own lprops table to pack | |
386 | */ | |
387 | void ubifs_pack_ltab(struct ubifs_info *c, void *buf, | |
388 | struct ubifs_lpt_lprops *ltab) | |
389 | { | |
390 | uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES; | |
391 | int i, pos = 0; | |
392 | uint16_t crc; | |
393 | ||
394 | pack_bits(&addr, &pos, UBIFS_LPT_LTAB, UBIFS_LPT_TYPE_BITS); | |
395 | for (i = 0; i < c->lpt_lebs; i++) { | |
396 | pack_bits(&addr, &pos, ltab[i].free, c->lpt_spc_bits); | |
397 | pack_bits(&addr, &pos, ltab[i].dirty, c->lpt_spc_bits); | |
398 | } | |
399 | crc = crc16(-1, buf + UBIFS_LPT_CRC_BYTES, | |
400 | c->ltab_sz - UBIFS_LPT_CRC_BYTES); | |
401 | addr = buf; | |
402 | pos = 0; | |
403 | pack_bits(&addr, &pos, crc, UBIFS_LPT_CRC_BITS); | |
404 | } | |
405 | ||
406 | /** | |
407 | * ubifs_pack_lsave - pack the LPT's save table. | |
408 | * @c: UBIFS file-system description object | |
409 | * @buf: buffer into which to pack | |
410 | * @lsave: LPT's save table to pack | |
411 | */ | |
412 | void ubifs_pack_lsave(struct ubifs_info *c, void *buf, int *lsave) | |
413 | { | |
414 | uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES; | |
415 | int i, pos = 0; | |
416 | uint16_t crc; | |
417 | ||
418 | pack_bits(&addr, &pos, UBIFS_LPT_LSAVE, UBIFS_LPT_TYPE_BITS); | |
419 | for (i = 0; i < c->lsave_cnt; i++) | |
420 | pack_bits(&addr, &pos, lsave[i], c->lnum_bits); | |
421 | crc = crc16(-1, buf + UBIFS_LPT_CRC_BYTES, | |
422 | c->lsave_sz - UBIFS_LPT_CRC_BYTES); | |
423 | addr = buf; | |
424 | pos = 0; | |
425 | pack_bits(&addr, &pos, crc, UBIFS_LPT_CRC_BITS); | |
426 | } | |
427 | ||
428 | /** | |
429 | * ubifs_add_lpt_dirt - add dirty space to LPT LEB properties. | |
430 | * @c: UBIFS file-system description object | |
431 | * @lnum: LEB number to which to add dirty space | |
432 | * @dirty: amount of dirty space to add | |
433 | */ | |
434 | void ubifs_add_lpt_dirt(struct ubifs_info *c, int lnum, int dirty) | |
435 | { | |
436 | if (!dirty || !lnum) | |
437 | return; | |
438 | dbg_lp("LEB %d add %d to %d", | |
439 | lnum, dirty, c->ltab[lnum - c->lpt_first].dirty); | |
440 | ubifs_assert(lnum >= c->lpt_first && lnum <= c->lpt_last); | |
441 | c->ltab[lnum - c->lpt_first].dirty += dirty; | |
442 | } | |
443 | ||
444 | /** | |
445 | * set_ltab - set LPT LEB properties. | |
446 | * @c: UBIFS file-system description object | |
447 | * @lnum: LEB number | |
448 | * @free: amount of free space | |
449 | * @dirty: amount of dirty space | |
450 | */ | |
451 | static void set_ltab(struct ubifs_info *c, int lnum, int free, int dirty) | |
452 | { | |
453 | dbg_lp("LEB %d free %d dirty %d to %d %d", | |
454 | lnum, c->ltab[lnum - c->lpt_first].free, | |
455 | c->ltab[lnum - c->lpt_first].dirty, free, dirty); | |
456 | ubifs_assert(lnum >= c->lpt_first && lnum <= c->lpt_last); | |
457 | c->ltab[lnum - c->lpt_first].free = free; | |
458 | c->ltab[lnum - c->lpt_first].dirty = dirty; | |
459 | } | |
460 | ||
461 | /** | |
462 | * ubifs_add_nnode_dirt - add dirty space to LPT LEB properties. | |
463 | * @c: UBIFS file-system description object | |
464 | * @nnode: nnode for which to add dirt | |
465 | */ | |
466 | void ubifs_add_nnode_dirt(struct ubifs_info *c, struct ubifs_nnode *nnode) | |
467 | { | |
468 | struct ubifs_nnode *np = nnode->parent; | |
469 | ||
470 | if (np) | |
471 | ubifs_add_lpt_dirt(c, np->nbranch[nnode->iip].lnum, | |
472 | c->nnode_sz); | |
473 | else { | |
474 | ubifs_add_lpt_dirt(c, c->lpt_lnum, c->nnode_sz); | |
475 | if (!(c->lpt_drty_flgs & LTAB_DIRTY)) { | |
476 | c->lpt_drty_flgs |= LTAB_DIRTY; | |
477 | ubifs_add_lpt_dirt(c, c->ltab_lnum, c->ltab_sz); | |
478 | } | |
479 | } | |
480 | } | |
481 | ||
482 | /** | |
483 | * add_pnode_dirt - add dirty space to LPT LEB properties. | |
484 | * @c: UBIFS file-system description object | |
485 | * @pnode: pnode for which to add dirt | |
486 | */ | |
487 | static void add_pnode_dirt(struct ubifs_info *c, struct ubifs_pnode *pnode) | |
488 | { | |
489 | ubifs_add_lpt_dirt(c, pnode->parent->nbranch[pnode->iip].lnum, | |
490 | c->pnode_sz); | |
491 | } | |
492 | ||
493 | /** | |
494 | * calc_nnode_num - calculate nnode number. | |
495 | * @row: the row in the tree (root is zero) | |
496 | * @col: the column in the row (leftmost is zero) | |
497 | * | |
498 | * The nnode number is a number that uniquely identifies a nnode and can be used | |
499 | * easily to traverse the tree from the root to that nnode. | |
500 | * | |
501 | * This function calculates and returns the nnode number for the nnode at @row | |
502 | * and @col. | |
503 | */ | |
504 | static int calc_nnode_num(int row, int col) | |
505 | { | |
506 | int num, bits; | |
507 | ||
508 | num = 1; | |
509 | while (row--) { | |
510 | bits = (col & (UBIFS_LPT_FANOUT - 1)); | |
511 | col >>= UBIFS_LPT_FANOUT_SHIFT; | |
512 | num <<= UBIFS_LPT_FANOUT_SHIFT; | |
513 | num |= bits; | |
514 | } | |
515 | return num; | |
516 | } | |
517 | ||
518 | /** | |
519 | * calc_nnode_num_from_parent - calculate nnode number. | |
520 | * @c: UBIFS file-system description object | |
521 | * @parent: parent nnode | |
522 | * @iip: index in parent | |
523 | * | |
524 | * The nnode number is a number that uniquely identifies a nnode and can be used | |
525 | * easily to traverse the tree from the root to that nnode. | |
526 | * | |
527 | * This function calculates and returns the nnode number based on the parent's | |
528 | * nnode number and the index in parent. | |
529 | */ | |
530 | static int calc_nnode_num_from_parent(struct ubifs_info *c, | |
531 | struct ubifs_nnode *parent, int iip) | |
532 | { | |
533 | int num, shft; | |
534 | ||
535 | if (!parent) | |
536 | return 1; | |
537 | shft = (c->lpt_hght - parent->level) * UBIFS_LPT_FANOUT_SHIFT; | |
538 | num = parent->num ^ (1 << shft); | |
539 | num |= (UBIFS_LPT_FANOUT + iip) << shft; | |
540 | return num; | |
541 | } | |
542 | ||
543 | /** | |
544 | * calc_pnode_num_from_parent - calculate pnode number. | |
545 | * @c: UBIFS file-system description object | |
546 | * @parent: parent nnode | |
547 | * @iip: index in parent | |
548 | * | |
549 | * The pnode number is a number that uniquely identifies a pnode and can be used | |
550 | * easily to traverse the tree from the root to that pnode. | |
551 | * | |
552 | * This function calculates and returns the pnode number based on the parent's | |
553 | * nnode number and the index in parent. | |
554 | */ | |
555 | static int calc_pnode_num_from_parent(struct ubifs_info *c, | |
556 | struct ubifs_nnode *parent, int iip) | |
557 | { | |
558 | int i, n = c->lpt_hght - 1, pnum = parent->num, num = 0; | |
559 | ||
560 | for (i = 0; i < n; i++) { | |
561 | num <<= UBIFS_LPT_FANOUT_SHIFT; | |
562 | num |= pnum & (UBIFS_LPT_FANOUT - 1); | |
563 | pnum >>= UBIFS_LPT_FANOUT_SHIFT; | |
564 | } | |
565 | num <<= UBIFS_LPT_FANOUT_SHIFT; | |
566 | num |= iip; | |
567 | return num; | |
568 | } | |
569 | ||
570 | /** | |
571 | * ubifs_create_dflt_lpt - create default LPT. | |
572 | * @c: UBIFS file-system description object | |
573 | * @main_lebs: number of main area LEBs is passed and returned here | |
574 | * @lpt_first: LEB number of first LPT LEB | |
575 | * @lpt_lebs: number of LEBs for LPT is passed and returned here | |
576 | * @big_lpt: use big LPT model is passed and returned here | |
577 | * | |
578 | * This function returns %0 on success and a negative error code on failure. | |
579 | */ | |
580 | int ubifs_create_dflt_lpt(struct ubifs_info *c, int *main_lebs, int lpt_first, | |
581 | int *lpt_lebs, int *big_lpt) | |
582 | { | |
583 | int lnum, err = 0, node_sz, iopos, i, j, cnt, len, alen, row; | |
584 | int blnum, boffs, bsz, bcnt; | |
585 | struct ubifs_pnode *pnode = NULL; | |
586 | struct ubifs_nnode *nnode = NULL; | |
587 | void *buf = NULL, *p; | |
588 | struct ubifs_lpt_lprops *ltab = NULL; | |
589 | int *lsave = NULL; | |
590 | ||
591 | err = calc_dflt_lpt_geom(c, main_lebs, big_lpt); | |
592 | if (err) | |
593 | return err; | |
594 | *lpt_lebs = c->lpt_lebs; | |
595 | ||
596 | /* Needed by 'ubifs_pack_nnode()' and 'set_ltab()' */ | |
597 | c->lpt_first = lpt_first; | |
598 | /* Needed by 'set_ltab()' */ | |
599 | c->lpt_last = lpt_first + c->lpt_lebs - 1; | |
600 | /* Needed by 'ubifs_pack_lsave()' */ | |
601 | c->main_first = c->leb_cnt - *main_lebs; | |
602 | ||
603 | lsave = kmalloc(sizeof(int) * c->lsave_cnt, GFP_KERNEL); | |
604 | pnode = kzalloc(sizeof(struct ubifs_pnode), GFP_KERNEL); | |
605 | nnode = kzalloc(sizeof(struct ubifs_nnode), GFP_KERNEL); | |
606 | buf = vmalloc(c->leb_size); | |
607 | ltab = vmalloc(sizeof(struct ubifs_lpt_lprops) * c->lpt_lebs); | |
608 | if (!pnode || !nnode || !buf || !ltab || !lsave) { | |
609 | err = -ENOMEM; | |
610 | goto out; | |
611 | } | |
612 | ||
613 | ubifs_assert(!c->ltab); | |
614 | c->ltab = ltab; /* Needed by set_ltab */ | |
615 | ||
616 | /* Initialize LPT's own lprops */ | |
617 | for (i = 0; i < c->lpt_lebs; i++) { | |
618 | ltab[i].free = c->leb_size; | |
619 | ltab[i].dirty = 0; | |
620 | ltab[i].tgc = 0; | |
621 | ltab[i].cmt = 0; | |
622 | } | |
623 | ||
624 | lnum = lpt_first; | |
625 | p = buf; | |
626 | /* Number of leaf nodes (pnodes) */ | |
627 | cnt = c->pnode_cnt; | |
628 | ||
629 | /* | |
630 | * The first pnode contains the LEB properties for the LEBs that contain | |
631 | * the root inode node and the root index node of the index tree. | |
632 | */ | |
633 | node_sz = ALIGN(ubifs_idx_node_sz(c, 1), 8); | |
634 | iopos = ALIGN(node_sz, c->min_io_size); | |
635 | pnode->lprops[0].free = c->leb_size - iopos; | |
636 | pnode->lprops[0].dirty = iopos - node_sz; | |
637 | pnode->lprops[0].flags = LPROPS_INDEX; | |
638 | ||
639 | node_sz = UBIFS_INO_NODE_SZ; | |
640 | iopos = ALIGN(node_sz, c->min_io_size); | |
641 | pnode->lprops[1].free = c->leb_size - iopos; | |
642 | pnode->lprops[1].dirty = iopos - node_sz; | |
643 | ||
644 | for (i = 2; i < UBIFS_LPT_FANOUT; i++) | |
645 | pnode->lprops[i].free = c->leb_size; | |
646 | ||
647 | /* Add first pnode */ | |
648 | ubifs_pack_pnode(c, p, pnode); | |
649 | p += c->pnode_sz; | |
650 | len = c->pnode_sz; | |
651 | pnode->num += 1; | |
652 | ||
653 | /* Reset pnode values for remaining pnodes */ | |
654 | pnode->lprops[0].free = c->leb_size; | |
655 | pnode->lprops[0].dirty = 0; | |
656 | pnode->lprops[0].flags = 0; | |
657 | ||
658 | pnode->lprops[1].free = c->leb_size; | |
659 | pnode->lprops[1].dirty = 0; | |
660 | ||
661 | /* | |
662 | * To calculate the internal node branches, we keep information about | |
663 | * the level below. | |
664 | */ | |
665 | blnum = lnum; /* LEB number of level below */ | |
666 | boffs = 0; /* Offset of level below */ | |
667 | bcnt = cnt; /* Number of nodes in level below */ | |
668 | bsz = c->pnode_sz; /* Size of nodes in level below */ | |
669 | ||
670 | /* Add all remaining pnodes */ | |
671 | for (i = 1; i < cnt; i++) { | |
672 | if (len + c->pnode_sz > c->leb_size) { | |
673 | alen = ALIGN(len, c->min_io_size); | |
674 | set_ltab(c, lnum, c->leb_size - alen, alen - len); | |
675 | memset(p, 0xff, alen - len); | |
676 | err = ubi_leb_change(c->ubi, lnum++, buf, alen, | |
677 | UBI_SHORTTERM); | |
678 | if (err) | |
679 | goto out; | |
680 | p = buf; | |
681 | len = 0; | |
682 | } | |
683 | ubifs_pack_pnode(c, p, pnode); | |
684 | p += c->pnode_sz; | |
685 | len += c->pnode_sz; | |
686 | /* | |
687 | * pnodes are simply numbered left to right starting at zero, | |
688 | * which means the pnode number can be used easily to traverse | |
689 | * down the tree to the corresponding pnode. | |
690 | */ | |
691 | pnode->num += 1; | |
692 | } | |
693 | ||
694 | row = 0; | |
695 | for (i = UBIFS_LPT_FANOUT; cnt > i; i <<= UBIFS_LPT_FANOUT_SHIFT) | |
696 | row += 1; | |
697 | /* Add all nnodes, one level at a time */ | |
698 | while (1) { | |
699 | /* Number of internal nodes (nnodes) at next level */ | |
700 | cnt = DIV_ROUND_UP(cnt, UBIFS_LPT_FANOUT); | |
701 | for (i = 0; i < cnt; i++) { | |
702 | if (len + c->nnode_sz > c->leb_size) { | |
703 | alen = ALIGN(len, c->min_io_size); | |
704 | set_ltab(c, lnum, c->leb_size - alen, | |
705 | alen - len); | |
706 | memset(p, 0xff, alen - len); | |
707 | err = ubi_leb_change(c->ubi, lnum++, buf, alen, | |
708 | UBI_SHORTTERM); | |
709 | if (err) | |
710 | goto out; | |
711 | p = buf; | |
712 | len = 0; | |
713 | } | |
714 | /* Only 1 nnode at this level, so it is the root */ | |
715 | if (cnt == 1) { | |
716 | c->lpt_lnum = lnum; | |
717 | c->lpt_offs = len; | |
718 | } | |
719 | /* Set branches to the level below */ | |
720 | for (j = 0; j < UBIFS_LPT_FANOUT; j++) { | |
721 | if (bcnt) { | |
722 | if (boffs + bsz > c->leb_size) { | |
723 | blnum += 1; | |
724 | boffs = 0; | |
725 | } | |
726 | nnode->nbranch[j].lnum = blnum; | |
727 | nnode->nbranch[j].offs = boffs; | |
728 | boffs += bsz; | |
729 | bcnt--; | |
730 | } else { | |
731 | nnode->nbranch[j].lnum = 0; | |
732 | nnode->nbranch[j].offs = 0; | |
733 | } | |
734 | } | |
735 | nnode->num = calc_nnode_num(row, i); | |
736 | ubifs_pack_nnode(c, p, nnode); | |
737 | p += c->nnode_sz; | |
738 | len += c->nnode_sz; | |
739 | } | |
740 | /* Only 1 nnode at this level, so it is the root */ | |
741 | if (cnt == 1) | |
742 | break; | |
743 | /* Update the information about the level below */ | |
744 | bcnt = cnt; | |
745 | bsz = c->nnode_sz; | |
746 | row -= 1; | |
747 | } | |
748 | ||
749 | if (*big_lpt) { | |
750 | /* Need to add LPT's save table */ | |
751 | if (len + c->lsave_sz > c->leb_size) { | |
752 | alen = ALIGN(len, c->min_io_size); | |
753 | set_ltab(c, lnum, c->leb_size - alen, alen - len); | |
754 | memset(p, 0xff, alen - len); | |
755 | err = ubi_leb_change(c->ubi, lnum++, buf, alen, | |
756 | UBI_SHORTTERM); | |
757 | if (err) | |
758 | goto out; | |
759 | p = buf; | |
760 | len = 0; | |
761 | } | |
762 | ||
763 | c->lsave_lnum = lnum; | |
764 | c->lsave_offs = len; | |
765 | ||
766 | for (i = 0; i < c->lsave_cnt && i < *main_lebs; i++) | |
767 | lsave[i] = c->main_first + i; | |
768 | for (; i < c->lsave_cnt; i++) | |
769 | lsave[i] = c->main_first; | |
770 | ||
771 | ubifs_pack_lsave(c, p, lsave); | |
772 | p += c->lsave_sz; | |
773 | len += c->lsave_sz; | |
774 | } | |
775 | ||
776 | /* Need to add LPT's own LEB properties table */ | |
777 | if (len + c->ltab_sz > c->leb_size) { | |
778 | alen = ALIGN(len, c->min_io_size); | |
779 | set_ltab(c, lnum, c->leb_size - alen, alen - len); | |
780 | memset(p, 0xff, alen - len); | |
781 | err = ubi_leb_change(c->ubi, lnum++, buf, alen, UBI_SHORTTERM); | |
782 | if (err) | |
783 | goto out; | |
784 | p = buf; | |
785 | len = 0; | |
786 | } | |
787 | ||
788 | c->ltab_lnum = lnum; | |
789 | c->ltab_offs = len; | |
790 | ||
791 | /* Update ltab before packing it */ | |
792 | len += c->ltab_sz; | |
793 | alen = ALIGN(len, c->min_io_size); | |
794 | set_ltab(c, lnum, c->leb_size - alen, alen - len); | |
795 | ||
796 | ubifs_pack_ltab(c, p, ltab); | |
797 | p += c->ltab_sz; | |
798 | ||
799 | /* Write remaining buffer */ | |
800 | memset(p, 0xff, alen - len); | |
801 | err = ubi_leb_change(c->ubi, lnum, buf, alen, UBI_SHORTTERM); | |
802 | if (err) | |
803 | goto out; | |
804 | ||
805 | c->nhead_lnum = lnum; | |
806 | c->nhead_offs = ALIGN(len, c->min_io_size); | |
807 | ||
808 | dbg_lp("space_bits %d", c->space_bits); | |
809 | dbg_lp("lpt_lnum_bits %d", c->lpt_lnum_bits); | |
810 | dbg_lp("lpt_offs_bits %d", c->lpt_offs_bits); | |
811 | dbg_lp("lpt_spc_bits %d", c->lpt_spc_bits); | |
812 | dbg_lp("pcnt_bits %d", c->pcnt_bits); | |
813 | dbg_lp("lnum_bits %d", c->lnum_bits); | |
814 | dbg_lp("pnode_sz %d", c->pnode_sz); | |
815 | dbg_lp("nnode_sz %d", c->nnode_sz); | |
816 | dbg_lp("ltab_sz %d", c->ltab_sz); | |
817 | dbg_lp("lsave_sz %d", c->lsave_sz); | |
818 | dbg_lp("lsave_cnt %d", c->lsave_cnt); | |
819 | dbg_lp("lpt_hght %d", c->lpt_hght); | |
820 | dbg_lp("big_lpt %d", c->big_lpt); | |
821 | dbg_lp("LPT root is at %d:%d", c->lpt_lnum, c->lpt_offs); | |
822 | dbg_lp("LPT head is at %d:%d", c->nhead_lnum, c->nhead_offs); | |
823 | dbg_lp("LPT ltab is at %d:%d", c->ltab_lnum, c->ltab_offs); | |
824 | if (c->big_lpt) | |
825 | dbg_lp("LPT lsave is at %d:%d", c->lsave_lnum, c->lsave_offs); | |
826 | out: | |
827 | c->ltab = NULL; | |
828 | kfree(lsave); | |
829 | vfree(ltab); | |
830 | vfree(buf); | |
831 | kfree(nnode); | |
832 | kfree(pnode); | |
833 | return err; | |
834 | } | |
835 | ||
836 | /** | |
837 | * update_cats - add LEB properties of a pnode to LEB category lists and heaps. | |
838 | * @c: UBIFS file-system description object | |
839 | * @pnode: pnode | |
840 | * | |
841 | * When a pnode is loaded into memory, the LEB properties it contains are added, | |
842 | * by this function, to the LEB category lists and heaps. | |
843 | */ | |
844 | static void update_cats(struct ubifs_info *c, struct ubifs_pnode *pnode) | |
845 | { | |
846 | int i; | |
847 | ||
848 | for (i = 0; i < UBIFS_LPT_FANOUT; i++) { | |
849 | int cat = pnode->lprops[i].flags & LPROPS_CAT_MASK; | |
850 | int lnum = pnode->lprops[i].lnum; | |
851 | ||
852 | if (!lnum) | |
853 | return; | |
854 | ubifs_add_to_cat(c, &pnode->lprops[i], cat); | |
855 | } | |
856 | } | |
857 | ||
858 | /** | |
859 | * replace_cats - add LEB properties of a pnode to LEB category lists and heaps. | |
860 | * @c: UBIFS file-system description object | |
861 | * @old_pnode: pnode copied | |
862 | * @new_pnode: pnode copy | |
863 | * | |
864 | * During commit it is sometimes necessary to copy a pnode | |
865 | * (see dirty_cow_pnode). When that happens, references in | |
866 | * category lists and heaps must be replaced. This function does that. | |
867 | */ | |
868 | static void replace_cats(struct ubifs_info *c, struct ubifs_pnode *old_pnode, | |
869 | struct ubifs_pnode *new_pnode) | |
870 | { | |
871 | int i; | |
872 | ||
873 | for (i = 0; i < UBIFS_LPT_FANOUT; i++) { | |
874 | if (!new_pnode->lprops[i].lnum) | |
875 | return; | |
876 | ubifs_replace_cat(c, &old_pnode->lprops[i], | |
877 | &new_pnode->lprops[i]); | |
878 | } | |
879 | } | |
880 | ||
881 | /** | |
882 | * check_lpt_crc - check LPT node crc is correct. | |
883 | * @c: UBIFS file-system description object | |
884 | * @buf: buffer containing node | |
885 | * @len: length of node | |
886 | * | |
887 | * This function returns %0 on success and a negative error code on failure. | |
888 | */ | |
889 | static int check_lpt_crc(void *buf, int len) | |
890 | { | |
891 | int pos = 0; | |
892 | uint8_t *addr = buf; | |
893 | uint16_t crc, calc_crc; | |
894 | ||
895 | crc = ubifs_unpack_bits(&addr, &pos, UBIFS_LPT_CRC_BITS); | |
896 | calc_crc = crc16(-1, buf + UBIFS_LPT_CRC_BYTES, | |
897 | len - UBIFS_LPT_CRC_BYTES); | |
898 | if (crc != calc_crc) { | |
899 | ubifs_err("invalid crc in LPT node: crc %hx calc %hx", crc, | |
900 | calc_crc); | |
901 | dbg_dump_stack(); | |
902 | return -EINVAL; | |
903 | } | |
904 | return 0; | |
905 | } | |
906 | ||
907 | /** | |
908 | * check_lpt_type - check LPT node type is correct. | |
909 | * @c: UBIFS file-system description object | |
910 | * @addr: address of type bit field is passed and returned updated here | |
911 | * @pos: position of type bit field is passed and returned updated here | |
912 | * @type: expected type | |
913 | * | |
914 | * This function returns %0 on success and a negative error code on failure. | |
915 | */ | |
916 | static int check_lpt_type(uint8_t **addr, int *pos, int type) | |
917 | { | |
918 | int node_type; | |
919 | ||
920 | node_type = ubifs_unpack_bits(addr, pos, UBIFS_LPT_TYPE_BITS); | |
921 | if (node_type != type) { | |
922 | ubifs_err("invalid type (%d) in LPT node type %d", node_type, | |
923 | type); | |
924 | dbg_dump_stack(); | |
925 | return -EINVAL; | |
926 | } | |
927 | return 0; | |
928 | } | |
929 | ||
930 | /** | |
931 | * unpack_pnode - unpack a pnode. | |
932 | * @c: UBIFS file-system description object | |
933 | * @buf: buffer containing packed pnode to unpack | |
934 | * @pnode: pnode structure to fill | |
935 | * | |
936 | * This function returns %0 on success and a negative error code on failure. | |
937 | */ | |
938 | static int unpack_pnode(struct ubifs_info *c, void *buf, | |
939 | struct ubifs_pnode *pnode) | |
940 | { | |
941 | uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES; | |
942 | int i, pos = 0, err; | |
943 | ||
944 | err = check_lpt_type(&addr, &pos, UBIFS_LPT_PNODE); | |
945 | if (err) | |
946 | return err; | |
947 | if (c->big_lpt) | |
948 | pnode->num = ubifs_unpack_bits(&addr, &pos, c->pcnt_bits); | |
949 | for (i = 0; i < UBIFS_LPT_FANOUT; i++) { | |
950 | struct ubifs_lprops * const lprops = &pnode->lprops[i]; | |
951 | ||
952 | lprops->free = ubifs_unpack_bits(&addr, &pos, c->space_bits); | |
953 | lprops->free <<= 3; | |
954 | lprops->dirty = ubifs_unpack_bits(&addr, &pos, c->space_bits); | |
955 | lprops->dirty <<= 3; | |
956 | ||
957 | if (ubifs_unpack_bits(&addr, &pos, 1)) | |
958 | lprops->flags = LPROPS_INDEX; | |
959 | else | |
960 | lprops->flags = 0; | |
961 | lprops->flags |= ubifs_categorize_lprops(c, lprops); | |
962 | } | |
963 | err = check_lpt_crc(buf, c->pnode_sz); | |
964 | return err; | |
965 | } | |
966 | ||
967 | /** | |
968 | * unpack_nnode - unpack a nnode. | |
969 | * @c: UBIFS file-system description object | |
970 | * @buf: buffer containing packed nnode to unpack | |
971 | * @nnode: nnode structure to fill | |
972 | * | |
973 | * This function returns %0 on success and a negative error code on failure. | |
974 | */ | |
975 | static int unpack_nnode(struct ubifs_info *c, void *buf, | |
976 | struct ubifs_nnode *nnode) | |
977 | { | |
978 | uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES; | |
979 | int i, pos = 0, err; | |
980 | ||
981 | err = check_lpt_type(&addr, &pos, UBIFS_LPT_NNODE); | |
982 | if (err) | |
983 | return err; | |
984 | if (c->big_lpt) | |
985 | nnode->num = ubifs_unpack_bits(&addr, &pos, c->pcnt_bits); | |
986 | for (i = 0; i < UBIFS_LPT_FANOUT; i++) { | |
987 | int lnum; | |
988 | ||
989 | lnum = ubifs_unpack_bits(&addr, &pos, c->lpt_lnum_bits) + | |
990 | c->lpt_first; | |
991 | if (lnum == c->lpt_last + 1) | |
992 | lnum = 0; | |
993 | nnode->nbranch[i].lnum = lnum; | |
994 | nnode->nbranch[i].offs = ubifs_unpack_bits(&addr, &pos, | |
995 | c->lpt_offs_bits); | |
996 | } | |
997 | err = check_lpt_crc(buf, c->nnode_sz); | |
998 | return err; | |
999 | } | |
1000 | ||
1001 | /** | |
1002 | * unpack_ltab - unpack the LPT's own lprops table. | |
1003 | * @c: UBIFS file-system description object | |
1004 | * @buf: buffer from which to unpack | |
1005 | * | |
1006 | * This function returns %0 on success and a negative error code on failure. | |
1007 | */ | |
1008 | static int unpack_ltab(struct ubifs_info *c, void *buf) | |
1009 | { | |
1010 | uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES; | |
1011 | int i, pos = 0, err; | |
1012 | ||
1013 | err = check_lpt_type(&addr, &pos, UBIFS_LPT_LTAB); | |
1014 | if (err) | |
1015 | return err; | |
1016 | for (i = 0; i < c->lpt_lebs; i++) { | |
1017 | int free = ubifs_unpack_bits(&addr, &pos, c->lpt_spc_bits); | |
1018 | int dirty = ubifs_unpack_bits(&addr, &pos, c->lpt_spc_bits); | |
1019 | ||
1020 | if (free < 0 || free > c->leb_size || dirty < 0 || | |
1021 | dirty > c->leb_size || free + dirty > c->leb_size) | |
1022 | return -EINVAL; | |
1023 | ||
1024 | c->ltab[i].free = free; | |
1025 | c->ltab[i].dirty = dirty; | |
1026 | c->ltab[i].tgc = 0; | |
1027 | c->ltab[i].cmt = 0; | |
1028 | } | |
1029 | err = check_lpt_crc(buf, c->ltab_sz); | |
1030 | return err; | |
1031 | } | |
1032 | ||
1033 | /** | |
1034 | * unpack_lsave - unpack the LPT's save table. | |
1035 | * @c: UBIFS file-system description object | |
1036 | * @buf: buffer from which to unpack | |
1037 | * | |
1038 | * This function returns %0 on success and a negative error code on failure. | |
1039 | */ | |
1040 | static int unpack_lsave(struct ubifs_info *c, void *buf) | |
1041 | { | |
1042 | uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES; | |
1043 | int i, pos = 0, err; | |
1044 | ||
1045 | err = check_lpt_type(&addr, &pos, UBIFS_LPT_LSAVE); | |
1046 | if (err) | |
1047 | return err; | |
1048 | for (i = 0; i < c->lsave_cnt; i++) { | |
1049 | int lnum = ubifs_unpack_bits(&addr, &pos, c->lnum_bits); | |
1050 | ||
1051 | if (lnum < c->main_first || lnum >= c->leb_cnt) | |
1052 | return -EINVAL; | |
1053 | c->lsave[i] = lnum; | |
1054 | } | |
1055 | err = check_lpt_crc(buf, c->lsave_sz); | |
1056 | return err; | |
1057 | } | |
1058 | ||
1059 | /** | |
1060 | * validate_nnode - validate a nnode. | |
1061 | * @c: UBIFS file-system description object | |
1062 | * @nnode: nnode to validate | |
1063 | * @parent: parent nnode (or NULL for the root nnode) | |
1064 | * @iip: index in parent | |
1065 | * | |
1066 | * This function returns %0 on success and a negative error code on failure. | |
1067 | */ | |
1068 | static int validate_nnode(struct ubifs_info *c, struct ubifs_nnode *nnode, | |
1069 | struct ubifs_nnode *parent, int iip) | |
1070 | { | |
1071 | int i, lvl, max_offs; | |
1072 | ||
1073 | if (c->big_lpt) { | |
1074 | int num = calc_nnode_num_from_parent(c, parent, iip); | |
1075 | ||
1076 | if (nnode->num != num) | |
1077 | return -EINVAL; | |
1078 | } | |
1079 | lvl = parent ? parent->level - 1 : c->lpt_hght; | |
1080 | if (lvl < 1) | |
1081 | return -EINVAL; | |
1082 | if (lvl == 1) | |
1083 | max_offs = c->leb_size - c->pnode_sz; | |
1084 | else | |
1085 | max_offs = c->leb_size - c->nnode_sz; | |
1086 | for (i = 0; i < UBIFS_LPT_FANOUT; i++) { | |
1087 | int lnum = nnode->nbranch[i].lnum; | |
1088 | int offs = nnode->nbranch[i].offs; | |
1089 | ||
1090 | if (lnum == 0) { | |
1091 | if (offs != 0) | |
1092 | return -EINVAL; | |
1093 | continue; | |
1094 | } | |
1095 | if (lnum < c->lpt_first || lnum > c->lpt_last) | |
1096 | return -EINVAL; | |
1097 | if (offs < 0 || offs > max_offs) | |
1098 | return -EINVAL; | |
1099 | } | |
1100 | return 0; | |
1101 | } | |
1102 | ||
1103 | /** | |
1104 | * validate_pnode - validate a pnode. | |
1105 | * @c: UBIFS file-system description object | |
1106 | * @pnode: pnode to validate | |
1107 | * @parent: parent nnode | |
1108 | * @iip: index in parent | |
1109 | * | |
1110 | * This function returns %0 on success and a negative error code on failure. | |
1111 | */ | |
1112 | static int validate_pnode(struct ubifs_info *c, struct ubifs_pnode *pnode, | |
1113 | struct ubifs_nnode *parent, int iip) | |
1114 | { | |
1115 | int i; | |
1116 | ||
1117 | if (c->big_lpt) { | |
1118 | int num = calc_pnode_num_from_parent(c, parent, iip); | |
1119 | ||
1120 | if (pnode->num != num) | |
1121 | return -EINVAL; | |
1122 | } | |
1123 | for (i = 0; i < UBIFS_LPT_FANOUT; i++) { | |
1124 | int free = pnode->lprops[i].free; | |
1125 | int dirty = pnode->lprops[i].dirty; | |
1126 | ||
1127 | if (free < 0 || free > c->leb_size || free % c->min_io_size || | |
1128 | (free & 7)) | |
1129 | return -EINVAL; | |
1130 | if (dirty < 0 || dirty > c->leb_size || (dirty & 7)) | |
1131 | return -EINVAL; | |
1132 | if (dirty + free > c->leb_size) | |
1133 | return -EINVAL; | |
1134 | } | |
1135 | return 0; | |
1136 | } | |
1137 | ||
1138 | /** | |
1139 | * set_pnode_lnum - set LEB numbers on a pnode. | |
1140 | * @c: UBIFS file-system description object | |
1141 | * @pnode: pnode to update | |
1142 | * | |
1143 | * This function calculates the LEB numbers for the LEB properties it contains | |
1144 | * based on the pnode number. | |
1145 | */ | |
1146 | static void set_pnode_lnum(struct ubifs_info *c, struct ubifs_pnode *pnode) | |
1147 | { | |
1148 | int i, lnum; | |
1149 | ||
1150 | lnum = (pnode->num << UBIFS_LPT_FANOUT_SHIFT) + c->main_first; | |
1151 | for (i = 0; i < UBIFS_LPT_FANOUT; i++) { | |
1152 | if (lnum >= c->leb_cnt) | |
1153 | return; | |
1154 | pnode->lprops[i].lnum = lnum++; | |
1155 | } | |
1156 | } | |
1157 | ||
1158 | /** | |
1159 | * ubifs_read_nnode - read a nnode from flash and link it to the tree in memory. | |
1160 | * @c: UBIFS file-system description object | |
1161 | * @parent: parent nnode (or NULL for the root) | |
1162 | * @iip: index in parent | |
1163 | * | |
1164 | * This function returns %0 on success and a negative error code on failure. | |
1165 | */ | |
1166 | int ubifs_read_nnode(struct ubifs_info *c, struct ubifs_nnode *parent, int iip) | |
1167 | { | |
1168 | struct ubifs_nbranch *branch = NULL; | |
1169 | struct ubifs_nnode *nnode = NULL; | |
1170 | void *buf = c->lpt_nod_buf; | |
1171 | int err, lnum, offs; | |
1172 | ||
1173 | if (parent) { | |
1174 | branch = &parent->nbranch[iip]; | |
1175 | lnum = branch->lnum; | |
1176 | offs = branch->offs; | |
1177 | } else { | |
1178 | lnum = c->lpt_lnum; | |
1179 | offs = c->lpt_offs; | |
1180 | } | |
1181 | nnode = kzalloc(sizeof(struct ubifs_nnode), GFP_NOFS); | |
1182 | if (!nnode) { | |
1183 | err = -ENOMEM; | |
1184 | goto out; | |
1185 | } | |
1186 | if (lnum == 0) { | |
1187 | /* | |
1188 | * This nnode was not written which just means that the LEB | |
1189 | * properties in the subtree below it describe empty LEBs. We | |
1190 | * make the nnode as though we had read it, which in fact means | |
1191 | * doing almost nothing. | |
1192 | */ | |
1193 | if (c->big_lpt) | |
1194 | nnode->num = calc_nnode_num_from_parent(c, parent, iip); | |
1195 | } else { | |
1196 | err = ubi_read(c->ubi, lnum, buf, offs, c->nnode_sz); | |
1197 | if (err) | |
1198 | goto out; | |
1199 | err = unpack_nnode(c, buf, nnode); | |
1200 | if (err) | |
1201 | goto out; | |
1202 | } | |
1203 | err = validate_nnode(c, nnode, parent, iip); | |
1204 | if (err) | |
1205 | goto out; | |
1206 | if (!c->big_lpt) | |
1207 | nnode->num = calc_nnode_num_from_parent(c, parent, iip); | |
1208 | if (parent) { | |
1209 | branch->nnode = nnode; | |
1210 | nnode->level = parent->level - 1; | |
1211 | } else { | |
1212 | c->nroot = nnode; | |
1213 | nnode->level = c->lpt_hght; | |
1214 | } | |
1215 | nnode->parent = parent; | |
1216 | nnode->iip = iip; | |
1217 | return 0; | |
1218 | ||
1219 | out: | |
1220 | ubifs_err("error %d reading nnode at %d:%d", err, lnum, offs); | |
1221 | kfree(nnode); | |
1222 | return err; | |
1223 | } | |
1224 | ||
1225 | /** | |
1226 | * read_pnode - read a pnode from flash and link it to the tree in memory. | |
1227 | * @c: UBIFS file-system description object | |
1228 | * @parent: parent nnode | |
1229 | * @iip: index in parent | |
1230 | * | |
1231 | * This function returns %0 on success and a negative error code on failure. | |
1232 | */ | |
1233 | static int read_pnode(struct ubifs_info *c, struct ubifs_nnode *parent, int iip) | |
1234 | { | |
1235 | struct ubifs_nbranch *branch; | |
1236 | struct ubifs_pnode *pnode = NULL; | |
1237 | void *buf = c->lpt_nod_buf; | |
1238 | int err, lnum, offs; | |
1239 | ||
1240 | branch = &parent->nbranch[iip]; | |
1241 | lnum = branch->lnum; | |
1242 | offs = branch->offs; | |
1243 | pnode = kzalloc(sizeof(struct ubifs_pnode), GFP_NOFS); | |
1244 | if (!pnode) { | |
1245 | err = -ENOMEM; | |
1246 | goto out; | |
1247 | } | |
1248 | if (lnum == 0) { | |
1249 | /* | |
1250 | * This pnode was not written which just means that the LEB | |
1251 | * properties in it describe empty LEBs. We make the pnode as | |
1252 | * though we had read it. | |
1253 | */ | |
1254 | int i; | |
1255 | ||
1256 | if (c->big_lpt) | |
1257 | pnode->num = calc_pnode_num_from_parent(c, parent, iip); | |
1258 | for (i = 0; i < UBIFS_LPT_FANOUT; i++) { | |
1259 | struct ubifs_lprops * const lprops = &pnode->lprops[i]; | |
1260 | ||
1261 | lprops->free = c->leb_size; | |
1262 | lprops->flags = ubifs_categorize_lprops(c, lprops); | |
1263 | } | |
1264 | } else { | |
1265 | err = ubi_read(c->ubi, lnum, buf, offs, c->pnode_sz); | |
1266 | if (err) | |
1267 | goto out; | |
1268 | err = unpack_pnode(c, buf, pnode); | |
1269 | if (err) | |
1270 | goto out; | |
1271 | } | |
1272 | err = validate_pnode(c, pnode, parent, iip); | |
1273 | if (err) | |
1274 | goto out; | |
1275 | if (!c->big_lpt) | |
1276 | pnode->num = calc_pnode_num_from_parent(c, parent, iip); | |
1277 | branch->pnode = pnode; | |
1278 | pnode->parent = parent; | |
1279 | pnode->iip = iip; | |
1280 | set_pnode_lnum(c, pnode); | |
1281 | c->pnodes_have += 1; | |
1282 | return 0; | |
1283 | ||
1284 | out: | |
1285 | ubifs_err("error %d reading pnode at %d:%d", err, lnum, offs); | |
1286 | dbg_dump_pnode(c, pnode, parent, iip); | |
1287 | dbg_msg("calc num: %d", calc_pnode_num_from_parent(c, parent, iip)); | |
1288 | kfree(pnode); | |
1289 | return err; | |
1290 | } | |
1291 | ||
1292 | /** | |
1293 | * read_ltab - read LPT's own lprops table. | |
1294 | * @c: UBIFS file-system description object | |
1295 | * | |
1296 | * This function returns %0 on success and a negative error code on failure. | |
1297 | */ | |
1298 | static int read_ltab(struct ubifs_info *c) | |
1299 | { | |
1300 | int err; | |
1301 | void *buf; | |
1302 | ||
1303 | buf = vmalloc(c->ltab_sz); | |
1304 | if (!buf) | |
1305 | return -ENOMEM; | |
1306 | err = ubi_read(c->ubi, c->ltab_lnum, buf, c->ltab_offs, c->ltab_sz); | |
1307 | if (err) | |
1308 | goto out; | |
1309 | err = unpack_ltab(c, buf); | |
1310 | out: | |
1311 | vfree(buf); | |
1312 | return err; | |
1313 | } | |
1314 | ||
1315 | /** | |
1316 | * read_lsave - read LPT's save table. | |
1317 | * @c: UBIFS file-system description object | |
1318 | * | |
1319 | * This function returns %0 on success and a negative error code on failure. | |
1320 | */ | |
1321 | static int read_lsave(struct ubifs_info *c) | |
1322 | { | |
1323 | int err, i; | |
1324 | void *buf; | |
1325 | ||
1326 | buf = vmalloc(c->lsave_sz); | |
1327 | if (!buf) | |
1328 | return -ENOMEM; | |
1329 | err = ubi_read(c->ubi, c->lsave_lnum, buf, c->lsave_offs, c->lsave_sz); | |
1330 | if (err) | |
1331 | goto out; | |
1332 | err = unpack_lsave(c, buf); | |
1333 | if (err) | |
1334 | goto out; | |
1335 | for (i = 0; i < c->lsave_cnt; i++) { | |
1336 | int lnum = c->lsave[i]; | |
1337 | ||
1338 | /* | |
1339 | * Due to automatic resizing, the values in the lsave table | |
1340 | * could be beyond the volume size - just ignore them. | |
1341 | */ | |
1342 | if (lnum >= c->leb_cnt) | |
1343 | continue; | |
1344 | ubifs_lpt_lookup(c, lnum); | |
1345 | } | |
1346 | out: | |
1347 | vfree(buf); | |
1348 | return err; | |
1349 | } | |
1350 | ||
1351 | /** | |
1352 | * ubifs_get_nnode - get a nnode. | |
1353 | * @c: UBIFS file-system description object | |
1354 | * @parent: parent nnode (or NULL for the root) | |
1355 | * @iip: index in parent | |
1356 | * | |
1357 | * This function returns a pointer to the nnode on success or a negative error | |
1358 | * code on failure. | |
1359 | */ | |
1360 | struct ubifs_nnode *ubifs_get_nnode(struct ubifs_info *c, | |
1361 | struct ubifs_nnode *parent, int iip) | |
1362 | { | |
1363 | struct ubifs_nbranch *branch; | |
1364 | struct ubifs_nnode *nnode; | |
1365 | int err; | |
1366 | ||
1367 | branch = &parent->nbranch[iip]; | |
1368 | nnode = branch->nnode; | |
1369 | if (nnode) | |
1370 | return nnode; | |
1371 | err = ubifs_read_nnode(c, parent, iip); | |
1372 | if (err) | |
1373 | return ERR_PTR(err); | |
1374 | return branch->nnode; | |
1375 | } | |
1376 | ||
1377 | /** | |
1378 | * ubifs_get_pnode - get a pnode. | |
1379 | * @c: UBIFS file-system description object | |
1380 | * @parent: parent nnode | |
1381 | * @iip: index in parent | |
1382 | * | |
1383 | * This function returns a pointer to the pnode on success or a negative error | |
1384 | * code on failure. | |
1385 | */ | |
1386 | struct ubifs_pnode *ubifs_get_pnode(struct ubifs_info *c, | |
1387 | struct ubifs_nnode *parent, int iip) | |
1388 | { | |
1389 | struct ubifs_nbranch *branch; | |
1390 | struct ubifs_pnode *pnode; | |
1391 | int err; | |
1392 | ||
1393 | branch = &parent->nbranch[iip]; | |
1394 | pnode = branch->pnode; | |
1395 | if (pnode) | |
1396 | return pnode; | |
1397 | err = read_pnode(c, parent, iip); | |
1398 | if (err) | |
1399 | return ERR_PTR(err); | |
1400 | update_cats(c, branch->pnode); | |
1401 | return branch->pnode; | |
1402 | } | |
1403 | ||
1404 | /** | |
1405 | * ubifs_lpt_lookup - lookup LEB properties in the LPT. | |
1406 | * @c: UBIFS file-system description object | |
1407 | * @lnum: LEB number to lookup | |
1408 | * | |
1409 | * This function returns a pointer to the LEB properties on success or a | |
1410 | * negative error code on failure. | |
1411 | */ | |
1412 | struct ubifs_lprops *ubifs_lpt_lookup(struct ubifs_info *c, int lnum) | |
1413 | { | |
1414 | int err, i, h, iip, shft; | |
1415 | struct ubifs_nnode *nnode; | |
1416 | struct ubifs_pnode *pnode; | |
1417 | ||
1418 | if (!c->nroot) { | |
1419 | err = ubifs_read_nnode(c, NULL, 0); | |
1420 | if (err) | |
1421 | return ERR_PTR(err); | |
1422 | } | |
1423 | nnode = c->nroot; | |
1424 | i = lnum - c->main_first; | |
1425 | shft = c->lpt_hght * UBIFS_LPT_FANOUT_SHIFT; | |
1426 | for (h = 1; h < c->lpt_hght; h++) { | |
1427 | iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1)); | |
1428 | shft -= UBIFS_LPT_FANOUT_SHIFT; | |
1429 | nnode = ubifs_get_nnode(c, nnode, iip); | |
1430 | if (IS_ERR(nnode)) | |
1431 | return ERR_PTR(PTR_ERR(nnode)); | |
1432 | } | |
1433 | iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1)); | |
1434 | shft -= UBIFS_LPT_FANOUT_SHIFT; | |
1435 | pnode = ubifs_get_pnode(c, nnode, iip); | |
1436 | if (IS_ERR(pnode)) | |
1437 | return ERR_PTR(PTR_ERR(pnode)); | |
1438 | iip = (i & (UBIFS_LPT_FANOUT - 1)); | |
1439 | dbg_lp("LEB %d, free %d, dirty %d, flags %d", lnum, | |
1440 | pnode->lprops[iip].free, pnode->lprops[iip].dirty, | |
1441 | pnode->lprops[iip].flags); | |
1442 | return &pnode->lprops[iip]; | |
1443 | } | |
1444 | ||
1445 | /** | |
1446 | * dirty_cow_nnode - ensure a nnode is not being committed. | |
1447 | * @c: UBIFS file-system description object | |
1448 | * @nnode: nnode to check | |
1449 | * | |
1450 | * Returns dirtied nnode on success or negative error code on failure. | |
1451 | */ | |
1452 | static struct ubifs_nnode *dirty_cow_nnode(struct ubifs_info *c, | |
1453 | struct ubifs_nnode *nnode) | |
1454 | { | |
1455 | struct ubifs_nnode *n; | |
1456 | int i; | |
1457 | ||
1458 | if (!test_bit(COW_CNODE, &nnode->flags)) { | |
1459 | /* nnode is not being committed */ | |
1460 | if (!test_and_set_bit(DIRTY_CNODE, &nnode->flags)) { | |
1461 | c->dirty_nn_cnt += 1; | |
1462 | ubifs_add_nnode_dirt(c, nnode); | |
1463 | } | |
1464 | return nnode; | |
1465 | } | |
1466 | ||
1467 | /* nnode is being committed, so copy it */ | |
1468 | n = kmalloc(sizeof(struct ubifs_nnode), GFP_NOFS); | |
1469 | if (unlikely(!n)) | |
1470 | return ERR_PTR(-ENOMEM); | |
1471 | ||
1472 | memcpy(n, nnode, sizeof(struct ubifs_nnode)); | |
1473 | n->cnext = NULL; | |
1474 | __set_bit(DIRTY_CNODE, &n->flags); | |
1475 | __clear_bit(COW_CNODE, &n->flags); | |
1476 | ||
1477 | /* The children now have new parent */ | |
1478 | for (i = 0; i < UBIFS_LPT_FANOUT; i++) { | |
1479 | struct ubifs_nbranch *branch = &n->nbranch[i]; | |
1480 | ||
1481 | if (branch->cnode) | |
1482 | branch->cnode->parent = n; | |
1483 | } | |
1484 | ||
1485 | ubifs_assert(!test_bit(OBSOLETE_CNODE, &nnode->flags)); | |
1486 | __set_bit(OBSOLETE_CNODE, &nnode->flags); | |
1487 | ||
1488 | c->dirty_nn_cnt += 1; | |
1489 | ubifs_add_nnode_dirt(c, nnode); | |
1490 | if (nnode->parent) | |
1491 | nnode->parent->nbranch[n->iip].nnode = n; | |
1492 | else | |
1493 | c->nroot = n; | |
1494 | return n; | |
1495 | } | |
1496 | ||
1497 | /** | |
1498 | * dirty_cow_pnode - ensure a pnode is not being committed. | |
1499 | * @c: UBIFS file-system description object | |
1500 | * @pnode: pnode to check | |
1501 | * | |
1502 | * Returns dirtied pnode on success or negative error code on failure. | |
1503 | */ | |
1504 | static struct ubifs_pnode *dirty_cow_pnode(struct ubifs_info *c, | |
1505 | struct ubifs_pnode *pnode) | |
1506 | { | |
1507 | struct ubifs_pnode *p; | |
1508 | ||
1509 | if (!test_bit(COW_CNODE, &pnode->flags)) { | |
1510 | /* pnode is not being committed */ | |
1511 | if (!test_and_set_bit(DIRTY_CNODE, &pnode->flags)) { | |
1512 | c->dirty_pn_cnt += 1; | |
1513 | add_pnode_dirt(c, pnode); | |
1514 | } | |
1515 | return pnode; | |
1516 | } | |
1517 | ||
1518 | /* pnode is being committed, so copy it */ | |
1519 | p = kmalloc(sizeof(struct ubifs_pnode), GFP_NOFS); | |
1520 | if (unlikely(!p)) | |
1521 | return ERR_PTR(-ENOMEM); | |
1522 | ||
1523 | memcpy(p, pnode, sizeof(struct ubifs_pnode)); | |
1524 | p->cnext = NULL; | |
1525 | __set_bit(DIRTY_CNODE, &p->flags); | |
1526 | __clear_bit(COW_CNODE, &p->flags); | |
1527 | replace_cats(c, pnode, p); | |
1528 | ||
1529 | ubifs_assert(!test_bit(OBSOLETE_CNODE, &pnode->flags)); | |
1530 | __set_bit(OBSOLETE_CNODE, &pnode->flags); | |
1531 | ||
1532 | c->dirty_pn_cnt += 1; | |
1533 | add_pnode_dirt(c, pnode); | |
1534 | pnode->parent->nbranch[p->iip].pnode = p; | |
1535 | return p; | |
1536 | } | |
1537 | ||
1538 | /** | |
1539 | * ubifs_lpt_lookup_dirty - lookup LEB properties in the LPT. | |
1540 | * @c: UBIFS file-system description object | |
1541 | * @lnum: LEB number to lookup | |
1542 | * | |
1543 | * This function returns a pointer to the LEB properties on success or a | |
1544 | * negative error code on failure. | |
1545 | */ | |
1546 | struct ubifs_lprops *ubifs_lpt_lookup_dirty(struct ubifs_info *c, int lnum) | |
1547 | { | |
1548 | int err, i, h, iip, shft; | |
1549 | struct ubifs_nnode *nnode; | |
1550 | struct ubifs_pnode *pnode; | |
1551 | ||
1552 | if (!c->nroot) { | |
1553 | err = ubifs_read_nnode(c, NULL, 0); | |
1554 | if (err) | |
1555 | return ERR_PTR(err); | |
1556 | } | |
1557 | nnode = c->nroot; | |
1558 | nnode = dirty_cow_nnode(c, nnode); | |
1559 | if (IS_ERR(nnode)) | |
1560 | return ERR_PTR(PTR_ERR(nnode)); | |
1561 | i = lnum - c->main_first; | |
1562 | shft = c->lpt_hght * UBIFS_LPT_FANOUT_SHIFT; | |
1563 | for (h = 1; h < c->lpt_hght; h++) { | |
1564 | iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1)); | |
1565 | shft -= UBIFS_LPT_FANOUT_SHIFT; | |
1566 | nnode = ubifs_get_nnode(c, nnode, iip); | |
1567 | if (IS_ERR(nnode)) | |
1568 | return ERR_PTR(PTR_ERR(nnode)); | |
1569 | nnode = dirty_cow_nnode(c, nnode); | |
1570 | if (IS_ERR(nnode)) | |
1571 | return ERR_PTR(PTR_ERR(nnode)); | |
1572 | } | |
1573 | iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1)); | |
1574 | shft -= UBIFS_LPT_FANOUT_SHIFT; | |
1575 | pnode = ubifs_get_pnode(c, nnode, iip); | |
1576 | if (IS_ERR(pnode)) | |
1577 | return ERR_PTR(PTR_ERR(pnode)); | |
1578 | pnode = dirty_cow_pnode(c, pnode); | |
1579 | if (IS_ERR(pnode)) | |
1580 | return ERR_PTR(PTR_ERR(pnode)); | |
1581 | iip = (i & (UBIFS_LPT_FANOUT - 1)); | |
1582 | dbg_lp("LEB %d, free %d, dirty %d, flags %d", lnum, | |
1583 | pnode->lprops[iip].free, pnode->lprops[iip].dirty, | |
1584 | pnode->lprops[iip].flags); | |
1585 | ubifs_assert(test_bit(DIRTY_CNODE, &pnode->flags)); | |
1586 | return &pnode->lprops[iip]; | |
1587 | } | |
1588 | ||
1589 | /** | |
1590 | * lpt_init_rd - initialize the LPT for reading. | |
1591 | * @c: UBIFS file-system description object | |
1592 | * | |
1593 | * This function returns %0 on success and a negative error code on failure. | |
1594 | */ | |
1595 | static int lpt_init_rd(struct ubifs_info *c) | |
1596 | { | |
1597 | int err, i; | |
1598 | ||
1599 | c->ltab = vmalloc(sizeof(struct ubifs_lpt_lprops) * c->lpt_lebs); | |
1600 | if (!c->ltab) | |
1601 | return -ENOMEM; | |
1602 | ||
1603 | i = max_t(int, c->nnode_sz, c->pnode_sz); | |
1604 | c->lpt_nod_buf = kmalloc(i, GFP_KERNEL); | |
1605 | if (!c->lpt_nod_buf) | |
1606 | return -ENOMEM; | |
1607 | ||
1608 | for (i = 0; i < LPROPS_HEAP_CNT; i++) { | |
1609 | c->lpt_heap[i].arr = kmalloc(sizeof(void *) * LPT_HEAP_SZ, | |
1610 | GFP_KERNEL); | |
1611 | if (!c->lpt_heap[i].arr) | |
1612 | return -ENOMEM; | |
1613 | c->lpt_heap[i].cnt = 0; | |
1614 | c->lpt_heap[i].max_cnt = LPT_HEAP_SZ; | |
1615 | } | |
1616 | ||
1617 | c->dirty_idx.arr = kmalloc(sizeof(void *) * LPT_HEAP_SZ, GFP_KERNEL); | |
1618 | if (!c->dirty_idx.arr) | |
1619 | return -ENOMEM; | |
1620 | c->dirty_idx.cnt = 0; | |
1621 | c->dirty_idx.max_cnt = LPT_HEAP_SZ; | |
1622 | ||
1623 | err = read_ltab(c); | |
1624 | if (err) | |
1625 | return err; | |
1626 | ||
1627 | dbg_lp("space_bits %d", c->space_bits); | |
1628 | dbg_lp("lpt_lnum_bits %d", c->lpt_lnum_bits); | |
1629 | dbg_lp("lpt_offs_bits %d", c->lpt_offs_bits); | |
1630 | dbg_lp("lpt_spc_bits %d", c->lpt_spc_bits); | |
1631 | dbg_lp("pcnt_bits %d", c->pcnt_bits); | |
1632 | dbg_lp("lnum_bits %d", c->lnum_bits); | |
1633 | dbg_lp("pnode_sz %d", c->pnode_sz); | |
1634 | dbg_lp("nnode_sz %d", c->nnode_sz); | |
1635 | dbg_lp("ltab_sz %d", c->ltab_sz); | |
1636 | dbg_lp("lsave_sz %d", c->lsave_sz); | |
1637 | dbg_lp("lsave_cnt %d", c->lsave_cnt); | |
1638 | dbg_lp("lpt_hght %d", c->lpt_hght); | |
1639 | dbg_lp("big_lpt %d", c->big_lpt); | |
1640 | dbg_lp("LPT root is at %d:%d", c->lpt_lnum, c->lpt_offs); | |
1641 | dbg_lp("LPT head is at %d:%d", c->nhead_lnum, c->nhead_offs); | |
1642 | dbg_lp("LPT ltab is at %d:%d", c->ltab_lnum, c->ltab_offs); | |
1643 | if (c->big_lpt) | |
1644 | dbg_lp("LPT lsave is at %d:%d", c->lsave_lnum, c->lsave_offs); | |
1645 | ||
1646 | return 0; | |
1647 | } | |
1648 | ||
1649 | /** | |
1650 | * lpt_init_wr - initialize the LPT for writing. | |
1651 | * @c: UBIFS file-system description object | |
1652 | * | |
1653 | * 'lpt_init_rd()' must have been called already. | |
1654 | * | |
1655 | * This function returns %0 on success and a negative error code on failure. | |
1656 | */ | |
1657 | static int lpt_init_wr(struct ubifs_info *c) | |
1658 | { | |
1659 | int err, i; | |
1660 | ||
1661 | c->ltab_cmt = vmalloc(sizeof(struct ubifs_lpt_lprops) * c->lpt_lebs); | |
1662 | if (!c->ltab_cmt) | |
1663 | return -ENOMEM; | |
1664 | ||
1665 | c->lpt_buf = vmalloc(c->leb_size); | |
1666 | if (!c->lpt_buf) | |
1667 | return -ENOMEM; | |
1668 | ||
1669 | if (c->big_lpt) { | |
1670 | c->lsave = kmalloc(sizeof(int) * c->lsave_cnt, GFP_NOFS); | |
1671 | if (!c->lsave) | |
1672 | return -ENOMEM; | |
1673 | err = read_lsave(c); | |
1674 | if (err) | |
1675 | return err; | |
1676 | } | |
1677 | ||
1678 | for (i = 0; i < c->lpt_lebs; i++) | |
1679 | if (c->ltab[i].free == c->leb_size) { | |
1680 | err = ubifs_leb_unmap(c, i + c->lpt_first); | |
1681 | if (err) | |
1682 | return err; | |
1683 | } | |
1684 | ||
1685 | return 0; | |
1686 | } | |
1687 | ||
1688 | /** | |
1689 | * ubifs_lpt_init - initialize the LPT. | |
1690 | * @c: UBIFS file-system description object | |
1691 | * @rd: whether to initialize lpt for reading | |
1692 | * @wr: whether to initialize lpt for writing | |
1693 | * | |
1694 | * For mounting 'rw', @rd and @wr are both true. For mounting 'ro', @rd is true | |
1695 | * and @wr is false. For mounting from 'ro' to 'rw', @rd is false and @wr is | |
1696 | * true. | |
1697 | * | |
1698 | * This function returns %0 on success and a negative error code on failure. | |
1699 | */ | |
1700 | int ubifs_lpt_init(struct ubifs_info *c, int rd, int wr) | |
1701 | { | |
1702 | int err; | |
1703 | ||
1704 | if (rd) { | |
1705 | err = lpt_init_rd(c); | |
1706 | if (err) | |
1707 | return err; | |
1708 | } | |
1709 | ||
1710 | if (wr) { | |
1711 | err = lpt_init_wr(c); | |
1712 | if (err) | |
1713 | return err; | |
1714 | } | |
1715 | ||
1716 | return 0; | |
1717 | } | |
1718 | ||
1719 | /** | |
1720 | * struct lpt_scan_node - somewhere to put nodes while we scan LPT. | |
1721 | * @nnode: where to keep a nnode | |
1722 | * @pnode: where to keep a pnode | |
1723 | * @cnode: where to keep a cnode | |
1724 | * @in_tree: is the node in the tree in memory | |
1725 | * @ptr.nnode: pointer to the nnode (if it is an nnode) which may be here or in | |
1726 | * the tree | |
1727 | * @ptr.pnode: ditto for pnode | |
1728 | * @ptr.cnode: ditto for cnode | |
1729 | */ | |
1730 | struct lpt_scan_node { | |
1731 | union { | |
1732 | struct ubifs_nnode nnode; | |
1733 | struct ubifs_pnode pnode; | |
1734 | struct ubifs_cnode cnode; | |
1735 | }; | |
1736 | int in_tree; | |
1737 | union { | |
1738 | struct ubifs_nnode *nnode; | |
1739 | struct ubifs_pnode *pnode; | |
1740 | struct ubifs_cnode *cnode; | |
1741 | } ptr; | |
1742 | }; | |
1743 | ||
1744 | /** | |
1745 | * scan_get_nnode - for the scan, get a nnode from either the tree or flash. | |
1746 | * @c: the UBIFS file-system description object | |
1747 | * @path: where to put the nnode | |
1748 | * @parent: parent of the nnode | |
1749 | * @iip: index in parent of the nnode | |
1750 | * | |
1751 | * This function returns a pointer to the nnode on success or a negative error | |
1752 | * code on failure. | |
1753 | */ | |
1754 | static struct ubifs_nnode *scan_get_nnode(struct ubifs_info *c, | |
1755 | struct lpt_scan_node *path, | |
1756 | struct ubifs_nnode *parent, int iip) | |
1757 | { | |
1758 | struct ubifs_nbranch *branch; | |
1759 | struct ubifs_nnode *nnode; | |
1760 | void *buf = c->lpt_nod_buf; | |
1761 | int err; | |
1762 | ||
1763 | branch = &parent->nbranch[iip]; | |
1764 | nnode = branch->nnode; | |
1765 | if (nnode) { | |
1766 | path->in_tree = 1; | |
1767 | path->ptr.nnode = nnode; | |
1768 | return nnode; | |
1769 | } | |
1770 | nnode = &path->nnode; | |
1771 | path->in_tree = 0; | |
1772 | path->ptr.nnode = nnode; | |
1773 | memset(nnode, 0, sizeof(struct ubifs_nnode)); | |
1774 | if (branch->lnum == 0) { | |
1775 | /* | |
1776 | * This nnode was not written which just means that the LEB | |
1777 | * properties in the subtree below it describe empty LEBs. We | |
1778 | * make the nnode as though we had read it, which in fact means | |
1779 | * doing almost nothing. | |
1780 | */ | |
1781 | if (c->big_lpt) | |
1782 | nnode->num = calc_nnode_num_from_parent(c, parent, iip); | |
1783 | } else { | |
1784 | err = ubi_read(c->ubi, branch->lnum, buf, branch->offs, | |
1785 | c->nnode_sz); | |
1786 | if (err) | |
1787 | return ERR_PTR(err); | |
1788 | err = unpack_nnode(c, buf, nnode); | |
1789 | if (err) | |
1790 | return ERR_PTR(err); | |
1791 | } | |
1792 | err = validate_nnode(c, nnode, parent, iip); | |
1793 | if (err) | |
1794 | return ERR_PTR(err); | |
1795 | if (!c->big_lpt) | |
1796 | nnode->num = calc_nnode_num_from_parent(c, parent, iip); | |
1797 | nnode->level = parent->level - 1; | |
1798 | nnode->parent = parent; | |
1799 | nnode->iip = iip; | |
1800 | return nnode; | |
1801 | } | |
1802 | ||
1803 | /** | |
1804 | * scan_get_pnode - for the scan, get a pnode from either the tree or flash. | |
1805 | * @c: the UBIFS file-system description object | |
1806 | * @path: where to put the pnode | |
1807 | * @parent: parent of the pnode | |
1808 | * @iip: index in parent of the pnode | |
1809 | * | |
1810 | * This function returns a pointer to the pnode on success or a negative error | |
1811 | * code on failure. | |
1812 | */ | |
1813 | static struct ubifs_pnode *scan_get_pnode(struct ubifs_info *c, | |
1814 | struct lpt_scan_node *path, | |
1815 | struct ubifs_nnode *parent, int iip) | |
1816 | { | |
1817 | struct ubifs_nbranch *branch; | |
1818 | struct ubifs_pnode *pnode; | |
1819 | void *buf = c->lpt_nod_buf; | |
1820 | int err; | |
1821 | ||
1822 | branch = &parent->nbranch[iip]; | |
1823 | pnode = branch->pnode; | |
1824 | if (pnode) { | |
1825 | path->in_tree = 1; | |
1826 | path->ptr.pnode = pnode; | |
1827 | return pnode; | |
1828 | } | |
1829 | pnode = &path->pnode; | |
1830 | path->in_tree = 0; | |
1831 | path->ptr.pnode = pnode; | |
1832 | memset(pnode, 0, sizeof(struct ubifs_pnode)); | |
1833 | if (branch->lnum == 0) { | |
1834 | /* | |
1835 | * This pnode was not written which just means that the LEB | |
1836 | * properties in it describe empty LEBs. We make the pnode as | |
1837 | * though we had read it. | |
1838 | */ | |
1839 | int i; | |
1840 | ||
1841 | if (c->big_lpt) | |
1842 | pnode->num = calc_pnode_num_from_parent(c, parent, iip); | |
1843 | for (i = 0; i < UBIFS_LPT_FANOUT; i++) { | |
1844 | struct ubifs_lprops * const lprops = &pnode->lprops[i]; | |
1845 | ||
1846 | lprops->free = c->leb_size; | |
1847 | lprops->flags = ubifs_categorize_lprops(c, lprops); | |
1848 | } | |
1849 | } else { | |
1850 | ubifs_assert(branch->lnum >= c->lpt_first && | |
1851 | branch->lnum <= c->lpt_last); | |
1852 | ubifs_assert(branch->offs >= 0 && branch->offs < c->leb_size); | |
1853 | err = ubi_read(c->ubi, branch->lnum, buf, branch->offs, | |
1854 | c->pnode_sz); | |
1855 | if (err) | |
1856 | return ERR_PTR(err); | |
1857 | err = unpack_pnode(c, buf, pnode); | |
1858 | if (err) | |
1859 | return ERR_PTR(err); | |
1860 | } | |
1861 | err = validate_pnode(c, pnode, parent, iip); | |
1862 | if (err) | |
1863 | return ERR_PTR(err); | |
1864 | if (!c->big_lpt) | |
1865 | pnode->num = calc_pnode_num_from_parent(c, parent, iip); | |
1866 | pnode->parent = parent; | |
1867 | pnode->iip = iip; | |
1868 | set_pnode_lnum(c, pnode); | |
1869 | return pnode; | |
1870 | } | |
1871 | ||
1872 | /** | |
1873 | * ubifs_lpt_scan_nolock - scan the LPT. | |
1874 | * @c: the UBIFS file-system description object | |
1875 | * @start_lnum: LEB number from which to start scanning | |
1876 | * @end_lnum: LEB number at which to stop scanning | |
1877 | * @scan_cb: callback function called for each lprops | |
1878 | * @data: data to be passed to the callback function | |
1879 | * | |
1880 | * This function returns %0 on success and a negative error code on failure. | |
1881 | */ | |
1882 | int ubifs_lpt_scan_nolock(struct ubifs_info *c, int start_lnum, int end_lnum, | |
1883 | ubifs_lpt_scan_callback scan_cb, void *data) | |
1884 | { | |
1885 | int err = 0, i, h, iip, shft; | |
1886 | struct ubifs_nnode *nnode; | |
1887 | struct ubifs_pnode *pnode; | |
1888 | struct lpt_scan_node *path; | |
1889 | ||
1890 | if (start_lnum == -1) { | |
1891 | start_lnum = end_lnum + 1; | |
1892 | if (start_lnum >= c->leb_cnt) | |
1893 | start_lnum = c->main_first; | |
1894 | } | |
1895 | ||
1896 | ubifs_assert(start_lnum >= c->main_first && start_lnum < c->leb_cnt); | |
1897 | ubifs_assert(end_lnum >= c->main_first && end_lnum < c->leb_cnt); | |
1898 | ||
1899 | if (!c->nroot) { | |
1900 | err = ubifs_read_nnode(c, NULL, 0); | |
1901 | if (err) | |
1902 | return err; | |
1903 | } | |
1904 | ||
1905 | path = kmalloc(sizeof(struct lpt_scan_node) * (c->lpt_hght + 1), | |
1906 | GFP_NOFS); | |
1907 | if (!path) | |
1908 | return -ENOMEM; | |
1909 | ||
1910 | path[0].ptr.nnode = c->nroot; | |
1911 | path[0].in_tree = 1; | |
1912 | again: | |
1913 | /* Descend to the pnode containing start_lnum */ | |
1914 | nnode = c->nroot; | |
1915 | i = start_lnum - c->main_first; | |
1916 | shft = c->lpt_hght * UBIFS_LPT_FANOUT_SHIFT; | |
1917 | for (h = 1; h < c->lpt_hght; h++) { | |
1918 | iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1)); | |
1919 | shft -= UBIFS_LPT_FANOUT_SHIFT; | |
1920 | nnode = scan_get_nnode(c, path + h, nnode, iip); | |
1921 | if (IS_ERR(nnode)) { | |
1922 | err = PTR_ERR(nnode); | |
1923 | goto out; | |
1924 | } | |
1925 | } | |
1926 | iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1)); | |
1927 | shft -= UBIFS_LPT_FANOUT_SHIFT; | |
1928 | pnode = scan_get_pnode(c, path + h, nnode, iip); | |
1929 | if (IS_ERR(pnode)) { | |
1930 | err = PTR_ERR(pnode); | |
1931 | goto out; | |
1932 | } | |
1933 | iip = (i & (UBIFS_LPT_FANOUT - 1)); | |
1934 | ||
1935 | /* Loop for each lprops */ | |
1936 | while (1) { | |
1937 | struct ubifs_lprops *lprops = &pnode->lprops[iip]; | |
1938 | int ret, lnum = lprops->lnum; | |
1939 | ||
1940 | ret = scan_cb(c, lprops, path[h].in_tree, data); | |
1941 | if (ret < 0) { | |
1942 | err = ret; | |
1943 | goto out; | |
1944 | } | |
1945 | if (ret & LPT_SCAN_ADD) { | |
1946 | /* Add all the nodes in path to the tree in memory */ | |
1947 | for (h = 1; h < c->lpt_hght; h++) { | |
1948 | const size_t sz = sizeof(struct ubifs_nnode); | |
1949 | struct ubifs_nnode *parent; | |
1950 | ||
1951 | if (path[h].in_tree) | |
1952 | continue; | |
1953 | nnode = kmalloc(sz, GFP_NOFS); | |
1954 | if (!nnode) { | |
1955 | err = -ENOMEM; | |
1956 | goto out; | |
1957 | } | |
1958 | memcpy(nnode, &path[h].nnode, sz); | |
1959 | parent = nnode->parent; | |
1960 | parent->nbranch[nnode->iip].nnode = nnode; | |
1961 | path[h].ptr.nnode = nnode; | |
1962 | path[h].in_tree = 1; | |
1963 | path[h + 1].cnode.parent = nnode; | |
1964 | } | |
1965 | if (path[h].in_tree) | |
1966 | ubifs_ensure_cat(c, lprops); | |
1967 | else { | |
1968 | const size_t sz = sizeof(struct ubifs_pnode); | |
1969 | struct ubifs_nnode *parent; | |
1970 | ||
1971 | pnode = kmalloc(sz, GFP_NOFS); | |
1972 | if (!pnode) { | |
1973 | err = -ENOMEM; | |
1974 | goto out; | |
1975 | } | |
1976 | memcpy(pnode, &path[h].pnode, sz); | |
1977 | parent = pnode->parent; | |
1978 | parent->nbranch[pnode->iip].pnode = pnode; | |
1979 | path[h].ptr.pnode = pnode; | |
1980 | path[h].in_tree = 1; | |
1981 | update_cats(c, pnode); | |
1982 | c->pnodes_have += 1; | |
1983 | } | |
1984 | err = dbg_check_lpt_nodes(c, (struct ubifs_cnode *) | |
1985 | c->nroot, 0, 0); | |
1986 | if (err) | |
1987 | goto out; | |
1988 | err = dbg_check_cats(c); | |
1989 | if (err) | |
1990 | goto out; | |
1991 | } | |
1992 | if (ret & LPT_SCAN_STOP) { | |
1993 | err = 0; | |
1994 | break; | |
1995 | } | |
1996 | /* Get the next lprops */ | |
1997 | if (lnum == end_lnum) { | |
1998 | /* | |
1999 | * We got to the end without finding what we were | |
2000 | * looking for | |
2001 | */ | |
2002 | err = -ENOSPC; | |
2003 | goto out; | |
2004 | } | |
2005 | if (lnum + 1 >= c->leb_cnt) { | |
2006 | /* Wrap-around to the beginning */ | |
2007 | start_lnum = c->main_first; | |
2008 | goto again; | |
2009 | } | |
2010 | if (iip + 1 < UBIFS_LPT_FANOUT) { | |
2011 | /* Next lprops is in the same pnode */ | |
2012 | iip += 1; | |
2013 | continue; | |
2014 | } | |
2015 | /* We need to get the next pnode. Go up until we can go right */ | |
2016 | iip = pnode->iip; | |
2017 | while (1) { | |
2018 | h -= 1; | |
2019 | ubifs_assert(h >= 0); | |
2020 | nnode = path[h].ptr.nnode; | |
2021 | if (iip + 1 < UBIFS_LPT_FANOUT) | |
2022 | break; | |
2023 | iip = nnode->iip; | |
2024 | } | |
2025 | /* Go right */ | |
2026 | iip += 1; | |
2027 | /* Descend to the pnode */ | |
2028 | h += 1; | |
2029 | for (; h < c->lpt_hght; h++) { | |
2030 | nnode = scan_get_nnode(c, path + h, nnode, iip); | |
2031 | if (IS_ERR(nnode)) { | |
2032 | err = PTR_ERR(nnode); | |
2033 | goto out; | |
2034 | } | |
2035 | iip = 0; | |
2036 | } | |
2037 | pnode = scan_get_pnode(c, path + h, nnode, iip); | |
2038 | if (IS_ERR(pnode)) { | |
2039 | err = PTR_ERR(pnode); | |
2040 | goto out; | |
2041 | } | |
2042 | iip = 0; | |
2043 | } | |
2044 | out: | |
2045 | kfree(path); | |
2046 | return err; | |
2047 | } | |
2048 | ||
2049 | #ifdef CONFIG_UBIFS_FS_DEBUG | |
2050 | ||
2051 | /** | |
2052 | * dbg_chk_pnode - check a pnode. | |
2053 | * @c: the UBIFS file-system description object | |
2054 | * @pnode: pnode to check | |
2055 | * @col: pnode column | |
2056 | * | |
2057 | * This function returns %0 on success and a negative error code on failure. | |
2058 | */ | |
2059 | static int dbg_chk_pnode(struct ubifs_info *c, struct ubifs_pnode *pnode, | |
2060 | int col) | |
2061 | { | |
2062 | int i; | |
2063 | ||
2064 | if (pnode->num != col) { | |
2065 | dbg_err("pnode num %d expected %d parent num %d iip %d", | |
2066 | pnode->num, col, pnode->parent->num, pnode->iip); | |
2067 | return -EINVAL; | |
2068 | } | |
2069 | for (i = 0; i < UBIFS_LPT_FANOUT; i++) { | |
2070 | struct ubifs_lprops *lp, *lprops = &pnode->lprops[i]; | |
2071 | int lnum = (pnode->num << UBIFS_LPT_FANOUT_SHIFT) + i + | |
2072 | c->main_first; | |
2073 | int found, cat = lprops->flags & LPROPS_CAT_MASK; | |
2074 | struct ubifs_lpt_heap *heap; | |
2075 | struct list_head *list = NULL; | |
2076 | ||
2077 | if (lnum >= c->leb_cnt) | |
2078 | continue; | |
2079 | if (lprops->lnum != lnum) { | |
2080 | dbg_err("bad LEB number %d expected %d", | |
2081 | lprops->lnum, lnum); | |
2082 | return -EINVAL; | |
2083 | } | |
2084 | if (lprops->flags & LPROPS_TAKEN) { | |
2085 | if (cat != LPROPS_UNCAT) { | |
2086 | dbg_err("LEB %d taken but not uncat %d", | |
2087 | lprops->lnum, cat); | |
2088 | return -EINVAL; | |
2089 | } | |
2090 | continue; | |
2091 | } | |
2092 | if (lprops->flags & LPROPS_INDEX) { | |
2093 | switch (cat) { | |
2094 | case LPROPS_UNCAT: | |
2095 | case LPROPS_DIRTY_IDX: | |
2096 | case LPROPS_FRDI_IDX: | |
2097 | break; | |
2098 | default: | |
2099 | dbg_err("LEB %d index but cat %d", | |
2100 | lprops->lnum, cat); | |
2101 | return -EINVAL; | |
2102 | } | |
2103 | } else { | |
2104 | switch (cat) { | |
2105 | case LPROPS_UNCAT: | |
2106 | case LPROPS_DIRTY: | |
2107 | case LPROPS_FREE: | |
2108 | case LPROPS_EMPTY: | |
2109 | case LPROPS_FREEABLE: | |
2110 | break; | |
2111 | default: | |
2112 | dbg_err("LEB %d not index but cat %d", | |
2113 | lprops->lnum, cat); | |
2114 | return -EINVAL; | |
2115 | } | |
2116 | } | |
2117 | switch (cat) { | |
2118 | case LPROPS_UNCAT: | |
2119 | list = &c->uncat_list; | |
2120 | break; | |
2121 | case LPROPS_EMPTY: | |
2122 | list = &c->empty_list; | |
2123 | break; | |
2124 | case LPROPS_FREEABLE: | |
2125 | list = &c->freeable_list; | |
2126 | break; | |
2127 | case LPROPS_FRDI_IDX: | |
2128 | list = &c->frdi_idx_list; | |
2129 | break; | |
2130 | } | |
2131 | found = 0; | |
2132 | switch (cat) { | |
2133 | case LPROPS_DIRTY: | |
2134 | case LPROPS_DIRTY_IDX: | |
2135 | case LPROPS_FREE: | |
2136 | heap = &c->lpt_heap[cat - 1]; | |
2137 | if (lprops->hpos < heap->cnt && | |
2138 | heap->arr[lprops->hpos] == lprops) | |
2139 | found = 1; | |
2140 | break; | |
2141 | case LPROPS_UNCAT: | |
2142 | case LPROPS_EMPTY: | |
2143 | case LPROPS_FREEABLE: | |
2144 | case LPROPS_FRDI_IDX: | |
2145 | list_for_each_entry(lp, list, list) | |
2146 | if (lprops == lp) { | |
2147 | found = 1; | |
2148 | break; | |
2149 | } | |
2150 | break; | |
2151 | } | |
2152 | if (!found) { | |
2153 | dbg_err("LEB %d cat %d not found in cat heap/list", | |
2154 | lprops->lnum, cat); | |
2155 | return -EINVAL; | |
2156 | } | |
2157 | switch (cat) { | |
2158 | case LPROPS_EMPTY: | |
2159 | if (lprops->free != c->leb_size) { | |
2160 | dbg_err("LEB %d cat %d free %d dirty %d", | |
2161 | lprops->lnum, cat, lprops->free, | |
2162 | lprops->dirty); | |
2163 | return -EINVAL; | |
2164 | } | |
2165 | case LPROPS_FREEABLE: | |
2166 | case LPROPS_FRDI_IDX: | |
2167 | if (lprops->free + lprops->dirty != c->leb_size) { | |
2168 | dbg_err("LEB %d cat %d free %d dirty %d", | |
2169 | lprops->lnum, cat, lprops->free, | |
2170 | lprops->dirty); | |
2171 | return -EINVAL; | |
2172 | } | |
2173 | } | |
2174 | } | |
2175 | return 0; | |
2176 | } | |
2177 | ||
2178 | /** | |
2179 | * dbg_check_lpt_nodes - check nnodes and pnodes. | |
2180 | * @c: the UBIFS file-system description object | |
2181 | * @cnode: next cnode (nnode or pnode) to check | |
2182 | * @row: row of cnode (root is zero) | |
2183 | * @col: column of cnode (leftmost is zero) | |
2184 | * | |
2185 | * This function returns %0 on success and a negative error code on failure. | |
2186 | */ | |
2187 | int dbg_check_lpt_nodes(struct ubifs_info *c, struct ubifs_cnode *cnode, | |
2188 | int row, int col) | |
2189 | { | |
2190 | struct ubifs_nnode *nnode, *nn; | |
2191 | struct ubifs_cnode *cn; | |
2192 | int num, iip = 0, err; | |
2193 | ||
2194 | if (!(ubifs_chk_flags & UBIFS_CHK_LPROPS)) | |
2195 | return 0; | |
2196 | ||
2197 | while (cnode) { | |
2198 | ubifs_assert(row >= 0); | |
2199 | nnode = cnode->parent; | |
2200 | if (cnode->level) { | |
2201 | /* cnode is a nnode */ | |
2202 | num = calc_nnode_num(row, col); | |
2203 | if (cnode->num != num) { | |
2204 | dbg_err("nnode num %d expected %d " | |
2205 | "parent num %d iip %d", cnode->num, num, | |
2206 | (nnode ? nnode->num : 0), cnode->iip); | |
2207 | return -EINVAL; | |
2208 | } | |
2209 | nn = (struct ubifs_nnode *)cnode; | |
2210 | while (iip < UBIFS_LPT_FANOUT) { | |
2211 | cn = nn->nbranch[iip].cnode; | |
2212 | if (cn) { | |
2213 | /* Go down */ | |
2214 | row += 1; | |
2215 | col <<= UBIFS_LPT_FANOUT_SHIFT; | |
2216 | col += iip; | |
2217 | iip = 0; | |
2218 | cnode = cn; | |
2219 | break; | |
2220 | } | |
2221 | /* Go right */ | |
2222 | iip += 1; | |
2223 | } | |
2224 | if (iip < UBIFS_LPT_FANOUT) | |
2225 | continue; | |
2226 | } else { | |
2227 | struct ubifs_pnode *pnode; | |
2228 | ||
2229 | /* cnode is a pnode */ | |
2230 | pnode = (struct ubifs_pnode *)cnode; | |
2231 | err = dbg_chk_pnode(c, pnode, col); | |
2232 | if (err) | |
2233 | return err; | |
2234 | } | |
2235 | /* Go up and to the right */ | |
2236 | row -= 1; | |
2237 | col >>= UBIFS_LPT_FANOUT_SHIFT; | |
2238 | iip = cnode->iip + 1; | |
2239 | cnode = (struct ubifs_cnode *)nnode; | |
2240 | } | |
2241 | return 0; | |
2242 | } | |
2243 | ||
2244 | #endif /* CONFIG_UBIFS_FS_DEBUG */ |