lpt_commit.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648
  1. /*
  2. * This file is part of UBIFS.
  3. *
  4. * Copyright (C) 2006-2008 Nokia Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License version 2 as published by
  8. * the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with
  16. * this program; if not, write to the Free Software Foundation, Inc., 51
  17. * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  18. *
  19. * Authors: Adrian Hunter
  20. * Artem Bityutskiy (Битюцкий Артём)
  21. */
  22. /*
  23. * This file implements commit-related functionality of the LEB properties
  24. * subsystem.
  25. */
  26. #include <linux/crc16.h>
  27. #include "ubifs.h"
  28. /**
  29. * first_dirty_cnode - find first dirty cnode.
  30. * @c: UBIFS file-system description object
  31. * @nnode: nnode at which to start
  32. *
  33. * This function returns the first dirty cnode or %NULL if there is not one.
  34. */
  35. static struct ubifs_cnode *first_dirty_cnode(struct ubifs_nnode *nnode)
  36. {
  37. ubifs_assert(nnode);
  38. while (1) {
  39. int i, cont = 0;
  40. for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
  41. struct ubifs_cnode *cnode;
  42. cnode = nnode->nbranch[i].cnode;
  43. if (cnode &&
  44. test_bit(DIRTY_CNODE, &cnode->flags)) {
  45. if (cnode->level == 0)
  46. return cnode;
  47. nnode = (struct ubifs_nnode *)cnode;
  48. cont = 1;
  49. break;
  50. }
  51. }
  52. if (!cont)
  53. return (struct ubifs_cnode *)nnode;
  54. }
  55. }
  56. /**
  57. * next_dirty_cnode - find next dirty cnode.
  58. * @cnode: cnode from which to begin searching
  59. *
  60. * This function returns the next dirty cnode or %NULL if there is not one.
  61. */
  62. static struct ubifs_cnode *next_dirty_cnode(struct ubifs_cnode *cnode)
  63. {
  64. struct ubifs_nnode *nnode;
  65. int i;
  66. ubifs_assert(cnode);
  67. nnode = cnode->parent;
  68. if (!nnode)
  69. return NULL;
  70. for (i = cnode->iip + 1; i < UBIFS_LPT_FANOUT; i++) {
  71. cnode = nnode->nbranch[i].cnode;
  72. if (cnode && test_bit(DIRTY_CNODE, &cnode->flags)) {
  73. if (cnode->level == 0)
  74. return cnode; /* cnode is a pnode */
  75. /* cnode is a nnode */
  76. return first_dirty_cnode((struct ubifs_nnode *)cnode);
  77. }
  78. }
  79. return (struct ubifs_cnode *)nnode;
  80. }
  81. /**
  82. * get_cnodes_to_commit - create list of dirty cnodes to commit.
  83. * @c: UBIFS file-system description object
  84. *
  85. * This function returns the number of cnodes to commit.
  86. */
  87. static int get_cnodes_to_commit(struct ubifs_info *c)
  88. {
  89. struct ubifs_cnode *cnode, *cnext;
  90. int cnt = 0;
  91. if (!c->nroot)
  92. return 0;
  93. if (!test_bit(DIRTY_CNODE, &c->nroot->flags))
  94. return 0;
  95. c->lpt_cnext = first_dirty_cnode(c->nroot);
  96. cnode = c->lpt_cnext;
  97. if (!cnode)
  98. return 0;
  99. cnt += 1;
  100. while (1) {
  101. ubifs_assert(!test_bit(COW_ZNODE, &cnode->flags));
  102. __set_bit(COW_ZNODE, &cnode->flags);
  103. cnext = next_dirty_cnode(cnode);
  104. if (!cnext) {
  105. cnode->cnext = c->lpt_cnext;
  106. break;
  107. }
  108. cnode->cnext = cnext;
  109. cnode = cnext;
  110. cnt += 1;
  111. }
  112. dbg_cmt("committing %d cnodes", cnt);
  113. dbg_lp("committing %d cnodes", cnt);
  114. ubifs_assert(cnt == c->dirty_nn_cnt + c->dirty_pn_cnt);
  115. return cnt;
  116. }
  117. /**
  118. * upd_ltab - update LPT LEB properties.
  119. * @c: UBIFS file-system description object
  120. * @lnum: LEB number
  121. * @free: amount of free space
  122. * @dirty: amount of dirty space to add
  123. */
  124. static void upd_ltab(struct ubifs_info *c, int lnum, int free, int dirty)
  125. {
  126. dbg_lp("LEB %d free %d dirty %d to %d +%d",
  127. lnum, c->ltab[lnum - c->lpt_first].free,
  128. c->ltab[lnum - c->lpt_first].dirty, free, dirty);
  129. ubifs_assert(lnum >= c->lpt_first && lnum <= c->lpt_last);
  130. c->ltab[lnum - c->lpt_first].free = free;
  131. c->ltab[lnum - c->lpt_first].dirty += dirty;
  132. }
  133. /**
  134. * alloc_lpt_leb - allocate an LPT LEB that is empty.
  135. * @c: UBIFS file-system description object
  136. * @lnum: LEB number is passed and returned here
  137. *
  138. * This function finds the next empty LEB in the ltab starting from @lnum. If a
  139. * an empty LEB is found it is returned in @lnum and the function returns %0.
  140. * Otherwise the function returns -ENOSPC. Note however, that LPT is designed
  141. * never to run out of space.
  142. */
  143. static int alloc_lpt_leb(struct ubifs_info *c, int *lnum)
  144. {
  145. int i, n;
  146. n = *lnum - c->lpt_first + 1;
  147. for (i = n; i < c->lpt_lebs; i++) {
  148. if (c->ltab[i].tgc || c->ltab[i].cmt)
  149. continue;
  150. if (c->ltab[i].free == c->leb_size) {
  151. c->ltab[i].cmt = 1;
  152. *lnum = i + c->lpt_first;
  153. return 0;
  154. }
  155. }
  156. for (i = 0; i < n; i++) {
  157. if (c->ltab[i].tgc || c->ltab[i].cmt)
  158. continue;
  159. if (c->ltab[i].free == c->leb_size) {
  160. c->ltab[i].cmt = 1;
  161. *lnum = i + c->lpt_first;
  162. return 0;
  163. }
  164. }
  165. dbg_err("last LEB %d", *lnum);
  166. dump_stack();
  167. return -ENOSPC;
  168. }
  169. /**
  170. * layout_cnodes - layout cnodes for commit.
  171. * @c: UBIFS file-system description object
  172. *
  173. * This function returns %0 on success and a negative error code on failure.
  174. */
  175. static int layout_cnodes(struct ubifs_info *c)
  176. {
  177. int lnum, offs, len, alen, done_lsave, done_ltab, err;
  178. struct ubifs_cnode *cnode;
  179. cnode = c->lpt_cnext;
  180. if (!cnode)
  181. return 0;
  182. lnum = c->nhead_lnum;
  183. offs = c->nhead_offs;
  184. /* Try to place lsave and ltab nicely */
  185. done_lsave = !c->big_lpt;
  186. done_ltab = 0;
  187. if (!done_lsave && offs + c->lsave_sz <= c->leb_size) {
  188. done_lsave = 1;
  189. c->lsave_lnum = lnum;
  190. c->lsave_offs = offs;
  191. offs += c->lsave_sz;
  192. }
  193. if (offs + c->ltab_sz <= c->leb_size) {
  194. done_ltab = 1;
  195. c->ltab_lnum = lnum;
  196. c->ltab_offs = offs;
  197. offs += c->ltab_sz;
  198. }
  199. do {
  200. if (cnode->level) {
  201. len = c->nnode_sz;
  202. c->dirty_nn_cnt -= 1;
  203. } else {
  204. len = c->pnode_sz;
  205. c->dirty_pn_cnt -= 1;
  206. }
  207. while (offs + len > c->leb_size) {
  208. alen = ALIGN(offs, c->min_io_size);
  209. upd_ltab(c, lnum, c->leb_size - alen, alen - offs);
  210. err = alloc_lpt_leb(c, &lnum);
  211. if (err)
  212. return err;
  213. offs = 0;
  214. ubifs_assert(lnum >= c->lpt_first &&
  215. lnum <= c->lpt_last);
  216. /* Try to place lsave and ltab nicely */
  217. if (!done_lsave) {
  218. done_lsave = 1;
  219. c->lsave_lnum = lnum;
  220. c->lsave_offs = offs;
  221. offs += c->lsave_sz;
  222. continue;
  223. }
  224. if (!done_ltab) {
  225. done_ltab = 1;
  226. c->ltab_lnum = lnum;
  227. c->ltab_offs = offs;
  228. offs += c->ltab_sz;
  229. continue;
  230. }
  231. break;
  232. }
  233. if (cnode->parent) {
  234. cnode->parent->nbranch[cnode->iip].lnum = lnum;
  235. cnode->parent->nbranch[cnode->iip].offs = offs;
  236. } else {
  237. c->lpt_lnum = lnum;
  238. c->lpt_offs = offs;
  239. }
  240. offs += len;
  241. cnode = cnode->cnext;
  242. } while (cnode && cnode != c->lpt_cnext);
  243. /* Make sure to place LPT's save table */
  244. if (!done_lsave) {
  245. if (offs + c->lsave_sz > c->leb_size) {
  246. alen = ALIGN(offs, c->min_io_size);
  247. upd_ltab(c, lnum, c->leb_size - alen, alen - offs);
  248. err = alloc_lpt_leb(c, &lnum);
  249. if (err)
  250. return err;
  251. offs = 0;
  252. ubifs_assert(lnum >= c->lpt_first &&
  253. lnum <= c->lpt_last);
  254. }
  255. done_lsave = 1;
  256. c->lsave_lnum = lnum;
  257. c->lsave_offs = offs;
  258. offs += c->lsave_sz;
  259. }
  260. /* Make sure to place LPT's own lprops table */
  261. if (!done_ltab) {
  262. if (offs + c->ltab_sz > c->leb_size) {
  263. alen = ALIGN(offs, c->min_io_size);
  264. upd_ltab(c, lnum, c->leb_size - alen, alen - offs);
  265. err = alloc_lpt_leb(c, &lnum);
  266. if (err)
  267. return err;
  268. offs = 0;
  269. ubifs_assert(lnum >= c->lpt_first &&
  270. lnum <= c->lpt_last);
  271. }
  272. done_ltab = 1;
  273. c->ltab_lnum = lnum;
  274. c->ltab_offs = offs;
  275. offs += c->ltab_sz;
  276. }
  277. alen = ALIGN(offs, c->min_io_size);
  278. upd_ltab(c, lnum, c->leb_size - alen, alen - offs);
  279. return 0;
  280. }
  281. /**
  282. * realloc_lpt_leb - allocate an LPT LEB that is empty.
  283. * @c: UBIFS file-system description object
  284. * @lnum: LEB number is passed and returned here
  285. *
  286. * This function duplicates exactly the results of the function alloc_lpt_leb.
  287. * It is used during end commit to reallocate the same LEB numbers that were
  288. * allocated by alloc_lpt_leb during start commit.
  289. *
  290. * This function finds the next LEB that was allocated by the alloc_lpt_leb
  291. * function starting from @lnum. If a LEB is found it is returned in @lnum and
  292. * the function returns %0. Otherwise the function returns -ENOSPC.
  293. * Note however, that LPT is designed never to run out of space.
  294. */
  295. static int realloc_lpt_leb(struct ubifs_info *c, int *lnum)
  296. {
  297. int i, n;
  298. n = *lnum - c->lpt_first + 1;
  299. for (i = n; i < c->lpt_lebs; i++)
  300. if (c->ltab[i].cmt) {
  301. c->ltab[i].cmt = 0;
  302. *lnum = i + c->lpt_first;
  303. return 0;
  304. }
  305. for (i = 0; i < n; i++)
  306. if (c->ltab[i].cmt) {
  307. c->ltab[i].cmt = 0;
  308. *lnum = i + c->lpt_first;
  309. return 0;
  310. }
  311. dbg_err("last LEB %d", *lnum);
  312. dump_stack();
  313. return -ENOSPC;
  314. }
  315. /**
  316. * write_cnodes - write cnodes for commit.
  317. * @c: UBIFS file-system description object
  318. *
  319. * This function returns %0 on success and a negative error code on failure.
  320. */
  321. static int write_cnodes(struct ubifs_info *c)
  322. {
  323. int lnum, offs, len, from, err, wlen, alen, done_ltab, done_lsave;
  324. struct ubifs_cnode *cnode;
  325. void *buf = c->lpt_buf;
  326. cnode = c->lpt_cnext;
  327. if (!cnode)
  328. return 0;
  329. lnum = c->nhead_lnum;
  330. offs = c->nhead_offs;
  331. from = offs;
  332. /* Ensure empty LEB is unmapped */
  333. if (offs == 0) {
  334. err = ubifs_leb_unmap(c, lnum);
  335. if (err)
  336. return err;
  337. }
  338. /* Try to place lsave and ltab nicely */
  339. done_lsave = !c->big_lpt;
  340. done_ltab = 0;
  341. if (!done_lsave && offs + c->lsave_sz <= c->leb_size) {
  342. done_lsave = 1;
  343. ubifs_pack_lsave(c, buf + offs, c->lsave);
  344. offs += c->lsave_sz;
  345. }
  346. if (offs + c->ltab_sz <= c->leb_size) {
  347. done_ltab = 1;
  348. ubifs_pack_ltab(c, buf + offs, c->ltab_cmt);
  349. offs += c->ltab_sz;
  350. }
  351. /* Loop for each cnode */
  352. do {
  353. if (cnode->level)
  354. len = c->nnode_sz;
  355. else
  356. len = c->pnode_sz;
  357. while (offs + len > c->leb_size) {
  358. wlen = offs - from;
  359. if (wlen) {
  360. alen = ALIGN(wlen, c->min_io_size);
  361. memset(buf + offs, 0xff, alen - wlen);
  362. err = ubifs_leb_write(c, lnum, buf + from, from,
  363. alen, UBI_SHORTTERM);
  364. if (err)
  365. return err;
  366. }
  367. err = realloc_lpt_leb(c, &lnum);
  368. if (err)
  369. return err;
  370. offs = 0;
  371. from = 0;
  372. ubifs_assert(lnum >= c->lpt_first &&
  373. lnum <= c->lpt_last);
  374. err = ubifs_leb_unmap(c, lnum);
  375. if (err)
  376. return err;
  377. /* Try to place lsave and ltab nicely */
  378. if (!done_lsave) {
  379. done_lsave = 1;
  380. ubifs_pack_lsave(c, buf + offs, c->lsave);
  381. offs += c->lsave_sz;
  382. continue;
  383. }
  384. if (!done_ltab) {
  385. done_ltab = 1;
  386. ubifs_pack_ltab(c, buf + offs, c->ltab_cmt);
  387. offs += c->ltab_sz;
  388. continue;
  389. }
  390. break;
  391. }
  392. if (cnode->level)
  393. ubifs_pack_nnode(c, buf + offs,
  394. (struct ubifs_nnode *)cnode);
  395. else
  396. ubifs_pack_pnode(c, buf + offs,
  397. (struct ubifs_pnode *)cnode);
  398. /*
  399. * The reason for the barriers is the same as in case of TNC.
  400. * See comment in 'write_index()'. 'dirty_cow_nnode()' and
  401. * 'dirty_cow_pnode()' are the functions for which this is
  402. * important.
  403. */
  404. clear_bit(DIRTY_CNODE, &cnode->flags);
  405. smp_mb__before_clear_bit();
  406. clear_bit(COW_ZNODE, &cnode->flags);
  407. smp_mb__after_clear_bit();
  408. offs += len;
  409. cnode = cnode->cnext;
  410. } while (cnode && cnode != c->lpt_cnext);
  411. /* Make sure to place LPT's save table */
  412. if (!done_lsave) {
  413. if (offs + c->lsave_sz > c->leb_size) {
  414. wlen = offs - from;
  415. alen = ALIGN(wlen, c->min_io_size);
  416. memset(buf + offs, 0xff, alen - wlen);
  417. err = ubifs_leb_write(c, lnum, buf + from, from, alen,
  418. UBI_SHORTTERM);
  419. if (err)
  420. return err;
  421. err = realloc_lpt_leb(c, &lnum);
  422. if (err)
  423. return err;
  424. offs = 0;
  425. ubifs_assert(lnum >= c->lpt_first &&
  426. lnum <= c->lpt_last);
  427. err = ubifs_leb_unmap(c, lnum);
  428. if (err)
  429. return err;
  430. }
  431. done_lsave = 1;
  432. ubifs_pack_lsave(c, buf + offs, c->lsave);
  433. offs += c->lsave_sz;
  434. }
  435. /* Make sure to place LPT's own lprops table */
  436. if (!done_ltab) {
  437. if (offs + c->ltab_sz > c->leb_size) {
  438. wlen = offs - from;
  439. alen = ALIGN(wlen, c->min_io_size);
  440. memset(buf + offs, 0xff, alen - wlen);
  441. err = ubifs_leb_write(c, lnum, buf + from, from, alen,
  442. UBI_SHORTTERM);
  443. if (err)
  444. return err;
  445. err = realloc_lpt_leb(c, &lnum);
  446. if (err)
  447. return err;
  448. offs = 0;
  449. ubifs_assert(lnum >= c->lpt_first &&
  450. lnum <= c->lpt_last);
  451. err = ubifs_leb_unmap(c, lnum);
  452. if (err)
  453. return err;
  454. }
  455. done_ltab = 1;
  456. ubifs_pack_ltab(c, buf + offs, c->ltab_cmt);
  457. offs += c->ltab_sz;
  458. }
  459. /* Write remaining data in buffer */
  460. wlen = offs - from;
  461. alen = ALIGN(wlen, c->min_io_size);
  462. memset(buf + offs, 0xff, alen - wlen);
  463. err = ubifs_leb_write(c, lnum, buf + from, from, alen, UBI_SHORTTERM);
  464. if (err)
  465. return err;
  466. c->nhead_lnum = lnum;
  467. c->nhead_offs = ALIGN(offs, c->min_io_size);
  468. dbg_lp("LPT root is at %d:%d", c->lpt_lnum, c->lpt_offs);
  469. dbg_lp("LPT head is at %d:%d", c->nhead_lnum, c->nhead_offs);
  470. dbg_lp("LPT ltab is at %d:%d", c->ltab_lnum, c->ltab_offs);
  471. if (c->big_lpt)
  472. dbg_lp("LPT lsave is at %d:%d", c->lsave_lnum, c->lsave_offs);
  473. return 0;
  474. }
  475. /**
  476. * next_pnode - find next pnode.
  477. * @c: UBIFS file-system description object
  478. * @pnode: pnode
  479. *
  480. * This function returns the next pnode or %NULL if there are no more pnodes.
  481. */
  482. static struct ubifs_pnode *next_pnode(struct ubifs_info *c,
  483. struct ubifs_pnode *pnode)
  484. {
  485. struct ubifs_nnode *nnode;
  486. int iip;
  487. /* Try to go right */
  488. nnode = pnode->parent;
  489. iip = pnode->iip + 1;
  490. if (iip < UBIFS_LPT_FANOUT) {
  491. /* We assume here that LEB zero is never an LPT LEB */
  492. if (nnode->nbranch[iip].lnum)
  493. return ubifs_get_pnode(c, nnode, iip);
  494. else
  495. return NULL;
  496. }
  497. /* Go up while can't go right */
  498. do {
  499. iip = nnode->iip + 1;
  500. nnode = nnode->parent;
  501. if (!nnode)
  502. return NULL;
  503. /* We assume here that LEB zero is never an LPT LEB */
  504. } while (iip >= UBIFS_LPT_FANOUT || !nnode->nbranch[iip].lnum);
  505. /* Go right */
  506. nnode = ubifs_get_nnode(c, nnode, iip);
  507. if (IS_ERR(nnode))
  508. return (void *)nnode;
  509. /* Go down to level 1 */
  510. while (nnode->level > 1) {
  511. nnode = ubifs_get_nnode(c, nnode, 0);
  512. if (IS_ERR(nnode))
  513. return (void *)nnode;
  514. }
  515. return ubifs_get_pnode(c, nnode, 0);
  516. }
  517. /**
  518. * pnode_lookup - lookup a pnode in the LPT.
  519. * @c: UBIFS file-system description object
  520. * @i: pnode number (0 to main_lebs - 1)
  521. *
  522. * This function returns a pointer to the pnode on success or a negative
  523. * error code on failure.
  524. */
  525. static struct ubifs_pnode *pnode_lookup(struct ubifs_info *c, int i)
  526. {
  527. int err, h, iip, shft;
  528. struct ubifs_nnode *nnode;
  529. if (!c->nroot) {
  530. err = ubifs_read_nnode(c, NULL, 0);
  531. if (err)
  532. return ERR_PTR(err);
  533. }
  534. i <<= UBIFS_LPT_FANOUT_SHIFT;
  535. nnode = c->nroot;
  536. shft = c->lpt_hght * UBIFS_LPT_FANOUT_SHIFT;
  537. for (h = 1; h < c->lpt_hght; h++) {
  538. iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
  539. shft -= UBIFS_LPT_FANOUT_SHIFT;
  540. nnode = ubifs_get_nnode(c, nnode, iip);
  541. if (IS_ERR(nnode))
  542. return ERR_PTR(PTR_ERR(nnode));
  543. }
  544. iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
  545. return ubifs_get_pnode(c, nnode, iip);
  546. }
  547. /**
  548. * add_pnode_dirt - add dirty space to LPT LEB properties.
  549. * @c: UBIFS file-system description object
  550. * @pnode: pnode for which to add dirt
  551. */
  552. static void add_pnode_dirt(struct ubifs_info *c, struct ubifs_pnode *pnode)
  553. {
  554. ubifs_add_lpt_dirt(c, pnode->parent->nbranch[pnode->iip].lnum,
  555. c->pnode_sz);
  556. }
  557. /**
  558. * do_make_pnode_dirty - mark a pnode dirty.
  559. * @c: UBIFS file-system description object
  560. * @pnode: pnode to mark dirty
  561. */
  562. static void do_make_pnode_dirty(struct ubifs_info *c, struct ubifs_pnode *pnode)
  563. {
  564. /* Assumes cnext list is empty i.e. not called during commit */
  565. if (!test_and_set_bit(DIRTY_CNODE, &pnode->flags)) {
  566. struct ubifs_nnode *nnode;
  567. c->dirty_pn_cnt += 1;
  568. add_pnode_dirt(c, pnode);
  569. /* Mark parent and ancestors dirty too */
  570. nnode = pnode->parent;
  571. while (nnode) {
  572. if (!test_and_set_bit(DIRTY_CNODE, &nnode->flags)) {
  573. c->dirty_nn_cnt += 1;
  574. ubifs_add_nnode_dirt(c, nnode);
  575. nnode = nnode->parent;
  576. } else
  577. break;
  578. }
  579. }
  580. }
  581. /**
  582. * make_tree_dirty - mark the entire LEB properties tree dirty.
  583. * @c: UBIFS file-system description object
  584. *
  585. * This function is used by the "small" LPT model to cause the entire LEB
  586. * properties tree to be written. The "small" LPT model does not use LPT
  587. * garbage collection because it is more efficient to write the entire tree
  588. * (because it is small).
  589. *
  590. * This function returns %0 on success and a negative error code on failure.
  591. */
  592. static int make_tree_dirty(struct ubifs_info *c)
  593. {
  594. struct ubifs_pnode *pnode;
  595. pnode = pnode_lookup(c, 0);
  596. while (pnode) {
  597. do_make_pnode_dirty(c, pnode);
  598. pnode = next_pnode(c, pnode);
  599. if (IS_ERR(pnode))
  600. return PTR_ERR(pnode);
  601. }
  602. return 0;
  603. }
  604. /**
  605. * need_write_all - determine if the LPT area is running out of free space.
  606. * @c: UBIFS file-system description object
  607. *
  608. * This function returns %1 if the LPT area is running out of free space and %0
  609. * if it is not.
  610. */
  611. static int need_write_all(struct ubifs_info *c)
  612. {
  613. long long free = 0;
  614. int i;
  615. for (i = 0; i < c->lpt_lebs; i++) {
  616. if (i + c->lpt_first == c->nhead_lnum)
  617. free += c->leb_size - c->nhead_offs;
  618. else if (c->ltab[i].free == c->leb_size)
  619. free += c->leb_size;
  620. else if (c->ltab[i].free + c->ltab[i].dirty == c->leb_size)
  621. free += c->leb_size;
  622. }
  623. /* Less than twice the size left */
  624. if (free <= c->lpt_sz * 2)
  625. return 1;
  626. return 0;
  627. }
  628. /**
  629. * lpt_tgc_start - start trivial garbage collection of LPT LEBs.
  630. * @c: UBIFS file-system description object
  631. *
  632. * LPT trivial garbage collection is where a LPT LEB contains only dirty and
  633. * free space and so may be reused as soon as the next commit is completed.
  634. * This function is called during start commit to mark LPT LEBs for trivial GC.
  635. */
  636. static void lpt_tgc_start(struct ubifs_info *c)
  637. {
  638. int i;
  639. for (i = 0; i < c->lpt_lebs; i++) {
  640. if (i + c->lpt_first == c->nhead_lnum)
  641. continue;
  642. if (c->ltab[i].dirty > 0 &&
  643. c->ltab[i].free + c->ltab[i].dirty == c->leb_size) {
  644. c->ltab[i].tgc = 1;
  645. c->ltab[i].free = c->leb_size;
  646. c->ltab[i].dirty = 0;
  647. dbg_lp("LEB %d", i + c->lpt_first);
  648. }
  649. }
  650. }
  651. /**
  652. * lpt_tgc_end - end trivial garbage collection of LPT LEBs.
  653. * @c: UBIFS file-system description object
  654. *
  655. * LPT trivial garbage collection is where a LPT LEB contains only dirty and
  656. * free space and so may be reused as soon as the next commit is completed.
  657. * This function is called after the commit is completed (master node has been
  658. * written) and unmaps LPT LEBs that were marked for trivial GC.
  659. */
  660. static int lpt_tgc_end(struct ubifs_info *c)
  661. {
  662. int i, err;
  663. for (i = 0; i < c->lpt_lebs; i++)
  664. if (c->ltab[i].tgc) {
  665. err = ubifs_leb_unmap(c, i + c->lpt_first);
  666. if (err)
  667. return err;
  668. c->ltab[i].tgc = 0;
  669. dbg_lp("LEB %d", i + c->lpt_first);
  670. }
  671. return 0;
  672. }
  673. /**
  674. * populate_lsave - fill the lsave array with important LEB numbers.
  675. * @c: the UBIFS file-system description object
  676. *
  677. * This function is only called for the "big" model. It records a small number
  678. * of LEB numbers of important LEBs. Important LEBs are ones that are (from
  679. * most important to least important): empty, freeable, freeable index, dirty
  680. * index, dirty or free. Upon mount, we read this list of LEB numbers and bring
  681. * their pnodes into memory. That will stop us from having to scan the LPT
  682. * straight away. For the "small" model we assume that scanning the LPT is no
  683. * big deal.
  684. */
  685. static void populate_lsave(struct ubifs_info *c)
  686. {
  687. struct ubifs_lprops *lprops;
  688. struct ubifs_lpt_heap *heap;
  689. int i, cnt = 0;
  690. ubifs_assert(c->big_lpt);
  691. if (!(c->lpt_drty_flgs & LSAVE_DIRTY)) {
  692. c->lpt_drty_flgs |= LSAVE_DIRTY;
  693. ubifs_add_lpt_dirt(c, c->lsave_lnum, c->lsave_sz);
  694. }
  695. list_for_each_entry(lprops, &c->empty_list, list) {
  696. c->lsave[cnt++] = lprops->lnum;
  697. if (cnt >= c->lsave_cnt)
  698. return;
  699. }
  700. list_for_each_entry(lprops, &c->freeable_list, list) {
  701. c->lsave[cnt++] = lprops->lnum;
  702. if (cnt >= c->lsave_cnt)
  703. return;
  704. }
  705. list_for_each_entry(lprops, &c->frdi_idx_list, list) {
  706. c->lsave[cnt++] = lprops->lnum;
  707. if (cnt >= c->lsave_cnt)
  708. return;
  709. }
  710. heap = &c->lpt_heap[LPROPS_DIRTY_IDX - 1];
  711. for (i = 0; i < heap->cnt; i++) {
  712. c->lsave[cnt++] = heap->arr[i]->lnum;
  713. if (cnt >= c->lsave_cnt)
  714. return;
  715. }
  716. heap = &c->lpt_heap[LPROPS_DIRTY - 1];
  717. for (i = 0; i < heap->cnt; i++) {
  718. c->lsave[cnt++] = heap->arr[i]->lnum;
  719. if (cnt >= c->lsave_cnt)
  720. return;
  721. }
  722. heap = &c->lpt_heap[LPROPS_FREE - 1];
  723. for (i = 0; i < heap->cnt; i++) {
  724. c->lsave[cnt++] = heap->arr[i]->lnum;
  725. if (cnt >= c->lsave_cnt)
  726. return;
  727. }
  728. /* Fill it up completely */
  729. while (cnt < c->lsave_cnt)
  730. c->lsave[cnt++] = c->main_first;
  731. }
  732. /**
  733. * nnode_lookup - lookup a nnode in the LPT.
  734. * @c: UBIFS file-system description object
  735. * @i: nnode number
  736. *
  737. * This function returns a pointer to the nnode on success or a negative
  738. * error code on failure.
  739. */
  740. static struct ubifs_nnode *nnode_lookup(struct ubifs_info *c, int i)
  741. {
  742. int err, iip;
  743. struct ubifs_nnode *nnode;
  744. if (!c->nroot) {
  745. err = ubifs_read_nnode(c, NULL, 0);
  746. if (err)
  747. return ERR_PTR(err);
  748. }
  749. nnode = c->nroot;
  750. while (1) {
  751. iip = i & (UBIFS_LPT_FANOUT - 1);
  752. i >>= UBIFS_LPT_FANOUT_SHIFT;
  753. if (!i)
  754. break;
  755. nnode = ubifs_get_nnode(c, nnode, iip);
  756. if (IS_ERR(nnode))
  757. return nnode;
  758. }
  759. return nnode;
  760. }
  761. /**
  762. * make_nnode_dirty - find a nnode and, if found, make it dirty.
  763. * @c: UBIFS file-system description object
  764. * @node_num: nnode number of nnode to make dirty
  765. * @lnum: LEB number where nnode was written
  766. * @offs: offset where nnode was written
  767. *
  768. * This function is used by LPT garbage collection. LPT garbage collection is
  769. * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
  770. * simply involves marking all the nodes in the LEB being garbage-collected as
  771. * dirty. The dirty nodes are written next commit, after which the LEB is free
  772. * to be reused.
  773. *
  774. * This function returns %0 on success and a negative error code on failure.
  775. */
  776. static int make_nnode_dirty(struct ubifs_info *c, int node_num, int lnum,
  777. int offs)
  778. {
  779. struct ubifs_nnode *nnode;
  780. nnode = nnode_lookup(c, node_num);
  781. if (IS_ERR(nnode))
  782. return PTR_ERR(nnode);
  783. if (nnode->parent) {
  784. struct ubifs_nbranch *branch;
  785. branch = &nnode->parent->nbranch[nnode->iip];
  786. if (branch->lnum != lnum || branch->offs != offs)
  787. return 0; /* nnode is obsolete */
  788. } else if (c->lpt_lnum != lnum || c->lpt_offs != offs)
  789. return 0; /* nnode is obsolete */
  790. /* Assumes cnext list is empty i.e. not called during commit */
  791. if (!test_and_set_bit(DIRTY_CNODE, &nnode->flags)) {
  792. c->dirty_nn_cnt += 1;
  793. ubifs_add_nnode_dirt(c, nnode);
  794. /* Mark parent and ancestors dirty too */
  795. nnode = nnode->parent;
  796. while (nnode) {
  797. if (!test_and_set_bit(DIRTY_CNODE, &nnode->flags)) {
  798. c->dirty_nn_cnt += 1;
  799. ubifs_add_nnode_dirt(c, nnode);
  800. nnode = nnode->parent;
  801. } else
  802. break;
  803. }
  804. }
  805. return 0;
  806. }
  807. /**
  808. * make_pnode_dirty - find a pnode and, if found, make it dirty.
  809. * @c: UBIFS file-system description object
  810. * @node_num: pnode number of pnode to make dirty
  811. * @lnum: LEB number where pnode was written
  812. * @offs: offset where pnode was written
  813. *
  814. * This function is used by LPT garbage collection. LPT garbage collection is
  815. * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
  816. * simply involves marking all the nodes in the LEB being garbage-collected as
  817. * dirty. The dirty nodes are written next commit, after which the LEB is free
  818. * to be reused.
  819. *
  820. * This function returns %0 on success and a negative error code on failure.
  821. */
  822. static int make_pnode_dirty(struct ubifs_info *c, int node_num, int lnum,
  823. int offs)
  824. {
  825. struct ubifs_pnode *pnode;
  826. struct ubifs_nbranch *branch;
  827. pnode = pnode_lookup(c, node_num);
  828. if (IS_ERR(pnode))
  829. return PTR_ERR(pnode);
  830. branch = &pnode->parent->nbranch[pnode->iip];
  831. if (branch->lnum != lnum || branch->offs != offs)
  832. return 0;
  833. do_make_pnode_dirty(c, pnode);
  834. return 0;
  835. }
  836. /**
  837. * make_ltab_dirty - make ltab node dirty.
  838. * @c: UBIFS file-system description object
  839. * @lnum: LEB number where ltab was written
  840. * @offs: offset where ltab was written
  841. *
  842. * This function is used by LPT garbage collection. LPT garbage collection is
  843. * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
  844. * simply involves marking all the nodes in the LEB being garbage-collected as
  845. * dirty. The dirty nodes are written next commit, after which the LEB is free
  846. * to be reused.
  847. *
  848. * This function returns %0 on success and a negative error code on failure.
  849. */
  850. static int make_ltab_dirty(struct ubifs_info *c, int lnum, int offs)
  851. {
  852. if (lnum != c->ltab_lnum || offs != c->ltab_offs)
  853. return 0; /* This ltab node is obsolete */
  854. if (!(c->lpt_drty_flgs & LTAB_DIRTY)) {
  855. c->lpt_drty_flgs |= LTAB_DIRTY;
  856. ubifs_add_lpt_dirt(c, c->ltab_lnum, c->ltab_sz);
  857. }
  858. return 0;
  859. }
  860. /**
  861. * make_lsave_dirty - make lsave node dirty.
  862. * @c: UBIFS file-system description object
  863. * @lnum: LEB number where lsave was written
  864. * @offs: offset where lsave was written
  865. *
  866. * This function is used by LPT garbage collection. LPT garbage collection is
  867. * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
  868. * simply involves marking all the nodes in the LEB being garbage-collected as
  869. * dirty. The dirty nodes are written next commit, after which the LEB is free
  870. * to be reused.
  871. *
  872. * This function returns %0 on success and a negative error code on failure.
  873. */
  874. static int make_lsave_dirty(struct ubifs_info *c, int lnum, int offs)
  875. {
  876. if (lnum != c->lsave_lnum || offs != c->lsave_offs)
  877. return 0; /* This lsave node is obsolete */
  878. if (!(c->lpt_drty_flgs & LSAVE_DIRTY)) {
  879. c->lpt_drty_flgs |= LSAVE_DIRTY;
  880. ubifs_add_lpt_dirt(c, c->lsave_lnum, c->lsave_sz);
  881. }
  882. return 0;
  883. }
  884. /**
  885. * make_node_dirty - make node dirty.
  886. * @c: UBIFS file-system description object
  887. * @node_type: LPT node type
  888. * @node_num: node number
  889. * @lnum: LEB number where node was written
  890. * @offs: offset where node was written
  891. *
  892. * This function is used by LPT garbage collection. LPT garbage collection is
  893. * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
  894. * simply involves marking all the nodes in the LEB being garbage-collected as
  895. * dirty. The dirty nodes are written next commit, after which the LEB is free
  896. * to be reused.
  897. *
  898. * This function returns %0 on success and a negative error code on failure.
  899. */
  900. static int make_node_dirty(struct ubifs_info *c, int node_type, int node_num,
  901. int lnum, int offs)
  902. {
  903. switch (node_type) {
  904. case UBIFS_LPT_NNODE:
  905. return make_nnode_dirty(c, node_num, lnum, offs);
  906. case UBIFS_LPT_PNODE:
  907. return make_pnode_dirty(c, node_num, lnum, offs);
  908. case UBIFS_LPT_LTAB:
  909. return make_ltab_dirty(c, lnum, offs);
  910. case UBIFS_LPT_LSAVE:
  911. return make_lsave_dirty(c, lnum, offs);
  912. }
  913. return -EINVAL;
  914. }
  915. /**
  916. * get_lpt_node_len - return the length of a node based on its type.
  917. * @c: UBIFS file-system description object
  918. * @node_type: LPT node type
  919. */
  920. static int get_lpt_node_len(struct ubifs_info *c, int node_type)
  921. {
  922. switch (node_type) {
  923. case UBIFS_LPT_NNODE:
  924. return c->nnode_sz;
  925. case UBIFS_LPT_PNODE:
  926. return c->pnode_sz;
  927. case UBIFS_LPT_LTAB:
  928. return c->ltab_sz;
  929. case UBIFS_LPT_LSAVE:
  930. return c->lsave_sz;
  931. }
  932. return 0;
  933. }
  934. /**
  935. * get_pad_len - return the length of padding in a buffer.
  936. * @c: UBIFS file-system description object
  937. * @buf: buffer
  938. * @len: length of buffer
  939. */
  940. static int get_pad_len(struct ubifs_info *c, uint8_t *buf, int len)
  941. {
  942. int offs, pad_len;
  943. if (c->min_io_size == 1)
  944. return 0;
  945. offs = c->leb_size - len;
  946. pad_len = ALIGN(offs, c->min_io_size) - offs;
  947. return pad_len;
  948. }
  949. /**
  950. * get_lpt_node_type - return type (and node number) of a node in a buffer.
  951. * @c: UBIFS file-system description object
  952. * @buf: buffer
  953. * @node_num: node number is returned here
  954. */
  955. static int get_lpt_node_type(struct ubifs_info *c, uint8_t *buf, int *node_num)
  956. {
  957. uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
  958. int pos = 0, node_type;
  959. node_type = ubifs_unpack_bits(&addr, &pos, UBIFS_LPT_TYPE_BITS);
  960. *node_num = ubifs_unpack_bits(&addr, &pos, c->pcnt_bits);
  961. return node_type;
  962. }
  963. /**
  964. * is_a_node - determine if a buffer contains a node.
  965. * @c: UBIFS file-system description object
  966. * @buf: buffer
  967. * @len: length of buffer
  968. *
  969. * This function returns %1 if the buffer contains a node or %0 if it does not.
  970. */
  971. static int is_a_node(struct ubifs_info *c, uint8_t *buf, int len)
  972. {
  973. uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
  974. int pos = 0, node_type, node_len;
  975. uint16_t crc, calc_crc;
  976. node_type = ubifs_unpack_bits(&addr, &pos, UBIFS_LPT_TYPE_BITS);
  977. if (node_type == UBIFS_LPT_NOT_A_NODE)
  978. return 0;
  979. node_len = get_lpt_node_len(c, node_type);
  980. if (!node_len || node_len > len)
  981. return 0;
  982. pos = 0;
  983. addr = buf;
  984. crc = ubifs_unpack_bits(&addr, &pos, UBIFS_LPT_CRC_BITS);
  985. calc_crc = crc16(-1, buf + UBIFS_LPT_CRC_BYTES,
  986. node_len - UBIFS_LPT_CRC_BYTES);
  987. if (crc != calc_crc)
  988. return 0;
  989. return 1;
  990. }
  991. /**
  992. * lpt_gc_lnum - garbage collect a LPT LEB.
  993. * @c: UBIFS file-system description object
  994. * @lnum: LEB number to garbage collect
  995. *
  996. * LPT garbage collection is used only for the "big" LPT model
  997. * (c->big_lpt == 1). Garbage collection simply involves marking all the nodes
  998. * in the LEB being garbage-collected as dirty. The dirty nodes are written
  999. * next commit, after which the LEB is free to be reused.
  1000. *
  1001. * This function returns %0 on success and a negative error code on failure.
  1002. */
  1003. static int lpt_gc_lnum(struct ubifs_info *c, int lnum)
  1004. {
  1005. int err, len = c->leb_size, node_type, node_num, node_len, offs;
  1006. void *buf = c->lpt_buf;
  1007. dbg_lp("LEB %d", lnum);
  1008. err = ubi_read(c->ubi, lnum, buf, 0, c->leb_size);
  1009. if (err) {
  1010. ubifs_err("cannot read LEB %d, error %d", lnum, err);
  1011. return err;
  1012. }
  1013. while (1) {
  1014. if (!is_a_node(c, buf, len)) {
  1015. int pad_len;
  1016. pad_len = get_pad_len(c, buf, len);
  1017. if (pad_len) {
  1018. buf += pad_len;
  1019. len -= pad_len;
  1020. continue;
  1021. }
  1022. return 0;
  1023. }
  1024. node_type = get_lpt_node_type(c, buf, &node_num);
  1025. node_len = get_lpt_node_len(c, node_type);
  1026. offs = c->leb_size - len;
  1027. ubifs_assert(node_len != 0);
  1028. mutex_lock(&c->lp_mutex);
  1029. err = make_node_dirty(c, node_type, node_num, lnum, offs);
  1030. mutex_unlock(&c->lp_mutex);
  1031. if (err)
  1032. return err;
  1033. buf += node_len;
  1034. len -= node_len;
  1035. }
  1036. return 0;
  1037. }
  1038. /**
  1039. * lpt_gc - LPT garbage collection.
  1040. * @c: UBIFS file-system description object
  1041. *
  1042. * Select a LPT LEB for LPT garbage collection and call 'lpt_gc_lnum()'.
  1043. * Returns %0 on success and a negative error code on failure.
  1044. */
  1045. static int lpt_gc(struct ubifs_info *c)
  1046. {
  1047. int i, lnum = -1, dirty = 0;
  1048. mutex_lock(&c->lp_mutex);
  1049. for (i = 0; i < c->lpt_lebs; i++) {
  1050. ubifs_assert(!c->ltab[i].tgc);
  1051. if (i + c->lpt_first == c->nhead_lnum ||
  1052. c->ltab[i].free + c->ltab[i].dirty == c->leb_size)
  1053. continue;
  1054. if (c->ltab[i].dirty > dirty) {
  1055. dirty = c->ltab[i].dirty;
  1056. lnum = i + c->lpt_first;
  1057. }
  1058. }
  1059. mutex_unlock(&c->lp_mutex);
  1060. if (lnum == -1)
  1061. return -ENOSPC;
  1062. return lpt_gc_lnum(c, lnum);
  1063. }
  1064. /**
  1065. * ubifs_lpt_start_commit - UBIFS commit starts.
  1066. * @c: the UBIFS file-system description object
  1067. *
  1068. * This function has to be called when UBIFS starts the commit operation.
  1069. * This function "freezes" all currently dirty LEB properties and does not
  1070. * change them anymore. Further changes are saved and tracked separately
  1071. * because they are not part of this commit. This function returns zero in case
  1072. * of success and a negative error code in case of failure.
  1073. */
  1074. int ubifs_lpt_start_commit(struct ubifs_info *c)
  1075. {
  1076. int err, cnt;
  1077. dbg_lp("");
  1078. mutex_lock(&c->lp_mutex);
  1079. err = dbg_check_ltab(c);
  1080. if (err)
  1081. goto out;
  1082. if (c->check_lpt_free) {
  1083. /*
  1084. * We ensure there is enough free space in
  1085. * ubifs_lpt_post_commit() by marking nodes dirty. That
  1086. * information is lost when we unmount, so we also need
  1087. * to check free space once after mounting also.
  1088. */
  1089. c->check_lpt_free = 0;
  1090. while (need_write_all(c)) {
  1091. mutex_unlock(&c->lp_mutex);
  1092. err = lpt_gc(c);
  1093. if (err)
  1094. return err;
  1095. mutex_lock(&c->lp_mutex);
  1096. }
  1097. }
  1098. lpt_tgc_start(c);
  1099. if (!c->dirty_pn_cnt) {
  1100. dbg_cmt("no cnodes to commit");
  1101. err = 0;
  1102. goto out;
  1103. }
  1104. if (!c->big_lpt && need_write_all(c)) {
  1105. /* If needed, write everything */
  1106. err = make_tree_dirty(c);
  1107. if (err)
  1108. goto out;
  1109. lpt_tgc_start(c);
  1110. }
  1111. if (c->big_lpt)
  1112. populate_lsave(c);
  1113. cnt = get_cnodes_to_commit(c);
  1114. ubifs_assert(cnt != 0);
  1115. err = layout_cnodes(c);
  1116. if (err)
  1117. goto out;
  1118. /* Copy the LPT's own lprops for end commit to write */
  1119. memcpy(c->ltab_cmt, c->ltab,
  1120. sizeof(struct ubifs_lpt_lprops) * c->lpt_lebs);
  1121. c->lpt_drty_flgs &= ~(LTAB_DIRTY | LSAVE_DIRTY);
  1122. out:
  1123. mutex_unlock(&c->lp_mutex);
  1124. return err;
  1125. }
  1126. /**
  1127. * free_obsolete_cnodes - free obsolete cnodes for commit end.
  1128. * @c: UBIFS file-system description object
  1129. */
  1130. static void free_obsolete_cnodes(struct ubifs_info *c)
  1131. {
  1132. struct ubifs_cnode *cnode, *cnext;
  1133. cnext = c->lpt_cnext;
  1134. if (!cnext)
  1135. return;
  1136. do {
  1137. cnode = cnext;
  1138. cnext = cnode->cnext;
  1139. if (test_bit(OBSOLETE_CNODE, &cnode->flags))
  1140. kfree(cnode);
  1141. else
  1142. cnode->cnext = NULL;
  1143. } while (cnext != c->lpt_cnext);
  1144. c->lpt_cnext = NULL;
  1145. }
  1146. /**
  1147. * ubifs_lpt_end_commit - finish the commit operation.
  1148. * @c: the UBIFS file-system description object
  1149. *
  1150. * This function has to be called when the commit operation finishes. It
  1151. * flushes the changes which were "frozen" by 'ubifs_lprops_start_commit()' to
  1152. * the media. Returns zero in case of success and a negative error code in case
  1153. * of failure.
  1154. */
  1155. int ubifs_lpt_end_commit(struct ubifs_info *c)
  1156. {
  1157. int err;
  1158. dbg_lp("");
  1159. if (!c->lpt_cnext)
  1160. return 0;
  1161. err = write_cnodes(c);
  1162. if (err)
  1163. return err;
  1164. mutex_lock(&c->lp_mutex);
  1165. free_obsolete_cnodes(c);
  1166. mutex_unlock(&c->lp_mutex);
  1167. return 0;
  1168. }
  1169. /**
  1170. * ubifs_lpt_post_commit - post commit LPT trivial GC and LPT GC.
  1171. * @c: UBIFS file-system description object
  1172. *
  1173. * LPT trivial GC is completed after a commit. Also LPT GC is done after a
  1174. * commit for the "big" LPT model.
  1175. */
  1176. int ubifs_lpt_post_commit(struct ubifs_info *c)
  1177. {
  1178. int err;
  1179. mutex_lock(&c->lp_mutex);
  1180. err = lpt_tgc_end(c);
  1181. if (err)
  1182. goto out;
  1183. if (c->big_lpt)
  1184. while (need_write_all(c)) {
  1185. mutex_unlock(&c->lp_mutex);
  1186. err = lpt_gc(c);
  1187. if (err)
  1188. return err;
  1189. mutex_lock(&c->lp_mutex);
  1190. }
  1191. out:
  1192. mutex_unlock(&c->lp_mutex);
  1193. return err;
  1194. }
  1195. /**
  1196. * first_nnode - find the first nnode in memory.
  1197. * @c: UBIFS file-system description object
  1198. * @hght: height of tree where nnode found is returned here
  1199. *
  1200. * This function returns a pointer to the nnode found or %NULL if no nnode is
  1201. * found. This function is a helper to 'ubifs_lpt_free()'.
  1202. */
  1203. static struct ubifs_nnode *first_nnode(struct ubifs_info *c, int *hght)
  1204. {
  1205. struct ubifs_nnode *nnode;
  1206. int h, i, found;
  1207. nnode = c->nroot;
  1208. *hght = 0;
  1209. if (!nnode)
  1210. return NULL;
  1211. for (h = 1; h < c->lpt_hght; h++) {
  1212. found = 0;
  1213. for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
  1214. if (nnode->nbranch[i].nnode) {
  1215. found = 1;
  1216. nnode = nnode->nbranch[i].nnode;
  1217. *hght = h;
  1218. break;
  1219. }
  1220. }
  1221. if (!found)
  1222. break;
  1223. }
  1224. return nnode;
  1225. }
  1226. /**
  1227. * next_nnode - find the next nnode in memory.
  1228. * @c: UBIFS file-system description object
  1229. * @nnode: nnode from which to start.
  1230. * @hght: height of tree where nnode is, is passed and returned here
  1231. *
  1232. * This function returns a pointer to the nnode found or %NULL if no nnode is
  1233. * found. This function is a helper to 'ubifs_lpt_free()'.
  1234. */
  1235. static struct ubifs_nnode *next_nnode(struct ubifs_info *c,
  1236. struct ubifs_nnode *nnode, int *hght)
  1237. {
  1238. struct ubifs_nnode *parent;
  1239. int iip, h, i, found;
  1240. parent = nnode->parent;
  1241. if (!parent)
  1242. return NULL;
  1243. if (nnode->iip == UBIFS_LPT_FANOUT - 1) {
  1244. *hght -= 1;
  1245. return parent;
  1246. }
  1247. for (iip = nnode->iip + 1; iip < UBIFS_LPT_FANOUT; iip++) {
  1248. nnode = parent->nbranch[iip].nnode;
  1249. if (nnode)
  1250. break;
  1251. }
  1252. if (!nnode) {
  1253. *hght -= 1;
  1254. return parent;
  1255. }
  1256. for (h = *hght + 1; h < c->lpt_hght; h++) {
  1257. found = 0;
  1258. for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
  1259. if (nnode->nbranch[i].nnode) {
  1260. found = 1;
  1261. nnode = nnode->nbranch[i].nnode;
  1262. *hght = h;
  1263. break;
  1264. }
  1265. }
  1266. if (!found)
  1267. break;
  1268. }
  1269. return nnode;
  1270. }
  1271. /**
  1272. * ubifs_lpt_free - free resources owned by the LPT.
  1273. * @c: UBIFS file-system description object
  1274. * @wr_only: free only resources used for writing
  1275. */
  1276. void ubifs_lpt_free(struct ubifs_info *c, int wr_only)
  1277. {
  1278. struct ubifs_nnode *nnode;
  1279. int i, hght;
  1280. /* Free write-only things first */
  1281. free_obsolete_cnodes(c); /* Leftover from a failed commit */
  1282. vfree(c->ltab_cmt);
  1283. c->ltab_cmt = NULL;
  1284. vfree(c->lpt_buf);
  1285. c->lpt_buf = NULL;
  1286. kfree(c->lsave);
  1287. c->lsave = NULL;
  1288. if (wr_only)
  1289. return;
  1290. /* Now free the rest */
  1291. nnode = first_nnode(c, &hght);
  1292. while (nnode) {
  1293. for (i = 0; i < UBIFS_LPT_FANOUT; i++)
  1294. kfree(nnode->nbranch[i].nnode);
  1295. nnode = next_nnode(c, nnode, &hght);
  1296. }
  1297. for (i = 0; i < LPROPS_HEAP_CNT; i++)
  1298. kfree(c->lpt_heap[i].arr);
  1299. kfree(c->dirty_idx.arr);
  1300. kfree(c->nroot);
  1301. vfree(c->ltab);
  1302. kfree(c->lpt_nod_buf);
  1303. }
  1304. #ifdef CONFIG_UBIFS_FS_DEBUG
  1305. /**
  1306. * dbg_is_all_ff - determine if a buffer contains only 0xff bytes.
  1307. * @buf: buffer
  1308. * @len: buffer length
  1309. */
  1310. static int dbg_is_all_ff(uint8_t *buf, int len)
  1311. {
  1312. int i;
  1313. for (i = 0; i < len; i++)
  1314. if (buf[i] != 0xff)
  1315. return 0;
  1316. return 1;
  1317. }
  1318. /**
  1319. * dbg_is_nnode_dirty - determine if a nnode is dirty.
  1320. * @c: the UBIFS file-system description object
  1321. * @lnum: LEB number where nnode was written
  1322. * @offs: offset where nnode was written
  1323. */
  1324. static int dbg_is_nnode_dirty(struct ubifs_info *c, int lnum, int offs)
  1325. {
  1326. struct ubifs_nnode *nnode;
  1327. int hght;
  1328. /* Entire tree is in memory so first_nnode / next_nnode are ok */
  1329. nnode = first_nnode(c, &hght);
  1330. for (; nnode; nnode = next_nnode(c, nnode, &hght)) {
  1331. struct ubifs_nbranch *branch;
  1332. cond_resched();
  1333. if (nnode->parent) {
  1334. branch = &nnode->parent->nbranch[nnode->iip];
  1335. if (branch->lnum != lnum || branch->offs != offs)
  1336. continue;
  1337. if (test_bit(DIRTY_CNODE, &nnode->flags))
  1338. return 1;
  1339. return 0;
  1340. } else {
  1341. if (c->lpt_lnum != lnum || c->lpt_offs != offs)
  1342. continue;
  1343. if (test_bit(DIRTY_CNODE, &nnode->flags))
  1344. return 1;
  1345. return 0;
  1346. }
  1347. }
  1348. return 1;
  1349. }
  1350. /**
  1351. * dbg_is_pnode_dirty - determine if a pnode is dirty.
  1352. * @c: the UBIFS file-system description object
  1353. * @lnum: LEB number where pnode was written
  1354. * @offs: offset where pnode was written
  1355. */
  1356. static int dbg_is_pnode_dirty(struct ubifs_info *c, int lnum, int offs)
  1357. {
  1358. int i, cnt;
  1359. cnt = DIV_ROUND_UP(c->main_lebs, UBIFS_LPT_FANOUT);
  1360. for (i = 0; i < cnt; i++) {
  1361. struct ubifs_pnode *pnode;
  1362. struct ubifs_nbranch *branch;
  1363. cond_resched();
  1364. pnode = pnode_lookup(c, i);
  1365. if (IS_ERR(pnode))
  1366. return PTR_ERR(pnode);
  1367. branch = &pnode->parent->nbranch[pnode->iip];
  1368. if (branch->lnum != lnum || branch->offs != offs)
  1369. continue;
  1370. if (test_bit(DIRTY_CNODE, &pnode->flags))
  1371. return 1;
  1372. return 0;
  1373. }
  1374. return 1;
  1375. }
  1376. /**
  1377. * dbg_is_ltab_dirty - determine if a ltab node is dirty.
  1378. * @c: the UBIFS file-system description object
  1379. * @lnum: LEB number where ltab node was written
  1380. * @offs: offset where ltab node was written
  1381. */
  1382. static int dbg_is_ltab_dirty(struct ubifs_info *c, int lnum, int offs)
  1383. {
  1384. if (lnum != c->ltab_lnum || offs != c->ltab_offs)
  1385. return 1;
  1386. return (c->lpt_drty_flgs & LTAB_DIRTY) != 0;
  1387. }
  1388. /**
  1389. * dbg_is_lsave_dirty - determine if a lsave node is dirty.
  1390. * @c: the UBIFS file-system description object
  1391. * @lnum: LEB number where lsave node was written
  1392. * @offs: offset where lsave node was written
  1393. */
  1394. static int dbg_is_lsave_dirty(struct ubifs_info *c, int lnum, int offs)
  1395. {
  1396. if (lnum != c->lsave_lnum || offs != c->lsave_offs)
  1397. return 1;
  1398. return (c->lpt_drty_flgs & LSAVE_DIRTY) != 0;
  1399. }
  1400. /**
  1401. * dbg_is_node_dirty - determine if a node is dirty.
  1402. * @c: the UBIFS file-system description object
  1403. * @node_type: node type
  1404. * @lnum: LEB number where node was written
  1405. * @offs: offset where node was written
  1406. */
  1407. static int dbg_is_node_dirty(struct ubifs_info *c, int node_type, int lnum,
  1408. int offs)
  1409. {
  1410. switch (node_type) {
  1411. case UBIFS_LPT_NNODE:
  1412. return dbg_is_nnode_dirty(c, lnum, offs);
  1413. case UBIFS_LPT_PNODE:
  1414. return dbg_is_pnode_dirty(c, lnum, offs);
  1415. case UBIFS_LPT_LTAB:
  1416. return dbg_is_ltab_dirty(c, lnum, offs);
  1417. case UBIFS_LPT_LSAVE:
  1418. return dbg_is_lsave_dirty(c, lnum, offs);
  1419. }
  1420. return 1;
  1421. }
  1422. /**
  1423. * dbg_check_ltab_lnum - check the ltab for a LPT LEB number.
  1424. * @c: the UBIFS file-system description object
  1425. * @lnum: LEB number where node was written
  1426. * @offs: offset where node was written
  1427. *
  1428. * This function returns %0 on success and a negative error code on failure.
  1429. */
  1430. static int dbg_check_ltab_lnum(struct ubifs_info *c, int lnum)
  1431. {
  1432. int err, len = c->leb_size, dirty = 0, node_type, node_num, node_len;
  1433. int ret;
  1434. void *buf = c->dbg_buf;
  1435. dbg_lp("LEB %d", lnum);
  1436. err = ubi_read(c->ubi, lnum, buf, 0, c->leb_size);
  1437. if (err) {
  1438. dbg_msg("ubi_read failed, LEB %d, error %d", lnum, err);
  1439. return err;
  1440. }
  1441. while (1) {
  1442. if (!is_a_node(c, buf, len)) {
  1443. int i, pad_len;
  1444. pad_len = get_pad_len(c, buf, len);
  1445. if (pad_len) {
  1446. buf += pad_len;
  1447. len -= pad_len;
  1448. dirty += pad_len;
  1449. continue;
  1450. }
  1451. if (!dbg_is_all_ff(buf, len)) {
  1452. dbg_msg("invalid empty space in LEB %d at %d",
  1453. lnum, c->leb_size - len);
  1454. err = -EINVAL;
  1455. }
  1456. i = lnum - c->lpt_first;
  1457. if (len != c->ltab[i].free) {
  1458. dbg_msg("invalid free space in LEB %d "
  1459. "(free %d, expected %d)",
  1460. lnum, len, c->ltab[i].free);
  1461. err = -EINVAL;
  1462. }
  1463. if (dirty != c->ltab[i].dirty) {
  1464. dbg_msg("invalid dirty space in LEB %d "
  1465. "(dirty %d, expected %d)",
  1466. lnum, dirty, c->ltab[i].dirty);
  1467. err = -EINVAL;
  1468. }
  1469. return err;
  1470. }
  1471. node_type = get_lpt_node_type(c, buf, &node_num);
  1472. node_len = get_lpt_node_len(c, node_type);
  1473. ret = dbg_is_node_dirty(c, node_type, lnum, c->leb_size - len);
  1474. if (ret == 1)
  1475. dirty += node_len;
  1476. buf += node_len;
  1477. len -= node_len;
  1478. }
  1479. }
  1480. /**
  1481. * dbg_check_ltab - check the free and dirty space in the ltab.
  1482. * @c: the UBIFS file-system description object
  1483. *
  1484. * This function returns %0 on success and a negative error code on failure.
  1485. */
  1486. int dbg_check_ltab(struct ubifs_info *c)
  1487. {
  1488. int lnum, err, i, cnt;
  1489. if (!(ubifs_chk_flags & UBIFS_CHK_LPROPS))
  1490. return 0;
  1491. /* Bring the entire tree into memory */
  1492. cnt = DIV_ROUND_UP(c->main_lebs, UBIFS_LPT_FANOUT);
  1493. for (i = 0; i < cnt; i++) {
  1494. struct ubifs_pnode *pnode;
  1495. pnode = pnode_lookup(c, i);
  1496. if (IS_ERR(pnode))
  1497. return PTR_ERR(pnode);
  1498. cond_resched();
  1499. }
  1500. /* Check nodes */
  1501. err = dbg_check_lpt_nodes(c, (struct ubifs_cnode *)c->nroot, 0, 0);
  1502. if (err)
  1503. return err;
  1504. /* Check each LEB */
  1505. for (lnum = c->lpt_first; lnum <= c->lpt_last; lnum++) {
  1506. err = dbg_check_ltab_lnum(c, lnum);
  1507. if (err) {
  1508. dbg_err("failed at LEB %d", lnum);
  1509. return err;
  1510. }
  1511. }
  1512. dbg_lp("succeeded");
  1513. return 0;
  1514. }
  1515. #endif /* CONFIG_UBIFS_FS_DEBUG */