replay.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087
  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 contains journal replay code. It runs when the file-system is being
  24. * mounted and requires no locking.
  25. *
  26. * The larger is the journal, the longer it takes to scan it, so the longer it
  27. * takes to mount UBIFS. This is why the journal has limited size which may be
  28. * changed depending on the system requirements. But a larger journal gives
  29. * faster I/O speed because it writes the index less frequently. So this is a
  30. * trade-off. Also, the journal is indexed by the in-memory index (TNC), so the
  31. * larger is the journal, the more memory its index may consume.
  32. */
  33. #include "ubifs.h"
  34. /*
  35. * Replay flags.
  36. *
  37. * REPLAY_DELETION: node was deleted
  38. * REPLAY_REF: node is a reference node
  39. */
  40. enum {
  41. REPLAY_DELETION = 1,
  42. REPLAY_REF = 2,
  43. };
  44. /**
  45. * struct replay_entry - replay tree entry.
  46. * @lnum: logical eraseblock number of the node
  47. * @offs: node offset
  48. * @len: node length
  49. * @sqnum: node sequence number
  50. * @flags: replay flags
  51. * @rb: links the replay tree
  52. * @key: node key
  53. * @nm: directory entry name
  54. * @old_size: truncation old size
  55. * @new_size: truncation new size
  56. * @free: amount of free space in a bud
  57. * @dirty: amount of dirty space in a bud from padding and deletion nodes
  58. * @jhead: journal head number of the bud
  59. *
  60. * UBIFS journal replay must compare node sequence numbers, which means it must
  61. * build a tree of node information to insert into the TNC.
  62. */
  63. struct replay_entry {
  64. int lnum;
  65. int offs;
  66. int len;
  67. unsigned long long sqnum;
  68. int flags;
  69. struct rb_node rb;
  70. union ubifs_key key;
  71. union {
  72. struct qstr nm;
  73. struct {
  74. loff_t old_size;
  75. loff_t new_size;
  76. };
  77. struct {
  78. int free;
  79. int dirty;
  80. int jhead;
  81. };
  82. };
  83. };
  84. /**
  85. * struct bud_entry - entry in the list of buds to replay.
  86. * @list: next bud in the list
  87. * @bud: bud description object
  88. * @free: free bytes in the bud
  89. * @sqnum: reference node sequence number
  90. */
  91. struct bud_entry {
  92. struct list_head list;
  93. struct ubifs_bud *bud;
  94. int free;
  95. unsigned long long sqnum;
  96. };
  97. /**
  98. * set_bud_lprops - set free and dirty space used by a bud.
  99. * @c: UBIFS file-system description object
  100. * @r: replay entry of bud
  101. */
  102. static int set_bud_lprops(struct ubifs_info *c, struct replay_entry *r)
  103. {
  104. const struct ubifs_lprops *lp;
  105. int err = 0, dirty;
  106. ubifs_get_lprops(c);
  107. lp = ubifs_lpt_lookup_dirty(c, r->lnum);
  108. if (IS_ERR(lp)) {
  109. err = PTR_ERR(lp);
  110. goto out;
  111. }
  112. dirty = lp->dirty;
  113. if (r->offs == 0 && (lp->free != c->leb_size || lp->dirty != 0)) {
  114. /*
  115. * The LEB was added to the journal with a starting offset of
  116. * zero which means the LEB must have been empty. The LEB
  117. * property values should be lp->free == c->leb_size and
  118. * lp->dirty == 0, but that is not the case. The reason is that
  119. * the LEB had been garbage collected before it became the bud,
  120. * and there was not commit inbetween. The garbage collector
  121. * resets the free and dirty space without recording it
  122. * anywhere except lprops, so if there was no commit then
  123. * lprops does not have that information.
  124. *
  125. * We do not need to adjust free space because the scan has told
  126. * us the exact value which is recorded in the replay entry as
  127. * r->free.
  128. *
  129. * However we do need to subtract from the dirty space the
  130. * amount of space that the garbage collector reclaimed, which
  131. * is the whole LEB minus the amount of space that was free.
  132. */
  133. dbg_mnt("bud LEB %d was GC'd (%d free, %d dirty)", r->lnum,
  134. lp->free, lp->dirty);
  135. dbg_gc("bud LEB %d was GC'd (%d free, %d dirty)", r->lnum,
  136. lp->free, lp->dirty);
  137. dirty -= c->leb_size - lp->free;
  138. /*
  139. * If the replay order was perfect the dirty space would now be
  140. * zero. The order is not perfect because the journal heads
  141. * race with each other. This is not a problem but is does mean
  142. * that the dirty space may temporarily exceed c->leb_size
  143. * during the replay.
  144. */
  145. if (dirty != 0)
  146. dbg_msg("LEB %d lp: %d free %d dirty "
  147. "replay: %d free %d dirty", r->lnum, lp->free,
  148. lp->dirty, r->free, r->dirty);
  149. }
  150. lp = ubifs_change_lp(c, lp, r->free, dirty + r->dirty,
  151. lp->flags | LPROPS_TAKEN, 0);
  152. if (IS_ERR(lp)) {
  153. err = PTR_ERR(lp);
  154. goto out;
  155. }
  156. /* Make sure the journal head points to the latest bud */
  157. err = ubifs_wbuf_seek_nolock(&c->jheads[r->jhead].wbuf, r->lnum,
  158. c->leb_size - r->free, UBI_SHORTTERM);
  159. out:
  160. ubifs_release_lprops(c);
  161. return err;
  162. }
  163. /**
  164. * trun_remove_range - apply a replay entry for a truncation to the TNC.
  165. * @c: UBIFS file-system description object
  166. * @r: replay entry of truncation
  167. */
  168. static int trun_remove_range(struct ubifs_info *c, struct replay_entry *r)
  169. {
  170. unsigned min_blk, max_blk;
  171. union ubifs_key min_key, max_key;
  172. ino_t ino;
  173. min_blk = r->new_size / UBIFS_BLOCK_SIZE;
  174. if (r->new_size & (UBIFS_BLOCK_SIZE - 1))
  175. min_blk += 1;
  176. max_blk = r->old_size / UBIFS_BLOCK_SIZE;
  177. if ((r->old_size & (UBIFS_BLOCK_SIZE - 1)) == 0)
  178. max_blk -= 1;
  179. ino = key_inum(c, &r->key);
  180. data_key_init(c, &min_key, ino, min_blk);
  181. data_key_init(c, &max_key, ino, max_blk);
  182. return ubifs_tnc_remove_range(c, &min_key, &max_key);
  183. }
  184. /**
  185. * apply_replay_entry - apply a replay entry to the TNC.
  186. * @c: UBIFS file-system description object
  187. * @r: replay entry to apply
  188. *
  189. * Apply a replay entry to the TNC.
  190. */
  191. static int apply_replay_entry(struct ubifs_info *c, struct replay_entry *r)
  192. {
  193. int err, deletion = ((r->flags & REPLAY_DELETION) != 0);
  194. dbg_mnt("LEB %d:%d len %d flgs %d sqnum %llu %s", r->lnum,
  195. r->offs, r->len, r->flags, r->sqnum, DBGKEY(&r->key));
  196. /* Set c->replay_sqnum to help deal with dangling branches. */
  197. c->replay_sqnum = r->sqnum;
  198. if (r->flags & REPLAY_REF)
  199. err = set_bud_lprops(c, r);
  200. else if (is_hash_key(c, &r->key)) {
  201. if (deletion)
  202. err = ubifs_tnc_remove_nm(c, &r->key, &r->nm);
  203. else
  204. err = ubifs_tnc_add_nm(c, &r->key, r->lnum, r->offs,
  205. r->len, &r->nm);
  206. } else {
  207. if (deletion)
  208. switch (key_type(c, &r->key)) {
  209. case UBIFS_INO_KEY:
  210. {
  211. ino_t inum = key_inum(c, &r->key);
  212. err = ubifs_tnc_remove_ino(c, inum);
  213. break;
  214. }
  215. case UBIFS_TRUN_KEY:
  216. err = trun_remove_range(c, r);
  217. break;
  218. default:
  219. err = ubifs_tnc_remove(c, &r->key);
  220. break;
  221. }
  222. else
  223. err = ubifs_tnc_add(c, &r->key, r->lnum, r->offs,
  224. r->len);
  225. if (err)
  226. return err;
  227. if (c->need_recovery)
  228. err = ubifs_recover_size_accum(c, &r->key, deletion,
  229. r->new_size);
  230. }
  231. return err;
  232. }
  233. /**
  234. * destroy_replay_tree - destroy the replay.
  235. * @c: UBIFS file-system description object
  236. *
  237. * Destroy the replay tree.
  238. */
  239. static void destroy_replay_tree(struct ubifs_info *c)
  240. {
  241. struct rb_node *this = c->replay_tree.rb_node;
  242. struct replay_entry *r;
  243. while (this) {
  244. if (this->rb_left) {
  245. this = this->rb_left;
  246. continue;
  247. } else if (this->rb_right) {
  248. this = this->rb_right;
  249. continue;
  250. }
  251. r = rb_entry(this, struct replay_entry, rb);
  252. this = rb_parent(this);
  253. if (this) {
  254. if (this->rb_left == &r->rb)
  255. this->rb_left = NULL;
  256. else
  257. this->rb_right = NULL;
  258. }
  259. if (is_hash_key(c, &r->key))
  260. kfree(r->nm.name);
  261. kfree(r);
  262. }
  263. c->replay_tree = RB_ROOT;
  264. }
  265. /**
  266. * apply_replay_tree - apply the replay tree to the TNC.
  267. * @c: UBIFS file-system description object
  268. *
  269. * Apply the replay tree.
  270. * Returns zero in case of success and a negative error code in case of
  271. * failure.
  272. */
  273. static int apply_replay_tree(struct ubifs_info *c)
  274. {
  275. struct rb_node *this = rb_first(&c->replay_tree);
  276. while (this) {
  277. struct replay_entry *r;
  278. int err;
  279. cond_resched();
  280. r = rb_entry(this, struct replay_entry, rb);
  281. err = apply_replay_entry(c, r);
  282. if (err)
  283. return err;
  284. this = rb_next(this);
  285. }
  286. return 0;
  287. }
  288. /**
  289. * insert_node - insert a node to the replay tree.
  290. * @c: UBIFS file-system description object
  291. * @lnum: node logical eraseblock number
  292. * @offs: node offset
  293. * @len: node length
  294. * @key: node key
  295. * @sqnum: sequence number
  296. * @deletion: non-zero if this is a deletion
  297. * @used: number of bytes in use in a LEB
  298. * @old_size: truncation old size
  299. * @new_size: truncation new size
  300. *
  301. * This function inserts a scanned non-direntry node to the replay tree. The
  302. * replay tree is an RB-tree containing @struct replay_entry elements which are
  303. * indexed by the sequence number. The replay tree is applied at the very end
  304. * of the replay process. Since the tree is sorted in sequence number order,
  305. * the older modifications are applied first. This function returns zero in
  306. * case of success and a negative error code in case of failure.
  307. */
  308. static int insert_node(struct ubifs_info *c, int lnum, int offs, int len,
  309. union ubifs_key *key, unsigned long long sqnum,
  310. int deletion, int *used, loff_t old_size,
  311. loff_t new_size)
  312. {
  313. struct rb_node **p = &c->replay_tree.rb_node, *parent = NULL;
  314. struct replay_entry *r;
  315. if (key_inum(c, key) >= c->highest_inum)
  316. c->highest_inum = key_inum(c, key);
  317. dbg_mnt("add LEB %d:%d, key %s", lnum, offs, DBGKEY(key));
  318. while (*p) {
  319. parent = *p;
  320. r = rb_entry(parent, struct replay_entry, rb);
  321. if (sqnum < r->sqnum) {
  322. p = &(*p)->rb_left;
  323. continue;
  324. } else if (sqnum > r->sqnum) {
  325. p = &(*p)->rb_right;
  326. continue;
  327. }
  328. ubifs_err("duplicate sqnum in replay");
  329. return -EINVAL;
  330. }
  331. r = kzalloc(sizeof(struct replay_entry), GFP_KERNEL);
  332. if (!r)
  333. return -ENOMEM;
  334. if (!deletion)
  335. *used += ALIGN(len, 8);
  336. r->lnum = lnum;
  337. r->offs = offs;
  338. r->len = len;
  339. r->sqnum = sqnum;
  340. r->flags = (deletion ? REPLAY_DELETION : 0);
  341. r->old_size = old_size;
  342. r->new_size = new_size;
  343. key_copy(c, key, &r->key);
  344. rb_link_node(&r->rb, parent, p);
  345. rb_insert_color(&r->rb, &c->replay_tree);
  346. return 0;
  347. }
  348. /**
  349. * insert_dent - insert a directory entry node into the replay tree.
  350. * @c: UBIFS file-system description object
  351. * @lnum: node logical eraseblock number
  352. * @offs: node offset
  353. * @len: node length
  354. * @key: node key
  355. * @name: directory entry name
  356. * @nlen: directory entry name length
  357. * @sqnum: sequence number
  358. * @deletion: non-zero if this is a deletion
  359. * @used: number of bytes in use in a LEB
  360. *
  361. * This function inserts a scanned directory entry node to the replay tree.
  362. * Returns zero in case of success and a negative error code in case of
  363. * failure.
  364. *
  365. * This function is also used for extended attribute entries because they are
  366. * implemented as directory entry nodes.
  367. */
  368. static int insert_dent(struct ubifs_info *c, int lnum, int offs, int len,
  369. union ubifs_key *key, const char *name, int nlen,
  370. unsigned long long sqnum, int deletion, int *used)
  371. {
  372. struct rb_node **p = &c->replay_tree.rb_node, *parent = NULL;
  373. struct replay_entry *r;
  374. char *nbuf;
  375. if (key_inum(c, key) >= c->highest_inum)
  376. c->highest_inum = key_inum(c, key);
  377. dbg_mnt("add LEB %d:%d, key %s", lnum, offs, DBGKEY(key));
  378. while (*p) {
  379. parent = *p;
  380. r = rb_entry(parent, struct replay_entry, rb);
  381. if (sqnum < r->sqnum) {
  382. p = &(*p)->rb_left;
  383. continue;
  384. }
  385. if (sqnum > r->sqnum) {
  386. p = &(*p)->rb_right;
  387. continue;
  388. }
  389. ubifs_err("duplicate sqnum in replay");
  390. return -EINVAL;
  391. }
  392. r = kzalloc(sizeof(struct replay_entry), GFP_KERNEL);
  393. if (!r)
  394. return -ENOMEM;
  395. nbuf = kmalloc(nlen + 1, GFP_KERNEL);
  396. if (!nbuf) {
  397. kfree(r);
  398. return -ENOMEM;
  399. }
  400. if (!deletion)
  401. *used += ALIGN(len, 8);
  402. r->lnum = lnum;
  403. r->offs = offs;
  404. r->len = len;
  405. r->sqnum = sqnum;
  406. r->nm.len = nlen;
  407. memcpy(nbuf, name, nlen);
  408. nbuf[nlen] = '\0';
  409. r->nm.name = nbuf;
  410. r->flags = (deletion ? REPLAY_DELETION : 0);
  411. key_copy(c, key, &r->key);
  412. ubifs_assert(!*p);
  413. rb_link_node(&r->rb, parent, p);
  414. rb_insert_color(&r->rb, &c->replay_tree);
  415. return 0;
  416. }
  417. /**
  418. * ubifs_validate_entry - validate directory or extended attribute entry node.
  419. * @c: UBIFS file-system description object
  420. * @dent: the node to validate
  421. *
  422. * This function validates directory or extended attribute entry node @dent.
  423. * Returns zero if the node is all right and a %-EINVAL if not.
  424. */
  425. int ubifs_validate_entry(struct ubifs_info *c,
  426. const struct ubifs_dent_node *dent)
  427. {
  428. int key_type = key_type_flash(c, dent->key);
  429. int nlen = le16_to_cpu(dent->nlen);
  430. if (le32_to_cpu(dent->ch.len) != nlen + UBIFS_DENT_NODE_SZ + 1 ||
  431. dent->type >= UBIFS_ITYPES_CNT ||
  432. nlen > UBIFS_MAX_NLEN || dent->name[nlen] != 0 ||
  433. strnlen(dent->name, nlen) != nlen ||
  434. le64_to_cpu(dent->inum) > MAX_INUM) {
  435. ubifs_err("bad %s node", key_type == UBIFS_DENT_KEY ?
  436. "directory entry" : "extended attribute entry");
  437. return -EINVAL;
  438. }
  439. if (key_type != UBIFS_DENT_KEY && key_type != UBIFS_XENT_KEY) {
  440. ubifs_err("bad key type %d", key_type);
  441. return -EINVAL;
  442. }
  443. return 0;
  444. }
  445. /**
  446. * replay_bud - replay a bud logical eraseblock.
  447. * @c: UBIFS file-system description object
  448. * @lnum: bud logical eraseblock number to replay
  449. * @offs: bud start offset
  450. * @jhead: journal head to which this bud belongs
  451. * @free: amount of free space in the bud is returned here
  452. * @dirty: amount of dirty space from padding and deletion nodes is returned
  453. * here
  454. *
  455. * This function returns zero in case of success and a negative error code in
  456. * case of failure.
  457. */
  458. static int replay_bud(struct ubifs_info *c, int lnum, int offs, int jhead,
  459. int *free, int *dirty)
  460. {
  461. int err = 0, used = 0;
  462. struct ubifs_scan_leb *sleb;
  463. struct ubifs_scan_node *snod;
  464. struct ubifs_bud *bud;
  465. dbg_mnt("replay bud LEB %d, head %d, offs %d", lnum, jhead, offs);
  466. if (c->need_recovery)
  467. sleb = ubifs_recover_leb(c, lnum, offs, c->sbuf, jhead != GCHD);
  468. else
  469. sleb = ubifs_scan(c, lnum, offs, c->sbuf, 0);
  470. if (IS_ERR(sleb))
  471. return PTR_ERR(sleb);
  472. /*
  473. * The bud does not have to start from offset zero - the beginning of
  474. * the 'lnum' LEB may contain previously committed data. One of the
  475. * things we have to do in replay is to correctly update lprops with
  476. * newer information about this LEB.
  477. *
  478. * At this point lprops thinks that this LEB has 'c->leb_size - offs'
  479. * bytes of free space because it only contain information about
  480. * committed data.
  481. *
  482. * But we know that real amount of free space is 'c->leb_size -
  483. * sleb->endpt', and the space in the 'lnum' LEB between 'offs' and
  484. * 'sleb->endpt' is used by bud data. We have to correctly calculate
  485. * how much of these data are dirty and update lprops with this
  486. * information.
  487. *
  488. * The dirt in that LEB region is comprised of padding nodes, deletion
  489. * nodes, truncation nodes and nodes which are obsoleted by subsequent
  490. * nodes in this LEB. So instead of calculating clean space, we
  491. * calculate used space ('used' variable).
  492. */
  493. list_for_each_entry(snod, &sleb->nodes, list) {
  494. int deletion = 0;
  495. cond_resched();
  496. if (snod->sqnum >= SQNUM_WATERMARK) {
  497. ubifs_err("file system's life ended");
  498. goto out_dump;
  499. }
  500. if (snod->sqnum > c->max_sqnum)
  501. c->max_sqnum = snod->sqnum;
  502. switch (snod->type) {
  503. case UBIFS_INO_NODE:
  504. {
  505. struct ubifs_ino_node *ino = snod->node;
  506. loff_t new_size = le64_to_cpu(ino->size);
  507. if (le32_to_cpu(ino->nlink) == 0)
  508. deletion = 1;
  509. err = insert_node(c, lnum, snod->offs, snod->len,
  510. &snod->key, snod->sqnum, deletion,
  511. &used, 0, new_size);
  512. break;
  513. }
  514. case UBIFS_DATA_NODE:
  515. {
  516. struct ubifs_data_node *dn = snod->node;
  517. loff_t new_size = le32_to_cpu(dn->size) +
  518. key_block(c, &snod->key) *
  519. UBIFS_BLOCK_SIZE;
  520. err = insert_node(c, lnum, snod->offs, snod->len,
  521. &snod->key, snod->sqnum, deletion,
  522. &used, 0, new_size);
  523. break;
  524. }
  525. case UBIFS_DENT_NODE:
  526. case UBIFS_XENT_NODE:
  527. {
  528. struct ubifs_dent_node *dent = snod->node;
  529. err = ubifs_validate_entry(c, dent);
  530. if (err)
  531. goto out_dump;
  532. err = insert_dent(c, lnum, snod->offs, snod->len,
  533. &snod->key, dent->name,
  534. le16_to_cpu(dent->nlen), snod->sqnum,
  535. !le64_to_cpu(dent->inum), &used);
  536. break;
  537. }
  538. case UBIFS_TRUN_NODE:
  539. {
  540. struct ubifs_trun_node *trun = snod->node;
  541. loff_t old_size = le64_to_cpu(trun->old_size);
  542. loff_t new_size = le64_to_cpu(trun->new_size);
  543. union ubifs_key key;
  544. /* Validate truncation node */
  545. if (old_size < 0 || old_size > c->max_inode_sz ||
  546. new_size < 0 || new_size > c->max_inode_sz ||
  547. old_size <= new_size) {
  548. ubifs_err("bad truncation node");
  549. goto out_dump;
  550. }
  551. /*
  552. * Create a fake truncation key just to use the same
  553. * functions which expect nodes to have keys.
  554. */
  555. trun_key_init(c, &key, le32_to_cpu(trun->inum));
  556. err = insert_node(c, lnum, snod->offs, snod->len,
  557. &key, snod->sqnum, 1, &used,
  558. old_size, new_size);
  559. break;
  560. }
  561. default:
  562. ubifs_err("unexpected node type %d in bud LEB %d:%d",
  563. snod->type, lnum, snod->offs);
  564. err = -EINVAL;
  565. goto out_dump;
  566. }
  567. if (err)
  568. goto out;
  569. }
  570. bud = ubifs_search_bud(c, lnum);
  571. if (!bud)
  572. BUG();
  573. ubifs_assert(sleb->endpt - offs >= used);
  574. ubifs_assert(sleb->endpt % c->min_io_size == 0);
  575. *dirty = sleb->endpt - offs - used;
  576. *free = c->leb_size - sleb->endpt;
  577. dbg_mnt("bud LEB %d replied: dirty %d, free %d", lnum, *dirty, *free);
  578. out:
  579. ubifs_scan_destroy(sleb);
  580. return err;
  581. out_dump:
  582. ubifs_err("bad node is at LEB %d:%d", lnum, snod->offs);
  583. dbg_dump_node(c, snod->node);
  584. ubifs_scan_destroy(sleb);
  585. return -EINVAL;
  586. }
  587. /**
  588. * insert_ref_node - insert a reference node to the replay tree.
  589. * @c: UBIFS file-system description object
  590. * @lnum: node logical eraseblock number
  591. * @offs: node offset
  592. * @sqnum: sequence number
  593. * @free: amount of free space in bud
  594. * @dirty: amount of dirty space from padding and deletion nodes
  595. * @jhead: journal head number for the bud
  596. *
  597. * This function inserts a reference node to the replay tree and returns zero
  598. * in case of success or a negative error code in case of failure.
  599. */
  600. static int insert_ref_node(struct ubifs_info *c, int lnum, int offs,
  601. unsigned long long sqnum, int free, int dirty,
  602. int jhead)
  603. {
  604. struct rb_node **p = &c->replay_tree.rb_node, *parent = NULL;
  605. struct replay_entry *r;
  606. dbg_mnt("add ref LEB %d:%d", lnum, offs);
  607. while (*p) {
  608. parent = *p;
  609. r = rb_entry(parent, struct replay_entry, rb);
  610. if (sqnum < r->sqnum) {
  611. p = &(*p)->rb_left;
  612. continue;
  613. } else if (sqnum > r->sqnum) {
  614. p = &(*p)->rb_right;
  615. continue;
  616. }
  617. ubifs_err("duplicate sqnum in replay tree");
  618. return -EINVAL;
  619. }
  620. r = kzalloc(sizeof(struct replay_entry), GFP_KERNEL);
  621. if (!r)
  622. return -ENOMEM;
  623. r->lnum = lnum;
  624. r->offs = offs;
  625. r->sqnum = sqnum;
  626. r->flags = REPLAY_REF;
  627. r->free = free;
  628. r->dirty = dirty;
  629. r->jhead = jhead;
  630. rb_link_node(&r->rb, parent, p);
  631. rb_insert_color(&r->rb, &c->replay_tree);
  632. return 0;
  633. }
  634. /**
  635. * replay_buds - replay all buds.
  636. * @c: UBIFS file-system description object
  637. *
  638. * This function returns zero in case of success and a negative error code in
  639. * case of failure.
  640. */
  641. static int replay_buds(struct ubifs_info *c)
  642. {
  643. struct bud_entry *b;
  644. int err, uninitialized_var(free), uninitialized_var(dirty);
  645. list_for_each_entry(b, &c->replay_buds, list) {
  646. err = replay_bud(c, b->bud->lnum, b->bud->start, b->bud->jhead,
  647. &free, &dirty);
  648. if (err)
  649. return err;
  650. err = insert_ref_node(c, b->bud->lnum, b->bud->start, b->sqnum,
  651. free, dirty, b->bud->jhead);
  652. if (err)
  653. return err;
  654. }
  655. return 0;
  656. }
  657. /**
  658. * destroy_bud_list - destroy the list of buds to replay.
  659. * @c: UBIFS file-system description object
  660. */
  661. static void destroy_bud_list(struct ubifs_info *c)
  662. {
  663. struct bud_entry *b;
  664. while (!list_empty(&c->replay_buds)) {
  665. b = list_entry(c->replay_buds.next, struct bud_entry, list);
  666. list_del(&b->list);
  667. kfree(b);
  668. }
  669. }
  670. /**
  671. * add_replay_bud - add a bud to the list of buds to replay.
  672. * @c: UBIFS file-system description object
  673. * @lnum: bud logical eraseblock number to replay
  674. * @offs: bud start offset
  675. * @jhead: journal head to which this bud belongs
  676. * @sqnum: reference node sequence number
  677. *
  678. * This function returns zero in case of success and a negative error code in
  679. * case of failure.
  680. */
  681. static int add_replay_bud(struct ubifs_info *c, int lnum, int offs, int jhead,
  682. unsigned long long sqnum)
  683. {
  684. struct ubifs_bud *bud;
  685. struct bud_entry *b;
  686. dbg_mnt("add replay bud LEB %d:%d, head %d", lnum, offs, jhead);
  687. bud = kmalloc(sizeof(struct ubifs_bud), GFP_KERNEL);
  688. if (!bud)
  689. return -ENOMEM;
  690. b = kmalloc(sizeof(struct bud_entry), GFP_KERNEL);
  691. if (!b) {
  692. kfree(bud);
  693. return -ENOMEM;
  694. }
  695. bud->lnum = lnum;
  696. bud->start = offs;
  697. bud->jhead = jhead;
  698. ubifs_add_bud(c, bud);
  699. b->bud = bud;
  700. b->sqnum = sqnum;
  701. list_add_tail(&b->list, &c->replay_buds);
  702. return 0;
  703. }
  704. /**
  705. * validate_ref - validate a reference node.
  706. * @c: UBIFS file-system description object
  707. * @ref: the reference node to validate
  708. * @ref_lnum: LEB number of the reference node
  709. * @ref_offs: reference node offset
  710. *
  711. * This function returns %1 if a bud reference already exists for the LEB. %0 is
  712. * returned if the reference node is new, otherwise %-EINVAL is returned if
  713. * validation failed.
  714. */
  715. static int validate_ref(struct ubifs_info *c, const struct ubifs_ref_node *ref)
  716. {
  717. struct ubifs_bud *bud;
  718. int lnum = le32_to_cpu(ref->lnum);
  719. unsigned int offs = le32_to_cpu(ref->offs);
  720. unsigned int jhead = le32_to_cpu(ref->jhead);
  721. /*
  722. * ref->offs may point to the end of LEB when the journal head points
  723. * to the end of LEB and we write reference node for it during commit.
  724. * So this is why we require 'offs > c->leb_size'.
  725. */
  726. if (jhead >= c->jhead_cnt || lnum >= c->leb_cnt ||
  727. lnum < c->main_first || offs > c->leb_size ||
  728. offs & (c->min_io_size - 1))
  729. return -EINVAL;
  730. /* Make sure we have not already looked at this bud */
  731. bud = ubifs_search_bud(c, lnum);
  732. if (bud) {
  733. if (bud->jhead == jhead && bud->start <= offs)
  734. return 1;
  735. ubifs_err("bud at LEB %d:%d was already referred", lnum, offs);
  736. return -EINVAL;
  737. }
  738. return 0;
  739. }
  740. /**
  741. * replay_log_leb - replay a log logical eraseblock.
  742. * @c: UBIFS file-system description object
  743. * @lnum: log logical eraseblock to replay
  744. * @offs: offset to start replaying from
  745. * @sbuf: scan buffer
  746. *
  747. * This function replays a log LEB and returns zero in case of success, %1 if
  748. * this is the last LEB in the log, and a negative error code in case of
  749. * failure.
  750. */
  751. static int replay_log_leb(struct ubifs_info *c, int lnum, int offs, void *sbuf)
  752. {
  753. int err;
  754. struct ubifs_scan_leb *sleb;
  755. struct ubifs_scan_node *snod;
  756. const struct ubifs_cs_node *node;
  757. dbg_mnt("replay log LEB %d:%d", lnum, offs);
  758. sleb = ubifs_scan(c, lnum, offs, sbuf, c->need_recovery);
  759. if (IS_ERR(sleb)) {
  760. if (PTR_ERR(sleb) != -EUCLEAN || !c->need_recovery)
  761. return PTR_ERR(sleb);
  762. /*
  763. * Note, the below function will recover this log LEB only if
  764. * it is the last, because unclean reboots can possibly corrupt
  765. * only the tail of the log.
  766. */
  767. sleb = ubifs_recover_log_leb(c, lnum, offs, sbuf);
  768. if (IS_ERR(sleb))
  769. return PTR_ERR(sleb);
  770. }
  771. if (sleb->nodes_cnt == 0) {
  772. err = 1;
  773. goto out;
  774. }
  775. node = sleb->buf;
  776. snod = list_entry(sleb->nodes.next, struct ubifs_scan_node, list);
  777. if (c->cs_sqnum == 0) {
  778. /*
  779. * This is the first log LEB we are looking at, make sure that
  780. * the first node is a commit start node. Also record its
  781. * sequence number so that UBIFS can determine where the log
  782. * ends, because all nodes which were have higher sequence
  783. * numbers.
  784. */
  785. if (snod->type != UBIFS_CS_NODE) {
  786. dbg_err("first log node at LEB %d:%d is not CS node",
  787. lnum, offs);
  788. goto out_dump;
  789. }
  790. if (le64_to_cpu(node->cmt_no) != c->cmt_no) {
  791. dbg_err("first CS node at LEB %d:%d has wrong "
  792. "commit number %llu expected %llu",
  793. lnum, offs,
  794. (unsigned long long)le64_to_cpu(node->cmt_no),
  795. c->cmt_no);
  796. goto out_dump;
  797. }
  798. c->cs_sqnum = le64_to_cpu(node->ch.sqnum);
  799. dbg_mnt("commit start sqnum %llu", c->cs_sqnum);
  800. }
  801. if (snod->sqnum < c->cs_sqnum) {
  802. /*
  803. * This means that we reached end of log and now
  804. * look to the older log data, which was already
  805. * committed but the eraseblock was not erased (UBIFS
  806. * only un-maps it). So this basically means we have to
  807. * exit with "end of log" code.
  808. */
  809. err = 1;
  810. goto out;
  811. }
  812. /* Make sure the first node sits at offset zero of the LEB */
  813. if (snod->offs != 0) {
  814. dbg_err("first node is not at zero offset");
  815. goto out_dump;
  816. }
  817. list_for_each_entry(snod, &sleb->nodes, list) {
  818. cond_resched();
  819. if (snod->sqnum >= SQNUM_WATERMARK) {
  820. ubifs_err("file system's life ended");
  821. goto out_dump;
  822. }
  823. if (snod->sqnum < c->cs_sqnum) {
  824. dbg_err("bad sqnum %llu, commit sqnum %llu",
  825. snod->sqnum, c->cs_sqnum);
  826. goto out_dump;
  827. }
  828. if (snod->sqnum > c->max_sqnum)
  829. c->max_sqnum = snod->sqnum;
  830. switch (snod->type) {
  831. case UBIFS_REF_NODE: {
  832. const struct ubifs_ref_node *ref = snod->node;
  833. err = validate_ref(c, ref);
  834. if (err == 1)
  835. break; /* Already have this bud */
  836. if (err)
  837. goto out_dump;
  838. err = add_replay_bud(c, le32_to_cpu(ref->lnum),
  839. le32_to_cpu(ref->offs),
  840. le32_to_cpu(ref->jhead),
  841. snod->sqnum);
  842. if (err)
  843. goto out;
  844. break;
  845. }
  846. case UBIFS_CS_NODE:
  847. /* Make sure it sits at the beginning of LEB */
  848. if (snod->offs != 0) {
  849. ubifs_err("unexpected node in log");
  850. goto out_dump;
  851. }
  852. break;
  853. default:
  854. ubifs_err("unexpected node in log");
  855. goto out_dump;
  856. }
  857. }
  858. if (sleb->endpt || c->lhead_offs >= c->leb_size) {
  859. c->lhead_lnum = lnum;
  860. c->lhead_offs = sleb->endpt;
  861. }
  862. err = !sleb->endpt;
  863. out:
  864. ubifs_scan_destroy(sleb);
  865. return err;
  866. out_dump:
  867. ubifs_err("log error detected while replaying the log at LEB %d:%d",
  868. lnum, offs + snod->offs);
  869. dbg_dump_node(c, snod->node);
  870. ubifs_scan_destroy(sleb);
  871. return -EINVAL;
  872. }
  873. /**
  874. * take_ihead - update the status of the index head in lprops to 'taken'.
  875. * @c: UBIFS file-system description object
  876. *
  877. * This function returns the amount of free space in the index head LEB or a
  878. * negative error code.
  879. */
  880. static int take_ihead(struct ubifs_info *c)
  881. {
  882. const struct ubifs_lprops *lp;
  883. int err, free;
  884. ubifs_get_lprops(c);
  885. lp = ubifs_lpt_lookup_dirty(c, c->ihead_lnum);
  886. if (IS_ERR(lp)) {
  887. err = PTR_ERR(lp);
  888. goto out;
  889. }
  890. free = lp->free;
  891. lp = ubifs_change_lp(c, lp, LPROPS_NC, LPROPS_NC,
  892. lp->flags | LPROPS_TAKEN, 0);
  893. if (IS_ERR(lp)) {
  894. err = PTR_ERR(lp);
  895. goto out;
  896. }
  897. err = free;
  898. out:
  899. ubifs_release_lprops(c);
  900. return err;
  901. }
  902. /**
  903. * ubifs_replay_journal - replay journal.
  904. * @c: UBIFS file-system description object
  905. *
  906. * This function scans the journal, replays and cleans it up. It makes sure all
  907. * memory data structures related to uncommitted journal are built (dirty TNC
  908. * tree, tree of buds, modified lprops, etc).
  909. */
  910. int ubifs_replay_journal(struct ubifs_info *c)
  911. {
  912. int err, i, lnum, offs, free;
  913. BUILD_BUG_ON(UBIFS_TRUN_KEY > 5);
  914. /* Update the status of the index head in lprops to 'taken' */
  915. free = take_ihead(c);
  916. if (free < 0)
  917. return free; /* Error code */
  918. if (c->ihead_offs != c->leb_size - free) {
  919. ubifs_err("bad index head LEB %d:%d", c->ihead_lnum,
  920. c->ihead_offs);
  921. return -EINVAL;
  922. }
  923. dbg_mnt("start replaying the journal");
  924. c->replaying = 1;
  925. lnum = c->ltail_lnum = c->lhead_lnum;
  926. offs = c->lhead_offs;
  927. for (i = 0; i < c->log_lebs; i++, lnum++) {
  928. if (lnum >= UBIFS_LOG_LNUM + c->log_lebs) {
  929. /*
  930. * The log is logically circular, we reached the last
  931. * LEB, switch to the first one.
  932. */
  933. lnum = UBIFS_LOG_LNUM;
  934. offs = 0;
  935. }
  936. err = replay_log_leb(c, lnum, offs, c->sbuf);
  937. if (err == 1)
  938. /* We hit the end of the log */
  939. break;
  940. if (err)
  941. goto out;
  942. offs = 0;
  943. }
  944. err = replay_buds(c);
  945. if (err)
  946. goto out;
  947. err = apply_replay_tree(c);
  948. if (err)
  949. goto out;
  950. /*
  951. * UBIFS budgeting calculations use @c->bi.uncommitted_idx variable
  952. * to roughly estimate index growth. Things like @c->bi.min_idx_lebs
  953. * depend on it. This means we have to initialize it to make sure
  954. * budgeting works properly.
  955. */
  956. c->bi.uncommitted_idx = atomic_long_read(&c->dirty_zn_cnt);
  957. c->bi.uncommitted_idx *= c->max_idx_node_sz;
  958. ubifs_assert(c->bud_bytes <= c->max_bud_bytes || c->need_recovery);
  959. dbg_mnt("finished, log head LEB %d:%d, max_sqnum %llu, "
  960. "highest_inum %lu", c->lhead_lnum, c->lhead_offs, c->max_sqnum,
  961. (unsigned long)c->highest_inum);
  962. out:
  963. destroy_replay_tree(c);
  964. destroy_bud_list(c);
  965. c->replaying = 0;
  966. return err;
  967. }