commit.c 19 KB

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