lpt_commit.c 51 KB

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