replay.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080
  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_msg("LEB %d lp: %d free %d dirty "
  137. "replay: %d free %d dirty", b->bud->lnum,
  138. lp->free, lp->dirty, b->free, 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. UBI_SHORTTERM);
  150. out:
  151. ubifs_release_lprops(c);
  152. return err;
  153. }
  154. /**
  155. * set_buds_lprops - set free and dirty space for all replayed buds.
  156. * @c: UBIFS file-system description object
  157. *
  158. * This function sets LEB properties for all replayed buds. Returns zero in
  159. * case of success and a negative error code in case of failure.
  160. */
  161. static int set_buds_lprops(struct ubifs_info *c)
  162. {
  163. struct bud_entry *b;
  164. int err;
  165. list_for_each_entry(b, &c->replay_buds, list) {
  166. err = set_bud_lprops(c, b);
  167. if (err)
  168. return err;
  169. }
  170. return 0;
  171. }
  172. /**
  173. * trun_remove_range - apply a replay entry for a truncation to the TNC.
  174. * @c: UBIFS file-system description object
  175. * @r: replay entry of truncation
  176. */
  177. static int trun_remove_range(struct ubifs_info *c, struct replay_entry *r)
  178. {
  179. unsigned min_blk, max_blk;
  180. union ubifs_key min_key, max_key;
  181. ino_t ino;
  182. min_blk = r->new_size / UBIFS_BLOCK_SIZE;
  183. if (r->new_size & (UBIFS_BLOCK_SIZE - 1))
  184. min_blk += 1;
  185. max_blk = r->old_size / UBIFS_BLOCK_SIZE;
  186. if ((r->old_size & (UBIFS_BLOCK_SIZE - 1)) == 0)
  187. max_blk -= 1;
  188. ino = key_inum(c, &r->key);
  189. data_key_init(c, &min_key, ino, min_blk);
  190. data_key_init(c, &max_key, ino, max_blk);
  191. return ubifs_tnc_remove_range(c, &min_key, &max_key);
  192. }
  193. /**
  194. * apply_replay_entry - apply a replay entry to the TNC.
  195. * @c: UBIFS file-system description object
  196. * @r: replay entry to apply
  197. *
  198. * Apply a replay entry to the TNC.
  199. */
  200. static int apply_replay_entry(struct ubifs_info *c, struct replay_entry *r)
  201. {
  202. int err;
  203. dbg_mntk(&r->key, "LEB %d:%d len %d deletion %d sqnum %llu key ",
  204. r->lnum, r->offs, r->len, r->deletion, r->sqnum);
  205. /* Set c->replay_sqnum to help deal with dangling branches. */
  206. c->replay_sqnum = r->sqnum;
  207. if (is_hash_key(c, &r->key)) {
  208. if (r->deletion)
  209. err = ubifs_tnc_remove_nm(c, &r->key, &r->nm);
  210. else
  211. err = ubifs_tnc_add_nm(c, &r->key, r->lnum, r->offs,
  212. r->len, &r->nm);
  213. } else {
  214. if (r->deletion)
  215. switch (key_type(c, &r->key)) {
  216. case UBIFS_INO_KEY:
  217. {
  218. ino_t inum = key_inum(c, &r->key);
  219. err = ubifs_tnc_remove_ino(c, inum);
  220. break;
  221. }
  222. case UBIFS_TRUN_KEY:
  223. err = trun_remove_range(c, r);
  224. break;
  225. default:
  226. err = ubifs_tnc_remove(c, &r->key);
  227. break;
  228. }
  229. else
  230. err = ubifs_tnc_add(c, &r->key, r->lnum, r->offs,
  231. r->len);
  232. if (err)
  233. return err;
  234. if (c->need_recovery)
  235. err = ubifs_recover_size_accum(c, &r->key, r->deletion,
  236. r->new_size);
  237. }
  238. return err;
  239. }
  240. /**
  241. * replay_entries_cmp - compare 2 replay entries.
  242. * @priv: UBIFS file-system description object
  243. * @a: first replay entry
  244. * @a: second replay entry
  245. *
  246. * This is a comparios function for 'list_sort()' which compares 2 replay
  247. * entries @a and @b by comparing their sequence numer. Returns %1 if @a has
  248. * greater sequence number and %-1 otherwise.
  249. */
  250. static int replay_entries_cmp(void *priv, struct list_head *a,
  251. struct list_head *b)
  252. {
  253. struct replay_entry *ra, *rb;
  254. cond_resched();
  255. if (a == b)
  256. return 0;
  257. ra = list_entry(a, struct replay_entry, list);
  258. rb = list_entry(b, struct replay_entry, list);
  259. ubifs_assert(ra->sqnum != rb->sqnum);
  260. if (ra->sqnum > rb->sqnum)
  261. return 1;
  262. return -1;
  263. }
  264. /**
  265. * apply_replay_list - apply the replay list to the TNC.
  266. * @c: UBIFS file-system description object
  267. *
  268. * Apply all entries in the replay list to the TNC. Returns zero in case of
  269. * success and a negative error code in case of failure.
  270. */
  271. static int apply_replay_list(struct ubifs_info *c)
  272. {
  273. struct replay_entry *r;
  274. int err;
  275. list_sort(c, &c->replay_list, &replay_entries_cmp);
  276. list_for_each_entry(r, &c->replay_list, list) {
  277. cond_resched();
  278. err = apply_replay_entry(c, r);
  279. if (err)
  280. return err;
  281. }
  282. return 0;
  283. }
  284. /**
  285. * destroy_replay_list - destroy the replay.
  286. * @c: UBIFS file-system description object
  287. *
  288. * Destroy the replay list.
  289. */
  290. static void destroy_replay_list(struct ubifs_info *c)
  291. {
  292. struct replay_entry *r, *tmp;
  293. list_for_each_entry_safe(r, tmp, &c->replay_list, list) {
  294. if (is_hash_key(c, &r->key))
  295. kfree(r->nm.name);
  296. list_del(&r->list);
  297. kfree(r);
  298. }
  299. }
  300. /**
  301. * insert_node - insert a node to the replay list
  302. * @c: UBIFS file-system description object
  303. * @lnum: node logical eraseblock number
  304. * @offs: node offset
  305. * @len: node length
  306. * @key: node key
  307. * @sqnum: sequence number
  308. * @deletion: non-zero if this is a deletion
  309. * @used: number of bytes in use in a LEB
  310. * @old_size: truncation old size
  311. * @new_size: truncation new size
  312. *
  313. * This function inserts a scanned non-direntry node to the replay list. The
  314. * replay list contains @struct replay_entry elements, and we sort this list in
  315. * sequence number order before applying it. The replay list is applied at the
  316. * very end of the replay process. Since the list is sorted in sequence number
  317. * order, the older modifications are applied first. This function returns zero
  318. * in case of success and a negative error code in case of failure.
  319. */
  320. static int insert_node(struct ubifs_info *c, int lnum, int offs, int len,
  321. union ubifs_key *key, unsigned long long sqnum,
  322. int deletion, int *used, loff_t old_size,
  323. loff_t new_size)
  324. {
  325. struct replay_entry *r;
  326. dbg_mntk(key, "add LEB %d:%d, key ", lnum, offs);
  327. if (key_inum(c, key) >= c->highest_inum)
  328. c->highest_inum = key_inum(c, key);
  329. r = kzalloc(sizeof(struct replay_entry), GFP_KERNEL);
  330. if (!r)
  331. return -ENOMEM;
  332. if (!deletion)
  333. *used += ALIGN(len, 8);
  334. r->lnum = lnum;
  335. r->offs = offs;
  336. r->len = len;
  337. r->deletion = !!deletion;
  338. r->sqnum = sqnum;
  339. key_copy(c, key, &r->key);
  340. r->old_size = old_size;
  341. r->new_size = new_size;
  342. list_add_tail(&r->list, &c->replay_list);
  343. return 0;
  344. }
  345. /**
  346. * insert_dent - insert a directory entry node into the replay list.
  347. * @c: UBIFS file-system description object
  348. * @lnum: node logical eraseblock number
  349. * @offs: node offset
  350. * @len: node length
  351. * @key: node key
  352. * @name: directory entry name
  353. * @nlen: directory entry name length
  354. * @sqnum: sequence number
  355. * @deletion: non-zero if this is a deletion
  356. * @used: number of bytes in use in a LEB
  357. *
  358. * This function inserts a scanned directory entry node or an extended
  359. * attribute entry to the replay list. Returns zero in case of success and a
  360. * negative error code in case of failure.
  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 replay_entry *r;
  367. char *nbuf;
  368. dbg_mntk(key, "add LEB %d:%d, key ", lnum, offs);
  369. if (key_inum(c, key) >= c->highest_inum)
  370. c->highest_inum = key_inum(c, key);
  371. r = kzalloc(sizeof(struct replay_entry), GFP_KERNEL);
  372. if (!r)
  373. return -ENOMEM;
  374. nbuf = kmalloc(nlen + 1, GFP_KERNEL);
  375. if (!nbuf) {
  376. kfree(r);
  377. return -ENOMEM;
  378. }
  379. if (!deletion)
  380. *used += ALIGN(len, 8);
  381. r->lnum = lnum;
  382. r->offs = offs;
  383. r->len = len;
  384. r->deletion = !!deletion;
  385. r->sqnum = sqnum;
  386. key_copy(c, key, &r->key);
  387. r->nm.len = nlen;
  388. memcpy(nbuf, name, nlen);
  389. nbuf[nlen] = '\0';
  390. r->nm.name = nbuf;
  391. list_add_tail(&r->list, &c->replay_list);
  392. return 0;
  393. }
  394. /**
  395. * ubifs_validate_entry - validate directory or extended attribute entry node.
  396. * @c: UBIFS file-system description object
  397. * @dent: the node to validate
  398. *
  399. * This function validates directory or extended attribute entry node @dent.
  400. * Returns zero if the node is all right and a %-EINVAL if not.
  401. */
  402. int ubifs_validate_entry(struct ubifs_info *c,
  403. const struct ubifs_dent_node *dent)
  404. {
  405. int key_type = key_type_flash(c, dent->key);
  406. int nlen = le16_to_cpu(dent->nlen);
  407. if (le32_to_cpu(dent->ch.len) != nlen + UBIFS_DENT_NODE_SZ + 1 ||
  408. dent->type >= UBIFS_ITYPES_CNT ||
  409. nlen > UBIFS_MAX_NLEN || dent->name[nlen] != 0 ||
  410. strnlen(dent->name, nlen) != nlen ||
  411. le64_to_cpu(dent->inum) > MAX_INUM) {
  412. ubifs_err("bad %s node", key_type == UBIFS_DENT_KEY ?
  413. "directory entry" : "extended attribute entry");
  414. return -EINVAL;
  415. }
  416. if (key_type != UBIFS_DENT_KEY && key_type != UBIFS_XENT_KEY) {
  417. ubifs_err("bad key type %d", key_type);
  418. return -EINVAL;
  419. }
  420. return 0;
  421. }
  422. /**
  423. * is_last_bud - check if the bud is the last in the journal head.
  424. * @c: UBIFS file-system description object
  425. * @bud: bud description object
  426. *
  427. * This function checks if bud @bud is the last bud in its journal head. This
  428. * information is then used by 'replay_bud()' to decide whether the bud can
  429. * have corruptions or not. Indeed, only last buds can be corrupted by power
  430. * cuts. Returns %1 if this is the last bud, and %0 if not.
  431. */
  432. static int is_last_bud(struct ubifs_info *c, struct ubifs_bud *bud)
  433. {
  434. struct ubifs_jhead *jh = &c->jheads[bud->jhead];
  435. struct ubifs_bud *next;
  436. uint32_t data;
  437. int err;
  438. if (list_is_last(&bud->list, &jh->buds_list))
  439. return 1;
  440. /*
  441. * The following is a quirk to make sure we work correctly with UBIFS
  442. * images used with older UBIFS.
  443. *
  444. * Normally, the last bud will be the last in the journal head's list
  445. * of bud. However, there is one exception if the UBIFS image belongs
  446. * to older UBIFS. This is fairly unlikely: one would need to use old
  447. * UBIFS, then have a power cut exactly at the right point, and then
  448. * try to mount this image with new UBIFS.
  449. *
  450. * The exception is: it is possible to have 2 buds A and B, A goes
  451. * before B, and B is the last, bud B is contains no data, and bud A is
  452. * corrupted at the end. The reason is that in older versions when the
  453. * journal code switched the next bud (from A to B), it first added a
  454. * log reference node for the new bud (B), and only after this it
  455. * synchronized the write-buffer of current bud (A). But later this was
  456. * changed and UBIFS started to always synchronize the write-buffer of
  457. * the bud (A) before writing the log reference for the new bud (B).
  458. *
  459. * But because older UBIFS always synchronized A's write-buffer before
  460. * writing to B, we can recognize this exceptional situation but
  461. * checking the contents of bud B - if it is empty, then A can be
  462. * treated as the last and we can recover it.
  463. *
  464. * TODO: remove this piece of code in a couple of years (today it is
  465. * 16.05.2011).
  466. */
  467. next = list_entry(bud->list.next, struct ubifs_bud, list);
  468. if (!list_is_last(&next->list, &jh->buds_list))
  469. return 0;
  470. err = ubifs_leb_read(c, next->lnum, (char *)&data, next->start, 4, 1);
  471. if (err)
  472. return 0;
  473. return data == 0xFFFFFFFF;
  474. }
  475. /**
  476. * replay_bud - replay a bud logical eraseblock.
  477. * @c: UBIFS file-system description object
  478. * @b: bud entry which describes the bud
  479. *
  480. * This function replays bud @bud, recovers it if needed, and adds all nodes
  481. * from this bud to the replay list. Returns zero in case of success and a
  482. * negative error code in case of failure.
  483. */
  484. static int replay_bud(struct ubifs_info *c, struct bud_entry *b)
  485. {
  486. int is_last = is_last_bud(c, b->bud);
  487. int err = 0, used = 0, lnum = b->bud->lnum, offs = b->bud->start;
  488. struct ubifs_scan_leb *sleb;
  489. struct ubifs_scan_node *snod;
  490. dbg_mnt("replay bud LEB %d, head %d, offs %d, is_last %d",
  491. lnum, b->bud->jhead, offs, is_last);
  492. if (c->need_recovery && is_last)
  493. /*
  494. * Recover only last LEBs in the journal heads, because power
  495. * cuts may cause corruptions only in these LEBs, because only
  496. * these LEBs could possibly be written to at the power cut
  497. * time.
  498. */
  499. sleb = ubifs_recover_leb(c, lnum, offs, c->sbuf, b->bud->jhead);
  500. else
  501. sleb = ubifs_scan(c, lnum, offs, c->sbuf, 0);
  502. if (IS_ERR(sleb))
  503. return PTR_ERR(sleb);
  504. /*
  505. * The bud does not have to start from offset zero - the beginning of
  506. * the 'lnum' LEB may contain previously committed data. One of the
  507. * things we have to do in replay is to correctly update lprops with
  508. * newer information about this LEB.
  509. *
  510. * At this point lprops thinks that this LEB has 'c->leb_size - offs'
  511. * bytes of free space because it only contain information about
  512. * committed data.
  513. *
  514. * But we know that real amount of free space is 'c->leb_size -
  515. * sleb->endpt', and the space in the 'lnum' LEB between 'offs' and
  516. * 'sleb->endpt' is used by bud data. We have to correctly calculate
  517. * how much of these data are dirty and update lprops with this
  518. * information.
  519. *
  520. * The dirt in that LEB region is comprised of padding nodes, deletion
  521. * nodes, truncation nodes and nodes which are obsoleted by subsequent
  522. * nodes in this LEB. So instead of calculating clean space, we
  523. * calculate used space ('used' variable).
  524. */
  525. list_for_each_entry(snod, &sleb->nodes, list) {
  526. int deletion = 0;
  527. cond_resched();
  528. if (snod->sqnum >= SQNUM_WATERMARK) {
  529. ubifs_err("file system's life ended");
  530. goto out_dump;
  531. }
  532. if (snod->sqnum > c->max_sqnum)
  533. c->max_sqnum = snod->sqnum;
  534. switch (snod->type) {
  535. case UBIFS_INO_NODE:
  536. {
  537. struct ubifs_ino_node *ino = snod->node;
  538. loff_t new_size = le64_to_cpu(ino->size);
  539. if (le32_to_cpu(ino->nlink) == 0)
  540. deletion = 1;
  541. err = insert_node(c, lnum, snod->offs, snod->len,
  542. &snod->key, snod->sqnum, deletion,
  543. &used, 0, new_size);
  544. break;
  545. }
  546. case UBIFS_DATA_NODE:
  547. {
  548. struct ubifs_data_node *dn = snod->node;
  549. loff_t new_size = le32_to_cpu(dn->size) +
  550. key_block(c, &snod->key) *
  551. UBIFS_BLOCK_SIZE;
  552. err = insert_node(c, lnum, snod->offs, snod->len,
  553. &snod->key, snod->sqnum, deletion,
  554. &used, 0, new_size);
  555. break;
  556. }
  557. case UBIFS_DENT_NODE:
  558. case UBIFS_XENT_NODE:
  559. {
  560. struct ubifs_dent_node *dent = snod->node;
  561. err = ubifs_validate_entry(c, dent);
  562. if (err)
  563. goto out_dump;
  564. err = insert_dent(c, lnum, snod->offs, snod->len,
  565. &snod->key, dent->name,
  566. le16_to_cpu(dent->nlen), snod->sqnum,
  567. !le64_to_cpu(dent->inum), &used);
  568. break;
  569. }
  570. case UBIFS_TRUN_NODE:
  571. {
  572. struct ubifs_trun_node *trun = snod->node;
  573. loff_t old_size = le64_to_cpu(trun->old_size);
  574. loff_t new_size = le64_to_cpu(trun->new_size);
  575. union ubifs_key key;
  576. /* Validate truncation node */
  577. if (old_size < 0 || old_size > c->max_inode_sz ||
  578. new_size < 0 || new_size > c->max_inode_sz ||
  579. old_size <= new_size) {
  580. ubifs_err("bad truncation node");
  581. goto out_dump;
  582. }
  583. /*
  584. * Create a fake truncation key just to use the same
  585. * functions which expect nodes to have keys.
  586. */
  587. trun_key_init(c, &key, le32_to_cpu(trun->inum));
  588. err = insert_node(c, lnum, snod->offs, snod->len,
  589. &key, snod->sqnum, 1, &used,
  590. old_size, new_size);
  591. break;
  592. }
  593. default:
  594. ubifs_err("unexpected node type %d in bud LEB %d:%d",
  595. snod->type, lnum, snod->offs);
  596. err = -EINVAL;
  597. goto out_dump;
  598. }
  599. if (err)
  600. goto out;
  601. }
  602. ubifs_assert(ubifs_search_bud(c, lnum));
  603. ubifs_assert(sleb->endpt - offs >= used);
  604. ubifs_assert(sleb->endpt % c->min_io_size == 0);
  605. b->dirty = sleb->endpt - offs - used;
  606. b->free = c->leb_size - sleb->endpt;
  607. dbg_mnt("bud LEB %d replied: dirty %d, free %d", lnum, b->dirty, b->free);
  608. out:
  609. ubifs_scan_destroy(sleb);
  610. return err;
  611. out_dump:
  612. ubifs_err("bad node is at LEB %d:%d", lnum, snod->offs);
  613. dbg_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("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. dbg_err("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. dbg_err("first CS node at LEB %d:%d has wrong "
  773. "commit number %llu expected %llu",
  774. lnum, offs,
  775. (unsigned long long)le64_to_cpu(node->cmt_no),
  776. c->cmt_no);
  777. goto out_dump;
  778. }
  779. c->cs_sqnum = le64_to_cpu(node->ch.sqnum);
  780. dbg_mnt("commit start sqnum %llu", c->cs_sqnum);
  781. }
  782. if (snod->sqnum < c->cs_sqnum) {
  783. /*
  784. * This means that we reached end of log and now
  785. * look to the older log data, which was already
  786. * committed but the eraseblock was not erased (UBIFS
  787. * only un-maps it). So this basically means we have to
  788. * exit with "end of log" code.
  789. */
  790. err = 1;
  791. goto out;
  792. }
  793. /* Make sure the first node sits at offset zero of the LEB */
  794. if (snod->offs != 0) {
  795. dbg_err("first node is not at zero offset");
  796. goto out_dump;
  797. }
  798. list_for_each_entry(snod, &sleb->nodes, list) {
  799. cond_resched();
  800. if (snod->sqnum >= SQNUM_WATERMARK) {
  801. ubifs_err("file system's life ended");
  802. goto out_dump;
  803. }
  804. if (snod->sqnum < c->cs_sqnum) {
  805. dbg_err("bad sqnum %llu, commit sqnum %llu",
  806. snod->sqnum, c->cs_sqnum);
  807. goto out_dump;
  808. }
  809. if (snod->sqnum > c->max_sqnum)
  810. c->max_sqnum = snod->sqnum;
  811. switch (snod->type) {
  812. case UBIFS_REF_NODE: {
  813. const struct ubifs_ref_node *ref = snod->node;
  814. err = validate_ref(c, ref);
  815. if (err == 1)
  816. break; /* Already have this bud */
  817. if (err)
  818. goto out_dump;
  819. err = add_replay_bud(c, le32_to_cpu(ref->lnum),
  820. le32_to_cpu(ref->offs),
  821. le32_to_cpu(ref->jhead),
  822. snod->sqnum);
  823. if (err)
  824. goto out;
  825. break;
  826. }
  827. case UBIFS_CS_NODE:
  828. /* Make sure it sits at the beginning of LEB */
  829. if (snod->offs != 0) {
  830. ubifs_err("unexpected node in log");
  831. goto out_dump;
  832. }
  833. break;
  834. default:
  835. ubifs_err("unexpected node in log");
  836. goto out_dump;
  837. }
  838. }
  839. if (sleb->endpt || c->lhead_offs >= c->leb_size) {
  840. c->lhead_lnum = lnum;
  841. c->lhead_offs = sleb->endpt;
  842. }
  843. err = !sleb->endpt;
  844. out:
  845. ubifs_scan_destroy(sleb);
  846. return err;
  847. out_dump:
  848. ubifs_err("log error detected while replaying the log at LEB %d:%d",
  849. lnum, offs + snod->offs);
  850. dbg_dump_node(c, snod->node);
  851. ubifs_scan_destroy(sleb);
  852. return -EINVAL;
  853. }
  854. /**
  855. * take_ihead - update the status of the index head in lprops to 'taken'.
  856. * @c: UBIFS file-system description object
  857. *
  858. * This function returns the amount of free space in the index head LEB or a
  859. * negative error code.
  860. */
  861. static int take_ihead(struct ubifs_info *c)
  862. {
  863. const struct ubifs_lprops *lp;
  864. int err, free;
  865. ubifs_get_lprops(c);
  866. lp = ubifs_lpt_lookup_dirty(c, c->ihead_lnum);
  867. if (IS_ERR(lp)) {
  868. err = PTR_ERR(lp);
  869. goto out;
  870. }
  871. free = lp->free;
  872. lp = ubifs_change_lp(c, lp, LPROPS_NC, LPROPS_NC,
  873. lp->flags | LPROPS_TAKEN, 0);
  874. if (IS_ERR(lp)) {
  875. err = PTR_ERR(lp);
  876. goto out;
  877. }
  878. err = free;
  879. out:
  880. ubifs_release_lprops(c);
  881. return err;
  882. }
  883. /**
  884. * ubifs_replay_journal - replay journal.
  885. * @c: UBIFS file-system description object
  886. *
  887. * This function scans the journal, replays and cleans it up. It makes sure all
  888. * memory data structures related to uncommitted journal are built (dirty TNC
  889. * tree, tree of buds, modified lprops, etc).
  890. */
  891. int ubifs_replay_journal(struct ubifs_info *c)
  892. {
  893. int err, i, lnum, offs, free;
  894. BUILD_BUG_ON(UBIFS_TRUN_KEY > 5);
  895. /* Update the status of the index head in lprops to 'taken' */
  896. free = take_ihead(c);
  897. if (free < 0)
  898. return free; /* Error code */
  899. if (c->ihead_offs != c->leb_size - free) {
  900. ubifs_err("bad index head LEB %d:%d", c->ihead_lnum,
  901. c->ihead_offs);
  902. return -EINVAL;
  903. }
  904. dbg_mnt("start replaying the journal");
  905. c->replaying = 1;
  906. lnum = c->ltail_lnum = c->lhead_lnum;
  907. offs = c->lhead_offs;
  908. for (i = 0; i < c->log_lebs; i++, lnum++) {
  909. if (lnum >= UBIFS_LOG_LNUM + c->log_lebs) {
  910. /*
  911. * The log is logically circular, we reached the last
  912. * LEB, switch to the first one.
  913. */
  914. lnum = UBIFS_LOG_LNUM;
  915. offs = 0;
  916. }
  917. err = replay_log_leb(c, lnum, offs, c->sbuf);
  918. if (err == 1)
  919. /* We hit the end of the log */
  920. break;
  921. if (err)
  922. goto out;
  923. offs = 0;
  924. }
  925. err = replay_buds(c);
  926. if (err)
  927. goto out;
  928. err = apply_replay_list(c);
  929. if (err)
  930. goto out;
  931. err = set_buds_lprops(c);
  932. if (err)
  933. goto out;
  934. /*
  935. * UBIFS budgeting calculations use @c->bi.uncommitted_idx variable
  936. * to roughly estimate index growth. Things like @c->bi.min_idx_lebs
  937. * depend on it. This means we have to initialize it to make sure
  938. * budgeting works properly.
  939. */
  940. c->bi.uncommitted_idx = atomic_long_read(&c->dirty_zn_cnt);
  941. c->bi.uncommitted_idx *= c->max_idx_node_sz;
  942. ubifs_assert(c->bud_bytes <= c->max_bud_bytes || c->need_recovery);
  943. dbg_mnt("finished, log head LEB %d:%d, max_sqnum %llu, "
  944. "highest_inum %lu", c->lhead_lnum, c->lhead_offs, c->max_sqnum,
  945. (unsigned long)c->highest_inum);
  946. out:
  947. destroy_replay_list(c);
  948. destroy_bud_list(c);
  949. c->replaying = 0;
  950. return err;
  951. }