lpt.c 59 KB

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