lpt_commit.c 51 KB

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