lpt_commit.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973
  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, c->leb_size - offs);
  214. err = alloc_lpt_leb(c, &lnum);
  215. if (err)
  216. goto no_space;
  217. offs = 0;
  218. ubifs_assert(lnum >= c->lpt_first &&
  219. lnum <= c->lpt_last);
  220. /* Try to place lsave and ltab nicely */
  221. if (!done_lsave) {
  222. done_lsave = 1;
  223. c->lsave_lnum = lnum;
  224. c->lsave_offs = offs;
  225. offs += c->lsave_sz;
  226. dbg_chk_lpt_sz(c, 1, c->lsave_sz);
  227. continue;
  228. }
  229. if (!done_ltab) {
  230. done_ltab = 1;
  231. c->ltab_lnum = lnum;
  232. c->ltab_offs = offs;
  233. offs += c->ltab_sz;
  234. dbg_chk_lpt_sz(c, 1, c->ltab_sz);
  235. continue;
  236. }
  237. break;
  238. }
  239. if (cnode->parent) {
  240. cnode->parent->nbranch[cnode->iip].lnum = lnum;
  241. cnode->parent->nbranch[cnode->iip].offs = offs;
  242. } else {
  243. c->lpt_lnum = lnum;
  244. c->lpt_offs = offs;
  245. }
  246. offs += len;
  247. dbg_chk_lpt_sz(c, 1, len);
  248. cnode = cnode->cnext;
  249. } while (cnode && cnode != c->lpt_cnext);
  250. /* Make sure to place LPT's save table */
  251. if (!done_lsave) {
  252. if (offs + c->lsave_sz > c->leb_size) {
  253. alen = ALIGN(offs, c->min_io_size);
  254. upd_ltab(c, lnum, c->leb_size - alen, alen - offs);
  255. dbg_chk_lpt_sz(c, 2, c->leb_size - offs);
  256. err = alloc_lpt_leb(c, &lnum);
  257. if (err)
  258. goto no_space;
  259. offs = 0;
  260. ubifs_assert(lnum >= c->lpt_first &&
  261. lnum <= c->lpt_last);
  262. }
  263. done_lsave = 1;
  264. c->lsave_lnum = lnum;
  265. c->lsave_offs = offs;
  266. offs += c->lsave_sz;
  267. dbg_chk_lpt_sz(c, 1, c->lsave_sz);
  268. }
  269. /* Make sure to place LPT's own lprops table */
  270. if (!done_ltab) {
  271. if (offs + c->ltab_sz > c->leb_size) {
  272. alen = ALIGN(offs, c->min_io_size);
  273. upd_ltab(c, lnum, c->leb_size - alen, alen - offs);
  274. dbg_chk_lpt_sz(c, 2, c->leb_size - offs);
  275. err = alloc_lpt_leb(c, &lnum);
  276. if (err)
  277. goto no_space;
  278. offs = 0;
  279. ubifs_assert(lnum >= c->lpt_first &&
  280. lnum <= c->lpt_last);
  281. }
  282. done_ltab = 1;
  283. c->ltab_lnum = lnum;
  284. c->ltab_offs = offs;
  285. offs += c->ltab_sz;
  286. dbg_chk_lpt_sz(c, 1, c->ltab_sz);
  287. }
  288. alen = ALIGN(offs, c->min_io_size);
  289. upd_ltab(c, lnum, c->leb_size - alen, alen - offs);
  290. dbg_chk_lpt_sz(c, 4, alen - offs);
  291. err = dbg_chk_lpt_sz(c, 3, alen);
  292. if (err)
  293. return err;
  294. return 0;
  295. no_space:
  296. ubifs_err("LPT out of space");
  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. }
  390. dbg_chk_lpt_sz(c, 2, c->leb_size - offs);
  391. err = realloc_lpt_leb(c, &lnum);
  392. if (err)
  393. goto no_space;
  394. offs = from = 0;
  395. ubifs_assert(lnum >= c->lpt_first &&
  396. lnum <= c->lpt_last);
  397. err = ubifs_leb_unmap(c, lnum);
  398. if (err)
  399. return err;
  400. /* Try to place lsave and ltab nicely */
  401. if (!done_lsave) {
  402. done_lsave = 1;
  403. ubifs_pack_lsave(c, buf + offs, c->lsave);
  404. offs += c->lsave_sz;
  405. dbg_chk_lpt_sz(c, 1, c->lsave_sz);
  406. continue;
  407. }
  408. if (!done_ltab) {
  409. done_ltab = 1;
  410. ubifs_pack_ltab(c, buf + offs, c->ltab_cmt);
  411. offs += c->ltab_sz;
  412. dbg_chk_lpt_sz(c, 1, c->ltab_sz);
  413. continue;
  414. }
  415. break;
  416. }
  417. if (cnode->level)
  418. ubifs_pack_nnode(c, buf + offs,
  419. (struct ubifs_nnode *)cnode);
  420. else
  421. ubifs_pack_pnode(c, buf + offs,
  422. (struct ubifs_pnode *)cnode);
  423. /*
  424. * The reason for the barriers is the same as in case of TNC.
  425. * See comment in 'write_index()'. 'dirty_cow_nnode()' and
  426. * 'dirty_cow_pnode()' are the functions for which this is
  427. * important.
  428. */
  429. clear_bit(DIRTY_CNODE, &cnode->flags);
  430. smp_mb__before_clear_bit();
  431. clear_bit(COW_ZNODE, &cnode->flags);
  432. smp_mb__after_clear_bit();
  433. offs += len;
  434. dbg_chk_lpt_sz(c, 1, len);
  435. cnode = cnode->cnext;
  436. } while (cnode && cnode != c->lpt_cnext);
  437. /* Make sure to place LPT's save table */
  438. if (!done_lsave) {
  439. if (offs + c->lsave_sz > c->leb_size) {
  440. wlen = offs - from;
  441. alen = ALIGN(wlen, c->min_io_size);
  442. memset(buf + offs, 0xff, alen - wlen);
  443. err = ubifs_leb_write(c, lnum, buf + from, from, alen,
  444. UBI_SHORTTERM);
  445. if (err)
  446. return err;
  447. dbg_chk_lpt_sz(c, 2, c->leb_size - offs);
  448. err = realloc_lpt_leb(c, &lnum);
  449. if (err)
  450. goto no_space;
  451. offs = from = 0;
  452. ubifs_assert(lnum >= c->lpt_first &&
  453. lnum <= c->lpt_last);
  454. err = ubifs_leb_unmap(c, lnum);
  455. if (err)
  456. return err;
  457. }
  458. done_lsave = 1;
  459. ubifs_pack_lsave(c, buf + offs, c->lsave);
  460. offs += c->lsave_sz;
  461. dbg_chk_lpt_sz(c, 1, c->lsave_sz);
  462. }
  463. /* Make sure to place LPT's own lprops table */
  464. if (!done_ltab) {
  465. if (offs + c->ltab_sz > c->leb_size) {
  466. wlen = offs - from;
  467. alen = ALIGN(wlen, c->min_io_size);
  468. memset(buf + offs, 0xff, alen - wlen);
  469. err = ubifs_leb_write(c, lnum, buf + from, from, alen,
  470. UBI_SHORTTERM);
  471. if (err)
  472. return err;
  473. dbg_chk_lpt_sz(c, 2, c->leb_size - offs);
  474. err = realloc_lpt_leb(c, &lnum);
  475. if (err)
  476. goto no_space;
  477. offs = from = 0;
  478. ubifs_assert(lnum >= c->lpt_first &&
  479. lnum <= c->lpt_last);
  480. err = ubifs_leb_unmap(c, lnum);
  481. if (err)
  482. return err;
  483. }
  484. done_ltab = 1;
  485. ubifs_pack_ltab(c, buf + offs, c->ltab_cmt);
  486. offs += c->ltab_sz;
  487. dbg_chk_lpt_sz(c, 1, c->ltab_sz);
  488. }
  489. /* Write remaining data in buffer */
  490. wlen = offs - from;
  491. alen = ALIGN(wlen, c->min_io_size);
  492. memset(buf + offs, 0xff, alen - wlen);
  493. err = ubifs_leb_write(c, lnum, buf + from, from, alen, UBI_SHORTTERM);
  494. if (err)
  495. return err;
  496. dbg_chk_lpt_sz(c, 4, alen - wlen);
  497. err = dbg_chk_lpt_sz(c, 3, ALIGN(offs, c->min_io_size));
  498. if (err)
  499. return err;
  500. c->nhead_lnum = lnum;
  501. c->nhead_offs = ALIGN(offs, c->min_io_size);
  502. dbg_lp("LPT root is at %d:%d", c->lpt_lnum, c->lpt_offs);
  503. dbg_lp("LPT head is at %d:%d", c->nhead_lnum, c->nhead_offs);
  504. dbg_lp("LPT ltab is at %d:%d", c->ltab_lnum, c->ltab_offs);
  505. if (c->big_lpt)
  506. dbg_lp("LPT lsave is at %d:%d", c->lsave_lnum, c->lsave_offs);
  507. return 0;
  508. no_space:
  509. ubifs_err("LPT out of space mismatch");
  510. dbg_err("LPT out of space mismatch at LEB %d:%d needing %d, done_ltab "
  511. "%d, done_lsave %d", lnum, offs, len, done_ltab, done_lsave);
  512. dbg_dump_lpt_info(c);
  513. dbg_dump_lpt_lebs(c);
  514. dump_stack();
  515. return err;
  516. }
  517. /**
  518. * next_pnode_to_dirty - find next pnode to dirty.
  519. * @c: UBIFS file-system description object
  520. * @pnode: pnode
  521. *
  522. * This function returns the next pnode to dirty or %NULL if there are no more
  523. * pnodes. Note that pnodes that have never been written (lnum == 0) are
  524. * skipped.
  525. */
  526. static struct ubifs_pnode *next_pnode_to_dirty(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. for (iip = pnode->iip + 1; iip < UBIFS_LPT_FANOUT; iip++) {
  534. if (nnode->nbranch[iip].lnum)
  535. return ubifs_get_pnode(c, nnode, iip);
  536. }
  537. /* Go up while can't go right */
  538. do {
  539. iip = nnode->iip + 1;
  540. nnode = nnode->parent;
  541. if (!nnode)
  542. return NULL;
  543. for (; iip < UBIFS_LPT_FANOUT; iip++) {
  544. if (nnode->nbranch[iip].lnum)
  545. break;
  546. }
  547. } while (iip >= UBIFS_LPT_FANOUT);
  548. /* Go right */
  549. nnode = ubifs_get_nnode(c, nnode, iip);
  550. if (IS_ERR(nnode))
  551. return (void *)nnode;
  552. /* Go down to level 1 */
  553. while (nnode->level > 1) {
  554. for (iip = 0; iip < UBIFS_LPT_FANOUT; iip++) {
  555. if (nnode->nbranch[iip].lnum)
  556. break;
  557. }
  558. if (iip >= UBIFS_LPT_FANOUT) {
  559. /*
  560. * Should not happen, but we need to keep going
  561. * if it does.
  562. */
  563. iip = 0;
  564. }
  565. nnode = ubifs_get_nnode(c, nnode, iip);
  566. if (IS_ERR(nnode))
  567. return (void *)nnode;
  568. }
  569. for (iip = 0; iip < UBIFS_LPT_FANOUT; iip++)
  570. if (nnode->nbranch[iip].lnum)
  571. break;
  572. if (iip >= UBIFS_LPT_FANOUT)
  573. /* Should not happen, but we need to keep going if it does */
  574. iip = 0;
  575. return ubifs_get_pnode(c, nnode, iip);
  576. }
  577. /**
  578. * pnode_lookup - lookup a pnode in the LPT.
  579. * @c: UBIFS file-system description object
  580. * @i: pnode number (0 to main_lebs - 1)
  581. *
  582. * This function returns a pointer to the pnode on success or a negative
  583. * error code on failure.
  584. */
  585. static struct ubifs_pnode *pnode_lookup(struct ubifs_info *c, int i)
  586. {
  587. int err, h, iip, shft;
  588. struct ubifs_nnode *nnode;
  589. if (!c->nroot) {
  590. err = ubifs_read_nnode(c, NULL, 0);
  591. if (err)
  592. return ERR_PTR(err);
  593. }
  594. i <<= UBIFS_LPT_FANOUT_SHIFT;
  595. nnode = c->nroot;
  596. shft = c->lpt_hght * UBIFS_LPT_FANOUT_SHIFT;
  597. for (h = 1; h < c->lpt_hght; h++) {
  598. iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
  599. shft -= UBIFS_LPT_FANOUT_SHIFT;
  600. nnode = ubifs_get_nnode(c, nnode, iip);
  601. if (IS_ERR(nnode))
  602. return ERR_PTR(PTR_ERR(nnode));
  603. }
  604. iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
  605. return ubifs_get_pnode(c, nnode, iip);
  606. }
  607. /**
  608. * add_pnode_dirt - add dirty space to LPT LEB properties.
  609. * @c: UBIFS file-system description object
  610. * @pnode: pnode for which to add dirt
  611. */
  612. static void add_pnode_dirt(struct ubifs_info *c, struct ubifs_pnode *pnode)
  613. {
  614. ubifs_add_lpt_dirt(c, pnode->parent->nbranch[pnode->iip].lnum,
  615. c->pnode_sz);
  616. }
  617. /**
  618. * do_make_pnode_dirty - mark a pnode dirty.
  619. * @c: UBIFS file-system description object
  620. * @pnode: pnode to mark dirty
  621. */
  622. static void do_make_pnode_dirty(struct ubifs_info *c, struct ubifs_pnode *pnode)
  623. {
  624. /* Assumes cnext list is empty i.e. not called during commit */
  625. if (!test_and_set_bit(DIRTY_CNODE, &pnode->flags)) {
  626. struct ubifs_nnode *nnode;
  627. c->dirty_pn_cnt += 1;
  628. add_pnode_dirt(c, pnode);
  629. /* Mark parent and ancestors dirty too */
  630. nnode = pnode->parent;
  631. while (nnode) {
  632. if (!test_and_set_bit(DIRTY_CNODE, &nnode->flags)) {
  633. c->dirty_nn_cnt += 1;
  634. ubifs_add_nnode_dirt(c, nnode);
  635. nnode = nnode->parent;
  636. } else
  637. break;
  638. }
  639. }
  640. }
  641. /**
  642. * make_tree_dirty - mark the entire LEB properties tree dirty.
  643. * @c: UBIFS file-system description object
  644. *
  645. * This function is used by the "small" LPT model to cause the entire LEB
  646. * properties tree to be written. The "small" LPT model does not use LPT
  647. * garbage collection because it is more efficient to write the entire tree
  648. * (because it is small).
  649. *
  650. * This function returns %0 on success and a negative error code on failure.
  651. */
  652. static int make_tree_dirty(struct ubifs_info *c)
  653. {
  654. struct ubifs_pnode *pnode;
  655. pnode = pnode_lookup(c, 0);
  656. while (pnode) {
  657. do_make_pnode_dirty(c, pnode);
  658. pnode = next_pnode_to_dirty(c, pnode);
  659. if (IS_ERR(pnode))
  660. return PTR_ERR(pnode);
  661. }
  662. return 0;
  663. }
  664. /**
  665. * need_write_all - determine if the LPT area is running out of free space.
  666. * @c: UBIFS file-system description object
  667. *
  668. * This function returns %1 if the LPT area is running out of free space and %0
  669. * if it is not.
  670. */
  671. static int need_write_all(struct ubifs_info *c)
  672. {
  673. long long free = 0;
  674. int i;
  675. for (i = 0; i < c->lpt_lebs; i++) {
  676. if (i + c->lpt_first == c->nhead_lnum)
  677. free += c->leb_size - c->nhead_offs;
  678. else if (c->ltab[i].free == c->leb_size)
  679. free += c->leb_size;
  680. else if (c->ltab[i].free + c->ltab[i].dirty == c->leb_size)
  681. free += c->leb_size;
  682. }
  683. /* Less than twice the size left */
  684. if (free <= c->lpt_sz * 2)
  685. return 1;
  686. return 0;
  687. }
  688. /**
  689. * lpt_tgc_start - start trivial garbage collection of LPT LEBs.
  690. * @c: UBIFS file-system description object
  691. *
  692. * LPT trivial garbage collection is where a LPT LEB contains only dirty and
  693. * free space and so may be reused as soon as the next commit is completed.
  694. * This function is called during start commit to mark LPT LEBs for trivial GC.
  695. */
  696. static void lpt_tgc_start(struct ubifs_info *c)
  697. {
  698. int i;
  699. for (i = 0; i < c->lpt_lebs; i++) {
  700. if (i + c->lpt_first == c->nhead_lnum)
  701. continue;
  702. if (c->ltab[i].dirty > 0 &&
  703. c->ltab[i].free + c->ltab[i].dirty == c->leb_size) {
  704. c->ltab[i].tgc = 1;
  705. c->ltab[i].free = c->leb_size;
  706. c->ltab[i].dirty = 0;
  707. dbg_lp("LEB %d", i + c->lpt_first);
  708. }
  709. }
  710. }
  711. /**
  712. * lpt_tgc_end - end trivial garbage collection of LPT LEBs.
  713. * @c: UBIFS file-system description object
  714. *
  715. * LPT trivial garbage collection is where a LPT LEB contains only dirty and
  716. * free space and so may be reused as soon as the next commit is completed.
  717. * This function is called after the commit is completed (master node has been
  718. * written) and un-maps LPT LEBs that were marked for trivial GC.
  719. */
  720. static int lpt_tgc_end(struct ubifs_info *c)
  721. {
  722. int i, err;
  723. for (i = 0; i < c->lpt_lebs; i++)
  724. if (c->ltab[i].tgc) {
  725. err = ubifs_leb_unmap(c, i + c->lpt_first);
  726. if (err)
  727. return err;
  728. c->ltab[i].tgc = 0;
  729. dbg_lp("LEB %d", i + c->lpt_first);
  730. }
  731. return 0;
  732. }
  733. /**
  734. * populate_lsave - fill the lsave array with important LEB numbers.
  735. * @c: the UBIFS file-system description object
  736. *
  737. * This function is only called for the "big" model. It records a small number
  738. * of LEB numbers of important LEBs. Important LEBs are ones that are (from
  739. * most important to least important): empty, freeable, freeable index, dirty
  740. * index, dirty or free. Upon mount, we read this list of LEB numbers and bring
  741. * their pnodes into memory. That will stop us from having to scan the LPT
  742. * straight away. For the "small" model we assume that scanning the LPT is no
  743. * big deal.
  744. */
  745. static void populate_lsave(struct ubifs_info *c)
  746. {
  747. struct ubifs_lprops *lprops;
  748. struct ubifs_lpt_heap *heap;
  749. int i, cnt = 0;
  750. ubifs_assert(c->big_lpt);
  751. if (!(c->lpt_drty_flgs & LSAVE_DIRTY)) {
  752. c->lpt_drty_flgs |= LSAVE_DIRTY;
  753. ubifs_add_lpt_dirt(c, c->lsave_lnum, c->lsave_sz);
  754. }
  755. 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 = ubi_read(c->ubi, lnum, buf, 0, c->leb_size);
  1072. if (err) {
  1073. ubifs_err("cannot read LEB %d, error %d", lnum, err);
  1074. return err;
  1075. }
  1076. while (1) {
  1077. if (!is_a_node(c, buf, len)) {
  1078. int pad_len;
  1079. pad_len = get_pad_len(c, buf, len);
  1080. if (pad_len) {
  1081. buf += pad_len;
  1082. len -= pad_len;
  1083. continue;
  1084. }
  1085. return 0;
  1086. }
  1087. node_type = get_lpt_node_type(c, buf, &node_num);
  1088. node_len = get_lpt_node_len(c, node_type);
  1089. offs = c->leb_size - len;
  1090. ubifs_assert(node_len != 0);
  1091. mutex_lock(&c->lp_mutex);
  1092. err = make_node_dirty(c, node_type, node_num, lnum, offs);
  1093. mutex_unlock(&c->lp_mutex);
  1094. if (err)
  1095. return err;
  1096. buf += node_len;
  1097. len -= node_len;
  1098. }
  1099. return 0;
  1100. }
  1101. /**
  1102. * lpt_gc - LPT garbage collection.
  1103. * @c: UBIFS file-system description object
  1104. *
  1105. * Select a LPT LEB for LPT garbage collection and call 'lpt_gc_lnum()'.
  1106. * Returns %0 on success and a negative error code on failure.
  1107. */
  1108. static int lpt_gc(struct ubifs_info *c)
  1109. {
  1110. int i, lnum = -1, dirty = 0;
  1111. mutex_lock(&c->lp_mutex);
  1112. for (i = 0; i < c->lpt_lebs; i++) {
  1113. ubifs_assert(!c->ltab[i].tgc);
  1114. if (i + c->lpt_first == c->nhead_lnum ||
  1115. c->ltab[i].free + c->ltab[i].dirty == c->leb_size)
  1116. continue;
  1117. if (c->ltab[i].dirty > dirty) {
  1118. dirty = c->ltab[i].dirty;
  1119. lnum = i + c->lpt_first;
  1120. }
  1121. }
  1122. mutex_unlock(&c->lp_mutex);
  1123. if (lnum == -1)
  1124. return -ENOSPC;
  1125. return lpt_gc_lnum(c, lnum);
  1126. }
  1127. /**
  1128. * ubifs_lpt_start_commit - UBIFS commit starts.
  1129. * @c: the UBIFS file-system description object
  1130. *
  1131. * This function has to be called when UBIFS starts the commit operation.
  1132. * This function "freezes" all currently dirty LEB properties and does not
  1133. * change them anymore. Further changes are saved and tracked separately
  1134. * because they are not part of this commit. This function returns zero in case
  1135. * of success and a negative error code in case of failure.
  1136. */
  1137. int ubifs_lpt_start_commit(struct ubifs_info *c)
  1138. {
  1139. int err, cnt;
  1140. dbg_lp("");
  1141. mutex_lock(&c->lp_mutex);
  1142. err = dbg_chk_lpt_free_spc(c);
  1143. if (err)
  1144. goto out;
  1145. err = dbg_check_ltab(c);
  1146. if (err)
  1147. goto out;
  1148. if (c->check_lpt_free) {
  1149. /*
  1150. * We ensure there is enough free space in
  1151. * ubifs_lpt_post_commit() by marking nodes dirty. That
  1152. * information is lost when we unmount, so we also need
  1153. * to check free space once after mounting also.
  1154. */
  1155. c->check_lpt_free = 0;
  1156. while (need_write_all(c)) {
  1157. mutex_unlock(&c->lp_mutex);
  1158. err = lpt_gc(c);
  1159. if (err)
  1160. return err;
  1161. mutex_lock(&c->lp_mutex);
  1162. }
  1163. }
  1164. lpt_tgc_start(c);
  1165. if (!c->dirty_pn_cnt) {
  1166. dbg_cmt("no cnodes to commit");
  1167. err = 0;
  1168. goto out;
  1169. }
  1170. if (!c->big_lpt && need_write_all(c)) {
  1171. /* If needed, write everything */
  1172. err = make_tree_dirty(c);
  1173. if (err)
  1174. goto out;
  1175. lpt_tgc_start(c);
  1176. }
  1177. if (c->big_lpt)
  1178. populate_lsave(c);
  1179. cnt = get_cnodes_to_commit(c);
  1180. ubifs_assert(cnt != 0);
  1181. err = layout_cnodes(c);
  1182. if (err)
  1183. goto out;
  1184. /* Copy the LPT's own lprops for end commit to write */
  1185. memcpy(c->ltab_cmt, c->ltab,
  1186. sizeof(struct ubifs_lpt_lprops) * c->lpt_lebs);
  1187. c->lpt_drty_flgs &= ~(LTAB_DIRTY | LSAVE_DIRTY);
  1188. out:
  1189. mutex_unlock(&c->lp_mutex);
  1190. return err;
  1191. }
  1192. /**
  1193. * free_obsolete_cnodes - free obsolete cnodes for commit end.
  1194. * @c: UBIFS file-system description object
  1195. */
  1196. static void free_obsolete_cnodes(struct ubifs_info *c)
  1197. {
  1198. struct ubifs_cnode *cnode, *cnext;
  1199. cnext = c->lpt_cnext;
  1200. if (!cnext)
  1201. return;
  1202. do {
  1203. cnode = cnext;
  1204. cnext = cnode->cnext;
  1205. if (test_bit(OBSOLETE_CNODE, &cnode->flags))
  1206. kfree(cnode);
  1207. else
  1208. cnode->cnext = NULL;
  1209. } while (cnext != c->lpt_cnext);
  1210. c->lpt_cnext = NULL;
  1211. }
  1212. /**
  1213. * ubifs_lpt_end_commit - finish the commit operation.
  1214. * @c: the UBIFS file-system description object
  1215. *
  1216. * This function has to be called when the commit operation finishes. It
  1217. * flushes the changes which were "frozen" by 'ubifs_lprops_start_commit()' to
  1218. * the media. Returns zero in case of success and a negative error code in case
  1219. * of failure.
  1220. */
  1221. int ubifs_lpt_end_commit(struct ubifs_info *c)
  1222. {
  1223. int err;
  1224. dbg_lp("");
  1225. if (!c->lpt_cnext)
  1226. return 0;
  1227. err = write_cnodes(c);
  1228. if (err)
  1229. return err;
  1230. mutex_lock(&c->lp_mutex);
  1231. free_obsolete_cnodes(c);
  1232. mutex_unlock(&c->lp_mutex);
  1233. return 0;
  1234. }
  1235. /**
  1236. * ubifs_lpt_post_commit - post commit LPT trivial GC and LPT GC.
  1237. * @c: UBIFS file-system description object
  1238. *
  1239. * LPT trivial GC is completed after a commit. Also LPT GC is done after a
  1240. * commit for the "big" LPT model.
  1241. */
  1242. int ubifs_lpt_post_commit(struct ubifs_info *c)
  1243. {
  1244. int err;
  1245. mutex_lock(&c->lp_mutex);
  1246. err = lpt_tgc_end(c);
  1247. if (err)
  1248. goto out;
  1249. if (c->big_lpt)
  1250. while (need_write_all(c)) {
  1251. mutex_unlock(&c->lp_mutex);
  1252. err = lpt_gc(c);
  1253. if (err)
  1254. return err;
  1255. mutex_lock(&c->lp_mutex);
  1256. }
  1257. out:
  1258. mutex_unlock(&c->lp_mutex);
  1259. return err;
  1260. }
  1261. /**
  1262. * first_nnode - find the first nnode in memory.
  1263. * @c: UBIFS file-system description object
  1264. * @hght: height of tree where nnode found is returned here
  1265. *
  1266. * This function returns a pointer to the nnode found or %NULL if no nnode is
  1267. * found. This function is a helper to 'ubifs_lpt_free()'.
  1268. */
  1269. static struct ubifs_nnode *first_nnode(struct ubifs_info *c, int *hght)
  1270. {
  1271. struct ubifs_nnode *nnode;
  1272. int h, i, found;
  1273. nnode = c->nroot;
  1274. *hght = 0;
  1275. if (!nnode)
  1276. return NULL;
  1277. for (h = 1; h < c->lpt_hght; h++) {
  1278. found = 0;
  1279. for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
  1280. if (nnode->nbranch[i].nnode) {
  1281. found = 1;
  1282. nnode = nnode->nbranch[i].nnode;
  1283. *hght = h;
  1284. break;
  1285. }
  1286. }
  1287. if (!found)
  1288. break;
  1289. }
  1290. return nnode;
  1291. }
  1292. /**
  1293. * next_nnode - find the next nnode in memory.
  1294. * @c: UBIFS file-system description object
  1295. * @nnode: nnode from which to start.
  1296. * @hght: height of tree where nnode is, is passed and returned here
  1297. *
  1298. * This function returns a pointer to the nnode found or %NULL if no nnode is
  1299. * found. This function is a helper to 'ubifs_lpt_free()'.
  1300. */
  1301. static struct ubifs_nnode *next_nnode(struct ubifs_info *c,
  1302. struct ubifs_nnode *nnode, int *hght)
  1303. {
  1304. struct ubifs_nnode *parent;
  1305. int iip, h, i, found;
  1306. parent = nnode->parent;
  1307. if (!parent)
  1308. return NULL;
  1309. if (nnode->iip == UBIFS_LPT_FANOUT - 1) {
  1310. *hght -= 1;
  1311. return parent;
  1312. }
  1313. for (iip = nnode->iip + 1; iip < UBIFS_LPT_FANOUT; iip++) {
  1314. nnode = parent->nbranch[iip].nnode;
  1315. if (nnode)
  1316. break;
  1317. }
  1318. if (!nnode) {
  1319. *hght -= 1;
  1320. return parent;
  1321. }
  1322. for (h = *hght + 1; h < c->lpt_hght; h++) {
  1323. found = 0;
  1324. for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
  1325. if (nnode->nbranch[i].nnode) {
  1326. found = 1;
  1327. nnode = nnode->nbranch[i].nnode;
  1328. *hght = h;
  1329. break;
  1330. }
  1331. }
  1332. if (!found)
  1333. break;
  1334. }
  1335. return nnode;
  1336. }
  1337. /**
  1338. * ubifs_lpt_free - free resources owned by the LPT.
  1339. * @c: UBIFS file-system description object
  1340. * @wr_only: free only resources used for writing
  1341. */
  1342. void ubifs_lpt_free(struct ubifs_info *c, int wr_only)
  1343. {
  1344. struct ubifs_nnode *nnode;
  1345. int i, hght;
  1346. /* Free write-only things first */
  1347. free_obsolete_cnodes(c); /* Leftover from a failed commit */
  1348. vfree(c->ltab_cmt);
  1349. c->ltab_cmt = NULL;
  1350. vfree(c->lpt_buf);
  1351. c->lpt_buf = NULL;
  1352. kfree(c->lsave);
  1353. c->lsave = NULL;
  1354. if (wr_only)
  1355. return;
  1356. /* Now free the rest */
  1357. nnode = first_nnode(c, &hght);
  1358. while (nnode) {
  1359. for (i = 0; i < UBIFS_LPT_FANOUT; i++)
  1360. kfree(nnode->nbranch[i].nnode);
  1361. nnode = next_nnode(c, nnode, &hght);
  1362. }
  1363. for (i = 0; i < LPROPS_HEAP_CNT; i++)
  1364. kfree(c->lpt_heap[i].arr);
  1365. kfree(c->dirty_idx.arr);
  1366. kfree(c->nroot);
  1367. vfree(c->ltab);
  1368. kfree(c->lpt_nod_buf);
  1369. }
  1370. #ifdef CONFIG_UBIFS_FS_DEBUG
  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. * @offs: offset where node was written
  1493. *
  1494. * This function returns %0 on success and a negative error code on failure.
  1495. */
  1496. static int dbg_check_ltab_lnum(struct ubifs_info *c, int lnum)
  1497. {
  1498. int err, len = c->leb_size, dirty = 0, node_type, node_num, node_len;
  1499. int ret;
  1500. void *buf = c->dbg->buf;
  1501. if (!(ubifs_chk_flags & UBIFS_CHK_LPROPS))
  1502. return 0;
  1503. dbg_lp("LEB %d", lnum);
  1504. err = ubi_read(c->ubi, lnum, buf, 0, c->leb_size);
  1505. if (err) {
  1506. dbg_msg("ubi_read failed, LEB %d, error %d", lnum, err);
  1507. return err;
  1508. }
  1509. while (1) {
  1510. if (!is_a_node(c, buf, len)) {
  1511. int i, pad_len;
  1512. pad_len = get_pad_len(c, buf, len);
  1513. if (pad_len) {
  1514. buf += pad_len;
  1515. len -= pad_len;
  1516. dirty += pad_len;
  1517. continue;
  1518. }
  1519. if (!dbg_is_all_ff(buf, len)) {
  1520. dbg_msg("invalid empty space in LEB %d at %d",
  1521. lnum, c->leb_size - len);
  1522. err = -EINVAL;
  1523. }
  1524. i = lnum - c->lpt_first;
  1525. if (len != c->ltab[i].free) {
  1526. dbg_msg("invalid free space in LEB %d "
  1527. "(free %d, expected %d)",
  1528. lnum, len, c->ltab[i].free);
  1529. err = -EINVAL;
  1530. }
  1531. if (dirty != c->ltab[i].dirty) {
  1532. dbg_msg("invalid dirty space in LEB %d "
  1533. "(dirty %d, expected %d)",
  1534. lnum, dirty, c->ltab[i].dirty);
  1535. err = -EINVAL;
  1536. }
  1537. return err;
  1538. }
  1539. node_type = get_lpt_node_type(c, buf, &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. buf += node_len;
  1545. len -= node_len;
  1546. }
  1547. }
  1548. /**
  1549. * dbg_check_ltab - check the free and dirty space in the ltab.
  1550. * @c: the UBIFS file-system description object
  1551. *
  1552. * This function returns %0 on success and a negative error code on failure.
  1553. */
  1554. int dbg_check_ltab(struct ubifs_info *c)
  1555. {
  1556. int lnum, err, i, cnt;
  1557. if (!(ubifs_chk_flags & UBIFS_CHK_LPROPS))
  1558. return 0;
  1559. /* Bring the entire tree into memory */
  1560. cnt = DIV_ROUND_UP(c->main_lebs, UBIFS_LPT_FANOUT);
  1561. for (i = 0; i < cnt; i++) {
  1562. struct ubifs_pnode *pnode;
  1563. pnode = pnode_lookup(c, i);
  1564. if (IS_ERR(pnode))
  1565. return PTR_ERR(pnode);
  1566. cond_resched();
  1567. }
  1568. /* Check nodes */
  1569. err = dbg_check_lpt_nodes(c, (struct ubifs_cnode *)c->nroot, 0, 0);
  1570. if (err)
  1571. return err;
  1572. /* Check each LEB */
  1573. for (lnum = c->lpt_first; lnum <= c->lpt_last; lnum++) {
  1574. err = dbg_check_ltab_lnum(c, lnum);
  1575. if (err) {
  1576. dbg_err("failed at LEB %d", lnum);
  1577. return err;
  1578. }
  1579. }
  1580. dbg_lp("succeeded");
  1581. return 0;
  1582. }
  1583. /**
  1584. * dbg_chk_lpt_free_spc - check LPT free space is enough to write entire LPT.
  1585. * @c: the UBIFS file-system description object
  1586. *
  1587. * This function returns %0 on success and a negative error code on failure.
  1588. */
  1589. int dbg_chk_lpt_free_spc(struct ubifs_info *c)
  1590. {
  1591. long long free = 0;
  1592. int i;
  1593. if (!(ubifs_chk_flags & UBIFS_CHK_LPROPS))
  1594. return 0;
  1595. for (i = 0; i < c->lpt_lebs; i++) {
  1596. if (c->ltab[i].tgc || c->ltab[i].cmt)
  1597. continue;
  1598. if (i + c->lpt_first == c->nhead_lnum)
  1599. free += c->leb_size - c->nhead_offs;
  1600. else if (c->ltab[i].free == c->leb_size)
  1601. free += c->leb_size;
  1602. }
  1603. if (free < c->lpt_sz) {
  1604. dbg_err("LPT space error: free %lld lpt_sz %lld",
  1605. free, c->lpt_sz);
  1606. dbg_dump_lpt_info(c);
  1607. dbg_dump_lpt_lebs(c);
  1608. dump_stack();
  1609. return -EINVAL;
  1610. }
  1611. return 0;
  1612. }
  1613. /**
  1614. * dbg_chk_lpt_sz - check LPT does not write more than LPT size.
  1615. * @c: the UBIFS file-system description object
  1616. * @action: what to do
  1617. * @len: length written
  1618. *
  1619. * This function returns %0 on success and a negative error code on failure.
  1620. * The @action argument may be one of:
  1621. * o %0 - LPT debugging checking starts, initialize debugging variables;
  1622. * o %1 - wrote an LPT node, increase LPT size by @len bytes;
  1623. * o %2 - switched to a different LEB and wasted @len bytes;
  1624. * o %3 - check that we've written the right number of bytes.
  1625. * o %4 - wasted @len bytes;
  1626. */
  1627. int dbg_chk_lpt_sz(struct ubifs_info *c, int action, int len)
  1628. {
  1629. struct ubifs_debug_info *d = c->dbg;
  1630. long long chk_lpt_sz, lpt_sz;
  1631. int err = 0;
  1632. if (!(ubifs_chk_flags & UBIFS_CHK_LPROPS))
  1633. return 0;
  1634. switch (action) {
  1635. case 0:
  1636. d->chk_lpt_sz = 0;
  1637. d->chk_lpt_sz2 = 0;
  1638. d->chk_lpt_lebs = 0;
  1639. d->chk_lpt_wastage = 0;
  1640. if (c->dirty_pn_cnt > c->pnode_cnt) {
  1641. dbg_err("dirty pnodes %d exceed max %d",
  1642. c->dirty_pn_cnt, c->pnode_cnt);
  1643. err = -EINVAL;
  1644. }
  1645. if (c->dirty_nn_cnt > c->nnode_cnt) {
  1646. dbg_err("dirty nnodes %d exceed max %d",
  1647. c->dirty_nn_cnt, c->nnode_cnt);
  1648. err = -EINVAL;
  1649. }
  1650. return err;
  1651. case 1:
  1652. d->chk_lpt_sz += len;
  1653. return 0;
  1654. case 2:
  1655. d->chk_lpt_sz += len;
  1656. d->chk_lpt_wastage += len;
  1657. d->chk_lpt_lebs += 1;
  1658. return 0;
  1659. case 3:
  1660. chk_lpt_sz = c->leb_size;
  1661. chk_lpt_sz *= d->chk_lpt_lebs;
  1662. chk_lpt_sz += len - c->nhead_offs;
  1663. if (d->chk_lpt_sz != chk_lpt_sz) {
  1664. dbg_err("LPT wrote %lld but space used was %lld",
  1665. d->chk_lpt_sz, chk_lpt_sz);
  1666. err = -EINVAL;
  1667. }
  1668. if (d->chk_lpt_sz > c->lpt_sz) {
  1669. dbg_err("LPT wrote %lld but lpt_sz is %lld",
  1670. d->chk_lpt_sz, c->lpt_sz);
  1671. err = -EINVAL;
  1672. }
  1673. if (d->chk_lpt_sz2 && d->chk_lpt_sz != d->chk_lpt_sz2) {
  1674. dbg_err("LPT layout size %lld but wrote %lld",
  1675. d->chk_lpt_sz, d->chk_lpt_sz2);
  1676. err = -EINVAL;
  1677. }
  1678. if (d->chk_lpt_sz2 && d->new_nhead_offs != len) {
  1679. dbg_err("LPT new nhead offs: expected %d was %d",
  1680. d->new_nhead_offs, len);
  1681. err = -EINVAL;
  1682. }
  1683. lpt_sz = (long long)c->pnode_cnt * c->pnode_sz;
  1684. lpt_sz += (long long)c->nnode_cnt * c->nnode_sz;
  1685. lpt_sz += c->ltab_sz;
  1686. if (c->big_lpt)
  1687. lpt_sz += c->lsave_sz;
  1688. if (d->chk_lpt_sz - d->chk_lpt_wastage > lpt_sz) {
  1689. dbg_err("LPT chk_lpt_sz %lld + waste %lld exceeds %lld",
  1690. d->chk_lpt_sz, d->chk_lpt_wastage, lpt_sz);
  1691. err = -EINVAL;
  1692. }
  1693. if (err) {
  1694. dbg_dump_lpt_info(c);
  1695. dbg_dump_lpt_lebs(c);
  1696. dump_stack();
  1697. }
  1698. d->chk_lpt_sz2 = d->chk_lpt_sz;
  1699. d->chk_lpt_sz = 0;
  1700. d->chk_lpt_wastage = 0;
  1701. d->chk_lpt_lebs = 0;
  1702. d->new_nhead_offs = len;
  1703. return err;
  1704. case 4:
  1705. d->chk_lpt_sz += len;
  1706. d->chk_lpt_wastage += len;
  1707. return 0;
  1708. default:
  1709. return -EINVAL;
  1710. }
  1711. }
  1712. /**
  1713. * dbg_dump_lpt_leb - dump an LPT LEB.
  1714. * @c: UBIFS file-system description object
  1715. * @lnum: LEB number to dump
  1716. *
  1717. * This function dumps an LEB from LPT area. Nodes in this area are very
  1718. * different to nodes in the main area (e.g., they do not have common headers,
  1719. * they do not have 8-byte alignments, etc), so we have a separate function to
  1720. * dump LPT area LEBs. Note, LPT has to be locked by the caller.
  1721. */
  1722. static void dump_lpt_leb(const struct ubifs_info *c, int lnum)
  1723. {
  1724. int err, len = c->leb_size, node_type, node_num, node_len, offs;
  1725. void *buf = c->dbg->buf;
  1726. printk(KERN_DEBUG "(pid %d) start dumping LEB %d\n",
  1727. current->pid, lnum);
  1728. err = ubi_read(c->ubi, lnum, buf, 0, c->leb_size);
  1729. if (err) {
  1730. ubifs_err("cannot read LEB %d, error %d", lnum, err);
  1731. return;
  1732. }
  1733. while (1) {
  1734. offs = c->leb_size - len;
  1735. if (!is_a_node(c, buf, len)) {
  1736. int pad_len;
  1737. pad_len = get_pad_len(c, buf, len);
  1738. if (pad_len) {
  1739. printk(KERN_DEBUG "LEB %d:%d, pad %d bytes\n",
  1740. lnum, offs, pad_len);
  1741. buf += pad_len;
  1742. len -= pad_len;
  1743. continue;
  1744. }
  1745. if (len)
  1746. printk(KERN_DEBUG "LEB %d:%d, free %d bytes\n",
  1747. lnum, offs, len);
  1748. break;
  1749. }
  1750. node_type = get_lpt_node_type(c, buf, &node_num);
  1751. switch (node_type) {
  1752. case UBIFS_LPT_PNODE:
  1753. {
  1754. node_len = c->pnode_sz;
  1755. if (c->big_lpt)
  1756. printk(KERN_DEBUG "LEB %d:%d, pnode num %d\n",
  1757. lnum, offs, node_num);
  1758. else
  1759. printk(KERN_DEBUG "LEB %d:%d, pnode\n",
  1760. lnum, offs);
  1761. break;
  1762. }
  1763. case UBIFS_LPT_NNODE:
  1764. {
  1765. int i;
  1766. struct ubifs_nnode nnode;
  1767. node_len = c->nnode_sz;
  1768. if (c->big_lpt)
  1769. printk(KERN_DEBUG "LEB %d:%d, nnode num %d, ",
  1770. lnum, offs, node_num);
  1771. else
  1772. printk(KERN_DEBUG "LEB %d:%d, nnode, ",
  1773. lnum, offs);
  1774. err = ubifs_unpack_nnode(c, buf, &nnode);
  1775. for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
  1776. printk(KERN_CONT "%d:%d", nnode.nbranch[i].lnum,
  1777. nnode.nbranch[i].offs);
  1778. if (i != UBIFS_LPT_FANOUT - 1)
  1779. printk(KERN_CONT ", ");
  1780. }
  1781. printk(KERN_CONT "\n");
  1782. break;
  1783. }
  1784. case UBIFS_LPT_LTAB:
  1785. node_len = c->ltab_sz;
  1786. printk(KERN_DEBUG "LEB %d:%d, ltab\n",
  1787. lnum, offs);
  1788. break;
  1789. case UBIFS_LPT_LSAVE:
  1790. node_len = c->lsave_sz;
  1791. printk(KERN_DEBUG "LEB %d:%d, lsave len\n", lnum, offs);
  1792. break;
  1793. default:
  1794. ubifs_err("LPT node type %d not recognized", node_type);
  1795. return;
  1796. }
  1797. buf += node_len;
  1798. len -= node_len;
  1799. }
  1800. printk(KERN_DEBUG "(pid %d) finish dumping LEB %d\n",
  1801. current->pid, lnum);
  1802. }
  1803. /**
  1804. * dbg_dump_lpt_lebs - dump LPT lebs.
  1805. * @c: UBIFS file-system description object
  1806. *
  1807. * This function dumps all LPT LEBs. The caller has to make sure the LPT is
  1808. * locked.
  1809. */
  1810. void dbg_dump_lpt_lebs(const struct ubifs_info *c)
  1811. {
  1812. int i;
  1813. printk(KERN_DEBUG "(pid %d) start dumping all LPT LEBs\n",
  1814. current->pid);
  1815. for (i = 0; i < c->lpt_lebs; i++)
  1816. dump_lpt_leb(c, i + c->lpt_first);
  1817. printk(KERN_DEBUG "(pid %d) finish dumping all LPT LEBs\n",
  1818. current->pid);
  1819. }
  1820. #endif /* CONFIG_UBIFS_FS_DEBUG */