Linux Audio

Check our new training course

Loading...
v3.15
   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 */
  58static 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 */
 137int 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("too few LPT LEBs");
 149		return -EINVAL;
 150	}
 151
 152	/* Verify that ltab fits in a single LEB (since ltab is a single node */
 153	if (c->ltab_sz > c->leb_size) {
 154		ubifs_err("LPT ltab too big");
 155		return -EINVAL;
 156	}
 157
 158	c->check_lpt_free = c->big_lpt;
 159	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 */
 174static 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("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 */
 233static 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 */
 283uint32_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 */
 350void 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 */
 383void 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 */
 415void 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 */
 440void 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 */
 462void 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 */
 479static 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 */
 494void 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 */
 515static 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 */
 532static 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 */
 558static 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 */
 583static 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 */
 608int 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(sizeof(int) * c->lsave_cnt, 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(sizeof(struct ubifs_lpt_lprops) * c->lpt_lebs);
 636	if (!pnode || !nnode || !buf || !ltab || !lsave) {
 637		err = -ENOMEM;
 638		goto out;
 639	}
 640
 641	ubifs_assert(!c->ltab);
 642	c->ltab = ltab; /* Needed by set_ltab */
 643
 644	/* Initialize LPT's own lprops */
 645	for (i = 0; i < c->lpt_lebs; i++) {
 646		ltab[i].free = c->leb_size;
 647		ltab[i].dirty = 0;
 648		ltab[i].tgc = 0;
 649		ltab[i].cmt = 0;
 650	}
 651
 652	lnum = lpt_first;
 653	p = buf;
 654	/* Number of leaf nodes (pnodes) */
 655	cnt = c->pnode_cnt;
 656
 657	/*
 658	 * The first pnode contains the LEB properties for the LEBs that contain
 659	 * the root inode node and the root index node of the index tree.
 660	 */
 661	node_sz = ALIGN(ubifs_idx_node_sz(c, 1), 8);
 662	iopos = ALIGN(node_sz, c->min_io_size);
 663	pnode->lprops[0].free = c->leb_size - iopos;
 664	pnode->lprops[0].dirty = iopos - node_sz;
 665	pnode->lprops[0].flags = LPROPS_INDEX;
 666
 667	node_sz = UBIFS_INO_NODE_SZ;
 668	iopos = ALIGN(node_sz, c->min_io_size);
 669	pnode->lprops[1].free = c->leb_size - iopos;
 670	pnode->lprops[1].dirty = iopos - node_sz;
 671
 672	for (i = 2; i < UBIFS_LPT_FANOUT; i++)
 673		pnode->lprops[i].free = c->leb_size;
 674
 675	/* Add first pnode */
 676	ubifs_pack_pnode(c, p, pnode);
 677	p += c->pnode_sz;
 678	len = c->pnode_sz;
 679	pnode->num += 1;
 680
 681	/* Reset pnode values for remaining pnodes */
 682	pnode->lprops[0].free = c->leb_size;
 683	pnode->lprops[0].dirty = 0;
 684	pnode->lprops[0].flags = 0;
 685
 686	pnode->lprops[1].free = c->leb_size;
 687	pnode->lprops[1].dirty = 0;
 688
 689	/*
 690	 * To calculate the internal node branches, we keep information about
 691	 * the level below.
 692	 */
 693	blnum = lnum; /* LEB number of level below */
 694	boffs = 0; /* Offset of level below */
 695	bcnt = cnt; /* Number of nodes in level below */
 696	bsz = c->pnode_sz; /* Size of nodes in level below */
 697
 698	/* Add all remaining pnodes */
 699	for (i = 1; i < cnt; i++) {
 700		if (len + c->pnode_sz > c->leb_size) {
 701			alen = ALIGN(len, c->min_io_size);
 702			set_ltab(c, lnum, c->leb_size - alen, alen - len);
 703			memset(p, 0xff, alen - len);
 704			err = ubifs_leb_change(c, lnum++, buf, alen);
 705			if (err)
 706				goto out;
 707			p = buf;
 708			len = 0;
 709		}
 710		ubifs_pack_pnode(c, p, pnode);
 711		p += c->pnode_sz;
 712		len += c->pnode_sz;
 713		/*
 714		 * pnodes are simply numbered left to right starting at zero,
 715		 * which means the pnode number can be used easily to traverse
 716		 * down the tree to the corresponding pnode.
 717		 */
 718		pnode->num += 1;
 719	}
 720
 721	row = 0;
 722	for (i = UBIFS_LPT_FANOUT; cnt > i; i <<= UBIFS_LPT_FANOUT_SHIFT)
 723		row += 1;
 724	/* Add all nnodes, one level at a time */
 725	while (1) {
 726		/* Number of internal nodes (nnodes) at next level */
 727		cnt = DIV_ROUND_UP(cnt, UBIFS_LPT_FANOUT);
 728		for (i = 0; i < cnt; i++) {
 729			if (len + c->nnode_sz > c->leb_size) {
 730				alen = ALIGN(len, c->min_io_size);
 731				set_ltab(c, lnum, c->leb_size - alen,
 732					    alen - len);
 733				memset(p, 0xff, alen - len);
 734				err = ubifs_leb_change(c, lnum++, buf, alen);
 735				if (err)
 736					goto out;
 737				p = buf;
 738				len = 0;
 739			}
 740			/* Only 1 nnode at this level, so it is the root */
 741			if (cnt == 1) {
 742				c->lpt_lnum = lnum;
 743				c->lpt_offs = len;
 744			}
 745			/* Set branches to the level below */
 746			for (j = 0; j < UBIFS_LPT_FANOUT; j++) {
 747				if (bcnt) {
 748					if (boffs + bsz > c->leb_size) {
 749						blnum += 1;
 750						boffs = 0;
 751					}
 752					nnode->nbranch[j].lnum = blnum;
 753					nnode->nbranch[j].offs = boffs;
 754					boffs += bsz;
 755					bcnt--;
 756				} else {
 757					nnode->nbranch[j].lnum = 0;
 758					nnode->nbranch[j].offs = 0;
 759				}
 760			}
 761			nnode->num = calc_nnode_num(row, i);
 762			ubifs_pack_nnode(c, p, nnode);
 763			p += c->nnode_sz;
 764			len += c->nnode_sz;
 765		}
 766		/* Only 1 nnode at this level, so it is the root */
 767		if (cnt == 1)
 768			break;
 769		/* Update the information about the level below */
 770		bcnt = cnt;
 771		bsz = c->nnode_sz;
 772		row -= 1;
 773	}
 774
 775	if (*big_lpt) {
 776		/* Need to add LPT's save table */
 777		if (len + c->lsave_sz > c->leb_size) {
 778			alen = ALIGN(len, c->min_io_size);
 779			set_ltab(c, lnum, c->leb_size - alen, alen - len);
 780			memset(p, 0xff, alen - len);
 781			err = ubifs_leb_change(c, lnum++, buf, alen);
 782			if (err)
 783				goto out;
 784			p = buf;
 785			len = 0;
 786		}
 787
 788		c->lsave_lnum = lnum;
 789		c->lsave_offs = len;
 790
 791		for (i = 0; i < c->lsave_cnt && i < *main_lebs; i++)
 792			lsave[i] = c->main_first + i;
 793		for (; i < c->lsave_cnt; i++)
 794			lsave[i] = c->main_first;
 795
 796		ubifs_pack_lsave(c, p, lsave);
 797		p += c->lsave_sz;
 798		len += c->lsave_sz;
 799	}
 800
 801	/* Need to add LPT's own LEB properties table */
 802	if (len + c->ltab_sz > c->leb_size) {
 803		alen = ALIGN(len, c->min_io_size);
 804		set_ltab(c, lnum, c->leb_size - alen, alen - len);
 805		memset(p, 0xff, alen - len);
 806		err = ubifs_leb_change(c, lnum++, buf, alen);
 807		if (err)
 808			goto out;
 809		p = buf;
 810		len = 0;
 811	}
 812
 813	c->ltab_lnum = lnum;
 814	c->ltab_offs = len;
 815
 816	/* Update ltab before packing it */
 817	len += c->ltab_sz;
 818	alen = ALIGN(len, c->min_io_size);
 819	set_ltab(c, lnum, c->leb_size - alen, alen - len);
 820
 821	ubifs_pack_ltab(c, p, ltab);
 822	p += c->ltab_sz;
 823
 824	/* Write remaining buffer */
 825	memset(p, 0xff, alen - len);
 826	err = ubifs_leb_change(c, lnum, buf, alen);
 827	if (err)
 828		goto out;
 829
 830	c->nhead_lnum = lnum;
 831	c->nhead_offs = ALIGN(len, c->min_io_size);
 832
 833	dbg_lp("space_bits %d", c->space_bits);
 834	dbg_lp("lpt_lnum_bits %d", c->lpt_lnum_bits);
 835	dbg_lp("lpt_offs_bits %d", c->lpt_offs_bits);
 836	dbg_lp("lpt_spc_bits %d", c->lpt_spc_bits);
 837	dbg_lp("pcnt_bits %d", c->pcnt_bits);
 838	dbg_lp("lnum_bits %d", c->lnum_bits);
 839	dbg_lp("pnode_sz %d", c->pnode_sz);
 840	dbg_lp("nnode_sz %d", c->nnode_sz);
 841	dbg_lp("ltab_sz %d", c->ltab_sz);
 842	dbg_lp("lsave_sz %d", c->lsave_sz);
 843	dbg_lp("lsave_cnt %d", c->lsave_cnt);
 844	dbg_lp("lpt_hght %d", c->lpt_hght);
 845	dbg_lp("big_lpt %d", c->big_lpt);
 846	dbg_lp("LPT root is at %d:%d", c->lpt_lnum, c->lpt_offs);
 847	dbg_lp("LPT head is at %d:%d", c->nhead_lnum, c->nhead_offs);
 848	dbg_lp("LPT ltab is at %d:%d", c->ltab_lnum, c->ltab_offs);
 849	if (c->big_lpt)
 850		dbg_lp("LPT lsave is at %d:%d", c->lsave_lnum, c->lsave_offs);
 851out:
 852	c->ltab = NULL;
 853	kfree(lsave);
 854	vfree(ltab);
 855	vfree(buf);
 856	kfree(nnode);
 857	kfree(pnode);
 858	return err;
 859}
 860
 861/**
 862 * update_cats - add LEB properties of a pnode to LEB category lists and heaps.
 863 * @c: UBIFS file-system description object
 864 * @pnode: pnode
 865 *
 866 * When a pnode is loaded into memory, the LEB properties it contains are added,
 867 * by this function, to the LEB category lists and heaps.
 868 */
 869static void update_cats(struct ubifs_info *c, struct ubifs_pnode *pnode)
 870{
 871	int i;
 872
 873	for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
 874		int cat = pnode->lprops[i].flags & LPROPS_CAT_MASK;
 875		int lnum = pnode->lprops[i].lnum;
 876
 877		if (!lnum)
 878			return;
 879		ubifs_add_to_cat(c, &pnode->lprops[i], cat);
 880	}
 881}
 882
 883/**
 884 * replace_cats - add LEB properties of a pnode to LEB category lists and heaps.
 885 * @c: UBIFS file-system description object
 886 * @old_pnode: pnode copied
 887 * @new_pnode: pnode copy
 888 *
 889 * During commit it is sometimes necessary to copy a pnode
 890 * (see dirty_cow_pnode).  When that happens, references in
 891 * category lists and heaps must be replaced.  This function does that.
 892 */
 893static void replace_cats(struct ubifs_info *c, struct ubifs_pnode *old_pnode,
 894			 struct ubifs_pnode *new_pnode)
 895{
 896	int i;
 897
 898	for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
 899		if (!new_pnode->lprops[i].lnum)
 900			return;
 901		ubifs_replace_cat(c, &old_pnode->lprops[i],
 902				  &new_pnode->lprops[i]);
 903	}
 904}
 905
 906/**
 907 * check_lpt_crc - check LPT node crc is correct.
 908 * @c: UBIFS file-system description object
 909 * @buf: buffer containing node
 910 * @len: length of node
 911 *
 912 * This function returns %0 on success and a negative error code on failure.
 913 */
 914static int check_lpt_crc(void *buf, int len)
 915{
 916	int pos = 0;
 917	uint8_t *addr = buf;
 918	uint16_t crc, calc_crc;
 919
 920	crc = ubifs_unpack_bits(&addr, &pos, UBIFS_LPT_CRC_BITS);
 921	calc_crc = crc16(-1, buf + UBIFS_LPT_CRC_BYTES,
 922			 len - UBIFS_LPT_CRC_BYTES);
 923	if (crc != calc_crc) {
 924		ubifs_err("invalid crc in LPT node: crc %hx calc %hx", crc,
 925			  calc_crc);
 926		dump_stack();
 927		return -EINVAL;
 928	}
 929	return 0;
 930}
 931
 932/**
 933 * check_lpt_type - check LPT node type is correct.
 934 * @c: UBIFS file-system description object
 935 * @addr: address of type bit field is passed and returned updated here
 936 * @pos: position of type bit field is passed and returned updated here
 937 * @type: expected type
 938 *
 939 * This function returns %0 on success and a negative error code on failure.
 940 */
 941static int check_lpt_type(uint8_t **addr, int *pos, int type)
 
 942{
 943	int node_type;
 944
 945	node_type = ubifs_unpack_bits(addr, pos, UBIFS_LPT_TYPE_BITS);
 946	if (node_type != type) {
 947		ubifs_err("invalid type (%d) in LPT node type %d", node_type,
 948			  type);
 949		dump_stack();
 950		return -EINVAL;
 951	}
 952	return 0;
 953}
 954
 955/**
 956 * unpack_pnode - unpack a pnode.
 957 * @c: UBIFS file-system description object
 958 * @buf: buffer containing packed pnode to unpack
 959 * @pnode: pnode structure to fill
 960 *
 961 * This function returns %0 on success and a negative error code on failure.
 962 */
 963static int unpack_pnode(const struct ubifs_info *c, void *buf,
 964			struct ubifs_pnode *pnode)
 965{
 966	uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
 967	int i, pos = 0, err;
 968
 969	err = check_lpt_type(&addr, &pos, UBIFS_LPT_PNODE);
 970	if (err)
 971		return err;
 972	if (c->big_lpt)
 973		pnode->num = ubifs_unpack_bits(&addr, &pos, c->pcnt_bits);
 974	for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
 975		struct ubifs_lprops * const lprops = &pnode->lprops[i];
 976
 977		lprops->free = ubifs_unpack_bits(&addr, &pos, c->space_bits);
 978		lprops->free <<= 3;
 979		lprops->dirty = ubifs_unpack_bits(&addr, &pos, c->space_bits);
 980		lprops->dirty <<= 3;
 981
 982		if (ubifs_unpack_bits(&addr, &pos, 1))
 983			lprops->flags = LPROPS_INDEX;
 984		else
 985			lprops->flags = 0;
 986		lprops->flags |= ubifs_categorize_lprops(c, lprops);
 987	}
 988	err = check_lpt_crc(buf, c->pnode_sz);
 989	return err;
 990}
 991
 992/**
 993 * ubifs_unpack_nnode - unpack a nnode.
 994 * @c: UBIFS file-system description object
 995 * @buf: buffer containing packed nnode to unpack
 996 * @nnode: nnode structure to fill
 997 *
 998 * This function returns %0 on success and a negative error code on failure.
 999 */
