replay.c 29 KB

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