lpt.c 58 KB

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