lpt.c 59 KB

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