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 } 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 194 err = dbg_chk_lpt_sz(c, 0, 0); 195 if (err) 196 return err; 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; 210 dbg_chk_lpt_sz(c, 1, c->lsave_sz); 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; 218 dbg_chk_lpt_sz(c, 1, c->ltab_sz); 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); 232 dbg_chk_lpt_sz(c, 2, alen - offs); 233 err = alloc_lpt_leb(c, &lnum); 234 if (err) 235 goto no_space; 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; 245 dbg_chk_lpt_sz(c, 1, c->lsave_sz); 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; 253 dbg_chk_lpt_sz(c, 1, c->ltab_sz); 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; 266 dbg_chk_lpt_sz(c, 1, len); 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); 275 dbg_chk_lpt_sz(c, 2, alen - offs); 276 err = alloc_lpt_leb(c, &lnum); 277 if (err) 278 goto no_space; 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; 287 dbg_chk_lpt_sz(c, 1, c->lsave_sz); 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); 295 dbg_chk_lpt_sz(c, 2, alen - offs); 296 err = alloc_lpt_leb(c, &lnum); 297 if (err) 298 goto no_space; 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; 307 dbg_chk_lpt_sz(c, 1, c->ltab_sz); 308 } 309 310 alen = ALIGN(offs, c->min_io_size); 311 upd_ltab(c, lnum, c->leb_size - alen, alen - offs); 312 dbg_chk_lpt_sz(c, 4, alen - offs); 313 err = dbg_chk_lpt_sz(c, 3, alen); 314 if (err) 315 return err; 316 return 0; 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); 323 dbg_dump_lpt_lebs(c); 324 dump_stack(); 325 return err; 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 } 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; 394 dbg_chk_lpt_sz(c, 1, c->lsave_sz); 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; 401 dbg_chk_lpt_sz(c, 1, c->ltab_sz); 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; 419 dbg_chk_lpt_sz(c, 4, alen - wlen); 420 } 421 dbg_chk_lpt_sz(c, 2, 0); 422 err = realloc_lpt_leb(c, &lnum); 423 if (err) 424 goto no_space; 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; 437 dbg_chk_lpt_sz(c, 1, c->lsave_sz); 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; 444 dbg_chk_lpt_sz(c, 1, c->ltab_sz); 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; 466 dbg_chk_lpt_sz(c, 1, len); 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; 480 dbg_chk_lpt_sz(c, 2, alen - wlen); 481 err = realloc_lpt_leb(c, &lnum); 482 if (err) 483 goto no_space; 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; 494 dbg_chk_lpt_sz(c, 1, c->lsave_sz); 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; 507 dbg_chk_lpt_sz(c, 2, alen - wlen); 508 err = realloc_lpt_leb(c, &lnum); 509 if (err) 510 goto no_space; 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; 521 dbg_chk_lpt_sz(c, 1, c->ltab_sz); 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; 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 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); 545 546 return 0; 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 " 551 "%d, done_lsave %d", lnum, offs, len, done_ltab, done_lsave); 552 dbg_dump_lpt_info(c); 553 dbg_dump_lpt_lebs(c); 554 dump_stack(); 555 return err; 556 } 557 558 /** 559 * next_pnode - find next pnode. 560 * @c: UBIFS file-system description object 561 * @pnode: pnode 562 * 563 * This function returns the next pnode or %NULL if there are no more pnodes. 564 */ 565 static struct ubifs_pnode *next_pnode(struct ubifs_info *c, 566 struct ubifs_pnode *pnode) 567 { 568 struct ubifs_nnode *nnode; 569 int iip; 570 571 /* Try to go right */ 572 nnode = pnode->parent; 573 iip = pnode->iip + 1; 574 if (iip < UBIFS_LPT_FANOUT) { 575 /* We assume here that LEB zero is never an LPT LEB */ 576 if (nnode->nbranch[iip].lnum) 577 return ubifs_get_pnode(c, nnode, iip); 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; 586 /* We assume here that LEB zero is never an LPT LEB */ 587 } while (iip >= UBIFS_LPT_FANOUT || !nnode->nbranch[iip].lnum); 588 589 /* Go right */ 590 nnode = ubifs_get_nnode(c, nnode, iip); 591 if (IS_ERR(nnode)) 592 return (void *)nnode; 593 594 /* Go down to level 1 */ 595 while (nnode->level > 1) { 596 nnode = ubifs_get_nnode(c, nnode, 0); 597 if (IS_ERR(nnode)) 598 return (void *)nnode; 599 } 600 601 return ubifs_get_pnode(c, nnode, 0); 602 } 603 604 /** 605 * pnode_lookup - lookup a pnode in the LPT. 606 * @c: UBIFS file-system description object 607 * @i: pnode number (0 to main_lebs - 1) 608 * 609 * This function returns a pointer to the pnode on success or a negative 610 * error code on failure. 611 */ 612 static struct ubifs_pnode *pnode_lookup(struct ubifs_info *c, int i) 613 { 614 int err, h, iip, shft; 615 struct ubifs_nnode *nnode; 616 617 if (!c->nroot) { 618 err = ubifs_read_nnode(c, NULL, 0); 619 if (err) 620 return ERR_PTR(err); 621 } 622 i <<= UBIFS_LPT_FANOUT_SHIFT; 623 nnode = c->nroot; 624 shft = c->lpt_hght * UBIFS_LPT_FANOUT_SHIFT; 625 for (h = 1; h < c->lpt_hght; h++) { 626 iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1)); 627 shft -= UBIFS_LPT_FANOUT_SHIFT; 628 nnode = ubifs_get_nnode(c, nnode, iip); 629 if (IS_ERR(nnode)) 630 return ERR_PTR(PTR_ERR(nnode)); 631 } 632 iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1)); 633 return ubifs_get_pnode(c, nnode, iip); 634 } 635 636 /** 637 * add_pnode_dirt - add dirty space to LPT LEB properties. 638 * @c: UBIFS file-system description object 639 * @pnode: pnode for which to add dirt 640 */ 641 static void add_pnode_dirt(struct ubifs_info *c, struct ubifs_pnode *pnode) 642 { 643 ubifs_add_lpt_dirt(c, pnode->parent->nbranch[pnode->iip].lnum, 644 c->pnode_sz); 645 } 646 647 /** 648 * do_make_pnode_dirty - mark a pnode dirty. 649 * @c: UBIFS file-system description object 650 * @pnode: pnode to mark dirty 651 */ 652 static void do_make_pnode_dirty(struct ubifs_info *c, struct ubifs_pnode *pnode) 653 { 654 /* Assumes cnext list is empty i.e. not called during commit */ 655 if (!test_and_set_bit(DIRTY_CNODE, &pnode->flags)) { 656 struct ubifs_nnode *nnode; 657 658 c->dirty_pn_cnt += 1; 659 add_pnode_dirt(c, pnode); 660 /* Mark parent and ancestors dirty too */ 661 nnode = pnode->parent; 662 while (nnode) { 663 if (!test_and_set_bit(DIRTY_CNODE, &nnode->flags)) { 664 c->dirty_nn_cnt += 1; 665 ubifs_add_nnode_dirt(c, nnode); 666 nnode = nnode->parent; 667 } else 668 break; 669 } 670 } 671 } 672 673 /** 674 * make_tree_dirty - mark the entire LEB properties tree dirty. 675 * @c: UBIFS file-system description object 676 * 677 * This function is used by the "small" LPT model to cause the entire LEB 678 * properties tree to be written. The "small" LPT model does not use LPT 679 * garbage collection because it is more efficient to write the entire tree 680 * (because it is small). 681 * 682 * This function returns %0 on success and a negative error code on failure. 683 */ 684 static int make_tree_dirty(struct ubifs_info *c) 685 { 686 struct ubifs_pnode *pnode; 687 688 pnode = pnode_lookup(c, 0); 689 while (pnode) { 690 do_make_pnode_dirty(c, pnode); 691 pnode = next_pnode(c, pnode); 692 if (IS_ERR(pnode)) 693 return PTR_ERR(pnode); 694 } 695 return 0; 696 } 697 698 /** 699 * need_write_all - determine if the LPT area is running out of free space. 700 * @c: UBIFS file-system description object 701 * 702 * This function returns %1 if the LPT area is running out of free space and %0 703 * if it is not. 704 */ 705 static int need_write_all(struct ubifs_info *c) 706 { 707 long long free = 0; 708 int i; 709 710 for (i = 0; i < c->lpt_lebs; i++) { 711 if (i + c->lpt_first == c->nhead_lnum) 712 free += c->leb_size - c->nhead_offs; 713 else if (c->ltab[i].free == c->leb_size) 714 free += c->leb_size; 715 else if (c->ltab[i].free + c->ltab[i].dirty == c->leb_size) 716 free += c->leb_size; 717 } 718 /* Less than twice the size left */ 719 if (free <= c->lpt_sz * 2) 720 return 1; 721 return 0; 722 } 723 724 /** 725 * lpt_tgc_start - start trivial garbage collection of LPT LEBs. 726 * @c: UBIFS file-system description object 727 * 728 * LPT trivial garbage collection is where a LPT LEB contains only dirty and 729 * free space and so may be reused as soon as the next commit is completed. 730 * This function is called during start commit to mark LPT LEBs for trivial GC. 731 */ 732 static void lpt_tgc_start(struct ubifs_info *c) 733 { 734 int i; 735 736 for (i = 0; i < c->lpt_lebs; i++) { 737 if (i + c->lpt_first == c->nhead_lnum) 738 continue; 739 if (c->ltab[i].dirty > 0 && 740 c->ltab[i].free + c->ltab[i].dirty == c->leb_size) { 741 c->ltab[i].tgc = 1; 742 c->ltab[i].free = c->leb_size; 743 c->ltab[i].dirty = 0; 744 dbg_lp("LEB %d", i + c->lpt_first); 745 } 746 } 747 } 748 749 /** 750 * lpt_tgc_end - end trivial garbage collection of LPT LEBs. 751 * @c: UBIFS file-system description object 752 * 753 * LPT trivial garbage collection is where a LPT LEB contains only dirty and 754 * free space and so may be reused as soon as the next commit is completed. 755 * This function is called after the commit is completed (master node has been 756 * written) and un-maps LPT LEBs that were marked for trivial GC. 757 */ 758 static int lpt_tgc_end(struct ubifs_info *c) 759 { 760 int i, err; 761 762 for (i = 0; i < c->lpt_lebs; i++) 763 if (c->ltab[i].tgc) { 764 err = ubifs_leb_unmap(c, i + c->lpt_first); 765 if (err) 766 return err; 767 c->ltab[i].tgc = 0; 768 dbg_lp("LEB %d", i + c->lpt_first); 769 } 770 return 0; 771 } 772 773 /** 774 * populate_lsave - fill the lsave array with important LEB numbers. 775 * @c: the UBIFS file-system description object 776 * 777 * This function is only called for the "big" model. It records a small number 778 * of LEB numbers of important LEBs. Important LEBs are ones that are (from 779 * most important to least important): empty, freeable, freeable index, dirty 780 * index, dirty or free. Upon mount, we read this list of LEB numbers and bring 781 * their pnodes into memory. That will stop us from having to scan the LPT 782 * straight away. For the "small" model we assume that scanning the LPT is no 783 * big deal. 784 */ 785 static void populate_lsave(struct ubifs_info *c) 786 { 787 struct ubifs_lprops *lprops; 788 struct ubifs_lpt_heap *heap; 789 int i, cnt = 0; 790 791 ubifs_assert(c->big_lpt); 792 if (!(c->lpt_drty_flgs & LSAVE_DIRTY)) { 793 c->lpt_drty_flgs |= LSAVE_DIRTY; 794 ubifs_add_lpt_dirt(c, c->lsave_lnum, c->lsave_sz); 795 } 796 list_for_each_entry(lprops, &c->empty_list, list) { 797 c->lsave[cnt++] = lprops->lnum; 798 if (cnt >= c->lsave_cnt) 799 return; 800 } 801 list_for_each_entry(lprops, &c->freeable_list, list) { 802 c->lsave[cnt++] = lprops->lnum; 803 if (cnt >= c->lsave_cnt) 804 return; 805 } 806 list_for_each_entry(lprops, &c->frdi_idx_list, list) { 807 c->lsave[cnt++] = lprops->lnum; 808 if (cnt >= c->lsave_cnt) 809 return; 810 } 811 heap = &c->lpt_heap[LPROPS_DIRTY_IDX - 1]; 812 for (i = 0; i < heap->cnt; i++) { 813 c->lsave[cnt++] = heap->arr[i]->lnum; 814 if (cnt >= c->lsave_cnt) 815 return; 816 } 817 heap = &c->lpt_heap[LPROPS_DIRTY - 1]; 818 for (i = 0; i < heap->cnt; i++) { 819 c->lsave[cnt++] = heap->arr[i]->lnum; 820 if (cnt >= c->lsave_cnt) 821 return; 822 } 823 heap = &c->lpt_heap[LPROPS_FREE - 1]; 824 for (i = 0; i < heap->cnt; i++) { 825 c->lsave[cnt++] = heap->arr[i]->lnum; 826 if (cnt >= c->lsave_cnt) 827 return; 828 } 829 /* Fill it up completely */ 830 while (cnt < c->lsave_cnt) 831 c->lsave[cnt++] = c->main_first; 832 } 833 834 /** 835 * nnode_lookup - lookup a nnode in the LPT. 836 * @c: UBIFS file-system description object 837 * @i: nnode number 838 * 839 * This function returns a pointer to the nnode on success or a negative 840 * error code on failure. 841 */ 842 static struct ubifs_nnode *nnode_lookup(struct ubifs_info *c, int i) 843 { 844 int err, iip; 845 struct ubifs_nnode *nnode; 846 847 if (!c->nroot) { 848 err = ubifs_read_nnode(c, NULL, 0); 849 if (err) 850 return ERR_PTR(err); 851 } 852 nnode = c->nroot; 853 while (1) { 854 iip = i & (UBIFS_LPT_FANOUT - 1); 855 i >>= UBIFS_LPT_FANOUT_SHIFT; 856 if (!i) 857 break; 858 nnode = ubifs_get_nnode(c, nnode, iip); 859 if (IS_ERR(nnode)) 860 return nnode; 861 } 862 return nnode; 863 } 864 865 /** 866 * make_nnode_dirty - find a nnode and, if found, make it dirty. 867 * @c: UBIFS file-system description object 868 * @node_num: nnode number of nnode to make dirty 869 * @lnum: LEB number where nnode was written 870 * @offs: offset where nnode was written 871 * 872 * This function is used by LPT garbage collection. LPT garbage collection is 873 * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection 874 * simply involves marking all the nodes in the LEB being garbage-collected as 875 * dirty. The dirty nodes are written next commit, after which the LEB is free 876 * to be reused. 877 * 878 * This function returns %0 on success and a negative error code on failure. 879 */ 880 static int make_nnode_dirty(struct ubifs_info *c, int node_num, int lnum, 881 int offs) 882 { 883 struct ubifs_nnode *nnode; 884 885 nnode = nnode_lookup(c, node_num); 886 if (IS_ERR(nnode)) 887 return PTR_ERR(nnode); 888 if (nnode->parent) { 889 struct ubifs_nbranch *branch; 890 891 branch = &nnode->parent->nbranch[nnode->iip]; 892 if (branch->lnum != lnum || branch->offs != offs) 893 return 0; /* nnode is obsolete */ 894 } else if (c->lpt_lnum != lnum || c->lpt_offs != offs) 895 return 0; /* nnode is obsolete */ 896 /* Assumes cnext list is empty i.e. not called during commit */ 897 if (!test_and_set_bit(DIRTY_CNODE, &nnode->flags)) { 898 c->dirty_nn_cnt += 1; 899 ubifs_add_nnode_dirt(c, nnode); 900 /* Mark parent and ancestors dirty too */ 901 nnode = nnode->parent; 902 while (nnode) { 903 if (!test_and_set_bit(DIRTY_CNODE, &nnode->flags)) { 904 c->dirty_nn_cnt += 1; 905 ubifs_add_nnode_dirt(c, nnode); 906 nnode = nnode->parent; 907 } else 908 break; 909 } 910 } 911 return 0; 912 } 913 914 /** 915 * make_pnode_dirty - find a pnode and, if found, make it dirty. 916 * @c: UBIFS file-system description object 917 * @node_num: pnode number of pnode to make dirty 918 * @lnum: LEB number where pnode was written 919 * @offs: offset where pnode was written 920 * 921 * This function is used by LPT garbage collection. LPT garbage collection is 922 * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection 923 * simply involves marking all the nodes in the LEB being garbage-collected as 924 * dirty. The dirty nodes are written next commit, after which the LEB is free 925 * to be reused. 926 * 927 * This function returns %0 on success and a negative error code on failure. 928 */ 929 static int make_pnode_dirty(struct ubifs_info *c, int node_num, int lnum, 930 int offs) 931 { 932 struct ubifs_pnode *pnode; 933 struct ubifs_nbranch *branch; 934 935 pnode = pnode_lookup(c, node_num); 936 if (IS_ERR(pnode)) 937 return PTR_ERR(pnode); 938 branch = &pnode->parent->nbranch[pnode->iip]; 939 if (branch->lnum != lnum || branch->offs != offs) 940 return 0; 941 do_make_pnode_dirty(c, pnode); 942 return 0; 943 } 944 945 /** 946 * make_ltab_dirty - make ltab node dirty. 947 * @c: UBIFS file-system description object 948 * @lnum: LEB number where ltab was written 949 * @offs: offset where ltab was written 950 * 951 * This function is used by LPT garbage collection. LPT garbage collection is 952 * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection 953 * simply involves marking all the nodes in the LEB being garbage-collected as 954 * dirty. The dirty nodes are written next commit, after which the LEB is free 955 * to be reused. 956 * 957 * This function returns %0 on success and a negative error code on failure. 958 */ 959 static int make_ltab_dirty(struct ubifs_info *c, int lnum, int offs) 960 { 961 if (lnum != c->ltab_lnum || offs != c->ltab_offs) 962 return 0; /* This ltab node is obsolete */ 963 if (!(c->lpt_drty_flgs & LTAB_DIRTY)) { 964 c->lpt_drty_flgs |= LTAB_DIRTY; 965 ubifs_add_lpt_dirt(c, c->ltab_lnum, c->ltab_sz); 966 } 967 return 0; 968 } 969 970 /** 971 * make_lsave_dirty - make lsave node dirty. 972 * @c: UBIFS file-system description object 973 * @lnum: LEB number where lsave was written 974 * @offs: offset where lsave was written 975 * 976 * This function is used by LPT garbage collection. LPT garbage collection is 977 * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection 978 * simply involves marking all the nodes in the LEB being garbage-collected as 979 * dirty. The dirty nodes are written next commit, after which the LEB is free 980 * to be reused. 981 * 982 * This function returns %0 on success and a negative error code on failure. 983 */ 984 static int make_lsave_dirty(struct ubifs_info *c, int lnum, int offs) 985 { 986 if (lnum != c->lsave_lnum || offs != c->lsave_offs) 987 return 0; /* This lsave node is obsolete */ 988 if (!(c->lpt_drty_flgs & LSAVE_DIRTY)) { 989 c->lpt_drty_flgs |= LSAVE_DIRTY; 990 ubifs_add_lpt_dirt(c, c->lsave_lnum, c->lsave_sz); 991 } 992 return 0; 993 } 994 995 /** 996 * make_node_dirty - make node dirty. 997 * @c: UBIFS file-system description object 998 * @node_type: LPT node type 999 * @node_num: node number 1000 * @lnum: LEB number where node was written 1001 * @offs: offset where node 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_node_dirty(struct ubifs_info *c, int node_type, int node_num, 1012 int lnum, int offs) 1013 { 1014 switch (node_type) { 1015 case UBIFS_LPT_NNODE: 1016 return make_nnode_dirty(c, node_num, lnum, offs); 1017 case UBIFS_LPT_PNODE: 1018 return make_pnode_dirty(c, node_num, lnum, offs); 1019 case UBIFS_LPT_LTAB: 1020 return make_ltab_dirty(c, lnum, offs); 1021 case UBIFS_LPT_LSAVE: 1022 return make_lsave_dirty(c, lnum, offs); 1023 } 1024 return -EINVAL; 1025 } 1026 1027 /** 1028 * get_lpt_node_len - return the length of a node based on its type. 1029 * @c: UBIFS file-system description object 1030 * @node_type: LPT node type 1031 */ 1032 static int get_lpt_node_len(const struct ubifs_info *c, int node_type) 1033 { 1034 switch (node_type) { 1035 case UBIFS_LPT_NNODE: 1036 return c->nnode_sz; 1037 case UBIFS_LPT_PNODE: 1038 return c->pnode_sz; 1039 case UBIFS_LPT_LTAB: 1040 return c->ltab_sz; 1041 case UBIFS_LPT_LSAVE: 1042 return c->lsave_sz; 1043 } 1044 return 0; 1045 } 1046 1047 /** 1048 * get_pad_len - return the length of padding in a buffer. 1049 * @c: UBIFS file-system description object 1050 * @buf: buffer 1051 * @len: length of buffer 1052 */ 1053 static int get_pad_len(const struct ubifs_info *c, uint8_t *buf, int len) 1054 { 1055 int offs, pad_len; 1056 1057 if (c->min_io_size == 1) 1058 return 0; 1059 offs = c->leb_size - len; 1060 pad_len = ALIGN(offs, c->min_io_size) - offs; 1061 return pad_len; 1062 } 1063 1064 /** 1065 * get_lpt_node_type - return type (and node number) of a node in a buffer. 1066 * @c: UBIFS file-system description object 1067 * @buf: buffer 1068 * @node_num: node number is returned here 1069 */ 1070 static int get_lpt_node_type(const struct ubifs_info *c, uint8_t *buf, 1071 int *node_num) 1072 { 1073 uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES; 1074 int pos = 0, node_type; 1075 1076 node_type = ubifs_unpack_bits(&addr, &pos, UBIFS_LPT_TYPE_BITS); 1077 *node_num = ubifs_unpack_bits(&addr, &pos, c->pcnt_bits); 1078 return node_type; 1079 } 1080 1081 /** 1082 * is_a_node - determine if a buffer contains a node. 1083 * @c: UBIFS file-system description object 1084 * @buf: buffer 1085 * @len: length of buffer 1086 * 1087 * This function returns %1 if the buffer contains a node or %0 if it does not. 1088 */ 1089 static int is_a_node(const struct ubifs_info *c, uint8_t *buf, int len) 1090 { 1091 uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES; 1092 int pos = 0, node_type, node_len; 1093 uint16_t crc, calc_crc; 1094 1095 if (len < UBIFS_LPT_CRC_BYTES + (UBIFS_LPT_TYPE_BITS + 7) / 8) 1096 return 0; 1097 node_type = ubifs_unpack_bits(&addr, &pos, UBIFS_LPT_TYPE_BITS); 1098 if (node_type == UBIFS_LPT_NOT_A_NODE) 1099 return 0; 1100 node_len = get_lpt_node_len(c, node_type); 1101 if (!node_len || node_len > len) 1102 return 0; 1103 pos = 0; 1104 addr = buf; 1105 crc = ubifs_unpack_bits(&addr, &pos, UBIFS_LPT_CRC_BITS); 1106 calc_crc = crc16(-1, buf + UBIFS_LPT_CRC_BYTES, 1107 node_len - UBIFS_LPT_CRC_BYTES); 1108 if (crc != calc_crc) 1109 return 0; 1110 return 1; 1111 } 1112 1113 /** 1114 * lpt_gc_lnum - garbage collect a LPT LEB. 1115 * @c: UBIFS file-system description object 1116 * @lnum: LEB number to garbage collect 1117 * 1118 * LPT garbage collection is used only for the "big" LPT model 1119 * (c->big_lpt == 1). Garbage collection simply involves marking all the nodes 1120 * in the LEB being garbage-collected as dirty. The dirty nodes are written 1121 * next commit, after which the LEB is free to be reused. 1122 * 1123 * This function returns %0 on success and a negative error code on failure. 1124 */ 1125 static int lpt_gc_lnum(struct ubifs_info *c, int lnum) 1126 { 1127 int err, len = c->leb_size, node_type, node_num, node_len, offs; 1128 void *buf = c->lpt_buf; 1129 1130 dbg_lp("LEB %d", lnum); 1131 err = ubi_read(c->ubi, lnum, buf, 0, c->leb_size); 1132 if (err) { 1133 ubifs_err("cannot read LEB %d, error %d", lnum, err); 1134 return err; 1135 } 1136 while (1) { 1137 if (!is_a_node(c, buf, len)) { 1138 int pad_len; 1139 1140 pad_len = get_pad_len(c, buf, len); 1141 if (pad_len) { 1142 buf += pad_len; 1143 len -= pad_len; 1144 continue; 1145 } 1146 return 0; 1147 } 1148 node_type = get_lpt_node_type(c, buf, &node_num); 1149 node_len = get_lpt_node_len(c, node_type); 1150 offs = c->leb_size - len; 1151 ubifs_assert(node_len != 0); 1152 mutex_lock(&c->lp_mutex); 1153 err = make_node_dirty(c, node_type, node_num, lnum, offs); 1154 mutex_unlock(&c->lp_mutex); 1155 if (err) 1156 return err; 1157 buf += node_len; 1158 len -= node_len; 1159 } 1160 return 0; 1161 } 1162 1163 /** 1164 * lpt_gc - LPT garbage collection. 1165 * @c: UBIFS file-system description object 1166 * 1167 * Select a LPT LEB for LPT garbage collection and call 'lpt_gc_lnum()'. 1168 * Returns %0 on success and a negative error code on failure. 1169 */ 1170 static int lpt_gc(struct ubifs_info *c) 1171 { 1172 int i, lnum = -1, dirty = 0; 1173 1174 mutex_lock(&c->lp_mutex); 1175 for (i = 0; i < c->lpt_lebs; i++) { 1176 ubifs_assert(!c->ltab[i].tgc); 1177 if (i + c->lpt_first == c->nhead_lnum || 1178 c->ltab[i].free + c->ltab[i].dirty == c->leb_size) 1179 continue; 1180 if (c->ltab[i].dirty > dirty) { 1181 dirty = c->ltab[i].dirty; 1182 lnum = i + c->lpt_first; 1183 } 1184 } 1185 mutex_unlock(&c->lp_mutex); 1186 if (lnum == -1) 1187 return -ENOSPC; 1188 return lpt_gc_lnum(c, lnum); 1189 } 1190 1191 /** 1192 * ubifs_lpt_start_commit - UBIFS commit starts. 1193 * @c: the UBIFS file-system description object 1194 * 1195 * This function has to be called when UBIFS starts the commit operation. 1196 * This function "freezes" all currently dirty LEB properties and does not 1197 * change them anymore. Further changes are saved and tracked separately 1198 * because they are not part of this commit. This function returns zero in case 1199 * of success and a negative error code in case of failure. 1200 */ 1201 int ubifs_lpt_start_commit(struct ubifs_info *c) 1202 { 1203 int err, cnt; 1204 1205 dbg_lp(""); 1206 1207 mutex_lock(&c->lp_mutex); 1208 err = dbg_chk_lpt_free_spc(c); 1209 if (err) 1210 goto out; 1211 err = dbg_check_ltab(c); 1212 if (err) 1213 goto out; 1214 1215 if (c->check_lpt_free) { 1216 /* 1217 * We ensure there is enough free space in 1218 * ubifs_lpt_post_commit() by marking nodes dirty. That 1219 * information is lost when we unmount, so we also need 1220 * to check free space once after mounting also. 1221 */ 1222 c->check_lpt_free = 0; 1223 while (need_write_all(c)) { 1224 mutex_unlock(&c->lp_mutex); 1225 err = lpt_gc(c); 1226 if (err) 1227 return err; 1228 mutex_lock(&c->lp_mutex); 1229 } 1230 } 1231 1232 lpt_tgc_start(c); 1233 1234 if (!c->dirty_pn_cnt) { 1235 dbg_cmt("no cnodes to commit"); 1236 err = 0; 1237 goto out; 1238 } 1239 1240 if (!c->big_lpt && need_write_all(c)) { 1241 /* If needed, write everything */ 1242 err = make_tree_dirty(c); 1243 if (err) 1244 goto out; 1245 lpt_tgc_start(c); 1246 } 1247 1248 if (c->big_lpt) 1249 populate_lsave(c); 1250 1251 cnt = get_cnodes_to_commit(c); 1252 ubifs_assert(cnt != 0); 1253 1254 err = layout_cnodes(c); 1255 if (err) 1256 goto out; 1257 1258 /* Copy the LPT's own lprops for end commit to write */ 1259 memcpy(c->ltab_cmt, c->ltab, 1260 sizeof(struct ubifs_lpt_lprops) * c->lpt_lebs); 1261 c->lpt_drty_flgs &= ~(LTAB_DIRTY | LSAVE_DIRTY); 1262 1263 out: 1264 mutex_unlock(&c->lp_mutex); 1265 return err; 1266 } 1267 1268 /** 1269 * free_obsolete_cnodes - free obsolete cnodes for commit end. 1270 * @c: UBIFS file-system description object 1271 */ 1272 static void free_obsolete_cnodes(struct ubifs_info *c) 1273 { 1274 struct ubifs_cnode *cnode, *cnext; 1275 1276 cnext = c->lpt_cnext; 1277 if (!cnext) 1278 return; 1279 do { 1280 cnode = cnext; 1281 cnext = cnode->cnext; 1282 if (test_bit(OBSOLETE_CNODE, &cnode->flags)) 1283 kfree(cnode); 1284 else 1285 cnode->cnext = NULL; 1286 } while (cnext != c->lpt_cnext); 1287 c->lpt_cnext = NULL; 1288 } 1289 1290 /** 1291 * ubifs_lpt_end_commit - finish the commit operation. 1292 * @c: the UBIFS file-system description object 1293 * 1294 * This function has to be called when the commit operation finishes. It 1295 * flushes the changes which were "frozen" by 'ubifs_lprops_start_commit()' to 1296 * the media. Returns zero in case of success and a negative error code in case 1297 * of failure. 1298 */ 1299 int ubifs_lpt_end_commit(struct ubifs_info *c) 1300 { 1301 int err; 1302 1303 dbg_lp(""); 1304 1305 if (!c->lpt_cnext) 1306 return 0; 1307 1308 err = write_cnodes(c); 1309 if (err) 1310 return err; 1311 1312 mutex_lock(&c->lp_mutex); 1313 free_obsolete_cnodes(c); 1314 mutex_unlock(&c->lp_mutex); 1315 1316 return 0; 1317 } 1318 1319 /** 1320 * ubifs_lpt_post_commit - post commit LPT trivial GC and LPT GC. 1321 * @c: UBIFS file-system description object 1322 * 1323 * LPT trivial GC is completed after a commit. Also LPT GC is done after a 1324 * commit for the "big" LPT model. 1325 */ 1326 int ubifs_lpt_post_commit(struct ubifs_info *c) 1327 { 1328 int err; 1329 1330 mutex_lock(&c->lp_mutex); 1331 err = lpt_tgc_end(c); 1332 if (err) 1333 goto out; 1334 if (c->big_lpt) 1335 while (need_write_all(c)) { 1336 mutex_unlock(&c->lp_mutex); 1337 err = lpt_gc(c); 1338 if (err) 1339 return err; 1340 mutex_lock(&c->lp_mutex); 1341 } 1342 out: 1343 mutex_unlock(&c->lp_mutex); 1344 return err; 1345 } 1346 1347 /** 1348 * first_nnode - find the first nnode in memory. 1349 * @c: UBIFS file-system description object 1350 * @hght: height of tree where nnode found is returned here 1351 * 1352 * This function returns a pointer to the nnode found or %NULL if no nnode is 1353 * found. This function is a helper to 'ubifs_lpt_free()'. 1354 */ 1355 static struct ubifs_nnode *first_nnode(struct ubifs_info *c, int *hght) 1356 { 1357 struct ubifs_nnode *nnode; 1358 int h, i, found; 1359 1360 nnode = c->nroot; 1361 *hght = 0; 1362 if (!nnode) 1363 return NULL; 1364 for (h = 1; h < c->lpt_hght; h++) { 1365 found = 0; 1366 for (i = 0; i < UBIFS_LPT_FANOUT; i++) { 1367 if (nnode->nbranch[i].nnode) { 1368 found = 1; 1369 nnode = nnode->nbranch[i].nnode; 1370 *hght = h; 1371 break; 1372 } 1373 } 1374 if (!found) 1375 break; 1376 } 1377 return nnode; 1378 } 1379 1380 /** 1381 * next_nnode - find the next nnode in memory. 1382 * @c: UBIFS file-system description object 1383 * @nnode: nnode from which to start. 1384 * @hght: height of tree where nnode is, is passed and returned here 1385 * 1386 * This function returns a pointer to the nnode found or %NULL if no nnode is 1387 * found. This function is a helper to 'ubifs_lpt_free()'. 1388 */ 1389 static struct ubifs_nnode *next_nnode(struct ubifs_info *c, 1390 struct ubifs_nnode *nnode, int *hght) 1391 { 1392 struct ubifs_nnode *parent; 1393 int iip, h, i, found; 1394 1395 parent = nnode->parent; 1396 if (!parent) 1397 return NULL; 1398 if (nnode->iip == UBIFS_LPT_FANOUT - 1) { 1399 *hght -= 1; 1400 return parent; 1401 } 1402 for (iip = nnode->iip + 1; iip < UBIFS_LPT_FANOUT; iip++) { 1403 nnode = parent->nbranch[iip].nnode; 1404 if (nnode) 1405 break; 1406 } 1407 if (!nnode) { 1408 *hght -= 1; 1409 return parent; 1410 } 1411 for (h = *hght + 1; h < c->lpt_hght; h++) { 1412 found = 0; 1413 for (i = 0; i < UBIFS_LPT_FANOUT; i++) { 1414 if (nnode->nbranch[i].nnode) { 1415 found = 1; 1416 nnode = nnode->nbranch[i].nnode; 1417 *hght = h; 1418 break; 1419 } 1420 } 1421 if (!found) 1422 break; 1423 } 1424 return nnode; 1425 } 1426 1427 /** 1428 * ubifs_lpt_free - free resources owned by the LPT. 1429 * @c: UBIFS file-system description object 1430 * @wr_only: free only resources used for writing 1431 */ 1432 void ubifs_lpt_free(struct ubifs_info *c, int wr_only) 1433 { 1434 struct ubifs_nnode *nnode; 1435 int i, hght; 1436 1437 /* Free write-only things first */ 1438 1439 free_obsolete_cnodes(c); /* Leftover from a failed commit */ 1440 1441 vfree(c->ltab_cmt); 1442 c->ltab_cmt = NULL; 1443 vfree(c->lpt_buf); 1444 c->lpt_buf = NULL; 1445 kfree(c->lsave); 1446 c->lsave = NULL; 1447 1448 if (wr_only) 1449 return; 1450 1451 /* Now free the rest */ 1452 1453 nnode = first_nnode(c, &hght); 1454 while (nnode) { 1455 for (i = 0; i < UBIFS_LPT_FANOUT; i++) 1456 kfree(nnode->nbranch[i].nnode); 1457 nnode = next_nnode(c, nnode, &hght); 1458 } 1459 for (i = 0; i < LPROPS_HEAP_CNT; i++) 1460 kfree(c->lpt_heap[i].arr); 1461 kfree(c->dirty_idx.arr); 1462 kfree(c->nroot); 1463 vfree(c->ltab); 1464 kfree(c->lpt_nod_buf); 1465 } 1466 1467 #ifdef CONFIG_UBIFS_FS_DEBUG 1468 1469 /** 1470 * dbg_is_all_ff - determine if a buffer contains only 0xFF bytes. 1471 * @buf: buffer 1472 * @len: buffer length 1473 */ 1474 static int dbg_is_all_ff(uint8_t *buf, int len) 1475 { 1476 int i; 1477 1478 for (i = 0; i < len; i++) 1479 if (buf[i] != 0xff) 1480 return 0; 1481 return 1; 1482 } 1483 1484 /** 1485 * dbg_is_nnode_dirty - determine if a nnode is dirty. 1486 * @c: the UBIFS file-system description object 1487 * @lnum: LEB number where nnode was written 1488 * @offs: offset where nnode was written 1489 */ 1490 static int dbg_is_nnode_dirty(struct ubifs_info *c, int lnum, int offs) 1491 { 1492 struct ubifs_nnode *nnode; 1493 int hght; 1494 1495 /* Entire tree is in memory so first_nnode / next_nnode are OK */ 1496 nnode = first_nnode(c, &hght); 1497 for (; nnode; nnode = next_nnode(c, nnode, &hght)) { 1498 struct ubifs_nbranch *branch; 1499 1500 cond_resched(); 1501 if (nnode->parent) { 1502 branch = &nnode->parent->nbranch[nnode->iip]; 1503 if (branch->lnum != lnum || branch->offs != offs) 1504 continue; 1505 if (test_bit(DIRTY_CNODE, &nnode->flags)) 1506 return 1; 1507 return 0; 1508 } else { 1509 if (c->lpt_lnum != lnum || c->lpt_offs != offs) 1510 continue; 1511 if (test_bit(DIRTY_CNODE, &nnode->flags)) 1512 return 1; 1513 return 0; 1514 } 1515 } 1516 return 1; 1517 } 1518 1519 /** 1520 * dbg_is_pnode_dirty - determine if a pnode is dirty. 1521 * @c: the UBIFS file-system description object 1522 * @lnum: LEB number where pnode was written 1523 * @offs: offset where pnode was written 1524 */ 1525 static int dbg_is_pnode_dirty(struct ubifs_info *c, int lnum, int offs) 1526 { 1527 int i, cnt; 1528 1529 cnt = DIV_ROUND_UP(c->main_lebs, UBIFS_LPT_FANOUT); 1530 for (i = 0; i < cnt; i++) { 1531 struct ubifs_pnode *pnode; 1532 struct ubifs_nbranch *branch; 1533 1534 cond_resched(); 1535 pnode = pnode_lookup(c, i); 1536 if (IS_ERR(pnode)) 1537 return PTR_ERR(pnode); 1538 branch = &pnode->parent->nbranch[pnode->iip]; 1539 if (branch->lnum != lnum || branch->offs != offs) 1540 continue; 1541 if (test_bit(DIRTY_CNODE, &pnode->flags)) 1542 return 1; 1543 return 0; 1544 } 1545 return 1; 1546 } 1547 1548 /** 1549 * dbg_is_ltab_dirty - determine if a ltab node is dirty. 1550 * @c: the UBIFS file-system description object 1551 * @lnum: LEB number where ltab node was written 1552 * @offs: offset where ltab node was written 1553 */ 1554 static int dbg_is_ltab_dirty(struct ubifs_info *c, int lnum, int offs) 1555 { 1556 if (lnum != c->ltab_lnum || offs != c->ltab_offs) 1557 return 1; 1558 return (c->lpt_drty_flgs & LTAB_DIRTY) != 0; 1559 } 1560 1561 /** 1562 * dbg_is_lsave_dirty - determine if a lsave node is dirty. 1563 * @c: the UBIFS file-system description object 1564 * @lnum: LEB number where lsave node was written 1565 * @offs: offset where lsave node was written 1566 */ 1567 static int dbg_is_lsave_dirty(struct ubifs_info *c, int lnum, int offs) 1568 { 1569 if (lnum != c->lsave_lnum || offs != c->lsave_offs) 1570 return 1; 1571 return (c->lpt_drty_flgs & LSAVE_DIRTY) != 0; 1572 } 1573 1574 /** 1575 * dbg_is_node_dirty - determine if a node is dirty. 1576 * @c: the UBIFS file-system description object 1577 * @node_type: node type 1578 * @lnum: LEB number where node was written 1579 * @offs: offset where node was written 1580 */ 1581 static int dbg_is_node_dirty(struct ubifs_info *c, int node_type, int lnum, 1582 int offs) 1583 { 1584 switch (node_type) { 1585 case UBIFS_LPT_NNODE: 1586 return dbg_is_nnode_dirty(c, lnum, offs); 1587 case UBIFS_LPT_PNODE: 1588 return dbg_is_pnode_dirty(c, lnum, offs); 1589 case UBIFS_LPT_LTAB: 1590 return dbg_is_ltab_dirty(c, lnum, offs); 1591 case UBIFS_LPT_LSAVE: 1592 return dbg_is_lsave_dirty(c, lnum, offs); 1593 } 1594 return 1; 1595 } 1596 1597 /** 1598 * dbg_check_ltab_lnum - check the ltab for a LPT LEB number. 1599 * @c: the UBIFS file-system description object 1600 * @lnum: LEB number where node was written 1601 * @offs: offset where node was written 1602 * 1603 * This function returns %0 on success and a negative error code on failure. 1604 */ 1605 static int dbg_check_ltab_lnum(struct ubifs_info *c, int lnum) 1606 { 1607 int err, len = c->leb_size, dirty = 0, node_type, node_num, node_len; 1608 int ret; 1609 void *buf = c->dbg->buf; 1610 1611 if (!(ubifs_chk_flags & UBIFS_CHK_LPROPS)) 1612 return 0; 1613 1614 dbg_lp("LEB %d", lnum); 1615 err = ubi_read(c->ubi, lnum, buf, 0, c->leb_size); 1616 if (err) { 1617 dbg_msg("ubi_read failed, LEB %d, error %d", lnum, err); 1618 return err; 1619 } 1620 while (1) { 1621 if (!is_a_node(c, buf, len)) { 1622 int i, pad_len; 1623 1624 pad_len = get_pad_len(c, buf, len); 1625 if (pad_len) { 1626 buf += pad_len; 1627 len -= pad_len; 1628 dirty += pad_len; 1629 continue; 1630 } 1631 if (!dbg_is_all_ff(buf, len)) { 1632 dbg_msg("invalid empty space in LEB %d at %d", 1633 lnum, c->leb_size - len); 1634 err = -EINVAL; 1635 } 1636 i = lnum - c->lpt_first; 1637 if (len != c->ltab[i].free) { 1638 dbg_msg("invalid free space in LEB %d " 1639 "(free %d, expected %d)", 1640 lnum, len, c->ltab[i].free); 1641 err = -EINVAL; 1642 } 1643 if (dirty != c->ltab[i].dirty) { 1644 dbg_msg("invalid dirty space in LEB %d " 1645 "(dirty %d, expected %d)", 1646 lnum, dirty, c->ltab[i].dirty); 1647 err = -EINVAL; 1648 } 1649 return err; 1650 } 1651 node_type = get_lpt_node_type(c, buf, &node_num); 1652 node_len = get_lpt_node_len(c, node_type); 1653 ret = dbg_is_node_dirty(c, node_type, lnum, c->leb_size - len); 1654 if (ret == 1) 1655 dirty += node_len; 1656 buf += node_len; 1657 len -= node_len; 1658 } 1659 } 1660 1661 /** 1662 * dbg_check_ltab - check the free and dirty space in the ltab. 1663 * @c: the UBIFS file-system description object 1664 * 1665 * This function returns %0 on success and a negative error code on failure. 1666 */ 1667 int dbg_check_ltab(struct ubifs_info *c) 1668 { 1669 int lnum, err, i, cnt; 1670 1671 if (!(ubifs_chk_flags & UBIFS_CHK_LPROPS)) 1672 return 0; 1673 1674 /* Bring the entire tree into memory */ 1675 cnt = DIV_ROUND_UP(c->main_lebs, UBIFS_LPT_FANOUT); 1676 for (i = 0; i < cnt; i++) { 1677 struct ubifs_pnode *pnode; 1678 1679 pnode = pnode_lookup(c, i); 1680 if (IS_ERR(pnode)) 1681 return PTR_ERR(pnode); 1682 cond_resched(); 1683 } 1684 1685 /* Check nodes */ 1686 err = dbg_check_lpt_nodes(c, (struct ubifs_cnode *)c->nroot, 0, 0); 1687 if (err) 1688 return err; 1689 1690 /* Check each LEB */ 1691 for (lnum = c->lpt_first; lnum <= c->lpt_last; lnum++) { 1692 err = dbg_check_ltab_lnum(c, lnum); 1693 if (err) { 1694 dbg_err("failed at LEB %d", lnum); 1695 return err; 1696 } 1697 } 1698 1699 dbg_lp("succeeded"); 1700 return 0; 1701 } 1702 1703 /** 1704 * dbg_chk_lpt_free_spc - check LPT free space is enough to write entire LPT. 1705 * @c: the UBIFS file-system description object 1706 * 1707 * This function returns %0 on success and a negative error code on failure. 1708 */ 1709 int dbg_chk_lpt_free_spc(struct ubifs_info *c) 1710 { 1711 long long free = 0; 1712 int i; 1713 1714 if (!(ubifs_chk_flags & UBIFS_CHK_LPROPS)) 1715 return 0; 1716 1717 for (i = 0; i < c->lpt_lebs; i++) { 1718 if (c->ltab[i].tgc || c->ltab[i].cmt) 1719 continue; 1720 if (i + c->lpt_first == c->nhead_lnum) 1721 free += c->leb_size - c->nhead_offs; 1722 else if (c->ltab[i].free == c->leb_size) 1723 free += c->leb_size; 1724 } 1725 if (free < c->lpt_sz) { 1726 dbg_err("LPT space error: free %lld lpt_sz %lld", 1727 free, c->lpt_sz); 1728 dbg_dump_lpt_info(c); 1729 dbg_dump_lpt_lebs(c); 1730 dump_stack(); 1731 return -EINVAL; 1732 } 1733 return 0; 1734 } 1735 1736 /** 1737 * dbg_chk_lpt_sz - check LPT does not write more than LPT size. 1738 * @c: the UBIFS file-system description object 1739 * @action: action 1740 * @len: length written 1741 * 1742 * This function returns %0 on success and a negative error code on failure. 1743 */ 1744 int dbg_chk_lpt_sz(struct ubifs_info *c, int action, int len) 1745 { 1746 struct ubifs_debug_info *d = c->dbg; 1747 long long chk_lpt_sz, lpt_sz; 1748 int err = 0; 1749 1750 if (!(ubifs_chk_flags & UBIFS_CHK_LPROPS)) 1751 return 0; 1752 1753 switch (action) { 1754 case 0: 1755 d->chk_lpt_sz = 0; 1756 d->chk_lpt_sz2 = 0; 1757 d->chk_lpt_lebs = 0; 1758 d->chk_lpt_wastage = 0; 1759 if (c->dirty_pn_cnt > c->pnode_cnt) { 1760 dbg_err("dirty pnodes %d exceed max %d", 1761 c->dirty_pn_cnt, c->pnode_cnt); 1762 err = -EINVAL; 1763 } 1764 if (c->dirty_nn_cnt > c->nnode_cnt) { 1765 dbg_err("dirty nnodes %d exceed max %d", 1766 c->dirty_nn_cnt, c->nnode_cnt); 1767 err = -EINVAL; 1768 } 1769 return err; 1770 case 1: 1771 d->chk_lpt_sz += len; 1772 return 0; 1773 case 2: 1774 d->chk_lpt_sz += len; 1775 d->chk_lpt_wastage += len; 1776 d->chk_lpt_lebs += 1; 1777 return 0; 1778 case 3: 1779 chk_lpt_sz = c->leb_size; 1780 chk_lpt_sz *= d->chk_lpt_lebs; 1781 chk_lpt_sz += len - c->nhead_offs; 1782 if (d->chk_lpt_sz != chk_lpt_sz) { 1783 dbg_err("LPT wrote %lld but space used was %lld", 1784 d->chk_lpt_sz, chk_lpt_sz); 1785 err = -EINVAL; 1786 } 1787 if (d->chk_lpt_sz > c->lpt_sz) { 1788 dbg_err("LPT wrote %lld but lpt_sz is %lld", 1789 d->chk_lpt_sz, c->lpt_sz); 1790 err = -EINVAL; 1791 } 1792 if (d->chk_lpt_sz2 && d->chk_lpt_sz != d->chk_lpt_sz2) { 1793 dbg_err("LPT layout size %lld but wrote %lld", 1794 d->chk_lpt_sz, d->chk_lpt_sz2); 1795 err = -EINVAL; 1796 } 1797 if (d->chk_lpt_sz2 && d->new_nhead_offs != len) { 1798 dbg_err("LPT new nhead offs: expected %d was %d", 1799 d->new_nhead_offs, len); 1800 err = -EINVAL; 1801 } 1802 lpt_sz = (long long)c->pnode_cnt * c->pnode_sz; 1803 lpt_sz += (long long)c->nnode_cnt * c->nnode_sz; 1804 lpt_sz += c->ltab_sz; 1805 if (c->big_lpt) 1806 lpt_sz += c->lsave_sz; 1807 if (d->chk_lpt_sz - d->chk_lpt_wastage > lpt_sz) { 1808 dbg_err("LPT chk_lpt_sz %lld + waste %lld exceeds %lld", 1809 d->chk_lpt_sz, d->chk_lpt_wastage, lpt_sz); 1810 err = -EINVAL; 1811 } 1812 if (err) { 1813 dbg_dump_lpt_info(c); 1814 dbg_dump_lpt_lebs(c); 1815 dump_stack(); 1816 } 1817 d->chk_lpt_sz2 = d->chk_lpt_sz; 1818 d->chk_lpt_sz = 0; 1819 d->chk_lpt_wastage = 0; 1820 d->chk_lpt_lebs = 0; 1821 d->new_nhead_offs = len; 1822 return err; 1823 case 4: 1824 d->chk_lpt_sz += len; 1825 d->chk_lpt_wastage += len; 1826 return 0; 1827 default: 1828 return -EINVAL; 1829 } 1830 } 1831 1832 /** 1833 * dbg_dump_lpt_leb - dump an LPT LEB. 1834 * @c: UBIFS file-system description object 1835 * @lnum: LEB number to dump 1836 * 1837 * This function dumps an LEB from LPT area. Nodes in this area are very 1838 * different to nodes in the main area (e.g., they do not have common headers, 1839 * they do not have 8-byte alignments, etc), so we have a separate function to 1840 * dump LPT area LEBs. Note, LPT has to be locked by the caller. 1841 */ 1842 static void dump_lpt_leb(const struct ubifs_info *c, int lnum) 1843 { 1844 int err, len = c->leb_size, node_type, node_num, node_len, offs; 1845 void *buf = c->dbg->buf; 1846 1847 printk(KERN_DEBUG "(pid %d) start dumping LEB %d\n", 1848 current->pid, lnum); 1849 err = ubi_read(c->ubi, lnum, buf, 0, c->leb_size); 1850 if (err) { 1851 ubifs_err("cannot read LEB %d, error %d", lnum, err); 1852 return; 1853 } 1854 while (1) { 1855 offs = c->leb_size - len; 1856 if (!is_a_node(c, buf, len)) { 1857 int pad_len; 1858 1859 pad_len = get_pad_len(c, buf, len); 1860 if (pad_len) { 1861 printk(KERN_DEBUG "LEB %d:%d, pad %d bytes\n", 1862 lnum, offs, pad_len); 1863 buf += pad_len; 1864 len -= pad_len; 1865 continue; 1866 } 1867 if (len) 1868 printk(KERN_DEBUG "LEB %d:%d, free %d bytes\n", 1869 lnum, offs, len); 1870 break; 1871 } 1872 1873 node_type = get_lpt_node_type(c, buf, &node_num); 1874 switch (node_type) { 1875 case UBIFS_LPT_PNODE: 1876 { 1877 node_len = c->pnode_sz; 1878 if (c->big_lpt) 1879 printk(KERN_DEBUG "LEB %d:%d, pnode num %d\n", 1880 lnum, offs, node_num); 1881 else 1882 printk(KERN_DEBUG "LEB %d:%d, pnode\n", 1883 lnum, offs); 1884 break; 1885 } 1886 case UBIFS_LPT_NNODE: 1887 { 1888 int i; 1889 struct ubifs_nnode nnode; 1890 1891 node_len = c->nnode_sz; 1892 if (c->big_lpt) 1893 printk(KERN_DEBUG "LEB %d:%d, nnode num %d, ", 1894 lnum, offs, node_num); 1895 else 1896 printk(KERN_DEBUG "LEB %d:%d, nnode, ", 1897 lnum, offs); 1898 err = ubifs_unpack_nnode(c, buf, &nnode); 1899 for (i = 0; i < UBIFS_LPT_FANOUT; i++) { 1900 printk("%d:%d", nnode.nbranch[i].lnum, 1901 nnode.nbranch[i].offs); 1902 if (i != UBIFS_LPT_FANOUT - 1) 1903 printk(", "); 1904 } 1905 printk("\n"); 1906 break; 1907 } 1908 case UBIFS_LPT_LTAB: 1909 node_len = c->ltab_sz; 1910 printk(KERN_DEBUG "LEB %d:%d, ltab\n", 1911 lnum, offs); 1912 break; 1913 case UBIFS_LPT_LSAVE: 1914 node_len = c->lsave_sz; 1915 printk(KERN_DEBUG "LEB %d:%d, lsave len\n", lnum, offs); 1916 break; 1917 default: 1918 ubifs_err("LPT node type %d not recognized", node_type); 1919 return; 1920 } 1921 1922 buf += node_len; 1923 len -= node_len; 1924 } 1925 1926 printk(KERN_DEBUG "(pid %d) finish dumping LEB %d\n", 1927 current->pid, lnum); 1928 } 1929 1930 /** 1931 * dbg_dump_lpt_lebs - dump LPT lebs. 1932 * @c: UBIFS file-system description object 1933 * 1934 * This function dumps all LPT LEBs. The caller has to make sure the LPT is 1935 * locked. 1936 */ 1937 void dbg_dump_lpt_lebs(const struct ubifs_info *c) 1938 { 1939 int i; 1940 1941 printk(KERN_DEBUG "(pid %d) start dumping all LPT LEBs\n", 1942 current->pid); 1943 for (i = 0; i < c->lpt_lebs; i++) 1944 dump_lpt_leb(c, i + c->lpt_first); 1945 printk(KERN_DEBUG "(pid %d) finish dumping all LPT LEBs\n", 1946 current->pid); 1947 } 1948 1949 #endif /* CONFIG_UBIFS_FS_DEBUG */ 1950