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