lpt.c 59 KB

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