lpt_commit.c 52 KB

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