lpt.c 59 KB

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