lpt_commit.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044
  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(struct ubifs_nnode *nnode)
  39. {
  40. ubifs_assert(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. * @cnode: cnode from which to begin searching
  62. *
  63. * This function returns the next dirty cnode or %NULL if there is not one.
  64. */
  65. static struct ubifs_cnode *next_dirty_cnode(struct ubifs_cnode *cnode)
  66. {
  67. struct ubifs_nnode *nnode;
  68. int i;
  69. ubifs_assert(cnode);
  70. nnode = cnode->parent;
  71. if (!nnode)
  72. return NULL;
  73. for (i = cnode->iip + 1; i < UBIFS_LPT_FANOUT; i++) {
  74. cnode = nnode->nbranch[i].cnode;
  75. if (cnode && test_bit(DIRTY_CNODE, &cnode->flags)) {
  76. if (cnode->level == 0)
  77. return cnode; /* cnode is a pnode */
  78. /* cnode is a nnode */
  79. return first_dirty_cnode((struct ubifs_nnode *)cnode);
  80. }
  81. }
  82. return (struct ubifs_cnode *)nnode;
  83. }
  84. /**
  85. * get_cnodes_to_commit - create list of dirty cnodes to commit.
  86. * @c: UBIFS file-system description object
  87. *
  88. * This function returns the number of cnodes to commit.
  89. */
  90. static int get_cnodes_to_commit(struct ubifs_info *c)
  91. {
  92. struct ubifs_cnode *cnode, *cnext;
  93. int cnt = 0;
  94. if (!c->nroot)
  95. return 0;
  96. if (!test_bit(DIRTY_CNODE, &c->nroot->flags))
  97. return 0;
  98. c->lpt_cnext = first_dirty_cnode(c->nroot);
  99. cnode = c->lpt_cnext;
  100. if (!cnode)
  101. return 0;
  102. cnt += 1;
  103. while (1) {
  104. ubifs_assert(!test_bit(COW_CNODE, &cnode->flags));
  105. __set_bit(COW_CNODE, &cnode->flags);
  106. cnext = next_dirty_cnode(cnode);
  107. if (!cnext) {
  108. cnode->cnext = c->lpt_cnext;
  109. break;
  110. }
  111. cnode->cnext = cnext;
  112. cnode = cnext;
  113. cnt += 1;
  114. }
  115. dbg_cmt("committing %d cnodes", cnt);
  116. dbg_lp("committing %d cnodes", cnt);
  117. ubifs_assert(cnt == c->dirty_nn_cnt + c->dirty_pn_cnt);
  118. return cnt;
  119. }
  120. /**
  121. * upd_ltab - update LPT LEB properties.
  122. * @c: UBIFS file-system description object
  123. * @lnum: LEB number
  124. * @free: amount of free space
  125. * @dirty: amount of dirty space to add
  126. */
  127. static void upd_ltab(struct ubifs_info *c, int lnum, int free, int dirty)
  128. {
  129. dbg_lp("LEB %d free %d dirty %d to %d +%d",
  130. lnum, c->ltab[lnum - c->lpt_first].free,
  131. c->ltab[lnum - c->lpt_first].dirty, free, dirty);
  132. ubifs_assert(lnum >= c->lpt_first && lnum <= c->lpt_last);
  133. c->ltab[lnum - c->lpt_first].free = free;
  134. c->ltab[lnum - c->lpt_first].dirty += dirty;
  135. }
  136. /**
  137. * alloc_lpt_leb - allocate an LPT LEB that is empty.
  138. * @c: UBIFS file-system description object
  139. * @lnum: LEB number is passed and returned here
  140. *
  141. * This function finds the next empty LEB in the ltab starting from @lnum. If a
  142. * an empty LEB is found it is returned in @lnum and the function returns %0.
  143. * Otherwise the function returns -ENOSPC. Note however, that LPT is designed
  144. * never to run out of space.
  145. */
  146. static int alloc_lpt_leb(struct ubifs_info *c, int *lnum)
  147. {
  148. int i, n;
  149. n = *lnum - c->lpt_first + 1;
  150. for (i = n; i < c->lpt_lebs; i++) {
  151. if (c->ltab[i].tgc || c->ltab[i].cmt)
  152. continue;
  153. if (c->ltab[i].free == c->leb_size) {
  154. c->ltab[i].cmt = 1;
  155. *lnum = i + c->lpt_first;
  156. return 0;
  157. }
  158. }
  159. for (i = 0; i < n; i++) {
  160. if (c->ltab[i].tgc || c->ltab[i].cmt)
  161. continue;
  162. if (c->ltab[i].free == c->leb_size) {
  163. c->ltab[i].cmt = 1;
  164. *lnum = i + c->lpt_first;
  165. return 0;
  166. }
  167. }
  168. return -ENOSPC;
  169. }
  170. /**
  171. * layout_cnodes - layout cnodes for commit.
  172. * @c: UBIFS file-system description object
  173. *
  174. * This function returns %0 on success and a negative error code on failure.
  175. */
  176. static int layout_cnodes(struct ubifs_info *c)
  177. {
  178. int lnum, offs, len, alen, done_lsave, done_ltab, err;
  179. struct ubifs_cnode *cnode;
  180. err = dbg_chk_lpt_sz(c, 0, 0);
  181. if (err)
  182. return err;
  183. cnode = c->lpt_cnext;
  184. if (!cnode)
  185. return 0;
  186. lnum = c->nhead_lnum;
  187. offs = c->nhead_offs;
  188. /* Try to place lsave and ltab nicely */
  189. done_lsave = !c->big_lpt;
  190. done_ltab = 0;
  191. if (!done_lsave && offs + c->lsave_sz <= c->leb_size) {
  192. done_lsave = 1;
  193. c->lsave_lnum = lnum;
  194. c->lsave_offs = offs;
  195. offs += c->lsave_sz;
  196. dbg_chk_lpt_sz(c, 1, c->lsave_sz);
  197. }
  198. if (offs + c->ltab_sz <= c->leb_size) {
  199. done_ltab = 1;
  200. c->ltab_lnum = lnum;
  201. c->ltab_offs = offs;
  202. offs += c->ltab_sz;
  203. dbg_chk_lpt_sz(c, 1, c->ltab_sz);
  204. }
  205. do {
  206. if (cnode->level) {
  207. len = c->nnode_sz;
  208. c->dirty_nn_cnt -= 1;
  209. } else {
  210. len = c->pnode_sz;
  211. c->dirty_pn_cnt -= 1;
  212. }
  213. while (offs + len > c->leb_size) {
  214. alen = ALIGN(offs, c->min_io_size);
  215. upd_ltab(c, lnum, c->leb_size - alen, alen - offs);
  216. dbg_chk_lpt_sz(c, 2, c->leb_size - offs);
  217. err = alloc_lpt_leb(c, &lnum);
  218. if (err)
  219. goto no_space;
  220. offs = 0;
  221. ubifs_assert(lnum >= c->lpt_first &&
  222. lnum <= c->lpt_last);
  223. /* Try to place lsave and ltab nicely */
  224. if (!done_lsave) {
  225. done_lsave = 1;
  226. c->lsave_lnum = lnum;
  227. c->lsave_offs = offs;
  228. offs += c->lsave_sz;
  229. dbg_chk_lpt_sz(c, 1, c->lsave_sz);
  230. continue;
  231. }
  232. if (!done_ltab) {
  233. done_ltab = 1;
  234. c->ltab_lnum = lnum;
  235. c->ltab_offs = offs;
  236. offs += c->ltab_sz;
  237. dbg_chk_lpt_sz(c, 1, c->ltab_sz);
  238. continue;
  239. }
  240. break;
  241. }
  242. if (cnode->parent) {
  243. cnode->parent->nbranch[cnode->iip].lnum = lnum;
  244. cnode->parent->nbranch[cnode->iip].offs = offs;
  245. } else {
  246. c->lpt_lnum = lnum;
  247. c->lpt_offs = offs;
  248. }
  249. offs += len;
  250. dbg_chk_lpt_sz(c, 1, len);
  251. cnode = cnode->cnext;
  252. } while (cnode && cnode != c->lpt_cnext);
  253. /* Make sure to place LPT's save table */
  254. if (!done_lsave) {
  255. if (offs + c->lsave_sz > c->leb_size) {
  256. alen = ALIGN(offs, c->min_io_size);
  257. upd_ltab(c, lnum, c->leb_size - alen, alen - offs);
  258. dbg_chk_lpt_sz(c, 2, c->leb_size - offs);
  259. err = alloc_lpt_leb(c, &lnum);
  260. if (err)
  261. goto no_space;
  262. offs = 0;
  263. ubifs_assert(lnum >= c->lpt_first &&
  264. lnum <= c->lpt_last);
  265. }
  266. done_lsave = 1;
  267. c->lsave_lnum = lnum;
  268. c->lsave_offs = offs;
  269. offs += c->lsave_sz;
  270. dbg_chk_lpt_sz(c, 1, c->lsave_sz);
  271. }
  272. /* Make sure to place LPT's own lprops table */
  273. if (!done_ltab) {
  274. if (offs + c->ltab_sz > c->leb_size) {
  275. alen = ALIGN(offs, c->min_io_size);
  276. upd_ltab(c, lnum, c->leb_size - alen, alen - offs);
  277. dbg_chk_lpt_sz(c, 2, c->leb_size - offs);
  278. err = alloc_lpt_leb(c, &lnum);
  279. if (err)
  280. goto no_space;
  281. offs = 0;
  282. ubifs_assert(lnum >= c->lpt_first &&
  283. lnum <= c->lpt_last);
  284. }
  285. done_ltab = 1;
  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("LPT out of space at LEB %d:%d needing %d, done_ltab %d, "
  300. "done_lsave %d", 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, UBI_SHORTTERM);
  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(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_CNODE, &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, c->leb_size - offs);
  450. err = realloc_lpt_leb(c, &lnum);
  451. if (err)
  452. goto no_space;
  453. offs = from = 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, c->leb_size - offs);
  476. err = realloc_lpt_leb(c, &lnum);
  477. if (err)
  478. goto no_space;
  479. offs = from = 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 at LEB %d:%d needing %d, done_ltab "
  512. "%d, done_lsave %d", lnum, offs, len, done_ltab, done_lsave);
  513. ubifs_dump_lpt_info(c);
  514. ubifs_dump_lpt_lebs(c);
  515. dump_stack();
  516. return err;
  517. }
  518. /**
  519. * next_pnode_to_dirty - find next pnode to dirty.
  520. * @c: UBIFS file-system description object
  521. * @pnode: pnode
  522. *
  523. * This function returns the next pnode to dirty or %NULL if there are no more
  524. * pnodes. Note that pnodes that have never been written (lnum == 0) are
  525. * skipped.
  526. */
  527. static struct ubifs_pnode *next_pnode_to_dirty(struct ubifs_info *c,
  528. struct ubifs_pnode *pnode)
  529. {
  530. struct ubifs_nnode *nnode;
  531. int iip;
  532. /* Try to go right */
  533. nnode = pnode->parent;
  534. for (iip = pnode->iip + 1; iip < UBIFS_LPT_FANOUT; iip++) {
  535. if (nnode->nbranch[iip].lnum)
  536. return ubifs_get_pnode(c, nnode, iip);
  537. }
  538. /* Go up while can't go right */
  539. do {
  540. iip = nnode->iip + 1;
  541. nnode = nnode->parent;
  542. if (!nnode)
  543. return NULL;
  544. for (; iip < UBIFS_LPT_FANOUT; iip++) {
  545. if (nnode->nbranch[iip].lnum)
  546. break;
  547. }
  548. } while (iip >= UBIFS_LPT_FANOUT);
  549. /* Go right */
  550. nnode = ubifs_get_nnode(c, nnode, iip);
  551. if (IS_ERR(nnode))
  552. return (void *)nnode;
  553. /* Go down to level 1 */
  554. while (nnode->level > 1) {
  555. for (iip = 0; iip < UBIFS_LPT_FANOUT; iip++) {
  556. if (nnode->nbranch[iip].lnum)
  557. break;
  558. }
  559. if (iip >= UBIFS_LPT_FANOUT) {
  560. /*
  561. * Should not happen, but we need to keep going
  562. * if it does.
  563. */
  564. iip = 0;
  565. }
  566. nnode = ubifs_get_nnode(c, nnode, iip);
  567. if (IS_ERR(nnode))
  568. return (void *)nnode;
  569. }
  570. for (iip = 0; iip < UBIFS_LPT_FANOUT; iip++)
  571. if (nnode->nbranch[iip].lnum)
  572. break;
  573. if (iip >= UBIFS_LPT_FANOUT)
  574. /* Should not happen, but we need to keep going if it does */
  575. iip = 0;
  576. return ubifs_get_pnode(c, nnode, iip);
  577. }
  578. /**
  579. * pnode_lookup - lookup a pnode in the LPT.
  580. * @c: UBIFS file-system description object
  581. * @i: pnode number (0 to main_lebs - 1)
  582. *
  583. * This function returns a pointer to the pnode on success or a negative
  584. * error code on failure.
  585. */
  586. static struct ubifs_pnode *pnode_lookup(struct ubifs_info *c, int i)
  587. {
  588. int err, h, iip, shft;
  589. struct ubifs_nnode *nnode;
  590. if (!c->nroot) {
  591. err = ubifs_read_nnode(c, NULL, 0);
  592. if (err)
  593. return ERR_PTR(err);
  594. }
  595. i <<= UBIFS_LPT_FANOUT_SHIFT;
  596. nnode = c->nroot;
  597. shft = c->lpt_hght * UBIFS_LPT_FANOUT_SHIFT;
  598. for (h = 1; h < c->lpt_hght; h++) {
  599. iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
  600. shft -= UBIFS_LPT_FANOUT_SHIFT;
  601. nnode = ubifs_get_nnode(c, nnode, iip);
  602. if (IS_ERR(nnode))
  603. return ERR_CAST(nnode);
  604. }
  605. iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
  606. return ubifs_get_pnode(c, nnode, iip);
  607. }
  608. /**
  609. * add_pnode_dirt - add dirty space to LPT LEB properties.
  610. * @c: UBIFS file-system description object
  611. * @pnode: pnode for which to add dirt
  612. */
  613. static void add_pnode_dirt(struct ubifs_info *c, struct ubifs_pnode *pnode)
  614. {
  615. ubifs_add_lpt_dirt(c, pnode->parent->nbranch[pnode->iip].lnum,
  616. c->pnode_sz);
  617. }
  618. /**
  619. * do_make_pnode_dirty - mark a pnode dirty.
  620. * @c: UBIFS file-system description object
  621. * @pnode: pnode to mark dirty
  622. */
  623. static void do_make_pnode_dirty(struct ubifs_info *c, struct ubifs_pnode *pnode)
  624. {
  625. /* Assumes cnext list is empty i.e. not called during commit */
  626. if (!test_and_set_bit(DIRTY_CNODE, &pnode->flags)) {
  627. struct ubifs_nnode *nnode;
  628. c->dirty_pn_cnt += 1;
  629. add_pnode_dirt(c, pnode);
  630. /* Mark parent and ancestors dirty too */
  631. nnode = pnode->parent;
  632. while (nnode) {
  633. if (!test_and_set_bit(DIRTY_CNODE, &nnode->flags)) {
  634. c->dirty_nn_cnt += 1;
  635. ubifs_add_nnode_dirt(c, nnode);
  636. nnode = nnode->parent;
  637. } else
  638. break;
  639. }
  640. }
  641. }
  642. /**
  643. * make_tree_dirty - mark the entire LEB properties tree dirty.
  644. * @c: UBIFS file-system description object
  645. *
  646. * This function is used by the "small" LPT model to cause the entire LEB
  647. * properties tree to be written. The "small" LPT model does not use LPT
  648. * garbage collection because it is more efficient to write the entire tree
  649. * (because it is small).
  650. *
  651. * This function returns %0 on success and a negative error code on failure.
  652. */
  653. static int make_tree_dirty(struct ubifs_info *c)
  654. {
  655. struct ubifs_pnode *pnode;
  656. pnode = pnode_lookup(c, 0);
  657. if (IS_ERR(pnode))
  658. return PTR_ERR(pnode);
  659. while (pnode) {
  660. do_make_pnode_dirty(c, pnode);
  661. pnode = next_pnode_to_dirty(c, pnode);
  662. if (IS_ERR(pnode))
  663. return PTR_ERR(pnode);
  664. }
  665. return 0;
  666. }
  667. /**
  668. * need_write_all - determine if the LPT area is running out of free space.
  669. * @c: UBIFS file-system description object
  670. *
  671. * This function returns %1 if the LPT area is running out of free space and %0
  672. * if it is not.
  673. */
  674. static int need_write_all(struct ubifs_info *c)
  675. {
  676. long long free = 0;
  677. int i;
  678. for (i = 0; i < c->lpt_lebs; i++) {
  679. if (i + c->lpt_first == c->nhead_lnum)
  680. free += c->leb_size - c->nhead_offs;
  681. else if (c->ltab[i].free == c->leb_size)
  682. free += c->leb_size;
  683. else if (c->ltab[i].free + c->ltab[i].dirty == c->leb_size)
  684. free += c->leb_size;
  685. }
  686. /* Less than twice the size left */
  687. if (free <= c->lpt_sz * 2)
  688. return 1;
  689. return 0;
  690. }
  691. /**
  692. * lpt_tgc_start - start trivial garbage collection of LPT LEBs.
  693. * @c: UBIFS file-system description object
  694. *
  695. * LPT trivial garbage collection is where a LPT LEB contains only dirty and
  696. * free space and so may be reused as soon as the next commit is completed.
  697. * This function is called during start commit to mark LPT LEBs for trivial GC.
  698. */
  699. static void lpt_tgc_start(struct ubifs_info *c)
  700. {
  701. int i;
  702. for (i = 0; i < c->lpt_lebs; i++) {
  703. if (i + c->lpt_first == c->nhead_lnum)
  704. continue;
  705. if (c->ltab[i].dirty > 0 &&
  706. c->ltab[i].free + c->ltab[i].dirty == c->leb_size) {
  707. c->ltab[i].tgc = 1;
  708. c->ltab[i].free = c->leb_size;
  709. c->ltab[i].dirty = 0;
  710. dbg_lp("LEB %d", i + c->lpt_first);
  711. }
  712. }
  713. }
  714. /**
  715. * lpt_tgc_end - end trivial garbage collection of LPT LEBs.
  716. * @c: UBIFS file-system description object
  717. *
  718. * LPT trivial garbage collection is where a LPT LEB contains only dirty and
  719. * free space and so may be reused as soon as the next commit is completed.
  720. * This function is called after the commit is completed (master node has been
  721. * written) and un-maps LPT LEBs that were marked for trivial GC.
  722. */
  723. static int lpt_tgc_end(struct ubifs_info *c)
  724. {
  725. int i, err;
  726. for (i = 0; i < c->lpt_lebs; i++)
  727. if (c->ltab[i].tgc) {
  728. err = ubifs_leb_unmap(c, i + c->lpt_first);
  729. if (err)
  730. return err;
  731. c->ltab[i].tgc = 0;
  732. dbg_lp("LEB %d", i + c->lpt_first);
  733. }
  734. return 0;
  735. }
  736. /**
  737. * populate_lsave - fill the lsave array with important LEB numbers.
  738. * @c: the UBIFS file-system description object
  739. *
  740. * This function is only called for the "big" model. It records a small number
  741. * of LEB numbers of important LEBs. Important LEBs are ones that are (from
  742. * most important to least important): empty, freeable, freeable index, dirty
  743. * index, dirty or free. Upon mount, we read this list of LEB numbers and bring
  744. * their pnodes into memory. That will stop us from having to scan the LPT
  745. * straight away. For the "small" model we assume that scanning the LPT is no
  746. * big deal.
  747. */
  748. static void populate_lsave(struct ubifs_info *c)
  749. {
  750. struct ubifs_lprops *lprops;
  751. struct ubifs_lpt_heap *heap;
  752. int i, cnt = 0;
  753. ubifs_assert(c->big_lpt);
  754. if (!(c->lpt_drty_flgs & LSAVE_DIRTY)) {
  755. c->lpt_drty_flgs |= LSAVE_DIRTY;
  756. ubifs_add_lpt_dirt(c, c->lsave_lnum, c->lsave_sz);
  757. }
  758. if (dbg_populate_lsave(c))
  759. return;
  760. list_for_each_entry(lprops, &c->empty_list, list) {
  761. c->lsave[cnt++] = lprops->lnum;
  762. if (cnt >= c->lsave_cnt)
  763. return;
  764. }
  765. list_for_each_entry(lprops, &c->freeable_list, list) {
  766. c->lsave[cnt++] = lprops->lnum;
  767. if (cnt >= c->lsave_cnt)
  768. return;
  769. }
  770. list_for_each_entry(lprops, &c->frdi_idx_list, list) {
  771. c->lsave[cnt++] = lprops->lnum;
  772. if (cnt >= c->lsave_cnt)
  773. return;
  774. }
  775. heap = &c->lpt_heap[LPROPS_DIRTY_IDX - 1];
  776. for (i = 0; i < heap->cnt; i++) {
  777. c->lsave[cnt++] = heap->arr[i]->lnum;
  778. if (cnt >= c->lsave_cnt)
  779. return;
  780. }
  781. heap = &c->lpt_heap[LPROPS_DIRTY - 1];
  782. for (i = 0; i < heap->cnt; i++) {
  783. c->lsave[cnt++] = heap->arr[i]->lnum;
  784. if (cnt >= c->lsave_cnt)
  785. return;
  786. }
  787. heap = &c->lpt_heap[LPROPS_FREE - 1];
  788. for (i = 0; i < heap->cnt; i++) {
  789. c->lsave[cnt++] = heap->arr[i]->lnum;
  790. if (cnt >= c->lsave_cnt)
  791. return;
  792. }
  793. /* Fill it up completely */
  794. while (cnt < c->lsave_cnt)
  795. c->lsave[cnt++] = c->main_first;
  796. }
  797. /**
  798. * nnode_lookup - lookup a nnode in the LPT.
  799. * @c: UBIFS file-system description object
  800. * @i: nnode number
  801. *
  802. * This function returns a pointer to the nnode on success or a negative
  803. * error code on failure.
  804. */
  805. static struct ubifs_nnode *nnode_lookup(struct ubifs_info *c, int i)
  806. {
  807. int err, iip;
  808. struct ubifs_nnode *nnode;
  809. if (!c->nroot) {
  810. err = ubifs_read_nnode(c, NULL, 0);
  811. if (err)
  812. return ERR_PTR(err);
  813. }
  814. nnode = c->nroot;
  815. while (1) {
  816. iip = i & (UBIFS_LPT_FANOUT - 1);
  817. i >>= UBIFS_LPT_FANOUT_SHIFT;
  818. if (!i)
  819. break;
  820. nnode = ubifs_get_nnode(c, nnode, iip);
  821. if (IS_ERR(nnode))
  822. return nnode;
  823. }
  824. return nnode;
  825. }
  826. /**
  827. * make_nnode_dirty - find a nnode and, if found, make it dirty.
  828. * @c: UBIFS file-system description object
  829. * @node_num: nnode number of nnode to make dirty
  830. * @lnum: LEB number where nnode was written
  831. * @offs: offset where nnode was written
  832. *
  833. * This function is used by LPT garbage collection. LPT garbage collection is
  834. * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
  835. * simply involves marking all the nodes in the LEB being garbage-collected as
  836. * dirty. The dirty nodes are written next commit, after which the LEB is free
  837. * to be reused.
  838. *
  839. * This function returns %0 on success and a negative error code on failure.
  840. */
  841. static int make_nnode_dirty(struct ubifs_info *c, int node_num, int lnum,
  842. int offs)
  843. {
  844. struct ubifs_nnode *nnode;
  845. nnode = nnode_lookup(c, node_num);
  846. if (IS_ERR(nnode))
  847. return PTR_ERR(nnode);
  848. if (nnode->parent) {
  849. struct ubifs_nbranch *branch;
  850. branch = &nnode->parent->nbranch[nnode->iip];
  851. if (branch->lnum != lnum || branch->offs != offs)
  852. return 0; /* nnode is obsolete */
  853. } else if (c->lpt_lnum != lnum || c->lpt_offs != offs)
  854. return 0; /* nnode is obsolete */
  855. /* Assumes cnext list is empty i.e. not called during commit */
  856. if (!test_and_set_bit(DIRTY_CNODE, &nnode->flags)) {
  857. c->dirty_nn_cnt += 1;
  858. ubifs_add_nnode_dirt(c, nnode);
  859. /* Mark parent and ancestors dirty too */
  860. nnode = nnode->parent;
  861. while (nnode) {
  862. if (!test_and_set_bit(DIRTY_CNODE, &nnode->flags)) {
  863. c->dirty_nn_cnt += 1;
  864. ubifs_add_nnode_dirt(c, nnode);
  865. nnode = nnode->parent;
  866. } else
  867. break;
  868. }
  869. }
  870. return 0;
  871. }
  872. /**
  873. * make_pnode_dirty - find a pnode and, if found, make it dirty.
  874. * @c: UBIFS file-system description object
  875. * @node_num: pnode number of pnode to make dirty
  876. * @lnum: LEB number where pnode was written
  877. * @offs: offset where pnode was written
  878. *
  879. * This function is used by LPT garbage collection. LPT garbage collection is
  880. * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
  881. * simply involves marking all the nodes in the LEB being garbage-collected as
  882. * dirty. The dirty nodes are written next commit, after which the LEB is free
  883. * to be reused.
  884. *
  885. * This function returns %0 on success and a negative error code on failure.
  886. */
  887. static int make_pnode_dirty(struct ubifs_info *c, int node_num, int lnum,
  888. int offs)
  889. {
  890. struct ubifs_pnode *pnode;
  891. struct ubifs_nbranch *branch;
  892. pnode = pnode_lookup(c, node_num);
  893. if (IS_ERR(pnode))
  894. return PTR_ERR(pnode);
  895. branch = &pnode->parent->nbranch[pnode->iip];
  896. if (branch->lnum != lnum || branch->offs != offs)
  897. return 0;
  898. do_make_pnode_dirty(c, pnode);
  899. return 0;
  900. }
  901. /**
  902. * make_ltab_dirty - make ltab node dirty.
  903. * @c: UBIFS file-system description object
  904. * @lnum: LEB number where ltab was written
  905. * @offs: offset where ltab was written
  906. *
  907. * This function is used by LPT garbage collection. LPT garbage collection is
  908. * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
  909. * simply involves marking all the nodes in the LEB being garbage-collected as
  910. * dirty. The dirty nodes are written next commit, after which the LEB is free
  911. * to be reused.
  912. *
  913. * This function returns %0 on success and a negative error code on failure.
  914. */
  915. static int make_ltab_dirty(struct ubifs_info *c, int lnum, int offs)
  916. {
  917. if (lnum != c->ltab_lnum || offs != c->ltab_offs)
  918. return 0; /* This ltab node is obsolete */
  919. if (!(c->lpt_drty_flgs & LTAB_DIRTY)) {
  920. c->lpt_drty_flgs |= LTAB_DIRTY;
  921. ubifs_add_lpt_dirt(c, c->ltab_lnum, c->ltab_sz);
  922. }
  923. return 0;
  924. }
  925. /**
  926. * make_lsave_dirty - make lsave node dirty.
  927. * @c: UBIFS file-system description object
  928. * @lnum: LEB number where lsave was written
  929. * @offs: offset where lsave was written
  930. *
  931. * This function is used by LPT garbage collection. LPT garbage collection is
  932. * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
  933. * simply involves marking all the nodes in the LEB being garbage-collected as
  934. * dirty. The dirty nodes are written next commit, after which the LEB is free
  935. * to be reused.
  936. *
  937. * This function returns %0 on success and a negative error code on failure.
  938. */
  939. static int make_lsave_dirty(struct ubifs_info *c, int lnum, int offs)
  940. {
  941. if (lnum != c->lsave_lnum || offs != c->lsave_offs)
  942. return 0; /* This lsave node is obsolete */
  943. if (!(c->lpt_drty_flgs & LSAVE_DIRTY)) {
  944. c->lpt_drty_flgs |= LSAVE_DIRTY;
  945. ubifs_add_lpt_dirt(c, c->lsave_lnum, c->lsave_sz);
  946. }
  947. return 0;
  948. }
  949. /**
  950. * make_node_dirty - make node dirty.
  951. * @c: UBIFS file-system description object
  952. * @node_type: LPT node type
  953. * @node_num: node number
  954. * @lnum: LEB number where node was written
  955. * @offs: offset where node was written
  956. *
  957. * This function is used by LPT garbage collection. LPT garbage collection is
  958. * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
  959. * simply involves marking all the nodes in the LEB being garbage-collected as
  960. * dirty. The dirty nodes are written next commit, after which the LEB is free
  961. * to be reused.
  962. *
  963. * This function returns %0 on success and a negative error code on failure.
  964. */
  965. static int make_node_dirty(struct ubifs_info *c, int node_type, int node_num,
  966. int lnum, int offs)
  967. {
  968. switch (node_type) {
  969. case UBIFS_LPT_NNODE:
  970. return make_nnode_dirty(c, node_num, lnum, offs);
  971. case UBIFS_LPT_PNODE:
  972. return make_pnode_dirty(c, node_num, lnum, offs);
  973. case UBIFS_LPT_LTAB:
  974. return make_ltab_dirty(c, lnum, offs);
  975. case UBIFS_LPT_LSAVE:
  976. return make_lsave_dirty(c, lnum, offs);
  977. }
  978. return -EINVAL;
  979. }
  980. /**
  981. * get_lpt_node_len - return the length of a node based on its type.
  982. * @c: UBIFS file-system description object
  983. * @node_type: LPT node type
  984. */
  985. static int get_lpt_node_len(const struct ubifs_info *c, int node_type)
  986. {
  987. switch (node_type) {
  988. case UBIFS_LPT_NNODE:
  989. return c->nnode_sz;
  990. case UBIFS_LPT_PNODE:
  991. return c->pnode_sz;
  992. case UBIFS_LPT_LTAB:
  993. return c->ltab_sz;
  994. case UBIFS_LPT_LSAVE:
  995. return c->lsave_sz;
  996. }
  997. return 0;
  998. }
  999. /**
  1000. * get_pad_len - return the length of padding in a buffer.
  1001. * @c: UBIFS file-system description object
  1002. * @buf: buffer
  1003. * @len: length of buffer
  1004. */
  1005. static int get_pad_len(const struct ubifs_info *c, uint8_t *buf, int len)
  1006. {
  1007. int offs, pad_len;
  1008. if (c->min_io_size == 1)
  1009. return 0;
  1010. offs = c->leb_size - len;
  1011. pad_len = ALIGN(offs, c->min_io_size) - offs;
  1012. return pad_len;
  1013. }
  1014. /**
  1015. * get_lpt_node_type - return type (and node number) of a node in a buffer.
  1016. * @c: UBIFS file-system description object
  1017. * @buf: buffer
  1018. * @node_num: node number is returned here
  1019. */
  1020. static int get_lpt_node_type(const struct ubifs_info *c, uint8_t *buf,
  1021. int *node_num)
  1022. {
  1023. uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
  1024. int pos = 0, node_type;
  1025. node_type = ubifs_unpack_bits(&addr, &pos, UBIFS_LPT_TYPE_BITS);
  1026. *node_num = ubifs_unpack_bits(&addr, &pos, c->pcnt_bits);
  1027. return node_type;
  1028. }
  1029. /**
  1030. * is_a_node - determine if a buffer contains a node.
  1031. * @c: UBIFS file-system description object
  1032. * @buf: buffer
  1033. * @len: length of buffer
  1034. *
  1035. * This function returns %1 if the buffer contains a node or %0 if it does not.
  1036. */
  1037. static int is_a_node(const struct ubifs_info *c, uint8_t *buf, int len)
  1038. {
  1039. uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
  1040. int pos = 0, node_type, node_len;
  1041. uint16_t crc, calc_crc;
  1042. if (len < UBIFS_LPT_CRC_BYTES + (UBIFS_LPT_TYPE_BITS + 7) / 8)
  1043. return 0;
  1044. node_type = ubifs_unpack_bits(&addr, &pos, UBIFS_LPT_TYPE_BITS);
  1045. if (node_type == UBIFS_LPT_NOT_A_NODE)
  1046. return 0;
  1047. node_len = get_lpt_node_len(c, node_type);
  1048. if (!node_len || node_len > len)
  1049. return 0;
  1050. pos = 0;
  1051. addr = buf;
  1052. crc = ubifs_unpack_bits(&addr, &pos, UBIFS_LPT_CRC_BITS);
  1053. calc_crc = crc16(-1, buf + UBIFS_LPT_CRC_BYTES,
  1054. node_len - UBIFS_LPT_CRC_BYTES);
  1055. if (crc != calc_crc)
  1056. return 0;
  1057. return 1;
  1058. }
  1059. /**
  1060. * lpt_gc_lnum - garbage collect a LPT LEB.
  1061. * @c: UBIFS file-system description object
  1062. * @lnum: LEB number to garbage collect
  1063. *
  1064. * LPT garbage collection is used only for the "big" LPT model
  1065. * (c->big_lpt == 1). Garbage collection simply involves marking all the nodes
  1066. * in the LEB being garbage-collected as dirty. The dirty nodes are written
  1067. * next commit, after which the LEB is free to be reused.
  1068. *
  1069. * This function returns %0 on success and a negative error code on failure.
  1070. */
  1071. static int lpt_gc_lnum(struct ubifs_info *c, int lnum)
  1072. {
  1073. int err, len = c->leb_size, node_type, node_num, node_len, offs;
  1074. void *buf = c->lpt_buf;
  1075. dbg_lp("LEB %d", lnum);
  1076. err = ubifs_leb_read(c, lnum, buf, 0, c->leb_size, 1);
  1077. if (err)
  1078. return err;
  1079. while (1) {
  1080. if (!is_a_node(c, buf, len)) {
  1081. int pad_len;
  1082. pad_len = get_pad_len(c, buf, len);
  1083. if (pad_len) {
  1084. buf += pad_len;
  1085. len -= pad_len;
  1086. continue;
  1087. }
  1088. return 0;
  1089. }
  1090. node_type = get_lpt_node_type(c, buf, &node_num);
  1091. node_len = get_lpt_node_len(c, node_type);
  1092. offs = c->leb_size - len;
  1093. ubifs_assert(node_len != 0);
  1094. mutex_lock(&c->lp_mutex);
  1095. err = make_node_dirty(c, node_type, node_num, lnum, offs);
  1096. mutex_unlock(&c->lp_mutex);
  1097. if (err)
  1098. return err;
  1099. buf += node_len;
  1100. len -= node_len;
  1101. }
  1102. return 0;
  1103. }
  1104. /**
  1105. * lpt_gc - LPT garbage collection.
  1106. * @c: UBIFS file-system description object
  1107. *
  1108. * Select a LPT LEB for LPT garbage collection and call 'lpt_gc_lnum()'.
  1109. * Returns %0 on success and a negative error code on failure.
  1110. */
  1111. static int lpt_gc(struct ubifs_info *c)
  1112. {
  1113. int i, lnum = -1, dirty = 0;
  1114. mutex_lock(&c->lp_mutex);
  1115. for (i = 0; i < c->lpt_lebs; i++) {
  1116. ubifs_assert(!c->ltab[i].tgc);
  1117. if (i + c->lpt_first == c->nhead_lnum ||
  1118. c->ltab[i].free + c->ltab[i].dirty == c->leb_size)
  1119. continue;
  1120. if (c->ltab[i].dirty > dirty) {
  1121. dirty = c->ltab[i].dirty;
  1122. lnum = i + c->lpt_first;
  1123. }
  1124. }
  1125. mutex_unlock(&c->lp_mutex);
  1126. if (lnum == -1)
  1127. return -ENOSPC;
  1128. return lpt_gc_lnum(c, lnum);
  1129. }
  1130. /**
  1131. * ubifs_lpt_start_commit - UBIFS commit starts.
  1132. * @c: the UBIFS file-system description object
  1133. *
  1134. * This function has to be called when UBIFS starts the commit operation.
  1135. * This function "freezes" all currently dirty LEB properties and does not
  1136. * change them anymore. Further changes are saved and tracked separately
  1137. * because they are not part of this commit. This function returns zero in case
  1138. * of success and a negative error code in case of failure.
  1139. */
  1140. int ubifs_lpt_start_commit(struct ubifs_info *c)
  1141. {
  1142. int err, cnt;
  1143. dbg_lp("");
  1144. mutex_lock(&c->lp_mutex);
  1145. err = dbg_chk_lpt_free_spc(c);
  1146. if (err)
  1147. goto out;
  1148. err = dbg_check_ltab(c);
  1149. if (err)
  1150. goto out;
  1151. if (c->check_lpt_free) {
  1152. /*
  1153. * We ensure there is enough free space in
  1154. * ubifs_lpt_post_commit() by marking nodes dirty. That
  1155. * information is lost when we unmount, so we also need
  1156. * to check free space once after mounting also.
  1157. */
  1158. c->check_lpt_free = 0;
  1159. while (need_write_all(c)) {
  1160. mutex_unlock(&c->lp_mutex);
  1161. err = lpt_gc(c);
  1162. if (err)
  1163. return err;
  1164. mutex_lock(&c->lp_mutex);
  1165. }
  1166. }
  1167. lpt_tgc_start(c);
  1168. if (!c->dirty_pn_cnt) {
  1169. dbg_cmt("no cnodes to commit");
  1170. err = 0;
  1171. goto out;
  1172. }
  1173. if (!c->big_lpt && need_write_all(c)) {
  1174. /* If needed, write everything */
  1175. err = make_tree_dirty(c);
  1176. if (err)
  1177. goto out;
  1178. lpt_tgc_start(c);
  1179. }
  1180. if (c->big_lpt)
  1181. populate_lsave(c);
  1182. cnt = get_cnodes_to_commit(c);
  1183. ubifs_assert(cnt != 0);
  1184. err = layout_cnodes(c);
  1185. if (err)
  1186. goto out;
  1187. /* Copy the LPT's own lprops for end commit to write */
  1188. memcpy(c->ltab_cmt, c->ltab,
  1189. sizeof(struct ubifs_lpt_lprops) * c->lpt_lebs);
  1190. c->lpt_drty_flgs &= ~(LTAB_DIRTY | LSAVE_DIRTY);
  1191. out:
  1192. mutex_unlock(&c->lp_mutex);
  1193. return err;
  1194. }
  1195. /**
  1196. * free_obsolete_cnodes - free obsolete cnodes for commit end.
  1197. * @c: UBIFS file-system description object
  1198. */
  1199. static void free_obsolete_cnodes(struct ubifs_info *c)
  1200. {
  1201. struct ubifs_cnode *cnode, *cnext;
  1202. cnext = c->lpt_cnext;
  1203. if (!cnext)
  1204. return;
  1205. do {
  1206. cnode = cnext;
  1207. cnext = cnode->cnext;
  1208. if (test_bit(OBSOLETE_CNODE, &cnode->flags))
  1209. kfree(cnode);
  1210. else
  1211. cnode->cnext = NULL;
  1212. } while (cnext != c->lpt_cnext);
  1213. c->lpt_cnext = NULL;
  1214. }
  1215. /**
  1216. * ubifs_lpt_end_commit - finish the commit operation.
  1217. * @c: the UBIFS file-system description object
  1218. *
  1219. * This function has to be called when the commit operation finishes. It
  1220. * flushes the changes which were "frozen" by 'ubifs_lprops_start_commit()' to
  1221. * the media. Returns zero in case of success and a negative error code in case
  1222. * of failure.
  1223. */
  1224. int ubifs_lpt_end_commit(struct ubifs_info *c)
  1225. {
  1226. int err;
  1227. dbg_lp("");
  1228. if (!c->lpt_cnext)
  1229. return 0;
  1230. err = write_cnodes(c);
  1231. if (err)
  1232. return err;
  1233. mutex_lock(&c->lp_mutex);
  1234. free_obsolete_cnodes(c);
  1235. mutex_unlock(&c->lp_mutex);
  1236. return 0;
  1237. }
  1238. /**
  1239. * ubifs_lpt_post_commit - post commit LPT trivial GC and LPT GC.
  1240. * @c: UBIFS file-system description object
  1241. *
  1242. * LPT trivial GC is completed after a commit. Also LPT GC is done after a
  1243. * commit for the "big" LPT model.
  1244. */
  1245. int ubifs_lpt_post_commit(struct ubifs_info *c)
  1246. {
  1247. int err;
  1248. mutex_lock(&c->lp_mutex);
  1249. err = lpt_tgc_end(c);
  1250. if (err)
  1251. goto out;
  1252. if (c->big_lpt)
  1253. while (need_write_all(c)) {
  1254. mutex_unlock(&c->lp_mutex);
  1255. err = lpt_gc(c);
  1256. if (err)
  1257. return err;
  1258. mutex_lock(&c->lp_mutex);
  1259. }
  1260. out:
  1261. mutex_unlock(&c->lp_mutex);
  1262. return err;
  1263. }
  1264. /**
  1265. * first_nnode - find the first nnode in memory.
  1266. * @c: UBIFS file-system description object
  1267. * @hght: height of tree where nnode found is returned here
  1268. *
  1269. * This function returns a pointer to the nnode found or %NULL if no nnode is
  1270. * found. This function is a helper to 'ubifs_lpt_free()'.
  1271. */
  1272. static struct ubifs_nnode *first_nnode(struct ubifs_info *c, int *hght)
  1273. {
  1274. struct ubifs_nnode *nnode;
  1275. int h, i, found;
  1276. nnode = c->nroot;
  1277. *hght = 0;
  1278. if (!nnode)
  1279. return NULL;
  1280. for (h = 1; h < c->lpt_hght; h++) {
  1281. found = 0;
  1282. for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
  1283. if (nnode->nbranch[i].nnode) {
  1284. found = 1;
  1285. nnode = nnode->nbranch[i].nnode;
  1286. *hght = h;
  1287. break;
  1288. }
  1289. }
  1290. if (!found)
  1291. break;
  1292. }
  1293. return nnode;
  1294. }
  1295. /**
  1296. * next_nnode - find the next nnode in memory.
  1297. * @c: UBIFS file-system description object
  1298. * @nnode: nnode from which to start.
  1299. * @hght: height of tree where nnode is, is passed and returned here
  1300. *
  1301. * This function returns a pointer to the nnode found or %NULL if no nnode is
  1302. * found. This function is a helper to 'ubifs_lpt_free()'.
  1303. */
  1304. static struct ubifs_nnode *next_nnode(struct ubifs_info *c,
  1305. struct ubifs_nnode *nnode, int *hght)
  1306. {
  1307. struct ubifs_nnode *parent;
  1308. int iip, h, i, found;
  1309. parent = nnode->parent;
  1310. if (!parent)
  1311. return NULL;
  1312. if (nnode->iip == UBIFS_LPT_FANOUT - 1) {
  1313. *hght -= 1;
  1314. return parent;
  1315. }
  1316. for (iip = nnode->iip + 1; iip < UBIFS_LPT_FANOUT; iip++) {
  1317. nnode = parent->nbranch[iip].nnode;
  1318. if (nnode)
  1319. break;
  1320. }
  1321. if (!nnode) {
  1322. *hght -= 1;
  1323. return parent;
  1324. }
  1325. for (h = *hght + 1; h < c->lpt_hght; h++) {
  1326. found = 0;
  1327. for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
  1328. if (nnode->nbranch[i].nnode) {
  1329. found = 1;
  1330. nnode = nnode->nbranch[i].nnode;
  1331. *hght = h;
  1332. break;
  1333. }
  1334. }
  1335. if (!found)
  1336. break;
  1337. }
  1338. return nnode;
  1339. }
  1340. /**
  1341. * ubifs_lpt_free - free resources owned by the LPT.
  1342. * @c: UBIFS file-system description object
  1343. * @wr_only: free only resources used for writing
  1344. */
  1345. void ubifs_lpt_free(struct ubifs_info *c, int wr_only)
  1346. {
  1347. struct ubifs_nnode *nnode;
  1348. int i, hght;
  1349. /* Free write-only things first */
  1350. free_obsolete_cnodes(c); /* Leftover from a failed commit */
  1351. vfree(c->ltab_cmt);
  1352. c->ltab_cmt = NULL;
  1353. vfree(c->lpt_buf);
  1354. c->lpt_buf = NULL;
  1355. kfree(c->lsave);
  1356. c->lsave = NULL;
  1357. if (wr_only)
  1358. return;
  1359. /* Now free the rest */
  1360. nnode = first_nnode(c, &hght);
  1361. while (nnode) {
  1362. for (i = 0; i < UBIFS_LPT_FANOUT; i++)
  1363. kfree(nnode->nbranch[i].nnode);
  1364. nnode = next_nnode(c, nnode, &hght);
  1365. }
  1366. for (i = 0; i < LPROPS_HEAP_CNT; i++)
  1367. kfree(c->lpt_heap[i].arr);
  1368. kfree(c->dirty_idx.arr);
  1369. kfree(c->nroot);
  1370. vfree(c->ltab);
  1371. kfree(c->lpt_nod_buf);
  1372. }
  1373. /*
  1374. * Everything below is related to debugging.
  1375. */
  1376. /**
  1377. * dbg_is_all_ff - determine if a buffer contains only 0xFF bytes.
  1378. * @buf: buffer
  1379. * @len: buffer length
  1380. */
  1381. static int dbg_is_all_ff(uint8_t *buf, int len)
  1382. {
  1383. int i;
  1384. for (i = 0; i < len; i++)
  1385. if (buf[i] != 0xff)
  1386. return 0;
  1387. return 1;
  1388. }
  1389. /**
  1390. * dbg_is_nnode_dirty - determine if a nnode is dirty.
  1391. * @c: the UBIFS file-system description object
  1392. * @lnum: LEB number where nnode was written
  1393. * @offs: offset where nnode was written
  1394. */
  1395. static int dbg_is_nnode_dirty(struct ubifs_info *c, int lnum, int offs)
  1396. {
  1397. struct ubifs_nnode *nnode;
  1398. int hght;
  1399. /* Entire tree is in memory so first_nnode / next_nnode are OK */
  1400. nnode = first_nnode(c, &hght);
  1401. for (; nnode; nnode = next_nnode(c, nnode, &hght)) {
  1402. struct ubifs_nbranch *branch;
  1403. cond_resched();
  1404. if (nnode->parent) {
  1405. branch = &nnode->parent->nbranch[nnode->iip];
  1406. if (branch->lnum != lnum || branch->offs != offs)
  1407. continue;
  1408. if (test_bit(DIRTY_CNODE, &nnode->flags))
  1409. return 1;
  1410. return 0;
  1411. } else {
  1412. if (c->lpt_lnum != lnum || c->lpt_offs != offs)
  1413. continue;
  1414. if (test_bit(DIRTY_CNODE, &nnode->flags))
  1415. return 1;
  1416. return 0;
  1417. }
  1418. }
  1419. return 1;
  1420. }
  1421. /**
  1422. * dbg_is_pnode_dirty - determine if a pnode is dirty.
  1423. * @c: the UBIFS file-system description object
  1424. * @lnum: LEB number where pnode was written
  1425. * @offs: offset where pnode was written
  1426. */
  1427. static int dbg_is_pnode_dirty(struct ubifs_info *c, int lnum, int offs)
  1428. {
  1429. int i, cnt;
  1430. cnt = DIV_ROUND_UP(c->main_lebs, UBIFS_LPT_FANOUT);
  1431. for (i = 0; i < cnt; i++) {
  1432. struct ubifs_pnode *pnode;
  1433. struct ubifs_nbranch *branch;
  1434. cond_resched();
  1435. pnode = pnode_lookup(c, i);
  1436. if (IS_ERR(pnode))
  1437. return PTR_ERR(pnode);
  1438. branch = &pnode->parent->nbranch[pnode->iip];
  1439. if (branch->lnum != lnum || branch->offs != offs)
  1440. continue;
  1441. if (test_bit(DIRTY_CNODE, &pnode->flags))
  1442. return 1;
  1443. return 0;
  1444. }
  1445. return 1;
  1446. }
  1447. /**
  1448. * dbg_is_ltab_dirty - determine if a ltab node is dirty.
  1449. * @c: the UBIFS file-system description object
  1450. * @lnum: LEB number where ltab node was written
  1451. * @offs: offset where ltab node was written
  1452. */
  1453. static int dbg_is_ltab_dirty(struct ubifs_info *c, int lnum, int offs)
  1454. {
  1455. if (lnum != c->ltab_lnum || offs != c->ltab_offs)
  1456. return 1;
  1457. return (c->lpt_drty_flgs & LTAB_DIRTY) != 0;
  1458. }
  1459. /**
  1460. * dbg_is_lsave_dirty - determine if a lsave node is dirty.
  1461. * @c: the UBIFS file-system description object
  1462. * @lnum: LEB number where lsave node was written
  1463. * @offs: offset where lsave node was written
  1464. */
  1465. static int dbg_is_lsave_dirty(struct ubifs_info *c, int lnum, int offs)
  1466. {
  1467. if (lnum != c->lsave_lnum || offs != c->lsave_offs)
  1468. return 1;
  1469. return (c->lpt_drty_flgs & LSAVE_DIRTY) != 0;
  1470. }
  1471. /**
  1472. * dbg_is_node_dirty - determine if a node is dirty.
  1473. * @c: the UBIFS file-system description object
  1474. * @node_type: node type
  1475. * @lnum: LEB number where node was written
  1476. * @offs: offset where node was written
  1477. */
  1478. static int dbg_is_node_dirty(struct ubifs_info *c, int node_type, int lnum,
  1479. int offs)
  1480. {
  1481. switch (node_type) {
  1482. case UBIFS_LPT_NNODE:
  1483. return dbg_is_nnode_dirty(c, lnum, offs);
  1484. case UBIFS_LPT_PNODE:
  1485. return dbg_is_pnode_dirty(c, lnum, offs);
  1486. case UBIFS_LPT_LTAB:
  1487. return dbg_is_ltab_dirty(c, lnum, offs);
  1488. case UBIFS_LPT_LSAVE:
  1489. return dbg_is_lsave_dirty(c, lnum, offs);
  1490. }
  1491. return 1;
  1492. }
  1493. /**
  1494. * dbg_check_ltab_lnum - check the ltab for a LPT LEB number.
  1495. * @c: the UBIFS file-system description object
  1496. * @lnum: LEB number where node was written
  1497. * @offs: offset where node was written
  1498. *
  1499. * This function returns %0 on success and a negative error code on failure.
  1500. */
  1501. static int dbg_check_ltab_lnum(struct ubifs_info *c, int lnum)
  1502. {
  1503. int err, len = c->leb_size, dirty = 0, node_type, node_num, node_len;
  1504. int ret;
  1505. void *buf, *p;
  1506. if (!dbg_is_chk_lprops(c))
  1507. return 0;
  1508. buf = p = __vmalloc(c->leb_size, GFP_NOFS, PAGE_KERNEL);
  1509. if (!buf) {
  1510. ubifs_err("cannot allocate memory for ltab checking");
  1511. return 0;
  1512. }
  1513. dbg_lp("LEB %d", lnum);
  1514. err = ubifs_leb_read(c, lnum, buf, 0, c->leb_size, 1);
  1515. if (err)
  1516. goto out;
  1517. while (1) {
  1518. if (!is_a_node(c, p, len)) {
  1519. int i, pad_len;
  1520. pad_len = get_pad_len(c, p, len);
  1521. if (pad_len) {
  1522. p += pad_len;
  1523. len -= pad_len;
  1524. dirty += pad_len;
  1525. continue;
  1526. }
  1527. if (!dbg_is_all_ff(p, len)) {
  1528. dbg_msg("invalid empty space in LEB %d at %d",
  1529. lnum, c->leb_size - len);
  1530. err = -EINVAL;
  1531. }
  1532. i = lnum - c->lpt_first;
  1533. if (len != c->ltab[i].free) {
  1534. dbg_msg("invalid free space in LEB %d "
  1535. "(free %d, expected %d)",
  1536. lnum, len, c->ltab[i].free);
  1537. err = -EINVAL;
  1538. }
  1539. if (dirty != c->ltab[i].dirty) {
  1540. dbg_msg("invalid dirty space in LEB %d "
  1541. "(dirty %d, expected %d)",
  1542. lnum, dirty, c->ltab[i].dirty);
  1543. err = -EINVAL;
  1544. }
  1545. goto out;
  1546. }
  1547. node_type = get_lpt_node_type(c, p, &node_num);
  1548. node_len = get_lpt_node_len(c, node_type);
  1549. ret = dbg_is_node_dirty(c, node_type, lnum, c->leb_size - len);
  1550. if (ret == 1)
  1551. dirty += node_len;
  1552. p += node_len;
  1553. len -= node_len;
  1554. }
  1555. err = 0;
  1556. out:
  1557. vfree(buf);
  1558. return err;
  1559. }
  1560. /**
  1561. * dbg_check_ltab - check the free and dirty space in the ltab.
  1562. * @c: the UBIFS file-system description object
  1563. *
  1564. * This function returns %0 on success and a negative error code on failure.
  1565. */
  1566. int dbg_check_ltab(struct ubifs_info *c)
  1567. {
  1568. int lnum, err, i, cnt;
  1569. if (!dbg_is_chk_lprops(c))
  1570. return 0;
  1571. /* Bring the entire tree into memory */
  1572. cnt = DIV_ROUND_UP(c->main_lebs, UBIFS_LPT_FANOUT);
  1573. for (i = 0; i < cnt; i++) {
  1574. struct ubifs_pnode *pnode;
  1575. pnode = pnode_lookup(c, i);
  1576. if (IS_ERR(pnode))
  1577. return PTR_ERR(pnode);
  1578. cond_resched();
  1579. }
  1580. /* Check nodes */
  1581. err = dbg_check_lpt_nodes(c, (struct ubifs_cnode *)c->nroot, 0, 0);
  1582. if (err)
  1583. return err;
  1584. /* Check each LEB */
  1585. for (lnum = c->lpt_first; lnum <= c->lpt_last; lnum++) {
  1586. err = dbg_check_ltab_lnum(c, lnum);
  1587. if (err) {
  1588. ubifs_err("failed at LEB %d", lnum);
  1589. return err;
  1590. }
  1591. }
  1592. dbg_lp("succeeded");
  1593. return 0;
  1594. }
  1595. /**
  1596. * dbg_chk_lpt_free_spc - check LPT free space is enough to write entire LPT.
  1597. * @c: the UBIFS file-system description object
  1598. *
  1599. * This function returns %0 on success and a negative error code on failure.
  1600. */
  1601. int dbg_chk_lpt_free_spc(struct ubifs_info *c)
  1602. {
  1603. long long free = 0;
  1604. int i;
  1605. if (!dbg_is_chk_lprops(c))
  1606. return 0;
  1607. for (i = 0; i < c->lpt_lebs; i++) {
  1608. if (c->ltab[i].tgc || c->ltab[i].cmt)
  1609. continue;
  1610. if (i + c->lpt_first == c->nhead_lnum)
  1611. free += c->leb_size - c->nhead_offs;
  1612. else if (c->ltab[i].free == c->leb_size)
  1613. free += c->leb_size;
  1614. }
  1615. if (free < c->lpt_sz) {
  1616. ubifs_err("LPT space error: free %lld lpt_sz %lld",
  1617. free, c->lpt_sz);
  1618. ubifs_dump_lpt_info(c);
  1619. ubifs_dump_lpt_lebs(c);
  1620. dump_stack();
  1621. return -EINVAL;
  1622. }
  1623. return 0;
  1624. }
  1625. /**
  1626. * dbg_chk_lpt_sz - check LPT does not write more than LPT size.
  1627. * @c: the UBIFS file-system description object
  1628. * @action: what to do
  1629. * @len: length written
  1630. *
  1631. * This function returns %0 on success and a negative error code on failure.
  1632. * The @action argument may be one of:
  1633. * o %0 - LPT debugging checking starts, initialize debugging variables;
  1634. * o %1 - wrote an LPT node, increase LPT size by @len bytes;
  1635. * o %2 - switched to a different LEB and wasted @len bytes;
  1636. * o %3 - check that we've written the right number of bytes.
  1637. * o %4 - wasted @len bytes;
  1638. */
  1639. int dbg_chk_lpt_sz(struct ubifs_info *c, int action, int len)
  1640. {
  1641. struct ubifs_debug_info *d = c->dbg;
  1642. long long chk_lpt_sz, lpt_sz;
  1643. int err = 0;
  1644. if (!dbg_is_chk_lprops(c))
  1645. return 0;
  1646. switch (action) {
  1647. case 0:
  1648. d->chk_lpt_sz = 0;
  1649. d->chk_lpt_sz2 = 0;
  1650. d->chk_lpt_lebs = 0;
  1651. d->chk_lpt_wastage = 0;
  1652. if (c->dirty_pn_cnt > c->pnode_cnt) {
  1653. ubifs_err("dirty pnodes %d exceed max %d",
  1654. c->dirty_pn_cnt, c->pnode_cnt);
  1655. err = -EINVAL;
  1656. }
  1657. if (c->dirty_nn_cnt > c->nnode_cnt) {
  1658. ubifs_err("dirty nnodes %d exceed max %d",
  1659. c->dirty_nn_cnt, c->nnode_cnt);
  1660. err = -EINVAL;
  1661. }
  1662. return err;
  1663. case 1:
  1664. d->chk_lpt_sz += len;
  1665. return 0;
  1666. case 2:
  1667. d->chk_lpt_sz += len;
  1668. d->chk_lpt_wastage += len;
  1669. d->chk_lpt_lebs += 1;
  1670. return 0;
  1671. case 3:
  1672. chk_lpt_sz = c->leb_size;
  1673. chk_lpt_sz *= d->chk_lpt_lebs;
  1674. chk_lpt_sz += len - c->nhead_offs;
  1675. if (d->chk_lpt_sz != chk_lpt_sz) {
  1676. ubifs_err("LPT wrote %lld but space used was %lld",
  1677. d->chk_lpt_sz, chk_lpt_sz);
  1678. err = -EINVAL;
  1679. }
  1680. if (d->chk_lpt_sz > c->lpt_sz) {
  1681. ubifs_err("LPT wrote %lld but lpt_sz is %lld",
  1682. d->chk_lpt_sz, c->lpt_sz);
  1683. err = -EINVAL;
  1684. }
  1685. if (d->chk_lpt_sz2 && d->chk_lpt_sz != d->chk_lpt_sz2) {
  1686. ubifs_err("LPT layout size %lld but wrote %lld",
  1687. d->chk_lpt_sz, d->chk_lpt_sz2);
  1688. err = -EINVAL;
  1689. }
  1690. if (d->chk_lpt_sz2 && d->new_nhead_offs != len) {
  1691. ubifs_err("LPT new nhead offs: expected %d was %d",
  1692. d->new_nhead_offs, len);
  1693. err = -EINVAL;
  1694. }
  1695. lpt_sz = (long long)c->pnode_cnt * c->pnode_sz;
  1696. lpt_sz += (long long)c->nnode_cnt * c->nnode_sz;
  1697. lpt_sz += c->ltab_sz;
  1698. if (c->big_lpt)
  1699. lpt_sz += c->lsave_sz;
  1700. if (d->chk_lpt_sz - d->chk_lpt_wastage > lpt_sz) {
  1701. ubifs_err("LPT chk_lpt_sz %lld + waste %lld exceeds %lld",
  1702. d->chk_lpt_sz, d->chk_lpt_wastage, lpt_sz);
  1703. err = -EINVAL;
  1704. }
  1705. if (err) {
  1706. ubifs_dump_lpt_info(c);
  1707. ubifs_dump_lpt_lebs(c);
  1708. dump_stack();
  1709. }
  1710. d->chk_lpt_sz2 = d->chk_lpt_sz;
  1711. d->chk_lpt_sz = 0;
  1712. d->chk_lpt_wastage = 0;
  1713. d->chk_lpt_lebs = 0;
  1714. d->new_nhead_offs = len;
  1715. return err;
  1716. case 4:
  1717. d->chk_lpt_sz += len;
  1718. d->chk_lpt_wastage += len;
  1719. return 0;
  1720. default:
  1721. return -EINVAL;
  1722. }
  1723. }
  1724. /**
  1725. * ubifs_dump_lpt_leb - dump an LPT LEB.
  1726. * @c: UBIFS file-system description object
  1727. * @lnum: LEB number to dump
  1728. *
  1729. * This function dumps an LEB from LPT area. Nodes in this area are very
  1730. * different to nodes in the main area (e.g., they do not have common headers,
  1731. * they do not have 8-byte alignments, etc), so we have a separate function to
  1732. * dump LPT area LEBs. Note, LPT has to be locked by the caller.
  1733. */
  1734. static void dump_lpt_leb(const struct ubifs_info *c, int lnum)
  1735. {
  1736. int err, len = c->leb_size, node_type, node_num, node_len, offs;
  1737. void *buf, *p;
  1738. printk(KERN_DEBUG "(pid %d) start dumping LEB %d\n",
  1739. current->pid, lnum);
  1740. buf = p = __vmalloc(c->leb_size, GFP_NOFS, PAGE_KERNEL);
  1741. if (!buf) {
  1742. ubifs_err("cannot allocate memory to dump LPT");
  1743. return;
  1744. }
  1745. err = ubifs_leb_read(c, lnum, buf, 0, c->leb_size, 1);
  1746. if (err)
  1747. goto out;
  1748. while (1) {
  1749. offs = c->leb_size - len;
  1750. if (!is_a_node(c, p, len)) {
  1751. int pad_len;
  1752. pad_len = get_pad_len(c, p, len);
  1753. if (pad_len) {
  1754. printk(KERN_DEBUG "LEB %d:%d, pad %d bytes\n",
  1755. lnum, offs, pad_len);
  1756. p += pad_len;
  1757. len -= pad_len;
  1758. continue;
  1759. }
  1760. if (len)
  1761. printk(KERN_DEBUG "LEB %d:%d, free %d bytes\n",
  1762. lnum, offs, len);
  1763. break;
  1764. }
  1765. node_type = get_lpt_node_type(c, p, &node_num);
  1766. switch (node_type) {
  1767. case UBIFS_LPT_PNODE:
  1768. {
  1769. node_len = c->pnode_sz;
  1770. if (c->big_lpt)
  1771. printk(KERN_DEBUG "LEB %d:%d, pnode num %d\n",
  1772. lnum, offs, node_num);
  1773. else
  1774. printk(KERN_DEBUG "LEB %d:%d, pnode\n",
  1775. lnum, offs);
  1776. break;
  1777. }
  1778. case UBIFS_LPT_NNODE:
  1779. {
  1780. int i;
  1781. struct ubifs_nnode nnode;
  1782. node_len = c->nnode_sz;
  1783. if (c->big_lpt)
  1784. printk(KERN_DEBUG "LEB %d:%d, nnode num %d, ",
  1785. lnum, offs, node_num);
  1786. else
  1787. printk(KERN_DEBUG "LEB %d:%d, nnode, ",
  1788. lnum, offs);
  1789. err = ubifs_unpack_nnode(c, p, &nnode);
  1790. for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
  1791. printk(KERN_CONT "%d:%d", nnode.nbranch[i].lnum,
  1792. nnode.nbranch[i].offs);
  1793. if (i != UBIFS_LPT_FANOUT - 1)
  1794. printk(KERN_CONT ", ");
  1795. }
  1796. printk(KERN_CONT "\n");
  1797. break;
  1798. }
  1799. case UBIFS_LPT_LTAB:
  1800. node_len = c->ltab_sz;
  1801. printk(KERN_DEBUG "LEB %d:%d, ltab\n",
  1802. lnum, offs);
  1803. break;
  1804. case UBIFS_LPT_LSAVE:
  1805. node_len = c->lsave_sz;
  1806. printk(KERN_DEBUG "LEB %d:%d, lsave len\n", lnum, offs);
  1807. break;
  1808. default:
  1809. ubifs_err("LPT node type %d not recognized", node_type);
  1810. goto out;
  1811. }
  1812. p += node_len;
  1813. len -= node_len;
  1814. }
  1815. printk(KERN_DEBUG "(pid %d) finish dumping LEB %d\n",
  1816. current->pid, lnum);
  1817. out:
  1818. vfree(buf);
  1819. return;
  1820. }
  1821. /**
  1822. * ubifs_dump_lpt_lebs - dump LPT lebs.
  1823. * @c: UBIFS file-system description object
  1824. *
  1825. * This function dumps all LPT LEBs. The caller has to make sure the LPT is
  1826. * locked.
  1827. */
  1828. void ubifs_dump_lpt_lebs(const struct ubifs_info *c)
  1829. {
  1830. int i;
  1831. printk(KERN_DEBUG "(pid %d) start dumping all LPT LEBs\n",
  1832. current->pid);
  1833. for (i = 0; i < c->lpt_lebs; i++)
  1834. dump_lpt_leb(c, i + c->lpt_first);
  1835. printk(KERN_DEBUG "(pid %d) finish dumping all LPT LEBs\n",
  1836. current->pid);
  1837. }
  1838. /**
  1839. * dbg_populate_lsave - debugging version of 'populate_lsave()'
  1840. * @c: UBIFS file-system description object
  1841. *
  1842. * This is a debugging version for 'populate_lsave()' which populates lsave
  1843. * with random LEBs instead of useful LEBs, which is good for test coverage.
  1844. * Returns zero if lsave has not been populated (this debugging feature is
  1845. * disabled) an non-zero if lsave has been populated.
  1846. */
  1847. static int dbg_populate_lsave(struct ubifs_info *c)
  1848. {
  1849. struct ubifs_lprops *lprops;
  1850. struct ubifs_lpt_heap *heap;
  1851. int i;
  1852. if (!dbg_is_chk_gen(c))
  1853. return 0;
  1854. if (random32() & 3)
  1855. return 0;
  1856. for (i = 0; i < c->lsave_cnt; i++)
  1857. c->lsave[i] = c->main_first;
  1858. list_for_each_entry(lprops, &c->empty_list, list)
  1859. c->lsave[random32() % c->lsave_cnt] = lprops->lnum;
  1860. list_for_each_entry(lprops, &c->freeable_list, list)
  1861. c->lsave[random32() % c->lsave_cnt] = lprops->lnum;
  1862. list_for_each_entry(lprops, &c->frdi_idx_list, list)
  1863. c->lsave[random32() % c->lsave_cnt] = lprops->lnum;
  1864. heap = &c->lpt_heap[LPROPS_DIRTY_IDX - 1];
  1865. for (i = 0; i < heap->cnt; i++)
  1866. c->lsave[random32() % c->lsave_cnt] = heap->arr[i]->lnum;
  1867. heap = &c->lpt_heap[LPROPS_DIRTY - 1];
  1868. for (i = 0; i < heap->cnt; i++)
  1869. c->lsave[random32() % c->lsave_cnt] = heap->arr[i]->lnum;
  1870. heap = &c->lpt_heap[LPROPS_FREE - 1];
  1871. for (i = 0; i < heap->cnt; i++)
  1872. c->lsave[random32() % c->lsave_cnt] = heap->arr[i]->lnum;
  1873. return 1;
  1874. }