lpt_commit.c 52 KB

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