replay.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081
  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_mnt("LEB %d:%d len %d deletion %d sqnum %llu %s", r->lnum,
  204. r->offs, r->len, r->deletion, r->sqnum, DBGKEY(&r->key));
  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_mnt("add LEB %d:%d, key %s", lnum, offs, DBGKEY(key));
  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_mnt("add LEB %d:%d, key %s", lnum, offs, DBGKEY(key));
  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 = ubi_read(c->ubi, next->lnum, (char *)&data,
  471. next->start, 4);
  472. if (err)
  473. return 0;
  474. return data == 0xFFFFFFFF;
  475. }
  476. /**
  477. * replay_bud - replay a bud logical eraseblock.
  478. * @c: UBIFS file-system description object
  479. * @b: bud entry which describes the bud
  480. *
  481. * This function replays bud @bud, recovers it if needed, and adds all nodes
  482. * from this bud to the replay list. Returns zero in case of success and a
  483. * negative error code in case of failure.
  484. */
  485. static int replay_bud(struct ubifs_info *c, struct bud_entry *b)
  486. {
  487. int is_last = is_last_bud(c, b->bud);
  488. int err = 0, used = 0, lnum = b->bud->lnum, offs = b->bud->start;
  489. struct ubifs_scan_leb *sleb;
  490. struct ubifs_scan_node *snod;
  491. dbg_mnt("replay bud LEB %d, head %d, offs %d, is_last %d",
  492. lnum, b->bud->jhead, offs, is_last);
  493. if (c->need_recovery && is_last)
  494. /*
  495. * Recover only last LEBs in the journal heads, because power
  496. * cuts may cause corruptions only in these LEBs, because only
  497. * these LEBs could possibly be written to at the power cut
  498. * time.
  499. */
  500. sleb = ubifs_recover_leb(c, lnum, offs, c->sbuf, b->bud->jhead);
  501. else
  502. sleb = ubifs_scan(c, lnum, offs, c->sbuf, 0);
  503. if (IS_ERR(sleb))
  504. return PTR_ERR(sleb);
  505. /*
  506. * The bud does not have to start from offset zero - the beginning of
  507. * the 'lnum' LEB may contain previously committed data. One of the
  508. * things we have to do in replay is to correctly update lprops with
  509. * newer information about this LEB.
  510. *
  511. * At this point lprops thinks that this LEB has 'c->leb_size - offs'
  512. * bytes of free space because it only contain information about
  513. * committed data.
  514. *
  515. * But we know that real amount of free space is 'c->leb_size -
  516. * sleb->endpt', and the space in the 'lnum' LEB between 'offs' and
  517. * 'sleb->endpt' is used by bud data. We have to correctly calculate
  518. * how much of these data are dirty and update lprops with this
  519. * information.
  520. *
  521. * The dirt in that LEB region is comprised of padding nodes, deletion
  522. * nodes, truncation nodes and nodes which are obsoleted by subsequent
  523. * nodes in this LEB. So instead of calculating clean space, we
  524. * calculate used space ('used' variable).
  525. */
  526. list_for_each_entry(snod, &sleb->nodes, list) {
  527. int deletion = 0;
  528. cond_resched();
  529. if (snod->sqnum >= SQNUM_WATERMARK) {
  530. ubifs_err("file system's life ended");
  531. goto out_dump;
  532. }
  533. if (snod->sqnum > c->max_sqnum)
  534. c->max_sqnum = snod->sqnum;
  535. switch (snod->type) {
  536. case UBIFS_INO_NODE:
  537. {
  538. struct ubifs_ino_node *ino = snod->node;
  539. loff_t new_size = le64_to_cpu(ino->size);
  540. if (le32_to_cpu(ino->nlink) == 0)
  541. deletion = 1;
  542. err = insert_node(c, lnum, snod->offs, snod->len,
  543. &snod->key, snod->sqnum, deletion,
  544. &used, 0, new_size);
  545. break;
  546. }
  547. case UBIFS_DATA_NODE:
  548. {
  549. struct ubifs_data_node *dn = snod->node;
  550. loff_t new_size = le32_to_cpu(dn->size) +
  551. key_block(c, &snod->key) *
  552. UBIFS_BLOCK_SIZE;
  553. err = insert_node(c, lnum, snod->offs, snod->len,
  554. &snod->key, snod->sqnum, deletion,
  555. &used, 0, new_size);
  556. break;
  557. }
  558. case UBIFS_DENT_NODE:
  559. case UBIFS_XENT_NODE:
  560. {
  561. struct ubifs_dent_node *dent = snod->node;
  562. err = ubifs_validate_entry(c, dent);
  563. if (err)
  564. goto out_dump;
  565. err = insert_dent(c, lnum, snod->offs, snod->len,
  566. &snod->key, dent->name,
  567. le16_to_cpu(dent->nlen), snod->sqnum,
  568. !le64_to_cpu(dent->inum), &used);
  569. break;
  570. }
  571. case UBIFS_TRUN_NODE:
  572. {
  573. struct ubifs_trun_node *trun = snod->node;
  574. loff_t old_size = le64_to_cpu(trun->old_size);
  575. loff_t new_size = le64_to_cpu(trun->new_size);
  576. union ubifs_key key;
  577. /* Validate truncation node */
  578. if (old_size < 0 || old_size > c->max_inode_sz ||
  579. new_size < 0 || new_size > c->max_inode_sz ||
  580. old_size <= new_size) {
  581. ubifs_err("bad truncation node");
  582. goto out_dump;
  583. }
  584. /*
  585. * Create a fake truncation key just to use the same
  586. * functions which expect nodes to have keys.
  587. */
  588. trun_key_init(c, &key, le32_to_cpu(trun->inum));
  589. err = insert_node(c, lnum, snod->offs, snod->len,
  590. &key, snod->sqnum, 1, &used,
  591. old_size, new_size);
  592. break;
  593. }
  594. default:
  595. ubifs_err("unexpected node type %d in bud LEB %d:%d",
  596. snod->type, lnum, snod->offs);
  597. err = -EINVAL;
  598. goto out_dump;
  599. }
  600. if (err)
  601. goto out;
  602. }
  603. ubifs_assert(ubifs_search_bud(c, lnum));
  604. ubifs_assert(sleb->endpt - offs >= used);
  605. ubifs_assert(sleb->endpt % c->min_io_size == 0);
  606. b->dirty = sleb->endpt - offs - used;
  607. b->free = c->leb_size - sleb->endpt;
  608. dbg_mnt("bud LEB %d replied: dirty %d, free %d", lnum, b->dirty, b->free);
  609. out:
  610. ubifs_scan_destroy(sleb);
  611. return err;
  612. out_dump:
  613. ubifs_err("bad node is at LEB %d:%d", lnum, snod->offs);
  614. dbg_dump_node(c, snod->node);
  615. ubifs_scan_destroy(sleb);
  616. return -EINVAL;
  617. }
  618. /**
  619. * replay_buds - replay all buds.
  620. * @c: UBIFS file-system description object
  621. *
  622. * This function returns zero in case of success and a negative error code in
  623. * case of failure.
  624. */
  625. static int replay_buds(struct ubifs_info *c)
  626. {
  627. struct bud_entry *b;
  628. int err;
  629. unsigned long long prev_sqnum = 0;
  630. list_for_each_entry(b, &c->replay_buds, list) {
  631. err = replay_bud(c, b);
  632. if (err)
  633. return err;
  634. ubifs_assert(b->sqnum > prev_sqnum);
  635. prev_sqnum = b->sqnum;
  636. }
  637. return 0;
  638. }
  639. /**
  640. * destroy_bud_list - destroy the list of buds to replay.
  641. * @c: UBIFS file-system description object
  642. */
  643. static void destroy_bud_list(struct ubifs_info *c)
  644. {
  645. struct bud_entry *b;
  646. while (!list_empty(&c->replay_buds)) {
  647. b = list_entry(c->replay_buds.next, struct bud_entry, list);
  648. list_del(&b->list);
  649. kfree(b);
  650. }
  651. }
  652. /**
  653. * add_replay_bud - add a bud to the list of buds to replay.
  654. * @c: UBIFS file-system description object
  655. * @lnum: bud logical eraseblock number to replay
  656. * @offs: bud start offset
  657. * @jhead: journal head to which this bud belongs
  658. * @sqnum: reference node sequence number
  659. *
  660. * This function returns zero in case of success and a negative error code in
  661. * case of failure.
  662. */
  663. static int add_replay_bud(struct ubifs_info *c, int lnum, int offs, int jhead,
  664. unsigned long long sqnum)
  665. {
  666. struct ubifs_bud *bud;
  667. struct bud_entry *b;
  668. dbg_mnt("add replay bud LEB %d:%d, head %d", lnum, offs, jhead);
  669. bud = kmalloc(sizeof(struct ubifs_bud), GFP_KERNEL);
  670. if (!bud)
  671. return -ENOMEM;
  672. b = kmalloc(sizeof(struct bud_entry), GFP_KERNEL);
  673. if (!b) {
  674. kfree(bud);
  675. return -ENOMEM;
  676. }
  677. bud->lnum = lnum;
  678. bud->start = offs;
  679. bud->jhead = jhead;
  680. ubifs_add_bud(c, bud);
  681. b->bud = bud;
  682. b->sqnum = sqnum;
  683. list_add_tail(&b->list, &c->replay_buds);
  684. return 0;
  685. }
  686. /**
  687. * validate_ref - validate a reference node.
  688. * @c: UBIFS file-system description object
  689. * @ref: the reference node to validate
  690. * @ref_lnum: LEB number of the reference node
  691. * @ref_offs: reference node offset
  692. *
  693. * This function returns %1 if a bud reference already exists for the LEB. %0 is
  694. * returned if the reference node is new, otherwise %-EINVAL is returned if
  695. * validation failed.
  696. */
  697. static int validate_ref(struct ubifs_info *c, const struct ubifs_ref_node *ref)
  698. {
  699. struct ubifs_bud *bud;
  700. int lnum = le32_to_cpu(ref->lnum);
  701. unsigned int offs = le32_to_cpu(ref->offs);
  702. unsigned int jhead = le32_to_cpu(ref->jhead);
  703. /*
  704. * ref->offs may point to the end of LEB when the journal head points
  705. * to the end of LEB and we write reference node for it during commit.
  706. * So this is why we require 'offs > c->leb_size'.
  707. */
  708. if (jhead >= c->jhead_cnt || lnum >= c->leb_cnt ||
  709. lnum < c->main_first || offs > c->leb_size ||
  710. offs & (c->min_io_size - 1))
  711. return -EINVAL;
  712. /* Make sure we have not already looked at this bud */
  713. bud = ubifs_search_bud(c, lnum);
  714. if (bud) {
  715. if (bud->jhead == jhead && bud->start <= offs)
  716. return 1;
  717. ubifs_err("bud at LEB %d:%d was already referred", lnum, offs);
  718. return -EINVAL;
  719. }
  720. return 0;
  721. }
  722. /**
  723. * replay_log_leb - replay a log logical eraseblock.
  724. * @c: UBIFS file-system description object
  725. * @lnum: log logical eraseblock to replay
  726. * @offs: offset to start replaying from
  727. * @sbuf: scan buffer
  728. *
  729. * This function replays a log LEB and returns zero in case of success, %1 if
  730. * this is the last LEB in the log, and a negative error code in case of
  731. * failure.
  732. */
  733. static int replay_log_leb(struct ubifs_info *c, int lnum, int offs, void *sbuf)
  734. {
  735. int err;
  736. struct ubifs_scan_leb *sleb;
  737. struct ubifs_scan_node *snod;
  738. const struct ubifs_cs_node *node;
  739. dbg_mnt("replay log LEB %d:%d", lnum, offs);
  740. sleb = ubifs_scan(c, lnum, offs, sbuf, c->need_recovery);
  741. if (IS_ERR(sleb)) {
  742. if (PTR_ERR(sleb) != -EUCLEAN || !c->need_recovery)
  743. return PTR_ERR(sleb);
  744. /*
  745. * Note, the below function will recover this log LEB only if
  746. * it is the last, because unclean reboots can possibly corrupt
  747. * only the tail of the log.
  748. */
  749. sleb = ubifs_recover_log_leb(c, lnum, offs, sbuf);
  750. if (IS_ERR(sleb))
  751. return PTR_ERR(sleb);
  752. }
  753. if (sleb->nodes_cnt == 0) {
  754. err = 1;
  755. goto out;
  756. }
  757. node = sleb->buf;
  758. snod = list_entry(sleb->nodes.next, struct ubifs_scan_node, list);
  759. if (c->cs_sqnum == 0) {
  760. /*
  761. * This is the first log LEB we are looking at, make sure that
  762. * the first node is a commit start node. Also record its
  763. * sequence number so that UBIFS can determine where the log
  764. * ends, because all nodes which were have higher sequence
  765. * numbers.
  766. */
  767. if (snod->type != UBIFS_CS_NODE) {
  768. dbg_err("first log node at LEB %d:%d is not CS node",
  769. lnum, offs);
  770. goto out_dump;
  771. }
  772. if (le64_to_cpu(node->cmt_no) != c->cmt_no) {
  773. dbg_err("first CS node at LEB %d:%d has wrong "
  774. "commit number %llu expected %llu",
  775. lnum, offs,
  776. (unsigned long long)le64_to_cpu(node->cmt_no),
  777. c->cmt_no);
  778. goto out_dump;
  779. }
  780. c->cs_sqnum = le64_to_cpu(node->ch.sqnum);
  781. dbg_mnt("commit start sqnum %llu", c->cs_sqnum);
  782. }
  783. if (snod->sqnum < c->cs_sqnum) {
  784. /*
  785. * This means that we reached end of log and now
  786. * look to the older log data, which was already
  787. * committed but the eraseblock was not erased (UBIFS
  788. * only un-maps it). So this basically means we have to
  789. * exit with "end of log" code.
  790. */
  791. err = 1;
  792. goto out;
  793. }
  794. /* Make sure the first node sits at offset zero of the LEB */
  795. if (snod->offs != 0) {
  796. dbg_err("first node is not at zero offset");
  797. goto out_dump;
  798. }
  799. list_for_each_entry(snod, &sleb->nodes, list) {
  800. cond_resched();
  801. if (snod->sqnum >= SQNUM_WATERMARK) {
  802. ubifs_err("file system's life ended");
  803. goto out_dump;
  804. }
  805. if (snod->sqnum < c->cs_sqnum) {
  806. dbg_err("bad sqnum %llu, commit sqnum %llu",
  807. snod->sqnum, c->cs_sqnum);
  808. goto out_dump;
  809. }
  810. if (snod->sqnum > c->max_sqnum)
  811. c->max_sqnum = snod->sqnum;
  812. switch (snod->type) {
  813. case UBIFS_REF_NODE: {
  814. const struct ubifs_ref_node *ref = snod->node;
  815. err = validate_ref(c, ref);
  816. if (err == 1)
  817. break; /* Already have this bud */
  818. if (err)
  819. goto out_dump;
  820. err = add_replay_bud(c, le32_to_cpu(ref->lnum),
  821. le32_to_cpu(ref->offs),
  822. le32_to_cpu(ref->jhead),
  823. snod->sqnum);
  824. if (err)
  825. goto out;
  826. break;
  827. }
  828. case UBIFS_CS_NODE:
  829. /* Make sure it sits at the beginning of LEB */
  830. if (snod->offs != 0) {
  831. ubifs_err("unexpected node in log");
  832. goto out_dump;
  833. }
  834. break;
  835. default:
  836. ubifs_err("unexpected node in log");
  837. goto out_dump;
  838. }
  839. }
  840. if (sleb->endpt || c->lhead_offs >= c->leb_size) {
  841. c->lhead_lnum = lnum;
  842. c->lhead_offs = sleb->endpt;
  843. }
  844. err = !sleb->endpt;
  845. out:
  846. ubifs_scan_destroy(sleb);
  847. return err;
  848. out_dump:
  849. ubifs_err("log error detected while replaying the log at LEB %d:%d",
  850. lnum, offs + snod->offs);
  851. dbg_dump_node(c, snod->node);
  852. ubifs_scan_destroy(sleb);
  853. return -EINVAL;
  854. }
  855. /**
  856. * take_ihead - update the status of the index head in lprops to 'taken'.
  857. * @c: UBIFS file-system description object
  858. *
  859. * This function returns the amount of free space in the index head LEB or a
  860. * negative error code.
  861. */
  862. static int take_ihead(struct ubifs_info *c)
  863. {
  864. const struct ubifs_lprops *lp;
  865. int err, free;
  866. ubifs_get_lprops(c);
  867. lp = ubifs_lpt_lookup_dirty(c, c->ihead_lnum);
  868. if (IS_ERR(lp)) {
  869. err = PTR_ERR(lp);
  870. goto out;
  871. }
  872. free = lp->free;
  873. lp = ubifs_change_lp(c, lp, LPROPS_NC, LPROPS_NC,
  874. lp->flags | LPROPS_TAKEN, 0);
  875. if (IS_ERR(lp)) {
  876. err = PTR_ERR(lp);
  877. goto out;
  878. }
  879. err = free;
  880. out:
  881. ubifs_release_lprops(c);
  882. return err;
  883. }
  884. /**
  885. * ubifs_replay_journal - replay journal.
  886. * @c: UBIFS file-system description object
  887. *
  888. * This function scans the journal, replays and cleans it up. It makes sure all
  889. * memory data structures related to uncommitted journal are built (dirty TNC
  890. * tree, tree of buds, modified lprops, etc).
  891. */
  892. int ubifs_replay_journal(struct ubifs_info *c)
  893. {
  894. int err, i, lnum, offs, free;
  895. BUILD_BUG_ON(UBIFS_TRUN_KEY > 5);
  896. /* Update the status of the index head in lprops to 'taken' */
  897. free = take_ihead(c);
  898. if (free < 0)
  899. return free; /* Error code */
  900. if (c->ihead_offs != c->leb_size - free) {
  901. ubifs_err("bad index head LEB %d:%d", c->ihead_lnum,
  902. c->ihead_offs);
  903. return -EINVAL;
  904. }
  905. dbg_mnt("start replaying the journal");
  906. c->replaying = 1;
  907. lnum = c->ltail_lnum = c->lhead_lnum;
  908. offs = c->lhead_offs;
  909. for (i = 0; i < c->log_lebs; i++, lnum++) {
  910. if (lnum >= UBIFS_LOG_LNUM + c->log_lebs) {
  911. /*
  912. * The log is logically circular, we reached the last
  913. * LEB, switch to the first one.
  914. */
  915. lnum = UBIFS_LOG_LNUM;
  916. offs = 0;
  917. }
  918. err = replay_log_leb(c, lnum, offs, c->sbuf);
  919. if (err == 1)
  920. /* We hit the end of the log */
  921. break;
  922. if (err)
  923. goto out;
  924. offs = 0;
  925. }
  926. err = replay_buds(c);
  927. if (err)
  928. goto out;
  929. err = apply_replay_list(c);
  930. if (err)
  931. goto out;
  932. err = set_buds_lprops(c);
  933. if (err)
  934. goto out;
  935. /*
  936. * UBIFS budgeting calculations use @c->bi.uncommitted_idx variable
  937. * to roughly estimate index growth. Things like @c->bi.min_idx_lebs
  938. * depend on it. This means we have to initialize it to make sure
  939. * budgeting works properly.
  940. */
  941. c->bi.uncommitted_idx = atomic_long_read(&c->dirty_zn_cnt);
  942. c->bi.uncommitted_idx *= c->max_idx_node_sz;
  943. ubifs_assert(c->bud_bytes <= c->max_bud_bytes || c->need_recovery);
  944. dbg_mnt("finished, log head LEB %d:%d, max_sqnum %llu, "
  945. "highest_inum %lu", c->lhead_lnum, c->lhead_offs, c->max_sqnum,
  946. (unsigned long)c->highest_inum);
  947. out:
  948. destroy_replay_list(c);
  949. destroy_bud_list(c);
  950. c->replaying = 0;
  951. return err;
  952. }