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