replay.c 28 KB

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