lpt_commit.c 46 KB

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