1000int ubifs_unpack_nnode(const struct ubifs_info *c, void *buf,
1001		       struct ubifs_nnode *nnode)
1002{
1003	uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
1004	int i, pos = 0, err;
1005
1006	err = check_lpt_type(&addr, &pos, UBIFS_LPT_NNODE);
1007	if (err)
1008		return err;
1009	if (c->big_lpt)
1010		nnode->num = ubifs_unpack_bits(&addr, &pos, c->pcnt_bits);
1011	for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
1012		int lnum;
1013
1014		lnum = ubifs_unpack_bits(&addr, &pos, c->lpt_lnum_bits) +
1015		       c->lpt_first;
1016		if (lnum == c->lpt_last + 1)
1017			lnum = 0;
1018		nnode->nbranch[i].lnum = lnum;
1019		nnode->nbranch[i].offs = ubifs_unpack_bits(&addr, &pos,
1020						     c->lpt_offs_bits);
1021	}
1022	err = check_lpt_crc(buf, c->nnode_sz);
1023	return err;
1024}
1025
1026/**
1027 * unpack_ltab - unpack the LPT's own lprops table.
1028 * @c: UBIFS file-system description object
1029 * @buf: buffer from which to unpack
1030 *
1031 * This function returns %0 on success and a negative error code on failure.
1032 */
1033static int unpack_ltab(const struct ubifs_info *c, void *buf)
1034{
1035	uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
1036	int i, pos = 0, err;
1037
1038	err = check_lpt_type(&addr, &pos, UBIFS_LPT_LTAB);
1039	if (err)
1040		return err;
1041	for (i = 0; i < c->lpt_lebs; i++) {
1042		int free = ubifs_unpack_bits(&addr, &pos, c->lpt_spc_bits);
1043		int dirty = ubifs_unpack_bits(&addr, &pos, c->lpt_spc_bits);
1044
1045		if (free < 0 || free > c->leb_size || dirty < 0 ||
1046		    dirty > c->leb_size || free + dirty > c->leb_size)
1047			return -EINVAL;
1048
1049		c->ltab[i].free = free;
1050		c->ltab[i].dirty = dirty;
1051		c->ltab[i].tgc = 0;
1052		c->ltab[i].cmt = 0;
1053	}
1054	err = check_lpt_crc(buf, c->ltab_sz);
1055	return err;
1056}
1057
1058/**
1059 * unpack_lsave - unpack the LPT's save table.
1060 * @c: UBIFS file-system description object
1061 * @buf: buffer from which to unpack
1062 *
1063 * This function returns %0 on success and a negative error code on failure.
1064 */
1065static int unpack_lsave(const struct ubifs_info *c, void *buf)
1066{
1067	uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
1068	int i, pos = 0, err;
1069
1070	err = check_lpt_type(&addr, &pos, UBIFS_LPT_LSAVE);
1071	if (err)
1072		return err;
1073	for (i = 0; i < c->lsave_cnt; i++) {
1074		int lnum = ubifs_unpack_bits(&addr, &pos, c->lnum_bits);
1075
1076		if (lnum < c->main_first || lnum >= c->leb_cnt)
1077			return -EINVAL;
1078		c->lsave[i] = lnum;
1079	}
1080	err = check_lpt_crc(buf, c->lsave_sz);
1081	return err;
1082}
1083
1084/**
1085 * validate_nnode - validate a nnode.
1086 * @c: UBIFS file-system description object
1087 * @nnode: nnode to validate
1088 * @parent: parent nnode (or NULL for the root nnode)
1089 * @iip: index in parent
1090 *
1091 * This function returns %0 on success and a negative error code on failure.
1092 */
1093static int validate_nnode(const struct ubifs_info *c, struct ubifs_nnode *nnode,
1094			  struct ubifs_nnode *parent, int iip)
1095{
1096	int i, lvl, max_offs;
1097
1098	if (c->big_lpt) {
1099		int num = calc_nnode_num_from_parent(c, parent, iip);
1100
1101		if (nnode->num != num)
1102			return -EINVAL;
1103	}
1104	lvl = parent ? parent->level - 1 : c->lpt_hght;
1105	if (lvl < 1)
1106		return -EINVAL;
1107	if (lvl == 1)
1108		max_offs = c->leb_size - c->pnode_sz;
1109	else
1110		max_offs = c->leb_size - c->nnode_sz;
1111	for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
1112		int lnum = nnode->nbranch[i].lnum;
1113		int offs = nnode->nbranch[i].offs;
1114
1115		if (lnum == 0) {
1116			if (offs != 0)
1117				return -EINVAL;
1118			continue;
1119		}
1120		if (lnum < c->lpt_first || lnum > c->lpt_last)
1121			return -EINVAL;
1122		if (offs < 0 || offs > max_offs)
1123			return -EINVAL;
1124	}
1125	return 0;
1126}
1127
1128/**
1129 * validate_pnode - validate a pnode.
1130 * @c: UBIFS file-system description object
1131 * @pnode: pnode to validate
1132 * @parent: parent nnode
1133 * @iip: index in parent
1134 *
1135 * This function returns %0 on success and a negative error code on failure.
1136 */
1137static int validate_pnode(const struct ubifs_info *c, struct ubifs_pnode *pnode,
1138			  struct ubifs_nnode *parent, int iip)
1139{
1140	int i;
1141
1142	if (c->big_lpt) {
1143		int num = calc_pnode_num_from_parent(c, parent, iip);
1144
1145		if (pnode->num != num)
1146			return -EINVAL;
1147	}
1148	for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
1149		int free = pnode->lprops[i].free;
1150		int dirty = pnode->lprops[i].dirty;
1151
1152		if (free < 0 || free > c->leb_size || free % c->min_io_size ||
1153		    (free & 7))
1154			return -EINVAL;
1155		if (dirty < 0 || dirty > c->leb_size || (dirty & 7))
1156			return -EINVAL;
1157		if (dirty + free > c->leb_size)
1158			return -EINVAL;
1159	}
1160	return 0;
1161}
1162
1163/**
1164 * set_pnode_lnum - set LEB numbers on a pnode.
1165 * @c: UBIFS file-system description object
1166 * @pnode: pnode to update
1167 *
1168 * This function calculates the LEB numbers for the LEB properties it contains
1169 * based on the pnode number.
1170 */
1171static void set_pnode_lnum(const struct ubifs_info *c,
1172			   struct ubifs_pnode *pnode)
1173{
1174	int i, lnum;
1175
1176	lnum = (pnode->num << UBIFS_LPT_FANOUT_SHIFT) + c->main_first;
1177	for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
1178		if (lnum >= c->leb_cnt)
1179			return;
1180		pnode->lprops[i].lnum = lnum++;
1181	}
1182}
1183
1184/**
1185 * ubifs_read_nnode - read a nnode from flash and link it to the tree in memory.
1186 * @c: UBIFS file-system description object
1187 * @parent: parent nnode (or NULL for the root)
1188 * @iip: index in parent
1189 *
1190 * This function returns %0 on success and a negative error code on failure.
1191 */
1192int ubifs_read_nnode(struct ubifs_info *c, struct ubifs_nnode *parent, int iip)
1193{
1194	struct ubifs_nbranch *branch = NULL;
1195	struct ubifs_nnode *nnode = NULL;
1196	void *buf = c->lpt_nod_buf;
1197	int err, lnum, offs;
1198
1199	if (parent) {
1200		branch = &parent->nbranch[iip];
1201		lnum = branch->lnum;
1202		offs = branch->offs;
1203	} else {
1204		lnum = c->lpt_lnum;
1205		offs = c->lpt_offs;
1206	}
1207	nnode = kzalloc(sizeof(struct ubifs_nnode), GFP_NOFS);
1208	if (!nnode) {
1209		err = -ENOMEM;
1210		goto out;
1211	}
1212	if (lnum == 0) {
1213		/*
1214		 * This nnode was not written which just means that the LEB
1215		 * properties in the subtree below it describe empty LEBs. We
1216		 * make the nnode as though we had read it, which in fact means
1217		 * doing almost nothing.
1218		 */
1219		if (c->big_lpt)
1220			nnode->num = calc_nnode_num_from_parent(c, parent, iip);
1221	} else {
1222		err = ubifs_leb_read(c, lnum, buf, offs, c->nnode_sz, 1);
1223		if (err)
1224			goto out;
1225		err = ubifs_unpack_nnode(c, buf, nnode);
1226		if (err)
1227			goto out;
1228	}
1229	err = validate_nnode(c, nnode, parent, iip);
1230	if (err)
1231		goto out;
1232	if (!c->big_lpt)
1233		nnode->num = calc_nnode_num_from_parent(c, parent, iip);
1234	if (parent) {
1235		branch->nnode = nnode;
1236		nnode->level = parent->level - 1;
1237	} else {
1238		c->nroot = nnode;
1239		nnode->level = c->lpt_hght;
1240	}
1241	nnode->parent = parent;
1242	nnode->iip = iip;
1243	return 0;
1244
1245out:
1246	ubifs_err("error %d reading nnode at %d:%d", err, lnum, offs);
1247	dump_stack();
1248	kfree(nnode);
1249	return err;
1250}
1251
1252/**
1253 * read_pnode - read a pnode from flash and link it to the tree in memory.
1254 * @c: UBIFS file-system description object
1255 * @parent: parent nnode
1256 * @iip: index in parent
1257 *
1258 * This function returns %0 on success and a negative error code on failure.
1259 */
1260static int read_pnode(struct ubifs_info *c, struct ubifs_nnode *parent, int iip)
1261{
1262	struct ubifs_nbranch *branch;
1263	struct ubifs_pnode *pnode = NULL;
1264	void *buf = c->lpt_nod_buf;
1265	int err, lnum, offs;
1266
1267	branch = &parent->nbranch[iip];
1268	lnum = branch->lnum;
1269	offs = branch->offs;
1270	pnode = kzalloc(sizeof(struct ubifs_pnode), GFP_NOFS);
1271	if (!pnode)
1272		return -ENOMEM;
1273
1274	if (lnum == 0) {
1275		/*
1276		 * This pnode was not written which just means that the LEB
1277		 * properties in it describe empty LEBs. We make the pnode as
1278		 * though we had read it.
1279		 */
1280		int i;
1281
1282		if (c->big_lpt)
1283			pnode->num = calc_pnode_num_from_parent(c, parent, iip);
1284		for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
1285			struct ubifs_lprops * const lprops = &pnode->lprops[i];
1286
1287			lprops->free = c->leb_size;
1288			lprops->flags = ubifs_categorize_lprops(c, lprops);
1289		}
1290	} else {
1291		err = ubifs_leb_read(c, lnum, buf, offs, c->pnode_sz, 1);
1292		if (err)
1293			goto out;
1294		err = unpack_pnode(c, buf, pnode);
1295		if (err)
1296			goto out;
1297	}
1298	err = validate_pnode(c, pnode, parent, iip);
1299	if (err)
1300		goto out;
1301	if (!c->big_lpt)
1302		pnode->num = calc_pnode_num_from_parent(c, parent, iip);
1303	branch->pnode = pnode;
1304	pnode->parent = parent;
1305	pnode->iip = iip;
1306	set_pnode_lnum(c, pnode);
1307	c->pnodes_have += 1;
1308	return 0;
1309
1310out:
1311	ubifs_err("error %d reading pnode at %d:%d", err, lnum, offs);
1312	ubifs_dump_pnode(c, pnode, parent, iip);
1313	dump_stack();
1314	ubifs_err("calc num: %d", calc_pnode_num_from_parent(c, parent, iip));
1315	kfree(pnode);
1316	return err;
1317}
1318
1319/**
1320 * read_ltab - read LPT's own lprops table.
1321 * @c: UBIFS file-system description object
1322 *
1323 * This function returns %0 on success and a negative error code on failure.
1324 */
1325static int read_ltab(struct ubifs_info *c)
1326{
1327	int err;
1328	void *buf;
1329
1330	buf = vmalloc(c->ltab_sz);
1331	if (!buf)
1332		return -ENOMEM;
1333	err = ubifs_leb_read(c, c->ltab_lnum, buf, c->ltab_offs, c->ltab_sz, 1);
1334	if (err)
1335		goto out;
1336	err = unpack_ltab(c, buf);
1337out:
1338	vfree(buf);
1339	return err;
1340}
1341
1342/**
1343 * read_lsave - read LPT's save table.
1344 * @c: UBIFS file-system description object
1345 *
1346 * This function returns %0 on success and a negative error code on failure.
1347 */
1348static int read_lsave(struct ubifs_info *c)
1349{
1350	int err, i;
1351	void *buf;
1352
1353	buf = vmalloc(c->lsave_sz);
1354	if (!buf)
1355		return -ENOMEM;
1356	err = ubifs_leb_read(c, c->lsave_lnum, buf, c->lsave_offs,
1357			     c->lsave_sz, 1);
1358	if (err)
1359		goto out;
1360	err = unpack_lsave(c, buf);
1361	if (err)
1362		goto out;
1363	for (i = 0; i < c->lsave_cnt; i++) {
1364		int lnum = c->lsave[i];
1365		struct ubifs_lprops *lprops;
1366
1367		/*
1368		 * Due to automatic resizing, the values in the lsave table
1369		 * could be beyond the volume size - just ignore them.
1370		 */
1371		if (lnum >= c->leb_cnt)
1372			continue;
1373		lprops = ubifs_lpt_lookup(c, lnum);
1374		if (IS_ERR(lprops)) {
1375			err = PTR_ERR(lprops);
1376			goto out;
1377		}
1378	}
1379out:
1380	vfree(buf);
1381	return err;
1382}
1383
1384/**
1385 * ubifs_get_nnode - get a nnode.
1386 * @c: UBIFS file-system description object
1387 * @parent: parent nnode (or NULL for the root)
1388 * @iip: index in parent
1389 *
1390 * This function returns a pointer to the nnode on success or a negative error
1391 * code on failure.
1392 */
1393struct ubifs_nnode *ubifs_get_nnode(struct ubifs_info *c,
1394				    struct ubifs_nnode *parent, int iip)
1395{
1396	struct ubifs_nbranch *branch;
1397	struct ubifs_nnode *nnode;
1398	int err;
1399
1400	branch = &parent->nbranch[iip];
1401	nnode = branch->nnode;
1402	if (nnode)
1403		return nnode;
1404	err = ubifs_read_nnode(c, parent, iip);
1405	if (err)
1406		return ERR_PTR(err);
1407	return branch->nnode;
1408}
1409
1410/**
1411 * ubifs_get_pnode - get a pnode.
1412 * @c: UBIFS file-system description object
1413 * @parent: parent nnode
1414 * @iip: index in parent
1415 *
1416 * This function returns a pointer to the pnode on success or a negative error
1417 * code on failure.
1418 */
1419struct ubifs_pnode *ubifs_get_pnode(struct ubifs_info *c,
1420				    struct ubifs_nnode *parent, int iip)
1421{
1422	struct ubifs_nbranch *branch;
1423	struct ubifs_pnode *pnode;
1424	int err;
1425
1426	branch = &parent->nbranch[iip];
1427	pnode = branch->pnode;
1428	if (pnode)
1429		return pnode;
1430	err = read_pnode(c, parent, iip);
1431	if (err)
1432		return ERR_PTR(err);
1433	update_cats(c, branch->pnode);
1434	return branch->pnode;
1435}
1436
1437/**
1438 * ubifs_lpt_lookup - lookup LEB properties in the LPT.
1439 * @c: UBIFS file-system description object
1440 * @lnum: LEB number to lookup
1441 *
1442 * This function returns a pointer to the LEB properties on success or a
1443 * negative error code on failure.
1444 */
1445struct ubifs_lprops *ubifs_lpt_lookup(struct ubifs_info *c, int lnum)
1446{
1447	int err, i, h, iip, shft;
1448	struct ubifs_nnode *nnode;
1449	struct ubifs_pnode *pnode;
1450
1451	if (!c->nroot) {
1452		err = ubifs_read_nnode(c, NULL, 0);
1453		if (err)
1454			return ERR_PTR(err);
1455	}
1456	nnode = c->nroot;
1457	i = lnum - c->main_first;
1458	shft = c->lpt_hght * UBIFS_LPT_FANOUT_SHIFT;
1459	for (h = 1; h < c->lpt_hght; h++) {
1460		iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
1461		shft -= UBIFS_LPT_FANOUT_SHIFT;
1462		nnode = ubifs_get_nnode(c, nnode, iip);
1463		if (IS_ERR(nnode))
1464			return ERR_CAST(nnode);
1465	}
1466	iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
1467	shft -= UBIFS_LPT_FANOUT_SHIFT;
1468	pnode = ubifs_get_pnode(c, nnode, iip);
1469	if (IS_ERR(pnode))
1470		return ERR_CAST(pnode);
1471	iip = (i & (UBIFS_LPT_FANOUT - 1));
1472	dbg_lp("LEB %d, free %d, dirty %d, flags %d", lnum,
1473	       pnode->lprops[iip].free, pnode->lprops[iip].dirty,
1474	       pnode->lprops[iip].flags);
1475	return &pnode->lprops[iip];
1476}
1477
1478/**
1479 * dirty_cow_nnode - ensure a nnode is not being committed.
1480 * @c: UBIFS file-system description object
1481 * @nnode: nnode to check
1482 *
1483 * Returns dirtied nnode on success or negative error code on failure.
1484 */
1485static struct ubifs_nnode *dirty_cow_nnode(struct ubifs_info *c,
1486					   struct ubifs_nnode *nnode)
1487{
1488	struct ubifs_nnode *n;
1489	int i;
1490
1491	if (!test_bit(COW_CNODE, &nnode->flags)) {
1492		/* nnode is not being committed */
1493		if (!test_and_set_bit(DIRTY_CNODE, &nnode->flags)) {
1494			c->dirty_nn_cnt += 1;
1495			ubifs_add_nnode_dirt(c, nnode);
1496		}
1497		return nnode;
1498	}
1499
1500	/* nnode is being committed, so copy it */
1501	n = kmalloc(sizeof(struct ubifs_nnode), GFP_NOFS);
1502	if (unlikely(!n))
1503		return ERR_PTR(-ENOMEM);
1504
1505	memcpy(n, nnode, sizeof(struct ubifs_nnode));
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 */
1537static 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 = kmalloc(sizeof(struct ubifs_pnode), GFP_NOFS);
1553	if (unlikely(!p))
1554		return ERR_PTR(-ENOMEM);
1555
1556	memcpy(p, pnode, sizeof(struct ubifs_pnode));
1557	p->cnext = NULL;
1558	__set_bit(DIRTY_CNODE, &p->flags);
1559	__clear_bit(COW_CNODE, &p->flags);
1560	replace_cats(c, pnode, p);
1561
1562	ubifs_assert(!test_bit(OBSOLETE_CNODE, &pnode->flags));
1563	__set_bit(OBSOLETE_CNODE, &pnode->flags);
1564
1565	c->dirty_pn_cnt += 1;
1566	add_pnode_dirt(c, pnode);
1567	pnode->parent->nbranch[p->iip].pnode = p;
1568	return p;
1569}
1570
1571/**
1572 * ubifs_lpt_lookup_dirty - lookup LEB properties in the LPT.
1573 * @c: UBIFS file-system description object
1574 * @lnum: LEB number to lookup
1575 *
1576 * This function returns a pointer to the LEB properties on success or a
1577 * negative error code on failure.
1578 */
1579struct ubifs_lprops *ubifs_lpt_lookup_dirty(struct ubifs_info *c, int lnum)
1580{
1581	int err, i, h, iip, shft;
1582	struct ubifs_nnode *nnode;
1583	struct ubifs_pnode *pnode;
1584
1585	if (!c->nroot) {
1586		err = ubifs_read_nnode(c, NULL, 0);
1587		if (err)
1588			return ERR_PTR(err);
1589	}
1590	nnode = c->nroot;
1591	nnode = dirty_cow_nnode(c, nnode);
1592	if (IS_ERR(nnode))
1593		return ERR_CAST(nnode);
1594	i = lnum - c->main_first;
1595	shft = c->lpt_hght * UBIFS_LPT_FANOUT_SHIFT;
1596	for (h = 1; h < c->lpt_hght; h++) {
1597		iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
1598		shft -= UBIFS_LPT_FANOUT_SHIFT;
1599		nnode = ubifs_get_nnode(c, nnode, iip);
1600		if (IS_ERR(nnode))
1601			return ERR_CAST(nnode);
1602		nnode = dirty_cow_nnode(c, nnode);
1603		if (IS_ERR(nnode))
1604			return ERR_CAST(nnode);
1605	}
1606	iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
1607	shft -= UBIFS_LPT_FANOUT_SHIFT;
1608	pnode = ubifs_get_pnode(c, nnode, iip);
1609	if (IS_ERR(pnode))
1610		return ERR_CAST(pnode);
1611	pnode = dirty_cow_pnode(c, pnode);
1612	if (IS_ERR(pnode))
1613		return ERR_CAST(pnode);
1614	iip = (i & (UBIFS_LPT_FANOUT - 1));
1615	dbg_lp("LEB %d, free %d, dirty %d, flags %d", lnum,
1616	       pnode->lprops[iip].free, pnode->lprops[iip].dirty,
1617	       pnode->lprops[iip].flags);
1618	ubifs_assert(test_bit(DIRTY_CNODE, &pnode->flags));
1619	return &pnode->lprops[iip];
1620}
1621
1622/**
1623 * lpt_init_rd - initialize the LPT for reading.
1624 * @c: UBIFS file-system description object
1625 *
1626 * This function returns %0 on success and a negative error code on failure.
1627 */
1628static int lpt_init_rd(struct ubifs_info *c)
1629{
1630	int err, i;
1631
1632	c->ltab = vmalloc(sizeof(struct ubifs_lpt_lprops) * c->lpt_lebs);
1633	if (!c->ltab)
1634		return -ENOMEM;
1635
1636	i = max_t(int, c->nnode_sz, c->pnode_sz);
1637	c->lpt_nod_buf = kmalloc(i, GFP_KERNEL);
1638	if (!c->lpt_nod_buf)
1639		return -ENOMEM;
1640
1641	for (i = 0; i < LPROPS_HEAP_CNT; i++) {
1642		c->lpt_heap[i].arr = kmalloc(sizeof(void *) * LPT_HEAP_SZ,
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(sizeof(void *) * LPT_HEAP_SZ, GFP_KERNEL);
1651	if (!c->dirty_idx.arr)
1652		return -ENOMEM;
1653	c->dirty_idx.cnt = 0;
1654	c->dirty_idx.max_cnt = LPT_HEAP_SZ;
1655
1656	err = read_ltab(c);
1657	if (err)
1658		return err;
1659
1660	dbg_lp("space_bits %d", c->space_bits);
1661	dbg_lp("lpt_lnum_bits %d", c->lpt_lnum_bits);
1662	dbg_lp("lpt_offs_bits %d", c->lpt_offs_bits);
1663	dbg_lp("lpt_spc_bits %d", c->lpt_spc_bits);
1664	dbg_lp("pcnt_bits %d", c->pcnt_bits);
1665	dbg_lp("lnum_bits %d", c->lnum_bits);
1666	dbg_lp("pnode_sz %d", c->pnode_sz);
1667	dbg_lp("nnode_sz %d", c->nnode_sz);
1668	dbg_lp("ltab_sz %d", c->ltab_sz);
1669	dbg_lp("lsave_sz %d", c->lsave_sz);
1670	dbg_lp("lsave_cnt %d", c->lsave_cnt);
1671	dbg_lp("lpt_hght %d", c->lpt_hght);
1672	dbg_lp("big_lpt %d", c->big_lpt);
1673	dbg_lp("LPT root is at %d:%d", c->lpt_lnum, c->lpt_offs);
1674	dbg_lp("LPT head is at %d:%d", c->nhead_lnum, c->nhead_offs);
1675	dbg_lp("LPT ltab is at %d:%d", c->ltab_lnum, c->ltab_offs);
1676	if (c->big_lpt)
1677		dbg_lp("LPT lsave is at %d:%d", c->lsave_lnum, c->lsave_offs);
1678
1679	return 0;
1680}
1681
1682/**
1683 * lpt_init_wr - initialize the LPT for writing.
1684 * @c: UBIFS file-system description object
1685 *
1686 * 'lpt_init_rd()' must have been called already.
1687 *
1688 * This function returns %0 on success and a negative error code on failure.
1689 */
1690static int lpt_init_wr(struct ubifs_info *c)
1691{
1692	int err, i;
1693
1694	c->ltab_cmt = vmalloc(sizeof(struct ubifs_lpt_lprops) * c->lpt_lebs);
1695	if (!c->ltab_cmt)
1696		return -ENOMEM;
1697
1698	c->lpt_buf = vmalloc(c->leb_size);
1699	if (!c->lpt_buf)
1700		return -ENOMEM;
1701
1702	if (c->big_lpt) {
1703		c->lsave = kmalloc(sizeof(int) * c->lsave_cnt, GFP_NOFS);
1704		if (!c->lsave)
1705			return -ENOMEM;
1706		err = read_lsave(c);
1707		if (err)
1708			return err;
1709	}
1710
1711	for (i = 0; i < c->lpt_lebs; i++)
1712		if (c->ltab[i].free == c->leb_size) {
1713			err = ubifs_leb_unmap(c, i + c->lpt_first);
1714			if (err)
1715				return err;
1716		}
1717
1718	return 0;
1719}
1720
1721/**
1722 * ubifs_lpt_init - initialize the LPT.
1723 * @c: UBIFS file-system description object
1724 * @rd: whether to initialize lpt for reading
1725 * @wr: whether to initialize lpt for writing
1726 *
1727 * For mounting 'rw', @rd and @wr are both true. For mounting 'ro', @rd is true
1728 * and @wr is false. For mounting from 'ro' to 'rw', @rd is false and @wr is
1729 * true.
1730 *
1731 * This function returns %0 on success and a negative error code on failure.
1732 */
1733int ubifs_lpt_init(struct ubifs_info *c, int rd, int wr)
1734{
1735	int err;
1736
1737	if (rd) {
1738		err = lpt_init_rd(c);
1739		if (err)
1740			goto out_err;
1741	}
1742
1743	if (wr) {
1744		err = lpt_init_wr(c);
1745		if (err)
1746			goto out_err;
1747	}
1748
1749	return 0;
1750
1751out_err:
1752	if (wr)
1753		ubifs_lpt_free(c, 1);
1754	if (rd)
1755		ubifs_lpt_free(c, 0);
1756	return err;
1757}
1758
1759/**
1760 * struct lpt_scan_node - somewhere to put nodes while we scan LPT.
1761 * @nnode: where to keep a nnode
1762 * @pnode: where to keep a pnode
1763 * @cnode: where to keep a cnode
1764 * @in_tree: is the node in the tree in memory
1765 * @ptr.nnode: pointer to the nnode (if it is an nnode) which may be here or in
1766 * the tree
1767 * @ptr.pnode: ditto for pnode
1768 * @ptr.cnode: ditto for cnode
1769 */
1770struct lpt_scan_node {
1771	union {
1772		struct ubifs_nnode nnode;
1773		struct ubifs_pnode pnode;
1774		struct ubifs_cnode cnode;
1775	};
1776	int in_tree;
1777	union {
1778		struct ubifs_nnode *nnode;
1779		struct ubifs_pnode *pnode;
1780		struct ubifs_cnode *cnode;
1781	} ptr;
1782};
1783
1784/**
1785 * scan_get_nnode - for the scan, get a nnode from either the tree or flash.
1786 * @c: the UBIFS file-system description object
1787 * @path: where to put the nnode
1788 * @parent: parent of the nnode
1789 * @iip: index in parent of the nnode
1790 *
1791 * This function returns a pointer to the nnode on success or a negative error
1792 * code on failure.
1793 */
1794static struct ubifs_nnode *scan_get_nnode(struct ubifs_info *c,
1795					  struct lpt_scan_node *path,
1796					  struct ubifs_nnode *parent, int iip)
1797{
1798	struct ubifs_nbranch *branch;
1799	struct ubifs_nnode *nnode;
1800	void *buf = c->lpt_nod_buf;
1801	int err;
1802
1803	branch = &parent->nbranch[iip];
1804	nnode = branch->nnode;
1805	if (nnode) {
1806		path->in_tree = 1;
1807		path->ptr.nnode = nnode;
1808		return nnode;
1809	}
1810	nnode = &path->nnode;
1811	path->in_tree = 0;
1812	path->ptr.nnode = nnode;
1813	memset(nnode, 0, sizeof(struct ubifs_nnode));
1814	if (branch->lnum == 0) {
1815		/*
1816		 * This nnode was not written which just means that the LEB
1817		 * properties in the subtree below it describe empty LEBs. We
1818		 * make the nnode as though we had read it, which in fact means
1819		 * doing almost nothing.
1820		 */
1821		if (c->big_lpt)
1822			nnode->num = calc_nnode_num_from_parent(c, parent, iip);
1823	} else {
1824		err = ubifs_leb_read(c, branch->lnum, buf, branch->offs,
1825				     c->nnode_sz, 1);
1826		if (err)
1827			return ERR_PTR(err);
1828		err = ubifs_unpack_nnode(c, buf, nnode);
1829		if (err)
1830			return ERR_PTR(err);
1831	}
1832	err = validate_nnode(c, nnode, parent, iip);
1833	if (err)
1834		return ERR_PTR(err);
1835	if (!c->big_lpt)
1836		nnode->num = calc_nnode_num_from_parent(c, parent, iip);
1837	nnode->level = parent->level - 1;
1838	nnode->parent = parent;
1839	nnode->iip = iip;
1840	return nnode;
1841}
1842
1843/**
1844 * scan_get_pnode - for the scan, get a pnode from either the tree or flash.
1845 * @c: the UBIFS file-system description object
1846 * @path: where to put the pnode
1847 * @parent: parent of the pnode
1848 * @iip: index in parent of the pnode
1849 *
1850 * This function returns a pointer to the pnode on success or a negative error
1851 * code on failure.
1852 */
1853static struct ubifs_pnode *scan_get_pnode(struct ubifs_info *c,
1854					  struct lpt_scan_node *path,
1855					  struct ubifs_nnode *parent, int iip)
1856{
1857	struct ubifs_nbranch *branch;
1858	struct ubifs_pnode *pnode;
1859	void *buf = c->lpt_nod_buf;
1860	int err;
1861
1862	branch = &parent->nbranch[iip];
1863	pnode = branch->pnode;
1864	if (pnode) {
1865		path->in_tree = 1;
1866		path->ptr.pnode = pnode;
1867		return pnode;
1868	}
1869	pnode = &path->pnode;
1870	path->in_tree = 0;
1871	path->ptr.pnode = pnode;
1872	memset(pnode, 0, sizeof(struct ubifs_pnode));
1873	if (branch->lnum == 0) {
1874		/*
1875		 * This pnode was not written which just means that the LEB
1876		 * properties in it describe empty LEBs. We make the pnode as
1877		 * though we had read it.
1878		 */
1879		int i;
1880
1881		if (c->big_lpt)
1882			pnode->num = calc_pnode_num_from_parent(c, parent, iip);
1883		for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
1884			struct ubifs_lprops * const lprops = &pnode->lprops[i];
1885
1886			lprops->free = c->leb_size;
1887			lprops->flags = ubifs_categorize_lprops(c, lprops);
1888		}
1889	} else {
1890		ubifs_assert(branch->lnum >= c->lpt_first &&
1891			     branch->lnum <= c->lpt_last);
1892		ubifs_assert(branch->offs >= 0 && branch->offs < c->leb_size);
1893		err = ubifs_leb_read(c, branch->lnum, buf, branch->offs,
1894				     c->pnode_sz, 1);
1895		if (err)
1896			return ERR_PTR(err);
1897		err = unpack_pnode(c, buf, pnode);
1898		if (err)
1899			return ERR_PTR(err);
1900	}
1901	err = validate_pnode(c, pnode, parent, iip);
1902	if (err)
1903		return ERR_PTR(err);
1904	if (!c->big_lpt)
1905		pnode->num = calc_pnode_num_from_parent(c, parent, iip);
1906	pnode->parent = parent;
1907	pnode->iip = iip;
1908	set_pnode_lnum(c, pnode);
1909	return pnode;
1910}
1911
1912/**
1913 * ubifs_lpt_scan_nolock - scan the LPT.
1914 * @c: the UBIFS file-system description object
1915 * @start_lnum: LEB number from which to start scanning
1916 * @end_lnum: LEB number at which to stop scanning
1917 * @scan_cb: callback function called for each lprops
1918 * @data: data to be passed to the callback function
1919 *
1920 * This function returns %0 on success and a negative error code on failure.
1921 */
1922int ubifs_lpt_scan_nolock(struct ubifs_info *c, int start_lnum, int end_lnum,
1923			  ubifs_lpt_scan_callback scan_cb, void *data)
1924{
1925	int err = 0, i, h, iip, shft;
1926	struct ubifs_nnode *nnode;
1927	struct ubifs_pnode *pnode;
1928	struct lpt_scan_node *path;
1929
1930	if (start_lnum == -1) {
1931		start_lnum = end_lnum + 1;
1932		if (start_lnum >= c->leb_cnt)
1933			start_lnum = c->main_first;
1934	}
1935
1936	ubifs_assert(start_lnum >= c->main_first && start_lnum < c->leb_cnt);
1937	ubifs_assert(end_lnum >= c->main_first && end_lnum < c->leb_cnt);
1938
1939	if (!c->nroot) {
1940		err = ubifs_read_nnode(c, NULL, 0);
1941		if (err)
1942			return err;
1943	}
1944
1945	path = kmalloc(sizeof(struct lpt_scan_node) * (c->lpt_hght + 1),
1946		       GFP_NOFS);
1947	if (!path)
1948		return -ENOMEM;
1949
1950	path[0].ptr.nnode = c->nroot;
1951	path[0].in_tree = 1;
1952again:
1953	/* Descend to the pnode containing start_lnum */
1954	nnode = c->nroot;
1955	i = start_lnum - c->main_first;
1956	shft = c->lpt_hght * UBIFS_LPT_FANOUT_SHIFT;
1957	for (h = 1; h < c->lpt_hght; h++) {
1958		iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
1959		shft -= UBIFS_LPT_FANOUT_SHIFT;
1960		nnode = scan_get_nnode(c, path + h, nnode, iip);
1961		if (IS_ERR(nnode)) {
1962			err = PTR_ERR(nnode);
1963			goto out;
1964		}
1965	}
1966	iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
1967	shft -= UBIFS_LPT_FANOUT_SHIFT;
1968	pnode = scan_get_pnode(c, path + h, nnode, iip);
1969	if (IS_ERR(pnode)) {
1970		err = PTR_ERR(pnode);
1971		goto out;
1972	}
1973	iip = (i & (UBIFS_LPT_FANOUT - 1));
1974
1975	/* Loop for each lprops */
1976	while (1) {
1977		struct ubifs_lprops *lprops = &pnode->lprops[iip];
1978		int ret, lnum = lprops->lnum;
1979
1980		ret = scan_cb(c, lprops, path[h].in_tree, data);
1981		if (ret < 0) {
1982			err = ret;
1983			goto out;
1984		}
1985		if (ret & LPT_SCAN_ADD) {
1986			/* Add all the nodes in path to the tree in memory */
1987			for (h = 1; h < c->lpt_hght; h++) {
1988				const size_t sz = sizeof(struct ubifs_nnode);
1989				struct ubifs_nnode *parent;
1990
1991				if (path[h].in_tree)
1992					continue;
1993				nnode = kmemdup(&path[h].nnode, sz, GFP_NOFS);
1994				if (!nnode) {
1995					err = -ENOMEM;
1996					goto out;
1997				}
1998				parent = nnode->parent;
1999				parent->nbranch[nnode->iip].nnode = nnode;
2000				path[h].ptr.nnode = nnode;
2001				path[h].in_tree = 1;
2002				path[h + 1].cnode.parent = nnode;
2003			}
2004			if (path[h].in_tree)
2005				ubifs_ensure_cat(c, lprops);
2006			else {
2007				const size_t sz = sizeof(struct ubifs_pnode);
2008				struct ubifs_nnode *parent;
2009
2010				pnode = kmemdup(&path[h].pnode, sz, GFP_NOFS);
2011				if (!pnode) {
2012					err = -ENOMEM;
2013					goto out;
2014				}
2015				parent = pnode->parent;
2016				parent->nbranch[pnode->iip].pnode = pnode;
2017				path[h].ptr.pnode = pnode;
2018				path[h].in_tree = 1;
2019				update_cats(c, pnode);
2020				c->pnodes_have += 1;
2021			}
2022			err = dbg_check_lpt_nodes(c, (struct ubifs_cnode *)
2023						  c->nroot, 0, 0);
2024			if (err)
2025				goto out;
2026			err = dbg_check_cats(c);
2027			if (err)
2028				goto out;
2029		}
2030		if (ret & LPT_SCAN_STOP) {
2031			err = 0;
2032			break;
2033		}
2034		/* Get the next lprops */
2035		if (lnum == end_lnum) {
2036			/*
2037			 * We got to the end without finding what we were
2038			 * looking for
2039			 */
2040			err = -ENOSPC;
2041			goto out;
2042		}
2043		if (lnum + 1 >= c->leb_cnt) {
2044			/* Wrap-around to the beginning */
2045			start_lnum = c->main_first;
2046			goto again;
2047		}
2048		if (iip + 1 < UBIFS_LPT_FANOUT) {
2049			/* Next lprops is in the same pnode */
2050			iip += 1;
2051			continue;
2052		}
2053		/* We need to get the next pnode. Go up until we can go right */
2054		iip = pnode->iip;
2055		while (1) {
2056			h -= 1;
2057			ubifs_assert(h >= 0);
2058			nnode = path[h].ptr.nnode;
2059			if (iip + 1 < UBIFS_LPT_FANOUT)
2060				break;
2061			iip = nnode->iip;
2062		}
2063		/* Go right */
2064		iip += 1;
2065		/* Descend to the pnode */
2066		h += 1;
2067		for (; h < c->lpt_hght; h++) {
2068			nnode = scan_get_nnode(c, path + h, nnode, iip);
2069			if (IS_ERR(nnode)) {
2070				err = PTR_ERR(nnode);
2071				goto out;
2072			}
2073			iip = 0;
2074		}
2075		pnode = scan_get_pnode(c, path + h, nnode, iip);
2076		if (IS_ERR(pnode)) {
2077			err = PTR_ERR(pnode);
2078			goto out;
2079		}
2080		iip = 0;
2081	}
2082out:
2083	kfree(path);
2084	return err;
2085}
2086
2087/**
2088 * dbg_chk_pnode - check a pnode.
2089 * @c: the UBIFS file-system description object
2090 * @pnode: pnode to check
2091 * @col: pnode column
2092 *
2093 * This function returns %0 on success and a negative error code on failure.
2094 */
2095static int dbg_chk_pnode(struct ubifs_info *c, struct ubifs_pnode *pnode,
2096			 int col)
2097{
2098	int i;
2099
2100	if (pnode->num != col) {
2101		ubifs_err("pnode num %d expected %d parent num %d iip %d",
2102			  pnode->num, col, pnode->parent->num, pnode->iip);
2103		return -EINVAL;
2104	}
2105	for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
2106		struct ubifs_lprops *lp, *lprops = &pnode->lprops[i];
2107		int lnum = (pnode->num << UBIFS_LPT_FANOUT_SHIFT) + i +
2108			   c->main_first;
2109		int found, cat = lprops->flags & LPROPS_CAT_MASK;
2110		struct ubifs_lpt_heap *heap;
2111		struct list_head *list = NULL;
2112
2113		if (lnum >= c->leb_cnt)
2114			continue;
2115		if (lprops->lnum != lnum) {
2116			ubifs_err("bad LEB number %d expected %d",
2117				  lprops->lnum, lnum);
2118			return -EINVAL;
2119		}
2120		if (lprops->flags & LPROPS_TAKEN) {
2121			if (cat != LPROPS_UNCAT) {
2122				ubifs_err("LEB %d taken but not uncat %d",
2123					  lprops->lnum, cat);
2124				return -EINVAL;
2125			}
2126			continue;
2127		}
2128		if (lprops->flags & LPROPS_INDEX) {
2129			switch (cat) {
2130			case LPROPS_UNCAT:
2131			case LPROPS_DIRTY_IDX:
2132			case LPROPS_FRDI_IDX:
2133				break;
2134			default:
2135				ubifs_err("LEB %d index but cat %d",
2136					  lprops->lnum, cat);
2137				return -EINVAL;
2138			}
2139		} else {
2140			switch (cat) {
2141			case LPROPS_UNCAT:
2142			case LPROPS_DIRTY:
2143			case LPROPS_FREE:
2144			case LPROPS_EMPTY:
2145			case LPROPS_FREEABLE:
2146				break;
2147			default:
2148				ubifs_err("LEB %d not index but cat %d",
2149					  lprops->lnum, cat);
2150				return -EINVAL;
2151			}
2152		}
2153		switch (cat) {
2154		case LPROPS_UNCAT:
2155			list = &c->uncat_list;
2156			break;
2157		case LPROPS_EMPTY:
2158			list = &c->empty_list;
2159			break;
2160		case LPROPS_FREEABLE:
2161			list = &c->freeable_list;
2162			break;
2163		case LPROPS_FRDI_IDX:
2164			list = &c->frdi_idx_list;
2165			break;
2166		}
2167		found = 0;
2168		switch (cat) {
2169		case LPROPS_DIRTY:
2170		case LPROPS_DIRTY_IDX:
2171		case LPROPS_FREE:
2172			heap = &c->lpt_heap[cat - 1];
2173			if (lprops->hpos < heap->cnt &&
2174			    heap->arr[lprops->hpos] == lprops)
2175				found = 1;
2176			break;
2177		case LPROPS_UNCAT:
2178		case LPROPS_EMPTY:
2179		case LPROPS_FREEABLE:
2180		case LPROPS_FRDI_IDX:
2181			list_for_each_entry(lp, list, list)
2182				if (lprops == lp) {
2183					found = 1;
2184					break;
2185				}
2186			break;
2187		}
2188		if (!found) {
2189			ubifs_err("LEB %d cat %d not found in cat heap/list",
2190				  lprops->lnum, cat);
2191			return -EINVAL;
2192		}
2193		switch (cat) {
2194		case LPROPS_EMPTY:
2195			if (lprops->free != c->leb_size) {
2196				ubifs_err("LEB %d cat %d free %d dirty %d",
2197					  lprops->lnum, cat, lprops->free,
2198					  lprops->dirty);
2199				return -EINVAL;
2200			}
 
2201		case LPROPS_FREEABLE:
2202		case LPROPS_FRDI_IDX:
2203			if (lprops->free + lprops->dirty != c->leb_size) {
2204				ubifs_err("LEB %d cat %d free %d dirty %d",
2205					  lprops->lnum, cat, lprops->free,
2206					  lprops->dirty);
2207				return -EINVAL;
2208			}
 
2209		}
2210	}
2211	return 0;
2212}
2213
2214/**
2215 * dbg_check_lpt_nodes - check nnodes and pnodes.
2216 * @c: the UBIFS file-system description object
2217 * @cnode: next cnode (nnode or pnode) to check
2218 * @row: row of cnode (root is zero)
2219 * @col: column of cnode (leftmost is zero)
2220 *
2221 * This function returns %0 on success and a negative error code on failure.
2222 */
2223int dbg_check_lpt_nodes(struct ubifs_info *c, struct ubifs_cnode *cnode,
2224			int row, int col)
2225{
2226	struct ubifs_nnode *nnode, *nn;
2227	struct ubifs_cnode *cn;
2228	int num, iip = 0, err;
2229
2230	if (!dbg_is_chk_lprops(c))
2231		return 0;
2232
2233	while (cnode) {
2234		ubifs_assert(row >= 0);
2235		nnode = cnode->parent;
2236		if (cnode->level) {
2237			/* cnode is a nnode */
2238			num = calc_nnode_num(row, col);
2239			if (cnode->num != num) {
2240				ubifs_err("nnode num %d expected %d parent num %d iip %d",
2241					  cnode->num, num,
2242					  (nnode ? nnode->num : 0), cnode->iip);
2243				return -EINVAL;
2244			}
2245			nn = (struct ubifs_nnode *)cnode;
2246			while (iip < UBIFS_LPT_FANOUT) {
2247				cn = nn->nbranch[iip].cnode;
2248				if (cn) {
2249					/* Go down */
2250					row += 1;
2251					col <<= UBIFS_LPT_FANOUT_SHIFT;
2252					col += iip;
2253					iip = 0;
2254					cnode = cn;
2255					break;
2256				}
2257				/* Go right */
2258				iip += 1;
2259			}
2260			if (iip < UBIFS_LPT_FANOUT)
2261				continue;
2262		} else {
2263			struct ubifs_pnode *pnode;
2264
2265			/* cnode is a pnode */
2266			pnode = (struct ubifs_pnode *)cnode;
2267			err = dbg_chk_pnode(c, pnode, col);
2268			if (err)
2269				return err;
2270		}
2271		/* Go up and to the right */
2272		row -= 1;
2273		col >>= UBIFS_LPT_FANOUT_SHIFT;
2274		iip = cnode->iip + 1;
2275		cnode = (struct ubifs_cnode *)nnode;
2276	}
2277	return 0;
2278}
v4.6
   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 */
  58static 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 */
 137int 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 */
 174static 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 */
 233static 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 */
 283uint32_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 */
 350void 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 */
 383void 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 */
 415void 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 */
 440void 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 */
 462void 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 */
 479static 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 */
 494void 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 */
 515static 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 */
 532static 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 */
 558static 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 */
 583static 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 */
 608int 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(sizeof(int) * c->lsave_cnt, 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(sizeof(struct ubifs_lpt_lprops) * c->lpt_lebs);
 636	if (!pnode || !nnode || !buf || !ltab || !lsave) {
 637		err = -ENOMEM;
 638		goto out;
 639	}
 640
 641	ubifs_assert(!c->ltab);
 642	c->ltab = ltab; /* Needed by set_ltab */
 643
 644	/* Initialize LPT's own lprops */
 645	for (i = 0; i < c->lpt_lebs; i++) {
 646		ltab[i].free = c->leb_size;
 647		ltab[i].dirty = 0;
 648		ltab[i].tgc = 0;
 649		ltab[i].cmt = 0;
 650	}
 651
 652	lnum = lpt_first;
 653	p = buf;
 654	/* Number of leaf nodes (pnodes) */
 655	cnt = c->pnode_cnt;
 656
 657	/*
 658	 * The first pnode contains the LEB properties for the LEBs that contain
 659	 * the root inode node and the root index node of the index tree.
 660	 */
 661	node_sz = ALIGN(ubifs_idx_node_sz(c, 1), 8);
 662	iopos = ALIGN(node_sz, c->min_io_size);
 663	pnode->lprops[0].free = c->leb_size - iopos;
 664	pnode->lprops[0].dirty = iopos - node_sz;
 665	pnode->lprops[0].flags = LPROPS_INDEX;
 666
 667	node_sz = UBIFS_INO_NODE_SZ;
 668	iopos = ALIGN(node_sz, c->min_io_size);
 669	pnode->lprops[1].free = c->leb_size - iopos;
 670	pnode->lprops[1].dirty = iopos - node_sz;
 671
 672	for (i = 2; i < UBIFS_LPT_FANOUT; i++)
 673		pnode->lprops[i].free = c->leb_size;
 674
 675	/* Add first pnode */
 676	ubifs_pack_pnode(c, p, pnode);
 677	p += c->pnode_sz;
 678	len = c->pnode_sz;
 679	pnode->num += 1;
 680
 681	/* Reset pnode values for remaining pnodes */
 682	pnode->lprops[0].free = c->leb_size;
 683	pnode->lprops[0].dirty = 0;
 684	pnode->lprops[0].flags = 0;
 685
 686	pnode->lprops[1].free = c->leb_size;
 687	pnode->lprops[1].dirty = 0;
 688
 689	/*
 690	 * To calculate the internal node branches, we keep information about
 691	 * the level below.
 692	 */
 693	blnum = lnum; /* LEB number of level below */
 694	boffs = 0; /* Offset of level below */
 695	bcnt = cnt; /* Number of nodes in level below */
 696	bsz = c->pnode_sz; /* Size of nodes in level below */
 697
 698	/* Add all remaining pnodes */
 699	for (i = 1; i < cnt; i++) {
 700		if (len + c->pnode_sz > c->leb_size) {
 701			alen = ALIGN(len, c->min_io_size);
 702			set_ltab(c, lnum, c->leb_size - alen, alen - len);
 703			memset(p, 0xff, alen - len);
 704			err = ubifs_leb_change(c, lnum++, buf, alen);
 705			if (err)
 706				goto out;
 707			p = buf;
 708			len = 0;
 709		}
 710		ubifs_pack_pnode(c, p, pnode);
 711		p += c->pnode_sz;
 712		len += c->pnode_sz;
 713		/*
 714		 * pnodes are simply numbered left to right starting at zero,
 715		 * which means the pnode number can be used easily to traverse
 716		 * down the tree to the corresponding pnode.
 717		 */
 718		pnode->num += 1;
 719	}
 720
 721	row = 0;
 722	for (i = UBIFS_LPT_FANOUT; cnt > i; i <<= UBIFS_LPT_FANOUT_SHIFT)
 723		row += 1;
 724	/* Add all nnodes, one level at a time */
 725	while (1) {
 726		/* Number of internal nodes (nnodes) at next level */
 727		cnt = DIV_ROUND_UP(cnt, UBIFS_LPT_FANOUT);
 728		for (i = 0; i < cnt; i++) {
 729			if (len + c->nnode_sz > c->leb_size) {
 730				alen = ALIGN(len, c->min_io_size);
 731				set_ltab(c, lnum, c->leb_size - alen,
 732					    alen - len);
 733				memset(p, 0xff, alen - len);
 734				err = ubifs_leb_change(c, lnum++, buf, alen);
 735				if (err)
 736					goto out;
 737				p = buf;
 738				len = 0;
 739			}
 740			/* Only 1 nnode at this level, so it is the root */
 741			if (cnt == 1) {
 742				c->lpt_lnum = lnum;
 743				c->lpt_offs = len;
 744			}
 745			/* Set branches to the level below */
 746			for (j = 0; j < UBIFS_LPT_FANOUT; j++) {
 747				if (bcnt) {
 748					if (boffs + bsz > c->leb_size) {
 749						blnum += 1;
 750						boffs = 0;
 751					}
 752					nnode->nbranch[j].lnum = blnum;
 753					nnode->nbranch[j].offs = boffs;
 754					boffs += bsz;
 755					bcnt--;
 756				} else {
 757					nnode->nbranch[j].lnum = 0;
 758					nnode->nbranch[j].offs = 0;
 759				}
 760			}
 761			nnode->num = calc_nnode_num(row, i);
 762			ubifs_pack_nnode(c, p, nnode);
 763			p += c->nnode_sz;
 764			len += c->nnode_sz;
 765		}
 766		/* Only 1 nnode at this level, so it is the root */
 767		if (cnt == 1)
 768			break;
 769		/* Update the information about the level below */
 770		bcnt = cnt;
 771		bsz = c->nnode_sz;
 772		row -= 1;
 773	}
 774
 775	if (*big_lpt) {
 776		/* Need to add LPT's save table */
 777		if (len + c->lsave_sz > c->leb_size) {
 778			alen = ALIGN(len, c->min_io_size);
 779			set_ltab(c, lnum, c->leb_size - alen, alen - len);
 780			memset(p, 0xff, alen - len);
 781			err = ubifs_leb_change(c, lnum++, buf, alen);
 782			if (err)
 783				goto out;
 784			p = buf;
 785			len = 0;
 786		}
 787
 788		c->lsave_lnum = lnum;
 789		c->lsave_offs = len;
 790
 791		for (i = 0; i < c->lsave_cnt && i < *main_lebs; i++)
 792			lsave[i] = c->main_first + i;
 793		for (; i < c->lsave_cnt; i++)
 794			lsave[i] = c->main_first;
 795
 796		ubifs_pack_lsave(c, p, lsave);
 797		p += c->lsave_sz;
 798		len += c->lsave_sz;
 799	}
 800
 801	/* Need to add LPT's own LEB properties table */
 802	if (len + c->ltab_sz > c->leb_size) {
 803		alen = ALIGN(len, c->min_io_size);
 804		set_ltab(c, lnum, c->leb_size - alen, alen - len);
 805		memset(p, 0xff, alen - len);
 806		err = ubifs_leb_change(c, lnum++, buf, alen);
 807		if (err)
 808			goto out;
 809		p = buf;
 810		len = 0;
 811	}
 812
 813	c->ltab_lnum = lnum;
 814	c->ltab_offs = len;
 815
 816	/* Update ltab before packing it */
 817	len += c->ltab_sz;
 818	alen = ALIGN(len, c->min_io_size);
 819	set_ltab(c, lnum, c->leb_size - alen, alen - len);
 820
 821	ubifs_pack_ltab(c, p, ltab);
 822	p += c->ltab_sz;
 823
 824	/* Write remaining buffer */
 825	memset(p, 0xff, alen - len);
 826	err = ubifs_leb_change(c, lnum, buf, alen);
 827	if (err)
 828		goto out;
 829
 830	c->nhead_lnum = lnum;
 831	c->nhead_offs = ALIGN(len, c->min_io_size);
 832
 833	dbg_lp("space_bits %d", c->space_bits);
 834	dbg_lp("lpt_lnum_bits %d", c->lpt_lnum_bits);
 835	dbg_lp("lpt_offs_bits %d", c->lpt_offs_bits);
 836	dbg_lp("lpt_spc_bits %d", c->lpt_spc_bits);
 837	dbg_lp("pcnt_bits %d", c->pcnt_bits);
 838	dbg_lp("lnum_bits %d", c->lnum_bits);
 839	dbg_lp("pnode_sz %d", c->pnode_sz);
 840	dbg_lp("nnode_sz %d", c->nnode_sz);
 841	dbg_lp("ltab_sz %d", c->ltab_sz);
 842	dbg_lp("lsave_sz %d", c->lsave_sz);
 843	dbg_lp("lsave_cnt %d", c->lsave_cnt);
 844	dbg_lp("lpt_hght %d", c->lpt_hght);
 845	dbg_lp("big_lpt %d", c->big_lpt);
 846	dbg_lp("LPT root is at %d:%d", c->lpt_lnum, c->lpt_offs);
 847	dbg_lp("LPT head is at %d:%d", c->nhead_lnum, c->nhead_offs);
 848	dbg_lp("LPT ltab is at %d:%d", c->ltab_lnum, c->ltab_offs);
 849	if (c->big_lpt)
 850		dbg_lp("LPT lsave is at %d:%d", c->lsave_lnum, c->lsave_offs);
 851out:
 852	c->ltab = NULL;
 853	kfree(lsave);
 854	vfree(ltab);
 855	vfree(buf);
 856	kfree(nnode);
 857	kfree(pnode);
 858	return err;
 859}
 860
 861/**
 862 * update_cats - add LEB properties of a pnode to LEB category lists and heaps.
 863 * @c: UBIFS file-system description object
 864 * @pnode: pnode
 865 *
 866 * When a pnode is loaded into memory, the LEB properties it contains are added,
 867 * by this function, to the LEB category lists and heaps.
 868 */
 869static void update_cats(struct ubifs_info *c, struct ubifs_pnode *pnode)
 870{
 871	int i;
 872
 873	for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
 874		int cat = pnode->lprops[i].flags & LPROPS_CAT_MASK;
 875		int lnum = pnode->lprops[i].lnum;
 876
 877		if (!lnum)
 878			return;
 879		ubifs_add_to_cat(c, &pnode->lprops[i], cat);
 880	}
 881}
 882
 883/**
 884 * replace_cats - add LEB properties of a pnode to LEB category lists and heaps.
 885 * @c: UBIFS file-system description object
 886 * @old_pnode: pnode copied
 887 * @new_pnode: pnode copy
 888 *
 889 * During commit it is sometimes necessary to copy a pnode
 890 * (see dirty_cow_pnode).  When that happens, references in
 891 * category lists and heaps must be replaced.  This function does that.
 892 */
 893static void replace_cats(struct ubifs_info *c, struct ubifs_pnode *old_pnode,
 894			 struct ubifs_pnode *new_pnode)
 895{
 896	int i;
 897
 898	for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
 899		if (!new_pnode->lprops[i].lnum)
 900			return;
 901		ubifs_replace_cat(c, &old_pnode->lprops[i],
 902				  &new_pnode->lprops[i]);
 903	}
 904}
 905
 906/**
 907 * check_lpt_crc - check LPT node crc is correct.
 908 * @c: UBIFS file-system description object
 909 * @buf: buffer containing node
 910 * @len: length of node
 911 *
 912 * This function returns %0 on success and a negative error code on failure.
 913 */
 914static int check_lpt_crc(const struct ubifs_info *c, void *buf, int len)
 915{
 916	int pos = 0;
 917	uint8_t *addr = buf;
 918	uint16_t crc, calc_crc;
 919
 920	crc = ubifs_unpack_bits(&addr, &pos, UBIFS_LPT_CRC_BITS);
 921	calc_crc = crc16(-1, buf + UBIFS_LPT_CRC_BYTES,
 922			 len - UBIFS_LPT_CRC_BYTES);
 923	if (crc != calc_crc) {
 924		ubifs_err(c, "invalid crc in LPT node: crc %hx calc %hx",
 925			  crc, calc_crc);
 926		dump_stack();
 927		return -EINVAL;
 928	}
 929	return 0;
 930}
 931
 932/**
 933 * check_lpt_type - check LPT node type is correct.
 934 * @c: UBIFS file-system description object
 935 * @addr: address of type bit field is passed and returned updated here
 936 * @pos: position of type bit field is passed and returned updated here
 937 * @type: expected type
 938 *
 939 * This function returns %0 on success and a negative error code on failure.
 940 */
 941static int check_lpt_type(const struct ubifs_info *c, uint8_t **addr,
 942			  int *pos, int type)
 943{
 944	int node_type;
 945
 946	node_type = ubifs_unpack_bits(addr, pos, UBIFS_LPT_TYPE_BITS);
 947	if (node_type != type) {
 948		ubifs_err(c, "invalid type (%d) in LPT node type %d",
 949			  node_type, type);
 950		dump_stack();
 951		return -EINVAL;
 952	}
 953	return 0;
 954}
 955
 956/**
 957 * unpack_pnode - unpack a pnode.
 958 * @c: UBIFS file-system description object
 959 * @buf: buffer containing packed pnode to unpack
 960 * @pnode: pnode structure to fill
 961 *
 962 * This function returns %0 on success and a negative error code on failure.
 963 */
 964static int unpack_pnode(const struct ubifs_info *c, void *buf,
 965			struct ubifs_pnode *pnode)
 966{
 967	uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
 968	int i, pos = 0, err;
 969
 970	err = check_lpt_type(c, &addr, &pos, UBIFS_LPT_PNODE);
 971	if (err)
 972		return err;
 973	if (c->big_lpt)
 974		pnode->num = ubifs_unpack_bits(&addr, &pos, c->pcnt_bits);
 975	for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
 976		struct ubifs_lprops * const lprops = &pnode->lprops[i];
 977
 978		lprops->free = ubifs_unpack_bits(&addr, &pos, c->space_bits);
 979		lprops->free <<= 3;
 980		lprops->dirty = ubifs_unpack_bits(&addr, &pos, c->space_bits);
 981		lprops->dirty <<= 3;
 982
 983		if (ubifs_unpack_bits(&addr, &pos, 1))
 984			lprops->flags = LPROPS_INDEX;
 985		else
 986			lprops->flags = 0;
 987		lprops->flags |= ubifs_categorize_lprops(c, lprops);
 988	}
 989	err = check_lpt_crc(c, buf, c->pnode_sz);
 990	return err;
 991}
 992
 993/**
 994 * ubifs_unpack_nnode - unpack a nnode.
 995 * @c: UBIFS file-system description object
 996 * @buf: buffer containing packed nnode to unpack
 997 * @nnode: nnode structure to fill
 998 *
 999 * This function returns %0 on success and a negative error code on failure.
1000 */
1001int ubifs_unpack_nnode(const struct ubifs_info *c, void *buf,
1002		       struct ubifs_nnode *nnode)
1003{
1004	uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
1005	int i, pos = 0, err;
1006
1007	err = check_lpt_type(c, &addr, &pos, UBIFS_LPT_NNODE);
1008	if (err)
1009		return err;
1010	if (c->big_lpt)
1011		nnode->num = ubifs_unpack_bits(&addr, &pos, c->pcnt_bits);
1012	for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
1013		int lnum;
1014
1015		lnum = ubifs_unpack_bits(&addr, &pos, c->lpt_lnum_bits) +
1016		       c->lpt_first;
1017		if (lnum == c->lpt_last + 1)
1018			lnum = 0;
1019		nnode->nbranch[i].lnum = lnum;
1020		nnode->nbranch[i].offs = ubifs_unpack_bits(&addr, &pos,
1021						     c->lpt_offs_bits);
1022	}
1023	err = check_lpt_crc(c, buf, c->nnode_sz);
1024	return err;
1025}
1026
1027/**
1028 * unpack_ltab - unpack the LPT's own lprops table.
1029 * @c: UBIFS file-system description object
1030 * @buf: buffer from which to unpack
1031 *
1032 * This function returns %0 on success and a negative error code on failure.
1033 */
1034static int unpack_ltab(const struct ubifs_info *c, void *buf)
1035{
1036	uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
1037	int i, pos = 0, err;
1038
1039	err = check_lpt_type(c, &addr, &pos, UBIFS_LPT_LTAB);
1040	if (err)
1041		return err;
1042	for (i = 0; i < c->lpt_lebs; i++) {
1043		int free = ubifs_unpack_bits(&addr, &pos, c->lpt_spc_bits);
1044		int dirty = ubifs_unpack_bits(&addr, &pos, c->lpt_spc_bits);
1045
1046		if (free < 0 || free > c->leb_size || dirty < 0 ||
1047		    dirty > c->leb_size || free + dirty > c->leb_size)
1048			return -EINVAL;
1049
1050		c->ltab[i].free = free;
1051		c->ltab[i].dirty = dirty;
1052		c->ltab[i].tgc = 0;
1053		c->ltab[i].cmt = 0;
1054	}
1055	err = check_lpt_crc(c, buf, c->ltab_sz);
1056	return err;
1057}
1058
1059/**
1060 * unpack_lsave - unpack the LPT's save table.
1061 * @c: UBIFS file-system description object
1062 * @buf: buffer from which to unpack
1063 *
1064 * This function returns %0 on success and a negative error code on failure.
1065 */
1066static int unpack_lsave(const struct ubifs_info *c, void *buf)
1067{
1068	uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
1069	int i, pos = 0, err;
1070
1071	err = check_lpt_type(c, &addr, &pos, UBIFS_LPT_LSAVE);
1072	if (err)
1073		return err;
1074	for (i = 0; i < c->lsave_cnt; i++) {
1075		int lnum = ubifs_unpack_bits(&addr, &pos, c->lnum_bits);
1076
1077		if (lnum < c->main_first || lnum >= c->leb_cnt)
1078			return -EINVAL;
1079		c->lsave[i] = lnum;
1080	}
1081	err = check_lpt_crc(c, buf, c->lsave_sz);
1082	return err;
1083}
1084
1085/**
1086 * validate_nnode - validate a nnode.
1087 * @c: UBIFS file-system description object
1088 * @nnode: nnode to validate
1089 * @parent: parent nnode (or NULL for the root nnode)
1090 * @iip: index in parent
1091 *
1092 * This function returns %0 on success and a negative error code on failure.
1093 */
1094static int validate_nnode(const struct ubifs_info *c, struct ubifs_nnode *nnode,
1095			  struct ubifs_nnode *parent, int iip)
1096{
1097	int i, lvl, max_offs;
1098
1099	if (c->big_lpt) {
1100		int num = calc_nnode_num_from_parent(c, parent, iip);
1101
1102		if (nnode->num != num)
1103			return -EINVAL;
1104	}
1105	lvl = parent ? parent->level - 1 : c->lpt_hght;
1106	if (lvl < 1)
1107		return -EINVAL;
1108	if (lvl == 1)
1109		max_offs = c->leb_size - c->pnode_sz;
1110	else
1111		max_offs = c->leb_size - c->nnode_sz;
1112	for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
1113		int lnum = nnode->nbranch[i].lnum;
1114		int offs = nnode->nbranch[i].offs;
1115
1116		if (lnum == 0) {
1117			if (offs != 0)
1118				return -EINVAL;
1119			continue;
1120		}
1121		if (lnum < c->lpt_first || lnum > c->lpt_last)
1122			return -EINVAL;
1123		if (offs < 0 || offs > max_offs)
1124			return -EINVAL;
1125	}
1126	return 0;
1127}
1128
1129/**
1130 * validate_pnode - validate a pnode.
1131 * @c: UBIFS file-system description object
1132 * @pnode: pnode to validate
1133 * @parent: parent nnode
1134 * @iip: index in parent
1135 *
1136 * This function returns %0 on success and a negative error code on failure.
1137 */
1138static int validate_pnode(const struct ubifs_info *c, struct ubifs_pnode *pnode,
1139			  struct ubifs_nnode *parent, int iip)
1140{
1141	int i;
1142
1143	if (c->big_lpt) {
1144		int num = calc_pnode_num_from_parent(c, parent, iip);
1145
1146		if (pnode->num != num)
1147			return -EINVAL;
1148	}
1149	for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
1150		int free = pnode->lprops[i].free;
1151		int dirty = pnode->lprops[i].dirty;
1152
1153		if (free < 0 || free > c->leb_size || free % c->min_io_size ||
1154		    (free & 7))
1155			return -EINVAL;
1156		if (dirty < 0 || dirty > c->leb_size || (dirty & 7))
1157			return -EINVAL;
1158		if (dirty + free > c->leb_size)
1159			return -EINVAL;
1160	}
1161	return 0;
1162}
1163
1164/**
1165 * set_pnode_lnum - set LEB numbers on a pnode.
1166 * @c: UBIFS file-system description object
1167 * @pnode: pnode to update
1168 *
1169 * This function calculates the LEB numbers for the LEB properties it contains
1170 * based on the pnode number.
1171 */
1172static void set_pnode_lnum(const struct ubifs_info *c,
1173			   struct ubifs_pnode *pnode)
1174{
1175	int i, lnum;
1176
1177	lnum = (pnode->num << UBIFS_LPT_FANOUT_SHIFT) + c->main_first;
1178	for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
1179		if (lnum >= c->leb_cnt)
1180			return;
1181		pnode->lprops[i].lnum = lnum++;
1182	}
1183}
1184
1185/**
1186 * ubifs_read_nnode - read a nnode from flash and link it to the tree in memory.
1187 * @c: UBIFS file-system description object
1188 * @parent: parent nnode (or NULL for the root)
1189 * @iip: index in parent
1190 *
1191 * This function returns %0 on success and a negative error code on failure.
1192 */
1193int ubifs_read_nnode(struct ubifs_info *c, struct ubifs_nnode *parent, int iip)
1194{
1195	struct ubifs_nbranch *branch = NULL;
1196	struct ubifs_nnode *nnode = NULL;
1197	void *buf = c->lpt_nod_buf;
1198	int err, lnum, offs;
1199
1200	if (parent) {
1201		branch = &parent->nbranch[iip];
1202		lnum = branch->lnum;
1203		offs = branch->offs;
1204	} else {
1205		lnum = c->lpt_lnum;
1206		offs = c->lpt_offs;
1207	}
1208	nnode = kzalloc(sizeof(struct ubifs_nnode), GFP_NOFS);
1209	if (!nnode) {
1210		err = -ENOMEM;
1211		goto out;
1212	}
1213	if (lnum == 0) {
1214		/*
1215		 * This nnode was not written which just means that the LEB
1216		 * properties in the subtree below it describe empty LEBs. We
1217		 * make the nnode as though we had read it, which in fact means
1218		 * doing almost nothing.
1219		 */
1220		if (c->big_lpt)
1221			nnode->num = calc_nnode_num_from_parent(c, parent, iip);
1222	} else {
1223		err = ubifs_leb_read(c, lnum, buf, offs, c->nnode_sz, 1);
1224		if (err)
1225			goto out;
1226		err = ubifs_unpack_nnode(c, buf, nnode);
1227		if (err)
1228			goto out;
1229	}
1230	err = validate_nnode(c, nnode, parent, iip);
1231	if (err)
1232		goto out;
1233	if (!c->big_lpt)
1234		nnode->num = calc_nnode_num_from_parent(c, parent, iip);
1235	if (parent) {
1236		branch->nnode = nnode;
1237		nnode->level = parent->level - 1;
1238	} else {
1239		c->nroot = nnode;
1240		nnode->level = c->lpt_hght;
1241	}
1242	nnode->parent = parent;
1243	nnode->iip = iip;
1244	return 0;
1245
1246out:
1247	ubifs_err(c, "error %d reading nnode at %d:%d", err, lnum, offs);
1248	dump_stack();
1249	kfree(nnode);
1250	return err;
1251}
1252
1253/**
1254 * read_pnode - read a pnode from flash and link it to the tree in memory.
1255 * @c: UBIFS file-system description object
1256 * @parent: parent nnode
1257 * @iip: index in parent
1258 *
1259 * This function returns %0 on success and a negative error code on failure.
1260 */
1261static int read_pnode(struct ubifs_info *c, struct ubifs_nnode *parent, int iip)
1262{
1263	struct ubifs_nbranch *branch;
1264	struct ubifs_pnode *pnode = NULL;
1265	void *buf = c->lpt_nod_buf;
1266	int err, lnum, offs;
1267
1268	branch = &parent->nbranch[iip];
1269	lnum = branch->lnum;
1270	offs = branch->offs;
1271	pnode = kzalloc(sizeof(struct ubifs_pnode), GFP_NOFS);
1272	if (!pnode)
1273		return -ENOMEM;
1274
1275	if (lnum == 0) {
1276		/*
1277		 * This pnode was not written which just means that the LEB
1278		 * properties in it describe empty LEBs. We make the pnode as
1279		 * though we had read it.
1280		 */
1281		int i;
1282
1283		if (c->big_lpt)
1284			pnode->num = calc_pnode_num_from_parent(c, parent, iip);
1285		for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
1286			struct ubifs_lprops * const lprops = &pnode->lprops[i];
1287
1288			lprops->free = c->leb_size;
1289			lprops->flags = ubifs_categorize_lprops(c, lprops);
1290		}
1291	} else {
1292		err = ubifs_leb_read(c, lnum, buf, offs, c->pnode_sz, 1);
1293		if (err)
1294			goto out;
1295		err = unpack_pnode(c, buf, pnode);
1296		if (err)
1297			goto out;
1298	}
1299	err = validate_pnode(c, pnode, parent, iip);
1300	if (err)
1301		goto out;
1302	if (!c->big_lpt)
1303		pnode->num = calc_pnode_num_from_parent(c, parent, iip);
1304	branch->pnode = pnode;
1305	pnode->parent = parent;
1306	pnode->iip = iip;
1307	set_pnode_lnum(c, pnode);
1308	c->pnodes_have += 1;
1309	return 0;
1310
1311out:
1312	ubifs_err(c, "error %d reading pnode at %d:%d", err, lnum, offs);
1313	ubifs_dump_pnode(c, pnode, parent, iip);
1314	dump_stack();
1315	ubifs_err(c, "calc num: %d", calc_pnode_num_from_parent(c, parent, iip));
1316	kfree(pnode);
1317	return err;
1318}
1319
1320/**
1321 * read_ltab - read LPT's own lprops table.
1322 * @c: UBIFS file-system description object
1323 *
1324 * This function returns %0 on success and a negative error code on failure.
1325 */
1326static int read_ltab(struct ubifs_info *c)
1327{
1328	int err;
1329	void *buf;
1330
1331	buf = vmalloc(c->ltab_sz);
1332	if (!buf)
1333		return -ENOMEM;
1334	err = ubifs_leb_read(c, c->ltab_lnum, buf, c->ltab_offs, c->ltab_sz, 1);
1335	if (err)
1336		goto out;
1337	err = unpack_ltab(c, buf);
1338out:
1339	vfree(buf);
1340	return err;
1341}
1342
1343/**
1344 * read_lsave - read LPT's save table.
1345 * @c: UBIFS file-system description object
1346 *
1347 * This function returns %0 on success and a negative error code on failure.
1348 */
1349static int read_lsave(struct ubifs_info *c)
1350{
1351	int err, i;
1352	void *buf;
1353
1354	buf = vmalloc(c->lsave_sz);
1355	if (!buf)
1356		return -ENOMEM;
1357	err = ubifs_leb_read(c, c->lsave_lnum, buf, c->lsave_offs,
1358			     c->lsave_sz, 1);
1359	if (err)
1360		goto out;
1361	err = unpack_lsave(c, buf);
1362	if (err)
1363		goto out;
1364	for (i = 0; i < c->lsave_cnt; i++) {
1365		int lnum = c->lsave[i];
1366		struct ubifs_lprops *lprops;
1367
1368		/*
1369		 * Due to automatic resizing, the values in the lsave table
1370		 * could be beyond the volume size - just ignore them.
1371		 */
1372		if (lnum >= c->leb_cnt)
1373			continue;
1374		lprops = ubifs_lpt_lookup(c, lnum);
1375		if (IS_ERR(lprops)) {
1376			err = PTR_ERR(lprops);
1377			goto out;
1378		}
1379	}
1380out:
1381	vfree(buf);
1382	return err;
1383}
1384
1385/**
1386 * ubifs_get_nnode - get a nnode.
1387 * @c: UBIFS file-system description object
1388 * @parent: parent nnode (or NULL for the root)
1389 * @iip: index in parent
1390 *
1391 * This function returns a pointer to the nnode on success or a negative error
1392 * code on failure.
1393 */
1394struct ubifs_nnode *ubifs_get_nnode(struct ubifs_info *c,
1395				    struct ubifs_nnode *parent, int iip)
1396{
1397	struct ubifs_nbranch *branch;
1398	struct ubifs_nnode *nnode;
1399	int err;
1400
1401	branch = &parent->nbranch[iip];
1402	nnode = branch->nnode;
1403	if (nnode)
1404		return nnode;
1405	err = ubifs_read_nnode(c, parent, iip);
1406	if (err)
1407		return ERR_PTR(err);
1408	return branch->nnode;
1409}
1410
1411/**
1412 * ubifs_get_pnode - get a pnode.
1413 * @c: UBIFS file-system description object
1414 * @parent: parent nnode
1415 * @iip: index in parent
1416 *
1417 * This function returns a pointer to the pnode on success or a negative error
1418 * code on failure.
1419 */
1420struct ubifs_pnode *ubifs_get_pnode(struct ubifs_info *c,
1421				    struct ubifs_nnode *parent, int iip)
1422{
1423	struct ubifs_nbranch *branch;
1424	struct ubifs_pnode *pnode;
1425	int err;
1426
1427	branch = &parent->nbranch[iip];
1428	pnode = branch->pnode;
1429	if (pnode)
1430		return pnode;
1431	err = read_pnode(c, parent, iip);
1432	if (err)
1433		return ERR_PTR(err);
1434	update_cats(c, branch->pnode);
1435	return branch->pnode;
1436}
1437
1438/**
1439 * ubifs_lpt_lookup - lookup LEB properties in the LPT.
1440 * @c: UBIFS file-system description object
1441 * @lnum: LEB number to lookup
1442 *
1443 * This function returns a pointer to the LEB properties on success or a
1444 * negative error code on failure.
1445 */
1446struct ubifs_lprops *ubifs_lpt_lookup(struct ubifs_info *c, int lnum)
1447{
1448	int err, i, h, iip, shft;
1449	struct ubifs_nnode *nnode;
1450	struct ubifs_pnode *pnode;
1451
1452	if (!c->nroot) {
1453		err = ubifs_read_nnode(c, NULL, 0);
1454		if (err)
1455			return ERR_PTR(err);
1456	}
1457	nnode = c->nroot;
1458	i = lnum - c->main_first;
1459	shft = c->lpt_hght * UBIFS_LPT_FANOUT_SHIFT;
1460	for (h = 1; h < c->lpt_hght; h++) {
1461		iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
1462		shft -= UBIFS_LPT_FANOUT_SHIFT;
1463		nnode = ubifs_get_nnode(c, nnode, iip);
1464		if (IS_ERR(nnode))
1465			return ERR_CAST(nnode);
1466	}
1467	iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
 
1468	pnode = ubifs_get_pnode(c, nnode, iip);
1469	if (IS_ERR(pnode))
1470		return ERR_CAST(pnode);
1471	iip = (i & (UBIFS_LPT_FANOUT - 1));
1472	dbg_lp("LEB %d, free %d, dirty %d, flags %d", lnum,
1473	       pnode->lprops[iip].free, pnode->lprops[iip].dirty,
1474	       pnode->lprops[iip].flags);
1475	return &pnode->lprops[iip];
1476}
1477
1478/**
1479 * dirty_cow_nnode - ensure a nnode is not being committed.
1480 * @c: UBIFS file-system description object
1481 * @nnode: nnode to check
1482 *
1483 * Returns dirtied nnode on success or negative error code on failure.
1484 */
1485static struct ubifs_nnode *dirty_cow_nnode(struct ubifs_info *c,
1486					   struct ubifs_nnode *nnode)
1487{
1488	struct ubifs_nnode *n;
1489	int i;
1490
1491	if (!test_bit(COW_CNODE, &nnode->flags)) {
1492		/* nnode is not being committed */
1493		if (!test_and_set_bit(DIRTY_CNODE, &nnode->flags)) {
1494			c->dirty_nn_cnt += 1;
1495			ubifs_add_nnode_dirt(c, nnode);
1496		}
1497		return nnode;
1498	}
1499
1500	/* nnode is being committed, so copy it */
1501	n = kmemdup(nnode, sizeof(struct ubifs_nnode), GFP_NOFS);
1502	if (unlikely(!n))
1503		return ERR_PTR(-ENOMEM);
1504
 
1505	n->cnext = NULL;
1506	__set_bit(DIRTY_CNODE, &n->flags);
1507	__clear_bit(COW_CNODE, &n->flags);
1508
1509	/* The children now have new parent */
1510	for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
1511		struct ubifs_nbranch *branch = &n->nbranch[i];
1512
1513		if (branch->cnode)
1514			branch->cnode->parent = n;
1515	}
1516
1517	ubifs_assert(!test_bit(OBSOLETE_CNODE, &nnode->flags));
1518	__set_bit(OBSOLETE_CNODE, &nnode->flags);
1519
1520	c->dirty_nn_cnt += 1;
1521	ubifs_add_nnode_dirt(c, nnode);
1522	if (nnode->parent)
1523		nnode->parent->nbranch[n->iip].nnode = n;
1524	else
1525		c->nroot = n;
1526	return n;
1527}
1528
1529/**
1530 * dirty_cow_pnode - ensure a pnode is not being committed.
1531 * @c: UBIFS file-system description object
1532 * @pnode: pnode to check
1533 *
1534 * Returns dirtied pnode on success or negative error code on failure.
1535 */
1536static struct ubifs_pnode *dirty_cow_pnode(struct ubifs_info *c,
1537					   struct ubifs_pnode *pnode)
1538{
1539	struct ubifs_pnode *p;
1540
1541	if (!test_bit(COW_CNODE, &pnode->flags)) {
1542		/* pnode is not being committed */
1543		if (!test_and_set_bit(DIRTY_CNODE, &pnode->flags)) {
1544			c->dirty_pn_cnt += 1;
1545			add_pnode_dirt(c, pnode);
1546		}
1547		return pnode;
1548	}
1549
1550	/* pnode is being committed, so copy it */
1551	p = kmemdup(pnode, sizeof(struct ubifs_pnode), GFP_NOFS);
1552	if (unlikely(!p))
1553		return ERR_PTR(-ENOMEM);
1554
 
1555	p->cnext = NULL;
1556	__set_bit(DIRTY_CNODE, &p->flags);
1557	__clear_bit(COW_CNODE, &p->flags);
1558	replace_cats(c, pnode, p);
1559
1560	ubifs_assert(!test_bit(OBSOLETE_CNODE, &pnode->flags));
1561	__set_bit(OBSOLETE_CNODE, &pnode->flags);
1562
1563	c->dirty_pn_cnt += 1;
1564	add_pnode_dirt(c, pnode);
1565	pnode->parent->nbranch[p->iip].pnode = p;
1566	return p;
1567}
1568
1569/**
1570 * ubifs_lpt_lookup_dirty - lookup LEB properties in the LPT.
1571 * @c: UBIFS file-system description object
1572 * @lnum: LEB number to lookup
1573 *
1574 * This function returns a pointer to the LEB properties on success or a
1575 * negative error code on failure.
1576 */
1577struct ubifs_lprops *ubifs_lpt_lookup_dirty(struct ubifs_info *c, int lnum)
1578{
1579	int err, i, h, iip, shft;
1580	struct ubifs_nnode *nnode;
1581	struct ubifs_pnode *pnode;
1582
1583	if (!c->nroot) {
1584		err = ubifs_read_nnode(c, NULL, 0);
1585		if (err)
1586			return ERR_PTR(err);
1587	}
1588	nnode = c->nroot;
1589	nnode = dirty_cow_nnode(c, nnode);
1590	if (IS_ERR(nnode))
1591		return ERR_CAST(nnode);
1592	i = lnum - c->main_first;
1593	shft = c->lpt_hght * UBIFS_LPT_FANOUT_SHIFT;
1594	for (h = 1; h < c->lpt_hght; h++) {
1595		iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
1596		shft -= UBIFS_LPT_FANOUT_SHIFT;
1597		nnode = ubifs_get_nnode(c, nnode, iip);
1598		if (IS_ERR(nnode))
1599			return ERR_CAST(nnode);
1600		nnode = dirty_cow_nnode(c, nnode);
1601		if (IS_ERR(nnode))
1602			return ERR_CAST(nnode);
1603	}
1604	iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
 
1605	pnode = ubifs_get_pnode(c, nnode, iip);
1606	if (IS_ERR(pnode))
1607		return ERR_CAST(pnode);
1608	pnode = dirty_cow_pnode(c, pnode);
1609	if (IS_ERR(pnode))
1610		return ERR_CAST(pnode);
1611	iip = (i & (UBIFS_LPT_FANOUT - 1));
1612	dbg_lp("LEB %d, free %d, dirty %d, flags %d", lnum,
1613	       pnode->lprops[iip].free, pnode->lprops[iip].dirty,
1614	       pnode->lprops[iip].flags);
1615	ubifs_assert(test_bit(DIRTY_CNODE, &pnode->flags));
1616	return &pnode->lprops[iip];
1617}
1618
1619/**
1620 * lpt_init_rd - initialize the LPT for reading.
1621 * @c: UBIFS file-system description object
1622 *
1623 * This function returns %0 on success and a negative error code on failure.
1624 */
1625static int lpt_init_rd(struct ubifs_info *c)
1626{
1627	int err, i;
1628
1629	c->ltab = vmalloc(sizeof(struct ubifs_lpt_lprops) * c->lpt_lebs);
1630	if (!c->ltab)
1631		return -ENOMEM;
1632
1633	i = max_t(int, c->nnode_sz, c->pnode_sz);
1634	c->lpt_nod_buf = kmalloc(i, GFP_KERNEL);
1635	if (!c->lpt_nod_buf)
1636		return -ENOMEM;
1637
1638	for (i = 0; i < LPROPS_HEAP_CNT; i++) {
1639		c->lpt_heap[i].arr = kmalloc(sizeof(void *) * LPT_HEAP_SZ,
1640					     GFP_KERNEL);
1641		if (!c->lpt_heap[i].arr)
1642			return -ENOMEM;
1643		c->lpt_heap[i].cnt = 0;
1644		c->lpt_heap[i].max_cnt = LPT_HEAP_SZ;
1645	}
1646
1647	c->dirty_idx.arr = kmalloc(sizeof(void *) * LPT_HEAP_SZ, GFP_KERNEL);
1648	if (!c->dirty_idx.arr)
1649		return -ENOMEM;
1650	c->dirty_idx.cnt = 0;
1651	c->dirty_idx.max_cnt = LPT_HEAP_SZ;
1652
1653	err = read_ltab(c);
1654	if (err)
1655		return err;
1656
1657	dbg_lp("space_bits %d", c->space_bits);
1658	dbg_lp("lpt_lnum_bits %d", c->lpt_lnum_bits);
1659	dbg_lp("lpt_offs_bits %d", c->lpt_offs_bits);
1660	dbg_lp("lpt_spc_bits %d", c->lpt_spc_bits);
1661	dbg_lp("pcnt_bits %d", c->pcnt_bits);
1662	dbg_lp("lnum_bits %d", c->lnum_bits);
1663	dbg_lp("pnode_sz %d", c->pnode_sz);
1664	dbg_lp("nnode_sz %d", c->nnode_sz);
1665	dbg_lp("ltab_sz %d", c->ltab_sz);
1666	dbg_lp("lsave_sz %d", c->lsave_sz);
1667	dbg_lp("lsave_cnt %d", c->lsave_cnt);
1668	dbg_lp("lpt_hght %d", c->lpt_hght);
1669	dbg_lp("big_lpt %d", c->big_lpt);
1670	dbg_lp("LPT root is at %d:%d", c->lpt_lnum, c->lpt_offs);
1671	dbg_lp("LPT head is at %d:%d", c->nhead_lnum, c->nhead_offs);
1672	dbg_lp("LPT ltab is at %d:%d", c->ltab_lnum, c->ltab_offs);
1673	if (c->big_lpt)
1674		dbg_lp("LPT lsave is at %d:%d", c->lsave_lnum, c->lsave_offs);
1675
1676	return 0;
1677}
1678
1679/**
1680 * lpt_init_wr - initialize the LPT for writing.
1681 * @c: UBIFS file-system description object
1682 *
1683 * 'lpt_init_rd()' must have been called already.
1684 *
1685 * This function returns %0 on success and a negative error code on failure.
1686 */
1687static int lpt_init_wr(struct ubifs_info *c)
1688{
1689	int err, i;
1690
1691	c->ltab_cmt = vmalloc(sizeof(struct ubifs_lpt_lprops) * c->lpt_lebs);
1692	if (!c->ltab_cmt)
1693		return -ENOMEM;
1694
1695	c->lpt_buf = vmalloc(c->leb_size);
1696	if (!c->lpt_buf)
1697		return -ENOMEM;
1698
1699	if (c->big_lpt) {
1700		c->lsave = kmalloc(sizeof(int) * c->lsave_cnt, GFP_NOFS);
1701		if (!c->lsave)
1702			return -ENOMEM;
1703		err = read_lsave(c);
1704		if (err)
1705			return err;
1706	}
1707
1708	for (i = 0; i < c->lpt_lebs; i++)
1709		if (c->ltab[i].free == c->leb_size) {
1710			err = ubifs_leb_unmap(c, i + c->lpt_first);
1711			if (err)
1712				return err;
1713		}
1714
1715	return 0;
1716}
1717
1718/**
1719 * ubifs_lpt_init - initialize the LPT.
1720 * @c: UBIFS file-system description object
1721 * @rd: whether to initialize lpt for reading
1722 * @wr: whether to initialize lpt for writing
1723 *
1724 * For mounting 'rw', @rd and @wr are both true. For mounting 'ro', @rd is true
1725 * and @wr is false. For mounting from 'ro' to 'rw', @rd is false and @wr is
1726 * true.
1727 *
1728 * This function returns %0 on success and a negative error code on failure.
1729 */
1730int ubifs_lpt_init(struct ubifs_info *c, int rd, int wr)
1731{
1732	int err;
1733
1734	if (rd) {
1735		err = lpt_init_rd(c);
1736		if (err)
1737			goto out_err;
1738	}
1739
1740	if (wr) {
1741		err = lpt_init_wr(c);
1742		if (err)
1743			goto out_err;
1744	}
1745
1746	return 0;
1747
1748out_err:
1749	if (wr)
1750		ubifs_lpt_free(c, 1);
1751	if (rd)
1752		ubifs_lpt_free(c, 0);
1753	return err;
1754}
1755
1756/**
1757 * struct lpt_scan_node - somewhere to put nodes while we scan LPT.
1758 * @nnode: where to keep a nnode
1759 * @pnode: where to keep a pnode
1760 * @cnode: where to keep a cnode
1761 * @in_tree: is the node in the tree in memory
1762 * @ptr.nnode: pointer to the nnode (if it is an nnode) which may be here or in
1763 * the tree
1764 * @ptr.pnode: ditto for pnode
1765 * @ptr.cnode: ditto for cnode
1766 */
1767struct lpt_scan_node {
1768	union {
1769		struct ubifs_nnode nnode;
1770		struct ubifs_pnode pnode;
1771		struct ubifs_cnode cnode;
1772	};
1773	int in_tree;
1774	union {
1775		struct ubifs_nnode *nnode;
1776		struct ubifs_pnode *pnode;
1777		struct ubifs_cnode *cnode;
1778	} ptr;
1779};
1780
1781/**
1782 * scan_get_nnode - for the scan, get a nnode from either the tree or flash.
1783 * @c: the UBIFS file-system description object
1784 * @path: where to put the nnode
1785 * @parent: parent of the nnode
1786 * @iip: index in parent of the nnode
1787 *
1788 * This function returns a pointer to the nnode on success or a negative error
1789 * code on failure.
1790 */
1791static struct ubifs_nnode *scan_get_nnode(struct ubifs_info *c,
1792					  struct lpt_scan_node *path,
1793					  struct ubifs_nnode *parent, int iip)
1794{
1795	struct ubifs_nbranch *branch;
1796	struct ubifs_nnode *nnode;
1797	void *buf = c->lpt_nod_buf;
1798	int err;
1799
1800	branch = &parent->nbranch[iip];
1801	nnode = branch->nnode;
1802	if (nnode) {
1803		path->in_tree = 1;
1804		path->ptr.nnode = nnode;
1805		return nnode;
1806	}
1807	nnode = &path->nnode;
1808	path->in_tree = 0;
1809	path->ptr.nnode = nnode;
1810	memset(nnode, 0, sizeof(struct ubifs_nnode));
1811	if (branch->lnum == 0) {
1812		/*
1813		 * This nnode was not written which just means that the LEB
1814		 * properties in the subtree below it describe empty LEBs. We
1815		 * make the nnode as though we had read it, which in fact means
1816		 * doing almost nothing.
1817		 */
1818		if (c->big_lpt)
1819			nnode->num = calc_nnode_num_from_parent(c, parent, iip);
1820	} else {
1821		err = ubifs_leb_read(c, branch->lnum, buf, branch->offs,
1822				     c->nnode_sz, 1);
1823		if (err)
1824			return ERR_PTR(err);
1825		err = ubifs_unpack_nnode(c, buf, nnode);
1826		if (err)
1827			return ERR_PTR(err);
1828	}
1829	err = validate_nnode(c, nnode, parent, iip);
1830	if (err)
1831		return ERR_PTR(err);
1832	if (!c->big_lpt)
1833		nnode->num = calc_nnode_num_from_parent(c, parent, iip);
1834	nnode->level = parent->level - 1;
1835	nnode->parent = parent;
1836	nnode->iip = iip;
1837	return nnode;
1838}
1839
1840/**
1841 * scan_get_pnode - for the scan, get a pnode from either the tree or flash.
1842 * @c: the UBIFS file-system description object
1843 * @path: where to put the pnode
1844 * @parent: parent of the pnode
1845 * @iip: index in parent of the pnode
1846 *
1847 * This function returns a pointer to the pnode on success or a negative error
1848 * code on failure.
1849 */
1850static struct ubifs_pnode *scan_get_pnode(struct ubifs_info *c,
1851					  struct lpt_scan_node *path,
1852					  struct ubifs_nnode *parent, int iip)
1853{
1854	struct ubifs_nbranch *branch;
1855	struct ubifs_pnode *pnode;
1856	void *buf = c->lpt_nod_buf;
1857	int err;
1858
1859	branch = &parent->nbranch[iip];
1860	pnode = branch->pnode;
1861	if (pnode) {
1862		path->in_tree = 1;
1863		path->ptr.pnode = pnode;
1864		return pnode;
1865	}
1866	pnode = &path->pnode;
1867	path->in_tree = 0;
1868	path->ptr.pnode = pnode;
1869	memset(pnode, 0, sizeof(struct ubifs_pnode));
1870	if (branch->lnum == 0) {
1871		/*
1872		 * This pnode was not written which just means that the LEB
1873		 * properties in it describe empty LEBs. We make the pnode as
1874		 * though we had read it.
1875		 */
1876		int i;
1877
1878		if (c->big_lpt)
1879			pnode->num = calc_pnode_num_from_parent(c, parent, iip);
1880		for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
1881			struct ubifs_lprops * const lprops = &pnode->lprops[i];
1882
1883			lprops->free = c->leb_size;
1884			lprops->flags = ubifs_categorize_lprops(c, lprops);
1885		}
1886	} else {
1887		ubifs_assert(branch->lnum >= c->lpt_first &&
1888			     branch->lnum <= c->lpt_last);
1889		ubifs_assert(branch->offs >= 0 && branch->offs < c->leb_size);
1890		err = ubifs_leb_read(c, branch->lnum, buf, branch->offs,
1891				     c->pnode_sz, 1);
1892		if (err)
1893			return ERR_PTR(err);
1894		err = unpack_pnode(c, buf, pnode);
1895		if (err)
1896			return ERR_PTR(err);
1897	}
1898	err = validate_pnode(c, pnode, parent, iip);
1899	if (err)
1900		return ERR_PTR(err);
1901	if (!c->big_lpt)
1902		pnode->num = calc_pnode_num_from_parent(c, parent, iip);
1903	pnode->parent = parent;
1904	pnode->iip = iip;
1905	set_pnode_lnum(c, pnode);
1906	return pnode;
1907}
1908
1909/**
1910 * ubifs_lpt_scan_nolock - scan the LPT.
1911 * @c: the UBIFS file-system description object
1912 * @start_lnum: LEB number from which to start scanning
1913 * @end_lnum: LEB number at which to stop scanning
1914 * @scan_cb: callback function called for each lprops
1915 * @data: data to be passed to the callback function
1916 *
1917 * This function returns %0 on success and a negative error code on failure.
1918 */
1919int ubifs_lpt_scan_nolock(struct ubifs_info *c, int start_lnum, int end_lnum,
1920			  ubifs_lpt_scan_callback scan_cb, void *data)
1921{
1922	int err = 0, i, h, iip, shft;
1923	struct ubifs_nnode *nnode;
1924	struct ubifs_pnode *pnode;
1925	struct lpt_scan_node *path;
1926
1927	if (start_lnum == -1) {
1928		start_lnum = end_lnum + 1;
1929		if (start_lnum >= c->leb_cnt)
1930			start_lnum = c->main_first;
1931	}
1932
1933	ubifs_assert(start_lnum >= c->main_first && start_lnum < c->leb_cnt);
1934	ubifs_assert(end_lnum >= c->main_first && end_lnum < c->leb_cnt);
1935
1936	if (!c->nroot) {
1937		err = ubifs_read_nnode(c, NULL, 0);
1938		if (err)
1939			return err;
1940	}
1941
1942	path = kmalloc(sizeof(struct lpt_scan_node) * (c->lpt_hght + 1),
1943		       GFP_NOFS);
1944	if (!path)
1945		return -ENOMEM;
1946
1947	path[0].ptr.nnode = c->nroot;
1948	path[0].in_tree = 1;
1949again:
1950	/* Descend to the pnode containing start_lnum */
1951	nnode = c->nroot;
1952	i = start_lnum - c->main_first;
1953	shft = c->lpt_hght * UBIFS_LPT_FANOUT_SHIFT;
1954	for (h = 1; h < c->lpt_hght; h++) {
1955		iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
1956		shft -= UBIFS_LPT_FANOUT_SHIFT;
1957		nnode = scan_get_nnode(c, path + h, nnode, iip);
1958		if (IS_ERR(nnode)) {
1959			err = PTR_ERR(nnode);
1960			goto out;
1961		}
1962	}
1963	iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
 
1964	pnode = scan_get_pnode(c, path + h, nnode, iip);
1965	if (IS_ERR(pnode)) {
1966		err = PTR_ERR(pnode);
1967		goto out;
1968	}
1969	iip = (i & (UBIFS_LPT_FANOUT - 1));
1970
1971	/* Loop for each lprops */
1972	while (1) {
1973		struct ubifs_lprops *lprops = &pnode->lprops[iip];
1974		int ret, lnum = lprops->lnum;
1975
1976		ret = scan_cb(c, lprops, path[h].in_tree, data);
1977		if (ret < 0) {
1978			err = ret;
1979			goto out;
1980		}
1981		if (ret & LPT_SCAN_ADD) {
1982			/* Add all the nodes in path to the tree in memory */
1983			for (h = 1; h < c->lpt_hght; h++) {
1984				const size_t sz = sizeof(struct ubifs_nnode);
1985				struct ubifs_nnode *parent;
1986
1987				if (path[h].in_tree)
1988					continue;
1989				nnode = kmemdup(&path[h].nnode, sz, GFP_NOFS);
1990				if (!nnode) {
1991					err = -ENOMEM;
1992					goto out;
1993				}
1994				parent = nnode->parent;
1995				parent->nbranch[nnode->iip].nnode = nnode;
1996				path[h].ptr.nnode = nnode;
1997				path[h].in_tree = 1;
1998				path[h + 1].cnode.parent = nnode;
1999			}
2000			if (path[h].in_tree)
2001				ubifs_ensure_cat(c, lprops);
2002			else {
2003				const size_t sz = sizeof(struct ubifs_pnode);
2004				struct ubifs_nnode *parent;
2005
2006				pnode = kmemdup(&path[h].pnode, sz, GFP_NOFS);
2007				if (!pnode) {
2008					err = -ENOMEM;
2009					goto out;
2010				}
2011				parent = pnode->parent;
2012				parent->nbranch[pnode->iip].pnode = pnode;
2013				path[h].ptr.pnode = pnode;
2014				path[h].in_tree = 1;
2015				update_cats(c, pnode);
2016				c->pnodes_have += 1;
2017			}
2018			err = dbg_check_lpt_nodes(c, (struct ubifs_cnode *)
2019						  c->nroot, 0, 0);
2020			if (err)
2021				goto out;
2022			err = dbg_check_cats(c);
2023			if (err)
2024				goto out;
2025		}
2026		if (ret & LPT_SCAN_STOP) {
2027			err = 0;
2028			break;
2029		}
2030		/* Get the next lprops */
2031		if (lnum == end_lnum) {
2032			/*
2033			 * We got to the end without finding what we were
2034			 * looking for
2035			 */
2036			err = -ENOSPC;
2037			goto out;
2038		}
2039		if (lnum + 1 >= c->leb_cnt) {
2040			/* Wrap-around to the beginning */
2041			start_lnum = c->main_first;
2042			goto again;
2043		}
2044		if (iip + 1 < UBIFS_LPT_FANOUT) {
2045			/* Next lprops is in the same pnode */
2046			iip += 1;
2047			continue;
2048		}
2049		/* We need to get the next pnode. Go up until we can go right */
2050		iip = pnode->iip;
2051		while (1) {
2052			h -= 1;
2053			ubifs_assert(h >= 0);
2054			nnode = path[h].ptr.nnode;
2055			if (iip + 1 < UBIFS_LPT_FANOUT)
2056				break;
2057			iip = nnode->iip;
2058		}
2059		/* Go right */
2060		iip += 1;
2061		/* Descend to the pnode */
2062		h += 1;
2063		for (; h < c->lpt_hght; h++) {
2064			nnode = scan_get_nnode(c, path + h, nnode, iip);
2065			if (IS_ERR(nnode)) {
2066				err = PTR_ERR(nnode);
2067				goto out;
2068			}
2069			iip = 0;
2070		}
2071		pnode = scan_get_pnode(c, path + h, nnode, iip);
2072		if (IS_ERR(pnode)) {
2073			err = PTR_ERR(pnode);
2074			goto out;
2075		}
2076		iip = 0;
2077	}
2078out:
2079	kfree(path);
2080	return err;
2081}
2082
2083/**
2084 * dbg_chk_pnode - check a pnode.
2085 * @c: the UBIFS file-system description object
2086 * @pnode: pnode to check
2087 * @col: pnode column
2088 *
2089 * This function returns %0 on success and a negative error code on failure.
2090 */
2091static int dbg_chk_pnode(struct ubifs_info *c, struct ubifs_pnode *pnode,
2092			 int col)
2093{
2094	int i;
2095
2096	if (pnode->num != col) {
2097		ubifs_err(c, "pnode num %d expected %d parent num %d iip %d",
2098			  pnode->num, col, pnode->parent->num, pnode->iip);
2099		return -EINVAL;
2100	}
2101	for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
2102		struct ubifs_lprops *lp, *lprops = &pnode->lprops[i];
2103		int lnum = (pnode->num << UBIFS_LPT_FANOUT_SHIFT) + i +
2104			   c->main_first;
2105		int found, cat = lprops->flags & LPROPS_CAT_MASK;
2106		struct ubifs_lpt_heap *heap;
2107		struct list_head *list = NULL;
2108
2109		if (lnum >= c->leb_cnt)
2110			continue;
2111		if (lprops->lnum != lnum) {
2112			ubifs_err(c, "bad LEB number %d expected %d",
2113				  lprops->lnum, lnum);
2114			return -EINVAL;
2115		}
2116		if (lprops->flags & LPROPS_TAKEN) {
2117			if (cat != LPROPS_UNCAT) {
2118				ubifs_err(c, "LEB %d taken but not uncat %d",
2119					  lprops->lnum, cat);
2120				return -EINVAL;
2121			}
2122			continue;
2123		}
2124		if (lprops->flags & LPROPS_INDEX) {
2125			switch (cat) {
2126			case LPROPS_UNCAT:
2127			case LPROPS_DIRTY_IDX:
2128			case LPROPS_FRDI_IDX:
2129				break;
2130			default:
2131				ubifs_err(c, "LEB %d index but cat %d",
2132					  lprops->lnum, cat);
2133				return -EINVAL;
2134			}
2135		} else {
2136			switch (cat) {
2137			case LPROPS_UNCAT:
2138			case LPROPS_DIRTY:
2139			case LPROPS_FREE:
2140			case LPROPS_EMPTY:
2141			case LPROPS_FREEABLE:
2142				break;
2143			default:
2144				ubifs_err(c, "LEB %d not index but cat %d",
2145					  lprops->lnum, cat);
2146				return -EINVAL;
2147			}
2148		}
2149		switch (cat) {
2150		case LPROPS_UNCAT:
2151			list = &c->uncat_list;
2152			break;
2153		case LPROPS_EMPTY:
2154			list = &c->empty_list;
2155			break;
2156		case LPROPS_FREEABLE:
2157			list = &c->freeable_list;
2158			break;
2159		case LPROPS_FRDI_IDX:
2160			list = &c->frdi_idx_list;
2161			break;
2162		}
2163		found = 0;
2164		switch (cat) {
2165		case LPROPS_DIRTY:
2166		case LPROPS_DIRTY_IDX:
2167		case LPROPS_FREE:
2168			heap = &c->lpt_heap[cat - 1];
2169			if (lprops->hpos < heap->cnt &&
2170			    heap->arr[lprops->hpos] == lprops)
2171				found = 1;
2172			break;
2173		case LPROPS_UNCAT:
2174		case LPROPS_EMPTY:
2175		case LPROPS_FREEABLE:
2176		case LPROPS_FRDI_IDX:
2177			list_for_each_entry(lp, list, list)
2178				if (lprops == lp) {
2179					found = 1;
2180					break;
2181				}
2182			break;
2183		}
2184		if (!found) {
2185			ubifs_err(c, "LEB %d cat %d not found in cat heap/list",
2186				  lprops->lnum, cat);
2187			return -EINVAL;
2188		}
2189		switch (cat) {
2190		case LPROPS_EMPTY:
2191			if (lprops->free != c->leb_size) {
2192				ubifs_err(c, "LEB %d cat %d free %d dirty %d",
2193					  lprops->lnum, cat, lprops->free,
2194					  lprops->dirty);
2195				return -EINVAL;
2196			}
2197			break;
2198		case LPROPS_FREEABLE:
2199		case LPROPS_FRDI_IDX:
2200			if (lprops->free + lprops->dirty != c->leb_size) {
2201				ubifs_err(c, "LEB %d cat %d free %d dirty %d",
2202					  lprops->lnum, cat, lprops->free,
2203					  lprops->dirty);
2204				return -EINVAL;
2205			}
2206			break;
2207		}
2208	}
2209	return 0;
2210}
2211
2212/**
2213 * dbg_check_lpt_nodes - check nnodes and pnodes.
2214 * @c: the UBIFS file-system description object
2215 * @cnode: next cnode (nnode or pnode) to check
2216 * @row: row of cnode (root is zero)
2217 * @col: column of cnode (leftmost is zero)
2218 *
2219 * This function returns %0 on success and a negative error code on failure.
2220 */
2221int dbg_check_lpt_nodes(struct ubifs_info *c, struct ubifs_cnode *cnode,
2222			int row, int col)
2223{
2224	struct ubifs_nnode *nnode, *nn;
2225	struct ubifs_cnode *cn;
2226	int num, iip = 0, err;
2227
2228	if (!dbg_is_chk_lprops(c))
2229		return 0;
2230
2231	while (cnode) {
2232		ubifs_assert(row >= 0);
2233		nnode = cnode->parent;
2234		if (cnode->level) {
2235			/* cnode is a nnode */
2236			num = calc_nnode_num(row, col);
2237			if (cnode->num != num) {
2238				ubifs_err(c, "nnode num %d expected %d parent num %d iip %d",
2239					  cnode->num, num,
2240					  (nnode ? nnode->num : 0), cnode->iip);
2241				return -EINVAL;
2242			}
2243			nn = (struct ubifs_nnode *)cnode;
2244			while (iip < UBIFS_LPT_FANOUT) {
2245				cn = nn->nbranch[iip].cnode;
2246				if (cn) {
2247					/* Go down */
2248					row += 1;
2249					col <<= UBIFS_LPT_FANOUT_SHIFT;
2250					col += iip;
2251					iip = 0;
2252					cnode = cn;
2253					break;
2254				}
2255				/* Go right */
2256				iip += 1;
2257			}
2258			if (iip < UBIFS_LPT_FANOUT)
2259				continue;
2260		} else {
2261			struct ubifs_pnode *pnode;
2262
2263			/* cnode is a pnode */
2264			pnode = (struct ubifs_pnode *)cnode;
2265			err = dbg_chk_pnode(c, pnode, col);
2266			if (err)
2267				return err;
2268		}
2269		/* Go up and to the right */
2270		row -= 1;
2271		col >>= UBIFS_LPT_FANOUT_SHIFT;
2272		iip = cnode->iip + 1;
2273		cnode = (struct ubifs_cnode *)nnode;
2274	}
2275	return 0;
2276}