lpt_commit.c 53 KB

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