replay.c 28 KB

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