lpt_commit.c 51 KB

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