lpt_commit.c 51 KB

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