replay.c 29 KB

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