lpt_commit.c 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * This file is part of UBIFS.
  4. *
  5. * Copyright (C) 2006-2008 Nokia Corporation.
  6. *
  7. * Authors: Adrian Hunter
  8. * Artem Bityutskiy (Битюцкий Артём)
  9. */
  10. /*
  11. * This file implements commit-related functionality of the LEB properties
  12. * subsystem.
  13. */
  14. #ifndef __UBOOT__
  15. #include <log.h>
  16. #include <dm/devres.h>
  17. #include <linux/crc16.h>
  18. #include <linux/slab.h>
  19. #include <linux/random.h>
  20. #else
  21. #include <linux/bitops.h>
  22. #include <linux/compat.h>
  23. #include <linux/err.h>
  24. #include <linux/crc16.h>
  25. #endif
  26. #include "ubifs.h"
  27. #ifndef __UBOOT__
  28. static int dbg_populate_lsave(struct ubifs_info *c);
  29. #endif
  30. /**
  31. * first_dirty_cnode - find first dirty cnode.
  32. * @c: UBIFS file-system description object
  33. * @nnode: nnode at which to start
  34. *
  35. * This function returns the first dirty cnode or %NULL if there is not one.
  36. */
  37. static struct ubifs_cnode *first_dirty_cnode(struct ubifs_nnode *nnode)
  38. {
  39. ubifs_assert(nnode);
  40. while (1) {
  41. int i, cont = 0;
  42. for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
  43. struct ubifs_cnode *cnode;
  44. cnode = nnode->nbranch[i].cnode;
  45. if (cnode &&
  46. test_bit(DIRTY_CNODE, &cnode->flags)) {
  47. if (cnode->level == 0)
  48. return cnode;
  49. nnode = (struct ubifs_nnode *)cnode;
  50. cont = 1;
  51. break;
  52. }
  53. }
  54. if (!cont)
  55. return (struct ubifs_cnode *)nnode;
  56. }
  57. }
  58. /**
  59. * next_dirty_cnode - find next dirty cnode.
  60. * @cnode: cnode from which to begin searching
  61. *
  62. * This function returns the next dirty cnode or %NULL if there is not one.
  63. */
  64. static struct ubifs_cnode *next_dirty_cnode(struct ubifs_cnode *cnode)
  65. {
  66. struct ubifs_nnode *nnode;
  67. int i;
  68. ubifs_assert(cnode);
  69. nnode = cnode->parent;
  70. if (!nnode)
  71. return NULL;
  72. for (i = cnode->iip + 1; i < UBIFS_LPT_FANOUT; i++) {
  73. cnode = nnode->nbranch[i].cnode;
  74. if (cnode && test_bit(DIRTY_CNODE, &cnode->flags)) {
  75. if (cnode->level == 0)
  76. return cnode; /* cnode is a pnode */
  77. /* cnode is a nnode */
  78. return first_dirty_cnode((struct ubifs_nnode *)cnode);
  79. }
  80. }
  81. return (struct ubifs_cnode *)nnode;
  82. }
  83. /**
  84. * get_cnodes_to_commit - create list of dirty cnodes to commit.
  85. * @c: UBIFS file-system description object
  86. *
  87. * This function returns the number of cnodes to commit.
  88. */
  89. static int get_cnodes_to_commit(struct ubifs_info *c)
  90. {
  91. struct ubifs_cnode *cnode, *cnext;
  92. int cnt = 0;
  93. if (!c->nroot)
  94. return 0;
  95. if (!test_bit(DIRTY_CNODE, &c->nroot->flags))
  96. return 0;
  97. c->lpt_cnext = first_dirty_cnode(c->nroot);
  98. cnode = c->lpt_cnext;
  99. if (!cnode)
  100. return 0;
  101. cnt += 1;
  102. while (1) {
  103. ubifs_assert(!test_bit(COW_CNODE, &cnode->flags));
  104. __set_bit(COW_CNODE, &cnode->flags);
  105. cnext = next_dirty_cnode(cnode);
  106. if (!cnext) {
  107. cnode->cnext = c->lpt_cnext;
  108. break;
  109. }
  110. cnode->cnext = cnext;
  111. cnode = cnext;
  112. cnt += 1;
  113. }
  114. dbg_cmt("committing %d cnodes", cnt);
  115. dbg_lp("committing %d cnodes", cnt);
  116. ubifs_assert(cnt == c->dirty_nn_cnt + c->dirty_pn_cnt);
  117. return cnt;
  118. }
  119. /**
  120. * upd_ltab - update LPT LEB properties.
  121. * @c: UBIFS file-system description object
  122. * @lnum: LEB number
  123. * @free: amount of free space
  124. * @dirty: amount of dirty space to add
  125. */
  126. static void upd_ltab(struct ubifs_info *c, int lnum, int free, int dirty)
  127. {
  128. dbg_lp("LEB %d free %d dirty %d to %d +%d",
  129. lnum, c->ltab[lnum - c->lpt_first].free,
  130. c->ltab[lnum - c->lpt_first].dirty, free, dirty);
  131. ubifs_assert(lnum >= c->lpt_first && lnum <= c->lpt_last);
  132. c->ltab[lnum - c->lpt_first].free = free;
  133. c->ltab[lnum - c->lpt_first].dirty += dirty;
  134. }
  135. /**
  136. * alloc_lpt_leb - allocate an LPT LEB that is empty.
  137. * @c: UBIFS file-system description object
  138. * @lnum: LEB number is passed and returned here
  139. *
  140. * This function finds the next empty LEB in the ltab starting from @lnum. If a
  141. * an empty LEB is found it is returned in @lnum and the function returns %0.
  142. * Otherwise the function returns -ENOSPC. Note however, that LPT is designed
  143. * never to run out of space.
  144. */
  145. static int alloc_lpt_leb(struct ubifs_info *c, int *lnum)
  146. {
  147. int i, n;
  148. n = *lnum - c->lpt_first + 1;
  149. for (i = n; i < c->lpt_lebs; i++) {
  150. if (c->ltab[i].tgc || c->ltab[i].cmt)
  151. continue;
  152. if (c->ltab[i].free == c->leb_size) {
  153. c->ltab[i].cmt = 1;
  154. *lnum = i + c->lpt_first;
  155. return 0;
  156. }
  157. }
  158. for (i = 0; i < n; i++) {
  159. if (c->ltab[i].tgc || c->ltab[i].cmt)
  160. continue;
  161. if (c->ltab[i].free == c->leb_size) {
  162. c->ltab[i].cmt = 1;
  163. *lnum = i + c->lpt_first;
  164. return 0;
  165. }
  166. }
  167. return -ENOSPC;
  168. }
  169. /**
  170. * layout_cnodes - layout cnodes for commit.
  171. * @c: UBIFS file-system description object
  172. *
  173. * This function returns %0 on success and a negative error code on failure.
  174. */
  175. static int layout_cnodes(struct ubifs_info *c)
  176. {
  177. int lnum, offs, len, alen, done_lsave, done_ltab, err;
  178. struct ubifs_cnode *cnode;
  179. err = dbg_chk_lpt_sz(c, 0, 0);
  180. if (err)
  181. return err;
  182. cnode = c->lpt_cnext;
  183. if (!cnode)
  184. return 0;
  185. lnum = c->nhead_lnum;
  186. offs = c->nhead_offs;
  187. /* Try to place lsave and ltab nicely */
  188. done_lsave = !c->big_lpt;
  189. done_ltab = 0;
  190. if (!done_lsave && offs + c->lsave_sz <= c->leb_size) {
  191. done_lsave = 1;
  192. c->lsave_lnum = lnum;
  193. c->lsave_offs = offs;
  194. offs += c->lsave_sz;
  195. dbg_chk_lpt_sz(c, 1, c->lsave_sz);
  196. }
  197. if (offs + c->ltab_sz <= c->leb_size) {
  198. done_ltab = 1;
  199. c->ltab_lnum = lnum;
  200. c->ltab_offs = offs;
  201. offs += c->ltab_sz;
  202. dbg_chk_lpt_sz(c, 1, c->ltab_sz);
  203. }
  204. do {
  205. if (cnode->level) {
  206. len = c->nnode_sz;
  207. c->dirty_nn_cnt -= 1;
  208. } else {
  209. len = c->pnode_sz;
  210. c->dirty_pn_cnt -= 1;
  211. }
  212. while (offs + len > c->leb_size) {
  213. alen = ALIGN(offs, c->min_io_size);
  214. upd_ltab(c, lnum, c->leb_size - alen, alen - offs);
  215. dbg_chk_lpt_sz(c, 2, c->leb_size - offs);
  216. err = alloc_lpt_leb(c, &lnum);
  217. if (err)
  218. goto no_space;
  219. offs = 0;
  220. ubifs_assert(lnum >= c->lpt_first &&
  221. lnum <= c->lpt_last);
  222. /* Try to place lsave and ltab nicely */
  223. if (!done_lsave) {
  224. done_lsave = 1;
  225. c->lsave_lnum = lnum;
  226. c->lsave_offs = offs;
  227. offs += c->lsave_sz;
  228. dbg_chk_lpt_sz(c, 1, c->lsave_sz);
  229. continue;
  230. }
  231. if (!done_ltab) {
  232. done_ltab = 1;
  233. c->ltab_lnum = lnum;
  234. c->ltab_offs = offs;
  235. offs += c->ltab_sz;
  236. dbg_chk_lpt_sz(c, 1, c->ltab_sz);
  237. continue;
  238. }
  239. break;
  240. }
  241. if (cnode->parent) {
  242. cnode->parent->nbranch[cnode->iip].lnum = lnum;
  243. cnode->parent->nbranch[cnode->iip].offs = offs;
  244. } else {
  245. c->lpt_lnum = lnum;
  246. c->lpt_offs = offs;
  247. }
  248. offs += len;
  249. dbg_chk_lpt_sz(c, 1, len);
  250. cnode = cnode->cnext;
  251. } while (cnode && cnode != c->lpt_cnext);
  252. /* Make sure to place LPT's save table */
  253. if (!done_lsave) {
  254. if (offs + c->lsave_sz > c->leb_size) {
  255. alen = ALIGN(offs, c->min_io_size);
  256. upd_ltab(c, lnum, c->leb_size - alen, alen - offs);
  257. dbg_chk_lpt_sz(c, 2, c->leb_size - offs);
  258. err = alloc_lpt_leb(c, &lnum);
  259. if (err)
  260. goto no_space;
  261. offs = 0;
  262. ubifs_assert(lnum >= c->lpt_first &&
  263. lnum <= c->lpt_last);
  264. }
  265. done_lsave = 1;
  266. c->lsave_lnum = lnum;
  267. c->lsave_offs = offs;
  268. offs += c->lsave_sz;
  269. dbg_chk_lpt_sz(c, 1, c->lsave_sz);
  270. }
  271. /* Make sure to place LPT's own lprops table */
  272. if (!done_ltab) {
  273. if (offs + c->ltab_sz > c->leb_size) {
  274. alen = ALIGN(offs, c->min_io_size);
  275. upd_ltab(c, lnum, c->leb_size - alen, alen - offs);
  276. dbg_chk_lpt_sz(c, 2, c->leb_size - offs);
  277. err = alloc_lpt_leb(c, &lnum);
  278. if (err)
  279. goto no_space;
  280. offs = 0;
  281. ubifs_assert(lnum >= c->lpt_first &&
  282. lnum <= c->lpt_last);
  283. }
  284. c->ltab_lnum = lnum;
  285. c->ltab_offs = offs;
  286. offs += c->ltab_sz;
  287. dbg_chk_lpt_sz(c, 1, c->ltab_sz);
  288. }
  289. alen = ALIGN(offs, c->min_io_size);
  290. upd_ltab(c, lnum, c->leb_size - alen, alen - offs);
  291. dbg_chk_lpt_sz(c, 4, alen - offs);
  292. err = dbg_chk_lpt_sz(c, 3, alen);
  293. if (err)
  294. return err;
  295. return 0;
  296. no_space:
  297. ubifs_err(c, "LPT out of space at LEB %d:%d needing %d, done_ltab %d, done_lsave %d",
  298. lnum, offs, len, done_ltab, done_lsave);
  299. ubifs_dump_lpt_info(c);
  300. ubifs_dump_lpt_lebs(c);
  301. dump_stack();
  302. return err;
  303. }
  304. #ifndef __UBOOT__
  305. /**
  306. * realloc_lpt_leb - allocate an LPT LEB that is empty.
  307. * @c: UBIFS file-system description object
  308. * @lnum: LEB number is passed and returned here
  309. *
  310. * This function duplicates exactly the results of the function alloc_lpt_leb.
  311. * It is used during end commit to reallocate the same LEB numbers that were
  312. * allocated by alloc_lpt_leb during start commit.
  313. *
  314. * This function finds the next LEB that was allocated by the alloc_lpt_leb
  315. * function starting from @lnum. If a LEB is found it is returned in @lnum and
  316. * the function returns %0. Otherwise the function returns -ENOSPC.
  317. * Note however, that LPT is designed never to run out of space.
  318. */
  319. static int realloc_lpt_leb(struct ubifs_info *c, int *lnum)
  320. {
  321. int i, n;
  322. n = *lnum - c->lpt_first + 1;
  323. for (i = n; i < c->lpt_lebs; i++)
  324. if (c->ltab[i].cmt) {
  325. c->ltab[i].cmt = 0;
  326. *lnum = i + c->lpt_first;
  327. return 0;
  328. }
  329. for (i = 0; i < n; i++)
  330. if (c->ltab[i].cmt) {
  331. c->ltab[i].cmt = 0;
  332. *lnum = i + c->lpt_first;
  333. return 0;
  334. }
  335. return -ENOSPC;
  336. }
  337. /**
  338. * write_cnodes - write cnodes for commit.
  339. * @c: UBIFS file-system description object
  340. *
  341. * This function returns %0 on success and a negative error code on failure.
  342. */
  343. static int write_cnodes(struct ubifs_info *c)
  344. {
  345. int lnum, offs, len, from, err, wlen, alen, done_ltab, done_lsave;
  346. struct ubifs_cnode *cnode;
  347. void *buf = c->lpt_buf;
  348. cnode = c->lpt_cnext;
  349. if (!cnode)
  350. return 0;
  351. lnum = c->nhead_lnum;
  352. offs = c->nhead_offs;
  353. from = offs;
  354. /* Ensure empty LEB is unmapped */
  355. if (offs == 0) {
  356. err = ubifs_leb_unmap(c, lnum);
  357. if (err)
  358. return err;
  359. }
  360. /* Try to place lsave and ltab nicely */
  361. done_lsave = !c->big_lpt;
  362. done_ltab = 0;
  363. if (!done_lsave && offs + c->lsave_sz <= c->leb_size) {
  364. done_lsave = 1;
  365. ubifs_pack_lsave(c, buf + offs, c->lsave);
  366. offs += c->lsave_sz;
  367. dbg_chk_lpt_sz(c, 1, c->lsave_sz);
  368. }
  369. if (offs + c->ltab_sz <= c->leb_size) {
  370. done_ltab = 1;
  371. ubifs_pack_ltab(c, buf + offs, c->ltab_cmt);
  372. offs += c->ltab_sz;
  373. dbg_chk_lpt_sz(c, 1, c->ltab_sz);
  374. }
  375. /* Loop for each cnode */
  376. do {
  377. if (cnode->level)
  378. len = c->nnode_sz;
  379. else
  380. len = c->pnode_sz;
  381. while (offs + len > c->leb_size) {
  382. wlen = offs - from;
  383. if (wlen) {
  384. alen = ALIGN(wlen, c->min_io_size);
  385. memset(buf + offs, 0xff, alen - wlen);
  386. err = ubifs_leb_write(c, lnum, buf + from, from,
  387. alen);
  388. if (err)
  389. return err;
  390. }
  391. dbg_chk_lpt_sz(c, 2, c->leb_size - offs);
  392. err = realloc_lpt_leb(c, &lnum);
  393. if (err)
  394. goto no_space;
  395. offs = from = 0;
  396. ubifs_assert(lnum >= c->lpt_first &&
  397. lnum <= c->lpt_last);
  398. err = ubifs_leb_unmap(c, lnum);
  399. if (err)
  400. return err;
  401. /* Try to place lsave and ltab nicely */
  402. if (!done_lsave) {
  403. done_lsave = 1;
  404. ubifs_pack_lsave(c, buf + offs, c->lsave);
  405. offs += c->lsave_sz;
  406. dbg_chk_lpt_sz(c, 1, c->lsave_sz);
  407. continue;
  408. }
  409. if (!done_ltab) {
  410. done_ltab = 1;
  411. ubifs_pack_ltab(c, buf + offs, c->ltab_cmt);
  412. offs += c->ltab_sz;
  413. dbg_chk_lpt_sz(c, 1, c->ltab_sz);
  414. continue;
  415. }
  416. break;
  417. }
  418. if (cnode->level)
  419. ubifs_pack_nnode(c, buf + offs,
  420. (struct ubifs_nnode *)cnode);
  421. else
  422. ubifs_pack_pnode(c, buf + offs,
  423. (struct ubifs_pnode *)cnode);
  424. /*
  425. * The reason for the barriers is the same as in case of TNC.
  426. * See comment in 'write_index()'. 'dirty_cow_nnode()' and
  427. * 'dirty_cow_pnode()' are the functions for which this is
  428. * important.
  429. */
  430. clear_bit(DIRTY_CNODE, &cnode->flags);
  431. smp_mb__before_atomic();
  432. clear_bit(COW_CNODE, &cnode->flags);
  433. smp_mb__after_atomic();
  434. offs += len;
  435. dbg_chk_lpt_sz(c, 1, len);
  436. cnode = cnode->cnext;
  437. } while (cnode && cnode != c->lpt_cnext);
  438. /* Make sure to place LPT's save table */
  439. if (!done_lsave) {
  440. if (offs + c->lsave_sz > c->leb_size) {
  441. wlen = offs - from;
  442. alen = ALIGN(wlen, c->min_io_size);
  443. memset(buf + offs, 0xff, alen - wlen);
  444. err = ubifs_leb_write(c, lnum, buf + from, from, alen);
  445. if (err)
  446. return err;
  447. dbg_chk_lpt_sz(c, 2, c->leb_size - offs);
  448. err = realloc_lpt_leb(c, &lnum);
  449. if (err)
  450. goto no_space;
  451. offs = from = 0;
  452. ubifs_assert(lnum >= c->lpt_first &&
  453. lnum <= c->lpt_last);
  454. err = ubifs_leb_unmap(c, lnum);
  455. if (err)
  456. return err;
  457. }
  458. done_lsave = 1;
  459. ubifs_pack_lsave(c, buf + offs, c->lsave);
  460. offs += c->lsave_sz;
  461. dbg_chk_lpt_sz(c, 1, c->lsave_sz);
  462. }
  463. /* Make sure to place LPT's own lprops table */
  464. if (!done_ltab) {
  465. if (offs + c->ltab_sz > c->leb_size) {
  466. wlen = offs - from;
  467. alen = ALIGN(wlen, c->min_io_size);
  468. memset(buf + offs, 0xff, alen - wlen);
  469. err = ubifs_leb_write(c, lnum, buf + from, from, alen);
  470. if (err)
  471. return err;
  472. dbg_chk_lpt_sz(c, 2, c->leb_size - offs);
  473. err = realloc_lpt_leb(c, &lnum);
  474. if (err)
  475. goto no_space;
  476. offs = from = 0;
  477. ubifs_assert(lnum >= c->lpt_first &&
  478. lnum <= c->lpt_last);
  479. err = ubifs_leb_unmap(c, lnum);
  480. if (err)
  481. return err;
  482. }
  483. ubifs_pack_ltab(c, buf + offs, c->ltab_cmt);
  484. offs += c->ltab_sz;
  485. dbg_chk_lpt_sz(c, 1, c->ltab_sz);
  486. }
  487. /* Write remaining data in buffer */
  488. wlen = offs - from;
  489. alen = ALIGN(wlen, c->min_io_size);
  490. memset(buf + offs, 0xff, alen - wlen);
  491. err = ubifs_leb_write(c, lnum, buf + from, from, alen);
  492. if (err)
  493. return err;
  494. dbg_chk_lpt_sz(c, 4, alen - wlen);
  495. err = dbg_chk_lpt_sz(c, 3, ALIGN(offs, c->min_io_size));
  496. if (err)
  497. return err;
  498. c->nhead_lnum = lnum;
  499. c->nhead_offs = ALIGN(offs, c->min_io_size);
  500. dbg_lp("LPT root is at %d:%d", c->lpt_lnum, c->lpt_offs);
  501. dbg_lp("LPT head is at %d:%d", c->nhead_lnum, c->nhead_offs);
  502. dbg_lp("LPT ltab is at %d:%d", c->ltab_lnum, c->ltab_offs);
  503. if (c->big_lpt)
  504. dbg_lp("LPT lsave is at %d:%d", c->lsave_lnum, c->lsave_offs);
  505. return 0;
  506. no_space:
  507. ubifs_err(c, "LPT out of space mismatch at LEB %d:%d needing %d, done_ltab %d, done_lsave %d",
  508. lnum, offs, len, done_ltab, done_lsave);
  509. ubifs_dump_lpt_info(c);
  510. ubifs_dump_lpt_lebs(c);
  511. dump_stack();
  512. return err;
  513. }
  514. #endif
  515. /**
  516. * next_pnode_to_dirty - find next pnode to dirty.
  517. * @c: UBIFS file-system description object
  518. * @pnode: pnode
  519. *
  520. * This function returns the next pnode to dirty or %NULL if there are no more
  521. * pnodes. Note that pnodes that have never been written (lnum == 0) are
  522. * skipped.
  523. */
  524. static struct ubifs_pnode *next_pnode_to_dirty(struct ubifs_info *c,
  525. struct ubifs_pnode *pnode)
  526. {
  527. struct ubifs_nnode *nnode;
  528. int iip;
  529. /* Try to go right */
  530. nnode = pnode->parent;
  531. for (iip = pnode->iip + 1; iip < UBIFS_LPT_FANOUT; iip++) {
  532. if (nnode->nbranch[iip].lnum)
  533. return ubifs_get_pnode(c, nnode, iip);
  534. }
  535. /* Go up while can't go right */
  536. do {
  537. iip = nnode->iip + 1;
  538. nnode = nnode->parent;
  539. if (!nnode)
  540. return NULL;
  541. for (; iip < UBIFS_LPT_FANOUT; iip++) {
  542. if (nnode->nbranch[iip].lnum)
  543. break;
  544. }
  545. } while (iip >= UBIFS_LPT_FANOUT);
  546. /* Go right */
  547. nnode = ubifs_get_nnode(c, nnode, iip);
  548. if (IS_ERR(nnode))
  549. return (void *)nnode;
  550. /* Go down to level 1 */
  551. while (nnode->level > 1) {
  552. for (iip = 0; iip < UBIFS_LPT_FANOUT; iip++) {
  553. if (nnode->nbranch[iip].lnum)
  554. break;
  555. }
  556. if (iip >= UBIFS_LPT_FANOUT) {
  557. /*
  558. * Should not happen, but we need to keep going
  559. * if it does.
  560. */
  561. iip = 0;
  562. }
  563. nnode = ubifs_get_nnode(c, nnode, iip);
  564. if (IS_ERR(nnode))
  565. return (void *)nnode;
  566. }
  567. for (iip = 0; iip < UBIFS_LPT_FANOUT; iip++)
  568. if (nnode->nbranch[iip].lnum)
  569. break;
  570. if (iip >= UBIFS_LPT_FANOUT)
  571. /* Should not happen, but we need to keep going if it does */
  572. iip = 0;
  573. return ubifs_get_pnode(c, nnode, iip);
  574. }
  575. /**
  576. * pnode_lookup - lookup a pnode in the LPT.
  577. * @c: UBIFS file-system description object
  578. * @i: pnode number (0 to main_lebs - 1)
  579. *
  580. * This function returns a pointer to the pnode on success or a negative
  581. * error code on failure.
  582. */
  583. static struct ubifs_pnode *pnode_lookup(struct ubifs_info *c, int i)
  584. {
  585. int err, h, iip, shft;
  586. struct ubifs_nnode *nnode;
  587. if (!c->nroot) {
  588. err = ubifs_read_nnode(c, NULL, 0);
  589. if (err)
  590. return ERR_PTR(err);
  591. }
  592. i <<= UBIFS_LPT_FANOUT_SHIFT;
  593. nnode = c->nroot;
  594. shft = c->lpt_hght * UBIFS_LPT_FANOUT_SHIFT;
  595. for (h = 1; h < c->lpt_hght; h++) {
  596. iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
  597. shft -= UBIFS_LPT_FANOUT_SHIFT;
  598. nnode = ubifs_get_nnode(c, nnode, iip);
  599. if (IS_ERR(nnode))
  600. return ERR_CAST(nnode);
  601. }
  602. iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
  603. return ubifs_get_pnode(c, nnode, iip);
  604. }
  605. /**
  606. * add_pnode_dirt - add dirty space to LPT LEB properties.
  607. * @c: UBIFS file-system description object
  608. * @pnode: pnode for which to add dirt
  609. */
  610. static void add_pnode_dirt(struct ubifs_info *c, struct ubifs_pnode *pnode)
  611. {
  612. ubifs_add_lpt_dirt(c, pnode->parent->nbranch[pnode->iip].lnum,
  613. c->pnode_sz);
  614. }
  615. /**
  616. * do_make_pnode_dirty - mark a pnode dirty.
  617. * @c: UBIFS file-system description object
  618. * @pnode: pnode to mark dirty
  619. */
  620. static void do_make_pnode_dirty(struct ubifs_info *c, struct ubifs_pnode *pnode)
  621. {
  622. /* Assumes cnext list is empty i.e. not called during commit */
  623. if (!test_and_set_bit(DIRTY_CNODE, &pnode->flags)) {
  624. struct ubifs_nnode *nnode;
  625. c->dirty_pn_cnt += 1;
  626. add_pnode_dirt(c, pnode);
  627. /* Mark parent and ancestors dirty too */
  628. nnode = pnode->parent;
  629. while (nnode) {
  630. if (!test_and_set_bit(DIRTY_CNODE, &nnode->flags)) {
  631. c->dirty_nn_cnt += 1;
  632. ubifs_add_nnode_dirt(c, nnode);
  633. nnode = nnode->parent;
  634. } else
  635. break;
  636. }
  637. }
  638. }
  639. /**
  640. * make_tree_dirty - mark the entire LEB properties tree dirty.
  641. * @c: UBIFS file-system description object
  642. *
  643. * This function is used by the "small" LPT model to cause the entire LEB
  644. * properties tree to be written. The "small" LPT model does not use LPT
  645. * garbage collection because it is more efficient to write the entire tree
  646. * (because it is small).
  647. *
  648. * This function returns %0 on success and a negative error code on failure.
  649. */
  650. static int make_tree_dirty(struct ubifs_info *c)
  651. {
  652. struct ubifs_pnode *pnode;
  653. pnode = pnode_lookup(c, 0);
  654. if (IS_ERR(pnode))
  655. return PTR_ERR(pnode);
  656. while (pnode) {
  657. do_make_pnode_dirty(c, pnode);
  658. pnode = next_pnode_to_dirty(c, pnode);
  659. if (IS_ERR(pnode))
  660. return PTR_ERR(pnode);
  661. }
  662. return 0;
  663. }
  664. /**
  665. * need_write_all - determine if the LPT area is running out of free space.
  666. * @c: UBIFS file-system description object
  667. *
  668. * This function returns %1 if the LPT area is running out of free space and %0
  669. * if it is not.
  670. */
  671. static int need_write_all(struct ubifs_info *c)
  672. {
  673. long long free = 0;
  674. int i;
  675. for (i = 0; i < c->lpt_lebs; i++) {
  676. if (i + c->lpt_first == c->nhead_lnum)
  677. free += c->leb_size - c->nhead_offs;
  678. else if (c->ltab[i].free == c->leb_size)
  679. free += c->leb_size;
  680. else if (c->ltab[i].free + c->ltab[i].dirty == c->leb_size)
  681. free += c->leb_size;
  682. }
  683. /* Less than twice the size left */
  684. if (free <= c->lpt_sz * 2)
  685. return 1;
  686. return 0;
  687. }
  688. /**
  689. * lpt_tgc_start - start trivial garbage collection of LPT LEBs.
  690. * @c: UBIFS file-system description object
  691. *
  692. * LPT trivial garbage collection is where a LPT LEB contains only dirty and
  693. * free space and so may be reused as soon as the next commit is completed.
  694. * This function is called during start commit to mark LPT LEBs for trivial GC.
  695. */
  696. static void lpt_tgc_start(struct ubifs_info *c)
  697. {
  698. int i;
  699. for (i = 0; i < c->lpt_lebs; i++) {
  700. if (i + c->lpt_first == c->nhead_lnum)
  701. continue;
  702. if (c->ltab[i].dirty > 0 &&
  703. c->ltab[i].free + c->ltab[i].dirty == c->leb_size) {
  704. c->ltab[i].tgc = 1;
  705. c->ltab[i].free = c->leb_size;
  706. c->ltab[i].dirty = 0;
  707. dbg_lp("LEB %d", i + c->lpt_first);
  708. }
  709. }
  710. }
  711. /**
  712. * lpt_tgc_end - end trivial garbage collection of LPT LEBs.
  713. * @c: UBIFS file-system description object
  714. *
  715. * LPT trivial garbage collection is where a LPT LEB contains only dirty and
  716. * free space and so may be reused as soon as the next commit is completed.
  717. * This function is called after the commit is completed (master node has been
  718. * written) and un-maps LPT LEBs that were marked for trivial GC.
  719. */
  720. static int lpt_tgc_end(struct ubifs_info *c)
  721. {
  722. int i, err;
  723. for (i = 0; i < c->lpt_lebs; i++)
  724. if (c->ltab[i].tgc) {
  725. err = ubifs_leb_unmap(c, i + c->lpt_first);
  726. if (err)
  727. return err;
  728. c->ltab[i].tgc = 0;
  729. dbg_lp("LEB %d", i + c->lpt_first);
  730. }
  731. return 0;
  732. }
  733. /**
  734. * populate_lsave - fill the lsave array with important LEB numbers.
  735. * @c: the UBIFS file-system description object
  736. *
  737. * This function is only called for the "big" model. It records a small number
  738. * of LEB numbers of important LEBs. Important LEBs are ones that are (from
  739. * most important to least important): empty, freeable, freeable index, dirty
  740. * index, dirty or free. Upon mount, we read this list of LEB numbers and bring
  741. * their pnodes into memory. That will stop us from having to scan the LPT
  742. * straight away. For the "small" model we assume that scanning the LPT is no
  743. * big deal.
  744. */
  745. static void populate_lsave(struct ubifs_info *c)
  746. {
  747. struct ubifs_lprops *lprops;
  748. struct ubifs_lpt_heap *heap;
  749. int i, cnt = 0;
  750. ubifs_assert(c->big_lpt);
  751. if (!(c->lpt_drty_flgs & LSAVE_DIRTY)) {
  752. c->lpt_drty_flgs |= LSAVE_DIRTY;
  753. ubifs_add_lpt_dirt(c, c->lsave_lnum, c->lsave_sz);
  754. }
  755. #ifndef __UBOOT__
  756. if (dbg_populate_lsave(c))
  757. return;
  758. #endif
  759. list_for_each_entry(lprops, &c->empty_list, list) {
  760. c->lsave[cnt++] = lprops->lnum;
  761. if (cnt >= c->lsave_cnt)
  762. return;
  763. }
  764. list_for_each_entry(lprops, &c->freeable_list, list) {
  765. c->lsave[cnt++] = lprops->lnum;
  766. if (cnt >= c->lsave_cnt)
  767. return;
  768. }
  769. list_for_each_entry(lprops, &c->frdi_idx_list, list) {
  770. c->lsave[cnt++] = lprops->lnum;
  771. if (cnt >= c->lsave_cnt)
  772. return;
  773. }
  774. heap = &c->lpt_heap[LPROPS_DIRTY_IDX - 1];
  775. for (i = 0; i < heap->cnt; i++) {
  776. c->lsave[cnt++] = heap->arr[i]->lnum;
  777. if (cnt >= c->lsave_cnt)
  778. return;
  779. }
  780. heap = &c->lpt_heap[LPROPS_DIRTY - 1];
  781. for (i = 0; i < heap->cnt; i++) {
  782. c->lsave[cnt++] = heap->arr[i]->lnum;
  783. if (cnt >= c->lsave_cnt)
  784. return;
  785. }
  786. heap = &c->lpt_heap[LPROPS_FREE - 1];
  787. for (i = 0; i < heap->cnt; i++) {
  788. c->lsave[cnt++] = heap->arr[i]->lnum;
  789. if (cnt >= c->lsave_cnt)
  790. return;
  791. }
  792. /* Fill it up completely */
  793. while (cnt < c->lsave_cnt)
  794. c->lsave[cnt++] = c->main_first;
  795. }
  796. /**
  797. * nnode_lookup - lookup a nnode in the LPT.
  798. * @c: UBIFS file-system description object
  799. * @i: nnode number
  800. *
  801. * This function returns a pointer to the nnode on success or a negative
  802. * error code on failure.
  803. */
  804. static struct ubifs_nnode *nnode_lookup(struct ubifs_info *c, int i)
  805. {
  806. int err, iip;
  807. struct ubifs_nnode *nnode;
  808. if (!c->nroot) {
  809. err = ubifs_read_nnode(c, NULL, 0);
  810. if (err)
  811. return ERR_PTR(err);
  812. }
  813. nnode = c->nroot;
  814. while (1) {
  815. iip = i & (UBIFS_LPT_FANOUT - 1);
  816. i >>= UBIFS_LPT_FANOUT_SHIFT;
  817. if (!i)
  818. break;
  819. nnode = ubifs_get_nnode(c, nnode, iip);
  820. if (IS_ERR(nnode))
  821. return nnode;
  822. }
  823. return nnode;
  824. }
  825. /**
  826. * make_nnode_dirty - find a nnode and, if found, make it dirty.
  827. * @c: UBIFS file-system description object
  828. * @node_num: nnode number of nnode to make dirty
  829. * @lnum: LEB number where nnode was written
  830. * @offs: offset where nnode was written
  831. *
  832. * This function is used by LPT garbage collection. LPT garbage collection is
  833. * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
  834. * simply involves marking all the nodes in the LEB being garbage-collected as
  835. * dirty. The dirty nodes are written next commit, after which the LEB is free
  836. * to be reused.
  837. *
  838. * This function returns %0 on success and a negative error code on failure.
  839. */
  840. static int make_nnode_dirty(struct ubifs_info *c, int node_num, int lnum,
  841. int offs)
  842. {
  843. struct ubifs_nnode *nnode;
  844. nnode = nnode_lookup(c, node_num);
  845. if (IS_ERR(nnode))
  846. return PTR_ERR(nnode);
  847. if (nnode->parent) {
  848. struct ubifs_nbranch *branch;
  849. branch = &nnode->parent->nbranch[nnode->iip];
  850. if (branch->lnum != lnum || branch->offs != offs)
  851. return 0; /* nnode is obsolete */
  852. } else if (c->lpt_lnum != lnum || c->lpt_offs != offs)
  853. return 0; /* nnode is obsolete */
  854. /* Assumes cnext list is empty i.e. not called during commit */
  855. if (!test_and_set_bit(DIRTY_CNODE, &nnode->flags)) {
  856. c->dirty_nn_cnt += 1;
  857. ubifs_add_nnode_dirt(c, nnode);
  858. /* Mark parent and ancestors dirty too */
  859. nnode = nnode->parent;
  860. while (nnode) {
  861. if (!test_and_set_bit(DIRTY_CNODE, &nnode->flags)) {
  862. c->dirty_nn_cnt += 1;
  863. ubifs_add_nnode_dirt(c, nnode);
  864. nnode = nnode->parent;
  865. } else
  866. break;
  867. }
  868. }
  869. return 0;
  870. }
  871. /**
  872. * make_pnode_dirty - find a pnode and, if found, make it dirty.
  873. * @c: UBIFS file-system description object
  874. * @node_num: pnode number of pnode to make dirty
  875. * @lnum: LEB number where pnode was written
  876. * @offs: offset where pnode was written
  877. *
  878. * This function is used by LPT garbage collection. LPT garbage collection is
  879. * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
  880. * simply involves marking all the nodes in the LEB being garbage-collected as
  881. * dirty. The dirty nodes are written next commit, after which the LEB is free
  882. * to be reused.
  883. *
  884. * This function returns %0 on success and a negative error code on failure.
  885. */
  886. static int make_pnode_dirty(struct ubifs_info *c, int node_num, int lnum,
  887. int offs)
  888. {
  889. struct ubifs_pnode *pnode;
  890. struct ubifs_nbranch *branch;
  891. pnode = pnode_lookup(c, node_num);
  892. if (IS_ERR(pnode))
  893. return PTR_ERR(pnode);
  894. branch = &pnode->parent->nbranch[pnode->iip];
  895. if (branch->lnum != lnum || branch->offs != offs)
  896. return 0;
  897. do_make_pnode_dirty(c, pnode);
  898. return 0;
  899. }
  900. /**
  901. * make_ltab_dirty - make ltab node dirty.
  902. * @c: UBIFS file-system description object
  903. * @lnum: LEB number where ltab was written
  904. * @offs: offset where ltab was written
  905. *
  906. * This function is used by LPT garbage collection. LPT garbage collection is
  907. * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
  908. * simply involves marking all the nodes in the LEB being garbage-collected as
  909. * dirty. The dirty nodes are written next commit, after which the LEB is free
  910. * to be reused.
  911. *
  912. * This function returns %0 on success and a negative error code on failure.
  913. */
  914. static int make_ltab_dirty(struct ubifs_info *c, int lnum, int offs)
  915. {
  916. if (lnum != c->ltab_lnum || offs != c->ltab_offs)
  917. return 0; /* This ltab node is obsolete */
  918. if (!(c->lpt_drty_flgs & LTAB_DIRTY)) {
  919. c->lpt_drty_flgs |= LTAB_DIRTY;
  920. ubifs_add_lpt_dirt(c, c->ltab_lnum, c->ltab_sz);
  921. }
  922. return 0;
  923. }
  924. /**
  925. * make_lsave_dirty - make lsave node dirty.
  926. * @c: UBIFS file-system description object
  927. * @lnum: LEB number where lsave was written
  928. * @offs: offset where lsave was written
  929. *
  930. * This function is used by LPT garbage collection. LPT garbage collection is
  931. * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
  932. * simply involves marking all the nodes in the LEB being garbage-collected as
  933. * dirty. The dirty nodes are written next commit, after which the LEB is free
  934. * to be reused.
  935. *
  936. * This function returns %0 on success and a negative error code on failure.
  937. */
  938. static int make_lsave_dirty(struct ubifs_info *c, int lnum, int offs)
  939. {
  940. if (lnum != c->lsave_lnum || offs != c->lsave_offs)
  941. return 0; /* This lsave node is obsolete */
  942. if (!(c->lpt_drty_flgs & LSAVE_DIRTY)) {
  943. c->lpt_drty_flgs |= LSAVE_DIRTY;
  944. ubifs_add_lpt_dirt(c, c->lsave_lnum, c->lsave_sz);
  945. }
  946. return 0;
  947. }
  948. /**
  949. * make_node_dirty - make node dirty.
  950. * @c: UBIFS file-system description object
  951. * @node_type: LPT node type
  952. * @node_num: node number
  953. * @lnum: LEB number where node was written
  954. * @offs: offset where node was written
  955. *
  956. * This function is used by LPT garbage collection. LPT garbage collection is
  957. * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
  958. * simply involves marking all the nodes in the LEB being garbage-collected as
  959. * dirty. The dirty nodes are written next commit, after which the LEB is free
  960. * to be reused.
  961. *
  962. * This function returns %0 on success and a negative error code on failure.
  963. */
  964. static int make_node_dirty(struct ubifs_info *c, int node_type, int node_num,
  965. int lnum, int offs)
  966. {
  967. switch (node_type) {
  968. case UBIFS_LPT_NNODE:
  969. return make_nnode_dirty(c, node_num, lnum, offs);
  970. case UBIFS_LPT_PNODE:
  971. return make_pnode_dirty(c, node_num, lnum, offs);
  972. case UBIFS_LPT_LTAB:
  973. return make_ltab_dirty(c, lnum, offs);
  974. case UBIFS_LPT_LSAVE:
  975. return make_lsave_dirty(c, lnum, offs);
  976. }
  977. return -EINVAL;
  978. }
  979. /**
  980. * get_lpt_node_len - return the length of a node based on its type.
  981. * @c: UBIFS file-system description object
  982. * @node_type: LPT node type
  983. */
  984. static int get_lpt_node_len(const struct ubifs_info *c, int node_type)
  985. {
  986. switch (node_type) {
  987. case UBIFS_LPT_NNODE:
  988. return c->nnode_sz;
  989. case UBIFS_LPT_PNODE:
  990. return c->pnode_sz;
  991. case UBIFS_LPT_LTAB:
  992. return c->ltab_sz;
  993. case UBIFS_LPT_LSAVE:
  994. return c->lsave_sz;
  995. }
  996. return 0;
  997. }
  998. /**
  999. * get_pad_len - return the length of padding in a buffer.
  1000. * @c: UBIFS file-system description object
  1001. * @buf: buffer
  1002. * @len: length of buffer
  1003. */
  1004. static int get_pad_len(const struct ubifs_info *c, uint8_t *buf, int len)
  1005. {
  1006. int offs, pad_len;
  1007. if (c->min_io_size == 1)
  1008. return 0;
  1009. offs = c->leb_size - len;
  1010. pad_len = ALIGN(offs, c->min_io_size) - offs;
  1011. return pad_len;
  1012. }
  1013. /**
  1014. * get_lpt_node_type - return type (and node number) of a node in a buffer.
  1015. * @c: UBIFS file-system description object
  1016. * @buf: buffer
  1017. * @node_num: node number is returned here
  1018. */
  1019. static int get_lpt_node_type(const struct ubifs_info *c, uint8_t *buf,
  1020. int *node_num)
  1021. {
  1022. uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
  1023. int pos = 0, node_type;
  1024. node_type = ubifs_unpack_bits(&addr, &pos, UBIFS_LPT_TYPE_BITS);
  1025. *node_num = ubifs_unpack_bits(&addr, &pos, c->pcnt_bits);
  1026. return node_type;
  1027. }
  1028. /**
  1029. * is_a_node - determine if a buffer contains a node.
  1030. * @c: UBIFS file-system description object
  1031. * @buf: buffer
  1032. * @len: length of buffer
  1033. *
  1034. * This function returns %1 if the buffer contains a node or %0 if it does not.
  1035. */
  1036. static int is_a_node(const struct ubifs_info *c, uint8_t *buf, int len)
  1037. {
  1038. uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
  1039. int pos = 0, node_type, node_len;
  1040. uint16_t crc, calc_crc;
  1041. if (len < UBIFS_LPT_CRC_BYTES + (UBIFS_LPT_TYPE_BITS + 7) / 8)
  1042. return 0;
  1043. node_type = ubifs_unpack_bits(&addr, &pos, UBIFS_LPT_TYPE_BITS);
  1044. if (node_type == UBIFS_LPT_NOT_A_NODE)
  1045. return 0;
  1046. node_len = get_lpt_node_len(c, node_type);
  1047. if (!node_len || node_len > len)
  1048. return 0;
  1049. pos = 0;
  1050. addr = buf;
  1051. crc = ubifs_unpack_bits(&addr, &pos, UBIFS_LPT_CRC_BITS);
  1052. calc_crc = crc16(-1, buf + UBIFS_LPT_CRC_BYTES,
  1053. node_len - UBIFS_LPT_CRC_BYTES);
  1054. if (crc != calc_crc)
  1055. return 0;
  1056. return 1;
  1057. }
  1058. /**
  1059. * lpt_gc_lnum - garbage collect a LPT LEB.
  1060. * @c: UBIFS file-system description object
  1061. * @lnum: LEB number to garbage collect
  1062. *
  1063. * LPT garbage collection is used only for the "big" LPT model
  1064. * (c->big_lpt == 1). Garbage collection simply involves marking all the nodes
  1065. * in the LEB being garbage-collected as dirty. The dirty nodes are written
  1066. * next commit, after which the LEB is free to be reused.
  1067. *
  1068. * This function returns %0 on success and a negative error code on failure.
  1069. */
  1070. static int lpt_gc_lnum(struct ubifs_info *c, int lnum)
  1071. {
  1072. int err, len = c->leb_size, node_type, node_num, node_len, offs;
  1073. void *buf = c->lpt_buf;
  1074. dbg_lp("LEB %d", lnum);
  1075. err = ubifs_leb_read(c, lnum, buf, 0, c->leb_size, 1);
  1076. if (err)
  1077. return err;
  1078. while (1) {
  1079. if (!is_a_node(c, buf, len)) {
  1080. int pad_len;
  1081. pad_len = get_pad_len(c, buf, len);
  1082. if (pad_len) {
  1083. buf += pad_len;
  1084. len -= pad_len;
  1085. continue;
  1086. }
  1087. return 0;
  1088. }
  1089. node_type = get_lpt_node_type(c, buf, &node_num);
  1090. node_len = get_lpt_node_len(c, node_type);
  1091. offs = c->leb_size - len;
  1092. ubifs_assert(node_len != 0);
  1093. mutex_lock(&c->lp_mutex);
  1094. err = make_node_dirty(c, node_type, node_num, lnum, offs);
  1095. mutex_unlock(&c->lp_mutex);
  1096. if (err)
  1097. return err;
  1098. buf += node_len;
  1099. len -= node_len;
  1100. }
  1101. return 0;
  1102. }
  1103. /**
  1104. * lpt_gc - LPT garbage collection.
  1105. * @c: UBIFS file-system description object
  1106. *
  1107. * Select a LPT LEB for LPT garbage collection and call 'lpt_gc_lnum()'.
  1108. * Returns %0 on success and a negative error code on failure.
  1109. */
  1110. static int lpt_gc(struct ubifs_info *c)
  1111. {
  1112. int i, lnum = -1, dirty = 0;
  1113. mutex_lock(&c->lp_mutex);
  1114. for (i = 0; i < c->lpt_lebs; i++) {
  1115. ubifs_assert(!c->ltab[i].tgc);
  1116. if (i + c->lpt_first == c->nhead_lnum ||
  1117. c->ltab[i].free + c->ltab[i].dirty == c->leb_size)
  1118. continue;
  1119. if (c->ltab[i].dirty > dirty) {
  1120. dirty = c->ltab[i].dirty;
  1121. lnum = i + c->lpt_first;
  1122. }
  1123. }
  1124. mutex_unlock(&c->lp_mutex);
  1125. if (lnum == -1)
  1126. return -ENOSPC;
  1127. return lpt_gc_lnum(c, lnum);
  1128. }
  1129. /**
  1130. * ubifs_lpt_start_commit - UBIFS commit starts.
  1131. * @c: the UBIFS file-system description object
  1132. *
  1133. * This function has to be called when UBIFS starts the commit operation.
  1134. * This function "freezes" all currently dirty LEB properties and does not
  1135. * change them anymore. Further changes are saved and tracked separately
  1136. * because they are not part of this commit. This function returns zero in case
  1137. * of success and a negative error code in case of failure.
  1138. */
  1139. int ubifs_lpt_start_commit(struct ubifs_info *c)
  1140. {
  1141. int err, cnt;
  1142. dbg_lp("");
  1143. mutex_lock(&c->lp_mutex);
  1144. err = dbg_chk_lpt_free_spc(c);
  1145. if (err)
  1146. goto out;
  1147. err = dbg_check_ltab(c);
  1148. if (err)
  1149. goto out;
  1150. if (c->check_lpt_free) {
  1151. /*
  1152. * We ensure there is enough free space in
  1153. * ubifs_lpt_post_commit() by marking nodes dirty. That
  1154. * information is lost when we unmount, so we also need
  1155. * to check free space once after mounting also.
  1156. */
  1157. c->check_lpt_free = 0;
  1158. while (need_write_all(c)) {
  1159. mutex_unlock(&c->lp_mutex);
  1160. err = lpt_gc(c);
  1161. if (err)
  1162. return err;
  1163. mutex_lock(&c->lp_mutex);
  1164. }
  1165. }
  1166. lpt_tgc_start(c);
  1167. if (!c->dirty_pn_cnt) {
  1168. dbg_cmt("no cnodes to commit");
  1169. err = 0;
  1170. goto out;
  1171. }
  1172. if (!c->big_lpt && need_write_all(c)) {
  1173. /* If needed, write everything */
  1174. err = make_tree_dirty(c);
  1175. if (err)
  1176. goto out;
  1177. lpt_tgc_start(c);
  1178. }
  1179. if (c->big_lpt)
  1180. populate_lsave(c);
  1181. cnt = get_cnodes_to_commit(c);
  1182. ubifs_assert(cnt != 0);
  1183. err = layout_cnodes(c);
  1184. if (err)
  1185. goto out;
  1186. /* Copy the LPT's own lprops for end commit to write */
  1187. memcpy(c->ltab_cmt, c->ltab,
  1188. sizeof(struct ubifs_lpt_lprops) * c->lpt_lebs);
  1189. c->lpt_drty_flgs &= ~(LTAB_DIRTY | LSAVE_DIRTY);
  1190. out:
  1191. mutex_unlock(&c->lp_mutex);
  1192. return err;
  1193. }
  1194. /**
  1195. * free_obsolete_cnodes - free obsolete cnodes for commit end.
  1196. * @c: UBIFS file-system description object
  1197. */
  1198. static void free_obsolete_cnodes(struct ubifs_info *c)
  1199. {
  1200. struct ubifs_cnode *cnode, *cnext;
  1201. cnext = c->lpt_cnext;
  1202. if (!cnext)
  1203. return;
  1204. do {
  1205. cnode = cnext;
  1206. cnext = cnode->cnext;
  1207. if (test_bit(OBSOLETE_CNODE, &cnode->flags))
  1208. kfree(cnode);
  1209. else
  1210. cnode->cnext = NULL;
  1211. } while (cnext != c->lpt_cnext);
  1212. c->lpt_cnext = NULL;
  1213. }
  1214. #ifndef __UBOOT__
  1215. /**
  1216. * ubifs_lpt_end_commit - finish the commit operation.
  1217. * @c: the UBIFS file-system description object
  1218. *
  1219. * This function has to be called when the commit operation finishes. It
  1220. * flushes the changes which were "frozen" by 'ubifs_lprops_start_commit()' to
  1221. * the media. Returns zero in case of success and a negative error code in case
  1222. * of failure.
  1223. */
  1224. int ubifs_lpt_end_commit(struct ubifs_info *c)
  1225. {
  1226. int err;
  1227. dbg_lp("");
  1228. if (!c->lpt_cnext)
  1229. return 0;
  1230. err = write_cnodes(c);
  1231. if (err)
  1232. return err;
  1233. mutex_lock(&c->lp_mutex);
  1234. free_obsolete_cnodes(c);
  1235. mutex_unlock(&c->lp_mutex);
  1236. return 0;
  1237. }
  1238. #endif
  1239. /**
  1240. * ubifs_lpt_post_commit - post commit LPT trivial GC and LPT GC.
  1241. * @c: UBIFS file-system description object
  1242. *
  1243. * LPT trivial GC is completed after a commit. Also LPT GC is done after a
  1244. * commit for the "big" LPT model.
  1245. */
  1246. int ubifs_lpt_post_commit(struct ubifs_info *c)
  1247. {
  1248. int err;
  1249. mutex_lock(&c->lp_mutex);
  1250. err = lpt_tgc_end(c);
  1251. if (err)
  1252. goto out;
  1253. if (c->big_lpt)
  1254. while (need_write_all(c)) {
  1255. mutex_unlock(&c->lp_mutex);
  1256. err = lpt_gc(c);
  1257. if (err)
  1258. return err;
  1259. mutex_lock(&c->lp_mutex);
  1260. }
  1261. out:
  1262. mutex_unlock(&c->lp_mutex);
  1263. return err;
  1264. }
  1265. /**
  1266. * first_nnode - find the first nnode in memory.
  1267. * @c: UBIFS file-system description object
  1268. * @hght: height of tree where nnode found is returned here
  1269. *
  1270. * This function returns a pointer to the nnode found or %NULL if no nnode is
  1271. * found. This function is a helper to 'ubifs_lpt_free()'.
  1272. */
  1273. static struct ubifs_nnode *first_nnode(struct ubifs_info *c, int *hght)
  1274. {
  1275. struct ubifs_nnode *nnode;
  1276. int h, i, found;
  1277. nnode = c->nroot;
  1278. *hght = 0;
  1279. if (!nnode)
  1280. return NULL;
  1281. for (h = 1; h < c->lpt_hght; h++) {
  1282. found = 0;
  1283. for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
  1284. if (nnode->nbranch[i].nnode) {
  1285. found = 1;
  1286. nnode = nnode->nbranch[i].nnode;
  1287. *hght = h;
  1288. break;
  1289. }
  1290. }
  1291. if (!found)
  1292. break;
  1293. }
  1294. return nnode;
  1295. }
  1296. /**
  1297. * next_nnode - find the next nnode in memory.
  1298. * @c: UBIFS file-system description object
  1299. * @nnode: nnode from which to start.
  1300. * @hght: height of tree where nnode is, is passed and returned here
  1301. *
  1302. * This function returns a pointer to the nnode found or %NULL if no nnode is
  1303. * found. This function is a helper to 'ubifs_lpt_free()'.
  1304. */
  1305. static struct ubifs_nnode *next_nnode(struct ubifs_info *c,
  1306. struct ubifs_nnode *nnode, int *hght)
  1307. {
  1308. struct ubifs_nnode *parent;
  1309. int iip, h, i, found;
  1310. parent = nnode->parent;
  1311. if (!parent)
  1312. return NULL;
  1313. if (nnode->iip == UBIFS_LPT_FANOUT - 1) {
  1314. *hght -= 1;
  1315. return parent;
  1316. }
  1317. for (iip = nnode->iip + 1; iip < UBIFS_LPT_FANOUT; iip++) {
  1318. nnode = parent->nbranch[iip].nnode;
  1319. if (nnode)
  1320. break;
  1321. }
  1322. if (!nnode) {
  1323. *hght -= 1;
  1324. return parent;
  1325. }
  1326. for (h = *hght + 1; h < c->lpt_hght; h++) {
  1327. found = 0;
  1328. for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
  1329. if (nnode->nbranch[i].nnode) {
  1330. found = 1;
  1331. nnode = nnode->nbranch[i].nnode;
  1332. *hght = h;
  1333. break;
  1334. }
  1335. }
  1336. if (!found)
  1337. break;
  1338. }
  1339. return nnode;
  1340. }
  1341. /**
  1342. * ubifs_lpt_free - free resources owned by the LPT.
  1343. * @c: UBIFS file-system description object
  1344. * @wr_only: free only resources used for writing
  1345. */
  1346. void ubifs_lpt_free(struct ubifs_info *c, int wr_only)
  1347. {
  1348. struct ubifs_nnode *nnode;
  1349. int i, hght;
  1350. /* Free write-only things first */
  1351. free_obsolete_cnodes(c); /* Leftover from a failed commit */
  1352. vfree(c->ltab_cmt);
  1353. c->ltab_cmt = NULL;
  1354. vfree(c->lpt_buf);
  1355. c->lpt_buf = NULL;
  1356. kfree(c->lsave);
  1357. c->lsave = NULL;
  1358. if (wr_only)
  1359. return;
  1360. /* Now free the rest */
  1361. nnode = first_nnode(c, &hght);
  1362. while (nnode) {
  1363. for (i = 0; i < UBIFS_LPT_FANOUT; i++)
  1364. kfree(nnode->nbranch[i].nnode);
  1365. nnode = next_nnode(c, nnode, &hght);
  1366. }
  1367. for (i = 0; i < LPROPS_HEAP_CNT; i++)
  1368. kfree(c->lpt_heap[i].arr);
  1369. kfree(c->dirty_idx.arr);
  1370. kfree(c->nroot);
  1371. vfree(c->ltab);
  1372. kfree(c->lpt_nod_buf);
  1373. }
  1374. #ifndef __UBOOT__
  1375. /*
  1376. * Everything below is related to debugging.
  1377. */
  1378. /**
  1379. * dbg_is_all_ff - determine if a buffer contains only 0xFF bytes.
  1380. * @buf: buffer
  1381. * @len: buffer length
  1382. */
  1383. static int dbg_is_all_ff(uint8_t *buf, int len)
  1384. {
  1385. int i;
  1386. for (i = 0; i < len; i++)
  1387. if (buf[i] != 0xff)
  1388. return 0;
  1389. return 1;
  1390. }
  1391. /**
  1392. * dbg_is_nnode_dirty - determine if a nnode is dirty.
  1393. * @c: the UBIFS file-system description object
  1394. * @lnum: LEB number where nnode was written
  1395. * @offs: offset where nnode was written
  1396. */
  1397. static int dbg_is_nnode_dirty(struct ubifs_info *c, int lnum, int offs)
  1398. {
  1399. struct ubifs_nnode *nnode;
  1400. int hght;
  1401. /* Entire tree is in memory so first_nnode / next_nnode are OK */
  1402. nnode = first_nnode(c, &hght);
  1403. for (; nnode; nnode = next_nnode(c, nnode, &hght)) {
  1404. struct ubifs_nbranch *branch;
  1405. cond_resched();
  1406. if (nnode->parent) {
  1407. branch = &nnode->parent->nbranch[nnode->iip];
  1408. if (branch->lnum != lnum || branch->offs != offs)
  1409. continue;
  1410. if (test_bit(DIRTY_CNODE, &nnode->flags))
  1411. return 1;
  1412. return 0;
  1413. } else {
  1414. if (c->lpt_lnum != lnum || c->lpt_offs != offs)
  1415. continue;
  1416. if (test_bit(DIRTY_CNODE, &nnode->flags))
  1417. return 1;
  1418. return 0;
  1419. }
  1420. }
  1421. return 1;
  1422. }
  1423. /**
  1424. * dbg_is_pnode_dirty - determine if a pnode is dirty.
  1425. * @c: the UBIFS file-system description object
  1426. * @lnum: LEB number where pnode was written
  1427. * @offs: offset where pnode was written
  1428. */
  1429. static int dbg_is_pnode_dirty(struct ubifs_info *c, int lnum, int offs)
  1430. {
  1431. int i, cnt;
  1432. cnt = DIV_ROUND_UP(c->main_lebs, UBIFS_LPT_FANOUT);
  1433. for (i = 0; i < cnt; i++) {
  1434. struct ubifs_pnode *pnode;
  1435. struct ubifs_nbranch *branch;
  1436. cond_resched();
  1437. pnode = pnode_lookup(c, i);
  1438. if (IS_ERR(pnode))
  1439. return PTR_ERR(pnode);
  1440. branch = &pnode->parent->nbranch[pnode->iip];
  1441. if (branch->lnum != lnum || branch->offs != offs)
  1442. continue;
  1443. if (test_bit(DIRTY_CNODE, &pnode->flags))
  1444. return 1;
  1445. return 0;
  1446. }
  1447. return 1;
  1448. }
  1449. /**
  1450. * dbg_is_ltab_dirty - determine if a ltab node is dirty.
  1451. * @c: the UBIFS file-system description object
  1452. * @lnum: LEB number where ltab node was written
  1453. * @offs: offset where ltab node was written
  1454. */
  1455. static int dbg_is_ltab_dirty(struct ubifs_info *c, int lnum, int offs)
  1456. {
  1457. if (lnum != c->ltab_lnum || offs != c->ltab_offs)
  1458. return 1;
  1459. return (c->lpt_drty_flgs & LTAB_DIRTY) != 0;
  1460. }
  1461. /**
  1462. * dbg_is_lsave_dirty - determine if a lsave node is dirty.
  1463. * @c: the UBIFS file-system description object
  1464. * @lnum: LEB number where lsave node was written
  1465. * @offs: offset where lsave node was written
  1466. */
  1467. static int dbg_is_lsave_dirty(struct ubifs_info *c, int lnum, int offs)
  1468. {
  1469. if (lnum != c->lsave_lnum || offs != c->lsave_offs)
  1470. return 1;
  1471. return (c->lpt_drty_flgs & LSAVE_DIRTY) != 0;
  1472. }
  1473. /**
  1474. * dbg_is_node_dirty - determine if a node is dirty.
  1475. * @c: the UBIFS file-system description object
  1476. * @node_type: node type
  1477. * @lnum: LEB number where node was written
  1478. * @offs: offset where node was written
  1479. */
  1480. static int dbg_is_node_dirty(struct ubifs_info *c, int node_type, int lnum,
  1481. int offs)
  1482. {
  1483. switch (node_type) {
  1484. case UBIFS_LPT_NNODE:
  1485. return dbg_is_nnode_dirty(c, lnum, offs);
  1486. case UBIFS_LPT_PNODE:
  1487. return dbg_is_pnode_dirty(c, lnum, offs);
  1488. case UBIFS_LPT_LTAB:
  1489. return dbg_is_ltab_dirty(c, lnum, offs);
  1490. case UBIFS_LPT_LSAVE:
  1491. return dbg_is_lsave_dirty(c, lnum, offs);
  1492. }
  1493. return 1;
  1494. }
  1495. /**
  1496. * dbg_check_ltab_lnum - check the ltab for a LPT LEB number.
  1497. * @c: the UBIFS file-system description object
  1498. * @lnum: LEB number where node was written
  1499. * @offs: offset where node was written
  1500. *
  1501. * This function returns %0 on success and a negative error code on failure.
  1502. */
  1503. static int dbg_check_ltab_lnum(struct ubifs_info *c, int lnum)
  1504. {
  1505. int err, len = c->leb_size, dirty = 0, node_type, node_num, node_len;
  1506. int ret;
  1507. void *buf, *p;
  1508. if (!dbg_is_chk_lprops(c))
  1509. return 0;
  1510. buf = p = __vmalloc(c->leb_size, GFP_NOFS, PAGE_KERNEL);
  1511. if (!buf) {
  1512. ubifs_err(c, "cannot allocate memory for ltab checking");
  1513. return 0;
  1514. }
  1515. dbg_lp("LEB %d", lnum);
  1516. err = ubifs_leb_read(c, lnum, buf, 0, c->leb_size, 1);
  1517. if (err)
  1518. goto out;
  1519. while (1) {
  1520. if (!is_a_node(c, p, len)) {
  1521. int i, pad_len;
  1522. pad_len = get_pad_len(c, p, len);
  1523. if (pad_len) {
  1524. p += pad_len;
  1525. len -= pad_len;
  1526. dirty += pad_len;
  1527. continue;
  1528. }
  1529. if (!dbg_is_all_ff(p, len)) {
  1530. ubifs_err(c, "invalid empty space in LEB %d at %d",
  1531. lnum, c->leb_size - len);
  1532. err = -EINVAL;
  1533. }
  1534. i = lnum - c->lpt_first;
  1535. if (len != c->ltab[i].free) {
  1536. ubifs_err(c, "invalid free space in LEB %d (free %d, expected %d)",
  1537. lnum, len, c->ltab[i].free);
  1538. err = -EINVAL;
  1539. }
  1540. if (dirty != c->ltab[i].dirty) {
  1541. ubifs_err(c, "invalid dirty space in LEB %d (dirty %d, expected %d)",
  1542. lnum, dirty, c->ltab[i].dirty);
  1543. err = -EINVAL;
  1544. }
  1545. goto out;
  1546. }
  1547. node_type = get_lpt_node_type(c, p, &node_num);
  1548. node_len = get_lpt_node_len(c, node_type);
  1549. ret = dbg_is_node_dirty(c, node_type, lnum, c->leb_size - len);
  1550. if (ret == 1)
  1551. dirty += node_len;
  1552. p += node_len;
  1553. len -= node_len;
  1554. }
  1555. err = 0;
  1556. out:
  1557. vfree(buf);
  1558. return err;
  1559. }
  1560. /**
  1561. * dbg_check_ltab - check the free and dirty space in the ltab.
  1562. * @c: the UBIFS file-system description object
  1563. *
  1564. * This function returns %0 on success and a negative error code on failure.
  1565. */
  1566. int dbg_check_ltab(struct ubifs_info *c)
  1567. {
  1568. int lnum, err, i, cnt;
  1569. if (!dbg_is_chk_lprops(c))
  1570. return 0;
  1571. /* Bring the entire tree into memory */
  1572. cnt = DIV_ROUND_UP(c->main_lebs, UBIFS_LPT_FANOUT);
  1573. for (i = 0; i < cnt; i++) {
  1574. struct ubifs_pnode *pnode;
  1575. pnode = pnode_lookup(c, i);
  1576. if (IS_ERR(pnode))
  1577. return PTR_ERR(pnode);
  1578. cond_resched();
  1579. }
  1580. /* Check nodes */
  1581. err = dbg_check_lpt_nodes(c, (struct ubifs_cnode *)c->nroot, 0, 0);
  1582. if (err)
  1583. return err;
  1584. /* Check each LEB */
  1585. for (lnum = c->lpt_first; lnum <= c->lpt_last; lnum++) {
  1586. err = dbg_check_ltab_lnum(c, lnum);
  1587. if (err) {
  1588. ubifs_err(c, "failed at LEB %d", lnum);
  1589. return err;
  1590. }
  1591. }
  1592. dbg_lp("succeeded");
  1593. return 0;
  1594. }
  1595. /**
  1596. * dbg_chk_lpt_free_spc - check LPT free space is enough to write entire LPT.
  1597. * @c: the UBIFS file-system description object
  1598. *
  1599. * This function returns %0 on success and a negative error code on failure.
  1600. */
  1601. int dbg_chk_lpt_free_spc(struct ubifs_info *c)
  1602. {
  1603. long long free = 0;
  1604. int i;
  1605. if (!dbg_is_chk_lprops(c))
  1606. return 0;
  1607. for (i = 0; i < c->lpt_lebs; i++) {
  1608. if (c->ltab[i].tgc || c->ltab[i].cmt)
  1609. continue;
  1610. if (i + c->lpt_first == c->nhead_lnum)
  1611. free += c->leb_size - c->nhead_offs;
  1612. else if (c->ltab[i].free == c->leb_size)
  1613. free += c->leb_size;
  1614. }
  1615. if (free < c->lpt_sz) {
  1616. ubifs_err(c, "LPT space error: free %lld lpt_sz %lld",
  1617. free, c->lpt_sz);
  1618. ubifs_dump_lpt_info(c);
  1619. ubifs_dump_lpt_lebs(c);
  1620. dump_stack();
  1621. return -EINVAL;
  1622. }
  1623. return 0;
  1624. }
  1625. /**
  1626. * dbg_chk_lpt_sz - check LPT does not write more than LPT size.
  1627. * @c: the UBIFS file-system description object
  1628. * @action: what to do
  1629. * @len: length written
  1630. *
  1631. * This function returns %0 on success and a negative error code on failure.
  1632. * The @action argument may be one of:
  1633. * o %0 - LPT debugging checking starts, initialize debugging variables;
  1634. * o %1 - wrote an LPT node, increase LPT size by @len bytes;
  1635. * o %2 - switched to a different LEB and wasted @len bytes;
  1636. * o %3 - check that we've written the right number of bytes.
  1637. * o %4 - wasted @len bytes;
  1638. */
  1639. int dbg_chk_lpt_sz(struct ubifs_info *c, int action, int len)
  1640. {
  1641. struct ubifs_debug_info *d = c->dbg;
  1642. long long chk_lpt_sz, lpt_sz;
  1643. int err = 0;
  1644. if (!dbg_is_chk_lprops(c))
  1645. return 0;
  1646. switch (action) {
  1647. case 0:
  1648. d->chk_lpt_sz = 0;
  1649. d->chk_lpt_sz2 = 0;
  1650. d->chk_lpt_lebs = 0;
  1651. d->chk_lpt_wastage = 0;
  1652. if (c->dirty_pn_cnt > c->pnode_cnt) {
  1653. ubifs_err(c, "dirty pnodes %d exceed max %d",
  1654. c->dirty_pn_cnt, c->pnode_cnt);
  1655. err = -EINVAL;
  1656. }
  1657. if (c->dirty_nn_cnt > c->nnode_cnt) {
  1658. ubifs_err(c, "dirty nnodes %d exceed max %d",
  1659. c->dirty_nn_cnt, c->nnode_cnt);
  1660. err = -EINVAL;
  1661. }
  1662. return err;
  1663. case 1:
  1664. d->chk_lpt_sz += len;
  1665. return 0;
  1666. case 2:
  1667. d->chk_lpt_sz += len;
  1668. d->chk_lpt_wastage += len;
  1669. d->chk_lpt_lebs += 1;
  1670. return 0;
  1671. case 3:
  1672. chk_lpt_sz = c->leb_size;
  1673. chk_lpt_sz *= d->chk_lpt_lebs;
  1674. chk_lpt_sz += len - c->nhead_offs;
  1675. if (d->chk_lpt_sz != chk_lpt_sz) {
  1676. ubifs_err(c, "LPT wrote %lld but space used was %lld",
  1677. d->chk_lpt_sz, chk_lpt_sz);
  1678. err = -EINVAL;
  1679. }
  1680. if (d->chk_lpt_sz > c->lpt_sz) {
  1681. ubifs_err(c, "LPT wrote %lld but lpt_sz is %lld",
  1682. d->chk_lpt_sz, c->lpt_sz);
  1683. err = -EINVAL;
  1684. }
  1685. if (d->chk_lpt_sz2 && d->chk_lpt_sz != d->chk_lpt_sz2) {
  1686. ubifs_err(c, "LPT layout size %lld but wrote %lld",
  1687. d->chk_lpt_sz, d->chk_lpt_sz2);
  1688. err = -EINVAL;
  1689. }
  1690. if (d->chk_lpt_sz2 && d->new_nhead_offs != len) {
  1691. ubifs_err(c, "LPT new nhead offs: expected %d was %d",
  1692. d->new_nhead_offs, len);
  1693. err = -EINVAL;
  1694. }
  1695. lpt_sz = (long long)c->pnode_cnt * c->pnode_sz;
  1696. lpt_sz += (long long)c->nnode_cnt * c->nnode_sz;
  1697. lpt_sz += c->ltab_sz;
  1698. if (c->big_lpt)
  1699. lpt_sz += c->lsave_sz;
  1700. if (d->chk_lpt_sz - d->chk_lpt_wastage > lpt_sz) {
  1701. ubifs_err(c, "LPT chk_lpt_sz %lld + waste %lld exceeds %lld",
  1702. d->chk_lpt_sz, d->chk_lpt_wastage, lpt_sz);
  1703. err = -EINVAL;
  1704. }
  1705. if (err) {
  1706. ubifs_dump_lpt_info(c);
  1707. ubifs_dump_lpt_lebs(c);
  1708. dump_stack();
  1709. }
  1710. d->chk_lpt_sz2 = d->chk_lpt_sz;
  1711. d->chk_lpt_sz = 0;
  1712. d->chk_lpt_wastage = 0;
  1713. d->chk_lpt_lebs = 0;
  1714. d->new_nhead_offs = len;
  1715. return err;
  1716. case 4:
  1717. d->chk_lpt_sz += len;
  1718. d->chk_lpt_wastage += len;
  1719. return 0;
  1720. default:
  1721. return -EINVAL;
  1722. }
  1723. }
  1724. /**
  1725. * ubifs_dump_lpt_leb - dump an LPT LEB.
  1726. * @c: UBIFS file-system description object
  1727. * @lnum: LEB number to dump
  1728. *
  1729. * This function dumps an LEB from LPT area. Nodes in this area are very
  1730. * different to nodes in the main area (e.g., they do not have common headers,
  1731. * they do not have 8-byte alignments, etc), so we have a separate function to
  1732. * dump LPT area LEBs. Note, LPT has to be locked by the caller.
  1733. */
  1734. static void dump_lpt_leb(const struct ubifs_info *c, int lnum)
  1735. {
  1736. int err, len = c->leb_size, node_type, node_num, node_len, offs;
  1737. void *buf, *p;
  1738. pr_err("(pid %d) start dumping LEB %d\n", current->pid, lnum);
  1739. buf = p = __vmalloc(c->leb_size, GFP_NOFS, PAGE_KERNEL);
  1740. if (!buf) {
  1741. ubifs_err(c, "cannot allocate memory to dump LPT");
  1742. return;
  1743. }
  1744. err = ubifs_leb_read(c, lnum, buf, 0, c->leb_size, 1);
  1745. if (err)
  1746. goto out;
  1747. while (1) {
  1748. offs = c->leb_size - len;
  1749. if (!is_a_node(c, p, len)) {
  1750. int pad_len;
  1751. pad_len = get_pad_len(c, p, len);
  1752. if (pad_len) {
  1753. pr_err("LEB %d:%d, pad %d bytes\n",
  1754. lnum, offs, pad_len);
  1755. p += pad_len;
  1756. len -= pad_len;
  1757. continue;
  1758. }
  1759. if (len)
  1760. pr_err("LEB %d:%d, free %d bytes\n",
  1761. lnum, offs, len);
  1762. break;
  1763. }
  1764. node_type = get_lpt_node_type(c, p, &node_num);
  1765. switch (node_type) {
  1766. case UBIFS_LPT_PNODE:
  1767. {
  1768. node_len = c->pnode_sz;
  1769. if (c->big_lpt)
  1770. pr_err("LEB %d:%d, pnode num %d\n",
  1771. lnum, offs, node_num);
  1772. else
  1773. pr_err("LEB %d:%d, pnode\n", lnum, offs);
  1774. break;
  1775. }
  1776. case UBIFS_LPT_NNODE:
  1777. {
  1778. int i;
  1779. struct ubifs_nnode nnode;
  1780. node_len = c->nnode_sz;
  1781. if (c->big_lpt)
  1782. pr_err("LEB %d:%d, nnode num %d, ",
  1783. lnum, offs, node_num);
  1784. else
  1785. pr_err("LEB %d:%d, nnode, ",
  1786. lnum, offs);
  1787. err = ubifs_unpack_nnode(c, p, &nnode);
  1788. if (err) {
  1789. pr_err("failed to unpack_node, error %d\n",
  1790. err);
  1791. break;
  1792. }
  1793. for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
  1794. pr_cont("%d:%d", nnode.nbranch[i].lnum,
  1795. nnode.nbranch[i].offs);
  1796. if (i != UBIFS_LPT_FANOUT - 1)
  1797. pr_cont(", ");
  1798. }
  1799. pr_cont("\n");
  1800. break;
  1801. }
  1802. case UBIFS_LPT_LTAB:
  1803. node_len = c->ltab_sz;
  1804. pr_err("LEB %d:%d, ltab\n", lnum, offs);
  1805. break;
  1806. case UBIFS_LPT_LSAVE:
  1807. node_len = c->lsave_sz;
  1808. pr_err("LEB %d:%d, lsave len\n", lnum, offs);
  1809. break;
  1810. default:
  1811. ubifs_err(c, "LPT node type %d not recognized", node_type);
  1812. goto out;
  1813. }
  1814. p += node_len;
  1815. len -= node_len;
  1816. }
  1817. pr_err("(pid %d) finish dumping LEB %d\n", current->pid, lnum);
  1818. out:
  1819. vfree(buf);
  1820. return;
  1821. }
  1822. /**
  1823. * ubifs_dump_lpt_lebs - dump LPT lebs.
  1824. * @c: UBIFS file-system description object
  1825. *
  1826. * This function dumps all LPT LEBs. The caller has to make sure the LPT is
  1827. * locked.
  1828. */
  1829. void ubifs_dump_lpt_lebs(const struct ubifs_info *c)
  1830. {
  1831. int i;
  1832. pr_err("(pid %d) start dumping all LPT LEBs\n", current->pid);
  1833. for (i = 0; i < c->lpt_lebs; i++)
  1834. dump_lpt_leb(c, i + c->lpt_first);
  1835. pr_err("(pid %d) finish dumping all LPT LEBs\n", current->pid);
  1836. }
  1837. /**
  1838. * dbg_populate_lsave - debugging version of 'populate_lsave()'
  1839. * @c: UBIFS file-system description object
  1840. *
  1841. * This is a debugging version for 'populate_lsave()' which populates lsave
  1842. * with random LEBs instead of useful LEBs, which is good for test coverage.
  1843. * Returns zero if lsave has not been populated (this debugging feature is
  1844. * disabled) an non-zero if lsave has been populated.
  1845. */
  1846. static int dbg_populate_lsave(struct ubifs_info *c)
  1847. {
  1848. struct ubifs_lprops *lprops;
  1849. struct ubifs_lpt_heap *heap;
  1850. int i;
  1851. if (!dbg_is_chk_gen(c))
  1852. return 0;
  1853. if (prandom_u32() & 3)
  1854. return 0;
  1855. for (i = 0; i < c->lsave_cnt; i++)
  1856. c->lsave[i] = c->main_first;
  1857. list_for_each_entry(lprops, &c->empty_list, list)
  1858. c->lsave[prandom_u32() % c->lsave_cnt] = lprops->lnum;
  1859. list_for_each_entry(lprops, &c->freeable_list, list)
  1860. c->lsave[prandom_u32() % c->lsave_cnt] = lprops->lnum;
  1861. list_for_each_entry(lprops, &c->frdi_idx_list, list)
  1862. c->lsave[prandom_u32() % c->lsave_cnt] = lprops->lnum;
  1863. heap = &c->lpt_heap[LPROPS_DIRTY_IDX - 1];
  1864. for (i = 0; i < heap->cnt; i++)
  1865. c->lsave[prandom_u32() % c->lsave_cnt] = heap->arr[i]->lnum;
  1866. heap = &c->lpt_heap[LPROPS_DIRTY - 1];
  1867. for (i = 0; i < heap->cnt; i++)
  1868. c->lsave[prandom_u32() % c->lsave_cnt] = heap->arr[i]->lnum;
  1869. heap = &c->lpt_heap[LPROPS_FREE - 1];
  1870. for (i = 0; i < heap->cnt; i++)
  1871. c->lsave[prandom_u32() % c->lsave_cnt] = heap->arr[i]->lnum;
  1872. return 1;
  1873. }
  1874. #endif