lpt.c 59 KB

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