commit.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  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 implements functions that manage the running of the commit process.
  24. * Each affected module has its own functions to accomplish their part in the
  25. * commit and those functions are called here.
  26. *
  27. * The commit is the process whereby all updates to the index and LEB properties
  28. * are written out together and the journal becomes empty. This keeps the
  29. * file system consistent - at all times the state can be recreated by reading
  30. * the index and LEB properties and then replaying the journal.
  31. *
  32. * The commit is split into two parts named "commit start" and "commit end".
  33. * During commit start, the commit process has exclusive access to the journal
  34. * by holding the commit semaphore down for writing. As few I/O operations as
  35. * possible are performed during commit start, instead the nodes that are to be
  36. * written are merely identified. During commit end, the commit semaphore is no
  37. * longer held and the journal is again in operation, allowing users to continue
  38. * to use the file system while the bulk of the commit I/O is performed. The
  39. * purpose of this two-step approach is to prevent the commit from causing any
  40. * latency blips. Note that in any case, the commit does not prevent lookups
  41. * (as permitted by the TNC mutex), or access to VFS data structures e.g. page
  42. * cache.
  43. */
  44. #include <linux/freezer.h>
  45. #include <linux/kthread.h>
  46. #include <linux/slab.h>
  47. #include "ubifs.h"
  48. /*
  49. * nothing_to_commit - check if there is nothing to commit.
  50. * @c: UBIFS file-system description object
  51. *
  52. * This is a helper function which checks if there is anything to commit. It is
  53. * used as an optimization to avoid starting the commit if it is not really
  54. * necessary. Indeed, the commit operation always assumes flash I/O (e.g.,
  55. * writing the commit start node to the log), and it is better to avoid doing
  56. * this unnecessarily. E.g., 'ubifs_sync_fs()' runs the commit, but if there is
  57. * nothing to commit, it is more optimal to avoid any flash I/O.
  58. *
  59. * This function has to be called with @c->commit_sem locked for writing -
  60. * this function does not take LPT/TNC locks because the @c->commit_sem
  61. * guarantees that we have exclusive access to the TNC and LPT data structures.
  62. *
  63. * This function returns %1 if there is nothing to commit and %0 otherwise.
  64. */
  65. static int nothing_to_commit(struct ubifs_info *c)
  66. {
  67. /*
  68. * During mounting or remounting from R/O mode to R/W mode we may
  69. * commit for various recovery-related reasons.
  70. */
  71. if (c->mounting || c->remounting_rw)
  72. return 0;
  73. /*
  74. * If the root TNC node is dirty, we definitely have something to
  75. * commit.
  76. */
  77. if (c->zroot.znode && ubifs_zn_dirty(c->zroot.znode))
  78. return 0;
  79. /*
  80. * Even though the TNC is clean, the LPT tree may have dirty nodes. For
  81. * example, this may happen if the budgeting subsystem invoked GC to
  82. * make some free space, and the GC found an LEB with only dirty and
  83. * free space. In this case GC would just change the lprops of this
  84. * LEB (by turning all space into free space) and unmap it.
  85. */
  86. if (c->nroot && test_bit(DIRTY_CNODE, &c->nroot->flags))
  87. return 0;
  88. ubifs_assert(atomic_long_read(&c->dirty_zn_cnt) == 0);
  89. ubifs_assert(c->dirty_pn_cnt == 0);
  90. ubifs_assert(c->dirty_nn_cnt == 0);
  91. return 1;
  92. }
  93. /**
  94. * do_commit - commit the journal.
  95. * @c: UBIFS file-system description object
  96. *
  97. * This function implements UBIFS commit. It has to be called with commit lock
  98. * locked. Returns zero in case of success and a negative error code in case of
  99. * failure.
  100. */
  101. static int do_commit(struct ubifs_info *c)
  102. {
  103. int err, new_ltail_lnum, old_ltail_lnum, i;
  104. struct ubifs_zbranch zroot;
  105. struct ubifs_lp_stats lst;
  106. dbg_cmt("start");
  107. ubifs_assert(!c->ro_media && !c->ro_mount);
  108. if (c->ro_error) {
  109. err = -EROFS;
  110. goto out_up;
  111. }
  112. if (nothing_to_commit(c)) {
  113. up_write(&c->commit_sem);
  114. err = 0;
  115. goto out_cancel;
  116. }
  117. /* Sync all write buffers (necessary for recovery) */
  118. for (i = 0; i < c->jhead_cnt; i++) {
  119. err = ubifs_wbuf_sync(&c->jheads[i].wbuf);
  120. if (err)
  121. goto out_up;
  122. }
  123. c->cmt_no += 1;
  124. err = ubifs_gc_start_commit(c);
  125. if (err)
  126. goto out_up;
  127. err = dbg_check_lprops(c);
  128. if (err)
  129. goto out_up;
  130. err = ubifs_log_start_commit(c, &new_ltail_lnum);
  131. if (err)
  132. goto out_up;
  133. err = ubifs_tnc_start_commit(c, &zroot);
  134. if (err)
  135. goto out_up;
  136. err = ubifs_lpt_start_commit(c);
  137. if (err)
  138. goto out_up;
  139. err = ubifs_orphan_start_commit(c);
  140. if (err)
  141. goto out_up;
  142. ubifs_get_lp_stats(c, &lst);
  143. up_write(&c->commit_sem);
  144. err = ubifs_tnc_end_commit(c);
  145. if (err)
  146. goto out;
  147. err = ubifs_lpt_end_commit(c);
  148. if (err)
  149. goto out;
  150. err = ubifs_orphan_end_commit(c);
  151. if (err)
  152. goto out;
  153. err = dbg_check_old_index(c, &zroot);
  154. if (err)
  155. goto out;
  156. c->mst_node->cmt_no = cpu_to_le64(c->cmt_no);
  157. c->mst_node->log_lnum = cpu_to_le32(new_ltail_lnum);
  158. c->mst_node->root_lnum = cpu_to_le32(zroot.lnum);
  159. c->mst_node->root_offs = cpu_to_le32(zroot.offs);
  160. c->mst_node->root_len = cpu_to_le32(zroot.len);
  161. c->mst_node->ihead_lnum = cpu_to_le32(c->ihead_lnum);
  162. c->mst_node->ihead_offs = cpu_to_le32(c->ihead_offs);
  163. c->mst_node->index_size = cpu_to_le64(c->bi.old_idx_sz);
  164. c->mst_node->lpt_lnum = cpu_to_le32(c->lpt_lnum);
  165. c->mst_node->lpt_offs = cpu_to_le32(c->lpt_offs);
  166. c->mst_node->nhead_lnum = cpu_to_le32(c->nhead_lnum);
  167. c->mst_node->nhead_offs = cpu_to_le32(c->nhead_offs);
  168. c->mst_node->ltab_lnum = cpu_to_le32(c->ltab_lnum);
  169. c->mst_node->ltab_offs = cpu_to_le32(c->ltab_offs);
  170. c->mst_node->lsave_lnum = cpu_to_le32(c->lsave_lnum);
  171. c->mst_node->lsave_offs = cpu_to_le32(c->lsave_offs);
  172. c->mst_node->lscan_lnum = cpu_to_le32(c->lscan_lnum);
  173. c->mst_node->empty_lebs = cpu_to_le32(lst.empty_lebs);
  174. c->mst_node->idx_lebs = cpu_to_le32(lst.idx_lebs);
  175. c->mst_node->total_free = cpu_to_le64(lst.total_free);
  176. c->mst_node->total_dirty = cpu_to_le64(lst.total_dirty);
  177. c->mst_node->total_used = cpu_to_le64(lst.total_used);
  178. c->mst_node->total_dead = cpu_to_le64(lst.total_dead);
  179. c->mst_node->total_dark = cpu_to_le64(lst.total_dark);
  180. if (c->no_orphs)
  181. c->mst_node->flags |= cpu_to_le32(UBIFS_MST_NO_ORPHS);
  182. else
  183. c->mst_node->flags &= ~cpu_to_le32(UBIFS_MST_NO_ORPHS);
  184. old_ltail_lnum = c->ltail_lnum;
  185. err = ubifs_log_end_commit(c, new_ltail_lnum);
  186. if (err)
  187. goto out;
  188. err = ubifs_log_post_commit(c, old_ltail_lnum);
  189. if (err)
  190. goto out;
  191. err = ubifs_gc_end_commit(c);
  192. if (err)
  193. goto out;
  194. err = ubifs_lpt_post_commit(c);
  195. if (err)
  196. goto out;
  197. out_cancel:
  198. spin_lock(&c->cs_lock);
  199. c->cmt_state = COMMIT_RESTING;
  200. wake_up(&c->cmt_wq);
  201. dbg_cmt("commit end");
  202. spin_unlock(&c->cs_lock);
  203. return 0;
  204. out_up:
  205. up_write(&c->commit_sem);
  206. out:
  207. ubifs_err(c, "commit failed, error %d", err);
  208. spin_lock(&c->cs_lock);
  209. c->cmt_state = COMMIT_BROKEN;
  210. wake_up(&c->cmt_wq);
  211. spin_unlock(&c->cs_lock);
  212. ubifs_ro_mode(c, err);
  213. return err;
  214. }
  215. /**
  216. * run_bg_commit - run background commit if it is needed.
  217. * @c: UBIFS file-system description object
  218. *
  219. * This function runs background commit if it is needed. Returns zero in case
  220. * of success and a negative error code in case of failure.
  221. */
  222. static int run_bg_commit(struct ubifs_info *c)
  223. {
  224. spin_lock(&c->cs_lock);
  225. /*
  226. * Run background commit only if background commit was requested or if
  227. * commit is required.
  228. */
  229. if (c->cmt_state != COMMIT_BACKGROUND &&
  230. c->cmt_state != COMMIT_REQUIRED)
  231. goto out;
  232. spin_unlock(&c->cs_lock);
  233. down_write(&c->commit_sem);
  234. spin_lock(&c->cs_lock);
  235. if (c->cmt_state == COMMIT_REQUIRED)
  236. c->cmt_state = COMMIT_RUNNING_REQUIRED;
  237. else if (c->cmt_state == COMMIT_BACKGROUND)
  238. c->cmt_state = COMMIT_RUNNING_BACKGROUND;
  239. else
  240. goto out_cmt_unlock;
  241. spin_unlock(&c->cs_lock);
  242. return do_commit(c);
  243. out_cmt_unlock:
  244. up_write(&c->commit_sem);
  245. out:
  246. spin_unlock(&c->cs_lock);
  247. return 0;
  248. }
  249. /**
  250. * ubifs_bg_thread - UBIFS background thread function.
  251. * @info: points to the file-system description object
  252. *
  253. * This function implements various file-system background activities:
  254. * o when a write-buffer timer expires it synchronizes the appropriate
  255. * write-buffer;
  256. * o when the journal is about to be full, it starts in-advance commit.
  257. *
  258. * Note, other stuff like background garbage collection may be added here in
  259. * future.
  260. */
  261. int ubifs_bg_thread(void *info)
  262. {
  263. int err;
  264. struct ubifs_info *c = info;
  265. ubifs_msg(c, "background thread \"%s\" started, PID %d",
  266. c->bgt_name, current->pid);
  267. set_freezable();
  268. while (1) {
  269. if (kthread_should_stop())
  270. break;
  271. if (try_to_freeze())
  272. continue;
  273. set_current_state(TASK_INTERRUPTIBLE);
  274. /* Check if there is something to do */
  275. if (!c->need_bgt) {
  276. /*
  277. * Nothing prevents us from going sleep now and
  278. * be never woken up and block the task which
  279. * could wait in 'kthread_stop()' forever.
  280. */
  281. if (kthread_should_stop())
  282. break;
  283. schedule();
  284. continue;
  285. } else
  286. __set_current_state(TASK_RUNNING);
  287. c->need_bgt = 0;
  288. err = ubifs_bg_wbufs_sync(c);
  289. if (err)
  290. ubifs_ro_mode(c, err);
  291. run_bg_commit(c);
  292. cond_resched();
  293. }
  294. ubifs_msg(c, "background thread \"%s\" stops", c->bgt_name);
  295. return 0;
  296. }
  297. /**
  298. * ubifs_commit_required - set commit state to "required".
  299. * @c: UBIFS file-system description object
  300. *
  301. * This function is called if a commit is required but cannot be done from the
  302. * calling function, so it is just flagged instead.
  303. */
  304. void ubifs_commit_required(struct ubifs_info *c)
  305. {
  306. spin_lock(&c->cs_lock);
  307. switch (c->cmt_state) {
  308. case COMMIT_RESTING:
  309. case COMMIT_BACKGROUND:
  310. dbg_cmt("old: %s, new: %s", dbg_cstate(c->cmt_state),
  311. dbg_cstate(COMMIT_REQUIRED));
  312. c->cmt_state = COMMIT_REQUIRED;
  313. break;
  314. case COMMIT_RUNNING_BACKGROUND:
  315. dbg_cmt("old: %s, new: %s", dbg_cstate(c->cmt_state),
  316. dbg_cstate(COMMIT_RUNNING_REQUIRED));
  317. c->cmt_state = COMMIT_RUNNING_REQUIRED;
  318. break;
  319. case COMMIT_REQUIRED:
  320. case COMMIT_RUNNING_REQUIRED:
  321. case COMMIT_BROKEN:
  322. break;
  323. }
  324. spin_unlock(&c->cs_lock);
  325. }
  326. /**
  327. * ubifs_request_bg_commit - notify the background thread to do a commit.
  328. * @c: UBIFS file-system description object
  329. *
  330. * This function is called if the journal is full enough to make a commit
  331. * worthwhile, so background thread is kicked to start it.
  332. */
  333. void ubifs_request_bg_commit(struct ubifs_info *c)
  334. {
  335. spin_lock(&c->cs_lock);
  336. if (c->cmt_state == COMMIT_RESTING) {
  337. dbg_cmt("old: %s, new: %s", dbg_cstate(c->cmt_state),
  338. dbg_cstate(COMMIT_BACKGROUND));
  339. c->cmt_state = COMMIT_BACKGROUND;
  340. spin_unlock(&c->cs_lock);
  341. ubifs_wake_up_bgt(c);
  342. } else
  343. spin_unlock(&c->cs_lock);
  344. }
  345. /**
  346. * wait_for_commit - wait for commit.
  347. * @c: UBIFS file-system description object
  348. *
  349. * This function sleeps until the commit operation is no longer running.
  350. */
  351. static int wait_for_commit(struct ubifs_info *c)
  352. {
  353. dbg_cmt("pid %d goes sleep", current->pid);
  354. /*
  355. * The following sleeps if the condition is false, and will be woken
  356. * when the commit ends. It is possible, although very unlikely, that we
  357. * will wake up and see the subsequent commit running, rather than the
  358. * one we were waiting for, and go back to sleep. However, we will be
  359. * woken again, so there is no danger of sleeping forever.
  360. */
  361. wait_event(c->cmt_wq, c->cmt_state != COMMIT_RUNNING_BACKGROUND &&
  362. c->cmt_state != COMMIT_RUNNING_REQUIRED);
  363. dbg_cmt("commit finished, pid %d woke up", current->pid);
  364. return 0;
  365. }
  366. /**
  367. * ubifs_run_commit - run or wait for commit.
  368. * @c: UBIFS file-system description object
  369. *
  370. * This function runs commit and returns zero in case of success and a negative
  371. * error code in case of failure.
  372. */
  373. int ubifs_run_commit(struct ubifs_info *c)
  374. {
  375. int err = 0;
  376. spin_lock(&c->cs_lock);
  377. if (c->cmt_state == COMMIT_BROKEN) {
  378. err = -EROFS;
  379. goto out;
  380. }
  381. if (c->cmt_state == COMMIT_RUNNING_BACKGROUND)
  382. /*
  383. * We set the commit state to 'running required' to indicate
  384. * that we want it to complete as quickly as possible.
  385. */
  386. c->cmt_state = COMMIT_RUNNING_REQUIRED;
  387. if (c->cmt_state == COMMIT_RUNNING_REQUIRED) {
  388. spin_unlock(&c->cs_lock);
  389. return wait_for_commit(c);
  390. }
  391. spin_unlock(&c->cs_lock);
  392. /* Ok, the commit is indeed needed */
  393. down_write(&c->commit_sem);
  394. spin_lock(&c->cs_lock);
  395. /*
  396. * Since we unlocked 'c->cs_lock', the state may have changed, so
  397. * re-check it.
  398. */
  399. if (c->cmt_state == COMMIT_BROKEN) {
  400. err = -EROFS;
  401. goto out_cmt_unlock;
  402. }
  403. if (c->cmt_state == COMMIT_RUNNING_BACKGROUND)
  404. c->cmt_state = COMMIT_RUNNING_REQUIRED;
  405. if (c->cmt_state == COMMIT_RUNNING_REQUIRED) {
  406. up_write(&c->commit_sem);
  407. spin_unlock(&c->cs_lock);
  408. return wait_for_commit(c);
  409. }
  410. c->cmt_state = COMMIT_RUNNING_REQUIRED;
  411. spin_unlock(&c->cs_lock);
  412. err = do_commit(c);
  413. return err;
  414. out_cmt_unlock:
  415. up_write(&c->commit_sem);
  416. out:
  417. spin_unlock(&c->cs_lock);
  418. return err;
  419. }
  420. /**
  421. * ubifs_gc_should_commit - determine if it is time for GC to run commit.
  422. * @c: UBIFS file-system description object
  423. *
  424. * This function is called by garbage collection to determine if commit should
  425. * be run. If commit state is @COMMIT_BACKGROUND, which means that the journal
  426. * is full enough to start commit, this function returns true. It is not
  427. * absolutely necessary to commit yet, but it feels like this should be better
  428. * then to keep doing GC. This function returns %1 if GC has to initiate commit
  429. * and %0 if not.
  430. */
  431. int ubifs_gc_should_commit(struct ubifs_info *c)
  432. {
  433. int ret = 0;
  434. spin_lock(&c->cs_lock);
  435. if (c->cmt_state == COMMIT_BACKGROUND) {
  436. dbg_cmt("commit required now");
  437. c->cmt_state = COMMIT_REQUIRED;
  438. } else
  439. dbg_cmt("commit not requested");
  440. if (c->cmt_state == COMMIT_REQUIRED)
  441. ret = 1;
  442. spin_unlock(&c->cs_lock);
  443. return ret;
  444. }
  445. /*
  446. * Everything below is related to debugging.
  447. */
  448. /**
  449. * struct idx_node - hold index nodes during index tree traversal.
  450. * @list: list
  451. * @iip: index in parent (slot number of this indexing node in the parent
  452. * indexing node)
  453. * @upper_key: all keys in this indexing node have to be less or equivalent to
  454. * this key
  455. * @idx: index node (8-byte aligned because all node structures must be 8-byte
  456. * aligned)
  457. */
  458. struct idx_node {
  459. struct list_head list;
  460. int iip;
  461. union ubifs_key upper_key;
  462. struct ubifs_idx_node idx __aligned(8);
  463. };
  464. /**
  465. * dbg_old_index_check_init - get information for the next old index check.
  466. * @c: UBIFS file-system description object
  467. * @zroot: root of the index
  468. *
  469. * This function records information about the index that will be needed for the
  470. * next old index check i.e. 'dbg_check_old_index()'.
  471. *
  472. * This function returns %0 on success and a negative error code on failure.
  473. */
  474. int dbg_old_index_check_init(struct ubifs_info *c, struct ubifs_zbranch *zroot)
  475. {
  476. struct ubifs_idx_node *idx;
  477. int lnum, offs, len, err = 0;
  478. struct ubifs_debug_info *d = c->dbg;
  479. d->old_zroot = *zroot;
  480. lnum = d->old_zroot.lnum;
  481. offs = d->old_zroot.offs;
  482. len = d->old_zroot.len;
  483. idx = kmalloc(c->max_idx_node_sz, GFP_NOFS);
  484. if (!idx)
  485. return -ENOMEM;
  486. err = ubifs_read_node(c, idx, UBIFS_IDX_NODE, len, lnum, offs);
  487. if (err)
  488. goto out;
  489. d->old_zroot_level = le16_to_cpu(idx->level);
  490. d->old_zroot_sqnum = le64_to_cpu(idx->ch.sqnum);
  491. out:
  492. kfree(idx);
  493. return err;
  494. }
  495. /**
  496. * dbg_check_old_index - check the old copy of the index.
  497. * @c: UBIFS file-system description object
  498. * @zroot: root of the new index
  499. *
  500. * In order to be able to recover from an unclean unmount, a complete copy of
  501. * the index must exist on flash. This is the "old" index. The commit process
  502. * must write the "new" index to flash without overwriting or destroying any
  503. * part of the old index. This function is run at commit end in order to check
  504. * that the old index does indeed exist completely intact.
  505. *
  506. * This function returns %0 on success and a negative error code on failure.
  507. */
  508. int dbg_check_old_index(struct ubifs_info *c, struct ubifs_zbranch *zroot)
  509. {
  510. int lnum, offs, len, err = 0, uninitialized_var(last_level), child_cnt;
  511. int first = 1, iip;
  512. struct ubifs_debug_info *d = c->dbg;
  513. union ubifs_key uninitialized_var(lower_key), upper_key, l_key, u_key;
  514. unsigned long long uninitialized_var(last_sqnum);
  515. struct ubifs_idx_node *idx;
  516. struct list_head list;
  517. struct idx_node *i;
  518. size_t sz;
  519. if (!dbg_is_chk_index(c))
  520. return 0;
  521. INIT_LIST_HEAD(&list);
  522. sz = sizeof(struct idx_node) + ubifs_idx_node_sz(c, c->fanout) -
  523. UBIFS_IDX_NODE_SZ;
  524. /* Start at the old zroot */
  525. lnum = d->old_zroot.lnum;
  526. offs = d->old_zroot.offs;
  527. len = d->old_zroot.len;
  528. iip = 0;
  529. /*
  530. * Traverse the index tree preorder depth-first i.e. do a node and then
  531. * its subtrees from left to right.
  532. */
  533. while (1) {
  534. struct ubifs_branch *br;
  535. /* Get the next index node */
  536. i = kmalloc(sz, GFP_NOFS);
  537. if (!i) {
  538. err = -ENOMEM;
  539. goto out_free;
  540. }
  541. i->iip = iip;
  542. /* Keep the index nodes on our path in a linked list */
  543. list_add_tail(&i->list, &list);
  544. /* Read the index node */
  545. idx = &i->idx;
  546. err = ubifs_read_node(c, idx, UBIFS_IDX_NODE, len, lnum, offs);
  547. if (err)
  548. goto out_free;
  549. /* Validate index node */
  550. child_cnt = le16_to_cpu(idx->child_cnt);
  551. if (child_cnt < 1 || child_cnt > c->fanout) {
  552. err = 1;
  553. goto out_dump;
  554. }
  555. if (first) {
  556. first = 0;
  557. /* Check root level and sqnum */
  558. if (le16_to_cpu(idx->level) != d->old_zroot_level) {
  559. err = 2;
  560. goto out_dump;
  561. }
  562. if (le64_to_cpu(idx->ch.sqnum) != d->old_zroot_sqnum) {
  563. err = 3;
  564. goto out_dump;
  565. }
  566. /* Set last values as though root had a parent */
  567. last_level = le16_to_cpu(idx->level) + 1;
  568. last_sqnum = le64_to_cpu(idx->ch.sqnum) + 1;
  569. key_read(c, ubifs_idx_key(c, idx), &lower_key);
  570. highest_ino_key(c, &upper_key, INUM_WATERMARK);
  571. }
  572. key_copy(c, &upper_key, &i->upper_key);
  573. if (le16_to_cpu(idx->level) != last_level - 1) {
  574. err = 3;
  575. goto out_dump;
  576. }
  577. /*
  578. * The index is always written bottom up hence a child's sqnum
  579. * is always less than the parents.
  580. */
  581. if (le64_to_cpu(idx->ch.sqnum) >= last_sqnum) {
  582. err = 4;
  583. goto out_dump;
  584. }
  585. /* Check key range */
  586. key_read(c, ubifs_idx_key(c, idx), &l_key);
  587. br = ubifs_idx_branch(c, idx, child_cnt - 1);
  588. key_read(c, &br->key, &u_key);
  589. if (keys_cmp(c, &lower_key, &l_key) > 0) {
  590. err = 5;
  591. goto out_dump;
  592. }
  593. if (keys_cmp(c, &upper_key, &u_key) < 0) {
  594. err = 6;
  595. goto out_dump;
  596. }
  597. if (keys_cmp(c, &upper_key, &u_key) == 0)
  598. if (!is_hash_key(c, &u_key)) {
  599. err = 7;
  600. goto out_dump;
  601. }
  602. /* Go to next index node */
  603. if (le16_to_cpu(idx->level) == 0) {
  604. /* At the bottom, so go up until can go right */
  605. while (1) {
  606. /* Drop the bottom of the list */
  607. list_del(&i->list);
  608. kfree(i);
  609. /* No more list means we are done */
  610. if (list_empty(&list))
  611. goto out;
  612. /* Look at the new bottom */
  613. i = list_entry(list.prev, struct idx_node,
  614. list);
  615. idx = &i->idx;
  616. /* Can we go right */
  617. if (iip + 1 < le16_to_cpu(idx->child_cnt)) {
  618. iip = iip + 1;
  619. break;
  620. } else
  621. /* Nope, so go up again */
  622. iip = i->iip;
  623. }
  624. } else
  625. /* Go down left */
  626. iip = 0;
  627. /*
  628. * We have the parent in 'idx' and now we set up for reading the
  629. * child pointed to by slot 'iip'.
  630. */
  631. last_level = le16_to_cpu(idx->level);
  632. last_sqnum = le64_to_cpu(idx->ch.sqnum);
  633. br = ubifs_idx_branch(c, idx, iip);
  634. lnum = le32_to_cpu(br->lnum);
  635. offs = le32_to_cpu(br->offs);
  636. len = le32_to_cpu(br->len);
  637. key_read(c, &br->key, &lower_key);
  638. if (iip + 1 < le16_to_cpu(idx->child_cnt)) {
  639. br = ubifs_idx_branch(c, idx, iip + 1);
  640. key_read(c, &br->key, &upper_key);
  641. } else
  642. key_copy(c, &i->upper_key, &upper_key);
  643. }
  644. out:
  645. err = dbg_old_index_check_init(c, zroot);
  646. if (err)
  647. goto out_free;
  648. return 0;
  649. out_dump:
  650. ubifs_err(c, "dumping index node (iip=%d)", i->iip);
  651. ubifs_dump_node(c, idx);
  652. list_del(&i->list);
  653. kfree(i);
  654. if (!list_empty(&list)) {
  655. i = list_entry(list.prev, struct idx_node, list);
  656. ubifs_err(c, "dumping parent index node");
  657. ubifs_dump_node(c, &i->idx);
  658. }
  659. out_free:
  660. while (!list_empty(&list)) {
  661. i = list_entry(list.next, struct idx_node, list);
  662. list_del(&i->list);
  663. kfree(i);
  664. }
  665. ubifs_err(c, "failed, error %d", err);
  666. if (err > 0)
  667. err = -EINVAL;
  668. return err;
  669. }