lpt_commit.c 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042
  1. /*
  2. * This file is part of UBIFS.
  3. *
  4. * Copyright (C) 2006-2008 Nokia Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License version 2 as published by
  8. * the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with
  16. * this program; if not, write to the Free Software Foundation, Inc., 51
  17. * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  18. *
  19. * Authors: Adrian Hunter
  20. * Artem Bityutskiy (Битюцкий Артём)
  21. */
  22. /*
  23. * This file implements commit-related functionality of the LEB properties
  24. * subsystem.
  25. */
  26. #include <linux/crc16.h>
  27. #include <linux/slab.h>
  28. #include <linux/random.h>
  29. #include "ubifs.h"
  30. static int dbg_populate_lsave(struct ubifs_info *c);
  31. /**
  32. * first_dirty_cnode - find first dirty cnode.
  33. * @c: UBIFS file-system description object
  34. * @nnode: nnode at which to start
  35. *
  36. * This function returns the first dirty cnode or %NULL if there is not one.
  37. */
  38. static struct ubifs_cnode *first_dirty_cnode(struct ubifs_nnode *nnode)
  39. {
  40. ubifs_assert(nnode);
  41. while (1) {
  42. int i, cont = 0;
  43. for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
  44. struct ubifs_cnode *cnode;
  45. cnode = nnode->nbranch[i].cnode;
  46. if (cnode &&
  47. test_bit(DIRTY_CNODE, &cnode->flags)) {
  48. if (cnode->level == 0)
  49. return cnode;
  50. nnode = (struct ubifs_nnode *)cnode;
  51. cont = 1;
  52. break;
  53. }
  54. }
  55. if (!cont)
  56. return (struct ubifs_cnode *)nnode;
  57. }
  58. }
  59. /**
  60. * next_dirty_cnode - find next dirty cnode.
  61. * @cnode: cnode from which to begin searching
  62. *
  63. * This function returns the next dirty cnode or %NULL if there is not one.
  64. */
  65. static struct ubifs_cnode *next_dirty_cnode(struct ubifs_cnode *cnode)
  66. {
  67. struct ubifs_nnode *nnode;
  68. int i;
  69. ubifs_assert(cnode);
  70. nnode = cnode->parent;
  71. if (!nnode)
  72. return NULL;
  73. for (i = cnode->iip + 1; i < UBIFS_LPT_FANOUT; i++) {
  74. cnode = nnode->nbranch[i].cnode;
  75. if (cnode && test_bit(DIRTY_CNODE, &cnode->flags)) {
  76. if (cnode->level == 0)
  77. return cnode; /* cnode is a pnode */
  78. /* cnode is a nnode */
  79. return first_dirty_cnode((struct ubifs_nnode *)cnode);
  80. }
  81. }
  82. return (struct ubifs_cnode *)nnode;
  83. }
  84. /**
  85. * get_cnodes_to_commit - create list of dirty cnodes to commit.
  86. * @c: UBIFS file-system description object
  87. *
  88. * This function returns the number of cnodes to commit.
  89. */
  90. static int get_cnodes_to_commit(struct ubifs_info *c)
  91. {
  92. struct ubifs_cnode *cnode, *cnext;
  93. int cnt = 0;
  94. if (!c->nroot)
  95. return 0;
  96. if (!test_bit(DIRTY_CNODE, &c->nroot->flags))
  97. return 0;
  98. c->lpt_cnext = first_dirty_cnode(c->nroot);
  99. cnode = c->lpt_cnext;
  100. if (!cnode)
  101. return 0;
  102. cnt += 1;
  103. while (1) {
  104. ubifs_assert(!test_bit(COW_CNODE, &cnode->flags));
  105. __set_bit(COW_CNODE, &cnode->flags);
  106. cnext = next_dirty_cnode(cnode);
  107. if (!cnext) {
  108. cnode->cnext = c->lpt_cnext;
  109. break;
  110. }
  111. cnode->cnext = cnext;
  112. cnode = cnext;
  113. cnt += 1;
  114. }
  115. dbg_cmt("committing %d cnodes", cnt);
  116. dbg_lp("committing %d cnodes", cnt);
  117. ubifs_assert(cnt == c->dirty_nn_cnt + c->dirty_pn_cnt);
  118. return cnt;
  119. }
  120. /**
  121. * upd_ltab - update LPT LEB properties.
  122. * @c: UBIFS file-system description object
  123. * @lnum: LEB number
  124. * @free: amount of free space
  125. * @dirty: amount of dirty space to add
  126. */
  127. static void upd_ltab(struct ubifs_info *c, int lnum, int free, int dirty)
  128. {
  129. dbg_lp("LEB %d free %d dirty %d to %d +%d",
  130. lnum, c->ltab[lnum - c->lpt_first].free,
  131. c->ltab[lnum - c->lpt_first].dirty, free, dirty);
  132. ubifs_assert(lnum >= c->lpt_first && lnum <= c->lpt_last);
  133. c->ltab[lnum - c->lpt_first].free = free;
  134. c->ltab[lnum - c->lpt_first].dirty += dirty;
  135. }
  136. /**
  137. * alloc_lpt_leb - allocate an LPT LEB that is empty.
  138. * @c: UBIFS file-system description object
  139. * @lnum: LEB number is passed and returned here
  140. *
  141. * This function finds the next empty LEB in the ltab starting from @lnum. If a
  142. * an empty LEB is found it is returned in @lnum and the function returns %0.
  143. * Otherwise the function returns -ENOSPC. Note however, that LPT is designed
  144. * never to run out of space.
  145. */
  146. static int alloc_lpt_leb(struct ubifs_info *c, int *lnum)
  147. {
  148. int i, n;
  149. n = *lnum - c->lpt_first + 1;
  150. for (i = n; i < c->lpt_lebs; i++) {
  151. if (c->ltab[i].tgc || c->ltab[i].cmt)
  152. continue;
  153. if (c->ltab[i].free == c->leb_size) {
  154. c->ltab[i].cmt = 1;
  155. *lnum = i + c->lpt_first;
  156. return 0;
  157. }
  158. }
  159. for (i = 0; i < n; i++) {
  160. if (c->ltab[i].tgc || c->ltab[i].cmt)
  161. continue;
  162. if (c->ltab[i].free == c->leb_size) {
  163. c->ltab[i].cmt = 1;
  164. *lnum = i + c->lpt_first;
  165. return 0;
  166. }
  167. }
  168. return -ENOSPC;
  169. }
  170. /**
  171. * layout_cnodes - layout cnodes for commit.
  172. * @c: UBIFS file-system description object
  173. *
  174. * This function returns %0 on success and a negative error code on failure.
  175. */
  176. static int layout_cnodes(struct ubifs_info *c)
  177. {
  178. int lnum, offs, len, alen, done_lsave, done_ltab, err;
  179. struct ubifs_cnode *cnode;
  180. err = dbg_chk_lpt_sz(c, 0, 0);
  181. if (err)
  182. return err;
  183. cnode = c->lpt_cnext;
  184. if (!cnode)
  185. return 0;
  186. lnum = c->nhead_lnum;
  187. offs = c->nhead_offs;
  188. /* Try to place lsave and ltab nicely */
  189. done_lsave = !c->big_lpt;
  190. done_ltab = 0;
  191. if (!done_lsave && offs + c->lsave_sz <= c->leb_size) {
  192. done_lsave = 1;
  193. c->lsave_lnum = lnum;
  194. c->lsave_offs = offs;
  195. offs += c->lsave_sz;
  196. dbg_chk_lpt_sz(c, 1, c->lsave_sz);
  197. }
  198. if (offs + c->ltab_sz <= c->leb_size) {
  199. done_ltab = 1;
  200. c->ltab_lnum = lnum;
  201. c->ltab_offs = offs;
  202. offs += c->ltab_sz;
  203. dbg_chk_lpt_sz(c, 1, c->ltab_sz);
  204. }
  205. do {
  206. if (cnode->level) {
  207. len = c->nnode_sz;
  208. c->dirty_nn_cnt -= 1;
  209. } else {
  210. len = c->pnode_sz;
  211. c->dirty_pn_cnt -= 1;
  212. }
  213. while (offs + len > c->leb_size) {
  214. alen = ALIGN(offs, c->min_io_size);
  215. upd_ltab(c, lnum, c->leb_size - alen, alen - offs);
  216. dbg_chk_lpt_sz(c, 2, c->leb_size - offs);
  217. err = alloc_lpt_leb(c, &lnum);
  218. if (err)
  219. goto no_space;
  220. offs = 0;
  221. ubifs_assert(lnum >= c->lpt_first &&
  222. lnum <= c->lpt_last);
  223. /* Try to place lsave and ltab nicely */
  224. if (!done_lsave) {
  225. done_lsave = 1;
  226. c->lsave_lnum = lnum;
  227. c->lsave_offs = offs;
  228. offs += c->lsave_sz;
  229. dbg_chk_lpt_sz(c, 1, c->lsave_sz);
  230. continue;
  231. }
  232. if (!done_ltab) {
  233. done_ltab = 1;
  234. c->ltab_lnum = lnum;
  235. c->ltab_offs = offs;
  236. offs += c->ltab_sz;
  237. dbg_chk_lpt_sz(c, 1, c->ltab_sz);
  238. continue;
  239. }
  240. break;
  241. }
  242. if (cnode->parent) {
  243. cnode->parent->nbranch[cnode->iip].lnum = lnum;
  244. cnode->parent->nbranch[cnode->iip].offs = offs;
  245. } else {
  246. c->lpt_lnum = lnum;
  247. c->lpt_offs = offs;
  248. }
  249. offs += len;
  250. dbg_chk_lpt_sz(c, 1, len);
  251. cnode = cnode->cnext;
  252. } while (cnode && cnode != c->lpt_cnext);
  253. /* Make sure to place LPT's save table */
  254. if (!done_lsave) {
  255. if (offs + c->lsave_sz > c->leb_size) {
  256. alen = ALIGN(offs, c->min_io_size);
  257. upd_ltab(c, lnum, c->leb_size - alen, alen - offs);
  258. dbg_chk_lpt_sz(c, 2, c->leb_size - offs);
  259. err = alloc_lpt_leb(c, &lnum);
  260. if (err)
  261. goto no_space;
  262. offs = 0;
  263. ubifs_assert(lnum >= c->lpt_first &&
  264. lnum <= c->lpt_last);
  265. }
  266. done_lsave = 1;
  267. c->lsave_lnum = lnum;
  268. c->lsave_offs = offs;
  269. offs += c->lsave_sz;
  270. dbg_chk_lpt_sz(c, 1, c->lsave_sz);
  271. }
  272. /* Make sure to place LPT's own lprops table */
  273. if (!done_ltab) {
  274. if (offs + c->ltab_sz > c->leb_size) {
  275. alen = ALIGN(offs, c->min_io_size);
  276. upd_ltab(c, lnum, c->leb_size - alen, alen - offs);
  277. dbg_chk_lpt_sz(c, 2, c->leb_size - offs);
  278. err = alloc_lpt_leb(c, &lnum);
  279. if (err)
  280. goto no_space;
  281. offs = 0;
  282. ubifs_assert(lnum >= c->lpt_first &&
  283. lnum <= c->lpt_last);
  284. }
  285. done_ltab = 1;
  286. c->ltab_lnum = lnum;
  287. c->ltab_offs = offs;
  288. offs += c->ltab_sz;
  289. dbg_chk_lpt_sz(c, 1, c->ltab_sz);
  290. }
  291. alen = ALIGN(offs, c->min_io_size);
  292. upd_ltab(c, lnum, c->leb_size - alen, alen - offs);
  293. dbg_chk_lpt_sz(c, 4, alen - offs);
  294. err = dbg_chk_lpt_sz(c, 3, alen);
  295. if (err)
  296. return err;
  297. return 0;
  298. no_space:
  299. ubifs_err("LPT out of space at LEB %d:%d needing %d, done_ltab %d, "
  300. "done_lsave %d", lnum, offs, len, done_ltab, done_lsave);
  301. ubifs_dump_lpt_info(c);
  302. ubifs_dump_lpt_lebs(c);
  303. dump_stack();
  304. return err;
  305. }
  306. /**
  307. * realloc_lpt_leb - allocate an LPT LEB that is empty.
  308. * @c: UBIFS file-system description object
  309. * @lnum: LEB number is passed and returned here
  310. *
  311. * This function duplicates exactly the results of the function alloc_lpt_leb.
  312. * It is used during end commit to reallocate the same LEB numbers that were
  313. * allocated by alloc_lpt_leb during start commit.
  314. *
  315. * This function finds the next LEB that was allocated by the alloc_lpt_leb
  316. * function starting from @lnum. If a LEB is found it is returned in @lnum and
  317. * the function returns %0. Otherwise the function returns -ENOSPC.
  318. * Note however, that LPT is designed never to run out of space.
  319. */
  320. static int realloc_lpt_leb(struct ubifs_info *c, int *lnum)
  321. {
  322. int i, n;
  323. n = *lnum - c->lpt_first + 1;
  324. for (i = n; i < c->lpt_lebs; i++)
  325. if (c->ltab[i].cmt) {
  326. c->ltab[i].cmt = 0;
  327. *lnum = i + c->lpt_first;
  328. return 0;
  329. }
  330. for (i = 0; i < n; i++)
  331. if (c->ltab[i].cmt) {
  332. c->ltab[i].cmt = 0;
  333. *lnum = i + c->lpt_first;
  334. return 0;
  335. }
  336. return -ENOSPC;
  337. }
  338. /**
  339. * write_cnodes - write cnodes for commit.
  340. * @c: UBIFS file-system description object
  341. *
  342. * This function returns %0 on success and a negative error code on failure.
  343. */
  344. static int write_cnodes(struct ubifs_info *c)
  345. {
  346. int lnum, offs, len, from, err, wlen, alen, done_ltab, done_lsave;
  347. struct ubifs_cnode *cnode;
  348. void *buf = c->lpt_buf;
  349. cnode = c->lpt_cnext;
  350. if (!cnode)
  351. return 0;
  352. lnum = c->nhead_lnum;
  353. offs = c->nhead_offs;
  354. from = offs;
  355. /* Ensure empty LEB is unmapped */
  356. if (offs == 0) {
  357. err = ubifs_leb_unmap(c, lnum);
  358. if (err)
  359. return err;
  360. }
  361. /* Try to place lsave and ltab nicely */
  362. done_lsave = !c->big_lpt;
  363. done_ltab = 0;
  364. if (!done_lsave && offs + c->lsave_sz <= c->leb_size) {
  365. done_lsave = 1;
  366. ubifs_pack_lsave(c, buf + offs, c->lsave);
  367. offs += c->lsave_sz;
  368. dbg_chk_lpt_sz(c, 1, c->lsave_sz);
  369. }
  370. if (offs + c->ltab_sz <= c->leb_size) {
  371. done_ltab = 1;
  372. ubifs_pack_ltab(c, buf + offs, c->ltab_cmt);
  373. offs += c->ltab_sz;
  374. dbg_chk_lpt_sz(c, 1, c->ltab_sz);
  375. }
  376. /* Loop for each cnode */
  377. do {
  378. if (cnode->level)
  379. len = c->nnode_sz;
  380. else
  381. len = c->pnode_sz;
  382. while (offs + len > c->leb_size) {
  383. wlen = offs - from;
  384. if (wlen) {
  385. alen = ALIGN(wlen, c->min_io_size);
  386. memset(buf + offs, 0xff, alen - wlen);
  387. err = ubifs_leb_write(c, lnum, buf + from, from,
  388. alen);
  389. if (err)
  390. return err;
  391. }
  392. dbg_chk_lpt_sz(c, 2, c->leb_size - offs);
  393. err = realloc_lpt_leb(c, &lnum);
  394. if (err)
  395. goto no_space;
  396. offs = from = 0;
  397. ubifs_assert(lnum >= c->lpt_first &&
  398. lnum <= c->lpt_last);
  399. err = ubifs_leb_unmap(c, lnum);
  400. if (err)
  401. return err;
  402. /* Try to place lsave and ltab nicely */
  403. if (!done_lsave) {
  404. done_lsave = 1;
  405. ubifs_pack_lsave(c, buf + offs, c->lsave);
  406. offs += c->lsave_sz;
  407. dbg_chk_lpt_sz(c, 1, c->lsave_sz);
  408. continue;
  409. }
  410. if (!done_ltab) {
  411. done_ltab = 1;
  412. ubifs_pack_ltab(c, buf + offs, c->ltab_cmt);
  413. offs += c->ltab_sz;
  414. dbg_chk_lpt_sz(c, 1, c->ltab_sz);
  415. continue;
  416. }
  417. break;
  418. }
  419. if (cnode->level)
  420. ubifs_pack_nnode(c, buf + offs,
  421. (struct ubifs_nnode *)cnode);
  422. else
  423. ubifs_pack_pnode(c, buf + offs,
  424. (struct ubifs_pnode *)cnode);
  425. /*
  426. * The reason for the barriers is the same as in case of TNC.
  427. * See comment in 'write_index()'. 'dirty_cow_nnode()' and
  428. * 'dirty_cow_pnode()' are the functions for which this is
  429. * important.
  430. */
  431. clear_bit(DIRTY_CNODE, &cnode->flags);
  432. smp_mb__before_clear_bit();
  433. clear_bit(COW_CNODE, &cnode->flags);
  434. smp_mb__after_clear_bit();
  435. offs += len;
  436. dbg_chk_lpt_sz(c, 1, len);
  437. cnode = cnode->cnext;
  438. } while (cnode && cnode != c->lpt_cnext);
  439. /* Make sure to place LPT's save table */
  440. if (!done_lsave) {
  441. if (offs + c->lsave_sz > c->leb_size) {
  442. wlen = offs - from;
  443. alen = ALIGN(wlen, c->min_io_size);
  444. memset(buf + offs, 0xff, alen - wlen);
  445. err = ubifs_leb_write(c, lnum, buf + from, from, alen);
  446. 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. 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);
  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 at LEB %d:%d needing %d, done_ltab "
  510. "%d, done_lsave %d", lnum, offs, len, done_ltab, done_lsave);
  511. ubifs_dump_lpt_info(c);
  512. ubifs_dump_lpt_lebs(c);
  513. dump_stack();
  514. return err;
  515. }
  516. /**
  517. * next_pnode_to_dirty - find next pnode to dirty.
  518. * @c: UBIFS file-system description object
  519. * @pnode: pnode
  520. *
  521. * This function returns the next pnode to dirty or %NULL if there are no more
  522. * pnodes. Note that pnodes that have never been written (lnum == 0) are
  523. * skipped.
  524. */
  525. static struct ubifs_pnode *next_pnode_to_dirty(struct ubifs_info *c,
  526. struct ubifs_pnode *pnode)
  527. {
  528. struct ubifs_nnode *nnode;
  529. int iip;
  530. /* Try to go right */
  531. nnode = pnode->parent;
  532. for (iip = pnode->iip + 1; iip < UBIFS_LPT_FANOUT; iip++) {
  533. if (nnode->nbranch[iip].lnum)
  534. return ubifs_get_pnode(c, nnode, iip);
  535. }
  536. /* Go up while can't go right */
  537. do {
  538. iip = nnode->iip + 1;
  539. nnode = nnode->parent;
  540. if (!nnode)
  541. return NULL;
  542. for (; iip < UBIFS_LPT_FANOUT; iip++) {
  543. if (nnode->nbranch[iip].lnum)
  544. break;
  545. }
  546. } while (iip >= UBIFS_LPT_FANOUT);
  547. /* Go right */
  548. nnode = ubifs_get_nnode(c, nnode, iip);
  549. if (IS_ERR(nnode))
  550. return (void *)nnode;
  551. /* Go down to level 1 */
  552. while (nnode->level > 1) {
  553. for (iip = 0; iip < UBIFS_LPT_FANOUT; iip++) {
  554. if (nnode->nbranch[iip].lnum)
  555. break;
  556. }
  557. if (iip >= UBIFS_LPT_FANOUT) {
  558. /*
  559. * Should not happen, but we need to keep going
  560. * if it does.
  561. */
  562. iip = 0;
  563. }
  564. nnode = ubifs_get_nnode(c, nnode, iip);
  565. if (IS_ERR(nnode))
  566. return (void *)nnode;
  567. }
  568. for (iip = 0; iip < UBIFS_LPT_FANOUT; iip++)
  569. if (nnode->nbranch[iip].lnum)
  570. break;
  571. if (iip >= UBIFS_LPT_FANOUT)
  572. /* Should not happen, but we need to keep going if it does */
  573. iip = 0;
  574. return ubifs_get_pnode(c, nnode, iip);
  575. }
  576. /**
  577. * pnode_lookup - lookup a pnode in the LPT.
  578. * @c: UBIFS file-system description object
  579. * @i: pnode number (0 to main_lebs - 1)
  580. *
  581. * This function returns a pointer to the pnode on success or a negative
  582. * error code on failure.
  583. */
  584. static struct ubifs_pnode *pnode_lookup(struct ubifs_info *c, int i)
  585. {
  586. int err, h, iip, shft;
  587. struct ubifs_nnode *nnode;
  588. if (!c->nroot) {
  589. err = ubifs_read_nnode(c, NULL, 0);
  590. if (err)
  591. return ERR_PTR(err);
  592. }
  593. i <<= UBIFS_LPT_FANOUT_SHIFT;
  594. nnode = c->nroot;
  595. shft = c->lpt_hght * UBIFS_LPT_FANOUT_SHIFT;
  596. for (h = 1; h < c->lpt_hght; h++) {
  597. iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
  598. shft -= UBIFS_LPT_FANOUT_SHIFT;
  599. nnode = ubifs_get_nnode(c, nnode, iip);
  600. if (IS_ERR(nnode))
  601. return ERR_CAST(nnode);
  602. }
  603. iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
  604. return ubifs_get_pnode(c, nnode, iip);
  605. }
  606. /**
  607. * add_pnode_dirt - add dirty space to LPT LEB properties.
  608. * @c: UBIFS file-system description object
  609. * @pnode: pnode for which to add dirt
  610. */
  611. static void add_pnode_dirt(struct ubifs_info *c, struct ubifs_pnode *pnode)
  612. {
  613. ubifs_add_lpt_dirt(c, pnode->parent->nbranch[pnode->iip].lnum,
  614. c->pnode_sz);
  615. }
  616. /**
  617. * do_make_pnode_dirty - mark a pnode dirty.
  618. * @c: UBIFS file-system description object
  619. * @pnode: pnode to mark dirty
  620. */
  621. static void do_make_pnode_dirty(struct ubifs_info *c, struct ubifs_pnode *pnode)
  622. {
  623. /* Assumes cnext list is empty i.e. not called during commit */
  624. if (!test_and_set_bit(DIRTY_CNODE, &pnode->flags)) {
  625. struct ubifs_nnode *nnode;
  626. c->dirty_pn_cnt += 1;
  627. add_pnode_dirt(c, pnode);
  628. /* Mark parent and ancestors dirty too */
  629. nnode = pnode->parent;
  630. while (nnode) {
  631. if (!test_and_set_bit(DIRTY_CNODE, &nnode->flags)) {
  632. c->dirty_nn_cnt += 1;
  633. ubifs_add_nnode_dirt(c, nnode);
  634. nnode = nnode->parent;
  635. } else
  636. break;
  637. }
  638. }
  639. }
  640. /**
  641. * make_tree_dirty - mark the entire LEB properties tree dirty.
  642. * @c: UBIFS file-system description object
  643. *
  644. * This function is used by the "small" LPT model to cause the entire LEB
  645. * properties tree to be written. The "small" LPT model does not use LPT
  646. * garbage collection because it is more efficient to write the entire tree
  647. * (because it is small).
  648. *
  649. * This function returns %0 on success and a negative error code on failure.
  650. */
  651. static int make_tree_dirty(struct ubifs_info *c)
  652. {
  653. struct ubifs_pnode *pnode;
  654. pnode = pnode_lookup(c, 0);
  655. if (IS_ERR(pnode))
  656. return PTR_ERR(pnode);
  657. while (pnode) {
  658. do_make_pnode_dirty(c, pnode);
  659. pnode = next_pnode_to_dirty(c, pnode);
  660. if (IS_ERR(pnode))
  661. return PTR_ERR(pnode);
  662. }
  663. return 0;
  664. }
  665. /**
  666. * need_write_all - determine if the LPT area is running out of free space.
  667. * @c: UBIFS file-system description object
  668. *
  669. * This function returns %1 if the LPT area is running out of free space and %0
  670. * if it is not.
  671. */
  672. static int need_write_all(struct ubifs_info *c)
  673. {
  674. long long free = 0;
  675. int i;
  676. for (i = 0; i < c->lpt_lebs; i++) {
  677. if (i + c->lpt_first == c->nhead_lnum)
  678. free += c->leb_size - c->nhead_offs;
  679. else if (c->ltab[i].free == c->leb_size)
  680. free += c->leb_size;
  681. else if (c->ltab[i].free + c->ltab[i].dirty == c->leb_size)
  682. free += c->leb_size;
  683. }
  684. /* Less than twice the size left */
  685. if (free <= c->lpt_sz * 2)
  686. return 1;
  687. return 0;
  688. }
  689. /**
  690. * lpt_tgc_start - start trivial garbage collection of LPT LEBs.
  691. * @c: UBIFS file-system description object
  692. *
  693. * LPT trivial garbage collection is where a LPT LEB contains only dirty and
  694. * free space and so may be reused as soon as the next commit is completed.
  695. * This function is called during start commit to mark LPT LEBs for trivial GC.
  696. */
  697. static void lpt_tgc_start(struct ubifs_info *c)
  698. {
  699. int i;
  700. for (i = 0; i < c->lpt_lebs; i++) {
  701. if (i + c->lpt_first == c->nhead_lnum)
  702. continue;
  703. if (c->ltab[i].dirty > 0 &&
  704. c->ltab[i].free + c->ltab[i].dirty == c->leb_size) {
  705. c->ltab[i].tgc = 1;
  706. c->ltab[i].free = c->leb_size;
  707. c->ltab[i].dirty = 0;
  708. dbg_lp("LEB %d", i + c->lpt_first);
  709. }
  710. }
  711. }
  712. /**
  713. * lpt_tgc_end - end trivial garbage collection of LPT LEBs.
  714. * @c: UBIFS file-system description object
  715. *
  716. * LPT trivial garbage collection is where a LPT LEB contains only dirty and
  717. * free space and so may be reused as soon as the next commit is completed.
  718. * This function is called after the commit is completed (master node has been
  719. * written) and un-maps LPT LEBs that were marked for trivial GC.
  720. */
  721. static int lpt_tgc_end(struct ubifs_info *c)
  722. {
  723. int i, err;
  724. for (i = 0; i < c->lpt_lebs; i++)
  725. if (c->ltab[i].tgc) {
  726. err = ubifs_leb_unmap(c, i + c->lpt_first);
  727. if (err)
  728. return err;
  729. c->ltab[i].tgc = 0;
  730. dbg_lp("LEB %d", i + c->lpt_first);
  731. }
  732. return 0;
  733. }
  734. /**
  735. * populate_lsave - fill the lsave array with important LEB numbers.
  736. * @c: the UBIFS file-system description object
  737. *
  738. * This function is only called for the "big" model. It records a small number
  739. * of LEB numbers of important LEBs. Important LEBs are ones that are (from
  740. * most important to least important): empty, freeable, freeable index, dirty
  741. * index, dirty or free. Upon mount, we read this list of LEB numbers and bring
  742. * their pnodes into memory. That will stop us from having to scan the LPT
  743. * straight away. For the "small" model we assume that scanning the LPT is no
  744. * big deal.
  745. */
  746. static void populate_lsave(struct ubifs_info *c)
  747. {
  748. struct ubifs_lprops *lprops;
  749. struct ubifs_lpt_heap *heap;
  750. int i, cnt = 0;
  751. ubifs_assert(c->big_lpt);
  752. if (!(c->lpt_drty_flgs & LSAVE_DIRTY)) {
  753. c->lpt_drty_flgs |= LSAVE_DIRTY;
  754. ubifs_add_lpt_dirt(c, c->lsave_lnum, c->lsave_sz);
  755. }
  756. if (dbg_populate_lsave(c))
  757. return;
  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 = ubifs_leb_read(c, lnum, buf, 0, c->leb_size, 1);
  1075. if (err)
  1076. return err;
  1077. while (1) {
  1078. if (!is_a_node(c, buf, len)) {
  1079. int pad_len;
  1080. pad_len = get_pad_len(c, buf, len);
  1081. if (pad_len) {
  1082. buf += pad_len;
  1083. len -= pad_len;
  1084. continue;
  1085. }
  1086. return 0;
  1087. }
  1088. node_type = get_lpt_node_type(c, buf, &node_num);
  1089. node_len = get_lpt_node_len(c, node_type);
  1090. offs = c->leb_size - len;
  1091. ubifs_assert(node_len != 0);
  1092. mutex_lock(&c->lp_mutex);
  1093. err = make_node_dirty(c, node_type, node_num, lnum, offs);
  1094. mutex_unlock(&c->lp_mutex);
  1095. if (err)
  1096. return err;
  1097. buf += node_len;
  1098. len -= node_len;
  1099. }
  1100. return 0;
  1101. }
  1102. /**
  1103. * lpt_gc - LPT garbage collection.
  1104. * @c: UBIFS file-system description object
  1105. *
  1106. * Select a LPT LEB for LPT garbage collection and call 'lpt_gc_lnum()'.
  1107. * Returns %0 on success and a negative error code on failure.
  1108. */
  1109. static int lpt_gc(struct ubifs_info *c)
  1110. {
  1111. int i, lnum = -1, dirty = 0;
  1112. mutex_lock(&c->lp_mutex);
  1113. for (i = 0; i < c->lpt_lebs; i++) {
  1114. ubifs_assert(!c->ltab[i].tgc);
  1115. if (i + c->lpt_first == c->nhead_lnum ||
  1116. c->ltab[i].free + c->ltab[i].dirty == c->leb_size)
  1117. continue;
  1118. if (c->ltab[i].dirty > dirty) {
  1119. dirty = c->ltab[i].dirty;
  1120. lnum = i + c->lpt_first;
  1121. }
  1122. }
  1123. mutex_unlock(&c->lp_mutex);
  1124. if (lnum == -1)
  1125. return -ENOSPC;
  1126. return lpt_gc_lnum(c, lnum);
  1127. }
  1128. /**
  1129. * ubifs_lpt_start_commit - UBIFS commit starts.
  1130. * @c: the UBIFS file-system description object
  1131. *
  1132. * This function has to be called when UBIFS starts the commit operation.
  1133. * This function "freezes" all currently dirty LEB properties and does not
  1134. * change them anymore. Further changes are saved and tracked separately
  1135. * because they are not part of this commit. This function returns zero in case
  1136. * of success and a negative error code in case of failure.
  1137. */
  1138. int ubifs_lpt_start_commit(struct ubifs_info *c)
  1139. {
  1140. int err, cnt;
  1141. dbg_lp("");
  1142. mutex_lock(&c->lp_mutex);
  1143. err = dbg_chk_lpt_free_spc(c);
  1144. if (err)
  1145. goto out;
  1146. err = dbg_check_ltab(c);
  1147. if (err)
  1148. goto out;
  1149. if (c->check_lpt_free) {
  1150. /*
  1151. * We ensure there is enough free space in
  1152. * ubifs_lpt_post_commit() by marking nodes dirty. That
  1153. * information is lost when we unmount, so we also need
  1154. * to check free space once after mounting also.
  1155. */
  1156. c->check_lpt_free = 0;
  1157. while (need_write_all(c)) {
  1158. mutex_unlock(&c->lp_mutex);
  1159. err = lpt_gc(c);
  1160. if (err)
  1161. return err;
  1162. mutex_lock(&c->lp_mutex);
  1163. }
  1164. }
  1165. lpt_tgc_start(c);
  1166. if (!c->dirty_pn_cnt) {
  1167. dbg_cmt("no cnodes to commit");
  1168. err = 0;
  1169. goto out;
  1170. }
  1171. if (!c->big_lpt && need_write_all(c)) {
  1172. /* If needed, write everything */
  1173. err = make_tree_dirty(c);
  1174. if (err)
  1175. goto out;
  1176. lpt_tgc_start(c);
  1177. }
  1178. if (c->big_lpt)
  1179. populate_lsave(c);
  1180. cnt = get_cnodes_to_commit(c);
  1181. ubifs_assert(cnt != 0);
  1182. err = layout_cnodes(c);
  1183. if (err)
  1184. goto out;
  1185. /* Copy the LPT's own lprops for end commit to write */
  1186. memcpy(c->ltab_cmt, c->ltab,
  1187. sizeof(struct ubifs_lpt_lprops) * c->lpt_lebs);
  1188. c->lpt_drty_flgs &= ~(LTAB_DIRTY | LSAVE_DIRTY);
  1189. out:
  1190. mutex_unlock(&c->lp_mutex);
  1191. return err;
  1192. }
  1193. /**
  1194. * free_obsolete_cnodes - free obsolete cnodes for commit end.
  1195. * @c: UBIFS file-system description object
  1196. */
  1197. static void free_obsolete_cnodes(struct ubifs_info *c)
  1198. {
  1199. struct ubifs_cnode *cnode, *cnext;
  1200. cnext = c->lpt_cnext;
  1201. if (!cnext)
  1202. return;
  1203. do {
  1204. cnode = cnext;
  1205. cnext = cnode->cnext;
  1206. if (test_bit(OBSOLETE_CNODE, &cnode->flags))
  1207. kfree(cnode);
  1208. else
  1209. cnode->cnext = NULL;
  1210. } while (cnext != c->lpt_cnext);
  1211. c->lpt_cnext = NULL;
  1212. }
  1213. /**
  1214. * ubifs_lpt_end_commit - finish the commit operation.
  1215. * @c: the UBIFS file-system description object
  1216. *
  1217. * This function has to be called when the commit operation finishes. It
  1218. * flushes the changes which were "frozen" by 'ubifs_lprops_start_commit()' to
  1219. * the media. Returns zero in case of success and a negative error code in case
  1220. * of failure.
  1221. */
  1222. int ubifs_lpt_end_commit(struct ubifs_info *c)
  1223. {
  1224. int err;
  1225. dbg_lp("");
  1226. if (!c->lpt_cnext)
  1227. return 0;
  1228. err = write_cnodes(c);
  1229. if (err)
  1230. return err;
  1231. mutex_lock(&c->lp_mutex);
  1232. free_obsolete_cnodes(c);
  1233. mutex_unlock(&c->lp_mutex);
  1234. return 0;
  1235. }
  1236. /**
  1237. * ubifs_lpt_post_commit - post commit LPT trivial GC and LPT GC.
  1238. * @c: UBIFS file-system description object
  1239. *
  1240. * LPT trivial GC is completed after a commit. Also LPT GC is done after a
  1241. * commit for the "big" LPT model.
  1242. */
  1243. int ubifs_lpt_post_commit(struct ubifs_info *c)
  1244. {
  1245. int err;
  1246. mutex_lock(&c->lp_mutex);
  1247. err = lpt_tgc_end(c);
  1248. if (err)
  1249. goto out;
  1250. if (c->big_lpt)
  1251. while (need_write_all(c)) {
  1252. mutex_unlock(&c->lp_mutex);
  1253. err = lpt_gc(c);
  1254. if (err)
  1255. return err;
  1256. mutex_lock(&c->lp_mutex);
  1257. }
  1258. out:
  1259. mutex_unlock(&c->lp_mutex);
  1260. return err;
  1261. }
  1262. /**
  1263. * first_nnode - find the first nnode in memory.
  1264. * @c: UBIFS file-system description object
  1265. * @hght: height of tree where nnode found is returned here
  1266. *
  1267. * This function returns a pointer to the nnode found or %NULL if no nnode is
  1268. * found. This function is a helper to 'ubifs_lpt_free()'.
  1269. */
  1270. static struct ubifs_nnode *first_nnode(struct ubifs_info *c, int *hght)
  1271. {
  1272. struct ubifs_nnode *nnode;
  1273. int h, i, found;
  1274. nnode = c->nroot;
  1275. *hght = 0;
  1276. if (!nnode)
  1277. return NULL;
  1278. for (h = 1; h < c->lpt_hght; h++) {
  1279. found = 0;
  1280. for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
  1281. if (nnode->nbranch[i].nnode) {
  1282. found = 1;
  1283. nnode = nnode->nbranch[i].nnode;
  1284. *hght = h;
  1285. break;
  1286. }
  1287. }
  1288. if (!found)
  1289. break;
  1290. }
  1291. return nnode;
  1292. }
  1293. /**
  1294. * next_nnode - find the next nnode in memory.
  1295. * @c: UBIFS file-system description object
  1296. * @nnode: nnode from which to start.
  1297. * @hght: height of tree where nnode is, is passed and returned here
  1298. *
  1299. * This function returns a pointer to the nnode found or %NULL if no nnode is
  1300. * found. This function is a helper to 'ubifs_lpt_free()'.
  1301. */
  1302. static struct ubifs_nnode *next_nnode(struct ubifs_info *c,
  1303. struct ubifs_nnode *nnode, int *hght)
  1304. {
  1305. struct ubifs_nnode *parent;
  1306. int iip, h, i, found;
  1307. parent = nnode->parent;
  1308. if (!parent)
  1309. return NULL;
  1310. if (nnode->iip == UBIFS_LPT_FANOUT - 1) {
  1311. *hght -= 1;
  1312. return parent;
  1313. }
  1314. for (iip = nnode->iip + 1; iip < UBIFS_LPT_FANOUT; iip++) {
  1315. nnode = parent->nbranch[iip].nnode;
  1316. if (nnode)
  1317. break;
  1318. }
  1319. if (!nnode) {
  1320. *hght -= 1;
  1321. return parent;
  1322. }
  1323. for (h = *hght + 1; h < c->lpt_hght; h++) {
  1324. found = 0;
  1325. for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
  1326. if (nnode->nbranch[i].nnode) {
  1327. found = 1;
  1328. nnode = nnode->nbranch[i].nnode;
  1329. *hght = h;
  1330. break;
  1331. }
  1332. }
  1333. if (!found)
  1334. break;
  1335. }
  1336. return nnode;
  1337. }
  1338. /**
  1339. * ubifs_lpt_free - free resources owned by the LPT.
  1340. * @c: UBIFS file-system description object
  1341. * @wr_only: free only resources used for writing
  1342. */
  1343. void ubifs_lpt_free(struct ubifs_info *c, int wr_only)
  1344. {
  1345. struct ubifs_nnode *nnode;
  1346. int i, hght;
  1347. /* Free write-only things first */
  1348. free_obsolete_cnodes(c); /* Leftover from a failed commit */
  1349. vfree(c->ltab_cmt);
  1350. c->ltab_cmt = NULL;
  1351. vfree(c->lpt_buf);
  1352. c->lpt_buf = NULL;
  1353. kfree(c->lsave);
  1354. c->lsave = NULL;
  1355. if (wr_only)
  1356. return;
  1357. /* Now free the rest */
  1358. nnode = first_nnode(c, &hght);
  1359. while (nnode) {
  1360. for (i = 0; i < UBIFS_LPT_FANOUT; i++)
  1361. kfree(nnode->nbranch[i].nnode);
  1362. nnode = next_nnode(c, nnode, &hght);
  1363. }
  1364. for (i = 0; i < LPROPS_HEAP_CNT; i++)
  1365. kfree(c->lpt_heap[i].arr);
  1366. kfree(c->dirty_idx.arr);
  1367. kfree(c->nroot);
  1368. vfree(c->ltab);
  1369. kfree(c->lpt_nod_buf);
  1370. }
  1371. /*
  1372. * Everything below is related to debugging.
  1373. */
  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 (!dbg_is_chk_lprops(c))
  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 = ubifs_leb_read(c, lnum, buf, 0, c->leb_size, 1);
  1513. if (err)
  1514. goto out;
  1515. while (1) {
  1516. if (!is_a_node(c, p, len)) {
  1517. int i, pad_len;
  1518. pad_len = get_pad_len(c, p, len);
  1519. if (pad_len) {
  1520. p += pad_len;
  1521. len -= pad_len;
  1522. dirty += pad_len;
  1523. continue;
  1524. }
  1525. if (!dbg_is_all_ff(p, len)) {
  1526. dbg_msg("invalid empty space in LEB %d at %d",
  1527. lnum, c->leb_size - len);
  1528. err = -EINVAL;
  1529. }
  1530. i = lnum - c->lpt_first;
  1531. if (len != c->ltab[i].free) {
  1532. dbg_msg("invalid free space in LEB %d "
  1533. "(free %d, expected %d)",
  1534. lnum, len, c->ltab[i].free);
  1535. err = -EINVAL;
  1536. }
  1537. if (dirty != c->ltab[i].dirty) {
  1538. dbg_msg("invalid dirty space in LEB %d "
  1539. "(dirty %d, expected %d)",
  1540. lnum, dirty, c->ltab[i].dirty);
  1541. err = -EINVAL;
  1542. }
  1543. goto out;
  1544. }
  1545. node_type = get_lpt_node_type(c, p, &node_num);
  1546. node_len = get_lpt_node_len(c, node_type);
  1547. ret = dbg_is_node_dirty(c, node_type, lnum, c->leb_size - len);
  1548. if (ret == 1)
  1549. dirty += node_len;
  1550. p += node_len;
  1551. len -= node_len;
  1552. }
  1553. err = 0;
  1554. out:
  1555. vfree(buf);
  1556. return err;
  1557. }
  1558. /**
  1559. * dbg_check_ltab - check the free and dirty space in the ltab.
  1560. * @c: the UBIFS file-system description object
  1561. *
  1562. * This function returns %0 on success and a negative error code on failure.
  1563. */
  1564. int dbg_check_ltab(struct ubifs_info *c)
  1565. {
  1566. int lnum, err, i, cnt;
  1567. if (!dbg_is_chk_lprops(c))
  1568. return 0;
  1569. /* Bring the entire tree into memory */
  1570. cnt = DIV_ROUND_UP(c->main_lebs, UBIFS_LPT_FANOUT);
  1571. for (i = 0; i < cnt; i++) {
  1572. struct ubifs_pnode *pnode;
  1573. pnode = pnode_lookup(c, i);
  1574. if (IS_ERR(pnode))
  1575. return PTR_ERR(pnode);
  1576. cond_resched();
  1577. }
  1578. /* Check nodes */
  1579. err = dbg_check_lpt_nodes(c, (struct ubifs_cnode *)c->nroot, 0, 0);
  1580. if (err)
  1581. return err;
  1582. /* Check each LEB */
  1583. for (lnum = c->lpt_first; lnum <= c->lpt_last; lnum++) {
  1584. err = dbg_check_ltab_lnum(c, lnum);
  1585. if (err) {
  1586. ubifs_err("failed at LEB %d", lnum);
  1587. return err;
  1588. }
  1589. }
  1590. dbg_lp("succeeded");
  1591. return 0;
  1592. }
  1593. /**
  1594. * dbg_chk_lpt_free_spc - check LPT free space is enough to write entire LPT.
  1595. * @c: the UBIFS file-system description object
  1596. *
  1597. * This function returns %0 on success and a negative error code on failure.
  1598. */
  1599. int dbg_chk_lpt_free_spc(struct ubifs_info *c)
  1600. {
  1601. long long free = 0;
  1602. int i;
  1603. if (!dbg_is_chk_lprops(c))
  1604. return 0;
  1605. for (i = 0; i < c->lpt_lebs; i++) {
  1606. if (c->ltab[i].tgc || c->ltab[i].cmt)
  1607. continue;
  1608. if (i + c->lpt_first == c->nhead_lnum)
  1609. free += c->leb_size - c->nhead_offs;
  1610. else if (c->ltab[i].free == c->leb_size)
  1611. free += c->leb_size;
  1612. }
  1613. if (free < c->lpt_sz) {
  1614. ubifs_err("LPT space error: free %lld lpt_sz %lld",
  1615. free, c->lpt_sz);
  1616. ubifs_dump_lpt_info(c);
  1617. ubifs_dump_lpt_lebs(c);
  1618. dump_stack();
  1619. return -EINVAL;
  1620. }
  1621. return 0;
  1622. }
  1623. /**
  1624. * dbg_chk_lpt_sz - check LPT does not write more than LPT size.
  1625. * @c: the UBIFS file-system description object
  1626. * @action: what to do
  1627. * @len: length written
  1628. *
  1629. * This function returns %0 on success and a negative error code on failure.
  1630. * The @action argument may be one of:
  1631. * o %0 - LPT debugging checking starts, initialize debugging variables;
  1632. * o %1 - wrote an LPT node, increase LPT size by @len bytes;
  1633. * o %2 - switched to a different LEB and wasted @len bytes;
  1634. * o %3 - check that we've written the right number of bytes.
  1635. * o %4 - wasted @len bytes;
  1636. */
  1637. int dbg_chk_lpt_sz(struct ubifs_info *c, int action, int len)
  1638. {
  1639. struct ubifs_debug_info *d = c->dbg;
  1640. long long chk_lpt_sz, lpt_sz;
  1641. int err = 0;
  1642. if (!dbg_is_chk_lprops(c))
  1643. return 0;
  1644. switch (action) {
  1645. case 0:
  1646. d->chk_lpt_sz = 0;
  1647. d->chk_lpt_sz2 = 0;
  1648. d->chk_lpt_lebs = 0;
  1649. d->chk_lpt_wastage = 0;
  1650. if (c->dirty_pn_cnt > c->pnode_cnt) {
  1651. ubifs_err("dirty pnodes %d exceed max %d",
  1652. c->dirty_pn_cnt, c->pnode_cnt);
  1653. err = -EINVAL;
  1654. }
  1655. if (c->dirty_nn_cnt > c->nnode_cnt) {
  1656. ubifs_err("dirty nnodes %d exceed max %d",
  1657. c->dirty_nn_cnt, c->nnode_cnt);
  1658. err = -EINVAL;
  1659. }
  1660. return err;
  1661. case 1:
  1662. d->chk_lpt_sz += len;
  1663. return 0;
  1664. case 2:
  1665. d->chk_lpt_sz += len;
  1666. d->chk_lpt_wastage += len;
  1667. d->chk_lpt_lebs += 1;
  1668. return 0;
  1669. case 3:
  1670. chk_lpt_sz = c->leb_size;
  1671. chk_lpt_sz *= d->chk_lpt_lebs;
  1672. chk_lpt_sz += len - c->nhead_offs;
  1673. if (d->chk_lpt_sz != chk_lpt_sz) {
  1674. ubifs_err("LPT wrote %lld but space used was %lld",
  1675. d->chk_lpt_sz, chk_lpt_sz);
  1676. err = -EINVAL;
  1677. }
  1678. if (d->chk_lpt_sz > c->lpt_sz) {
  1679. ubifs_err("LPT wrote %lld but lpt_sz is %lld",
  1680. d->chk_lpt_sz, c->lpt_sz);
  1681. err = -EINVAL;
  1682. }
  1683. if (d->chk_lpt_sz2 && d->chk_lpt_sz != d->chk_lpt_sz2) {
  1684. ubifs_err("LPT layout size %lld but wrote %lld",
  1685. d->chk_lpt_sz, d->chk_lpt_sz2);
  1686. err = -EINVAL;
  1687. }
  1688. if (d->chk_lpt_sz2 && d->new_nhead_offs != len) {
  1689. ubifs_err("LPT new nhead offs: expected %d was %d",
  1690. d->new_nhead_offs, len);
  1691. err = -EINVAL;
  1692. }
  1693. lpt_sz = (long long)c->pnode_cnt * c->pnode_sz;
  1694. lpt_sz += (long long)c->nnode_cnt * c->nnode_sz;
  1695. lpt_sz += c->ltab_sz;
  1696. if (c->big_lpt)
  1697. lpt_sz += c->lsave_sz;
  1698. if (d->chk_lpt_sz - d->chk_lpt_wastage > lpt_sz) {
  1699. ubifs_err("LPT chk_lpt_sz %lld + waste %lld exceeds %lld",
  1700. d->chk_lpt_sz, d->chk_lpt_wastage, lpt_sz);
  1701. err = -EINVAL;
  1702. }
  1703. if (err) {
  1704. ubifs_dump_lpt_info(c);
  1705. ubifs_dump_lpt_lebs(c);
  1706. dump_stack();
  1707. }
  1708. d->chk_lpt_sz2 = d->chk_lpt_sz;
  1709. d->chk_lpt_sz = 0;
  1710. d->chk_lpt_wastage = 0;
  1711. d->chk_lpt_lebs = 0;
  1712. d->new_nhead_offs = len;
  1713. return err;
  1714. case 4:
  1715. d->chk_lpt_sz += len;
  1716. d->chk_lpt_wastage += len;
  1717. return 0;
  1718. default:
  1719. return -EINVAL;
  1720. }
  1721. }
  1722. /**
  1723. * ubifs_dump_lpt_leb - dump an LPT LEB.
  1724. * @c: UBIFS file-system description object
  1725. * @lnum: LEB number to dump
  1726. *
  1727. * This function dumps an LEB from LPT area. Nodes in this area are very
  1728. * different to nodes in the main area (e.g., they do not have common headers,
  1729. * they do not have 8-byte alignments, etc), so we have a separate function to
  1730. * dump LPT area LEBs. Note, LPT has to be locked by the caller.
  1731. */
  1732. static void dump_lpt_leb(const struct ubifs_info *c, int lnum)
  1733. {
  1734. int err, len = c->leb_size, node_type, node_num, node_len, offs;
  1735. void *buf, *p;
  1736. printk(KERN_DEBUG "(pid %d) start dumping LEB %d\n",
  1737. current->pid, lnum);
  1738. buf = p = __vmalloc(c->leb_size, GFP_NOFS, PAGE_KERNEL);
  1739. if (!buf) {
  1740. ubifs_err("cannot allocate memory to dump LPT");
  1741. return;
  1742. }
  1743. err = ubifs_leb_read(c, lnum, buf, 0, c->leb_size, 1);
  1744. if (err)
  1745. goto out;
  1746. while (1) {
  1747. offs = c->leb_size - len;
  1748. if (!is_a_node(c, p, len)) {
  1749. int pad_len;
  1750. pad_len = get_pad_len(c, p, len);
  1751. if (pad_len) {
  1752. printk(KERN_DEBUG "LEB %d:%d, pad %d bytes\n",
  1753. lnum, offs, pad_len);
  1754. p += pad_len;
  1755. len -= pad_len;
  1756. continue;
  1757. }
  1758. if (len)
  1759. printk(KERN_DEBUG "LEB %d:%d, free %d bytes\n",
  1760. lnum, offs, len);
  1761. break;
  1762. }
  1763. node_type = get_lpt_node_type(c, p, &node_num);
  1764. switch (node_type) {
  1765. case UBIFS_LPT_PNODE:
  1766. {
  1767. node_len = c->pnode_sz;
  1768. if (c->big_lpt)
  1769. printk(KERN_DEBUG "LEB %d:%d, pnode num %d\n",
  1770. lnum, offs, node_num);
  1771. else
  1772. printk(KERN_DEBUG "LEB %d:%d, pnode\n",
  1773. lnum, offs);
  1774. break;
  1775. }
  1776. case UBIFS_LPT_NNODE:
  1777. {
  1778. int i;
  1779. struct ubifs_nnode nnode;
  1780. node_len = c->nnode_sz;
  1781. if (c->big_lpt)
  1782. printk(KERN_DEBUG "LEB %d:%d, nnode num %d, ",
  1783. lnum, offs, node_num);
  1784. else
  1785. printk(KERN_DEBUG "LEB %d:%d, nnode, ",
  1786. lnum, offs);
  1787. err = ubifs_unpack_nnode(c, p, &nnode);
  1788. for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
  1789. printk(KERN_CONT "%d:%d", nnode.nbranch[i].lnum,
  1790. nnode.nbranch[i].offs);
  1791. if (i != UBIFS_LPT_FANOUT - 1)
  1792. printk(KERN_CONT ", ");
  1793. }
  1794. printk(KERN_CONT "\n");
  1795. break;
  1796. }
  1797. case UBIFS_LPT_LTAB:
  1798. node_len = c->ltab_sz;
  1799. printk(KERN_DEBUG "LEB %d:%d, ltab\n",
  1800. lnum, offs);
  1801. break;
  1802. case UBIFS_LPT_LSAVE:
  1803. node_len = c->lsave_sz;
  1804. printk(KERN_DEBUG "LEB %d:%d, lsave len\n", lnum, offs);
  1805. break;
  1806. default:
  1807. ubifs_err("LPT node type %d not recognized", node_type);
  1808. goto out;
  1809. }
  1810. p += node_len;
  1811. len -= node_len;
  1812. }
  1813. printk(KERN_DEBUG "(pid %d) finish dumping LEB %d\n",
  1814. current->pid, lnum);
  1815. out:
  1816. vfree(buf);
  1817. return;
  1818. }
  1819. /**
  1820. * ubifs_dump_lpt_lebs - dump LPT lebs.
  1821. * @c: UBIFS file-system description object
  1822. *
  1823. * This function dumps all LPT LEBs. The caller has to make sure the LPT is
  1824. * locked.
  1825. */
  1826. void ubifs_dump_lpt_lebs(const struct ubifs_info *c)
  1827. {
  1828. int i;
  1829. printk(KERN_DEBUG "(pid %d) start dumping all LPT LEBs\n",
  1830. current->pid);
  1831. for (i = 0; i < c->lpt_lebs; i++)
  1832. dump_lpt_leb(c, i + c->lpt_first);
  1833. printk(KERN_DEBUG "(pid %d) finish dumping all LPT LEBs\n",
  1834. current->pid);
  1835. }
  1836. /**
  1837. * dbg_populate_lsave - debugging version of 'populate_lsave()'
  1838. * @c: UBIFS file-system description object
  1839. *
  1840. * This is a debugging version for 'populate_lsave()' which populates lsave
  1841. * with random LEBs instead of useful LEBs, which is good for test coverage.
  1842. * Returns zero if lsave has not been populated (this debugging feature is
  1843. * disabled) an non-zero if lsave has been populated.
  1844. */
  1845. static int dbg_populate_lsave(struct ubifs_info *c)
  1846. {
  1847. struct ubifs_lprops *lprops;
  1848. struct ubifs_lpt_heap *heap;
  1849. int i;
  1850. if (!dbg_is_chk_gen(c))
  1851. return 0;
  1852. if (random32() & 3)
  1853. return 0;
  1854. for (i = 0; i < c->lsave_cnt; i++)
  1855. c->lsave[i] = c->main_first;
  1856. list_for_each_entry(lprops, &c->empty_list, list)
  1857. c->lsave[random32() % c->lsave_cnt] = lprops->lnum;
  1858. list_for_each_entry(lprops, &c->freeable_list, list)
  1859. c->lsave[random32() % c->lsave_cnt] = lprops->lnum;
  1860. list_for_each_entry(lprops, &c->frdi_idx_list, list)
  1861. c->lsave[random32() % c->lsave_cnt] = lprops->lnum;
  1862. heap = &c->lpt_heap[LPROPS_DIRTY_IDX - 1];
  1863. for (i = 0; i < heap->cnt; i++)
  1864. c->lsave[random32() % c->lsave_cnt] = heap->arr[i]->lnum;
  1865. heap = &c->lpt_heap[LPROPS_DIRTY - 1];
  1866. for (i = 0; i < heap->cnt; i++)
  1867. c->lsave[random32() % c->lsave_cnt] = heap->arr[i]->lnum;
  1868. heap = &c->lpt_heap[LPROPS_FREE - 1];
  1869. for (i = 0; i < heap->cnt; i++)
  1870. c->lsave[random32() % c->lsave_cnt] = heap->arr[i]->lnum;
  1871. return 1;
  1872. }