commit.c 19 KB

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