Commit | Line | Data |
---|---|---|
7d4e9ccb | 1 | /* |
1e51764a AB |
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 commit-related functionality of the LEB properties | |
25 | * subsystem. | |
26 | */ | |
27 | ||
28 | #include <linux/crc16.h> | |
5a0e3ad6 | 29 | #include <linux/slab.h> |
8d7819b4 | 30 | #include <linux/random.h> |
1e51764a AB |
31 | #include "ubifs.h" |
32 | ||
cdd8ad6e | 33 | static int dbg_populate_lsave(struct ubifs_info *c); |
cdd8ad6e | 34 | |
1e51764a AB |
35 | /** |
36 | * first_dirty_cnode - find first dirty cnode. | |
37 | * @c: UBIFS file-system description object | |
38 | * @nnode: nnode at which to start | |
39 | * | |
40 | * This function returns the first dirty cnode or %NULL if there is not one. | |
41 | */ | |
42 | static struct ubifs_cnode *first_dirty_cnode(struct ubifs_nnode *nnode) | |
43 | { | |
44 | ubifs_assert(nnode); | |
45 | while (1) { | |
46 | int i, cont = 0; | |
47 | ||
48 | for (i = 0; i < UBIFS_LPT_FANOUT; i++) { | |
49 | struct ubifs_cnode *cnode; | |
50 | ||
51 | cnode = nnode->nbranch[i].cnode; | |
52 | if (cnode && | |
53 | test_bit(DIRTY_CNODE, &cnode->flags)) { | |
54 | if (cnode->level == 0) | |
55 | return cnode; | |
56 | nnode = (struct ubifs_nnode *)cnode; | |
57 | cont = 1; | |
58 | break; | |
59 | } | |
60 | } | |
61 | if (!cont) | |
62 | return (struct ubifs_cnode *)nnode; | |
63 | } | |
64 | } | |
65 | ||
66 | /** | |
67 | * next_dirty_cnode - find next dirty cnode. | |
68 | * @cnode: cnode from which to begin searching | |
69 | * | |
70 | * This function returns the next dirty cnode or %NULL if there is not one. | |
71 | */ | |
72 | static struct ubifs_cnode *next_dirty_cnode(struct ubifs_cnode *cnode) | |
73 | { | |
74 | struct ubifs_nnode *nnode; | |
75 | int i; | |
76 | ||
77 | ubifs_assert(cnode); | |
78 | nnode = cnode->parent; | |
79 | if (!nnode) | |
80 | return NULL; | |
81 | for (i = cnode->iip + 1; i < UBIFS_LPT_FANOUT; i++) { | |
82 | cnode = nnode->nbranch[i].cnode; | |
83 | if (cnode && test_bit(DIRTY_CNODE, &cnode->flags)) { | |
84 | if (cnode->level == 0) | |
85 | return cnode; /* cnode is a pnode */ | |
86 | /* cnode is a nnode */ | |
87 | return first_dirty_cnode((struct ubifs_nnode *)cnode); | |
88 | } | |
89 | } | |
90 | return (struct ubifs_cnode *)nnode; | |
91 | } | |
92 | ||
93 | /** | |
94 | * get_cnodes_to_commit - create list of dirty cnodes to commit. | |
95 | * @c: UBIFS file-system description object | |
96 | * | |
97 | * This function returns the number of cnodes to commit. | |
98 | */ | |
99 | static int get_cnodes_to_commit(struct ubifs_info *c) | |
100 | { | |
101 | struct ubifs_cnode *cnode, *cnext; | |
102 | int cnt = 0; | |
103 | ||
104 | if (!c->nroot) | |
105 | return 0; | |
106 | ||
107 | if (!test_bit(DIRTY_CNODE, &c->nroot->flags)) | |
108 | return 0; | |
109 | ||
110 | c->lpt_cnext = first_dirty_cnode(c->nroot); | |
111 | cnode = c->lpt_cnext; | |
112 | if (!cnode) | |
113 | return 0; | |
114 | cnt += 1; | |
115 | while (1) { | |
37662447 AB |
116 | ubifs_assert(!test_bit(COW_CNODE, &cnode->flags)); |
117 | __set_bit(COW_CNODE, &cnode->flags); | |
1e51764a AB |
118 | cnext = next_dirty_cnode(cnode); |
119 | if (!cnext) { | |
120 | cnode->cnext = c->lpt_cnext; | |
121 | break; | |
122 | } | |
123 | cnode->cnext = cnext; | |
124 | cnode = cnext; | |
125 | cnt += 1; | |
126 | } | |
127 | dbg_cmt("committing %d cnodes", cnt); | |
128 | dbg_lp("committing %d cnodes", cnt); | |
129 | ubifs_assert(cnt == c->dirty_nn_cnt + c->dirty_pn_cnt); | |
130 | return cnt; | |
131 | } | |
132 | ||
133 | /** | |
134 | * upd_ltab - update LPT LEB properties. | |
135 | * @c: UBIFS file-system description object | |
136 | * @lnum: LEB number | |
137 | * @free: amount of free space | |
138 | * @dirty: amount of dirty space to add | |
139 | */ | |
140 | static void upd_ltab(struct ubifs_info *c, int lnum, int free, int dirty) | |
141 | { | |
142 | dbg_lp("LEB %d free %d dirty %d to %d +%d", | |
143 | lnum, c->ltab[lnum - c->lpt_first].free, | |
144 | c->ltab[lnum - c->lpt_first].dirty, free, dirty); | |
145 | ubifs_assert(lnum >= c->lpt_first && lnum <= c->lpt_last); | |
146 | c->ltab[lnum - c->lpt_first].free = free; | |
147 | c->ltab[lnum - c->lpt_first].dirty += dirty; | |
148 | } | |
149 | ||
150 | /** | |
151 | * alloc_lpt_leb - allocate an LPT LEB that is empty. | |
152 | * @c: UBIFS file-system description object | |
153 | * @lnum: LEB number is passed and returned here | |
154 | * | |
155 | * This function finds the next empty LEB in the ltab starting from @lnum. If a | |
156 | * an empty LEB is found it is returned in @lnum and the function returns %0. | |
157 | * Otherwise the function returns -ENOSPC. Note however, that LPT is designed | |
158 | * never to run out of space. | |
159 | */ | |
160 | static int alloc_lpt_leb(struct ubifs_info *c, int *lnum) | |
161 | { | |
162 | int i, n; | |
163 | ||
164 | n = *lnum - c->lpt_first + 1; | |
165 | for (i = n; i < c->lpt_lebs; i++) { | |
166 | if (c->ltab[i].tgc || c->ltab[i].cmt) | |
167 | continue; | |
168 | if (c->ltab[i].free == c->leb_size) { | |
169 | c->ltab[i].cmt = 1; | |
170 | *lnum = i + c->lpt_first; | |
171 | return 0; | |
172 | } | |
173 | } | |
174 | ||
175 | for (i = 0; i < n; i++) { | |
176 | if (c->ltab[i].tgc || c->ltab[i].cmt) | |
177 | continue; | |
178 | if (c->ltab[i].free == c->leb_size) { | |
179 | c->ltab[i].cmt = 1; | |
180 | *lnum = i + c->lpt_first; | |
181 | return 0; | |
182 | } | |
183 | } | |
1e51764a AB |
184 | return -ENOSPC; |
185 | } | |
186 | ||
187 | /** | |
188 | * layout_cnodes - layout cnodes for commit. | |
189 | * @c: UBIFS file-system description object | |
190 | * | |
191 | * This function returns %0 on success and a negative error code on failure. | |
192 | */ | |
193 | static int layout_cnodes(struct ubifs_info *c) | |
194 | { | |
195 | int lnum, offs, len, alen, done_lsave, done_ltab, err; | |
196 | struct ubifs_cnode *cnode; | |
197 | ||
73944a6d AH |
198 | err = dbg_chk_lpt_sz(c, 0, 0); |
199 | if (err) | |
200 | return err; | |
1e51764a AB |
201 | cnode = c->lpt_cnext; |
202 | if (!cnode) | |
203 | return 0; | |
204 | lnum = c->nhead_lnum; | |
205 | offs = c->nhead_offs; | |
206 | /* Try to place lsave and ltab nicely */ | |
207 | done_lsave = !c->big_lpt; | |
208 | done_ltab = 0; | |
209 | if (!done_lsave && offs + c->lsave_sz <= c->leb_size) { | |
210 | done_lsave = 1; | |
211 | c->lsave_lnum = lnum; | |
212 | c->lsave_offs = offs; | |
213 | offs += c->lsave_sz; | |
73944a6d | 214 | dbg_chk_lpt_sz(c, 1, c->lsave_sz); |
1e51764a AB |
215 | } |
216 | ||
217 | if (offs + c->ltab_sz <= c->leb_size) { | |
218 | done_ltab = 1; | |
219 | c->ltab_lnum = lnum; | |
220 | c->ltab_offs = offs; | |
221 | offs += c->ltab_sz; | |
73944a6d | 222 | dbg_chk_lpt_sz(c, 1, c->ltab_sz); |
1e51764a AB |
223 | } |
224 | ||
225 | do { | |
226 | if (cnode->level) { | |
227 | len = c->nnode_sz; | |
228 | c->dirty_nn_cnt -= 1; | |
229 | } else { | |
230 | len = c->pnode_sz; | |
231 | c->dirty_pn_cnt -= 1; | |
232 | } | |
233 | while (offs + len > c->leb_size) { | |
234 | alen = ALIGN(offs, c->min_io_size); | |
235 | upd_ltab(c, lnum, c->leb_size - alen, alen - offs); | |
2bc275e9 | 236 | dbg_chk_lpt_sz(c, 2, c->leb_size - offs); |
1e51764a AB |
237 | err = alloc_lpt_leb(c, &lnum); |
238 | if (err) | |
73944a6d | 239 | goto no_space; |
1e51764a AB |
240 | offs = 0; |
241 | ubifs_assert(lnum >= c->lpt_first && | |
242 | lnum <= c->lpt_last); | |
243 | /* Try to place lsave and ltab nicely */ | |
244 | if (!done_lsave) { | |
245 | done_lsave = 1; | |
246 | c->lsave_lnum = lnum; | |
247 | c->lsave_offs = offs; | |
248 | offs += c->lsave_sz; | |
73944a6d | 249 | dbg_chk_lpt_sz(c, 1, c->lsave_sz); |
1e51764a AB |
250 | continue; |
251 | } | |
252 | if (!done_ltab) { | |
253 | done_ltab = 1; | |
254 | c->ltab_lnum = lnum; | |
255 | c->ltab_offs = offs; | |
256 | offs += c->ltab_sz; | |
73944a6d | 257 | dbg_chk_lpt_sz(c, 1, c->ltab_sz); |
1e51764a AB |
258 | continue; |
259 | } | |
260 | break; | |
261 | } | |
262 | if (cnode->parent) { | |
263 | cnode->parent->nbranch[cnode->iip].lnum = lnum; | |
264 | cnode->parent->nbranch[cnode->iip].offs = offs; | |
265 | } else { | |
266 | c->lpt_lnum = lnum; | |
267 | c->lpt_offs = offs; | |
268 | } | |
269 | offs += len; | |
73944a6d | 270 | dbg_chk_lpt_sz(c, 1, len); |
1e51764a AB |
271 | cnode = cnode->cnext; |
272 | } while (cnode && cnode != c->lpt_cnext); | |
273 | ||
274 | /* Make sure to place LPT's save table */ | |
275 | if (!done_lsave) { | |
276 | if (offs + c->lsave_sz > c->leb_size) { | |
277 | alen = ALIGN(offs, c->min_io_size); | |
278 | upd_ltab(c, lnum, c->leb_size - alen, alen - offs); | |
2bc275e9 | 279 | dbg_chk_lpt_sz(c, 2, c->leb_size - offs); |
1e51764a AB |
280 | err = alloc_lpt_leb(c, &lnum); |
281 | if (err) | |
73944a6d | 282 | goto no_space; |
1e51764a AB |
283 | offs = 0; |
284 | ubifs_assert(lnum >= c->lpt_first && | |
285 | lnum <= c->lpt_last); | |
286 | } | |
287 | done_lsave = 1; | |
288 | c->lsave_lnum = lnum; | |
289 | c->lsave_offs = offs; | |
290 | offs += c->lsave_sz; | |
73944a6d | 291 | dbg_chk_lpt_sz(c, 1, c->lsave_sz); |
1e51764a AB |
292 | } |
293 | ||
294 | /* Make sure to place LPT's own lprops table */ | |
295 | if (!done_ltab) { | |
296 | if (offs + c->ltab_sz > c->leb_size) { | |
297 | alen = ALIGN(offs, c->min_io_size); | |
298 | upd_ltab(c, lnum, c->leb_size - alen, alen - offs); | |
2bc275e9 | 299 | dbg_chk_lpt_sz(c, 2, c->leb_size - offs); |
1e51764a AB |
300 | err = alloc_lpt_leb(c, &lnum); |
301 | if (err) | |
73944a6d | 302 | goto no_space; |
1e51764a AB |
303 | offs = 0; |
304 | ubifs_assert(lnum >= c->lpt_first && | |
305 | lnum <= c->lpt_last); | |
306 | } | |
307 | done_ltab = 1; | |
308 | c->ltab_lnum = lnum; | |
309 | c->ltab_offs = offs; | |
310 | offs += c->ltab_sz; | |
73944a6d | 311 | dbg_chk_lpt_sz(c, 1, c->ltab_sz); |
1e51764a AB |
312 | } |
313 | ||
314 | alen = ALIGN(offs, c->min_io_size); | |
315 | upd_ltab(c, lnum, c->leb_size - alen, alen - offs); | |
73944a6d AH |
316 | dbg_chk_lpt_sz(c, 4, alen - offs); |
317 | err = dbg_chk_lpt_sz(c, 3, alen); | |
318 | if (err) | |
319 | return err; | |
1e51764a | 320 | return 0; |
73944a6d AH |
321 | |
322 | no_space: | |
a6aae4dd AB |
323 | ubifs_err("LPT out of space at LEB %d:%d needing %d, done_ltab %d, " |
324 | "done_lsave %d", lnum, offs, len, done_ltab, done_lsave); | |
edf6be24 AB |
325 | ubifs_dump_lpt_info(c); |
326 | ubifs_dump_lpt_lebs(c); | |
787845bd | 327 | dump_stack(); |
73944a6d | 328 | return err; |
1e51764a AB |
329 | } |
330 | ||
331 | /** | |
332 | * realloc_lpt_leb - allocate an LPT LEB that is empty. | |
333 | * @c: UBIFS file-system description object | |
334 | * @lnum: LEB number is passed and returned here | |
335 | * | |
336 | * This function duplicates exactly the results of the function alloc_lpt_leb. | |
337 | * It is used during end commit to reallocate the same LEB numbers that were | |
338 | * allocated by alloc_lpt_leb during start commit. | |
339 | * | |
340 | * This function finds the next LEB that was allocated by the alloc_lpt_leb | |
341 | * function starting from @lnum. If a LEB is found it is returned in @lnum and | |
342 | * the function returns %0. Otherwise the function returns -ENOSPC. | |
343 | * Note however, that LPT is designed never to run out of space. | |
344 | */ | |
345 | static int realloc_lpt_leb(struct ubifs_info *c, int *lnum) | |
346 | { | |
347 | int i, n; | |
348 | ||
349 | n = *lnum - c->lpt_first + 1; | |
350 | for (i = n; i < c->lpt_lebs; i++) | |
351 | if (c->ltab[i].cmt) { | |
352 | c->ltab[i].cmt = 0; | |
353 | *lnum = i + c->lpt_first; | |
354 | return 0; | |
355 | } | |
356 | ||
357 | for (i = 0; i < n; i++) | |
358 | if (c->ltab[i].cmt) { | |
359 | c->ltab[i].cmt = 0; | |
360 | *lnum = i + c->lpt_first; | |
361 | return 0; | |
362 | } | |
1e51764a AB |
363 | return -ENOSPC; |
364 | } | |
365 | ||
366 | /** | |
367 | * write_cnodes - write cnodes for commit. | |
368 | * @c: UBIFS file-system description object | |
369 | * | |
370 | * This function returns %0 on success and a negative error code on failure. | |
371 | */ | |
372 | static int write_cnodes(struct ubifs_info *c) | |
373 | { | |
374 | int lnum, offs, len, from, err, wlen, alen, done_ltab, done_lsave; | |
375 | struct ubifs_cnode *cnode; | |
376 | void *buf = c->lpt_buf; | |
377 | ||
378 | cnode = c->lpt_cnext; | |
379 | if (!cnode) | |
380 | return 0; | |
381 | lnum = c->nhead_lnum; | |
382 | offs = c->nhead_offs; | |
383 | from = offs; | |
384 | /* Ensure empty LEB is unmapped */ | |
385 | if (offs == 0) { | |
386 | err = ubifs_leb_unmap(c, lnum); | |
387 | if (err) | |
388 | return err; | |
389 | } | |
390 | /* Try to place lsave and ltab nicely */ | |
391 | done_lsave = !c->big_lpt; | |
392 | done_ltab = 0; | |
393 | if (!done_lsave && offs + c->lsave_sz <= c->leb_size) { | |
394 | done_lsave = 1; | |
395 | ubifs_pack_lsave(c, buf + offs, c->lsave); | |
396 | offs += c->lsave_sz; | |
73944a6d | 397 | dbg_chk_lpt_sz(c, 1, c->lsave_sz); |
1e51764a AB |
398 | } |
399 | ||
400 | if (offs + c->ltab_sz <= c->leb_size) { | |
401 | done_ltab = 1; | |
402 | ubifs_pack_ltab(c, buf + offs, c->ltab_cmt); | |
403 | offs += c->ltab_sz; | |
73944a6d | 404 | dbg_chk_lpt_sz(c, 1, c->ltab_sz); |
1e51764a AB |
405 | } |
406 | ||
407 | /* Loop for each cnode */ | |
408 | do { | |
409 | if (cnode->level) | |
410 | len = c->nnode_sz; | |
411 | else | |
412 | len = c->pnode_sz; | |
413 | while (offs + len > c->leb_size) { | |
414 | wlen = offs - from; | |
415 | if (wlen) { | |
416 | alen = ALIGN(wlen, c->min_io_size); | |
417 | memset(buf + offs, 0xff, alen - wlen); | |
418 | err = ubifs_leb_write(c, lnum, buf + from, from, | |
419 | alen, UBI_SHORTTERM); | |
420 | if (err) | |
421 | return err; | |
422 | } | |
2bc275e9 | 423 | dbg_chk_lpt_sz(c, 2, c->leb_size - offs); |
1e51764a AB |
424 | err = realloc_lpt_leb(c, &lnum); |
425 | if (err) | |
73944a6d | 426 | goto no_space; |
0a6fb8d9 | 427 | offs = from = 0; |
1e51764a AB |
428 | ubifs_assert(lnum >= c->lpt_first && |
429 | lnum <= c->lpt_last); | |
430 | err = ubifs_leb_unmap(c, lnum); | |
431 | if (err) | |
432 | return err; | |
433 | /* Try to place lsave and ltab nicely */ | |
434 | if (!done_lsave) { | |
435 | done_lsave = 1; | |
436 | ubifs_pack_lsave(c, buf + offs, c->lsave); | |
437 | offs += c->lsave_sz; | |
73944a6d | 438 | dbg_chk_lpt_sz(c, 1, c->lsave_sz); |
1e51764a AB |
439 | continue; |
440 | } | |
441 | if (!done_ltab) { | |
442 | done_ltab = 1; | |
443 | ubifs_pack_ltab(c, buf + offs, c->ltab_cmt); | |
444 | offs += c->ltab_sz; | |
73944a6d | 445 | dbg_chk_lpt_sz(c, 1, c->ltab_sz); |
1e51764a AB |
446 | continue; |
447 | } | |
448 | break; | |
449 | } | |
450 | if (cnode->level) | |
451 | ubifs_pack_nnode(c, buf + offs, | |
452 | (struct ubifs_nnode *)cnode); | |
453 | else | |
454 | ubifs_pack_pnode(c, buf + offs, | |
455 | (struct ubifs_pnode *)cnode); | |
456 | /* | |
457 | * The reason for the barriers is the same as in case of TNC. | |
458 | * See comment in 'write_index()'. 'dirty_cow_nnode()' and | |
459 | * 'dirty_cow_pnode()' are the functions for which this is | |
460 | * important. | |
461 | */ | |
462 | clear_bit(DIRTY_CNODE, &cnode->flags); | |
463 | smp_mb__before_clear_bit(); | |
37662447 | 464 | clear_bit(COW_CNODE, &cnode->flags); |
1e51764a AB |
465 | smp_mb__after_clear_bit(); |
466 | offs += len; | |
73944a6d | 467 | dbg_chk_lpt_sz(c, 1, len); |
1e51764a AB |
468 | cnode = cnode->cnext; |
469 | } while (cnode && cnode != c->lpt_cnext); | |
470 | ||
471 | /* Make sure to place LPT's save table */ | |
472 | if (!done_lsave) { | |
473 | if (offs + c->lsave_sz > c->leb_size) { | |
474 | wlen = offs - from; | |
475 | alen = ALIGN(wlen, c->min_io_size); | |
476 | memset(buf + offs, 0xff, alen - wlen); | |
477 | err = ubifs_leb_write(c, lnum, buf + from, from, alen, | |
478 | UBI_SHORTTERM); | |
479 | if (err) | |
480 | return err; | |
2bc275e9 | 481 | dbg_chk_lpt_sz(c, 2, c->leb_size - offs); |
1e51764a AB |
482 | err = realloc_lpt_leb(c, &lnum); |
483 | if (err) | |
73944a6d | 484 | goto no_space; |
0a6fb8d9 | 485 | offs = from = 0; |
1e51764a AB |
486 | ubifs_assert(lnum >= c->lpt_first && |
487 | lnum <= c->lpt_last); | |
488 | err = ubifs_leb_unmap(c, lnum); | |
489 | if (err) | |
490 | return err; | |
491 | } | |
492 | done_lsave = 1; | |
493 | ubifs_pack_lsave(c, buf + offs, c->lsave); | |
494 | offs += c->lsave_sz; | |
73944a6d | 495 | dbg_chk_lpt_sz(c, 1, c->lsave_sz); |
1e51764a AB |
496 | } |
497 | ||
498 | /* Make sure to place LPT's own lprops table */ | |
499 | if (!done_ltab) { | |
500 | if (offs + c->ltab_sz > c->leb_size) { | |
501 | wlen = offs - from; | |
502 | alen = ALIGN(wlen, c->min_io_size); | |
503 | memset(buf + offs, 0xff, alen - wlen); | |
504 | err = ubifs_leb_write(c, lnum, buf + from, from, alen, | |
505 | UBI_SHORTTERM); | |
506 | if (err) | |
507 | return err; | |
2bc275e9 | 508 | dbg_chk_lpt_sz(c, 2, c->leb_size - offs); |
1e51764a AB |
509 | err = realloc_lpt_leb(c, &lnum); |
510 | if (err) | |
73944a6d | 511 | goto no_space; |
0a6fb8d9 | 512 | offs = from = 0; |
1e51764a AB |
513 | ubifs_assert(lnum >= c->lpt_first && |
514 | lnum <= c->lpt_last); | |
515 | err = ubifs_leb_unmap(c, lnum); | |
516 | if (err) | |
517 | return err; | |
518 | } | |
519 | done_ltab = 1; | |
520 | ubifs_pack_ltab(c, buf + offs, c->ltab_cmt); | |
521 | offs += c->ltab_sz; | |
73944a6d | 522 | dbg_chk_lpt_sz(c, 1, c->ltab_sz); |
1e51764a AB |
523 | } |
524 | ||
525 | /* Write remaining data in buffer */ | |
526 | wlen = offs - from; | |
527 | alen = ALIGN(wlen, c->min_io_size); | |
528 | memset(buf + offs, 0xff, alen - wlen); | |
529 | err = ubifs_leb_write(c, lnum, buf + from, from, alen, UBI_SHORTTERM); | |
530 | if (err) | |
531 | return err; | |
73944a6d AH |
532 | |
533 | dbg_chk_lpt_sz(c, 4, alen - wlen); | |
534 | err = dbg_chk_lpt_sz(c, 3, ALIGN(offs, c->min_io_size)); | |
535 | if (err) | |
536 | return err; | |
537 | ||
1e51764a AB |
538 | c->nhead_lnum = lnum; |
539 | c->nhead_offs = ALIGN(offs, c->min_io_size); | |
540 | ||
541 | dbg_lp("LPT root is at %d:%d", c->lpt_lnum, c->lpt_offs); | |
542 | dbg_lp("LPT head is at %d:%d", c->nhead_lnum, c->nhead_offs); | |
543 | dbg_lp("LPT ltab is at %d:%d", c->ltab_lnum, c->ltab_offs); | |
544 | if (c->big_lpt) | |
545 | dbg_lp("LPT lsave is at %d:%d", c->lsave_lnum, c->lsave_offs); | |
73944a6d | 546 | |
1e51764a | 547 | return 0; |
73944a6d AH |
548 | |
549 | no_space: | |
a6aae4dd AB |
550 | ubifs_err("LPT out of space mismatch at LEB %d:%d needing %d, done_ltab " |
551 | "%d, done_lsave %d", lnum, offs, len, done_ltab, done_lsave); | |
edf6be24 AB |
552 | ubifs_dump_lpt_info(c); |
553 | ubifs_dump_lpt_lebs(c); | |
787845bd | 554 | dump_stack(); |
73944a6d | 555 | return err; |
1e51764a AB |
556 | } |
557 | ||
558 | /** | |
4a29d200 | 559 | * next_pnode_to_dirty - find next pnode to dirty. |
1e51764a AB |
560 | * @c: UBIFS file-system description object |
561 | * @pnode: pnode | |
562 | * | |
4a29d200 AH |
563 | * This function returns the next pnode to dirty or %NULL if there are no more |
564 | * pnodes. Note that pnodes that have never been written (lnum == 0) are | |
565 | * skipped. | |
1e51764a | 566 | */ |
4a29d200 AH |
567 | static struct ubifs_pnode *next_pnode_to_dirty(struct ubifs_info *c, |
568 | struct ubifs_pnode *pnode) | |
1e51764a AB |
569 | { |
570 | struct ubifs_nnode *nnode; | |
571 | int iip; | |
572 | ||
573 | /* Try to go right */ | |
574 | nnode = pnode->parent; | |
4a29d200 | 575 | for (iip = pnode->iip + 1; iip < UBIFS_LPT_FANOUT; iip++) { |
1e51764a AB |
576 | if (nnode->nbranch[iip].lnum) |
577 | return ubifs_get_pnode(c, nnode, iip); | |
1e51764a AB |
578 | } |
579 | ||
580 | /* Go up while can't go right */ | |
581 | do { | |
582 | iip = nnode->iip + 1; | |
583 | nnode = nnode->parent; | |
584 | if (!nnode) | |
585 | return NULL; | |
4a29d200 AH |
586 | for (; iip < UBIFS_LPT_FANOUT; iip++) { |
587 | if (nnode->nbranch[iip].lnum) | |
588 | break; | |
589 | } | |
c4361570 | 590 | } while (iip >= UBIFS_LPT_FANOUT); |
1e51764a AB |
591 | |
592 | /* Go right */ | |
593 | nnode = ubifs_get_nnode(c, nnode, iip); | |
594 | if (IS_ERR(nnode)) | |
595 | return (void *)nnode; | |
596 | ||
597 | /* Go down to level 1 */ | |
598 | while (nnode->level > 1) { | |
4a29d200 AH |
599 | for (iip = 0; iip < UBIFS_LPT_FANOUT; iip++) { |
600 | if (nnode->nbranch[iip].lnum) | |
601 | break; | |
602 | } | |
603 | if (iip >= UBIFS_LPT_FANOUT) { | |
604 | /* | |
605 | * Should not happen, but we need to keep going | |
606 | * if it does. | |
607 | */ | |
608 | iip = 0; | |
609 | } | |
610 | nnode = ubifs_get_nnode(c, nnode, iip); | |
1e51764a AB |
611 | if (IS_ERR(nnode)) |
612 | return (void *)nnode; | |
613 | } | |
614 | ||
4a29d200 AH |
615 | for (iip = 0; iip < UBIFS_LPT_FANOUT; iip++) |
616 | if (nnode->nbranch[iip].lnum) | |
617 | break; | |
618 | if (iip >= UBIFS_LPT_FANOUT) | |
619 | /* Should not happen, but we need to keep going if it does */ | |
620 | iip = 0; | |
621 | return ubifs_get_pnode(c, nnode, iip); | |
1e51764a AB |
622 | } |
623 | ||
624 | /** | |
625 | * pnode_lookup - lookup a pnode in the LPT. | |
626 | * @c: UBIFS file-system description object | |
627 | * @i: pnode number (0 to main_lebs - 1) | |
628 | * | |
629 | * This function returns a pointer to the pnode on success or a negative | |
630 | * error code on failure. | |
631 | */ | |
632 | static struct ubifs_pnode *pnode_lookup(struct ubifs_info *c, int i) | |
633 | { | |
634 | int err, h, iip, shft; | |
635 | struct ubifs_nnode *nnode; | |
636 | ||
637 | if (!c->nroot) { | |
638 | err = ubifs_read_nnode(c, NULL, 0); | |
639 | if (err) | |
640 | return ERR_PTR(err); | |
641 | } | |
642 | i <<= UBIFS_LPT_FANOUT_SHIFT; | |
643 | nnode = c->nroot; | |
644 | shft = c->lpt_hght * UBIFS_LPT_FANOUT_SHIFT; | |
645 | for (h = 1; h < c->lpt_hght; h++) { | |
646 | iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1)); | |
647 | shft -= UBIFS_LPT_FANOUT_SHIFT; | |
648 | nnode = ubifs_get_nnode(c, nnode, iip); | |
649 | if (IS_ERR(nnode)) | |
6da5156f | 650 | return ERR_CAST(nnode); |
1e51764a AB |
651 | } |
652 | iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1)); | |
653 | return ubifs_get_pnode(c, nnode, iip); | |
654 | } | |
655 | ||
656 | /** | |
657 | * add_pnode_dirt - add dirty space to LPT LEB properties. | |
658 | * @c: UBIFS file-system description object | |
659 | * @pnode: pnode for which to add dirt | |
660 | */ | |
661 | static void add_pnode_dirt(struct ubifs_info *c, struct ubifs_pnode *pnode) | |
662 | { | |
663 | ubifs_add_lpt_dirt(c, pnode->parent->nbranch[pnode->iip].lnum, | |
664 | c->pnode_sz); | |
665 | } | |
666 | ||
667 | /** | |
668 | * do_make_pnode_dirty - mark a pnode dirty. | |
669 | * @c: UBIFS file-system description object | |
670 | * @pnode: pnode to mark dirty | |
671 | */ | |
672 | static void do_make_pnode_dirty(struct ubifs_info *c, struct ubifs_pnode *pnode) | |
673 | { | |
674 | /* Assumes cnext list is empty i.e. not called during commit */ | |
675 | if (!test_and_set_bit(DIRTY_CNODE, &pnode->flags)) { | |
676 | struct ubifs_nnode *nnode; | |
677 | ||
678 | c->dirty_pn_cnt += 1; | |
679 | add_pnode_dirt(c, pnode); | |
680 | /* Mark parent and ancestors dirty too */ | |
681 | nnode = pnode->parent; | |
682 | while (nnode) { | |
683 | if (!test_and_set_bit(DIRTY_CNODE, &nnode->flags)) { | |
684 | c->dirty_nn_cnt += 1; | |
685 | ubifs_add_nnode_dirt(c, nnode); | |
686 | nnode = nnode->parent; | |
687 | } else | |
688 | break; | |
689 | } | |
690 | } | |
691 | } | |
692 | ||
693 | /** | |
694 | * make_tree_dirty - mark the entire LEB properties tree dirty. | |
695 | * @c: UBIFS file-system description object | |
696 | * | |
697 | * This function is used by the "small" LPT model to cause the entire LEB | |
698 | * properties tree to be written. The "small" LPT model does not use LPT | |
699 | * garbage collection because it is more efficient to write the entire tree | |
700 | * (because it is small). | |
701 | * | |
702 | * This function returns %0 on success and a negative error code on failure. | |
703 | */ | |
704 | static int make_tree_dirty(struct ubifs_info *c) | |
705 | { | |
706 | struct ubifs_pnode *pnode; | |
707 | ||
708 | pnode = pnode_lookup(c, 0); | |
8c893a55 VK |
709 | if (IS_ERR(pnode)) |
710 | return PTR_ERR(pnode); | |
711 | ||
1e51764a AB |
712 | while (pnode) { |
713 | do_make_pnode_dirty(c, pnode); | |
4a29d200 | 714 | pnode = next_pnode_to_dirty(c, pnode); |
1e51764a AB |
715 | if (IS_ERR(pnode)) |
716 | return PTR_ERR(pnode); | |
717 | } | |
718 | return 0; | |
719 | } | |
720 | ||
721 | /** | |
722 | * need_write_all - determine if the LPT area is running out of free space. | |
723 | * @c: UBIFS file-system description object | |
724 | * | |
725 | * This function returns %1 if the LPT area is running out of free space and %0 | |
726 | * if it is not. | |
727 | */ | |
728 | static int need_write_all(struct ubifs_info *c) | |
729 | { | |
730 | long long free = 0; | |
731 | int i; | |
732 | ||
733 | for (i = 0; i < c->lpt_lebs; i++) { | |
734 | if (i + c->lpt_first == c->nhead_lnum) | |
735 | free += c->leb_size - c->nhead_offs; | |
736 | else if (c->ltab[i].free == c->leb_size) | |
737 | free += c->leb_size; | |
738 | else if (c->ltab[i].free + c->ltab[i].dirty == c->leb_size) | |
739 | free += c->leb_size; | |
740 | } | |
741 | /* Less than twice the size left */ | |
742 | if (free <= c->lpt_sz * 2) | |
743 | return 1; | |
744 | return 0; | |
745 | } | |
746 | ||
747 | /** | |
748 | * lpt_tgc_start - start trivial garbage collection of LPT LEBs. | |
749 | * @c: UBIFS file-system description object | |
750 | * | |
751 | * LPT trivial garbage collection is where a LPT LEB contains only dirty and | |
752 | * free space and so may be reused as soon as the next commit is completed. | |
753 | * This function is called during start commit to mark LPT LEBs for trivial GC. | |
754 | */ | |
755 | static void lpt_tgc_start(struct ubifs_info *c) | |
756 | { | |
757 | int i; | |
758 | ||
759 | for (i = 0; i < c->lpt_lebs; i++) { | |
760 | if (i + c->lpt_first == c->nhead_lnum) | |
761 | continue; | |
762 | if (c->ltab[i].dirty > 0 && | |
763 | c->ltab[i].free + c->ltab[i].dirty == c->leb_size) { | |
764 | c->ltab[i].tgc = 1; | |
765 | c->ltab[i].free = c->leb_size; | |
766 | c->ltab[i].dirty = 0; | |
767 | dbg_lp("LEB %d", i + c->lpt_first); | |
768 | } | |
769 | } | |
770 | } | |
771 | ||
772 | /** | |
773 | * lpt_tgc_end - end trivial garbage collection of LPT LEBs. | |
774 | * @c: UBIFS file-system description object | |
775 | * | |
776 | * LPT trivial garbage collection is where a LPT LEB contains only dirty and | |
777 | * free space and so may be reused as soon as the next commit is completed. | |
778 | * This function is called after the commit is completed (master node has been | |
80736d41 | 779 | * written) and un-maps LPT LEBs that were marked for trivial GC. |
1e51764a AB |
780 | */ |
781 | static int lpt_tgc_end(struct ubifs_info *c) | |
782 | { | |
783 | int i, err; | |
784 | ||
785 | for (i = 0; i < c->lpt_lebs; i++) | |
786 | if (c->ltab[i].tgc) { | |
787 | err = ubifs_leb_unmap(c, i + c->lpt_first); | |
788 | if (err) | |
789 | return err; | |
790 | c->ltab[i].tgc = 0; | |
791 | dbg_lp("LEB %d", i + c->lpt_first); | |
792 | } | |
793 | return 0; | |
794 | } | |
795 | ||
796 | /** | |
797 | * populate_lsave - fill the lsave array with important LEB numbers. | |
798 | * @c: the UBIFS file-system description object | |
799 | * | |
800 | * This function is only called for the "big" model. It records a small number | |
801 | * of LEB numbers of important LEBs. Important LEBs are ones that are (from | |
802 | * most important to least important): empty, freeable, freeable index, dirty | |
803 | * index, dirty or free. Upon mount, we read this list of LEB numbers and bring | |
804 | * their pnodes into memory. That will stop us from having to scan the LPT | |
805 | * straight away. For the "small" model we assume that scanning the LPT is no | |
806 | * big deal. | |
807 | */ | |
808 | static void populate_lsave(struct ubifs_info *c) | |
809 | { | |
810 | struct ubifs_lprops *lprops; | |
811 | struct ubifs_lpt_heap *heap; | |
812 | int i, cnt = 0; | |
813 | ||
814 | ubifs_assert(c->big_lpt); | |
815 | if (!(c->lpt_drty_flgs & LSAVE_DIRTY)) { | |
816 | c->lpt_drty_flgs |= LSAVE_DIRTY; | |
817 | ubifs_add_lpt_dirt(c, c->lsave_lnum, c->lsave_sz); | |
818 | } | |
cdd8ad6e AB |
819 | |
820 | if (dbg_populate_lsave(c)) | |
821 | return; | |
822 | ||
1e51764a AB |
823 | list_for_each_entry(lprops, &c->empty_list, list) { |
824 | c->lsave[cnt++] = lprops->lnum; | |
825 | if (cnt >= c->lsave_cnt) | |
826 | return; | |
827 | } | |
828 | list_for_each_entry(lprops, &c->freeable_list, list) { | |
829 | c->lsave[cnt++] = lprops->lnum; | |
830 | if (cnt >= c->lsave_cnt) | |
831 | return; | |
832 | } | |
833 | list_for_each_entry(lprops, &c->frdi_idx_list, list) { | |
834 | c->lsave[cnt++] = lprops->lnum; | |
835 | if (cnt >= c->lsave_cnt) | |
836 | return; | |
837 | } | |
838 | heap = &c->lpt_heap[LPROPS_DIRTY_IDX - 1]; | |
839 | for (i = 0; i < heap->cnt; i++) { | |
840 | c->lsave[cnt++] = heap->arr[i]->lnum; | |
841 | if (cnt >= c->lsave_cnt) | |
842 | return; | |
843 | } | |
844 | heap = &c->lpt_heap[LPROPS_DIRTY - 1]; | |
845 | for (i = 0; i < heap->cnt; i++) { | |
846 | c->lsave[cnt++] = heap->arr[i]->lnum; | |
847 | if (cnt >= c->lsave_cnt) | |
848 | return; | |
849 | } | |
850 | heap = &c->lpt_heap[LPROPS_FREE - 1]; | |
851 | for (i = 0; i < heap->cnt; i++) { | |
852 | c->lsave[cnt++] = heap->arr[i]->lnum; | |
853 | if (cnt >= c->lsave_cnt) | |
854 | return; | |
855 | } | |
856 | /* Fill it up completely */ | |
857 | while (cnt < c->lsave_cnt) | |
858 | c->lsave[cnt++] = c->main_first; | |
859 | } | |
860 | ||
861 | /** | |
862 | * nnode_lookup - lookup a nnode in the LPT. | |
863 | * @c: UBIFS file-system description object | |
864 | * @i: nnode number | |
865 | * | |
866 | * This function returns a pointer to the nnode on success or a negative | |
867 | * error code on failure. | |
868 | */ | |
869 | static struct ubifs_nnode *nnode_lookup(struct ubifs_info *c, int i) | |
870 | { | |
871 | int err, iip; | |
872 | struct ubifs_nnode *nnode; | |
873 | ||
874 | if (!c->nroot) { | |
875 | err = ubifs_read_nnode(c, NULL, 0); | |
876 | if (err) | |
877 | return ERR_PTR(err); | |
878 | } | |
879 | nnode = c->nroot; | |
880 | while (1) { | |
881 | iip = i & (UBIFS_LPT_FANOUT - 1); | |
882 | i >>= UBIFS_LPT_FANOUT_SHIFT; | |
883 | if (!i) | |
884 | break; | |
885 | nnode = ubifs_get_nnode(c, nnode, iip); | |
886 | if (IS_ERR(nnode)) | |
887 | return nnode; | |
888 | } | |
889 | return nnode; | |
890 | } | |
891 | ||
892 | /** | |
893 | * make_nnode_dirty - find a nnode and, if found, make it dirty. | |
894 | * @c: UBIFS file-system description object | |
895 | * @node_num: nnode number of nnode to make dirty | |
896 | * @lnum: LEB number where nnode was written | |
897 | * @offs: offset where nnode was written | |
898 | * | |
899 | * This function is used by LPT garbage collection. LPT garbage collection is | |
900 | * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection | |
901 | * simply involves marking all the nodes in the LEB being garbage-collected as | |
902 | * dirty. The dirty nodes are written next commit, after which the LEB is free | |
903 | * to be reused. | |
904 | * | |
905 | * This function returns %0 on success and a negative error code on failure. | |
906 | */ | |
907 | static int make_nnode_dirty(struct ubifs_info *c, int node_num, int lnum, | |
908 | int offs) | |
909 | { | |
910 | struct ubifs_nnode *nnode; | |
911 | ||
912 | nnode = nnode_lookup(c, node_num); | |
913 | if (IS_ERR(nnode)) | |
914 | return PTR_ERR(nnode); | |
915 | if (nnode->parent) { | |
916 | struct ubifs_nbranch *branch; | |
917 | ||
918 | branch = &nnode->parent->nbranch[nnode->iip]; | |
919 | if (branch->lnum != lnum || branch->offs != offs) | |
920 | return 0; /* nnode is obsolete */ | |
921 | } else if (c->lpt_lnum != lnum || c->lpt_offs != offs) | |
922 | return 0; /* nnode is obsolete */ | |
923 | /* Assumes cnext list is empty i.e. not called during commit */ | |
924 | if (!test_and_set_bit(DIRTY_CNODE, &nnode->flags)) { | |
925 | c->dirty_nn_cnt += 1; | |
926 | ubifs_add_nnode_dirt(c, nnode); | |
927 | /* Mark parent and ancestors dirty too */ | |
928 | nnode = nnode->parent; | |
929 | while (nnode) { | |
930 | if (!test_and_set_bit(DIRTY_CNODE, &nnode->flags)) { | |
931 | c->dirty_nn_cnt += 1; | |
932 | ubifs_add_nnode_dirt(c, nnode); | |
933 | nnode = nnode->parent; | |
934 | } else | |
935 | break; | |
936 | } | |
937 | } | |
938 | return 0; | |
939 | } | |
940 | ||
941 | /** | |
942 | * make_pnode_dirty - find a pnode and, if found, make it dirty. | |
943 | * @c: UBIFS file-system description object | |
944 | * @node_num: pnode number of pnode to make dirty | |
945 | * @lnum: LEB number where pnode was written | |
946 | * @offs: offset where pnode was written | |
947 | * | |
948 | * This function is used by LPT garbage collection. LPT garbage collection is | |
949 | * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection | |
950 | * simply involves marking all the nodes in the LEB being garbage-collected as | |
951 | * dirty. The dirty nodes are written next commit, after which the LEB is free | |
952 | * to be reused. | |
953 | * | |
954 | * This function returns %0 on success and a negative error code on failure. | |
955 | */ | |
956 | static int make_pnode_dirty(struct ubifs_info *c, int node_num, int lnum, | |
957 | int offs) | |
958 | { | |
959 | struct ubifs_pnode *pnode; | |
960 | struct ubifs_nbranch *branch; | |
961 | ||
962 | pnode = pnode_lookup(c, node_num); | |
963 | if (IS_ERR(pnode)) | |
964 | return PTR_ERR(pnode); | |
965 | branch = &pnode->parent->nbranch[pnode->iip]; | |
966 | if (branch->lnum != lnum || branch->offs != offs) | |
967 | return 0; | |
968 | do_make_pnode_dirty(c, pnode); | |
969 | return 0; | |
970 | } | |
971 | ||
972 | /** | |
973 | * make_ltab_dirty - make ltab node dirty. | |
974 | * @c: UBIFS file-system description object | |
975 | * @lnum: LEB number where ltab was written | |
976 | * @offs: offset where ltab was written | |
977 | * | |
978 | * This function is used by LPT garbage collection. LPT garbage collection is | |
979 | * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection | |
980 | * simply involves marking all the nodes in the LEB being garbage-collected as | |
981 | * dirty. The dirty nodes are written next commit, after which the LEB is free | |
982 | * to be reused. | |
983 | * | |
984 | * This function returns %0 on success and a negative error code on failure. | |
985 | */ | |
986 | static int make_ltab_dirty(struct ubifs_info *c, int lnum, int offs) | |
987 | { | |
988 | if (lnum != c->ltab_lnum || offs != c->ltab_offs) | |
989 | return 0; /* This ltab node is obsolete */ | |
990 | if (!(c->lpt_drty_flgs & LTAB_DIRTY)) { | |
991 | c->lpt_drty_flgs |= LTAB_DIRTY; | |
992 | ubifs_add_lpt_dirt(c, c->ltab_lnum, c->ltab_sz); | |
993 | } | |
994 | return 0; | |
995 | } | |
996 | ||
997 | /** | |
998 | * make_lsave_dirty - make lsave node dirty. | |
999 | * @c: UBIFS file-system description object | |
1000 | * @lnum: LEB number where lsave was written | |
1001 | * @offs: offset where lsave was written | |
1002 | * | |
1003 | * This function is used by LPT garbage collection. LPT garbage collection is | |
1004 | * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection | |
1005 | * simply involves marking all the nodes in the LEB being garbage-collected as | |
1006 | * dirty. The dirty nodes are written next commit, after which the LEB is free | |
1007 | * to be reused. | |
1008 | * | |
1009 | * This function returns %0 on success and a negative error code on failure. | |
1010 | */ | |
1011 | static int make_lsave_dirty(struct ubifs_info *c, int lnum, int offs) | |
1012 | { | |
1013 | if (lnum != c->lsave_lnum || offs != c->lsave_offs) | |
1014 | return 0; /* This lsave node is obsolete */ | |
1015 | if (!(c->lpt_drty_flgs & LSAVE_DIRTY)) { | |
1016 | c->lpt_drty_flgs |= LSAVE_DIRTY; | |
1017 | ubifs_add_lpt_dirt(c, c->lsave_lnum, c->lsave_sz); | |
1018 | } | |
1019 | return 0; | |
1020 | } | |
1021 | ||
1022 | /** | |
1023 | * make_node_dirty - make node dirty. | |
1024 | * @c: UBIFS file-system description object | |
1025 | * @node_type: LPT node type | |
1026 | * @node_num: node number | |
1027 | * @lnum: LEB number where node was written | |
1028 | * @offs: offset where node was written | |
1029 | * | |
1030 | * This function is used by LPT garbage collection. LPT garbage collection is | |
1031 | * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection | |
1032 | * simply involves marking all the nodes in the LEB being garbage-collected as | |
1033 | * dirty. The dirty nodes are written next commit, after which the LEB is free | |
1034 | * to be reused. | |
1035 | * | |
1036 | * This function returns %0 on success and a negative error code on failure. | |
1037 | */ | |
1038 | static int make_node_dirty(struct ubifs_info *c, int node_type, int node_num, | |
1039 | int lnum, int offs) | |
1040 | { | |
1041 | switch (node_type) { | |
1042 | case UBIFS_LPT_NNODE: | |
1043 | return make_nnode_dirty(c, node_num, lnum, offs); | |
1044 | case UBIFS_LPT_PNODE: | |
1045 | return make_pnode_dirty(c, node_num, lnum, offs); | |
1046 | case UBIFS_LPT_LTAB: | |
1047 | return make_ltab_dirty(c, lnum, offs); | |
1048 | case UBIFS_LPT_LSAVE: | |
1049 | return make_lsave_dirty(c, lnum, offs); | |
1050 | } | |
1051 | return -EINVAL; | |
1052 | } | |
1053 | ||
1054 | /** | |
1055 | * get_lpt_node_len - return the length of a node based on its type. | |
1056 | * @c: UBIFS file-system description object | |
1057 | * @node_type: LPT node type | |
1058 | */ | |
2ba5f7ae | 1059 | static int get_lpt_node_len(const struct ubifs_info *c, int node_type) |
1e51764a AB |
1060 | { |
1061 | switch (node_type) { | |
1062 | case UBIFS_LPT_NNODE: | |
1063 | return c->nnode_sz; | |
1064 | case UBIFS_LPT_PNODE: | |
1065 | return c->pnode_sz; | |
1066 | case UBIFS_LPT_LTAB: | |
1067 | return c->ltab_sz; | |
1068 | case UBIFS_LPT_LSAVE: | |
1069 | return c->lsave_sz; | |
1070 | } | |
1071 | return 0; | |
1072 | } | |
1073 | ||
1074 | /** | |
1075 | * get_pad_len - return the length of padding in a buffer. | |
1076 | * @c: UBIFS file-system description object | |
1077 | * @buf: buffer | |
1078 | * @len: length of buffer | |
1079 | */ | |
2ba5f7ae | 1080 | static int get_pad_len(const struct ubifs_info *c, uint8_t *buf, int len) |
1e51764a AB |
1081 | { |
1082 | int offs, pad_len; | |
1083 | ||
1084 | if (c->min_io_size == 1) | |
1085 | return 0; | |
1086 | offs = c->leb_size - len; | |
1087 | pad_len = ALIGN(offs, c->min_io_size) - offs; | |
1088 | return pad_len; | |
1089 | } | |
1090 | ||
1091 | /** | |
1092 | * get_lpt_node_type - return type (and node number) of a node in a buffer. | |
1093 | * @c: UBIFS file-system description object | |
1094 | * @buf: buffer | |
1095 | * @node_num: node number is returned here | |
1096 | */ | |
2ba5f7ae AB |
1097 | static int get_lpt_node_type(const struct ubifs_info *c, uint8_t *buf, |
1098 | int *node_num) | |
1e51764a AB |
1099 | { |
1100 | uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES; | |
1101 | int pos = 0, node_type; | |
1102 | ||
1103 | node_type = ubifs_unpack_bits(&addr, &pos, UBIFS_LPT_TYPE_BITS); | |
1104 | *node_num = ubifs_unpack_bits(&addr, &pos, c->pcnt_bits); | |
1105 | return node_type; | |
1106 | } | |
1107 | ||
1108 | /** | |
1109 | * is_a_node - determine if a buffer contains a node. | |
1110 | * @c: UBIFS file-system description object | |
1111 | * @buf: buffer | |
1112 | * @len: length of buffer | |
1113 | * | |
1114 | * This function returns %1 if the buffer contains a node or %0 if it does not. | |
1115 | */ | |
2ba5f7ae | 1116 | static int is_a_node(const struct ubifs_info *c, uint8_t *buf, int len) |
1e51764a AB |
1117 | { |
1118 | uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES; | |
1119 | int pos = 0, node_type, node_len; | |
1120 | uint16_t crc, calc_crc; | |
1121 | ||
be2f6bd6 AH |
1122 | if (len < UBIFS_LPT_CRC_BYTES + (UBIFS_LPT_TYPE_BITS + 7) / 8) |
1123 | return 0; | |
1e51764a AB |
1124 | node_type = ubifs_unpack_bits(&addr, &pos, UBIFS_LPT_TYPE_BITS); |
1125 | if (node_type == UBIFS_LPT_NOT_A_NODE) | |
1126 | return 0; | |
1127 | node_len = get_lpt_node_len(c, node_type); | |
1128 | if (!node_len || node_len > len) | |
1129 | return 0; | |
1130 | pos = 0; | |
1131 | addr = buf; | |
1132 | crc = ubifs_unpack_bits(&addr, &pos, UBIFS_LPT_CRC_BITS); | |
1133 | calc_crc = crc16(-1, buf + UBIFS_LPT_CRC_BYTES, | |
1134 | node_len - UBIFS_LPT_CRC_BYTES); | |
1135 | if (crc != calc_crc) | |
1136 | return 0; | |
1137 | return 1; | |
1138 | } | |
1139 | ||
1e51764a AB |
1140 | /** |
1141 | * lpt_gc_lnum - garbage collect a LPT LEB. | |
1142 | * @c: UBIFS file-system description object | |
1143 | * @lnum: LEB number to garbage collect | |
1144 | * | |
1145 | * LPT garbage collection is used only for the "big" LPT model | |
1146 | * (c->big_lpt == 1). Garbage collection simply involves marking all the nodes | |
1147 | * in the LEB being garbage-collected as dirty. The dirty nodes are written | |
1148 | * next commit, after which the LEB is free to be reused. | |
1149 | * | |
1150 | * This function returns %0 on success and a negative error code on failure. | |
1151 | */ | |
1152 | static int lpt_gc_lnum(struct ubifs_info *c, int lnum) | |
1153 | { | |
1154 | int err, len = c->leb_size, node_type, node_num, node_len, offs; | |
1155 | void *buf = c->lpt_buf; | |
1156 | ||
1157 | dbg_lp("LEB %d", lnum); | |
d304820a AB |
1158 | |
1159 | err = ubifs_leb_read(c, lnum, buf, 0, c->leb_size, 1); | |
1160 | if (err) | |
1e51764a | 1161 | return err; |
d304820a | 1162 | |
1e51764a AB |
1163 | while (1) { |
1164 | if (!is_a_node(c, buf, len)) { | |
1165 | int pad_len; | |
1166 | ||
1167 | pad_len = get_pad_len(c, buf, len); | |
1168 | if (pad_len) { | |
1169 | buf += pad_len; | |
1170 | len -= pad_len; | |
1171 | continue; | |
1172 | } | |
1173 | return 0; | |
1174 | } | |
1175 | node_type = get_lpt_node_type(c, buf, &node_num); | |
1176 | node_len = get_lpt_node_len(c, node_type); | |
1177 | offs = c->leb_size - len; | |
1178 | ubifs_assert(node_len != 0); | |
1179 | mutex_lock(&c->lp_mutex); | |
1180 | err = make_node_dirty(c, node_type, node_num, lnum, offs); | |
1181 | mutex_unlock(&c->lp_mutex); | |
1182 | if (err) | |
1183 | return err; | |
1184 | buf += node_len; | |
1185 | len -= node_len; | |
1186 | } | |
1187 | return 0; | |
1188 | } | |
1189 | ||
1190 | /** | |
1191 | * lpt_gc - LPT garbage collection. | |
1192 | * @c: UBIFS file-system description object | |
1193 | * | |
1194 | * Select a LPT LEB for LPT garbage collection and call 'lpt_gc_lnum()'. | |
1195 | * Returns %0 on success and a negative error code on failure. | |
1196 | */ | |
1197 | static int lpt_gc(struct ubifs_info *c) | |
1198 | { | |
1199 | int i, lnum = -1, dirty = 0; | |
1200 | ||
1201 | mutex_lock(&c->lp_mutex); | |
1202 | for (i = 0; i < c->lpt_lebs; i++) { | |
1203 | ubifs_assert(!c->ltab[i].tgc); | |
1204 | if (i + c->lpt_first == c->nhead_lnum || | |
1205 | c->ltab[i].free + c->ltab[i].dirty == c->leb_size) | |
1206 | continue; | |
1207 | if (c->ltab[i].dirty > dirty) { | |
1208 | dirty = c->ltab[i].dirty; | |
1209 | lnum = i + c->lpt_first; | |
1210 | } | |
1211 | } | |
1212 | mutex_unlock(&c->lp_mutex); | |
1213 | if (lnum == -1) | |
1214 | return -ENOSPC; | |
1215 | return lpt_gc_lnum(c, lnum); | |
1216 | } | |
1217 | ||
1218 | /** | |
1219 | * ubifs_lpt_start_commit - UBIFS commit starts. | |
1220 | * @c: the UBIFS file-system description object | |
1221 | * | |
1222 | * This function has to be called when UBIFS starts the commit operation. | |
1223 | * This function "freezes" all currently dirty LEB properties and does not | |
1224 | * change them anymore. Further changes are saved and tracked separately | |
1225 | * because they are not part of this commit. This function returns zero in case | |
1226 | * of success and a negative error code in case of failure. | |
1227 | */ | |
1228 | int ubifs_lpt_start_commit(struct ubifs_info *c) | |
1229 | { | |
1230 | int err, cnt; | |
1231 | ||
1232 | dbg_lp(""); | |
1233 | ||
1234 | mutex_lock(&c->lp_mutex); | |
73944a6d AH |
1235 | err = dbg_chk_lpt_free_spc(c); |
1236 | if (err) | |
1237 | goto out; | |
1e51764a AB |
1238 | err = dbg_check_ltab(c); |
1239 | if (err) | |
1240 | goto out; | |
1241 | ||
1242 | if (c->check_lpt_free) { | |
1243 | /* | |
1244 | * We ensure there is enough free space in | |
1245 | * ubifs_lpt_post_commit() by marking nodes dirty. That | |
1246 | * information is lost when we unmount, so we also need | |
1247 | * to check free space once after mounting also. | |
1248 | */ | |
1249 | c->check_lpt_free = 0; | |
1250 | while (need_write_all(c)) { | |
1251 | mutex_unlock(&c->lp_mutex); | |
1252 | err = lpt_gc(c); | |
1253 | if (err) | |
1254 | return err; | |
1255 | mutex_lock(&c->lp_mutex); | |
1256 | } | |
1257 | } | |
1258 | ||
1259 | lpt_tgc_start(c); | |
1260 | ||
1261 | if (!c->dirty_pn_cnt) { | |
1262 | dbg_cmt("no cnodes to commit"); | |
1263 | err = 0; | |
1264 | goto out; | |
1265 | } | |
1266 | ||
1267 | if (!c->big_lpt && need_write_all(c)) { | |
1268 | /* If needed, write everything */ | |
1269 | err = make_tree_dirty(c); | |
1270 | if (err) | |
1271 | goto out; | |
1272 | lpt_tgc_start(c); | |
1273 | } | |
1274 | ||
1275 | if (c->big_lpt) | |
1276 | populate_lsave(c); | |
1277 | ||
1278 | cnt = get_cnodes_to_commit(c); | |
1279 | ubifs_assert(cnt != 0); | |
1280 | ||
1281 | err = layout_cnodes(c); | |
1282 | if (err) | |
1283 | goto out; | |
1284 | ||
1285 | /* Copy the LPT's own lprops for end commit to write */ | |
1286 | memcpy(c->ltab_cmt, c->ltab, | |
1287 | sizeof(struct ubifs_lpt_lprops) * c->lpt_lebs); | |
1288 | c->lpt_drty_flgs &= ~(LTAB_DIRTY | LSAVE_DIRTY); | |
1289 | ||
1290 | out: | |
1291 | mutex_unlock(&c->lp_mutex); | |
1292 | return err; | |
1293 | } | |
1294 | ||
1295 | /** | |
1296 | * free_obsolete_cnodes - free obsolete cnodes for commit end. | |
1297 | * @c: UBIFS file-system description object | |
1298 | */ | |
1299 | static void free_obsolete_cnodes(struct ubifs_info *c) | |
1300 | { | |
1301 | struct ubifs_cnode *cnode, *cnext; | |
1302 | ||
1303 | cnext = c->lpt_cnext; | |
1304 | if (!cnext) | |
1305 | return; | |
1306 | do { | |
1307 | cnode = cnext; | |
1308 | cnext = cnode->cnext; | |
1309 | if (test_bit(OBSOLETE_CNODE, &cnode->flags)) | |
1310 | kfree(cnode); | |
1311 | else | |
1312 | cnode->cnext = NULL; | |
1313 | } while (cnext != c->lpt_cnext); | |
1314 | c->lpt_cnext = NULL; | |
1315 | } | |
1316 | ||
1317 | /** | |
1318 | * ubifs_lpt_end_commit - finish the commit operation. | |
1319 | * @c: the UBIFS file-system description object | |
1320 | * | |
1321 | * This function has to be called when the commit operation finishes. It | |
1322 | * flushes the changes which were "frozen" by 'ubifs_lprops_start_commit()' to | |
1323 | * the media. Returns zero in case of success and a negative error code in case | |
1324 | * of failure. | |
1325 | */ | |
1326 | int ubifs_lpt_end_commit(struct ubifs_info *c) | |
1327 | { | |
1328 | int err; | |
1329 | ||
1330 | dbg_lp(""); | |
1331 | ||
1332 | if (!c->lpt_cnext) | |
1333 | return 0; | |
1334 | ||
1335 | err = write_cnodes(c); | |
1336 | if (err) | |
1337 | return err; | |
1338 | ||
1339 | mutex_lock(&c->lp_mutex); | |
1340 | free_obsolete_cnodes(c); | |
1341 | mutex_unlock(&c->lp_mutex); | |
1342 | ||
1343 | return 0; | |
1344 | } | |
1345 | ||
1346 | /** | |
1347 | * ubifs_lpt_post_commit - post commit LPT trivial GC and LPT GC. | |
1348 | * @c: UBIFS file-system description object | |
1349 | * | |
1350 | * LPT trivial GC is completed after a commit. Also LPT GC is done after a | |
1351 | * commit for the "big" LPT model. | |
1352 | */ | |
1353 | int ubifs_lpt_post_commit(struct ubifs_info *c) | |
1354 | { | |
1355 | int err; | |
1356 | ||
1357 | mutex_lock(&c->lp_mutex); | |
1358 | err = lpt_tgc_end(c); | |
1359 | if (err) | |
1360 | goto out; | |
1361 | if (c->big_lpt) | |
1362 | while (need_write_all(c)) { | |
1363 | mutex_unlock(&c->lp_mutex); | |
1364 | err = lpt_gc(c); | |
1365 | if (err) | |
1366 | return err; | |
1367 | mutex_lock(&c->lp_mutex); | |
1368 | } | |
1369 | out: | |
1370 | mutex_unlock(&c->lp_mutex); | |
1371 | return err; | |
1372 | } | |
1373 | ||
1374 | /** | |
1375 | * first_nnode - find the first nnode in memory. | |
1376 | * @c: UBIFS file-system description object | |
1377 | * @hght: height of tree where nnode found is returned here | |
1378 | * | |
1379 | * This function returns a pointer to the nnode found or %NULL if no nnode is | |
1380 | * found. This function is a helper to 'ubifs_lpt_free()'. | |
1381 | */ | |
1382 | static struct ubifs_nnode *first_nnode(struct ubifs_info *c, int *hght) | |
1383 | { | |
1384 | struct ubifs_nnode *nnode; | |
1385 | int h, i, found; | |
1386 | ||
1387 | nnode = c->nroot; | |
1388 | *hght = 0; | |
1389 | if (!nnode) | |
1390 | return NULL; | |
1391 | for (h = 1; h < c->lpt_hght; h++) { | |
1392 | found = 0; | |
1393 | for (i = 0; i < UBIFS_LPT_FANOUT; i++) { | |
1394 | if (nnode->nbranch[i].nnode) { | |
1395 | found = 1; | |
1396 | nnode = nnode->nbranch[i].nnode; | |
1397 | *hght = h; | |
1398 | break; | |
1399 | } | |
1400 | } | |
1401 | if (!found) | |
1402 | break; | |
1403 | } | |
1404 | return nnode; | |
1405 | } | |
1406 | ||
1407 | /** | |
1408 | * next_nnode - find the next nnode in memory. | |
1409 | * @c: UBIFS file-system description object | |
1410 | * @nnode: nnode from which to start. | |
1411 | * @hght: height of tree where nnode is, is passed and returned here | |
1412 | * | |
1413 | * This function returns a pointer to the nnode found or %NULL if no nnode is | |
1414 | * found. This function is a helper to 'ubifs_lpt_free()'. | |
1415 | */ | |
1416 | static struct ubifs_nnode *next_nnode(struct ubifs_info *c, | |
1417 | struct ubifs_nnode *nnode, int *hght) | |
1418 | { | |
1419 | struct ubifs_nnode *parent; | |
1420 | int iip, h, i, found; | |
1421 | ||
1422 | parent = nnode->parent; | |
1423 | if (!parent) | |
1424 | return NULL; | |
1425 | if (nnode->iip == UBIFS_LPT_FANOUT - 1) { | |
1426 | *hght -= 1; | |
1427 | return parent; | |
1428 | } | |
1429 | for (iip = nnode->iip + 1; iip < UBIFS_LPT_FANOUT; iip++) { | |
1430 | nnode = parent->nbranch[iip].nnode; | |
1431 | if (nnode) | |
1432 | break; | |
1433 | } | |
1434 | if (!nnode) { | |
1435 | *hght -= 1; | |
1436 | return parent; | |
1437 | } | |
1438 | for (h = *hght + 1; h < c->lpt_hght; h++) { | |
1439 | found = 0; | |
1440 | for (i = 0; i < UBIFS_LPT_FANOUT; i++) { | |
1441 | if (nnode->nbranch[i].nnode) { | |
1442 | found = 1; | |
1443 | nnode = nnode->nbranch[i].nnode; | |
1444 | *hght = h; | |
1445 | break; | |
1446 | } | |
1447 | } | |
1448 | if (!found) | |
1449 | break; | |
1450 | } | |
1451 | return nnode; | |
1452 | } | |
1453 | ||
1454 | /** | |
1455 | * ubifs_lpt_free - free resources owned by the LPT. | |
1456 | * @c: UBIFS file-system description object | |
1457 | * @wr_only: free only resources used for writing | |
1458 | */ | |
1459 | void ubifs_lpt_free(struct ubifs_info *c, int wr_only) | |
1460 | { | |
1461 | struct ubifs_nnode *nnode; | |
1462 | int i, hght; | |
1463 | ||
1464 | /* Free write-only things first */ | |
1465 | ||
1466 | free_obsolete_cnodes(c); /* Leftover from a failed commit */ | |
1467 | ||
1468 | vfree(c->ltab_cmt); | |
1469 | c->ltab_cmt = NULL; | |
1470 | vfree(c->lpt_buf); | |
1471 | c->lpt_buf = NULL; | |
1472 | kfree(c->lsave); | |
1473 | c->lsave = NULL; | |
1474 | ||
1475 | if (wr_only) | |
1476 | return; | |
1477 | ||
1478 | /* Now free the rest */ | |
1479 | ||
1480 | nnode = first_nnode(c, &hght); | |
1481 | while (nnode) { | |
1482 | for (i = 0; i < UBIFS_LPT_FANOUT; i++) | |
1483 | kfree(nnode->nbranch[i].nnode); | |
1484 | nnode = next_nnode(c, nnode, &hght); | |
1485 | } | |
1486 | for (i = 0; i < LPROPS_HEAP_CNT; i++) | |
1487 | kfree(c->lpt_heap[i].arr); | |
1488 | kfree(c->dirty_idx.arr); | |
1489 | kfree(c->nroot); | |
1490 | vfree(c->ltab); | |
1491 | kfree(c->lpt_nod_buf); | |
1492 | } | |
1493 | ||
f70b7e52 AB |
1494 | /* |
1495 | * Everything below is related to debugging. | |
1496 | */ | |
1e51764a AB |
1497 | |
1498 | /** | |
80736d41 | 1499 | * dbg_is_all_ff - determine if a buffer contains only 0xFF bytes. |
1e51764a AB |
1500 | * @buf: buffer |
1501 | * @len: buffer length | |
1502 | */ | |
1503 | static int dbg_is_all_ff(uint8_t *buf, int len) | |
1504 | { | |
1505 | int i; | |
1506 | ||
1507 | for (i = 0; i < len; i++) | |
1508 | if (buf[i] != 0xff) | |
1509 | return 0; | |
1510 | return 1; | |
1511 | } | |
1512 | ||
1513 | /** | |
1514 | * dbg_is_nnode_dirty - determine if a nnode is dirty. | |
1515 | * @c: the UBIFS file-system description object | |
1516 | * @lnum: LEB number where nnode was written | |
1517 | * @offs: offset where nnode was written | |
1518 | */ | |
1519 | static int dbg_is_nnode_dirty(struct ubifs_info *c, int lnum, int offs) | |
1520 | { | |
1521 | struct ubifs_nnode *nnode; | |
1522 | int hght; | |
1523 | ||
80736d41 | 1524 | /* Entire tree is in memory so first_nnode / next_nnode are OK */ |
1e51764a AB |
1525 | nnode = first_nnode(c, &hght); |
1526 | for (; nnode; nnode = next_nnode(c, nnode, &hght)) { | |
1527 | struct ubifs_nbranch *branch; | |
1528 | ||
1529 | cond_resched(); | |
1530 | if (nnode->parent) { | |
1531 | branch = &nnode->parent->nbranch[nnode->iip]; | |
1532 | if (branch->lnum != lnum || branch->offs != offs) | |
1533 | continue; | |
1534 | if (test_bit(DIRTY_CNODE, &nnode->flags)) | |
1535 | return 1; | |
1536 | return 0; | |
1537 | } else { | |
1538 | if (c->lpt_lnum != lnum || c->lpt_offs != offs) | |
1539 | continue; | |
1540 | if (test_bit(DIRTY_CNODE, &nnode->flags)) | |
1541 | return 1; | |
1542 | return 0; | |
1543 | } | |
1544 | } | |
1545 | return 1; | |
1546 | } | |
1547 | ||
1548 | /** | |
1549 | * dbg_is_pnode_dirty - determine if a pnode is dirty. | |
1550 | * @c: the UBIFS file-system description object | |
1551 | * @lnum: LEB number where pnode was written | |
1552 | * @offs: offset where pnode was written | |
1553 | */ | |
1554 | static int dbg_is_pnode_dirty(struct ubifs_info *c, int lnum, int offs) | |
1555 | { | |
1556 | int i, cnt; | |
1557 | ||
1558 | cnt = DIV_ROUND_UP(c->main_lebs, UBIFS_LPT_FANOUT); | |
1559 | for (i = 0; i < cnt; i++) { | |
1560 | struct ubifs_pnode *pnode; | |
1561 | struct ubifs_nbranch *branch; | |
1562 | ||
1563 | cond_resched(); | |
1564 | pnode = pnode_lookup(c, i); | |
1565 | if (IS_ERR(pnode)) | |
1566 | return PTR_ERR(pnode); | |
1567 | branch = &pnode->parent->nbranch[pnode->iip]; | |
1568 | if (branch->lnum != lnum || branch->offs != offs) | |
1569 | continue; | |
1570 | if (test_bit(DIRTY_CNODE, &pnode->flags)) | |
1571 | return 1; | |
1572 | return 0; | |
1573 | } | |
1574 | return 1; | |
1575 | } | |
1576 | ||
1577 | /** | |
1578 | * dbg_is_ltab_dirty - determine if a ltab node is dirty. | |
1579 | * @c: the UBIFS file-system description object | |
1580 | * @lnum: LEB number where ltab node was written | |
1581 | * @offs: offset where ltab node was written | |
1582 | */ | |
1583 | static int dbg_is_ltab_dirty(struct ubifs_info *c, int lnum, int offs) | |
1584 | { | |
1585 | if (lnum != c->ltab_lnum || offs != c->ltab_offs) | |
1586 | return 1; | |
1587 | return (c->lpt_drty_flgs & LTAB_DIRTY) != 0; | |
1588 | } | |
1589 | ||
1590 | /** | |
1591 | * dbg_is_lsave_dirty - determine if a lsave node is dirty. | |
1592 | * @c: the UBIFS file-system description object | |
1593 | * @lnum: LEB number where lsave node was written | |
1594 | * @offs: offset where lsave node was written | |
1595 | */ | |
1596 | static int dbg_is_lsave_dirty(struct ubifs_info *c, int lnum, int offs) | |
1597 | { | |
1598 | if (lnum != c->lsave_lnum || offs != c->lsave_offs) | |
1599 | return 1; | |
1600 | return (c->lpt_drty_flgs & LSAVE_DIRTY) != 0; | |
1601 | } | |
1602 | ||
1603 | /** | |
1604 | * dbg_is_node_dirty - determine if a node is dirty. | |
1605 | * @c: the UBIFS file-system description object | |
1606 | * @node_type: node type | |
1607 | * @lnum: LEB number where node was written | |
1608 | * @offs: offset where node was written | |
1609 | */ | |
1610 | static int dbg_is_node_dirty(struct ubifs_info *c, int node_type, int lnum, | |
1611 | int offs) | |
1612 | { | |
1613 | switch (node_type) { | |
1614 | case UBIFS_LPT_NNODE: | |
1615 | return dbg_is_nnode_dirty(c, lnum, offs); | |
1616 | case UBIFS_LPT_PNODE: | |
1617 | return dbg_is_pnode_dirty(c, lnum, offs); | |
1618 | case UBIFS_LPT_LTAB: | |
1619 | return dbg_is_ltab_dirty(c, lnum, offs); | |
1620 | case UBIFS_LPT_LSAVE: | |
1621 | return dbg_is_lsave_dirty(c, lnum, offs); | |
1622 | } | |
1623 | return 1; | |
1624 | } | |
1625 | ||
1626 | /** | |
1627 | * dbg_check_ltab_lnum - check the ltab for a LPT LEB number. | |
1628 | * @c: the UBIFS file-system description object | |
1629 | * @lnum: LEB number where node was written | |
1630 | * @offs: offset where node was written | |
1631 | * | |
1632 | * This function returns %0 on success and a negative error code on failure. | |
1633 | */ | |
1634 | static int dbg_check_ltab_lnum(struct ubifs_info *c, int lnum) | |
1635 | { | |
1636 | int err, len = c->leb_size, dirty = 0, node_type, node_num, node_len; | |
1637 | int ret; | |
6fb324a4 | 1638 | void *buf, *p; |
1e51764a | 1639 | |
2b1844a8 | 1640 | if (!dbg_is_chk_lprops(c)) |
45e12d90 AB |
1641 | return 0; |
1642 | ||
fc5e58c0 | 1643 | buf = p = __vmalloc(c->leb_size, GFP_NOFS, PAGE_KERNEL); |
6fb324a4 AB |
1644 | if (!buf) { |
1645 | ubifs_err("cannot allocate memory for ltab checking"); | |
1646 | return 0; | |
1647 | } | |
1648 | ||
1e51764a | 1649 | dbg_lp("LEB %d", lnum); |
d304820a AB |
1650 | |
1651 | err = ubifs_leb_read(c, lnum, buf, 0, c->leb_size, 1); | |
1652 | if (err) | |
6fb324a4 | 1653 | goto out; |
d304820a | 1654 | |
1e51764a | 1655 | while (1) { |
6fb324a4 | 1656 | if (!is_a_node(c, p, len)) { |
1e51764a AB |
1657 | int i, pad_len; |
1658 | ||
6fb324a4 | 1659 | pad_len = get_pad_len(c, p, len); |
1e51764a | 1660 | if (pad_len) { |
6fb324a4 | 1661 | p += pad_len; |
1e51764a AB |
1662 | len -= pad_len; |
1663 | dirty += pad_len; | |
1664 | continue; | |
1665 | } | |
6fb324a4 | 1666 | if (!dbg_is_all_ff(p, len)) { |
1e51764a AB |
1667 | dbg_msg("invalid empty space in LEB %d at %d", |
1668 | lnum, c->leb_size - len); | |
1669 | err = -EINVAL; | |
1670 | } | |
1671 | i = lnum - c->lpt_first; | |
1672 | if (len != c->ltab[i].free) { | |
1673 | dbg_msg("invalid free space in LEB %d " | |
1674 | "(free %d, expected %d)", | |
1675 | lnum, len, c->ltab[i].free); | |
1676 | err = -EINVAL; | |
1677 | } | |
1678 | if (dirty != c->ltab[i].dirty) { | |
1679 | dbg_msg("invalid dirty space in LEB %d " | |
1680 | "(dirty %d, expected %d)", | |
1681 | lnum, dirty, c->ltab[i].dirty); | |
1682 | err = -EINVAL; | |
1683 | } | |
6fb324a4 | 1684 | goto out; |
1e51764a | 1685 | } |
6fb324a4 | 1686 | node_type = get_lpt_node_type(c, p, &node_num); |
1e51764a AB |
1687 | node_len = get_lpt_node_len(c, node_type); |
1688 | ret = dbg_is_node_dirty(c, node_type, lnum, c->leb_size - len); | |
1689 | if (ret == 1) | |
1690 | dirty += node_len; | |
6fb324a4 | 1691 | p += node_len; |
1e51764a AB |
1692 | len -= node_len; |
1693 | } | |
6fb324a4 AB |
1694 | |
1695 | err = 0; | |
1696 | out: | |
1697 | vfree(buf); | |
1698 | return err; | |
1e51764a AB |
1699 | } |
1700 | ||
1701 | /** | |
1702 | * dbg_check_ltab - check the free and dirty space in the ltab. | |
1703 | * @c: the UBIFS file-system description object | |
1704 | * | |
1705 | * This function returns %0 on success and a negative error code on failure. | |
1706 | */ | |
1707 | int dbg_check_ltab(struct ubifs_info *c) | |
1708 | { | |
1709 | int lnum, err, i, cnt; | |
1710 | ||
2b1844a8 | 1711 | if (!dbg_is_chk_lprops(c)) |
1e51764a AB |
1712 | return 0; |
1713 | ||
1714 | /* Bring the entire tree into memory */ | |
1715 | cnt = DIV_ROUND_UP(c->main_lebs, UBIFS_LPT_FANOUT); | |
1716 | for (i = 0; i < cnt; i++) { | |
1717 | struct ubifs_pnode *pnode; | |
1718 | ||
1719 | pnode = pnode_lookup(c, i); | |
1720 | if (IS_ERR(pnode)) | |
1721 | return PTR_ERR(pnode); | |
1722 | cond_resched(); | |
1723 | } | |
1724 | ||
1725 | /* Check nodes */ | |
1726 | err = dbg_check_lpt_nodes(c, (struct ubifs_cnode *)c->nroot, 0, 0); | |
1727 | if (err) | |
1728 | return err; | |
1729 | ||
1730 | /* Check each LEB */ | |
1731 | for (lnum = c->lpt_first; lnum <= c->lpt_last; lnum++) { | |
1732 | err = dbg_check_ltab_lnum(c, lnum); | |
1733 | if (err) { | |
a6aae4dd | 1734 | ubifs_err("failed at LEB %d", lnum); |
1e51764a AB |
1735 | return err; |
1736 | } | |
1737 | } | |
1738 | ||
1739 | dbg_lp("succeeded"); | |
1740 | return 0; | |
1741 | } | |
1742 | ||
73944a6d AH |
1743 | /** |
1744 | * dbg_chk_lpt_free_spc - check LPT free space is enough to write entire LPT. | |
1745 | * @c: the UBIFS file-system description object | |
1746 | * | |
1747 | * This function returns %0 on success and a negative error code on failure. | |
1748 | */ | |
1749 | int dbg_chk_lpt_free_spc(struct ubifs_info *c) | |
1750 | { | |
1751 | long long free = 0; | |
1752 | int i; | |
1753 | ||
2b1844a8 | 1754 | if (!dbg_is_chk_lprops(c)) |
45e12d90 AB |
1755 | return 0; |
1756 | ||
73944a6d AH |
1757 | for (i = 0; i < c->lpt_lebs; i++) { |
1758 | if (c->ltab[i].tgc || c->ltab[i].cmt) | |
1759 | continue; | |
1760 | if (i + c->lpt_first == c->nhead_lnum) | |
1761 | free += c->leb_size - c->nhead_offs; | |
1762 | else if (c->ltab[i].free == c->leb_size) | |
1763 | free += c->leb_size; | |
1764 | } | |
1765 | if (free < c->lpt_sz) { | |
a6aae4dd AB |
1766 | ubifs_err("LPT space error: free %lld lpt_sz %lld", |
1767 | free, c->lpt_sz); | |
edf6be24 AB |
1768 | ubifs_dump_lpt_info(c); |
1769 | ubifs_dump_lpt_lebs(c); | |
787845bd | 1770 | dump_stack(); |
73944a6d AH |
1771 | return -EINVAL; |
1772 | } | |
1773 | return 0; | |
1774 | } | |
1775 | ||
1776 | /** | |
1777 | * dbg_chk_lpt_sz - check LPT does not write more than LPT size. | |
1778 | * @c: the UBIFS file-system description object | |
2bc275e9 | 1779 | * @action: what to do |
73944a6d AH |
1780 | * @len: length written |
1781 | * | |
1782 | * This function returns %0 on success and a negative error code on failure. | |
2bc275e9 AH |
1783 | * The @action argument may be one of: |
1784 | * o %0 - LPT debugging checking starts, initialize debugging variables; | |
1785 | * o %1 - wrote an LPT node, increase LPT size by @len bytes; | |
1786 | * o %2 - switched to a different LEB and wasted @len bytes; | |
1787 | * o %3 - check that we've written the right number of bytes. | |
1788 | * o %4 - wasted @len bytes; | |
73944a6d AH |
1789 | */ |
1790 | int dbg_chk_lpt_sz(struct ubifs_info *c, int action, int len) | |
1791 | { | |
17c2f9f8 | 1792 | struct ubifs_debug_info *d = c->dbg; |
73944a6d AH |
1793 | long long chk_lpt_sz, lpt_sz; |
1794 | int err = 0; | |
1795 | ||
2b1844a8 | 1796 | if (!dbg_is_chk_lprops(c)) |
45e12d90 AB |
1797 | return 0; |
1798 | ||
73944a6d AH |
1799 | switch (action) { |
1800 | case 0: | |
17c2f9f8 AB |
1801 | d->chk_lpt_sz = 0; |
1802 | d->chk_lpt_sz2 = 0; | |
1803 | d->chk_lpt_lebs = 0; | |
1804 | d->chk_lpt_wastage = 0; | |
73944a6d | 1805 | if (c->dirty_pn_cnt > c->pnode_cnt) { |
a6aae4dd AB |
1806 | ubifs_err("dirty pnodes %d exceed max %d", |
1807 | c->dirty_pn_cnt, c->pnode_cnt); | |
73944a6d AH |
1808 | err = -EINVAL; |
1809 | } | |
1810 | if (c->dirty_nn_cnt > c->nnode_cnt) { | |
a6aae4dd AB |
1811 | ubifs_err("dirty nnodes %d exceed max %d", |
1812 | c->dirty_nn_cnt, c->nnode_cnt); | |
73944a6d AH |
1813 | err = -EINVAL; |
1814 | } | |
1815 | return err; | |
1816 | case 1: | |
17c2f9f8 | 1817 | d->chk_lpt_sz += len; |
73944a6d AH |
1818 | return 0; |
1819 | case 2: | |
17c2f9f8 AB |
1820 | d->chk_lpt_sz += len; |
1821 | d->chk_lpt_wastage += len; | |
1822 | d->chk_lpt_lebs += 1; | |
73944a6d AH |
1823 | return 0; |
1824 | case 3: | |
1825 | chk_lpt_sz = c->leb_size; | |
17c2f9f8 | 1826 | chk_lpt_sz *= d->chk_lpt_lebs; |
73944a6d | 1827 | chk_lpt_sz += len - c->nhead_offs; |
17c2f9f8 | 1828 | if (d->chk_lpt_sz != chk_lpt_sz) { |
a6aae4dd AB |
1829 | ubifs_err("LPT wrote %lld but space used was %lld", |
1830 | d->chk_lpt_sz, chk_lpt_sz); | |
73944a6d AH |
1831 | err = -EINVAL; |
1832 | } | |
17c2f9f8 | 1833 | if (d->chk_lpt_sz > c->lpt_sz) { |
a6aae4dd AB |
1834 | ubifs_err("LPT wrote %lld but lpt_sz is %lld", |
1835 | d->chk_lpt_sz, c->lpt_sz); | |
73944a6d AH |
1836 | err = -EINVAL; |
1837 | } | |
17c2f9f8 | 1838 | if (d->chk_lpt_sz2 && d->chk_lpt_sz != d->chk_lpt_sz2) { |
a6aae4dd AB |
1839 | ubifs_err("LPT layout size %lld but wrote %lld", |
1840 | d->chk_lpt_sz, d->chk_lpt_sz2); | |
73944a6d AH |
1841 | err = -EINVAL; |
1842 | } | |
17c2f9f8 | 1843 | if (d->chk_lpt_sz2 && d->new_nhead_offs != len) { |
a6aae4dd AB |
1844 | ubifs_err("LPT new nhead offs: expected %d was %d", |
1845 | d->new_nhead_offs, len); | |
73944a6d AH |
1846 | err = -EINVAL; |
1847 | } | |
1848 | lpt_sz = (long long)c->pnode_cnt * c->pnode_sz; | |
1849 | lpt_sz += (long long)c->nnode_cnt * c->nnode_sz; | |
1850 | lpt_sz += c->ltab_sz; | |
1851 | if (c->big_lpt) | |
1852 | lpt_sz += c->lsave_sz; | |
17c2f9f8 | 1853 | if (d->chk_lpt_sz - d->chk_lpt_wastage > lpt_sz) { |
a6aae4dd AB |
1854 | ubifs_err("LPT chk_lpt_sz %lld + waste %lld exceeds %lld", |
1855 | d->chk_lpt_sz, d->chk_lpt_wastage, lpt_sz); | |
73944a6d AH |
1856 | err = -EINVAL; |
1857 | } | |
787845bd | 1858 | if (err) { |
edf6be24 AB |
1859 | ubifs_dump_lpt_info(c); |
1860 | ubifs_dump_lpt_lebs(c); | |
787845bd AB |
1861 | dump_stack(); |
1862 | } | |
17c2f9f8 AB |
1863 | d->chk_lpt_sz2 = d->chk_lpt_sz; |
1864 | d->chk_lpt_sz = 0; | |
1865 | d->chk_lpt_wastage = 0; | |
1866 | d->chk_lpt_lebs = 0; | |
1867 | d->new_nhead_offs = len; | |
73944a6d AH |
1868 | return err; |
1869 | case 4: | |
17c2f9f8 AB |
1870 | d->chk_lpt_sz += len; |
1871 | d->chk_lpt_wastage += len; | |
73944a6d AH |
1872 | return 0; |
1873 | default: | |
1874 | return -EINVAL; | |
1875 | } | |
1876 | } | |
1877 | ||
2ba5f7ae | 1878 | /** |
edf6be24 | 1879 | * ubifs_dump_lpt_leb - dump an LPT LEB. |
2ba5f7ae AB |
1880 | * @c: UBIFS file-system description object |
1881 | * @lnum: LEB number to dump | |
1882 | * | |
1883 | * This function dumps an LEB from LPT area. Nodes in this area are very | |
1884 | * different to nodes in the main area (e.g., they do not have common headers, | |
1885 | * they do not have 8-byte alignments, etc), so we have a separate function to | |
80736d41 | 1886 | * dump LPT area LEBs. Note, LPT has to be locked by the caller. |
2ba5f7ae AB |
1887 | */ |
1888 | static void dump_lpt_leb(const struct ubifs_info *c, int lnum) | |
1889 | { | |
1890 | int err, len = c->leb_size, node_type, node_num, node_len, offs; | |
cab95d44 | 1891 | void *buf, *p; |
2ba5f7ae AB |
1892 | |
1893 | printk(KERN_DEBUG "(pid %d) start dumping LEB %d\n", | |
1894 | current->pid, lnum); | |
fc5e58c0 | 1895 | buf = p = __vmalloc(c->leb_size, GFP_NOFS, PAGE_KERNEL); |
cab95d44 AB |
1896 | if (!buf) { |
1897 | ubifs_err("cannot allocate memory to dump LPT"); | |
1898 | return; | |
1899 | } | |
1900 | ||
d304820a AB |
1901 | err = ubifs_leb_read(c, lnum, buf, 0, c->leb_size, 1); |
1902 | if (err) | |
cab95d44 | 1903 | goto out; |
d304820a | 1904 | |
2ba5f7ae AB |
1905 | while (1) { |
1906 | offs = c->leb_size - len; | |
cab95d44 | 1907 | if (!is_a_node(c, p, len)) { |
2ba5f7ae AB |
1908 | int pad_len; |
1909 | ||
cab95d44 | 1910 | pad_len = get_pad_len(c, p, len); |
2ba5f7ae AB |
1911 | if (pad_len) { |
1912 | printk(KERN_DEBUG "LEB %d:%d, pad %d bytes\n", | |
1913 | lnum, offs, pad_len); | |
cab95d44 | 1914 | p += pad_len; |
2ba5f7ae AB |
1915 | len -= pad_len; |
1916 | continue; | |
1917 | } | |
1918 | if (len) | |
1919 | printk(KERN_DEBUG "LEB %d:%d, free %d bytes\n", | |
1920 | lnum, offs, len); | |
1921 | break; | |
1922 | } | |
1923 | ||
cab95d44 | 1924 | node_type = get_lpt_node_type(c, p, &node_num); |
2ba5f7ae AB |
1925 | switch (node_type) { |
1926 | case UBIFS_LPT_PNODE: | |
1927 | { | |
1928 | node_len = c->pnode_sz; | |
1929 | if (c->big_lpt) | |
1930 | printk(KERN_DEBUG "LEB %d:%d, pnode num %d\n", | |
1931 | lnum, offs, node_num); | |
1932 | else | |
1933 | printk(KERN_DEBUG "LEB %d:%d, pnode\n", | |
1934 | lnum, offs); | |
1935 | break; | |
1936 | } | |
1937 | case UBIFS_LPT_NNODE: | |
1938 | { | |
1939 | int i; | |
1940 | struct ubifs_nnode nnode; | |
1941 | ||
1942 | node_len = c->nnode_sz; | |
1943 | if (c->big_lpt) | |
1944 | printk(KERN_DEBUG "LEB %d:%d, nnode num %d, ", | |
1945 | lnum, offs, node_num); | |
1946 | else | |
1947 | printk(KERN_DEBUG "LEB %d:%d, nnode, ", | |
1948 | lnum, offs); | |
cab95d44 | 1949 | err = ubifs_unpack_nnode(c, p, &nnode); |
2ba5f7ae | 1950 | for (i = 0; i < UBIFS_LPT_FANOUT; i++) { |
c9927c3e | 1951 | printk(KERN_CONT "%d:%d", nnode.nbranch[i].lnum, |
2ba5f7ae AB |
1952 | nnode.nbranch[i].offs); |
1953 | if (i != UBIFS_LPT_FANOUT - 1) | |
c9927c3e | 1954 | printk(KERN_CONT ", "); |
2ba5f7ae | 1955 | } |
c9927c3e | 1956 | printk(KERN_CONT "\n"); |
2ba5f7ae AB |
1957 | break; |
1958 | } | |
1959 | case UBIFS_LPT_LTAB: | |
1960 | node_len = c->ltab_sz; | |
1961 | printk(KERN_DEBUG "LEB %d:%d, ltab\n", | |
1962 | lnum, offs); | |
1963 | break; | |
1964 | case UBIFS_LPT_LSAVE: | |
1965 | node_len = c->lsave_sz; | |
1966 | printk(KERN_DEBUG "LEB %d:%d, lsave len\n", lnum, offs); | |
1967 | break; | |
1968 | default: | |
1969 | ubifs_err("LPT node type %d not recognized", node_type); | |
cab95d44 | 1970 | goto out; |
2ba5f7ae AB |
1971 | } |
1972 | ||
cab95d44 | 1973 | p += node_len; |
2ba5f7ae AB |
1974 | len -= node_len; |
1975 | } | |
1976 | ||
1977 | printk(KERN_DEBUG "(pid %d) finish dumping LEB %d\n", | |
1978 | current->pid, lnum); | |
cab95d44 AB |
1979 | out: |
1980 | vfree(buf); | |
1981 | return; | |
2ba5f7ae AB |
1982 | } |
1983 | ||
1984 | /** | |
edf6be24 | 1985 | * ubifs_dump_lpt_lebs - dump LPT lebs. |
2ba5f7ae AB |
1986 | * @c: UBIFS file-system description object |
1987 | * | |
1988 | * This function dumps all LPT LEBs. The caller has to make sure the LPT is | |
1989 | * locked. | |
1990 | */ | |
edf6be24 | 1991 | void ubifs_dump_lpt_lebs(const struct ubifs_info *c) |
2ba5f7ae AB |
1992 | { |
1993 | int i; | |
1994 | ||
1995 | printk(KERN_DEBUG "(pid %d) start dumping all LPT LEBs\n", | |
1996 | current->pid); | |
1997 | for (i = 0; i < c->lpt_lebs; i++) | |
1998 | dump_lpt_leb(c, i + c->lpt_first); | |
1999 | printk(KERN_DEBUG "(pid %d) finish dumping all LPT LEBs\n", | |
2000 | current->pid); | |
2001 | } | |
2002 | ||
cdd8ad6e AB |
2003 | /** |
2004 | * dbg_populate_lsave - debugging version of 'populate_lsave()' | |
2005 | * @c: UBIFS file-system description object | |
2006 | * | |
2007 | * This is a debugging version for 'populate_lsave()' which populates lsave | |
2008 | * with random LEBs instead of useful LEBs, which is good for test coverage. | |
2009 | * Returns zero if lsave has not been populated (this debugging feature is | |
2010 | * disabled) an non-zero if lsave has been populated. | |
2011 | */ | |
2012 | static int dbg_populate_lsave(struct ubifs_info *c) | |
2013 | { | |
2014 | struct ubifs_lprops *lprops; | |
2015 | struct ubifs_lpt_heap *heap; | |
2016 | int i; | |
2017 | ||
2b1844a8 | 2018 | if (!dbg_is_chk_gen(c)) |
cdd8ad6e AB |
2019 | return 0; |
2020 | if (random32() & 3) | |
2021 | return 0; | |
2022 | ||
2023 | for (i = 0; i < c->lsave_cnt; i++) | |
2024 | c->lsave[i] = c->main_first; | |
2025 | ||
2026 | list_for_each_entry(lprops, &c->empty_list, list) | |
2027 | c->lsave[random32() % c->lsave_cnt] = lprops->lnum; | |
2028 | list_for_each_entry(lprops, &c->freeable_list, list) | |
2029 | c->lsave[random32() % c->lsave_cnt] = lprops->lnum; | |
2030 | list_for_each_entry(lprops, &c->frdi_idx_list, list) | |
2031 | c->lsave[random32() % c->lsave_cnt] = lprops->lnum; | |
2032 | ||
2033 | heap = &c->lpt_heap[LPROPS_DIRTY_IDX - 1]; | |
2034 | for (i = 0; i < heap->cnt; i++) | |
2035 | c->lsave[random32() % c->lsave_cnt] = heap->arr[i]->lnum; | |
2036 | heap = &c->lpt_heap[LPROPS_DIRTY - 1]; | |
2037 | for (i = 0; i < heap->cnt; i++) | |
2038 | c->lsave[random32() % c->lsave_cnt] = heap->arr[i]->lnum; | |
2039 | heap = &c->lpt_heap[LPROPS_FREE - 1]; | |
2040 | for (i = 0; i < heap->cnt; i++) | |
2041 | c->lsave[random32() % c->lsave_cnt] = heap->arr[i]->lnum; | |
2042 | ||
2043 | return 1; | |
2044 | } |