lpt.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105
  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. * This file implements the LEB properties tree (LPT) area. The LPT area
  24. * contains the LEB properties tree, a table of LPT area eraseblocks (ltab), and
  25. * (for the "big" model) a table of saved LEB numbers (lsave). The LPT area sits
  26. * between the log and the orphan area.
  27. *
  28. * The LPT area is like a miniature self-contained file system. It is required
  29. * that it never runs out of space, is fast to access and update, and scales
  30. * logarithmically. The LEB properties tree is implemented as a wandering tree
  31. * much like the TNC, and the LPT area has its own garbage collection.
  32. *
  33. * The LPT has two slightly different forms called the "small model" and the
  34. * "big model". The small model is used when the entire LEB properties table
  35. * can be written into a single eraseblock. In that case, garbage collection
  36. * consists of just writing the whole table, which therefore makes all other
  37. * eraseblocks reusable. In the case of the big model, dirty eraseblocks are
  38. * selected for garbage collection, which consists of marking the clean nodes in
  39. * that LEB as dirty, and then only the dirty nodes are written out. Also, in
  40. * the case of the big model, a table of LEB numbers is saved so that the entire
  41. * LPT does not to be scanned looking for empty eraseblocks when UBIFS is first
  42. * mounted.
  43. */
  44. #include "ubifs.h"
  45. #include "crc16.h"
  46. #include <linux/math64.h>
  47. /**
  48. * do_calc_lpt_geom - calculate sizes for the LPT area.
  49. * @c: the UBIFS file-system description object
  50. *
  51. * Calculate the sizes of LPT bit fields, nodes, and tree, based on the
  52. * properties of the flash and whether LPT is "big" (c->big_lpt).
  53. */
  54. static void do_calc_lpt_geom(struct ubifs_info *c)
  55. {
  56. int i, n, bits, per_leb_wastage, max_pnode_cnt;
  57. long long sz, tot_wastage;
  58. n = c->main_lebs + c->max_leb_cnt - c->leb_cnt;
  59. max_pnode_cnt = DIV_ROUND_UP(n, UBIFS_LPT_FANOUT);
  60. c->lpt_hght = 1;
  61. n = UBIFS_LPT_FANOUT;
  62. while (n < max_pnode_cnt) {
  63. c->lpt_hght += 1;
  64. n <<= UBIFS_LPT_FANOUT_SHIFT;
  65. }
  66. c->pnode_cnt = DIV_ROUND_UP(c->main_lebs, UBIFS_LPT_FANOUT);
  67. n = DIV_ROUND_UP(c->pnode_cnt, UBIFS_LPT_FANOUT);
  68. c->nnode_cnt = n;
  69. for (i = 1; i < c->lpt_hght; i++) {
  70. n = DIV_ROUND_UP(n, UBIFS_LPT_FANOUT);
  71. c->nnode_cnt += n;
  72. }
  73. c->space_bits = fls(c->leb_size) - 3;
  74. c->lpt_lnum_bits = fls(c->lpt_lebs);
  75. c->lpt_offs_bits = fls(c->leb_size - 1);
  76. c->lpt_spc_bits = fls(c->leb_size);
  77. n = DIV_ROUND_UP(c->max_leb_cnt, UBIFS_LPT_FANOUT);
  78. c->pcnt_bits = fls(n - 1);
  79. c->lnum_bits = fls(c->max_leb_cnt - 1);
  80. bits = UBIFS_LPT_CRC_BITS + UBIFS_LPT_TYPE_BITS +
  81. (c->big_lpt ? c->pcnt_bits : 0) +
  82. (c->space_bits * 2 + 1) * UBIFS_LPT_FANOUT;
  83. c->pnode_sz = (bits + 7) / 8;
  84. bits = UBIFS_LPT_CRC_BITS + UBIFS_LPT_TYPE_BITS +
  85. (c->big_lpt ? c->pcnt_bits : 0) +
  86. (c->lpt_lnum_bits + c->lpt_offs_bits) * UBIFS_LPT_FANOUT;
  87. c->nnode_sz = (bits + 7) / 8;
  88. bits = UBIFS_LPT_CRC_BITS + UBIFS_LPT_TYPE_BITS +
  89. c->lpt_lebs * c->lpt_spc_bits * 2;
  90. c->ltab_sz = (bits + 7) / 8;
  91. bits = UBIFS_LPT_CRC_BITS + UBIFS_LPT_TYPE_BITS +
  92. c->lnum_bits * c->lsave_cnt;
  93. c->lsave_sz = (bits + 7) / 8;
  94. /* Calculate the minimum LPT size */
  95. c->lpt_sz = (long long)c->pnode_cnt * c->pnode_sz;
  96. c->lpt_sz += (long long)c->nnode_cnt * c->nnode_sz;
  97. c->lpt_sz += c->ltab_sz;
  98. if (c->big_lpt)
  99. c->lpt_sz += c->lsave_sz;
  100. /* Add wastage */
  101. sz = c->lpt_sz;
  102. per_leb_wastage = max_t(int, c->pnode_sz, c->nnode_sz);
  103. sz += per_leb_wastage;
  104. tot_wastage = per_leb_wastage;
  105. while (sz > c->leb_size) {
  106. sz += per_leb_wastage;
  107. sz -= c->leb_size;
  108. tot_wastage += per_leb_wastage;
  109. }
  110. tot_wastage += ALIGN(sz, c->min_io_size) - sz;
  111. c->lpt_sz += tot_wastage;
  112. }
  113. /**
  114. * ubifs_calc_lpt_geom - calculate and check sizes for the LPT area.
  115. * @c: the UBIFS file-system description object
  116. *
  117. * This function returns %0 on success and a negative error code on failure.
  118. */
  119. int ubifs_calc_lpt_geom(struct ubifs_info *c)
  120. {
  121. int lebs_needed;
  122. long long sz;
  123. do_calc_lpt_geom(c);
  124. /* Verify that lpt_lebs is big enough */
  125. sz = c->lpt_sz * 2; /* Must have at least 2 times the size */
  126. lebs_needed = div_u64(sz + c->leb_size - 1, c->leb_size);
  127. if (lebs_needed > c->lpt_lebs) {
  128. ubifs_err("too few LPT LEBs");
  129. return -EINVAL;
  130. }
  131. /* Verify that ltab fits in a single LEB (since ltab is a single node */
  132. if (c->ltab_sz > c->leb_size) {
  133. ubifs_err("LPT ltab too big");
  134. return -EINVAL;
  135. }
  136. c->check_lpt_free = c->big_lpt;
  137. return 0;
  138. }
  139. /**
  140. * ubifs_unpack_bits - unpack bit fields.
  141. * @addr: address at which to unpack (passed and next address returned)
  142. * @pos: bit position at which to unpack (passed and next position returned)
  143. * @nrbits: number of bits of value to unpack (1-32)
  144. *
  145. * This functions returns the value unpacked.
  146. */
  147. uint32_t ubifs_unpack_bits(uint8_t **addr, int *pos, int nrbits)
  148. {
  149. const int k = 32 - nrbits;
  150. uint8_t *p = *addr;
  151. int b = *pos;
  152. uint32_t uninitialized_var(val);
  153. const int bytes = (nrbits + b + 7) >> 3;
  154. ubifs_assert(nrbits > 0);
  155. ubifs_assert(nrbits <= 32);
  156. ubifs_assert(*pos >= 0);
  157. ubifs_assert(*pos < 8);
  158. if (b) {
  159. switch (bytes) {
  160. case 2:
  161. val = p[1];
  162. break;
  163. case 3:
  164. val = p[1] | ((uint32_t)p[2] << 8);
  165. break;
  166. case 4:
  167. val = p[1] | ((uint32_t)p[2] << 8) |
  168. ((uint32_t)p[3] << 16);
  169. break;
  170. case 5:
  171. val = p[1] | ((uint32_t)p[2] << 8) |
  172. ((uint32_t)p[3] << 16) |
  173. ((uint32_t)p[4] << 24);
  174. }
  175. val <<= (8 - b);
  176. val |= *p >> b;
  177. nrbits += b;
  178. } else {
  179. switch (bytes) {
  180. case 1:
  181. val = p[0];
  182. break;
  183. case 2:
  184. val = p[0] | ((uint32_t)p[1] << 8);
  185. break;
  186. case 3:
  187. val = p[0] | ((uint32_t)p[1] << 8) |
  188. ((uint32_t)p[2] << 16);
  189. break;
  190. case 4:
  191. val = p[0] | ((uint32_t)p[1] << 8) |
  192. ((uint32_t)p[2] << 16) |
  193. ((uint32_t)p[3] << 24);
  194. break;
  195. }
  196. }
  197. val <<= k;
  198. val >>= k;
  199. b = nrbits & 7;
  200. p += nrbits >> 3;
  201. *addr = p;
  202. *pos = b;
  203. ubifs_assert((val >> nrbits) == 0 || nrbits - b == 32);
  204. return val;
  205. }
  206. /**
  207. * ubifs_add_lpt_dirt - add dirty space to LPT LEB properties.
  208. * @c: UBIFS file-system description object
  209. * @lnum: LEB number to which to add dirty space
  210. * @dirty: amount of dirty space to add
  211. */
  212. void ubifs_add_lpt_dirt(struct ubifs_info *c, int lnum, int dirty)
  213. {
  214. if (!dirty || !lnum)
  215. return;
  216. dbg_lp("LEB %d add %d to %d",
  217. lnum, dirty, c->ltab[lnum - c->lpt_first].dirty);
  218. ubifs_assert(lnum >= c->lpt_first && lnum <= c->lpt_last);
  219. c->ltab[lnum - c->lpt_first].dirty += dirty;
  220. }
  221. /**
  222. * ubifs_add_nnode_dirt - add dirty space to LPT LEB properties.
  223. * @c: UBIFS file-system description object
  224. * @nnode: nnode for which to add dirt
  225. */
  226. void ubifs_add_nnode_dirt(struct ubifs_info *c, struct ubifs_nnode *nnode)
  227. {
  228. struct ubifs_nnode *np = nnode->parent;
  229. if (np)
  230. ubifs_add_lpt_dirt(c, np->nbranch[nnode->iip].lnum,
  231. c->nnode_sz);
  232. else {
  233. ubifs_add_lpt_dirt(c, c->lpt_lnum, c->nnode_sz);
  234. if (!(c->lpt_drty_flgs & LTAB_DIRTY)) {
  235. c->lpt_drty_flgs |= LTAB_DIRTY;
  236. ubifs_add_lpt_dirt(c, c->ltab_lnum, c->ltab_sz);
  237. }
  238. }
  239. }
  240. /**
  241. * add_pnode_dirt - add dirty space to LPT LEB properties.
  242. * @c: UBIFS file-system description object
  243. * @pnode: pnode for which to add dirt
  244. */
  245. static void add_pnode_dirt(struct ubifs_info *c, struct ubifs_pnode *pnode)
  246. {
  247. ubifs_add_lpt_dirt(c, pnode->parent->nbranch[pnode->iip].lnum,
  248. c->pnode_sz);
  249. }
  250. /**
  251. * calc_nnode_num_from_parent - calculate nnode number.
  252. * @c: UBIFS file-system description object
  253. * @parent: parent nnode
  254. * @iip: index in parent
  255. *
  256. * The nnode number is a number that uniquely identifies a nnode and can be used
  257. * easily to traverse the tree from the root to that nnode.
  258. *
  259. * This function calculates and returns the nnode number based on the parent's
  260. * nnode number and the index in parent.
  261. */
  262. static int calc_nnode_num_from_parent(const struct ubifs_info *c,
  263. struct ubifs_nnode *parent, int iip)
  264. {
  265. int num, shft;
  266. if (!parent)
  267. return 1;
  268. shft = (c->lpt_hght - parent->level) * UBIFS_LPT_FANOUT_SHIFT;
  269. num = parent->num ^ (1 << shft);
  270. num |= (UBIFS_LPT_FANOUT + iip) << shft;
  271. return num;
  272. }
  273. /**
  274. * calc_pnode_num_from_parent - calculate pnode number.
  275. * @c: UBIFS file-system description object
  276. * @parent: parent nnode
  277. * @iip: index in parent
  278. *
  279. * The pnode number is a number that uniquely identifies a pnode and can be used
  280. * easily to traverse the tree from the root to that pnode.
  281. *
  282. * This function calculates and returns the pnode number based on the parent's
  283. * nnode number and the index in parent.
  284. */
  285. static int calc_pnode_num_from_parent(const struct ubifs_info *c,
  286. struct ubifs_nnode *parent, int iip)
  287. {
  288. int i, n = c->lpt_hght - 1, pnum = parent->num, num = 0;
  289. for (i = 0; i < n; i++) {
  290. num <<= UBIFS_LPT_FANOUT_SHIFT;
  291. num |= pnum & (UBIFS_LPT_FANOUT - 1);
  292. pnum >>= UBIFS_LPT_FANOUT_SHIFT;
  293. }
  294. num <<= UBIFS_LPT_FANOUT_SHIFT;
  295. num |= iip;
  296. return num;
  297. }
  298. /**
  299. * update_cats - add LEB properties of a pnode to LEB category lists and heaps.
  300. * @c: UBIFS file-system description object
  301. * @pnode: pnode
  302. *
  303. * When a pnode is loaded into memory, the LEB properties it contains are added,
  304. * by this function, to the LEB category lists and heaps.
  305. */
  306. static void update_cats(struct ubifs_info *c, struct ubifs_pnode *pnode)
  307. {
  308. int i;
  309. for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
  310. int cat = pnode->lprops[i].flags & LPROPS_CAT_MASK;
  311. int lnum = pnode->lprops[i].lnum;
  312. if (!lnum)
  313. return;
  314. ubifs_add_to_cat(c, &pnode->lprops[i], cat);
  315. }
  316. }
  317. /**
  318. * replace_cats - add LEB properties of a pnode to LEB category lists and heaps.
  319. * @c: UBIFS file-system description object
  320. * @old_pnode: pnode copied
  321. * @new_pnode: pnode copy
  322. *
  323. * During commit it is sometimes necessary to copy a pnode
  324. * (see dirty_cow_pnode). When that happens, references in
  325. * category lists and heaps must be replaced. This function does that.
  326. */
  327. static void replace_cats(struct ubifs_info *c, struct ubifs_pnode *old_pnode,
  328. struct ubifs_pnode *new_pnode)
  329. {
  330. int i;
  331. for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
  332. if (!new_pnode->lprops[i].lnum)
  333. return;
  334. ubifs_replace_cat(c, &old_pnode->lprops[i],
  335. &new_pnode->lprops[i]);
  336. }
  337. }
  338. /**
  339. * check_lpt_crc - check LPT node crc is correct.
  340. * @c: UBIFS file-system description object
  341. * @buf: buffer containing node
  342. * @len: length of node
  343. *
  344. * This function returns %0 on success and a negative error code on failure.
  345. */
  346. static int check_lpt_crc(void *buf, int len)
  347. {
  348. int pos = 0;
  349. uint8_t *addr = buf;
  350. uint16_t crc, calc_crc;
  351. crc = ubifs_unpack_bits(&addr, &pos, UBIFS_LPT_CRC_BITS);
  352. calc_crc = crc16(-1, buf + UBIFS_LPT_CRC_BYTES,
  353. len - UBIFS_LPT_CRC_BYTES);
  354. if (crc != calc_crc) {
  355. ubifs_err("invalid crc in LPT node: crc %hx calc %hx", crc,
  356. calc_crc);
  357. dbg_dump_stack();
  358. return -EINVAL;
  359. }
  360. return 0;
  361. }
  362. /**
  363. * check_lpt_type - check LPT node type is correct.
  364. * @c: UBIFS file-system description object
  365. * @addr: address of type bit field is passed and returned updated here
  366. * @pos: position of type bit field is passed and returned updated here
  367. * @type: expected type
  368. *
  369. * This function returns %0 on success and a negative error code on failure.
  370. */
  371. static int check_lpt_type(uint8_t **addr, int *pos, int type)
  372. {
  373. int node_type;
  374. node_type = ubifs_unpack_bits(addr, pos, UBIFS_LPT_TYPE_BITS);
  375. if (node_type != type) {
  376. ubifs_err("invalid type (%d) in LPT node type %d", node_type,
  377. type);
  378. dbg_dump_stack();
  379. return -EINVAL;
  380. }
  381. return 0;
  382. }
  383. /**
  384. * unpack_pnode - unpack a pnode.
  385. * @c: UBIFS file-system description object
  386. * @buf: buffer containing packed pnode to unpack
  387. * @pnode: pnode structure to fill
  388. *
  389. * This function returns %0 on success and a negative error code on failure.
  390. */
  391. static int unpack_pnode(const struct ubifs_info *c, void *buf,
  392. struct ubifs_pnode *pnode)
  393. {
  394. uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
  395. int i, pos = 0, err;
  396. err = check_lpt_type(&addr, &pos, UBIFS_LPT_PNODE);
  397. if (err)
  398. return err;
  399. if (c->big_lpt)
  400. pnode->num = ubifs_unpack_bits(&addr, &pos, c->pcnt_bits);
  401. for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
  402. struct ubifs_lprops * const lprops = &pnode->lprops[i];
  403. lprops->free = ubifs_unpack_bits(&addr, &pos, c->space_bits);
  404. lprops->free <<= 3;
  405. lprops->dirty = ubifs_unpack_bits(&addr, &pos, c->space_bits);
  406. lprops->dirty <<= 3;
  407. if (ubifs_unpack_bits(&addr, &pos, 1))
  408. lprops->flags = LPROPS_INDEX;
  409. else
  410. lprops->flags = 0;
  411. lprops->flags |= ubifs_categorize_lprops(c, lprops);
  412. }
  413. err = check_lpt_crc(buf, c->pnode_sz);
  414. return err;
  415. }
  416. /**
  417. * ubifs_unpack_nnode - unpack a nnode.
  418. * @c: UBIFS file-system description object
  419. * @buf: buffer containing packed nnode to unpack
  420. * @nnode: nnode structure to fill
  421. *
  422. * This function returns %0 on success and a negative error code on failure.
  423. */
  424. int ubifs_unpack_nnode(const struct ubifs_info *c, void *buf,
  425. struct ubifs_nnode *nnode)
  426. {
  427. uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
  428. int i, pos = 0, err;
  429. err = check_lpt_type(&addr, &pos, UBIFS_LPT_NNODE);
  430. if (err)
  431. return err;
  432. if (c->big_lpt)
  433. nnode->num = ubifs_unpack_bits(&addr, &pos, c->pcnt_bits);
  434. for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
  435. int lnum;
  436. lnum = ubifs_unpack_bits(&addr, &pos, c->lpt_lnum_bits) +
  437. c->lpt_first;
  438. if (lnum == c->lpt_last + 1)
  439. lnum = 0;
  440. nnode->nbranch[i].lnum = lnum;
  441. nnode->nbranch[i].offs = ubifs_unpack_bits(&addr, &pos,
  442. c->lpt_offs_bits);
  443. }
  444. err = check_lpt_crc(buf, c->nnode_sz);
  445. return err;
  446. }
  447. /**
  448. * unpack_ltab - unpack the LPT's own lprops table.
  449. * @c: UBIFS file-system description object
  450. * @buf: buffer from which to unpack
  451. *
  452. * This function returns %0 on success and a negative error code on failure.
  453. */
  454. static int unpack_ltab(const struct ubifs_info *c, void *buf)
  455. {
  456. uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
  457. int i, pos = 0, err;
  458. err = check_lpt_type(&addr, &pos, UBIFS_LPT_LTAB);
  459. if (err)
  460. return err;
  461. for (i = 0; i < c->lpt_lebs; i++) {
  462. int free = ubifs_unpack_bits(&addr, &pos, c->lpt_spc_bits);
  463. int dirty = ubifs_unpack_bits(&addr, &pos, c->lpt_spc_bits);
  464. if (free < 0 || free > c->leb_size || dirty < 0 ||
  465. dirty > c->leb_size || free + dirty > c->leb_size)
  466. return -EINVAL;
  467. c->ltab[i].free = free;
  468. c->ltab[i].dirty = dirty;
  469. c->ltab[i].tgc = 0;
  470. c->ltab[i].cmt = 0;
  471. }
  472. err = check_lpt_crc(buf, c->ltab_sz);
  473. return err;
  474. }
  475. /**
  476. * validate_nnode - validate a nnode.
  477. * @c: UBIFS file-system description object
  478. * @nnode: nnode to validate
  479. * @parent: parent nnode (or NULL for the root nnode)
  480. * @iip: index in parent
  481. *
  482. * This function returns %0 on success and a negative error code on failure.
  483. */
  484. static int validate_nnode(const struct ubifs_info *c, struct ubifs_nnode *nnode,
  485. struct ubifs_nnode *parent, int iip)
  486. {
  487. int i, lvl, max_offs;
  488. if (c->big_lpt) {
  489. int num = calc_nnode_num_from_parent(c, parent, iip);
  490. if (nnode->num != num)
  491. return -EINVAL;
  492. }
  493. lvl = parent ? parent->level - 1 : c->lpt_hght;
  494. if (lvl < 1)
  495. return -EINVAL;
  496. if (lvl == 1)
  497. max_offs = c->leb_size - c->pnode_sz;
  498. else
  499. max_offs = c->leb_size - c->nnode_sz;
  500. for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
  501. int lnum = nnode->nbranch[i].lnum;
  502. int offs = nnode->nbranch[i].offs;
  503. if (lnum == 0) {
  504. if (offs != 0)
  505. return -EINVAL;
  506. continue;
  507. }
  508. if (lnum < c->lpt_first || lnum > c->lpt_last)
  509. return -EINVAL;
  510. if (offs < 0 || offs > max_offs)
  511. return -EINVAL;
  512. }
  513. return 0;
  514. }
  515. /**
  516. * validate_pnode - validate a pnode.
  517. * @c: UBIFS file-system description object
  518. * @pnode: pnode to validate
  519. * @parent: parent nnode
  520. * @iip: index in parent
  521. *
  522. * This function returns %0 on success and a negative error code on failure.
  523. */
  524. static int validate_pnode(const struct ubifs_info *c, struct ubifs_pnode *pnode,
  525. struct ubifs_nnode *parent, int iip)
  526. {
  527. int i;
  528. if (c->big_lpt) {
  529. int num = calc_pnode_num_from_parent(c, parent, iip);
  530. if (pnode->num != num)
  531. return -EINVAL;
  532. }
  533. for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
  534. int free = pnode->lprops[i].free;
  535. int dirty = pnode->lprops[i].dirty;
  536. if (free < 0 || free > c->leb_size || free % c->min_io_size ||
  537. (free & 7))
  538. return -EINVAL;
  539. if (dirty < 0 || dirty > c->leb_size || (dirty & 7))
  540. return -EINVAL;
  541. if (dirty + free > c->leb_size)
  542. return -EINVAL;
  543. }
  544. return 0;
  545. }
  546. /**
  547. * set_pnode_lnum - set LEB numbers on a pnode.
  548. * @c: UBIFS file-system description object
  549. * @pnode: pnode to update
  550. *
  551. * This function calculates the LEB numbers for the LEB properties it contains
  552. * based on the pnode number.
  553. */
  554. static void set_pnode_lnum(const struct ubifs_info *c,
  555. struct ubifs_pnode *pnode)
  556. {
  557. int i, lnum;
  558. lnum = (pnode->num << UBIFS_LPT_FANOUT_SHIFT) + c->main_first;
  559. for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
  560. if (lnum >= c->leb_cnt)
  561. return;
  562. pnode->lprops[i].lnum = lnum++;
  563. }
  564. }
  565. /**
  566. * ubifs_read_nnode - read a nnode from flash and link it to the tree in memory.
  567. * @c: UBIFS file-system description object
  568. * @parent: parent nnode (or NULL for the root)
  569. * @iip: index in parent
  570. *
  571. * This function returns %0 on success and a negative error code on failure.
  572. */
  573. int ubifs_read_nnode(struct ubifs_info *c, struct ubifs_nnode *parent, int iip)
  574. {
  575. struct ubifs_nbranch *branch = NULL;
  576. struct ubifs_nnode *nnode = NULL;
  577. void *buf = c->lpt_nod_buf;
  578. int err, lnum, offs;
  579. if (parent) {
  580. branch = &parent->nbranch[iip];
  581. lnum = branch->lnum;
  582. offs = branch->offs;
  583. } else {
  584. lnum = c->lpt_lnum;
  585. offs = c->lpt_offs;
  586. }
  587. nnode = kzalloc(sizeof(struct ubifs_nnode), GFP_NOFS);
  588. if (!nnode) {
  589. err = -ENOMEM;
  590. goto out;
  591. }
  592. if (lnum == 0) {
  593. /*
  594. * This nnode was not written which just means that the LEB
  595. * properties in the subtree below it describe empty LEBs. We
  596. * make the nnode as though we had read it, which in fact means
  597. * doing almost nothing.
  598. */
  599. if (c->big_lpt)
  600. nnode->num = calc_nnode_num_from_parent(c, parent, iip);
  601. } else {
  602. err = ubi_read(c->ubi, lnum, buf, offs, c->nnode_sz);
  603. if (err)
  604. goto out;
  605. err = ubifs_unpack_nnode(c, buf, nnode);
  606. if (err)
  607. goto out;
  608. }
  609. err = validate_nnode(c, nnode, parent, iip);
  610. if (err)
  611. goto out;
  612. if (!c->big_lpt)
  613. nnode->num = calc_nnode_num_from_parent(c, parent, iip);
  614. if (parent) {
  615. branch->nnode = nnode;
  616. nnode->level = parent->level - 1;
  617. } else {
  618. c->nroot = nnode;
  619. nnode->level = c->lpt_hght;
  620. }
  621. nnode->parent = parent;
  622. nnode->iip = iip;
  623. return 0;
  624. out:
  625. ubifs_err("error %d reading nnode at %d:%d", err, lnum, offs);
  626. kfree(nnode);
  627. return err;
  628. }
  629. /**
  630. * read_pnode - read a pnode from flash and link it to the tree in memory.
  631. * @c: UBIFS file-system description object
  632. * @parent: parent nnode
  633. * @iip: index in parent
  634. *
  635. * This function returns %0 on success and a negative error code on failure.
  636. */
  637. static int read_pnode(struct ubifs_info *c, struct ubifs_nnode *parent, int iip)
  638. {
  639. struct ubifs_nbranch *branch;
  640. struct ubifs_pnode *pnode = NULL;
  641. void *buf = c->lpt_nod_buf;
  642. int err, lnum, offs;
  643. branch = &parent->nbranch[iip];
  644. lnum = branch->lnum;
  645. offs = branch->offs;
  646. pnode = kzalloc(sizeof(struct ubifs_pnode), GFP_NOFS);
  647. if (!pnode) {
  648. err = -ENOMEM;
  649. goto out;
  650. }
  651. if (lnum == 0) {
  652. /*
  653. * This pnode was not written which just means that the LEB
  654. * properties in it describe empty LEBs. We make the pnode as
  655. * though we had read it.
  656. */
  657. int i;
  658. if (c->big_lpt)
  659. pnode->num = calc_pnode_num_from_parent(c, parent, iip);
  660. for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
  661. struct ubifs_lprops * const lprops = &pnode->lprops[i];
  662. lprops->free = c->leb_size;
  663. lprops->flags = ubifs_categorize_lprops(c, lprops);
  664. }
  665. } else {
  666. err = ubi_read(c->ubi, lnum, buf, offs, c->pnode_sz);
  667. if (err)
  668. goto out;
  669. err = unpack_pnode(c, buf, pnode);
  670. if (err)
  671. goto out;
  672. }
  673. err = validate_pnode(c, pnode, parent, iip);
  674. if (err)
  675. goto out;
  676. if (!c->big_lpt)
  677. pnode->num = calc_pnode_num_from_parent(c, parent, iip);
  678. branch->pnode = pnode;
  679. pnode->parent = parent;
  680. pnode->iip = iip;
  681. set_pnode_lnum(c, pnode);
  682. c->pnodes_have += 1;
  683. return 0;
  684. out:
  685. ubifs_err("error %d reading pnode at %d:%d", err, lnum, offs);
  686. dbg_dump_pnode(c, pnode, parent, iip);
  687. dbg_msg("calc num: %d", calc_pnode_num_from_parent(c, parent, iip));
  688. kfree(pnode);
  689. return err;
  690. }
  691. /**
  692. * read_ltab - read LPT's own lprops table.
  693. * @c: UBIFS file-system description object
  694. *
  695. * This function returns %0 on success and a negative error code on failure.
  696. */
  697. static int read_ltab(struct ubifs_info *c)
  698. {
  699. int err;
  700. void *buf;
  701. buf = vmalloc(c->ltab_sz);
  702. if (!buf)
  703. return -ENOMEM;
  704. err = ubi_read(c->ubi, c->ltab_lnum, buf, c->ltab_offs, c->ltab_sz);
  705. if (err)
  706. goto out;
  707. err = unpack_ltab(c, buf);
  708. out:
  709. vfree(buf);
  710. return err;
  711. }
  712. /**
  713. * ubifs_get_nnode - get a nnode.
  714. * @c: UBIFS file-system description object
  715. * @parent: parent nnode (or NULL for the root)
  716. * @iip: index in parent
  717. *
  718. * This function returns a pointer to the nnode on success or a negative error
  719. * code on failure.
  720. */
  721. struct ubifs_nnode *ubifs_get_nnode(struct ubifs_info *c,
  722. struct ubifs_nnode *parent, int iip)
  723. {
  724. struct ubifs_nbranch *branch;
  725. struct ubifs_nnode *nnode;
  726. int err;
  727. branch = &parent->nbranch[iip];
  728. nnode = branch->nnode;
  729. if (nnode)
  730. return nnode;
  731. err = ubifs_read_nnode(c, parent, iip);
  732. if (err)
  733. return ERR_PTR(err);
  734. return branch->nnode;
  735. }
  736. /**
  737. * ubifs_get_pnode - get a pnode.
  738. * @c: UBIFS file-system description object
  739. * @parent: parent nnode
  740. * @iip: index in parent
  741. *
  742. * This function returns a pointer to the pnode on success or a negative error
  743. * code on failure.
  744. */
  745. struct ubifs_pnode *ubifs_get_pnode(struct ubifs_info *c,
  746. struct ubifs_nnode *parent, int iip)
  747. {
  748. struct ubifs_nbranch *branch;
  749. struct ubifs_pnode *pnode;
  750. int err;
  751. branch = &parent->nbranch[iip];
  752. pnode = branch->pnode;
  753. if (pnode)
  754. return pnode;
  755. err = read_pnode(c, parent, iip);
  756. if (err)
  757. return ERR_PTR(err);
  758. update_cats(c, branch->pnode);
  759. return branch->pnode;
  760. }
  761. /**
  762. * ubifs_lpt_lookup - lookup LEB properties in the LPT.
  763. * @c: UBIFS file-system description object
  764. * @lnum: LEB number to lookup
  765. *
  766. * This function returns a pointer to the LEB properties on success or a
  767. * negative error code on failure.
  768. */
  769. struct ubifs_lprops *ubifs_lpt_lookup(struct ubifs_info *c, int lnum)
  770. {
  771. int err, i, h, iip, shft;
  772. struct ubifs_nnode *nnode;
  773. struct ubifs_pnode *pnode;
  774. if (!c->nroot) {
  775. err = ubifs_read_nnode(c, NULL, 0);
  776. if (err)
  777. return ERR_PTR(err);
  778. }
  779. nnode = c->nroot;
  780. i = lnum - c->main_first;
  781. shft = c->lpt_hght * UBIFS_LPT_FANOUT_SHIFT;
  782. for (h = 1; h < c->lpt_hght; h++) {
  783. iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
  784. shft -= UBIFS_LPT_FANOUT_SHIFT;
  785. nnode = ubifs_get_nnode(c, nnode, iip);
  786. if (IS_ERR(nnode))
  787. return ERR_PTR(PTR_ERR(nnode));
  788. }
  789. iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
  790. shft -= UBIFS_LPT_FANOUT_SHIFT;
  791. pnode = ubifs_get_pnode(c, nnode, iip);
  792. if (IS_ERR(pnode))
  793. return ERR_PTR(PTR_ERR(pnode));
  794. iip = (i & (UBIFS_LPT_FANOUT - 1));
  795. dbg_lp("LEB %d, free %d, dirty %d, flags %d", lnum,
  796. pnode->lprops[iip].free, pnode->lprops[iip].dirty,
  797. pnode->lprops[iip].flags);
  798. return &pnode->lprops[iip];
  799. }
  800. /**
  801. * dirty_cow_nnode - ensure a nnode is not being committed.
  802. * @c: UBIFS file-system description object
  803. * @nnode: nnode to check
  804. *
  805. * Returns dirtied nnode on success or negative error code on failure.
  806. */
  807. static struct ubifs_nnode *dirty_cow_nnode(struct ubifs_info *c,
  808. struct ubifs_nnode *nnode)
  809. {
  810. struct ubifs_nnode *n;
  811. int i;
  812. if (!test_bit(COW_CNODE, &nnode->flags)) {
  813. /* nnode is not being committed */
  814. if (!test_and_set_bit(DIRTY_CNODE, &nnode->flags)) {
  815. c->dirty_nn_cnt += 1;
  816. ubifs_add_nnode_dirt(c, nnode);
  817. }
  818. return nnode;
  819. }
  820. /* nnode is being committed, so copy it */
  821. n = kmalloc(sizeof(struct ubifs_nnode), GFP_NOFS);
  822. if (unlikely(!n))
  823. return ERR_PTR(-ENOMEM);
  824. memcpy(n, nnode, sizeof(struct ubifs_nnode));
  825. n->cnext = NULL;
  826. __set_bit(DIRTY_CNODE, &n->flags);
  827. __clear_bit(COW_CNODE, &n->flags);
  828. /* The children now have new parent */
  829. for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
  830. struct ubifs_nbranch *branch = &n->nbranch[i];
  831. if (branch->cnode)
  832. branch->cnode->parent = n;
  833. }
  834. ubifs_assert(!test_bit(OBSOLETE_CNODE, &nnode->flags));
  835. __set_bit(OBSOLETE_CNODE, &nnode->flags);
  836. c->dirty_nn_cnt += 1;
  837. ubifs_add_nnode_dirt(c, nnode);
  838. if (nnode->parent)
  839. nnode->parent->nbranch[n->iip].nnode = n;
  840. else
  841. c->nroot = n;
  842. return n;
  843. }
  844. /**
  845. * dirty_cow_pnode - ensure a pnode is not being committed.
  846. * @c: UBIFS file-system description object
  847. * @pnode: pnode to check
  848. *
  849. * Returns dirtied pnode on success or negative error code on failure.
  850. */
  851. static struct ubifs_pnode *dirty_cow_pnode(struct ubifs_info *c,
  852. struct ubifs_pnode *pnode)
  853. {
  854. struct ubifs_pnode *p;
  855. if (!test_bit(COW_CNODE, &pnode->flags)) {
  856. /* pnode is not being committed */
  857. if (!test_and_set_bit(DIRTY_CNODE, &pnode->flags)) {
  858. c->dirty_pn_cnt += 1;
  859. add_pnode_dirt(c, pnode);
  860. }
  861. return pnode;
  862. }
  863. /* pnode is being committed, so copy it */
  864. p = kmalloc(sizeof(struct ubifs_pnode), GFP_NOFS);
  865. if (unlikely(!p))
  866. return ERR_PTR(-ENOMEM);
  867. memcpy(p, pnode, sizeof(struct ubifs_pnode));
  868. p->cnext = NULL;
  869. __set_bit(DIRTY_CNODE, &p->flags);
  870. __clear_bit(COW_CNODE, &p->flags);
  871. replace_cats(c, pnode, p);
  872. ubifs_assert(!test_bit(OBSOLETE_CNODE, &pnode->flags));
  873. __set_bit(OBSOLETE_CNODE, &pnode->flags);
  874. c->dirty_pn_cnt += 1;
  875. add_pnode_dirt(c, pnode);
  876. pnode->parent->nbranch[p->iip].pnode = p;
  877. return p;
  878. }
  879. /**
  880. * ubifs_lpt_lookup_dirty - lookup LEB properties in the LPT.
  881. * @c: UBIFS file-system description object
  882. * @lnum: LEB number to lookup
  883. *
  884. * This function returns a pointer to the LEB properties on success or a
  885. * negative error code on failure.
  886. */
  887. struct ubifs_lprops *ubifs_lpt_lookup_dirty(struct ubifs_info *c, int lnum)
  888. {
  889. int err, i, h, iip, shft;
  890. struct ubifs_nnode *nnode;
  891. struct ubifs_pnode *pnode;
  892. if (!c->nroot) {
  893. err = ubifs_read_nnode(c, NULL, 0);
  894. if (err)
  895. return ERR_PTR(err);
  896. }
  897. nnode = c->nroot;
  898. nnode = dirty_cow_nnode(c, nnode);
  899. if (IS_ERR(nnode))
  900. return ERR_PTR(PTR_ERR(nnode));
  901. i = lnum - c->main_first;
  902. shft = c->lpt_hght * UBIFS_LPT_FANOUT_SHIFT;
  903. for (h = 1; h < c->lpt_hght; h++) {
  904. iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
  905. shft -= UBIFS_LPT_FANOUT_SHIFT;
  906. nnode = ubifs_get_nnode(c, nnode, iip);
  907. if (IS_ERR(nnode))
  908. return ERR_PTR(PTR_ERR(nnode));
  909. nnode = dirty_cow_nnode(c, nnode);
  910. if (IS_ERR(nnode))
  911. return ERR_PTR(PTR_ERR(nnode));
  912. }
  913. iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
  914. shft -= UBIFS_LPT_FANOUT_SHIFT;
  915. pnode = ubifs_get_pnode(c, nnode, iip);
  916. if (IS_ERR(pnode))
  917. return ERR_PTR(PTR_ERR(pnode));
  918. pnode = dirty_cow_pnode(c, pnode);
  919. if (IS_ERR(pnode))
  920. return ERR_PTR(PTR_ERR(pnode));
  921. iip = (i & (UBIFS_LPT_FANOUT - 1));
  922. dbg_lp("LEB %d, free %d, dirty %d, flags %d", lnum,
  923. pnode->lprops[iip].free, pnode->lprops[iip].dirty,
  924. pnode->lprops[iip].flags);
  925. ubifs_assert(test_bit(DIRTY_CNODE, &pnode->flags));
  926. return &pnode->lprops[iip];
  927. }
  928. /**
  929. * lpt_init_rd - initialize the LPT for reading.
  930. * @c: UBIFS file-system description object
  931. *
  932. * This function returns %0 on success and a negative error code on failure.
  933. */
  934. static int lpt_init_rd(struct ubifs_info *c)
  935. {
  936. int err, i;
  937. c->ltab = vmalloc(sizeof(struct ubifs_lpt_lprops) * c->lpt_lebs);
  938. if (!c->ltab)
  939. return -ENOMEM;
  940. i = max_t(int, c->nnode_sz, c->pnode_sz);
  941. c->lpt_nod_buf = kmalloc(i, GFP_KERNEL);
  942. if (!c->lpt_nod_buf)
  943. return -ENOMEM;
  944. for (i = 0; i < LPROPS_HEAP_CNT; i++) {
  945. c->lpt_heap[i].arr = kmalloc(sizeof(void *) * LPT_HEAP_SZ,
  946. GFP_KERNEL);
  947. if (!c->lpt_heap[i].arr)
  948. return -ENOMEM;
  949. c->lpt_heap[i].cnt = 0;
  950. c->lpt_heap[i].max_cnt = LPT_HEAP_SZ;
  951. }
  952. c->dirty_idx.arr = kmalloc(sizeof(void *) * LPT_HEAP_SZ, GFP_KERNEL);
  953. if (!c->dirty_idx.arr)
  954. return -ENOMEM;
  955. c->dirty_idx.cnt = 0;
  956. c->dirty_idx.max_cnt = LPT_HEAP_SZ;
  957. err = read_ltab(c);
  958. if (err)
  959. return err;
  960. dbg_lp("space_bits %d", c->space_bits);
  961. dbg_lp("lpt_lnum_bits %d", c->lpt_lnum_bits);
  962. dbg_lp("lpt_offs_bits %d", c->lpt_offs_bits);
  963. dbg_lp("lpt_spc_bits %d", c->lpt_spc_bits);
  964. dbg_lp("pcnt_bits %d", c->pcnt_bits);
  965. dbg_lp("lnum_bits %d", c->lnum_bits);
  966. dbg_lp("pnode_sz %d", c->pnode_sz);
  967. dbg_lp("nnode_sz %d", c->nnode_sz);
  968. dbg_lp("ltab_sz %d", c->ltab_sz);
  969. dbg_lp("lsave_sz %d", c->lsave_sz);
  970. dbg_lp("lsave_cnt %d", c->lsave_cnt);
  971. dbg_lp("lpt_hght %d", c->lpt_hght);
  972. dbg_lp("big_lpt %d", c->big_lpt);
  973. dbg_lp("LPT root is at %d:%d", c->lpt_lnum, c->lpt_offs);
  974. dbg_lp("LPT head is at %d:%d", c->nhead_lnum, c->nhead_offs);
  975. dbg_lp("LPT ltab is at %d:%d", c->ltab_lnum, c->ltab_offs);
  976. if (c->big_lpt)
  977. dbg_lp("LPT lsave is at %d:%d", c->lsave_lnum, c->lsave_offs);
  978. return 0;
  979. }
  980. /**
  981. * ubifs_lpt_init - initialize the LPT.
  982. * @c: UBIFS file-system description object
  983. * @rd: whether to initialize lpt for reading
  984. * @wr: whether to initialize lpt for writing
  985. *
  986. * For mounting 'rw', @rd and @wr are both true. For mounting 'ro', @rd is true
  987. * and @wr is false. For mounting from 'ro' to 'rw', @rd is false and @wr is
  988. * true.
  989. *
  990. * This function returns %0 on success and a negative error code on failure.
  991. */
  992. int ubifs_lpt_init(struct ubifs_info *c, int rd, int wr)
  993. {
  994. int err;
  995. if (rd) {
  996. err = lpt_init_rd(c);
  997. if (err)
  998. return err;
  999. }
  1000. return 0;
  1001. }