lpt_commit.c 52 KB

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