lpt_commit.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815
  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. else
  535. return NULL;
  536. }
  537. /* Go up while can't go right */
  538. do {
  539. iip = nnode->iip + 1;
  540. nnode = nnode->parent;
  541. if (!nnode)
  542. return NULL;
  543. /* We assume here that LEB zero is never an LPT LEB */
  544. } while (iip >= UBIFS_LPT_FANOUT || !nnode->nbranch[iip].lnum);
  545. /* Go right */
  546. nnode = ubifs_get_nnode(c, nnode, iip);
  547. if (IS_ERR(nnode))
  548. return (void *)nnode;
  549. /* Go down to level 1 */
  550. while (nnode->level > 1) {
  551. nnode = ubifs_get_nnode(c, nnode, 0);
  552. if (IS_ERR(nnode))
  553. return (void *)nnode;
  554. }
  555. return ubifs_get_pnode(c, nnode, 0);
  556. }
  557. /**
  558. * pnode_lookup - lookup a pnode in the LPT.
  559. * @c: UBIFS file-system description object
  560. * @i: pnode number (0 to main_lebs - 1)
  561. *
  562. * This function returns a pointer to the pnode on success or a negative
  563. * error code on failure.
  564. */
  565. static struct ubifs_pnode *pnode_lookup(struct ubifs_info *c, int i)
  566. {
  567. int err, h, iip, shft;
  568. struct ubifs_nnode *nnode;
  569. if (!c->nroot) {
  570. err = ubifs_read_nnode(c, NULL, 0);
  571. if (err)
  572. return ERR_PTR(err);
  573. }
  574. i <<= UBIFS_LPT_FANOUT_SHIFT;
  575. nnode = c->nroot;
  576. shft = c->lpt_hght * UBIFS_LPT_FANOUT_SHIFT;
  577. for (h = 1; h < c->lpt_hght; h++) {
  578. iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
  579. shft -= UBIFS_LPT_FANOUT_SHIFT;
  580. nnode = ubifs_get_nnode(c, nnode, iip);
  581. if (IS_ERR(nnode))
  582. return ERR_PTR(PTR_ERR(nnode));
  583. }
  584. iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
  585. return ubifs_get_pnode(c, nnode, iip);
  586. }
  587. /**
  588. * add_pnode_dirt - add dirty space to LPT LEB properties.
  589. * @c: UBIFS file-system description object
  590. * @pnode: pnode for which to add dirt
  591. */
  592. static void add_pnode_dirt(struct ubifs_info *c, struct ubifs_pnode *pnode)
  593. {
  594. ubifs_add_lpt_dirt(c, pnode->parent->nbranch[pnode->iip].lnum,
  595. c->pnode_sz);
  596. }
  597. /**
  598. * do_make_pnode_dirty - mark a pnode dirty.
  599. * @c: UBIFS file-system description object
  600. * @pnode: pnode to mark dirty
  601. */
  602. static void do_make_pnode_dirty(struct ubifs_info *c, struct ubifs_pnode *pnode)
  603. {
  604. /* Assumes cnext list is empty i.e. not called during commit */
  605. if (!test_and_set_bit(DIRTY_CNODE, &pnode->flags)) {
  606. struct ubifs_nnode *nnode;
  607. c->dirty_pn_cnt += 1;
  608. add_pnode_dirt(c, pnode);
  609. /* Mark parent and ancestors dirty too */
  610. nnode = pnode->parent;
  611. while (nnode) {
  612. if (!test_and_set_bit(DIRTY_CNODE, &nnode->flags)) {
  613. c->dirty_nn_cnt += 1;
  614. ubifs_add_nnode_dirt(c, nnode);
  615. nnode = nnode->parent;
  616. } else
  617. break;
  618. }
  619. }
  620. }
  621. /**
  622. * make_tree_dirty - mark the entire LEB properties tree dirty.
  623. * @c: UBIFS file-system description object
  624. *
  625. * This function is used by the "small" LPT model to cause the entire LEB
  626. * properties tree to be written. The "small" LPT model does not use LPT
  627. * garbage collection because it is more efficient to write the entire tree
  628. * (because it is small).
  629. *
  630. * This function returns %0 on success and a negative error code on failure.
  631. */
  632. static int make_tree_dirty(struct ubifs_info *c)
  633. {
  634. struct ubifs_pnode *pnode;
  635. pnode = pnode_lookup(c, 0);
  636. while (pnode) {
  637. do_make_pnode_dirty(c, pnode);
  638. pnode = next_pnode(c, pnode);
  639. if (IS_ERR(pnode))
  640. return PTR_ERR(pnode);
  641. }
  642. return 0;
  643. }
  644. /**
  645. * need_write_all - determine if the LPT area is running out of free space.
  646. * @c: UBIFS file-system description object
  647. *
  648. * This function returns %1 if the LPT area is running out of free space and %0
  649. * if it is not.
  650. */
  651. static int need_write_all(struct ubifs_info *c)
  652. {
  653. long long free = 0;
  654. int i;
  655. for (i = 0; i < c->lpt_lebs; i++) {
  656. if (i + c->lpt_first == c->nhead_lnum)
  657. free += c->leb_size - c->nhead_offs;
  658. else if (c->ltab[i].free == c->leb_size)
  659. free += c->leb_size;
  660. else if (c->ltab[i].free + c->ltab[i].dirty == c->leb_size)
  661. free += c->leb_size;
  662. }
  663. /* Less than twice the size left */
  664. if (free <= c->lpt_sz * 2)
  665. return 1;
  666. return 0;
  667. }
  668. /**
  669. * lpt_tgc_start - start trivial garbage collection of LPT LEBs.
  670. * @c: UBIFS file-system description object
  671. *
  672. * LPT trivial garbage collection is where a LPT LEB contains only dirty and
  673. * free space and so may be reused as soon as the next commit is completed.
  674. * This function is called during start commit to mark LPT LEBs for trivial GC.
  675. */
  676. static void lpt_tgc_start(struct ubifs_info *c)
  677. {
  678. int i;
  679. for (i = 0; i < c->lpt_lebs; i++) {
  680. if (i + c->lpt_first == c->nhead_lnum)
  681. continue;
  682. if (c->ltab[i].dirty > 0 &&
  683. c->ltab[i].free + c->ltab[i].dirty == c->leb_size) {
  684. c->ltab[i].tgc = 1;
  685. c->ltab[i].free = c->leb_size;
  686. c->ltab[i].dirty = 0;
  687. dbg_lp("LEB %d", i + c->lpt_first);
  688. }
  689. }
  690. }
  691. /**
  692. * lpt_tgc_end - end trivial garbage collection of LPT LEBs.
  693. * @c: UBIFS file-system description object
  694. *
  695. * LPT trivial garbage collection is where a LPT LEB contains only dirty and
  696. * free space and so may be reused as soon as the next commit is completed.
  697. * This function is called after the commit is completed (master node has been
  698. * written) and unmaps LPT LEBs that were marked for trivial GC.
  699. */
  700. static int lpt_tgc_end(struct ubifs_info *c)
  701. {
  702. int i, err;
  703. for (i = 0; i < c->lpt_lebs; i++)
  704. if (c->ltab[i].tgc) {
  705. err = ubifs_leb_unmap(c, i + c->lpt_first);
  706. if (err)
  707. return err;
  708. c->ltab[i].tgc = 0;
  709. dbg_lp("LEB %d", i + c->lpt_first);
  710. }
  711. return 0;
  712. }
  713. /**
  714. * populate_lsave - fill the lsave array with important LEB numbers.
  715. * @c: the UBIFS file-system description object
  716. *
  717. * This function is only called for the "big" model. It records a small number
  718. * of LEB numbers of important LEBs. Important LEBs are ones that are (from
  719. * most important to least important): empty, freeable, freeable index, dirty
  720. * index, dirty or free. Upon mount, we read this list of LEB numbers and bring
  721. * their pnodes into memory. That will stop us from having to scan the LPT
  722. * straight away. For the "small" model we assume that scanning the LPT is no
  723. * big deal.
  724. */
  725. static void populate_lsave(struct ubifs_info *c)
  726. {
  727. struct ubifs_lprops *lprops;
  728. struct ubifs_lpt_heap *heap;
  729. int i, cnt = 0;
  730. ubifs_assert(c->big_lpt);
  731. if (!(c->lpt_drty_flgs & LSAVE_DIRTY)) {
  732. c->lpt_drty_flgs |= LSAVE_DIRTY;
  733. ubifs_add_lpt_dirt(c, c->lsave_lnum, c->lsave_sz);
  734. }
  735. list_for_each_entry(lprops, &c->empty_list, list) {
  736. c->lsave[cnt++] = lprops->lnum;
  737. if (cnt >= c->lsave_cnt)
  738. return;
  739. }
  740. list_for_each_entry(lprops, &c->freeable_list, list) {
  741. c->lsave[cnt++] = lprops->lnum;
  742. if (cnt >= c->lsave_cnt)
  743. return;
  744. }
  745. list_for_each_entry(lprops, &c->frdi_idx_list, list) {
  746. c->lsave[cnt++] = lprops->lnum;
  747. if (cnt >= c->lsave_cnt)
  748. return;
  749. }
  750. heap = &c->lpt_heap[LPROPS_DIRTY_IDX - 1];
  751. for (i = 0; i < heap->cnt; i++) {
  752. c->lsave[cnt++] = heap->arr[i]->lnum;
  753. if (cnt >= c->lsave_cnt)
  754. return;
  755. }
  756. heap = &c->lpt_heap[LPROPS_DIRTY - 1];
  757. for (i = 0; i < heap->cnt; i++) {
  758. c->lsave[cnt++] = heap->arr[i]->lnum;
  759. if (cnt >= c->lsave_cnt)
  760. return;
  761. }
  762. heap = &c->lpt_heap[LPROPS_FREE - 1];
  763. for (i = 0; i < heap->cnt; i++) {
  764. c->lsave[cnt++] = heap->arr[i]->lnum;
  765. if (cnt >= c->lsave_cnt)
  766. return;
  767. }
  768. /* Fill it up completely */
  769. while (cnt < c->lsave_cnt)
  770. c->lsave[cnt++] = c->main_first;
  771. }
  772. /**
  773. * nnode_lookup - lookup a nnode in the LPT.
  774. * @c: UBIFS file-system description object
  775. * @i: nnode number
  776. *
  777. * This function returns a pointer to the nnode on success or a negative
  778. * error code on failure.
  779. */
  780. static struct ubifs_nnode *nnode_lookup(struct ubifs_info *c, int i)
  781. {
  782. int err, iip;
  783. struct ubifs_nnode *nnode;
  784. if (!c->nroot) {
  785. err = ubifs_read_nnode(c, NULL, 0);
  786. if (err)
  787. return ERR_PTR(err);
  788. }
  789. nnode = c->nroot;
  790. while (1) {
  791. iip = i & (UBIFS_LPT_FANOUT - 1);
  792. i >>= UBIFS_LPT_FANOUT_SHIFT;
  793. if (!i)
  794. break;
  795. nnode = ubifs_get_nnode(c, nnode, iip);
  796. if (IS_ERR(nnode))
  797. return nnode;
  798. }
  799. return nnode;
  800. }
  801. /**
  802. * make_nnode_dirty - find a nnode and, if found, make it dirty.
  803. * @c: UBIFS file-system description object
  804. * @node_num: nnode number of nnode to make dirty
  805. * @lnum: LEB number where nnode was written
  806. * @offs: offset where nnode was written
  807. *
  808. * This function is used by LPT garbage collection. LPT garbage collection is
  809. * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
  810. * simply involves marking all the nodes in the LEB being garbage-collected as
  811. * dirty. The dirty nodes are written next commit, after which the LEB is free
  812. * to be reused.
  813. *
  814. * This function returns %0 on success and a negative error code on failure.
  815. */
  816. static int make_nnode_dirty(struct ubifs_info *c, int node_num, int lnum,
  817. int offs)
  818. {
  819. struct ubifs_nnode *nnode;
  820. nnode = nnode_lookup(c, node_num);
  821. if (IS_ERR(nnode))
  822. return PTR_ERR(nnode);
  823. if (nnode->parent) {
  824. struct ubifs_nbranch *branch;
  825. branch = &nnode->parent->nbranch[nnode->iip];
  826. if (branch->lnum != lnum || branch->offs != offs)
  827. return 0; /* nnode is obsolete */
  828. } else if (c->lpt_lnum != lnum || c->lpt_offs != offs)
  829. return 0; /* nnode is obsolete */
  830. /* Assumes cnext list is empty i.e. not called during commit */
  831. if (!test_and_set_bit(DIRTY_CNODE, &nnode->flags)) {
  832. c->dirty_nn_cnt += 1;
  833. ubifs_add_nnode_dirt(c, nnode);
  834. /* Mark parent and ancestors dirty too */
  835. nnode = nnode->parent;
  836. while (nnode) {
  837. if (!test_and_set_bit(DIRTY_CNODE, &nnode->flags)) {
  838. c->dirty_nn_cnt += 1;
  839. ubifs_add_nnode_dirt(c, nnode);
  840. nnode = nnode->parent;
  841. } else
  842. break;
  843. }
  844. }
  845. return 0;
  846. }
  847. /**
  848. * make_pnode_dirty - find a pnode and, if found, make it dirty.
  849. * @c: UBIFS file-system description object
  850. * @node_num: pnode number of pnode to make dirty
  851. * @lnum: LEB number where pnode was written
  852. * @offs: offset where pnode was written
  853. *
  854. * This function is used by LPT garbage collection. LPT garbage collection is
  855. * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
  856. * simply involves marking all the nodes in the LEB being garbage-collected as
  857. * dirty. The dirty nodes are written next commit, after which the LEB is free
  858. * to be reused.
  859. *
  860. * This function returns %0 on success and a negative error code on failure.
  861. */
  862. static int make_pnode_dirty(struct ubifs_info *c, int node_num, int lnum,
  863. int offs)
  864. {
  865. struct ubifs_pnode *pnode;
  866. struct ubifs_nbranch *branch;
  867. pnode = pnode_lookup(c, node_num);
  868. if (IS_ERR(pnode))
  869. return PTR_ERR(pnode);
  870. branch = &pnode->parent->nbranch[pnode->iip];
  871. if (branch->lnum != lnum || branch->offs != offs)
  872. return 0;
  873. do_make_pnode_dirty(c, pnode);
  874. return 0;
  875. }
  876. /**
  877. * make_ltab_dirty - make ltab node dirty.
  878. * @c: UBIFS file-system description object
  879. * @lnum: LEB number where ltab was written
  880. * @offs: offset where ltab was written
  881. *
  882. * This function is used by LPT garbage collection. LPT garbage collection is
  883. * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
  884. * simply involves marking all the nodes in the LEB being garbage-collected as
  885. * dirty. The dirty nodes are written next commit, after which the LEB is free
  886. * to be reused.
  887. *
  888. * This function returns %0 on success and a negative error code on failure.
  889. */
  890. static int make_ltab_dirty(struct ubifs_info *c, int lnum, int offs)
  891. {
  892. if (lnum != c->ltab_lnum || offs != c->ltab_offs)
  893. return 0; /* This ltab node is obsolete */
  894. if (!(c->lpt_drty_flgs & LTAB_DIRTY)) {
  895. c->lpt_drty_flgs |= LTAB_DIRTY;
  896. ubifs_add_lpt_dirt(c, c->ltab_lnum, c->ltab_sz);
  897. }
  898. return 0;
  899. }
  900. /**
  901. * make_lsave_dirty - make lsave node dirty.
  902. * @c: UBIFS file-system description object
  903. * @lnum: LEB number where lsave was written
  904. * @offs: offset where lsave was written
  905. *
  906. * This function is used by LPT garbage collection. LPT garbage collection is
  907. * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
  908. * simply involves marking all the nodes in the LEB being garbage-collected as
  909. * dirty. The dirty nodes are written next commit, after which the LEB is free
  910. * to be reused.
  911. *
  912. * This function returns %0 on success and a negative error code on failure.
  913. */
  914. static int make_lsave_dirty(struct ubifs_info *c, int lnum, int offs)
  915. {
  916. if (lnum != c->lsave_lnum || offs != c->lsave_offs)
  917. return 0; /* This lsave node is obsolete */
  918. if (!(c->lpt_drty_flgs & LSAVE_DIRTY)) {
  919. c->lpt_drty_flgs |= LSAVE_DIRTY;
  920. ubifs_add_lpt_dirt(c, c->lsave_lnum, c->lsave_sz);
  921. }
  922. return 0;
  923. }
  924. /**
  925. * make_node_dirty - make node dirty.
  926. * @c: UBIFS file-system description object
  927. * @node_type: LPT node type
  928. * @node_num: node number
  929. * @lnum: LEB number where node was written
  930. * @offs: offset where node was written
  931. *
  932. * This function is used by LPT garbage collection. LPT garbage collection is
  933. * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
  934. * simply involves marking all the nodes in the LEB being garbage-collected as
  935. * dirty. The dirty nodes are written next commit, after which the LEB is free
  936. * to be reused.
  937. *
  938. * This function returns %0 on success and a negative error code on failure.
  939. */
  940. static int make_node_dirty(struct ubifs_info *c, int node_type, int node_num,
  941. int lnum, int offs)
  942. {
  943. switch (node_type) {
  944. case UBIFS_LPT_NNODE:
  945. return make_nnode_dirty(c, node_num, lnum, offs);
  946. case UBIFS_LPT_PNODE:
  947. return make_pnode_dirty(c, node_num, lnum, offs);
  948. case UBIFS_LPT_LTAB:
  949. return make_ltab_dirty(c, lnum, offs);
  950. case UBIFS_LPT_LSAVE:
  951. return make_lsave_dirty(c, lnum, offs);
  952. }
  953. return -EINVAL;
  954. }
  955. /**
  956. * get_lpt_node_len - return the length of a node based on its type.
  957. * @c: UBIFS file-system description object
  958. * @node_type: LPT node type
  959. */
  960. static int get_lpt_node_len(struct ubifs_info *c, int node_type)
  961. {
  962. switch (node_type) {
  963. case UBIFS_LPT_NNODE:
  964. return c->nnode_sz;
  965. case UBIFS_LPT_PNODE:
  966. return c->pnode_sz;
  967. case UBIFS_LPT_LTAB:
  968. return c->ltab_sz;
  969. case UBIFS_LPT_LSAVE:
  970. return c->lsave_sz;
  971. }
  972. return 0;
  973. }
  974. /**
  975. * get_pad_len - return the length of padding in a buffer.
  976. * @c: UBIFS file-system description object
  977. * @buf: buffer
  978. * @len: length of buffer
  979. */
  980. static int get_pad_len(struct ubifs_info *c, uint8_t *buf, int len)
  981. {
  982. int offs, pad_len;
  983. if (c->min_io_size == 1)
  984. return 0;
  985. offs = c->leb_size - len;
  986. pad_len = ALIGN(offs, c->min_io_size) - offs;
  987. return pad_len;
  988. }
  989. /**
  990. * get_lpt_node_type - return type (and node number) of a node in a buffer.
  991. * @c: UBIFS file-system description object
  992. * @buf: buffer
  993. * @node_num: node number is returned here
  994. */
  995. static int get_lpt_node_type(struct ubifs_info *c, uint8_t *buf, int *node_num)
  996. {
  997. uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
  998. int pos = 0, node_type;
  999. node_type = ubifs_unpack_bits(&addr, &pos, UBIFS_LPT_TYPE_BITS);
  1000. *node_num = ubifs_unpack_bits(&addr, &pos, c->pcnt_bits);
  1001. return node_type;
  1002. }
  1003. /**
  1004. * is_a_node - determine if a buffer contains a node.
  1005. * @c: UBIFS file-system description object
  1006. * @buf: buffer
  1007. * @len: length of buffer
  1008. *
  1009. * This function returns %1 if the buffer contains a node or %0 if it does not.
  1010. */
  1011. static int is_a_node(struct ubifs_info *c, uint8_t *buf, int len)
  1012. {
  1013. uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
  1014. int pos = 0, node_type, node_len;
  1015. uint16_t crc, calc_crc;
  1016. if (len < UBIFS_LPT_CRC_BYTES + (UBIFS_LPT_TYPE_BITS + 7) / 8)
  1017. return 0;
  1018. node_type = ubifs_unpack_bits(&addr, &pos, UBIFS_LPT_TYPE_BITS);
  1019. if (node_type == UBIFS_LPT_NOT_A_NODE)
  1020. return 0;
  1021. node_len = get_lpt_node_len(c, node_type);
  1022. if (!node_len || node_len > len)
  1023. return 0;
  1024. pos = 0;
  1025. addr = buf;
  1026. crc = ubifs_unpack_bits(&addr, &pos, UBIFS_LPT_CRC_BITS);
  1027. calc_crc = crc16(-1, buf + UBIFS_LPT_CRC_BYTES,
  1028. node_len - UBIFS_LPT_CRC_BYTES);
  1029. if (crc != calc_crc)
  1030. return 0;
  1031. return 1;
  1032. }
  1033. /**
  1034. * lpt_gc_lnum - garbage collect a LPT LEB.
  1035. * @c: UBIFS file-system description object
  1036. * @lnum: LEB number to garbage collect
  1037. *
  1038. * LPT garbage collection is used only for the "big" LPT model
  1039. * (c->big_lpt == 1). Garbage collection simply involves marking all the nodes
  1040. * in the LEB being garbage-collected as dirty. The dirty nodes are written
  1041. * next commit, after which the LEB is free to be reused.
  1042. *
  1043. * This function returns %0 on success and a negative error code on failure.
  1044. */
  1045. static int lpt_gc_lnum(struct ubifs_info *c, int lnum)
  1046. {
  1047. int err, len = c->leb_size, node_type, node_num, node_len, offs;
  1048. void *buf = c->lpt_buf;
  1049. dbg_lp("LEB %d", lnum);
  1050. err = ubi_read(c->ubi, lnum, buf, 0, c->leb_size);
  1051. if (err) {
  1052. ubifs_err("cannot read LEB %d, error %d", lnum, err);
  1053. return err;
  1054. }
  1055. while (1) {
  1056. if (!is_a_node(c, buf, len)) {
  1057. int pad_len;
  1058. pad_len = get_pad_len(c, buf, len);
  1059. if (pad_len) {
  1060. buf += pad_len;
  1061. len -= pad_len;
  1062. continue;
  1063. }
  1064. return 0;
  1065. }
  1066. node_type = get_lpt_node_type(c, buf, &node_num);
  1067. node_len = get_lpt_node_len(c, node_type);
  1068. offs = c->leb_size - len;
  1069. ubifs_assert(node_len != 0);
  1070. mutex_lock(&c->lp_mutex);
  1071. err = make_node_dirty(c, node_type, node_num, lnum, offs);
  1072. mutex_unlock(&c->lp_mutex);
  1073. if (err)
  1074. return err;
  1075. buf += node_len;
  1076. len -= node_len;
  1077. }
  1078. return 0;
  1079. }
  1080. /**
  1081. * lpt_gc - LPT garbage collection.
  1082. * @c: UBIFS file-system description object
  1083. *
  1084. * Select a LPT LEB for LPT garbage collection and call 'lpt_gc_lnum()'.
  1085. * Returns %0 on success and a negative error code on failure.
  1086. */
  1087. static int lpt_gc(struct ubifs_info *c)
  1088. {
  1089. int i, lnum = -1, dirty = 0;
  1090. mutex_lock(&c->lp_mutex);
  1091. for (i = 0; i < c->lpt_lebs; i++) {
  1092. ubifs_assert(!c->ltab[i].tgc);
  1093. if (i + c->lpt_first == c->nhead_lnum ||
  1094. c->ltab[i].free + c->ltab[i].dirty == c->leb_size)
  1095. continue;
  1096. if (c->ltab[i].dirty > dirty) {
  1097. dirty = c->ltab[i].dirty;
  1098. lnum = i + c->lpt_first;
  1099. }
  1100. }
  1101. mutex_unlock(&c->lp_mutex);
  1102. if (lnum == -1)
  1103. return -ENOSPC;
  1104. return lpt_gc_lnum(c, lnum);
  1105. }
  1106. /**
  1107. * ubifs_lpt_start_commit - UBIFS commit starts.
  1108. * @c: the UBIFS file-system description object
  1109. *
  1110. * This function has to be called when UBIFS starts the commit operation.
  1111. * This function "freezes" all currently dirty LEB properties and does not
  1112. * change them anymore. Further changes are saved and tracked separately
  1113. * because they are not part of this commit. This function returns zero in case
  1114. * of success and a negative error code in case of failure.
  1115. */
  1116. int ubifs_lpt_start_commit(struct ubifs_info *c)
  1117. {
  1118. int err, cnt;
  1119. dbg_lp("");
  1120. mutex_lock(&c->lp_mutex);
  1121. err = dbg_chk_lpt_free_spc(c);
  1122. if (err)
  1123. goto out;
  1124. err = dbg_check_ltab(c);
  1125. if (err)
  1126. goto out;
  1127. if (c->check_lpt_free) {
  1128. /*
  1129. * We ensure there is enough free space in
  1130. * ubifs_lpt_post_commit() by marking nodes dirty. That
  1131. * information is lost when we unmount, so we also need
  1132. * to check free space once after mounting also.
  1133. */
  1134. c->check_lpt_free = 0;
  1135. while (need_write_all(c)) {
  1136. mutex_unlock(&c->lp_mutex);
  1137. err = lpt_gc(c);
  1138. if (err)
  1139. return err;
  1140. mutex_lock(&c->lp_mutex);
  1141. }
  1142. }
  1143. lpt_tgc_start(c);
  1144. if (!c->dirty_pn_cnt) {
  1145. dbg_cmt("no cnodes to commit");
  1146. err = 0;
  1147. goto out;
  1148. }
  1149. if (!c->big_lpt && need_write_all(c)) {
  1150. /* If needed, write everything */
  1151. err = make_tree_dirty(c);
  1152. if (err)
  1153. goto out;
  1154. lpt_tgc_start(c);
  1155. }
  1156. if (c->big_lpt)
  1157. populate_lsave(c);
  1158. cnt = get_cnodes_to_commit(c);
  1159. ubifs_assert(cnt != 0);
  1160. err = layout_cnodes(c);
  1161. if (err)
  1162. goto out;
  1163. /* Copy the LPT's own lprops for end commit to write */
  1164. memcpy(c->ltab_cmt, c->ltab,
  1165. sizeof(struct ubifs_lpt_lprops) * c->lpt_lebs);
  1166. c->lpt_drty_flgs &= ~(LTAB_DIRTY | LSAVE_DIRTY);
  1167. out:
  1168. mutex_unlock(&c->lp_mutex);
  1169. return err;
  1170. }
  1171. /**
  1172. * free_obsolete_cnodes - free obsolete cnodes for commit end.
  1173. * @c: UBIFS file-system description object
  1174. */
  1175. static void free_obsolete_cnodes(struct ubifs_info *c)
  1176. {
  1177. struct ubifs_cnode *cnode, *cnext;
  1178. cnext = c->lpt_cnext;
  1179. if (!cnext)
  1180. return;
  1181. do {
  1182. cnode = cnext;
  1183. cnext = cnode->cnext;
  1184. if (test_bit(OBSOLETE_CNODE, &cnode->flags))
  1185. kfree(cnode);
  1186. else
  1187. cnode->cnext = NULL;
  1188. } while (cnext != c->lpt_cnext);
  1189. c->lpt_cnext = NULL;
  1190. }
  1191. /**
  1192. * ubifs_lpt_end_commit - finish the commit operation.
  1193. * @c: the UBIFS file-system description object
  1194. *
  1195. * This function has to be called when the commit operation finishes. It
  1196. * flushes the changes which were "frozen" by 'ubifs_lprops_start_commit()' to
  1197. * the media. Returns zero in case of success and a negative error code in case
  1198. * of failure.
  1199. */
  1200. int ubifs_lpt_end_commit(struct ubifs_info *c)
  1201. {
  1202. int err;
  1203. dbg_lp("");
  1204. if (!c->lpt_cnext)
  1205. return 0;
  1206. err = write_cnodes(c);
  1207. if (err)
  1208. return err;
  1209. mutex_lock(&c->lp_mutex);
  1210. free_obsolete_cnodes(c);
  1211. mutex_unlock(&c->lp_mutex);
  1212. return 0;
  1213. }
  1214. /**
  1215. * ubifs_lpt_post_commit - post commit LPT trivial GC and LPT GC.
  1216. * @c: UBIFS file-system description object
  1217. *
  1218. * LPT trivial GC is completed after a commit. Also LPT GC is done after a
  1219. * commit for the "big" LPT model.
  1220. */
  1221. int ubifs_lpt_post_commit(struct ubifs_info *c)
  1222. {
  1223. int err;
  1224. mutex_lock(&c->lp_mutex);
  1225. err = lpt_tgc_end(c);
  1226. if (err)
  1227. goto out;
  1228. if (c->big_lpt)
  1229. while (need_write_all(c)) {
  1230. mutex_unlock(&c->lp_mutex);
  1231. err = lpt_gc(c);
  1232. if (err)
  1233. return err;
  1234. mutex_lock(&c->lp_mutex);
  1235. }
  1236. out:
  1237. mutex_unlock(&c->lp_mutex);
  1238. return err;
  1239. }
  1240. /**
  1241. * first_nnode - find the first nnode in memory.
  1242. * @c: UBIFS file-system description object
  1243. * @hght: height of tree where nnode found is returned here
  1244. *
  1245. * This function returns a pointer to the nnode found or %NULL if no nnode is
  1246. * found. This function is a helper to 'ubifs_lpt_free()'.
  1247. */
  1248. static struct ubifs_nnode *first_nnode(struct ubifs_info *c, int *hght)
  1249. {
  1250. struct ubifs_nnode *nnode;
  1251. int h, i, found;
  1252. nnode = c->nroot;
  1253. *hght = 0;
  1254. if (!nnode)
  1255. return NULL;
  1256. for (h = 1; h < c->lpt_hght; h++) {
  1257. found = 0;
  1258. for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
  1259. if (nnode->nbranch[i].nnode) {
  1260. found = 1;
  1261. nnode = nnode->nbranch[i].nnode;
  1262. *hght = h;
  1263. break;
  1264. }
  1265. }
  1266. if (!found)
  1267. break;
  1268. }
  1269. return nnode;
  1270. }
  1271. /**
  1272. * next_nnode - find the next nnode in memory.
  1273. * @c: UBIFS file-system description object
  1274. * @nnode: nnode from which to start.
  1275. * @hght: height of tree where nnode is, is passed and returned here
  1276. *
  1277. * This function returns a pointer to the nnode found or %NULL if no nnode is
  1278. * found. This function is a helper to 'ubifs_lpt_free()'.
  1279. */
  1280. static struct ubifs_nnode *next_nnode(struct ubifs_info *c,
  1281. struct ubifs_nnode *nnode, int *hght)
  1282. {
  1283. struct ubifs_nnode *parent;
  1284. int iip, h, i, found;
  1285. parent = nnode->parent;
  1286. if (!parent)
  1287. return NULL;
  1288. if (nnode->iip == UBIFS_LPT_FANOUT - 1) {
  1289. *hght -= 1;
  1290. return parent;
  1291. }
  1292. for (iip = nnode->iip + 1; iip < UBIFS_LPT_FANOUT; iip++) {
  1293. nnode = parent->nbranch[iip].nnode;
  1294. if (nnode)
  1295. break;
  1296. }
  1297. if (!nnode) {
  1298. *hght -= 1;
  1299. return parent;
  1300. }
  1301. for (h = *hght + 1; h < c->lpt_hght; h++) {
  1302. found = 0;
  1303. for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
  1304. if (nnode->nbranch[i].nnode) {
  1305. found = 1;
  1306. nnode = nnode->nbranch[i].nnode;
  1307. *hght = h;
  1308. break;
  1309. }
  1310. }
  1311. if (!found)
  1312. break;
  1313. }
  1314. return nnode;
  1315. }
  1316. /**
  1317. * ubifs_lpt_free - free resources owned by the LPT.
  1318. * @c: UBIFS file-system description object
  1319. * @wr_only: free only resources used for writing
  1320. */
  1321. void ubifs_lpt_free(struct ubifs_info *c, int wr_only)
  1322. {
  1323. struct ubifs_nnode *nnode;
  1324. int i, hght;
  1325. /* Free write-only things first */
  1326. free_obsolete_cnodes(c); /* Leftover from a failed commit */
  1327. vfree(c->ltab_cmt);
  1328. c->ltab_cmt = NULL;
  1329. vfree(c->lpt_buf);
  1330. c->lpt_buf = NULL;
  1331. kfree(c->lsave);
  1332. c->lsave = NULL;
  1333. if (wr_only)
  1334. return;
  1335. /* Now free the rest */
  1336. nnode = first_nnode(c, &hght);
  1337. while (nnode) {
  1338. for (i = 0; i < UBIFS_LPT_FANOUT; i++)
  1339. kfree(nnode->nbranch[i].nnode);
  1340. nnode = next_nnode(c, nnode, &hght);
  1341. }
  1342. for (i = 0; i < LPROPS_HEAP_CNT; i++)
  1343. kfree(c->lpt_heap[i].arr);
  1344. kfree(c->dirty_idx.arr);
  1345. kfree(c->nroot);
  1346. vfree(c->ltab);
  1347. kfree(c->lpt_nod_buf);
  1348. }
  1349. #ifdef CONFIG_UBIFS_FS_DEBUG
  1350. /**
  1351. * dbg_is_all_ff - determine if a buffer contains only 0xff bytes.
  1352. * @buf: buffer
  1353. * @len: buffer length
  1354. */
  1355. static int dbg_is_all_ff(uint8_t *buf, int len)
  1356. {
  1357. int i;
  1358. for (i = 0; i < len; i++)
  1359. if (buf[i] != 0xff)
  1360. return 0;
  1361. return 1;
  1362. }
  1363. /**
  1364. * dbg_is_nnode_dirty - determine if a nnode is dirty.
  1365. * @c: the UBIFS file-system description object
  1366. * @lnum: LEB number where nnode was written
  1367. * @offs: offset where nnode was written
  1368. */
  1369. static int dbg_is_nnode_dirty(struct ubifs_info *c, int lnum, int offs)
  1370. {
  1371. struct ubifs_nnode *nnode;
  1372. int hght;
  1373. /* Entire tree is in memory so first_nnode / next_nnode are ok */
  1374. nnode = first_nnode(c, &hght);
  1375. for (; nnode; nnode = next_nnode(c, nnode, &hght)) {
  1376. struct ubifs_nbranch *branch;
  1377. cond_resched();
  1378. if (nnode->parent) {
  1379. branch = &nnode->parent->nbranch[nnode->iip];
  1380. if (branch->lnum != lnum || branch->offs != offs)
  1381. continue;
  1382. if (test_bit(DIRTY_CNODE, &nnode->flags))
  1383. return 1;
  1384. return 0;
  1385. } else {
  1386. if (c->lpt_lnum != lnum || c->lpt_offs != offs)
  1387. continue;
  1388. if (test_bit(DIRTY_CNODE, &nnode->flags))
  1389. return 1;
  1390. return 0;
  1391. }
  1392. }
  1393. return 1;
  1394. }
  1395. /**
  1396. * dbg_is_pnode_dirty - determine if a pnode is dirty.
  1397. * @c: the UBIFS file-system description object
  1398. * @lnum: LEB number where pnode was written
  1399. * @offs: offset where pnode was written
  1400. */
  1401. static int dbg_is_pnode_dirty(struct ubifs_info *c, int lnum, int offs)
  1402. {
  1403. int i, cnt;
  1404. cnt = DIV_ROUND_UP(c->main_lebs, UBIFS_LPT_FANOUT);
  1405. for (i = 0; i < cnt; i++) {
  1406. struct ubifs_pnode *pnode;
  1407. struct ubifs_nbranch *branch;
  1408. cond_resched();
  1409. pnode = pnode_lookup(c, i);
  1410. if (IS_ERR(pnode))
  1411. return PTR_ERR(pnode);
  1412. branch = &pnode->parent->nbranch[pnode->iip];
  1413. if (branch->lnum != lnum || branch->offs != offs)
  1414. continue;
  1415. if (test_bit(DIRTY_CNODE, &pnode->flags))
  1416. return 1;
  1417. return 0;
  1418. }
  1419. return 1;
  1420. }
  1421. /**
  1422. * dbg_is_ltab_dirty - determine if a ltab node is dirty.
  1423. * @c: the UBIFS file-system description object
  1424. * @lnum: LEB number where ltab node was written
  1425. * @offs: offset where ltab node was written
  1426. */
  1427. static int dbg_is_ltab_dirty(struct ubifs_info *c, int lnum, int offs)
  1428. {
  1429. if (lnum != c->ltab_lnum || offs != c->ltab_offs)
  1430. return 1;
  1431. return (c->lpt_drty_flgs & LTAB_DIRTY) != 0;
  1432. }
  1433. /**
  1434. * dbg_is_lsave_dirty - determine if a lsave node is dirty.
  1435. * @c: the UBIFS file-system description object
  1436. * @lnum: LEB number where lsave node was written
  1437. * @offs: offset where lsave node was written
  1438. */
  1439. static int dbg_is_lsave_dirty(struct ubifs_info *c, int lnum, int offs)
  1440. {
  1441. if (lnum != c->lsave_lnum || offs != c->lsave_offs)
  1442. return 1;
  1443. return (c->lpt_drty_flgs & LSAVE_DIRTY) != 0;
  1444. }
  1445. /**
  1446. * dbg_is_node_dirty - determine if a node is dirty.
  1447. * @c: the UBIFS file-system description object
  1448. * @node_type: node type
  1449. * @lnum: LEB number where node was written
  1450. * @offs: offset where node was written
  1451. */
  1452. static int dbg_is_node_dirty(struct ubifs_info *c, int node_type, int lnum,
  1453. int offs)
  1454. {
  1455. switch (node_type) {
  1456. case UBIFS_LPT_NNODE:
  1457. return dbg_is_nnode_dirty(c, lnum, offs);
  1458. case UBIFS_LPT_PNODE:
  1459. return dbg_is_pnode_dirty(c, lnum, offs);
  1460. case UBIFS_LPT_LTAB:
  1461. return dbg_is_ltab_dirty(c, lnum, offs);
  1462. case UBIFS_LPT_LSAVE:
  1463. return dbg_is_lsave_dirty(c, lnum, offs);
  1464. }
  1465. return 1;
  1466. }
  1467. /**
  1468. * dbg_check_ltab_lnum - check the ltab for a LPT LEB number.
  1469. * @c: the UBIFS file-system description object
  1470. * @lnum: LEB number where node was written
  1471. * @offs: offset where node was written
  1472. *
  1473. * This function returns %0 on success and a negative error code on failure.
  1474. */
  1475. static int dbg_check_ltab_lnum(struct ubifs_info *c, int lnum)
  1476. {
  1477. int err, len = c->leb_size, dirty = 0, node_type, node_num, node_len;
  1478. int ret;
  1479. void *buf = c->dbg_buf;
  1480. dbg_lp("LEB %d", lnum);
  1481. err = ubi_read(c->ubi, lnum, buf, 0, c->leb_size);
  1482. if (err) {
  1483. dbg_msg("ubi_read failed, LEB %d, error %d", lnum, err);
  1484. return err;
  1485. }
  1486. while (1) {
  1487. if (!is_a_node(c, buf, len)) {
  1488. int i, pad_len;
  1489. pad_len = get_pad_len(c, buf, len);
  1490. if (pad_len) {
  1491. buf += pad_len;
  1492. len -= pad_len;
  1493. dirty += pad_len;
  1494. continue;
  1495. }
  1496. if (!dbg_is_all_ff(buf, len)) {
  1497. dbg_msg("invalid empty space in LEB %d at %d",
  1498. lnum, c->leb_size - len);
  1499. err = -EINVAL;
  1500. }
  1501. i = lnum - c->lpt_first;
  1502. if (len != c->ltab[i].free) {
  1503. dbg_msg("invalid free space in LEB %d "
  1504. "(free %d, expected %d)",
  1505. lnum, len, c->ltab[i].free);
  1506. err = -EINVAL;
  1507. }
  1508. if (dirty != c->ltab[i].dirty) {
  1509. dbg_msg("invalid dirty space in LEB %d "
  1510. "(dirty %d, expected %d)",
  1511. lnum, dirty, c->ltab[i].dirty);
  1512. err = -EINVAL;
  1513. }
  1514. return err;
  1515. }
  1516. node_type = get_lpt_node_type(c, buf, &node_num);
  1517. node_len = get_lpt_node_len(c, node_type);
  1518. ret = dbg_is_node_dirty(c, node_type, lnum, c->leb_size - len);
  1519. if (ret == 1)
  1520. dirty += node_len;
  1521. buf += node_len;
  1522. len -= node_len;
  1523. }
  1524. }
  1525. /**
  1526. * dbg_check_ltab - check the free and dirty space in the ltab.
  1527. * @c: the UBIFS file-system description object
  1528. *
  1529. * This function returns %0 on success and a negative error code on failure.
  1530. */
  1531. int dbg_check_ltab(struct ubifs_info *c)
  1532. {
  1533. int lnum, err, i, cnt;
  1534. if (!(ubifs_chk_flags & UBIFS_CHK_LPROPS))
  1535. return 0;
  1536. /* Bring the entire tree into memory */
  1537. cnt = DIV_ROUND_UP(c->main_lebs, UBIFS_LPT_FANOUT);
  1538. for (i = 0; i < cnt; i++) {
  1539. struct ubifs_pnode *pnode;
  1540. pnode = pnode_lookup(c, i);
  1541. if (IS_ERR(pnode))
  1542. return PTR_ERR(pnode);
  1543. cond_resched();
  1544. }
  1545. /* Check nodes */
  1546. err = dbg_check_lpt_nodes(c, (struct ubifs_cnode *)c->nroot, 0, 0);
  1547. if (err)
  1548. return err;
  1549. /* Check each LEB */
  1550. for (lnum = c->lpt_first; lnum <= c->lpt_last; lnum++) {
  1551. err = dbg_check_ltab_lnum(c, lnum);
  1552. if (err) {
  1553. dbg_err("failed at LEB %d", lnum);
  1554. return err;
  1555. }
  1556. }
  1557. dbg_lp("succeeded");
  1558. return 0;
  1559. }
  1560. /**
  1561. * dbg_chk_lpt_free_spc - check LPT free space is enough to write entire LPT.
  1562. * @c: the UBIFS file-system description object
  1563. *
  1564. * This function returns %0 on success and a negative error code on failure.
  1565. */
  1566. int dbg_chk_lpt_free_spc(struct ubifs_info *c)
  1567. {
  1568. long long free = 0;
  1569. int i;
  1570. for (i = 0; i < c->lpt_lebs; i++) {
  1571. if (c->ltab[i].tgc || c->ltab[i].cmt)
  1572. continue;
  1573. if (i + c->lpt_first == c->nhead_lnum)
  1574. free += c->leb_size - c->nhead_offs;
  1575. else if (c->ltab[i].free == c->leb_size)
  1576. free += c->leb_size;
  1577. }
  1578. if (free < c->lpt_sz) {
  1579. dbg_err("LPT space error: free %lld lpt_sz %lld",
  1580. free, c->lpt_sz);
  1581. dbg_dump_lpt_info(c);
  1582. return -EINVAL;
  1583. }
  1584. return 0;
  1585. }
  1586. /**
  1587. * dbg_chk_lpt_sz - check LPT does not write more than LPT size.
  1588. * @c: the UBIFS file-system description object
  1589. * @action: action
  1590. * @len: length written
  1591. *
  1592. * This function returns %0 on success and a negative error code on failure.
  1593. */
  1594. int dbg_chk_lpt_sz(struct ubifs_info *c, int action, int len)
  1595. {
  1596. long long chk_lpt_sz, lpt_sz;
  1597. int err = 0;
  1598. switch (action) {
  1599. case 0:
  1600. c->chk_lpt_sz = 0;
  1601. c->chk_lpt_sz2 = 0;
  1602. c->chk_lpt_lebs = 0;
  1603. c->chk_lpt_wastage = 0;
  1604. if (c->dirty_pn_cnt > c->pnode_cnt) {
  1605. dbg_err("dirty pnodes %d exceed max %d",
  1606. c->dirty_pn_cnt, c->pnode_cnt);
  1607. err = -EINVAL;
  1608. }
  1609. if (c->dirty_nn_cnt > c->nnode_cnt) {
  1610. dbg_err("dirty nnodes %d exceed max %d",
  1611. c->dirty_nn_cnt, c->nnode_cnt);
  1612. err = -EINVAL;
  1613. }
  1614. return err;
  1615. case 1:
  1616. c->chk_lpt_sz += len;
  1617. return 0;
  1618. case 2:
  1619. c->chk_lpt_sz += len;
  1620. c->chk_lpt_wastage += len;
  1621. c->chk_lpt_lebs += 1;
  1622. return 0;
  1623. case 3:
  1624. chk_lpt_sz = c->leb_size;
  1625. chk_lpt_sz *= c->chk_lpt_lebs;
  1626. chk_lpt_sz += len - c->nhead_offs;
  1627. if (c->chk_lpt_sz != chk_lpt_sz) {
  1628. dbg_err("LPT wrote %lld but space used was %lld",
  1629. c->chk_lpt_sz, chk_lpt_sz);
  1630. err = -EINVAL;
  1631. }
  1632. if (c->chk_lpt_sz > c->lpt_sz) {
  1633. dbg_err("LPT wrote %lld but lpt_sz is %lld",
  1634. c->chk_lpt_sz, c->lpt_sz);
  1635. err = -EINVAL;
  1636. }
  1637. if (c->chk_lpt_sz2 && c->chk_lpt_sz != c->chk_lpt_sz2) {
  1638. dbg_err("LPT layout size %lld but wrote %lld",
  1639. c->chk_lpt_sz, c->chk_lpt_sz2);
  1640. err = -EINVAL;
  1641. }
  1642. if (c->chk_lpt_sz2 && c->new_nhead_offs != len) {
  1643. dbg_err("LPT new nhead offs: expected %d was %d",
  1644. c->new_nhead_offs, len);
  1645. err = -EINVAL;
  1646. }
  1647. lpt_sz = (long long)c->pnode_cnt * c->pnode_sz;
  1648. lpt_sz += (long long)c->nnode_cnt * c->nnode_sz;
  1649. lpt_sz += c->ltab_sz;
  1650. if (c->big_lpt)
  1651. lpt_sz += c->lsave_sz;
  1652. if (c->chk_lpt_sz - c->chk_lpt_wastage > lpt_sz) {
  1653. dbg_err("LPT chk_lpt_sz %lld + waste %lld exceeds %lld",
  1654. c->chk_lpt_sz, c->chk_lpt_wastage, lpt_sz);
  1655. err = -EINVAL;
  1656. }
  1657. if (err)
  1658. dbg_dump_lpt_info(c);
  1659. c->chk_lpt_sz2 = c->chk_lpt_sz;
  1660. c->chk_lpt_sz = 0;
  1661. c->chk_lpt_wastage = 0;
  1662. c->chk_lpt_lebs = 0;
  1663. c->new_nhead_offs = len;
  1664. return err;
  1665. case 4:
  1666. c->chk_lpt_sz += len;
  1667. c->chk_lpt_wastage += len;
  1668. return 0;
  1669. default:
  1670. return -EINVAL;
  1671. }
  1672. }
  1673. #endif /* CONFIG_UBIFS_FS_DEBUG */