lpt_commit.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038
  1. /*
  2. * This file is part of UBIFS.
  3. *
  4. * Copyright (C) 2006-2008 Nokia Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License version 2 as published by
  8. * the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with
  16. * this program; if not, write to the Free Software Foundation, Inc., 51
  17. * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  18. *
  19. * Authors: Adrian Hunter
  20. * Artem Bityutskiy (Битюцкий Артём)
  21. */
  22. /*
  23. * This file implements commit-related functionality of the LEB properties
  24. * subsystem.
  25. */
  26. #include <linux/crc16.h>
  27. #include <linux/slab.h>
  28. #include <linux/random.h>
  29. #include "ubifs.h"
  30. static int dbg_populate_lsave(struct ubifs_info *c);
  31. /**
  32. * first_dirty_cnode - find first dirty cnode.
  33. * @c: UBIFS file-system description object
  34. * @nnode: nnode at which to start
  35. *
  36. * This function returns the first dirty cnode or %NULL if there is not one.
  37. */
  38. static struct ubifs_cnode *first_dirty_cnode(const struct ubifs_info *c, struct ubifs_nnode *nnode)
  39. {
  40. ubifs_assert(c, nnode);
  41. while (1) {
  42. int i, cont = 0;
  43. for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
  44. struct ubifs_cnode *cnode;
  45. cnode = nnode->nbranch[i].cnode;
  46. if (cnode &&
  47. test_bit(DIRTY_CNODE, &cnode->flags)) {
  48. if (cnode->level == 0)
  49. return cnode;
  50. nnode = (struct ubifs_nnode *)cnode;
  51. cont = 1;
  52. break;
  53. }
  54. }
  55. if (!cont)
  56. return (struct ubifs_cnode *)nnode;
  57. }
  58. }
  59. /**
  60. * next_dirty_cnode - find next dirty cnode.
  61. * @c: UBIFS file-system description object
  62. * @cnode: cnode from which to begin searching
  63. *
  64. * This function returns the next dirty cnode or %NULL if there is not one.
  65. */
  66. static struct ubifs_cnode *next_dirty_cnode(const struct ubifs_info *c, struct ubifs_cnode *cnode)
  67. {
  68. struct ubifs_nnode *nnode;
  69. int i;
  70. ubifs_assert(c, cnode);
  71. nnode = cnode->parent;
  72. if (!nnode)
  73. return NULL;
  74. for (i = cnode->iip + 1; i < UBIFS_LPT_FANOUT; i++) {
  75. cnode = nnode->nbranch[i].cnode;
  76. if (cnode && test_bit(DIRTY_CNODE, &cnode->flags)) {
  77. if (cnode->level == 0)
  78. return cnode; /* cnode is a pnode */
  79. /* cnode is a nnode */
  80. return first_dirty_cnode(c, (struct ubifs_nnode *)cnode);
  81. }
  82. }
  83. return (struct ubifs_cnode *)nnode;
  84. }
  85. /**
  86. * get_cnodes_to_commit - create list of dirty cnodes to commit.
  87. * @c: UBIFS file-system description object
  88. *
  89. * This function returns the number of cnodes to commit.
  90. */
  91. static int get_cnodes_to_commit(struct ubifs_info *c)
  92. {
  93. struct ubifs_cnode *cnode, *cnext;
  94. int cnt = 0;
  95. if (!c->nroot)
  96. return 0;
  97. if (!test_bit(DIRTY_CNODE, &c->nroot->flags))
  98. return 0;
  99. c->lpt_cnext = first_dirty_cnode(c, c->nroot);
  100. cnode = c->lpt_cnext;
  101. if (!cnode)
  102. return 0;
  103. cnt += 1;
  104. while (1) {
  105. ubifs_assert(c, !test_bit(COW_CNODE, &cnode->flags));
  106. __set_bit(COW_CNODE, &cnode->flags);
  107. cnext = next_dirty_cnode(c, cnode);
  108. if (!cnext) {
  109. cnode->cnext = c->lpt_cnext;
  110. break;
  111. }
  112. cnode->cnext = cnext;
  113. cnode = cnext;
  114. cnt += 1;
  115. }
  116. dbg_cmt("committing %d cnodes", cnt);
  117. dbg_lp("committing %d cnodes", cnt);
  118. ubifs_assert(c, cnt == c->dirty_nn_cnt + c->dirty_pn_cnt);
  119. return cnt;
  120. }
  121. /**
  122. * upd_ltab - update LPT LEB properties.
  123. * @c: UBIFS file-system description object
  124. * @lnum: LEB number
  125. * @free: amount of free space
  126. * @dirty: amount of dirty space to add
  127. */
  128. static void upd_ltab(struct ubifs_info *c, int lnum, int free, int dirty)
  129. {
  130. dbg_lp("LEB %d free %d dirty %d to %d +%d",
  131. lnum, c->ltab[lnum - c->lpt_first].free,
  132. c->ltab[lnum - c->lpt_first].dirty, free, dirty);
  133. ubifs_assert(c, lnum >= c->lpt_first && lnum <= c->lpt_last);
  134. c->ltab[lnum - c->lpt_first].free = free;
  135. c->ltab[lnum - c->lpt_first].dirty += dirty;
  136. }
  137. /**
  138. * alloc_lpt_leb - allocate an LPT LEB that is empty.
  139. * @c: UBIFS file-system description object
  140. * @lnum: LEB number is passed and returned here
  141. *
  142. * This function finds the next empty LEB in the ltab starting from @lnum. If a
  143. * an empty LEB is found it is returned in @lnum and the function returns %0.
  144. * Otherwise the function returns -ENOSPC. Note however, that LPT is designed
  145. * never to run out of space.
  146. */
  147. static int alloc_lpt_leb(struct ubifs_info *c, int *lnum)
  148. {
  149. int i, n;
  150. n = *lnum - c->lpt_first + 1;
  151. for (i = n; i < c->lpt_lebs; i++) {
  152. if (c->ltab[i].tgc || c->ltab[i].cmt)
  153. continue;
  154. if (c->ltab[i].free == c->leb_size) {
  155. c->ltab[i].cmt = 1;
  156. *lnum = i + c->lpt_first;
  157. return 0;
  158. }
  159. }
  160. for (i = 0; i < n; i++) {
  161. if (c->ltab[i].tgc || c->ltab[i].cmt)
  162. continue;
  163. if (c->ltab[i].free == c->leb_size) {
  164. c->ltab[i].cmt = 1;
  165. *lnum = i + c->lpt_first;
  166. return 0;
  167. }
  168. }
  169. return -ENOSPC;
  170. }
  171. /**
  172. * layout_cnodes - layout cnodes for commit.
  173. * @c: UBIFS file-system description object
  174. *
  175. * This function returns %0 on success and a negative error code on failure.
  176. */
  177. static int layout_cnodes(struct ubifs_info *c)
  178. {
  179. int lnum, offs, len, alen, done_lsave, done_ltab, err;
  180. struct ubifs_cnode *cnode;
  181. err = dbg_chk_lpt_sz(c, 0, 0);
  182. if (err)
  183. return err;
  184. cnode = c->lpt_cnext;
  185. if (!cnode)
  186. return 0;
  187. lnum = c->nhead_lnum;
  188. offs = c->nhead_offs;
  189. /* Try to place lsave and ltab nicely */
  190. done_lsave = !c->big_lpt;
  191. done_ltab = 0;
  192. if (!done_lsave && offs + c->lsave_sz <= c->leb_size) {
  193. done_lsave = 1;
  194. c->lsave_lnum = lnum;
  195. c->lsave_offs = offs;
  196. offs += c->lsave_sz;
  197. dbg_chk_lpt_sz(c, 1, c->lsave_sz);
  198. }
  199. if (offs + c->ltab_sz <= c->leb_size) {
  200. done_ltab = 1;
  201. c->ltab_lnum = lnum;
  202. c->ltab_offs = offs;
  203. offs += c->ltab_sz;
  204. dbg_chk_lpt_sz(c, 1, c->ltab_sz);
  205. }
  206. do {
  207. if (cnode->level) {
  208. len = c->nnode_sz;
  209. c->dirty_nn_cnt -= 1;
  210. } else {
  211. len = c->pnode_sz;
  212. c->dirty_pn_cnt -= 1;
  213. }
  214. while (offs + len > c->leb_size) {
  215. alen = ALIGN(offs, c->min_io_size);
  216. upd_ltab(c, lnum, c->leb_size - alen, alen - offs);
  217. dbg_chk_lpt_sz(c, 2, c->leb_size - offs);
  218. err = alloc_lpt_leb(c, &lnum);
  219. if (err)
  220. goto no_space;
  221. offs = 0;
  222. ubifs_assert(c, lnum >= c->lpt_first &&
  223. lnum <= c->lpt_last);
  224. /* Try to place lsave and ltab nicely */
  225. if (!done_lsave) {
  226. done_lsave = 1;
  227. c->lsave_lnum = lnum;
  228. c->lsave_offs = offs;
  229. offs += c->lsave_sz;
  230. dbg_chk_lpt_sz(c, 1, c->lsave_sz);
  231. continue;
  232. }
  233. if (!done_ltab) {
  234. done_ltab = 1;
  235. c->ltab_lnum = lnum;
  236. c->ltab_offs = offs;
  237. offs += c->ltab_sz;
  238. dbg_chk_lpt_sz(c, 1, c->ltab_sz);
  239. continue;
  240. }
  241. break;
  242. }
  243. if (cnode->parent) {
  244. cnode->parent->nbranch[cnode->iip].lnum = lnum;
  245. cnode->parent->nbranch[cnode->iip].offs = offs;
  246. } else {
  247. c->lpt_lnum = lnum;
  248. c->lpt_offs = offs;
  249. }
  250. offs += len;
  251. dbg_chk_lpt_sz(c, 1, len);
  252. cnode = cnode->cnext;
  253. } while (cnode && cnode != c->lpt_cnext);
  254. /* Make sure to place LPT's save table */
  255. if (!done_lsave) {
  256. if (offs + c->lsave_sz > c->leb_size) {
  257. alen = ALIGN(offs, c->min_io_size);
  258. upd_ltab(c, lnum, c->leb_size - alen, alen - offs);
  259. dbg_chk_lpt_sz(c, 2, c->leb_size - offs);
  260. err = alloc_lpt_leb(c, &lnum);
  261. if (err)
  262. goto no_space;
  263. offs = 0;
  264. ubifs_assert(c, lnum >= c->lpt_first &&
  265. lnum <= c->lpt_last);
  266. }
  267. done_lsave = 1;
  268. c->lsave_lnum = lnum;
  269. c->lsave_offs = offs;
  270. offs += c->lsave_sz;
  271. dbg_chk_lpt_sz(c, 1, c->lsave_sz);
  272. }
  273. /* Make sure to place LPT's own lprops table */
  274. if (!done_ltab) {
  275. if (offs + c->ltab_sz > c->leb_size) {
  276. alen = ALIGN(offs, c->min_io_size);
  277. upd_ltab(c, lnum, c->leb_size - alen, alen - offs);
  278. dbg_chk_lpt_sz(c, 2, c->leb_size - offs);
  279. err = alloc_lpt_leb(c, &lnum);
  280. if (err)
  281. goto no_space;
  282. offs = 0;
  283. ubifs_assert(c, lnum >= c->lpt_first &&
  284. lnum <= c->lpt_last);
  285. }
  286. c->ltab_lnum = lnum;
  287. c->ltab_offs = offs;
  288. offs += c->ltab_sz;
  289. dbg_chk_lpt_sz(c, 1, c->ltab_sz);
  290. }
  291. alen = ALIGN(offs, c->min_io_size);
  292. upd_ltab(c, lnum, c->leb_size - alen, alen - offs);
  293. dbg_chk_lpt_sz(c, 4, alen - offs);
  294. err = dbg_chk_lpt_sz(c, 3, alen);
  295. if (err)
  296. return err;
  297. return 0;
  298. no_space:
  299. ubifs_err(c, "LPT out of space at LEB %d:%d needing %d, done_ltab %d, done_lsave %d",
  300. lnum, offs, len, done_ltab, done_lsave);
  301. ubifs_dump_lpt_info(c);
  302. ubifs_dump_lpt_lebs(c);
  303. dump_stack();
  304. return err;
  305. }
  306. /**
  307. * realloc_lpt_leb - allocate an LPT LEB that is empty.
  308. * @c: UBIFS file-system description object
  309. * @lnum: LEB number is passed and returned here
  310. *
  311. * This function duplicates exactly the results of the function alloc_lpt_leb.
  312. * It is used during end commit to reallocate the same LEB numbers that were
  313. * allocated by alloc_lpt_leb during start commit.
  314. *
  315. * This function finds the next LEB that was allocated by the alloc_lpt_leb
  316. * function starting from @lnum. If a LEB is found it is returned in @lnum and
  317. * the function returns %0. Otherwise the function returns -ENOSPC.
  318. * Note however, that LPT is designed never to run out of space.
  319. */
  320. static int realloc_lpt_leb(struct ubifs_info *c, int *lnum)
  321. {
  322. int i, n;
  323. n = *lnum - c->lpt_first + 1;
  324. for (i = n; i < c->lpt_lebs; i++)
  325. if (c->ltab[i].cmt) {
  326. c->ltab[i].cmt = 0;
  327. *lnum = i + c->lpt_first;
  328. return 0;
  329. }
  330. for (i = 0; i < n; i++)
  331. if (c->ltab[i].cmt) {
  332. c->ltab[i].cmt = 0;
  333. *lnum = i + c->lpt_first;
  334. return 0;
  335. }
  336. return -ENOSPC;
  337. }
  338. /**
  339. * write_cnodes - write cnodes for commit.
  340. * @c: UBIFS file-system description object
  341. *
  342. * This function returns %0 on success and a negative error code on failure.
  343. */
  344. static int write_cnodes(struct ubifs_info *c)
  345. {
  346. int lnum, offs, len, from, err, wlen, alen, done_ltab, done_lsave;
  347. struct ubifs_cnode *cnode;
  348. void *buf = c->lpt_buf;
  349. cnode = c->lpt_cnext;
  350. if (!cnode)
  351. return 0;
  352. lnum = c->nhead_lnum;
  353. offs = c->nhead_offs;
  354. from = offs;
  355. /* Ensure empty LEB is unmapped */
  356. if (offs == 0) {
  357. err = ubifs_leb_unmap(c, lnum);
  358. if (err)
  359. return err;
  360. }
  361. /* Try to place lsave and ltab nicely */
  362. done_lsave = !c->big_lpt;
  363. done_ltab = 0;
  364. if (!done_lsave && offs + c->lsave_sz <= c->leb_size) {
  365. done_lsave = 1;
  366. ubifs_pack_lsave(c, buf + offs, c->lsave);
  367. offs += c->lsave_sz;
  368. dbg_chk_lpt_sz(c, 1, c->lsave_sz);
  369. }
  370. if (offs + c->ltab_sz <= c->leb_size) {
  371. done_ltab = 1;
  372. ubifs_pack_ltab(c, buf + offs, c->ltab_cmt);
  373. offs += c->ltab_sz;
  374. dbg_chk_lpt_sz(c, 1, c->ltab_sz);
  375. }
  376. /* Loop for each cnode */
  377. do {
  378. if (cnode->level)
  379. len = c->nnode_sz;
  380. else
  381. len = c->pnode_sz;
  382. while (offs + len > c->leb_size) {
  383. wlen = offs - from;
  384. if (wlen) {
  385. alen = ALIGN(wlen, c->min_io_size);
  386. memset(buf + offs, 0xff, alen - wlen);
  387. err = ubifs_leb_write(c, lnum, buf + from, from,
  388. alen);
  389. if (err)
  390. return err;
  391. }
  392. dbg_chk_lpt_sz(c, 2, c->leb_size - offs);
  393. err = realloc_lpt_leb(c, &lnum);
  394. if (err)
  395. goto no_space;
  396. offs = from = 0;
  397. ubifs_assert(c, 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_atomic();
  433. clear_bit(COW_CNODE, &cnode->flags);
  434. smp_mb__after_atomic();
  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. if (err)
  447. return err;
  448. dbg_chk_lpt_sz(c, 2, c->leb_size - offs);
  449. err = realloc_lpt_leb(c, &lnum);
  450. if (err)
  451. goto no_space;
  452. offs = from = 0;
  453. ubifs_assert(c, lnum >= c->lpt_first &&
  454. lnum <= c->lpt_last);
  455. err = ubifs_leb_unmap(c, lnum);
  456. if (err)
  457. return err;
  458. }
  459. done_lsave = 1;
  460. ubifs_pack_lsave(c, buf + offs, c->lsave);
  461. offs += c->lsave_sz;
  462. dbg_chk_lpt_sz(c, 1, c->lsave_sz);
  463. }
  464. /* Make sure to place LPT's own lprops table */
  465. if (!done_ltab) {
  466. if (offs + c->ltab_sz > c->leb_size) {
  467. wlen = offs - from;
  468. alen = ALIGN(wlen, c->min_io_size);
  469. memset(buf + offs, 0xff, alen - wlen);
  470. err = ubifs_leb_write(c, lnum, buf + from, from, alen);
  471. if (err)
  472. return err;
  473. dbg_chk_lpt_sz(c, 2, c->leb_size - offs);
  474. err = realloc_lpt_leb(c, &lnum);
  475. if (err)
  476. goto no_space;
  477. offs = from = 0;
  478. ubifs_assert(c, lnum >= c->lpt_first &&
  479. lnum <= c->lpt_last);
  480. err = ubifs_leb_unmap(c, lnum);
  481. if (err)
  482. return err;
  483. }
  484. ubifs_pack_ltab(c, buf + offs, c->ltab_cmt);
  485. offs += c->ltab_sz;
  486. dbg_chk_lpt_sz(c, 1, c->ltab_sz);
  487. }
  488. /* Write remaining data in buffer */
  489. wlen = offs - from;
  490. alen = ALIGN(wlen, c->min_io_size);
  491. memset(buf + offs, 0xff, alen - wlen);
  492. err = ubifs_leb_write(c, lnum, buf + from, from, alen);
  493. if (err)
  494. return err;
  495. dbg_chk_lpt_sz(c, 4, alen - wlen);
  496. err = dbg_chk_lpt_sz(c, 3, ALIGN(offs, c->min_io_size));
  497. if (err)
  498. return err;
  499. c->nhead_lnum = lnum;
  500. c->nhead_offs = ALIGN(offs, c->min_io_size);
  501. dbg_lp("LPT root is at %d:%d", c->lpt_lnum, c->lpt_offs);
  502. dbg_lp("LPT head is at %d:%d", c->nhead_lnum, c->nhead_offs);
  503. dbg_lp("LPT ltab is at %d:%d", c->ltab_lnum, c->ltab_offs);
  504. if (c->big_lpt)
  505. dbg_lp("LPT lsave is at %d:%d", c->lsave_lnum, c->lsave_offs);
  506. return 0;
  507. no_space:
  508. ubifs_err(c, "LPT out of space mismatch at LEB %d:%d needing %d, done_ltab %d, done_lsave %d",
  509. lnum, offs, len, done_ltab, done_lsave);
  510. ubifs_dump_lpt_info(c);
  511. ubifs_dump_lpt_lebs(c);
  512. dump_stack();
  513. return err;
  514. }
  515. /**
  516. * next_pnode_to_dirty - find next pnode to dirty.
  517. * @c: UBIFS file-system description object
  518. * @pnode: pnode
  519. *
  520. * This function returns the next pnode to dirty or %NULL if there are no more
  521. * pnodes. Note that pnodes that have never been written (lnum == 0) are
  522. * skipped.
  523. */
  524. static struct ubifs_pnode *next_pnode_to_dirty(struct ubifs_info *c,
  525. struct ubifs_pnode *pnode)
  526. {
  527. struct ubifs_nnode *nnode;
  528. int iip;
  529. /* Try to go right */
  530. nnode = pnode->parent;
  531. for (iip = pnode->iip + 1; iip < UBIFS_LPT_FANOUT; iip++) {
  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. for (; iip < UBIFS_LPT_FANOUT; iip++) {
  542. if (nnode->nbranch[iip].lnum)
  543. break;
  544. }
  545. } while (iip >= UBIFS_LPT_FANOUT);
  546. /* Go right */
  547. nnode = ubifs_get_nnode(c, nnode, iip);
  548. if (IS_ERR(nnode))
  549. return (void *)nnode;
  550. /* Go down to level 1 */
  551. while (nnode->level > 1) {
  552. for (iip = 0; iip < UBIFS_LPT_FANOUT; iip++) {
  553. if (nnode->nbranch[iip].lnum)
  554. break;
  555. }
  556. if (iip >= UBIFS_LPT_FANOUT) {
  557. /*
  558. * Should not happen, but we need to keep going
  559. * if it does.
  560. */
  561. iip = 0;
  562. }
  563. nnode = ubifs_get_nnode(c, nnode, iip);
  564. if (IS_ERR(nnode))
  565. return (void *)nnode;
  566. }
  567. for (iip = 0; iip < UBIFS_LPT_FANOUT; iip++)
  568. if (nnode->nbranch[iip].lnum)
  569. break;
  570. if (iip >= UBIFS_LPT_FANOUT)
  571. /* Should not happen, but we need to keep going if it does */
  572. iip = 0;
  573. return ubifs_get_pnode(c, nnode, iip);
  574. }
  575. /**
  576. * pnode_lookup - lookup a pnode in the LPT.
  577. * @c: UBIFS file-system description object
  578. * @i: pnode number (0 to (main_lebs - 1) / UBIFS_LPT_FANOUT))
  579. *
  580. * This function returns a pointer to the pnode on success or a negative
  581. * error code on failure.
  582. */
  583. static struct ubifs_pnode *pnode_lookup(struct ubifs_info *c, int i)
  584. {
  585. int err, h, iip, shft;
  586. struct ubifs_nnode *nnode;
  587. if (!c->nroot) {
  588. err = ubifs_read_nnode(c, NULL, 0);
  589. if (err)
  590. return ERR_PTR(err);
  591. }
  592. i <<= UBIFS_LPT_FANOUT_SHIFT;
  593. nnode = c->nroot;
  594. shft = c->lpt_hght * UBIFS_LPT_FANOUT_SHIFT;
  595. for (h = 1; h < c->lpt_hght; h++) {
  596. iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
  597. shft -= UBIFS_LPT_FANOUT_SHIFT;
  598. nnode = ubifs_get_nnode(c, nnode, iip);
  599. if (IS_ERR(nnode))
  600. return ERR_CAST(nnode);
  601. }
  602. iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
  603. return ubifs_get_pnode(c, nnode, iip);
  604. }
  605. /**
  606. * add_pnode_dirt - add dirty space to LPT LEB properties.
  607. * @c: UBIFS file-system description object
  608. * @pnode: pnode for which to add dirt
  609. */
  610. static void add_pnode_dirt(struct ubifs_info *c, struct ubifs_pnode *pnode)
  611. {
  612. ubifs_add_lpt_dirt(c, pnode->parent->nbranch[pnode->iip].lnum,
  613. c->pnode_sz);
  614. }
  615. /**
  616. * do_make_pnode_dirty - mark a pnode dirty.
  617. * @c: UBIFS file-system description object
  618. * @pnode: pnode to mark dirty
  619. */
  620. static void do_make_pnode_dirty(struct ubifs_info *c, struct ubifs_pnode *pnode)
  621. {
  622. /* Assumes cnext list is empty i.e. not called during commit */
  623. if (!test_and_set_bit(DIRTY_CNODE, &pnode->flags)) {
  624. struct ubifs_nnode *nnode;
  625. c->dirty_pn_cnt += 1;
  626. add_pnode_dirt(c, pnode);
  627. /* Mark parent and ancestors dirty too */
  628. nnode = pnode->parent;
  629. while (nnode) {
  630. if (!test_and_set_bit(DIRTY_CNODE, &nnode->flags)) {
  631. c->dirty_nn_cnt += 1;
  632. ubifs_add_nnode_dirt(c, nnode);
  633. nnode = nnode->parent;
  634. } else
  635. break;
  636. }
  637. }
  638. }
  639. /**
  640. * make_tree_dirty - mark the entire LEB properties tree dirty.
  641. * @c: UBIFS file-system description object
  642. *
  643. * This function is used by the "small" LPT model to cause the entire LEB
  644. * properties tree to be written. The "small" LPT model does not use LPT
  645. * garbage collection because it is more efficient to write the entire tree
  646. * (because it is small).
  647. *
  648. * This function returns %0 on success and a negative error code on failure.
  649. */
  650. static int make_tree_dirty(struct ubifs_info *c)
  651. {
  652. struct ubifs_pnode *pnode;
  653. pnode = pnode_lookup(c, 0);
  654. if (IS_ERR(pnode))
  655. return PTR_ERR(pnode);
  656. while (pnode) {
  657. do_make_pnode_dirty(c, pnode);
  658. pnode = next_pnode_to_dirty(c, pnode);
  659. if (IS_ERR(pnode))
  660. return PTR_ERR(pnode);
  661. }
  662. return 0;
  663. }
  664. /**
  665. * need_write_all - determine if the LPT area is running out of free space.
  666. * @c: UBIFS file-system description object
  667. *
  668. * This function returns %1 if the LPT area is running out of free space and %0
  669. * if it is not.
  670. */
  671. static int need_write_all(struct ubifs_info *c)
  672. {
  673. long long free = 0;
  674. int i;
  675. for (i = 0; i < c->lpt_lebs; i++) {
  676. if (i + c->lpt_first == c->nhead_lnum)
  677. free += c->leb_size - c->nhead_offs;
  678. else if (c->ltab[i].free == c->leb_size)
  679. free += c->leb_size;
  680. else if (c->ltab[i].free + c->ltab[i].dirty == c->leb_size)
  681. free += c->leb_size;
  682. }
  683. /* Less than twice the size left */
  684. if (free <= c->lpt_sz * 2)
  685. return 1;
  686. return 0;
  687. }
  688. /**
  689. * lpt_tgc_start - start trivial garbage collection of LPT LEBs.
  690. * @c: UBIFS file-system description object
  691. *
  692. * LPT trivial garbage collection is where a LPT LEB contains only dirty and
  693. * free space and so may be reused as soon as the next commit is completed.
  694. * This function is called during start commit to mark LPT LEBs for trivial GC.
  695. */
  696. static void lpt_tgc_start(struct ubifs_info *c)
  697. {
  698. int i;
  699. for (i = 0; i < c->lpt_lebs; i++) {
  700. if (i + c->lpt_first == c->nhead_lnum)
  701. continue;
  702. if (c->ltab[i].dirty > 0 &&
  703. c->ltab[i].free + c->ltab[i].dirty == c->leb_size) {
  704. c->ltab[i].tgc = 1;
  705. c->ltab[i].free = c->leb_size;
  706. c->ltab[i].dirty = 0;
  707. dbg_lp("LEB %d", i + c->lpt_first);
  708. }
  709. }
  710. }
  711. /**
  712. * lpt_tgc_end - end trivial garbage collection of LPT LEBs.
  713. * @c: UBIFS file-system description object
  714. *
  715. * LPT trivial garbage collection is where a LPT LEB contains only dirty and
  716. * free space and so may be reused as soon as the next commit is completed.
  717. * This function is called after the commit is completed (master node has been
  718. * written) and un-maps LPT LEBs that were marked for trivial GC.
  719. */
  720. static int lpt_tgc_end(struct ubifs_info *c)
  721. {
  722. int i, err;
  723. for (i = 0; i < c->lpt_lebs; i++)
  724. if (c->ltab[i].tgc) {
  725. err = ubifs_leb_unmap(c, i + c->lpt_first);
  726. if (err)
  727. return err;
  728. c->ltab[i].tgc = 0;
  729. dbg_lp("LEB %d", i + c->lpt_first);
  730. }
  731. return 0;
  732. }
  733. /**
  734. * populate_lsave - fill the lsave array with important LEB numbers.
  735. * @c: the UBIFS file-system description object
  736. *
  737. * This function is only called for the "big" model. It records a small number
  738. * of LEB numbers of important LEBs. Important LEBs are ones that are (from
  739. * most important to least important): empty, freeable, freeable index, dirty
  740. * index, dirty or free. Upon mount, we read this list of LEB numbers and bring
  741. * their pnodes into memory. That will stop us from having to scan the LPT
  742. * straight away. For the "small" model we assume that scanning the LPT is no
  743. * big deal.
  744. */
  745. static void populate_lsave(struct ubifs_info *c)
  746. {
  747. struct ubifs_lprops *lprops;
  748. struct ubifs_lpt_heap *heap;
  749. int i, cnt = 0;
  750. ubifs_assert(c, c->big_lpt);
  751. if (!(c->lpt_drty_flgs & LSAVE_DIRTY)) {
  752. c->lpt_drty_flgs |= LSAVE_DIRTY;
  753. ubifs_add_lpt_dirt(c, c->lsave_lnum, c->lsave_sz);
  754. }
  755. if (dbg_populate_lsave(c))
  756. return;
  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(c, &addr, &pos, UBIFS_LPT_TYPE_BITS);
  1023. *node_num = ubifs_unpack_bits(c, &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(c, &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(c, &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 = ubifs_leb_read(c, lnum, buf, 0, c->leb_size, 1);
  1074. if (err)
  1075. return err;
  1076. while (1) {
  1077. if (!is_a_node(c, buf, len)) {
  1078. int pad_len;
  1079. pad_len = get_pad_len(c, buf, len);
  1080. if (pad_len) {
  1081. buf += pad_len;
  1082. len -= pad_len;
  1083. continue;
  1084. }
  1085. return 0;
  1086. }
  1087. node_type = get_lpt_node_type(c, buf, &node_num);
  1088. node_len = get_lpt_node_len(c, node_type);
  1089. offs = c->leb_size - len;
  1090. ubifs_assert(c, node_len != 0);
  1091. mutex_lock(&c->lp_mutex);
  1092. err = make_node_dirty(c, node_type, node_num, lnum, offs);
  1093. mutex_unlock(&c->lp_mutex);
  1094. if (err)
  1095. return err;
  1096. buf += node_len;
  1097. len -= node_len;
  1098. }
  1099. return 0;
  1100. }
  1101. /**
  1102. * lpt_gc - LPT garbage collection.
  1103. * @c: UBIFS file-system description object
  1104. *
  1105. * Select a LPT LEB for LPT garbage collection and call 'lpt_gc_lnum()'.
  1106. * Returns %0 on success and a negative error code on failure.
  1107. */
  1108. static int lpt_gc(struct ubifs_info *c)
  1109. {
  1110. int i, lnum = -1, dirty = 0;
  1111. mutex_lock(&c->lp_mutex);
  1112. for (i = 0; i < c->lpt_lebs; i++) {
  1113. ubifs_assert(c, !c->ltab[i].tgc);
  1114. if (i + c->lpt_first == c->nhead_lnum ||
  1115. c->ltab[i].free + c->ltab[i].dirty == c->leb_size)
  1116. continue;
  1117. if (c->ltab[i].dirty > dirty) {
  1118. dirty = c->ltab[i].dirty;
  1119. lnum = i + c->lpt_first;
  1120. }
  1121. }
  1122. mutex_unlock(&c->lp_mutex);
  1123. if (lnum == -1)
  1124. return -ENOSPC;
  1125. return lpt_gc_lnum(c, lnum);
  1126. }
  1127. /**
  1128. * ubifs_lpt_start_commit - UBIFS commit starts.
  1129. * @c: the UBIFS file-system description object
  1130. *
  1131. * This function has to be called when UBIFS starts the commit operation.
  1132. * This function "freezes" all currently dirty LEB properties and does not
  1133. * change them anymore. Further changes are saved and tracked separately
  1134. * because they are not part of this commit. This function returns zero in case
  1135. * of success and a negative error code in case of failure.
  1136. */
  1137. int ubifs_lpt_start_commit(struct ubifs_info *c)
  1138. {
  1139. int err, cnt;
  1140. dbg_lp("");
  1141. mutex_lock(&c->lp_mutex);
  1142. err = dbg_chk_lpt_free_spc(c);
  1143. if (err)
  1144. goto out;
  1145. err = dbg_check_ltab(c);
  1146. if (err)
  1147. goto out;
  1148. if (c->check_lpt_free) {
  1149. /*
  1150. * We ensure there is enough free space in
  1151. * ubifs_lpt_post_commit() by marking nodes dirty. That
  1152. * information is lost when we unmount, so we also need
  1153. * to check free space once after mounting also.
  1154. */
  1155. c->check_lpt_free = 0;
  1156. while (need_write_all(c)) {
  1157. mutex_unlock(&c->lp_mutex);
  1158. err = lpt_gc(c);
  1159. if (err)
  1160. return err;
  1161. mutex_lock(&c->lp_mutex);
  1162. }
  1163. }
  1164. lpt_tgc_start(c);
  1165. if (!c->dirty_pn_cnt) {
  1166. dbg_cmt("no cnodes to commit");
  1167. err = 0;
  1168. goto out;
  1169. }
  1170. if (!c->big_lpt && need_write_all(c)) {
  1171. /* If needed, write everything */
  1172. err = make_tree_dirty(c);
  1173. if (err)
  1174. goto out;
  1175. lpt_tgc_start(c);
  1176. }
  1177. if (c->big_lpt)
  1178. populate_lsave(c);
  1179. cnt = get_cnodes_to_commit(c);
  1180. ubifs_assert(c, cnt != 0);
  1181. err = layout_cnodes(c);
  1182. if (err)
  1183. goto out;
  1184. /* Copy the LPT's own lprops for end commit to write */
  1185. memcpy(c->ltab_cmt, c->ltab,
  1186. sizeof(struct ubifs_lpt_lprops) * c->lpt_lebs);
  1187. c->lpt_drty_flgs &= ~(LTAB_DIRTY | LSAVE_DIRTY);
  1188. out:
  1189. mutex_unlock(&c->lp_mutex);
  1190. return err;
  1191. }
  1192. /**
  1193. * free_obsolete_cnodes - free obsolete cnodes for commit end.
  1194. * @c: UBIFS file-system description object
  1195. */
  1196. static void free_obsolete_cnodes(struct ubifs_info *c)
  1197. {
  1198. struct ubifs_cnode *cnode, *cnext;
  1199. cnext = c->lpt_cnext;
  1200. if (!cnext)
  1201. return;
  1202. do {
  1203. cnode = cnext;
  1204. cnext = cnode->cnext;
  1205. if (test_bit(OBSOLETE_CNODE, &cnode->flags))
  1206. kfree(cnode);
  1207. else
  1208. cnode->cnext = NULL;
  1209. } while (cnext != c->lpt_cnext);
  1210. c->lpt_cnext = NULL;
  1211. }
  1212. /**
  1213. * ubifs_lpt_end_commit - finish the commit operation.
  1214. * @c: the UBIFS file-system description object
  1215. *
  1216. * This function has to be called when the commit operation finishes. It
  1217. * flushes the changes which were "frozen" by 'ubifs_lprops_start_commit()' to
  1218. * the media. Returns zero in case of success and a negative error code in case
  1219. * of failure.
  1220. */
  1221. int ubifs_lpt_end_commit(struct ubifs_info *c)
  1222. {
  1223. int err;
  1224. dbg_lp("");
  1225. if (!c->lpt_cnext)
  1226. return 0;
  1227. err = write_cnodes(c);
  1228. if (err)
  1229. return err;
  1230. mutex_lock(&c->lp_mutex);
  1231. free_obsolete_cnodes(c);
  1232. mutex_unlock(&c->lp_mutex);
  1233. return 0;
  1234. }
  1235. /**
  1236. * ubifs_lpt_post_commit - post commit LPT trivial GC and LPT GC.
  1237. * @c: UBIFS file-system description object
  1238. *
  1239. * LPT trivial GC is completed after a commit. Also LPT GC is done after a
  1240. * commit for the "big" LPT model.
  1241. */
  1242. int ubifs_lpt_post_commit(struct ubifs_info *c)
  1243. {
  1244. int err;
  1245. mutex_lock(&c->lp_mutex);
  1246. err = lpt_tgc_end(c);
  1247. if (err)
  1248. goto out;
  1249. if (c->big_lpt)
  1250. while (need_write_all(c)) {
  1251. mutex_unlock(&c->lp_mutex);
  1252. err = lpt_gc(c);
  1253. if (err)
  1254. return err;
  1255. mutex_lock(&c->lp_mutex);
  1256. }
  1257. out:
  1258. mutex_unlock(&c->lp_mutex);
  1259. return err;
  1260. }
  1261. /**
  1262. * first_nnode - find the first nnode in memory.
  1263. * @c: UBIFS file-system description object
  1264. * @hght: height of tree where nnode found is returned here
  1265. *
  1266. * This function returns a pointer to the nnode found or %NULL if no nnode is
  1267. * found. This function is a helper to 'ubifs_lpt_free()'.
  1268. */
  1269. static struct ubifs_nnode *first_nnode(struct ubifs_info *c, int *hght)
  1270. {
  1271. struct ubifs_nnode *nnode;
  1272. int h, i, found;
  1273. nnode = c->nroot;
  1274. *hght = 0;
  1275. if (!nnode)
  1276. return NULL;
  1277. for (h = 1; h < c->lpt_hght; h++) {
  1278. found = 0;
  1279. for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
  1280. if (nnode->nbranch[i].nnode) {
  1281. found = 1;
  1282. nnode = nnode->nbranch[i].nnode;
  1283. *hght = h;
  1284. break;
  1285. }
  1286. }
  1287. if (!found)
  1288. break;
  1289. }
  1290. return nnode;
  1291. }
  1292. /**
  1293. * next_nnode - find the next nnode in memory.
  1294. * @c: UBIFS file-system description object
  1295. * @nnode: nnode from which to start.
  1296. * @hght: height of tree where nnode is, is passed and returned here
  1297. *
  1298. * This function returns a pointer to the nnode found or %NULL if no nnode is
  1299. * found. This function is a helper to 'ubifs_lpt_free()'.
  1300. */
  1301. static struct ubifs_nnode *next_nnode(struct ubifs_info *c,
  1302. struct ubifs_nnode *nnode, int *hght)
  1303. {
  1304. struct ubifs_nnode *parent;
  1305. int iip, h, i, found;
  1306. parent = nnode->parent;
  1307. if (!parent)
  1308. return NULL;
  1309. if (nnode->iip == UBIFS_LPT_FANOUT - 1) {
  1310. *hght -= 1;
  1311. return parent;
  1312. }
  1313. for (iip = nnode->iip + 1; iip < UBIFS_LPT_FANOUT; iip++) {
  1314. nnode = parent->nbranch[iip].nnode;
  1315. if (nnode)
  1316. break;
  1317. }
  1318. if (!nnode) {
  1319. *hght -= 1;
  1320. return parent;
  1321. }
  1322. for (h = *hght + 1; h < c->lpt_hght; h++) {
  1323. found = 0;
  1324. for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
  1325. if (nnode->nbranch[i].nnode) {
  1326. found = 1;
  1327. nnode = nnode->nbranch[i].nnode;
  1328. *hght = h;
  1329. break;
  1330. }
  1331. }
  1332. if (!found)
  1333. break;
  1334. }
  1335. return nnode;
  1336. }
  1337. /**
  1338. * ubifs_lpt_free - free resources owned by the LPT.
  1339. * @c: UBIFS file-system description object
  1340. * @wr_only: free only resources used for writing
  1341. */
  1342. void ubifs_lpt_free(struct ubifs_info *c, int wr_only)
  1343. {
  1344. struct ubifs_nnode *nnode;
  1345. int i, hght;
  1346. /* Free write-only things first */
  1347. free_obsolete_cnodes(c); /* Leftover from a failed commit */
  1348. vfree(c->ltab_cmt);
  1349. c->ltab_cmt = NULL;
  1350. vfree(c->lpt_buf);
  1351. c->lpt_buf = NULL;
  1352. kfree(c->lsave);
  1353. c->lsave = NULL;
  1354. if (wr_only)
  1355. return;
  1356. /* Now free the rest */
  1357. nnode = first_nnode(c, &hght);
  1358. while (nnode) {
  1359. for (i = 0; i < UBIFS_LPT_FANOUT; i++)
  1360. kfree(nnode->nbranch[i].nnode);
  1361. nnode = next_nnode(c, nnode, &hght);
  1362. }
  1363. for (i = 0; i < LPROPS_HEAP_CNT; i++)
  1364. kfree(c->lpt_heap[i].arr);
  1365. kfree(c->dirty_idx.arr);
  1366. kfree(c->nroot);
  1367. vfree(c->ltab);
  1368. kfree(c->lpt_nod_buf);
  1369. }
  1370. /*
  1371. * Everything below is related to debugging.
  1372. */
  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. *
  1495. * This function returns %0 on success and a negative error code on failure.
  1496. */
  1497. static int dbg_check_ltab_lnum(struct ubifs_info *c, int lnum)
  1498. {
  1499. int err, len = c->leb_size, dirty = 0, node_type, node_num, node_len;
  1500. int ret;
  1501. void *buf, *p;
  1502. if (!dbg_is_chk_lprops(c))
  1503. return 0;
  1504. buf = p = __vmalloc(c->leb_size, GFP_NOFS, PAGE_KERNEL);
  1505. if (!buf) {
  1506. ubifs_err(c, "cannot allocate memory for ltab checking");
  1507. return 0;
  1508. }
  1509. dbg_lp("LEB %d", lnum);
  1510. err = ubifs_leb_read(c, lnum, buf, 0, c->leb_size, 1);
  1511. if (err)
  1512. goto out;
  1513. while (1) {
  1514. if (!is_a_node(c, p, len)) {
  1515. int i, pad_len;
  1516. pad_len = get_pad_len(c, p, len);
  1517. if (pad_len) {
  1518. p += pad_len;
  1519. len -= pad_len;
  1520. dirty += pad_len;
  1521. continue;
  1522. }
  1523. if (!dbg_is_all_ff(p, len)) {
  1524. ubifs_err(c, "invalid empty space in LEB %d at %d",
  1525. lnum, c->leb_size - len);
  1526. err = -EINVAL;
  1527. }
  1528. i = lnum - c->lpt_first;
  1529. if (len != c->ltab[i].free) {
  1530. ubifs_err(c, "invalid free space in LEB %d (free %d, expected %d)",
  1531. lnum, len, c->ltab[i].free);
  1532. err = -EINVAL;
  1533. }
  1534. if (dirty != c->ltab[i].dirty) {
  1535. ubifs_err(c, "invalid dirty space in LEB %d (dirty %d, expected %d)",
  1536. lnum, dirty, c->ltab[i].dirty);
  1537. err = -EINVAL;
  1538. }
  1539. goto out;
  1540. }
  1541. node_type = get_lpt_node_type(c, p, &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. p += node_len;
  1547. len -= node_len;
  1548. }
  1549. err = 0;
  1550. out:
  1551. vfree(buf);
  1552. return err;
  1553. }
  1554. /**
  1555. * dbg_check_ltab - check the free and dirty space in the ltab.
  1556. * @c: the UBIFS file-system description object
  1557. *
  1558. * This function returns %0 on success and a negative error code on failure.
  1559. */
  1560. int dbg_check_ltab(struct ubifs_info *c)
  1561. {
  1562. int lnum, err, i, cnt;
  1563. if (!dbg_is_chk_lprops(c))
  1564. return 0;
  1565. /* Bring the entire tree into memory */
  1566. cnt = DIV_ROUND_UP(c->main_lebs, UBIFS_LPT_FANOUT);
  1567. for (i = 0; i < cnt; i++) {
  1568. struct ubifs_pnode *pnode;
  1569. pnode = pnode_lookup(c, i);
  1570. if (IS_ERR(pnode))
  1571. return PTR_ERR(pnode);
  1572. cond_resched();
  1573. }
  1574. /* Check nodes */
  1575. err = dbg_check_lpt_nodes(c, (struct ubifs_cnode *)c->nroot, 0, 0);
  1576. if (err)
  1577. return err;
  1578. /* Check each LEB */
  1579. for (lnum = c->lpt_first; lnum <= c->lpt_last; lnum++) {
  1580. err = dbg_check_ltab_lnum(c, lnum);
  1581. if (err) {
  1582. ubifs_err(c, "failed at LEB %d", lnum);
  1583. return err;
  1584. }
  1585. }
  1586. dbg_lp("succeeded");
  1587. return 0;
  1588. }
  1589. /**
  1590. * dbg_chk_lpt_free_spc - check LPT free space is enough to write entire LPT.
  1591. * @c: the UBIFS file-system description object
  1592. *
  1593. * This function returns %0 on success and a negative error code on failure.
  1594. */
  1595. int dbg_chk_lpt_free_spc(struct ubifs_info *c)
  1596. {
  1597. long long free = 0;
  1598. int i;
  1599. if (!dbg_is_chk_lprops(c))
  1600. return 0;
  1601. for (i = 0; i < c->lpt_lebs; i++) {
  1602. if (c->ltab[i].tgc || c->ltab[i].cmt)
  1603. continue;
  1604. if (i + c->lpt_first == c->nhead_lnum)
  1605. free += c->leb_size - c->nhead_offs;
  1606. else if (c->ltab[i].free == c->leb_size)
  1607. free += c->leb_size;
  1608. }
  1609. if (free < c->lpt_sz) {
  1610. ubifs_err(c, "LPT space error: free %lld lpt_sz %lld",
  1611. free, c->lpt_sz);
  1612. ubifs_dump_lpt_info(c);
  1613. ubifs_dump_lpt_lebs(c);
  1614. dump_stack();
  1615. return -EINVAL;
  1616. }
  1617. return 0;
  1618. }
  1619. /**
  1620. * dbg_chk_lpt_sz - check LPT does not write more than LPT size.
  1621. * @c: the UBIFS file-system description object
  1622. * @action: what to do
  1623. * @len: length written
  1624. *
  1625. * This function returns %0 on success and a negative error code on failure.
  1626. * The @action argument may be one of:
  1627. * o %0 - LPT debugging checking starts, initialize debugging variables;
  1628. * o %1 - wrote an LPT node, increase LPT size by @len bytes;
  1629. * o %2 - switched to a different LEB and wasted @len bytes;
  1630. * o %3 - check that we've written the right number of bytes.
  1631. * o %4 - wasted @len bytes;
  1632. */
  1633. int dbg_chk_lpt_sz(struct ubifs_info *c, int action, int len)
  1634. {
  1635. struct ubifs_debug_info *d = c->dbg;
  1636. long long chk_lpt_sz, lpt_sz;
  1637. int err = 0;
  1638. if (!dbg_is_chk_lprops(c))
  1639. return 0;
  1640. switch (action) {
  1641. case 0:
  1642. d->chk_lpt_sz = 0;
  1643. d->chk_lpt_sz2 = 0;
  1644. d->chk_lpt_lebs = 0;
  1645. d->chk_lpt_wastage = 0;
  1646. if (c->dirty_pn_cnt > c->pnode_cnt) {
  1647. ubifs_err(c, "dirty pnodes %d exceed max %d",
  1648. c->dirty_pn_cnt, c->pnode_cnt);
  1649. err = -EINVAL;
  1650. }
  1651. if (c->dirty_nn_cnt > c->nnode_cnt) {
  1652. ubifs_err(c, "dirty nnodes %d exceed max %d",
  1653. c->dirty_nn_cnt, c->nnode_cnt);
  1654. err = -EINVAL;
  1655. }
  1656. return err;
  1657. case 1:
  1658. d->chk_lpt_sz += len;
  1659. return 0;
  1660. case 2:
  1661. d->chk_lpt_sz += len;
  1662. d->chk_lpt_wastage += len;
  1663. d->chk_lpt_lebs += 1;
  1664. return 0;
  1665. case 3:
  1666. chk_lpt_sz = c->leb_size;
  1667. chk_lpt_sz *= d->chk_lpt_lebs;
  1668. chk_lpt_sz += len - c->nhead_offs;
  1669. if (d->chk_lpt_sz != chk_lpt_sz) {
  1670. ubifs_err(c, "LPT wrote %lld but space used was %lld",
  1671. d->chk_lpt_sz, chk_lpt_sz);
  1672. err = -EINVAL;
  1673. }
  1674. if (d->chk_lpt_sz > c->lpt_sz) {
  1675. ubifs_err(c, "LPT wrote %lld but lpt_sz is %lld",
  1676. d->chk_lpt_sz, c->lpt_sz);
  1677. err = -EINVAL;
  1678. }
  1679. if (d->chk_lpt_sz2 && d->chk_lpt_sz != d->chk_lpt_sz2) {
  1680. ubifs_err(c, "LPT layout size %lld but wrote %lld",
  1681. d->chk_lpt_sz, d->chk_lpt_sz2);
  1682. err = -EINVAL;
  1683. }
  1684. if (d->chk_lpt_sz2 && d->new_nhead_offs != len) {
  1685. ubifs_err(c, "LPT new nhead offs: expected %d was %d",
  1686. d->new_nhead_offs, len);
  1687. err = -EINVAL;
  1688. }
  1689. lpt_sz = (long long)c->pnode_cnt * c->pnode_sz;
  1690. lpt_sz += (long long)c->nnode_cnt * c->nnode_sz;
  1691. lpt_sz += c->ltab_sz;
  1692. if (c->big_lpt)
  1693. lpt_sz += c->lsave_sz;
  1694. if (d->chk_lpt_sz - d->chk_lpt_wastage > lpt_sz) {
  1695. ubifs_err(c, "LPT chk_lpt_sz %lld + waste %lld exceeds %lld",
  1696. d->chk_lpt_sz, d->chk_lpt_wastage, lpt_sz);
  1697. err = -EINVAL;
  1698. }
  1699. if (err) {
  1700. ubifs_dump_lpt_info(c);
  1701. ubifs_dump_lpt_lebs(c);
  1702. dump_stack();
  1703. }
  1704. d->chk_lpt_sz2 = d->chk_lpt_sz;
  1705. d->chk_lpt_sz = 0;
  1706. d->chk_lpt_wastage = 0;
  1707. d->chk_lpt_lebs = 0;
  1708. d->new_nhead_offs = len;
  1709. return err;
  1710. case 4:
  1711. d->chk_lpt_sz += len;
  1712. d->chk_lpt_wastage += len;
  1713. return 0;
  1714. default:
  1715. return -EINVAL;
  1716. }
  1717. }
  1718. /**
  1719. * dump_lpt_leb - dump an LPT LEB.
  1720. * @c: UBIFS file-system description object
  1721. * @lnum: LEB number to dump
  1722. *
  1723. * This function dumps an LEB from LPT area. Nodes in this area are very
  1724. * different to nodes in the main area (e.g., they do not have common headers,
  1725. * they do not have 8-byte alignments, etc), so we have a separate function to
  1726. * dump LPT area LEBs. Note, LPT has to be locked by the caller.
  1727. */
  1728. static void dump_lpt_leb(const struct ubifs_info *c, int lnum)
  1729. {
  1730. int err, len = c->leb_size, node_type, node_num, node_len, offs;
  1731. void *buf, *p;
  1732. pr_err("(pid %d) start dumping LEB %d\n", current->pid, lnum);
  1733. buf = p = __vmalloc(c->leb_size, GFP_NOFS, PAGE_KERNEL);
  1734. if (!buf) {
  1735. ubifs_err(c, "cannot allocate memory to dump LPT");
  1736. return;
  1737. }
  1738. err = ubifs_leb_read(c, lnum, buf, 0, c->leb_size, 1);
  1739. if (err)
  1740. goto out;
  1741. while (1) {
  1742. offs = c->leb_size - len;
  1743. if (!is_a_node(c, p, len)) {
  1744. int pad_len;
  1745. pad_len = get_pad_len(c, p, len);
  1746. if (pad_len) {
  1747. pr_err("LEB %d:%d, pad %d bytes\n",
  1748. lnum, offs, pad_len);
  1749. p += pad_len;
  1750. len -= pad_len;
  1751. continue;
  1752. }
  1753. if (len)
  1754. pr_err("LEB %d:%d, free %d bytes\n",
  1755. lnum, offs, len);
  1756. break;
  1757. }
  1758. node_type = get_lpt_node_type(c, p, &node_num);
  1759. switch (node_type) {
  1760. case UBIFS_LPT_PNODE:
  1761. {
  1762. node_len = c->pnode_sz;
  1763. if (c->big_lpt)
  1764. pr_err("LEB %d:%d, pnode num %d\n",
  1765. lnum, offs, node_num);
  1766. else
  1767. pr_err("LEB %d:%d, pnode\n", lnum, offs);
  1768. break;
  1769. }
  1770. case UBIFS_LPT_NNODE:
  1771. {
  1772. int i;
  1773. struct ubifs_nnode nnode;
  1774. node_len = c->nnode_sz;
  1775. if (c->big_lpt)
  1776. pr_err("LEB %d:%d, nnode num %d, ",
  1777. lnum, offs, node_num);
  1778. else
  1779. pr_err("LEB %d:%d, nnode, ",
  1780. lnum, offs);
  1781. err = ubifs_unpack_nnode(c, p, &nnode);
  1782. if (err) {
  1783. pr_err("failed to unpack_node, error %d\n",
  1784. err);
  1785. break;
  1786. }
  1787. for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
  1788. pr_cont("%d:%d", nnode.nbranch[i].lnum,
  1789. nnode.nbranch[i].offs);
  1790. if (i != UBIFS_LPT_FANOUT - 1)
  1791. pr_cont(", ");
  1792. }
  1793. pr_cont("\n");
  1794. break;
  1795. }
  1796. case UBIFS_LPT_LTAB:
  1797. node_len = c->ltab_sz;
  1798. pr_err("LEB %d:%d, ltab\n", lnum, offs);
  1799. break;
  1800. case UBIFS_LPT_LSAVE:
  1801. node_len = c->lsave_sz;
  1802. pr_err("LEB %d:%d, lsave len\n", lnum, offs);
  1803. break;
  1804. default:
  1805. ubifs_err(c, "LPT node type %d not recognized", node_type);
  1806. goto out;
  1807. }
  1808. p += node_len;
  1809. len -= node_len;
  1810. }
  1811. pr_err("(pid %d) finish dumping LEB %d\n", current->pid, lnum);
  1812. out:
  1813. vfree(buf);
  1814. return;
  1815. }
  1816. /**
  1817. * ubifs_dump_lpt_lebs - dump LPT lebs.
  1818. * @c: UBIFS file-system description object
  1819. *
  1820. * This function dumps all LPT LEBs. The caller has to make sure the LPT is
  1821. * locked.
  1822. */
  1823. void ubifs_dump_lpt_lebs(const struct ubifs_info *c)
  1824. {
  1825. int i;
  1826. pr_err("(pid %d) start dumping all LPT LEBs\n", current->pid);
  1827. for (i = 0; i < c->lpt_lebs; i++)
  1828. dump_lpt_leb(c, i + c->lpt_first);
  1829. pr_err("(pid %d) finish dumping all LPT LEBs\n", current->pid);
  1830. }
  1831. /**
  1832. * dbg_populate_lsave - debugging version of 'populate_lsave()'
  1833. * @c: UBIFS file-system description object
  1834. *
  1835. * This is a debugging version for 'populate_lsave()' which populates lsave
  1836. * with random LEBs instead of useful LEBs, which is good for test coverage.
  1837. * Returns zero if lsave has not been populated (this debugging feature is
  1838. * disabled) an non-zero if lsave has been populated.
  1839. */
  1840. static int dbg_populate_lsave(struct ubifs_info *c)
  1841. {
  1842. struct ubifs_lprops *lprops;
  1843. struct ubifs_lpt_heap *heap;
  1844. int i;
  1845. if (!dbg_is_chk_gen(c))
  1846. return 0;
  1847. if (prandom_u32() & 3)
  1848. return 0;
  1849. for (i = 0; i < c->lsave_cnt; i++)
  1850. c->lsave[i] = c->main_first;
  1851. list_for_each_entry(lprops, &c->empty_list, list)
  1852. c->lsave[prandom_u32() % c->lsave_cnt] = lprops->lnum;
  1853. list_for_each_entry(lprops, &c->freeable_list, list)
  1854. c->lsave[prandom_u32() % c->lsave_cnt] = lprops->lnum;
  1855. list_for_each_entry(lprops, &c->frdi_idx_list, list)
  1856. c->lsave[prandom_u32() % c->lsave_cnt] = lprops->lnum;
  1857. heap = &c->lpt_heap[LPROPS_DIRTY_IDX - 1];
  1858. for (i = 0; i < heap->cnt; i++)
  1859. c->lsave[prandom_u32() % c->lsave_cnt] = heap->arr[i]->lnum;
  1860. heap = &c->lpt_heap[LPROPS_DIRTY - 1];
  1861. for (i = 0; i < heap->cnt; i++)
  1862. c->lsave[prandom_u32() % c->lsave_cnt] = heap->arr[i]->lnum;
  1863. heap = &c->lpt_heap[LPROPS_FREE - 1];
  1864. for (i = 0; i < heap->cnt; i++)
  1865. c->lsave[prandom_u32() % c->lsave_cnt] = heap->arr[i]->lnum;
  1866. return 1;
  1867. }