lpt_commit.c 52 KB

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