replay.c 32 KB

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