commit.c 19 KB

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