lpt.c 59 KB

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