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. err = -ENOMEM;
  1159. goto out;
  1160. }
  1161. if (lnum == 0) {
  1162. /*
  1163. * This pnode was not written which just means that the LEB
  1164. * properties in it describe empty LEBs. We make the pnode as
  1165. * though we had read it.
  1166. */
  1167. int i;
  1168. if (c->big_lpt)
  1169. pnode->num = calc_pnode_num_from_parent(c, parent, iip);
  1170. for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
  1171. struct ubifs_lprops * const lprops = &pnode->lprops[i];
  1172. lprops->free = c->leb_size;
  1173. lprops->flags = ubifs_categorize_lprops(c, lprops);
  1174. }
  1175. } else {
  1176. err = ubi_read(c->ubi, lnum, buf, offs, c->pnode_sz);
  1177. if (err)
  1178. goto out;
  1179. err = unpack_pnode(c, buf, pnode);
  1180. if (err)
  1181. goto out;
  1182. }
  1183. err = validate_pnode(c, pnode, parent, iip);
  1184. if (err)
  1185. goto out;
  1186. if (!c->big_lpt)
  1187. pnode->num = calc_pnode_num_from_parent(c, parent, iip);
  1188. branch->pnode = pnode;
  1189. pnode->parent = parent;
  1190. pnode->iip = iip;
  1191. set_pnode_lnum(c, pnode);
  1192. c->pnodes_have += 1;
  1193. return 0;
  1194. out:
  1195. ubifs_err("error %d reading pnode at %d:%d", err, lnum, offs);
  1196. dbg_dump_pnode(c, pnode, parent, iip);
  1197. dbg_msg("calc num: %d", calc_pnode_num_from_parent(c, parent, iip));
  1198. kfree(pnode);
  1199. return err;
  1200. }
  1201. /**
  1202. * read_ltab - read LPT's own lprops table.
  1203. * @c: UBIFS file-system description object
  1204. *
  1205. * This function returns %0 on success and a negative error code on failure.
  1206. */
  1207. static int read_ltab(struct ubifs_info *c)
  1208. {
  1209. int err;
  1210. void *buf;
  1211. buf = vmalloc(c->ltab_sz);
  1212. if (!buf)
  1213. return -ENOMEM;
  1214. err = ubi_read(c->ubi, c->ltab_lnum, buf, c->ltab_offs, c->ltab_sz);
  1215. if (err)
  1216. goto out;
  1217. err = unpack_ltab(c, buf);
  1218. out:
  1219. vfree(buf);
  1220. return err;
  1221. }
  1222. /**
  1223. * read_lsave - read LPT's save table.
  1224. * @c: UBIFS file-system description object
  1225. *
  1226. * This function returns %0 on success and a negative error code on failure.
  1227. */
  1228. static int read_lsave(struct ubifs_info *c)
  1229. {
  1230. int err, i;
  1231. void *buf;
  1232. buf = vmalloc(c->lsave_sz);
  1233. if (!buf)
  1234. return -ENOMEM;
  1235. err = ubi_read(c->ubi, c->lsave_lnum, buf, c->lsave_offs, c->lsave_sz);
  1236. if (err)
  1237. goto out;
  1238. err = unpack_lsave(c, buf);
  1239. if (err)
  1240. goto out;
  1241. for (i = 0; i < c->lsave_cnt; i++) {
  1242. int lnum = c->lsave[i];
  1243. struct ubifs_lprops *lprops;
  1244. /*
  1245. * Due to automatic resizing, the values in the lsave table
  1246. * could be beyond the volume size - just ignore them.
  1247. */
  1248. if (lnum >= c->leb_cnt)
  1249. continue;
  1250. lprops = ubifs_lpt_lookup(c, lnum);
  1251. if (IS_ERR(lprops)) {
  1252. err = PTR_ERR(lprops);
  1253. goto out;
  1254. }
  1255. }
  1256. out:
  1257. vfree(buf);
  1258. return err;
  1259. }
  1260. /**
  1261. * ubifs_get_nnode - get a nnode.
  1262. * @c: UBIFS file-system description object
  1263. * @parent: parent nnode (or NULL for the root)
  1264. * @iip: index in parent
  1265. *
  1266. * This function returns a pointer to the nnode on success or a negative error
  1267. * code on failure.
  1268. */
  1269. struct ubifs_nnode *ubifs_get_nnode(struct ubifs_info *c,
  1270. struct ubifs_nnode *parent, int iip)
  1271. {
  1272. struct ubifs_nbranch *branch;
  1273. struct ubifs_nnode *nnode;
  1274. int err;
  1275. branch = &parent->nbranch[iip];
  1276. nnode = branch->nnode;
  1277. if (nnode)
  1278. return nnode;
  1279. err = ubifs_read_nnode(c, parent, iip);
  1280. if (err)
  1281. return ERR_PTR(err);
  1282. return branch->nnode;
  1283. }
  1284. /**
  1285. * ubifs_get_pnode - get a pnode.
  1286. * @c: UBIFS file-system description object
  1287. * @parent: parent nnode
  1288. * @iip: index in parent
  1289. *
  1290. * This function returns a pointer to the pnode on success or a negative error
  1291. * code on failure.
  1292. */
  1293. struct ubifs_pnode *ubifs_get_pnode(struct ubifs_info *c,
  1294. struct ubifs_nnode *parent, int iip)
  1295. {
  1296. struct ubifs_nbranch *branch;
  1297. struct ubifs_pnode *pnode;
  1298. int err;
  1299. branch = &parent->nbranch[iip];
  1300. pnode = branch->pnode;
  1301. if (pnode)
  1302. return pnode;
  1303. err = read_pnode(c, parent, iip);
  1304. if (err)
  1305. return ERR_PTR(err);
  1306. update_cats(c, branch->pnode);
  1307. return branch->pnode;
  1308. }
  1309. /**
  1310. * ubifs_lpt_lookup - lookup LEB properties in the LPT.
  1311. * @c: UBIFS file-system description object
  1312. * @lnum: LEB number to lookup
  1313. *
  1314. * This function returns a pointer to the LEB properties on success or a
  1315. * negative error code on failure.
  1316. */
  1317. struct ubifs_lprops *ubifs_lpt_lookup(struct ubifs_info *c, int lnum)
  1318. {
  1319. int err, i, h, iip, shft;
  1320. struct ubifs_nnode *nnode;
  1321. struct ubifs_pnode *pnode;
  1322. if (!c->nroot) {
  1323. err = ubifs_read_nnode(c, NULL, 0);
  1324. if (err)
  1325. return ERR_PTR(err);
  1326. }
  1327. nnode = c->nroot;
  1328. i = lnum - c->main_first;
  1329. shft = c->lpt_hght * UBIFS_LPT_FANOUT_SHIFT;
  1330. for (h = 1; h < c->lpt_hght; h++) {
  1331. iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
  1332. shft -= UBIFS_LPT_FANOUT_SHIFT;
  1333. nnode = ubifs_get_nnode(c, nnode, iip);
  1334. if (IS_ERR(nnode))
  1335. return ERR_CAST(nnode);
  1336. }
  1337. iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
  1338. shft -= UBIFS_LPT_FANOUT_SHIFT;
  1339. pnode = ubifs_get_pnode(c, nnode, iip);
  1340. if (IS_ERR(pnode))
  1341. return ERR_CAST(pnode);
  1342. iip = (i & (UBIFS_LPT_FANOUT - 1));
  1343. dbg_lp("LEB %d, free %d, dirty %d, flags %d", lnum,
  1344. pnode->lprops[iip].free, pnode->lprops[iip].dirty,
  1345. pnode->lprops[iip].flags);
  1346. return &pnode->lprops[iip];
  1347. }
  1348. /**
  1349. * dirty_cow_nnode - ensure a nnode is not being committed.
  1350. * @c: UBIFS file-system description object
  1351. * @nnode: nnode to check
  1352. *
  1353. * Returns dirtied nnode on success or negative error code on failure.
  1354. */
  1355. static struct ubifs_nnode *dirty_cow_nnode(struct ubifs_info *c,
  1356. struct ubifs_nnode *nnode)
  1357. {
  1358. struct ubifs_nnode *n;
  1359. int i;
  1360. if (!test_bit(COW_CNODE, &nnode->flags)) {
  1361. /* nnode is not being committed */
  1362. if (!test_and_set_bit(DIRTY_CNODE, &nnode->flags)) {
  1363. c->dirty_nn_cnt += 1;
  1364. ubifs_add_nnode_dirt(c, nnode);
  1365. }
  1366. return nnode;
  1367. }
  1368. /* nnode is being committed, so copy it */
  1369. n = kmalloc(sizeof(struct ubifs_nnode), GFP_NOFS);
  1370. if (unlikely(!n))
  1371. return ERR_PTR(-ENOMEM);
  1372. memcpy(n, nnode, sizeof(struct ubifs_nnode));
  1373. n->cnext = NULL;
  1374. __set_bit(DIRTY_CNODE, &n->flags);
  1375. __clear_bit(COW_CNODE, &n->flags);
  1376. /* The children now have new parent */
  1377. for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
  1378. struct ubifs_nbranch *branch = &n->nbranch[i];
  1379. if (branch->cnode)
  1380. branch->cnode->parent = n;
  1381. }
  1382. ubifs_assert(!test_bit(OBSOLETE_CNODE, &nnode->flags));
  1383. __set_bit(OBSOLETE_CNODE, &nnode->flags);
  1384. c->dirty_nn_cnt += 1;
  1385. ubifs_add_nnode_dirt(c, nnode);
  1386. if (nnode->parent)
  1387. nnode->parent->nbranch[n->iip].nnode = n;
  1388. else
  1389. c->nroot = n;
  1390. return n;
  1391. }
  1392. /**
  1393. * dirty_cow_pnode - ensure a pnode is not being committed.
  1394. * @c: UBIFS file-system description object
  1395. * @pnode: pnode to check
  1396. *
  1397. * Returns dirtied pnode on success or negative error code on failure.
  1398. */
  1399. static struct ubifs_pnode *dirty_cow_pnode(struct ubifs_info *c,
  1400. struct ubifs_pnode *pnode)
  1401. {
  1402. struct ubifs_pnode *p;
  1403. if (!test_bit(COW_CNODE, &pnode->flags)) {
  1404. /* pnode is not being committed */
  1405. if (!test_and_set_bit(DIRTY_CNODE, &pnode->flags)) {
  1406. c->dirty_pn_cnt += 1;
  1407. add_pnode_dirt(c, pnode);
  1408. }
  1409. return pnode;
  1410. }
  1411. /* pnode is being committed, so copy it */
  1412. p = kmalloc(sizeof(struct ubifs_pnode), GFP_NOFS);
  1413. if (unlikely(!p))
  1414. return ERR_PTR(-ENOMEM);
  1415. memcpy(p, pnode, sizeof(struct ubifs_pnode));
  1416. p->cnext = NULL;
  1417. __set_bit(DIRTY_CNODE, &p->flags);
  1418. __clear_bit(COW_CNODE, &p->flags);
  1419. replace_cats(c, pnode, p);
  1420. ubifs_assert(!test_bit(OBSOLETE_CNODE, &pnode->flags));
  1421. __set_bit(OBSOLETE_CNODE, &pnode->flags);
  1422. c->dirty_pn_cnt += 1;
  1423. add_pnode_dirt(c, pnode);
  1424. pnode->parent->nbranch[p->iip].pnode = p;
  1425. return p;
  1426. }
  1427. /**
  1428. * ubifs_lpt_lookup_dirty - lookup LEB properties in the LPT.
  1429. * @c: UBIFS file-system description object
  1430. * @lnum: LEB number to lookup
  1431. *
  1432. * This function returns a pointer to the LEB properties on success or a
  1433. * negative error code on failure.
  1434. */
  1435. struct ubifs_lprops *ubifs_lpt_lookup_dirty(struct ubifs_info *c, int lnum)
  1436. {
  1437. int err, i, h, iip, shft;
  1438. struct ubifs_nnode *nnode;
  1439. struct ubifs_pnode *pnode;
  1440. if (!c->nroot) {
  1441. err = ubifs_read_nnode(c, NULL, 0);
  1442. if (err)
  1443. return ERR_PTR(err);
  1444. }
  1445. nnode = c->nroot;
  1446. nnode = dirty_cow_nnode(c, nnode);
  1447. if (IS_ERR(nnode))
  1448. return ERR_CAST(nnode);
  1449. i = lnum - c->main_first;
  1450. shft = c->lpt_hght * UBIFS_LPT_FANOUT_SHIFT;
  1451. for (h = 1; h < c->lpt_hght; h++) {
  1452. iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
  1453. shft -= UBIFS_LPT_FANOUT_SHIFT;
  1454. nnode = ubifs_get_nnode(c, nnode, iip);
  1455. if (IS_ERR(nnode))
  1456. return ERR_CAST(nnode);
  1457. nnode = dirty_cow_nnode(c, nnode);
  1458. if (IS_ERR(nnode))
  1459. return ERR_CAST(nnode);
  1460. }
  1461. iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
  1462. shft -= UBIFS_LPT_FANOUT_SHIFT;
  1463. pnode = ubifs_get_pnode(c, nnode, iip);
  1464. if (IS_ERR(pnode))
  1465. return ERR_CAST(pnode);
  1466. pnode = dirty_cow_pnode(c, pnode);
  1467. if (IS_ERR(pnode))
  1468. return ERR_CAST(pnode);
  1469. iip = (i & (UBIFS_LPT_FANOUT - 1));
  1470. dbg_lp("LEB %d, free %d, dirty %d, flags %d", lnum,
  1471. pnode->lprops[iip].free, pnode->lprops[iip].dirty,
  1472. pnode->lprops[iip].flags);
  1473. ubifs_assert(test_bit(DIRTY_CNODE, &pnode->flags));
  1474. return &pnode->lprops[iip];
  1475. }
  1476. /**
  1477. * lpt_init_rd - initialize the LPT for reading.
  1478. * @c: UBIFS file-system description object
  1479. *
  1480. * This function returns %0 on success and a negative error code on failure.
  1481. */
  1482. static int lpt_init_rd(struct ubifs_info *c)
  1483. {
  1484. int err, i;
  1485. c->ltab = vmalloc(sizeof(struct ubifs_lpt_lprops) * c->lpt_lebs);
  1486. if (!c->ltab)
  1487. return -ENOMEM;
  1488. i = max_t(int, c->nnode_sz, c->pnode_sz);
  1489. c->lpt_nod_buf = kmalloc(i, GFP_KERNEL);
  1490. if (!c->lpt_nod_buf)
  1491. return -ENOMEM;
  1492. for (i = 0; i < LPROPS_HEAP_CNT; i++) {
  1493. c->lpt_heap[i].arr = kmalloc(sizeof(void *) * LPT_HEAP_SZ,
  1494. GFP_KERNEL);
  1495. if (!c->lpt_heap[i].arr)
  1496. return -ENOMEM;
  1497. c->lpt_heap[i].cnt = 0;
  1498. c->lpt_heap[i].max_cnt = LPT_HEAP_SZ;
  1499. }
  1500. c->dirty_idx.arr = kmalloc(sizeof(void *) * LPT_HEAP_SZ, GFP_KERNEL);
  1501. if (!c->dirty_idx.arr)
  1502. return -ENOMEM;
  1503. c->dirty_idx.cnt = 0;
  1504. c->dirty_idx.max_cnt = LPT_HEAP_SZ;
  1505. err = read_ltab(c);
  1506. if (err)
  1507. return err;
  1508. dbg_lp("space_bits %d", c->space_bits);
  1509. dbg_lp("lpt_lnum_bits %d", c->lpt_lnum_bits);
  1510. dbg_lp("lpt_offs_bits %d", c->lpt_offs_bits);
  1511. dbg_lp("lpt_spc_bits %d", c->lpt_spc_bits);
  1512. dbg_lp("pcnt_bits %d", c->pcnt_bits);
  1513. dbg_lp("lnum_bits %d", c->lnum_bits);
  1514. dbg_lp("pnode_sz %d", c->pnode_sz);
  1515. dbg_lp("nnode_sz %d", c->nnode_sz);
  1516. dbg_lp("ltab_sz %d", c->ltab_sz);
  1517. dbg_lp("lsave_sz %d", c->lsave_sz);
  1518. dbg_lp("lsave_cnt %d", c->lsave_cnt);
  1519. dbg_lp("lpt_hght %d", c->lpt_hght);
  1520. dbg_lp("big_lpt %d", c->big_lpt);
  1521. dbg_lp("LPT root is at %d:%d", c->lpt_lnum, c->lpt_offs);
  1522. dbg_lp("LPT head is at %d:%d", c->nhead_lnum, c->nhead_offs);
  1523. dbg_lp("LPT ltab is at %d:%d", c->ltab_lnum, c->ltab_offs);
  1524. if (c->big_lpt)
  1525. dbg_lp("LPT lsave is at %d:%d", c->lsave_lnum, c->lsave_offs);
  1526. return 0;
  1527. }
  1528. /**
  1529. * lpt_init_wr - initialize the LPT for writing.
  1530. * @c: UBIFS file-system description object
  1531. *
  1532. * 'lpt_init_rd()' must have been called already.
  1533. *
  1534. * This function returns %0 on success and a negative error code on failure.
  1535. */
  1536. static int lpt_init_wr(struct ubifs_info *c)
  1537. {
  1538. int err, i;
  1539. c->ltab_cmt = vmalloc(sizeof(struct ubifs_lpt_lprops) * c->lpt_lebs);
  1540. if (!c->ltab_cmt)
  1541. return -ENOMEM;
  1542. c->lpt_buf = vmalloc(c->leb_size);
  1543. if (!c->lpt_buf)
  1544. return -ENOMEM;
  1545. if (c->big_lpt) {
  1546. c->lsave = kmalloc(sizeof(int) * c->lsave_cnt, GFP_NOFS);
  1547. if (!c->lsave)
  1548. return -ENOMEM;
  1549. err = read_lsave(c);
  1550. if (err)
  1551. return err;
  1552. }
  1553. for (i = 0; i < c->lpt_lebs; i++)
  1554. if (c->ltab[i].free == c->leb_size) {
  1555. err = ubifs_leb_unmap(c, i + c->lpt_first);
  1556. if (err)
  1557. return err;
  1558. }
  1559. return 0;
  1560. }
  1561. /**
  1562. * ubifs_lpt_init - initialize the LPT.
  1563. * @c: UBIFS file-system description object
  1564. * @rd: whether to initialize lpt for reading
  1565. * @wr: whether to initialize lpt for writing
  1566. *
  1567. * For mounting 'rw', @rd and @wr are both true. For mounting 'ro', @rd is true
  1568. * and @wr is false. For mounting from 'ro' to 'rw', @rd is false and @wr is
  1569. * true.
  1570. *
  1571. * This function returns %0 on success and a negative error code on failure.
  1572. */
  1573. int ubifs_lpt_init(struct ubifs_info *c, int rd, int wr)
  1574. {
  1575. int err;
  1576. if (rd) {
  1577. err = lpt_init_rd(c);
  1578. if (err)
  1579. return err;
  1580. }
  1581. if (wr) {
  1582. err = lpt_init_wr(c);
  1583. if (err)
  1584. return err;
  1585. }
  1586. return 0;
  1587. }
  1588. /**
  1589. * struct lpt_scan_node - somewhere to put nodes while we scan LPT.
  1590. * @nnode: where to keep a nnode
  1591. * @pnode: where to keep a pnode
  1592. * @cnode: where to keep a cnode
  1593. * @in_tree: is the node in the tree in memory
  1594. * @ptr.nnode: pointer to the nnode (if it is an nnode) which may be here or in
  1595. * the tree
  1596. * @ptr.pnode: ditto for pnode
  1597. * @ptr.cnode: ditto for cnode
  1598. */
  1599. struct lpt_scan_node {
  1600. union {
  1601. struct ubifs_nnode nnode;
  1602. struct ubifs_pnode pnode;
  1603. struct ubifs_cnode cnode;
  1604. };
  1605. int in_tree;
  1606. union {
  1607. struct ubifs_nnode *nnode;
  1608. struct ubifs_pnode *pnode;
  1609. struct ubifs_cnode *cnode;
  1610. } ptr;
  1611. };
  1612. /**
  1613. * scan_get_nnode - for the scan, get a nnode from either the tree or flash.
  1614. * @c: the UBIFS file-system description object
  1615. * @path: where to put the nnode
  1616. * @parent: parent of the nnode
  1617. * @iip: index in parent of the nnode
  1618. *
  1619. * This function returns a pointer to the nnode on success or a negative error
  1620. * code on failure.
  1621. */
  1622. static struct ubifs_nnode *scan_get_nnode(struct ubifs_info *c,
  1623. struct lpt_scan_node *path,
  1624. struct ubifs_nnode *parent, int iip)
  1625. {
  1626. struct ubifs_nbranch *branch;
  1627. struct ubifs_nnode *nnode;
  1628. void *buf = c->lpt_nod_buf;
  1629. int err;
  1630. branch = &parent->nbranch[iip];
  1631. nnode = branch->nnode;
  1632. if (nnode) {
  1633. path->in_tree = 1;
  1634. path->ptr.nnode = nnode;
  1635. return nnode;
  1636. }
  1637. nnode = &path->nnode;
  1638. path->in_tree = 0;
  1639. path->ptr.nnode = nnode;
  1640. memset(nnode, 0, sizeof(struct ubifs_nnode));
  1641. if (branch->lnum == 0) {
  1642. /*
  1643. * This nnode was not written which just means that the LEB
  1644. * properties in the subtree below it describe empty LEBs. We
  1645. * make the nnode as though we had read it, which in fact means
  1646. * doing almost nothing.
  1647. */
  1648. if (c->big_lpt)
  1649. nnode->num = calc_nnode_num_from_parent(c, parent, iip);
  1650. } else {
  1651. err = ubi_read(c->ubi, branch->lnum, buf, branch->offs,
  1652. c->nnode_sz);
  1653. if (err)
  1654. return ERR_PTR(err);
  1655. err = ubifs_unpack_nnode(c, buf, nnode);
  1656. if (err)
  1657. return ERR_PTR(err);
  1658. }
  1659. err = validate_nnode(c, nnode, parent, iip);
  1660. if (err)
  1661. return ERR_PTR(err);
  1662. if (!c->big_lpt)
  1663. nnode->num = calc_nnode_num_from_parent(c, parent, iip);
  1664. nnode->level = parent->level - 1;
  1665. nnode->parent = parent;
  1666. nnode->iip = iip;
  1667. return nnode;
  1668. }
  1669. /**
  1670. * scan_get_pnode - for the scan, get a pnode from either the tree or flash.
  1671. * @c: the UBIFS file-system description object
  1672. * @path: where to put the pnode
  1673. * @parent: parent of the pnode
  1674. * @iip: index in parent of the pnode
  1675. *
  1676. * This function returns a pointer to the pnode on success or a negative error
  1677. * code on failure.
  1678. */
  1679. static struct ubifs_pnode *scan_get_pnode(struct ubifs_info *c,
  1680. struct lpt_scan_node *path,
  1681. struct ubifs_nnode *parent, int iip)
  1682. {
  1683. struct ubifs_nbranch *branch;
  1684. struct ubifs_pnode *pnode;
  1685. void *buf = c->lpt_nod_buf;
  1686. int err;
  1687. branch = &parent->nbranch[iip];
  1688. pnode = branch->pnode;
  1689. if (pnode) {
  1690. path->in_tree = 1;
  1691. path->ptr.pnode = pnode;
  1692. return pnode;
  1693. }
  1694. pnode = &path->pnode;
  1695. path->in_tree = 0;
  1696. path->ptr.pnode = pnode;
  1697. memset(pnode, 0, sizeof(struct ubifs_pnode));
  1698. if (branch->lnum == 0) {
  1699. /*
  1700. * This pnode was not written which just means that the LEB
  1701. * properties in it describe empty LEBs. We make the pnode as
  1702. * though we had read it.
  1703. */
  1704. int i;
  1705. if (c->big_lpt)
  1706. pnode->num = calc_pnode_num_from_parent(c, parent, iip);
  1707. for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
  1708. struct ubifs_lprops * const lprops = &pnode->lprops[i];
  1709. lprops->free = c->leb_size;
  1710. lprops->flags = ubifs_categorize_lprops(c, lprops);
  1711. }
  1712. } else {
  1713. ubifs_assert(branch->lnum >= c->lpt_first &&
  1714. branch->lnum <= c->lpt_last);
  1715. ubifs_assert(branch->offs >= 0 && branch->offs < c->leb_size);
  1716. err = ubi_read(c->ubi, branch->lnum, buf, branch->offs,
  1717. c->pnode_sz);
  1718. if (err)
  1719. return ERR_PTR(err);
  1720. err = unpack_pnode(c, buf, pnode);
  1721. if (err)
  1722. return ERR_PTR(err);
  1723. }
  1724. err = validate_pnode(c, pnode, parent, iip);
  1725. if (err)
  1726. return ERR_PTR(err);
  1727. if (!c->big_lpt)
  1728. pnode->num = calc_pnode_num_from_parent(c, parent, iip);
  1729. pnode->parent = parent;
  1730. pnode->iip = iip;
  1731. set_pnode_lnum(c, pnode);
  1732. return pnode;
  1733. }
  1734. /**
  1735. * ubifs_lpt_scan_nolock - scan the LPT.
  1736. * @c: the UBIFS file-system description object
  1737. * @start_lnum: LEB number from which to start scanning
  1738. * @end_lnum: LEB number at which to stop scanning
  1739. * @scan_cb: callback function called for each lprops
  1740. * @data: data to be passed to the callback function
  1741. *
  1742. * This function returns %0 on success and a negative error code on failure.
  1743. */
  1744. int ubifs_lpt_scan_nolock(struct ubifs_info *c, int start_lnum, int end_lnum,
  1745. ubifs_lpt_scan_callback scan_cb, void *data)
  1746. {
  1747. int err = 0, i, h, iip, shft;
  1748. struct ubifs_nnode *nnode;
  1749. struct ubifs_pnode *pnode;
  1750. struct lpt_scan_node *path;
  1751. if (start_lnum == -1) {
  1752. start_lnum = end_lnum + 1;
  1753. if (start_lnum >= c->leb_cnt)
  1754. start_lnum = c->main_first;
  1755. }
  1756. ubifs_assert(start_lnum >= c->main_first && start_lnum < c->leb_cnt);
  1757. ubifs_assert(end_lnum >= c->main_first && end_lnum < c->leb_cnt);
  1758. if (!c->nroot) {
  1759. err = ubifs_read_nnode(c, NULL, 0);
  1760. if (err)
  1761. return err;
  1762. }
  1763. path = kmalloc(sizeof(struct lpt_scan_node) * (c->lpt_hght + 1),
  1764. GFP_NOFS);
  1765. if (!path)
  1766. return -ENOMEM;
  1767. path[0].ptr.nnode = c->nroot;
  1768. path[0].in_tree = 1;
  1769. again:
  1770. /* Descend to the pnode containing start_lnum */
  1771. nnode = c->nroot;
  1772. i = start_lnum - c->main_first;
  1773. shft = c->lpt_hght * UBIFS_LPT_FANOUT_SHIFT;
  1774. for (h = 1; h < c->lpt_hght; h++) {
  1775. iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
  1776. shft -= UBIFS_LPT_FANOUT_SHIFT;
  1777. nnode = scan_get_nnode(c, path + h, nnode, iip);
  1778. if (IS_ERR(nnode)) {
  1779. err = PTR_ERR(nnode);
  1780. goto out;
  1781. }
  1782. }
  1783. iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
  1784. shft -= UBIFS_LPT_FANOUT_SHIFT;
  1785. pnode = scan_get_pnode(c, path + h, nnode, iip);
  1786. if (IS_ERR(pnode)) {
  1787. err = PTR_ERR(pnode);
  1788. goto out;
  1789. }
  1790. iip = (i & (UBIFS_LPT_FANOUT - 1));
  1791. /* Loop for each lprops */
  1792. while (1) {
  1793. struct ubifs_lprops *lprops = &pnode->lprops[iip];
  1794. int ret, lnum = lprops->lnum;
  1795. ret = scan_cb(c, lprops, path[h].in_tree, data);
  1796. if (ret < 0) {
  1797. err = ret;
  1798. goto out;
  1799. }
  1800. if (ret & LPT_SCAN_ADD) {
  1801. /* Add all the nodes in path to the tree in memory */
  1802. for (h = 1; h < c->lpt_hght; h++) {
  1803. const size_t sz = sizeof(struct ubifs_nnode);
  1804. struct ubifs_nnode *parent;
  1805. if (path[h].in_tree)
  1806. continue;
  1807. nnode = kmalloc(sz, GFP_NOFS);
  1808. if (!nnode) {
  1809. err = -ENOMEM;
  1810. goto out;
  1811. }
  1812. memcpy(nnode, &path[h].nnode, sz);
  1813. parent = nnode->parent;
  1814. parent->nbranch[nnode->iip].nnode = nnode;
  1815. path[h].ptr.nnode = nnode;
  1816. path[h].in_tree = 1;
  1817. path[h + 1].cnode.parent = nnode;
  1818. }
  1819. if (path[h].in_tree)
  1820. ubifs_ensure_cat(c, lprops);
  1821. else {
  1822. const size_t sz = sizeof(struct ubifs_pnode);
  1823. struct ubifs_nnode *parent;
  1824. pnode = kmalloc(sz, GFP_NOFS);
  1825. if (!pnode) {
  1826. err = -ENOMEM;
  1827. goto out;
  1828. }
  1829. memcpy(pnode, &path[h].pnode, sz);
  1830. parent = pnode->parent;
  1831. parent->nbranch[pnode->iip].pnode = pnode;
  1832. path[h].ptr.pnode = pnode;
  1833. path[h].in_tree = 1;
  1834. update_cats(c, pnode);
  1835. c->pnodes_have += 1;
  1836. }
  1837. err = dbg_check_lpt_nodes(c, (struct ubifs_cnode *)
  1838. c->nroot, 0, 0);
  1839. if (err)
  1840. goto out;
  1841. err = dbg_check_cats(c);
  1842. if (err)
  1843. goto out;
  1844. }
  1845. if (ret & LPT_SCAN_STOP) {
  1846. err = 0;
  1847. break;
  1848. }
  1849. /* Get the next lprops */
  1850. if (lnum == end_lnum) {
  1851. /*
  1852. * We got to the end without finding what we were
  1853. * looking for
  1854. */
  1855. err = -ENOSPC;
  1856. goto out;
  1857. }
  1858. if (lnum + 1 >= c->leb_cnt) {
  1859. /* Wrap-around to the beginning */
  1860. start_lnum = c->main_first;
  1861. goto again;
  1862. }
  1863. if (iip + 1 < UBIFS_LPT_FANOUT) {
  1864. /* Next lprops is in the same pnode */
  1865. iip += 1;
  1866. continue;
  1867. }
  1868. /* We need to get the next pnode. Go up until we can go right */
  1869. iip = pnode->iip;
  1870. while (1) {
  1871. h -= 1;
  1872. ubifs_assert(h >= 0);
  1873. nnode = path[h].ptr.nnode;
  1874. if (iip + 1 < UBIFS_LPT_FANOUT)
  1875. break;
  1876. iip = nnode->iip;
  1877. }
  1878. /* Go right */
  1879. iip += 1;
  1880. /* Descend to the pnode */
  1881. h += 1;
  1882. for (; h < c->lpt_hght; h++) {
  1883. nnode = scan_get_nnode(c, path + h, nnode, iip);
  1884. if (IS_ERR(nnode)) {
  1885. err = PTR_ERR(nnode);
  1886. goto out;
  1887. }
  1888. iip = 0;
  1889. }
  1890. pnode = scan_get_pnode(c, path + h, nnode, iip);
  1891. if (IS_ERR(pnode)) {
  1892. err = PTR_ERR(pnode);
  1893. goto out;
  1894. }
  1895. iip = 0;
  1896. }
  1897. out:
  1898. kfree(path);
  1899. return err;
  1900. }
  1901. #ifdef CONFIG_UBIFS_FS_DEBUG
  1902. /**
  1903. * dbg_chk_pnode - check a pnode.
  1904. * @c: the UBIFS file-system description object
  1905. * @pnode: pnode to check
  1906. * @col: pnode column
  1907. *
  1908. * This function returns %0 on success and a negative error code on failure.
  1909. */
  1910. static int dbg_chk_pnode(struct ubifs_info *c, struct ubifs_pnode *pnode,
  1911. int col)
  1912. {
  1913. int i;
  1914. if (pnode->num != col) {
  1915. dbg_err("pnode num %d expected %d parent num %d iip %d",
  1916. pnode->num, col, pnode->parent->num, pnode->iip);
  1917. return -EINVAL;
  1918. }
  1919. for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
  1920. struct ubifs_lprops *lp, *lprops = &pnode->lprops[i];
  1921. int lnum = (pnode->num << UBIFS_LPT_FANOUT_SHIFT) + i +
  1922. c->main_first;
  1923. int found, cat = lprops->flags & LPROPS_CAT_MASK;
  1924. struct ubifs_lpt_heap *heap;
  1925. struct list_head *list = NULL;
  1926. if (lnum >= c->leb_cnt)
  1927. continue;
  1928. if (lprops->lnum != lnum) {
  1929. dbg_err("bad LEB number %d expected %d",
  1930. lprops->lnum, lnum);
  1931. return -EINVAL;
  1932. }
  1933. if (lprops->flags & LPROPS_TAKEN) {
  1934. if (cat != LPROPS_UNCAT) {
  1935. dbg_err("LEB %d taken but not uncat %d",
  1936. lprops->lnum, cat);
  1937. return -EINVAL;
  1938. }
  1939. continue;
  1940. }
  1941. if (lprops->flags & LPROPS_INDEX) {
  1942. switch (cat) {
  1943. case LPROPS_UNCAT:
  1944. case LPROPS_DIRTY_IDX:
  1945. case LPROPS_FRDI_IDX:
  1946. break;
  1947. default:
  1948. dbg_err("LEB %d index but cat %d",
  1949. lprops->lnum, cat);
  1950. return -EINVAL;
  1951. }
  1952. } else {
  1953. switch (cat) {
  1954. case LPROPS_UNCAT:
  1955. case LPROPS_DIRTY:
  1956. case LPROPS_FREE:
  1957. case LPROPS_EMPTY:
  1958. case LPROPS_FREEABLE:
  1959. break;
  1960. default:
  1961. dbg_err("LEB %d not index but cat %d",
  1962. lprops->lnum, cat);
  1963. return -EINVAL;
  1964. }
  1965. }
  1966. switch (cat) {
  1967. case LPROPS_UNCAT:
  1968. list = &c->uncat_list;
  1969. break;
  1970. case LPROPS_EMPTY:
  1971. list = &c->empty_list;
  1972. break;
  1973. case LPROPS_FREEABLE:
  1974. list = &c->freeable_list;
  1975. break;
  1976. case LPROPS_FRDI_IDX:
  1977. list = &c->frdi_idx_list;
  1978. break;
  1979. }
  1980. found = 0;
  1981. switch (cat) {
  1982. case LPROPS_DIRTY:
  1983. case LPROPS_DIRTY_IDX:
  1984. case LPROPS_FREE:
  1985. heap = &c->lpt_heap[cat - 1];
  1986. if (lprops->hpos < heap->cnt &&
  1987. heap->arr[lprops->hpos] == lprops)
  1988. found = 1;
  1989. break;
  1990. case LPROPS_UNCAT:
  1991. case LPROPS_EMPTY:
  1992. case LPROPS_FREEABLE:
  1993. case LPROPS_FRDI_IDX:
  1994. list_for_each_entry(lp, list, list)
  1995. if (lprops == lp) {
  1996. found = 1;
  1997. break;
  1998. }
  1999. break;
  2000. }
  2001. if (!found) {
  2002. dbg_err("LEB %d cat %d not found in cat heap/list",
  2003. lprops->lnum, cat);
  2004. return -EINVAL;
  2005. }
  2006. switch (cat) {
  2007. case LPROPS_EMPTY:
  2008. if (lprops->free != c->leb_size) {
  2009. dbg_err("LEB %d cat %d free %d dirty %d",
  2010. lprops->lnum, cat, lprops->free,
  2011. lprops->dirty);
  2012. return -EINVAL;
  2013. }
  2014. case LPROPS_FREEABLE:
  2015. case LPROPS_FRDI_IDX:
  2016. if (lprops->free + lprops->dirty != c->leb_size) {
  2017. dbg_err("LEB %d cat %d free %d dirty %d",
  2018. lprops->lnum, cat, lprops->free,
  2019. lprops->dirty);
  2020. return -EINVAL;
  2021. }
  2022. }
  2023. }
  2024. return 0;
  2025. }
  2026. /**
  2027. * dbg_check_lpt_nodes - check nnodes and pnodes.
  2028. * @c: the UBIFS file-system description object
  2029. * @cnode: next cnode (nnode or pnode) to check
  2030. * @row: row of cnode (root is zero)
  2031. * @col: column of cnode (leftmost is zero)
  2032. *
  2033. * This function returns %0 on success and a negative error code on failure.
  2034. */
  2035. int dbg_check_lpt_nodes(struct ubifs_info *c, struct ubifs_cnode *cnode,
  2036. int row, int col)
  2037. {
  2038. struct ubifs_nnode *nnode, *nn;
  2039. struct ubifs_cnode *cn;
  2040. int num, iip = 0, err;
  2041. if (!(ubifs_chk_flags & UBIFS_CHK_LPROPS))
  2042. return 0;
  2043. while (cnode) {
  2044. ubifs_assert(row >= 0);
  2045. nnode = cnode->parent;
  2046. if (cnode->level) {
  2047. /* cnode is a nnode */
  2048. num = calc_nnode_num(row, col);
  2049. if (cnode->num != num) {
  2050. dbg_err("nnode num %d expected %d "
  2051. "parent num %d iip %d", cnode->num, num,
  2052. (nnode ? nnode->num : 0), cnode->iip);
  2053. return -EINVAL;
  2054. }
  2055. nn = (struct ubifs_nnode *)cnode;
  2056. while (iip < UBIFS_LPT_FANOUT) {
  2057. cn = nn->nbranch[iip].cnode;
  2058. if (cn) {
  2059. /* Go down */
  2060. row += 1;
  2061. col <<= UBIFS_LPT_FANOUT_SHIFT;
  2062. col += iip;
  2063. iip = 0;
  2064. cnode = cn;
  2065. break;
  2066. }
  2067. /* Go right */
  2068. iip += 1;
  2069. }
  2070. if (iip < UBIFS_LPT_FANOUT)
  2071. continue;
  2072. } else {
  2073. struct ubifs_pnode *pnode;
  2074. /* cnode is a pnode */
  2075. pnode = (struct ubifs_pnode *)cnode;
  2076. err = dbg_chk_pnode(c, pnode, col);
  2077. if (err)
  2078. return err;
  2079. }
  2080. /* Go up and to the right */
  2081. row -= 1;
  2082. col >>= UBIFS_LPT_FANOUT_SHIFT;
  2083. iip = cnode->iip + 1;
  2084. cnode = (struct ubifs_cnode *)nnode;
  2085. }
  2086. return 0;
  2087. }
  2088. #endif /* CONFIG_UBIFS_FS_DEBUG */