lpt_commit.c 50 KB

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