lpt.c 59 KB

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