lpt_commit.c 52 KB

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