replay.c 30 KB

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