lpt_commit.c 51 KB

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