lpt.c 58 KB

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