lpt_commit.c 52 KB

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