lpt_commit.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813
  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. long long chk_lpt_sz, lpt_sz;
  1595. int err = 0;
  1596. switch (action) {
  1597. case 0:
  1598. c->chk_lpt_sz = 0;
  1599. c->chk_lpt_sz2 = 0;
  1600. c->chk_lpt_lebs = 0;
  1601. c->chk_lpt_wastage = 0;
  1602. if (c->dirty_pn_cnt > c->pnode_cnt) {
  1603. dbg_err("dirty pnodes %d exceed max %d",
  1604. c->dirty_pn_cnt, c->pnode_cnt);
  1605. err = -EINVAL;
  1606. }
  1607. if (c->dirty_nn_cnt > c->nnode_cnt) {
  1608. dbg_err("dirty nnodes %d exceed max %d",
  1609. c->dirty_nn_cnt, c->nnode_cnt);
  1610. err = -EINVAL;
  1611. }
  1612. return err;
  1613. case 1:
  1614. c->chk_lpt_sz += len;
  1615. return 0;
  1616. case 2:
  1617. c->chk_lpt_sz += len;
  1618. c->chk_lpt_wastage += len;
  1619. c->chk_lpt_lebs += 1;
  1620. return 0;
  1621. case 3:
  1622. chk_lpt_sz = c->leb_size;
  1623. chk_lpt_sz *= c->chk_lpt_lebs;
  1624. chk_lpt_sz += len - c->nhead_offs;
  1625. if (c->chk_lpt_sz != chk_lpt_sz) {
  1626. dbg_err("LPT wrote %lld but space used was %lld",
  1627. c->chk_lpt_sz, chk_lpt_sz);
  1628. err = -EINVAL;
  1629. }
  1630. if (c->chk_lpt_sz > c->lpt_sz) {
  1631. dbg_err("LPT wrote %lld but lpt_sz is %lld",
  1632. c->chk_lpt_sz, c->lpt_sz);
  1633. err = -EINVAL;
  1634. }
  1635. if (c->chk_lpt_sz2 && c->chk_lpt_sz != c->chk_lpt_sz2) {
  1636. dbg_err("LPT layout size %lld but wrote %lld",
  1637. c->chk_lpt_sz, c->chk_lpt_sz2);
  1638. err = -EINVAL;
  1639. }
  1640. if (c->chk_lpt_sz2 && c->new_nhead_offs != len) {
  1641. dbg_err("LPT new nhead offs: expected %d was %d",
  1642. c->new_nhead_offs, len);
  1643. err = -EINVAL;
  1644. }
  1645. lpt_sz = (long long)c->pnode_cnt * c->pnode_sz;
  1646. lpt_sz += (long long)c->nnode_cnt * c->nnode_sz;
  1647. lpt_sz += c->ltab_sz;
  1648. if (c->big_lpt)
  1649. lpt_sz += c->lsave_sz;
  1650. if (c->chk_lpt_sz - c->chk_lpt_wastage > lpt_sz) {
  1651. dbg_err("LPT chk_lpt_sz %lld + waste %lld exceeds %lld",
  1652. c->chk_lpt_sz, c->chk_lpt_wastage, lpt_sz);
  1653. err = -EINVAL;
  1654. }
  1655. if (err)
  1656. dbg_dump_lpt_info(c);
  1657. c->chk_lpt_sz2 = c->chk_lpt_sz;
  1658. c->chk_lpt_sz = 0;
  1659. c->chk_lpt_wastage = 0;
  1660. c->chk_lpt_lebs = 0;
  1661. c->new_nhead_offs = len;
  1662. return err;
  1663. case 4:
  1664. c->chk_lpt_sz += len;
  1665. c->chk_lpt_wastage += len;
  1666. return 0;
  1667. default:
  1668. return -EINVAL;
  1669. }
  1670. }
  1671. #endif /* CONFIG_UBIFS_FS_DEBUG */