lpt_commit.c 49 KB

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