replay.c 30 KB

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