commit.c 19 KB

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