master.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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: Artem Bityutskiy (Битюцкий Артём)
  20. * Adrian Hunter
  21. */
  22. /* This file implements reading and writing the master node */
  23. #include "ubifs.h"
  24. /**
  25. * scan_for_master - search the valid master node.
  26. * @c: UBIFS file-system description object
  27. *
  28. * This function scans the master node LEBs and search for the latest master
  29. * node. Returns zero in case of success and a negative error code in case of
  30. * failure.
  31. */
  32. static int scan_for_master(struct ubifs_info *c)
  33. {
  34. struct ubifs_scan_leb *sleb;
  35. struct ubifs_scan_node *snod;
  36. int lnum, offs = 0, nodes_cnt;
  37. lnum = UBIFS_MST_LNUM;
  38. sleb = ubifs_scan(c, lnum, 0, c->sbuf);
  39. if (IS_ERR(sleb))
  40. return PTR_ERR(sleb);
  41. nodes_cnt = sleb->nodes_cnt;
  42. if (nodes_cnt > 0) {
  43. snod = list_entry(sleb->nodes.prev, struct ubifs_scan_node,
  44. list);
  45. if (snod->type != UBIFS_MST_NODE)
  46. goto out;
  47. memcpy(c->mst_node, snod->node, snod->len);
  48. offs = snod->offs;
  49. }
  50. ubifs_scan_destroy(sleb);
  51. lnum += 1;
  52. sleb = ubifs_scan(c, lnum, 0, c->sbuf);
  53. if (IS_ERR(sleb))
  54. return PTR_ERR(sleb);
  55. if (sleb->nodes_cnt != nodes_cnt)
  56. goto out;
  57. if (!sleb->nodes_cnt)
  58. goto out;
  59. snod = list_entry(sleb->nodes.prev, struct ubifs_scan_node, list);
  60. if (snod->type != UBIFS_MST_NODE)
  61. goto out;
  62. if (snod->offs != offs)
  63. goto out;
  64. if (memcmp((void *)c->mst_node + UBIFS_CH_SZ,
  65. (void *)snod->node + UBIFS_CH_SZ,
  66. UBIFS_MST_NODE_SZ - UBIFS_CH_SZ))
  67. goto out;
  68. c->mst_offs = offs;
  69. ubifs_scan_destroy(sleb);
  70. return 0;
  71. out:
  72. ubifs_scan_destroy(sleb);
  73. return -EINVAL;
  74. }
  75. /**
  76. * validate_master - validate master node.
  77. * @c: UBIFS file-system description object
  78. *
  79. * This function validates data which was read from master node. Returns zero
  80. * if the data is all right and %-EINVAL if not.
  81. */
  82. static int validate_master(const struct ubifs_info *c)
  83. {
  84. long long main_sz;
  85. int err;
  86. if (c->max_sqnum >= SQNUM_WATERMARK) {
  87. err = 1;
  88. goto out;
  89. }
  90. if (c->cmt_no >= c->max_sqnum) {
  91. err = 2;
  92. goto out;
  93. }
  94. if (c->highest_inum >= INUM_WATERMARK) {
  95. err = 3;
  96. goto out;
  97. }
  98. if (c->lhead_lnum < UBIFS_LOG_LNUM ||
  99. c->lhead_lnum >= UBIFS_LOG_LNUM + c->log_lebs ||
  100. c->lhead_offs < 0 || c->lhead_offs >= c->leb_size ||
  101. c->lhead_offs & (c->min_io_size - 1)) {
  102. err = 4;
  103. goto out;
  104. }
  105. if (c->zroot.lnum >= c->leb_cnt || c->zroot.lnum < c->main_first ||
  106. c->zroot.offs >= c->leb_size || c->zroot.offs & 7) {
  107. err = 5;
  108. goto out;
  109. }
  110. if (c->zroot.len < c->ranges[UBIFS_IDX_NODE].min_len ||
  111. c->zroot.len > c->ranges[UBIFS_IDX_NODE].max_len) {
  112. err = 6;
  113. goto out;
  114. }
  115. if (c->gc_lnum >= c->leb_cnt || c->gc_lnum < c->main_first) {
  116. err = 7;
  117. goto out;
  118. }
  119. if (c->ihead_lnum >= c->leb_cnt || c->ihead_lnum < c->main_first ||
  120. c->ihead_offs % c->min_io_size || c->ihead_offs < 0 ||
  121. c->ihead_offs > c->leb_size || c->ihead_offs & 7) {
  122. err = 8;
  123. goto out;
  124. }
  125. main_sz = (long long)c->main_lebs * c->leb_size;
  126. if (c->old_idx_sz & 7 || c->old_idx_sz >= main_sz) {
  127. err = 9;
  128. goto out;
  129. }
  130. if (c->lpt_lnum < c->lpt_first || c->lpt_lnum > c->lpt_last ||
  131. c->lpt_offs < 0 || c->lpt_offs + c->nnode_sz > c->leb_size) {
  132. err = 10;
  133. goto out;
  134. }
  135. if (c->nhead_lnum < c->lpt_first || c->nhead_lnum > c->lpt_last ||
  136. c->nhead_offs < 0 || c->nhead_offs % c->min_io_size ||
  137. c->nhead_offs > c->leb_size) {
  138. err = 11;
  139. goto out;
  140. }
  141. if (c->ltab_lnum < c->lpt_first || c->ltab_lnum > c->lpt_last ||
  142. c->ltab_offs < 0 ||
  143. c->ltab_offs + c->ltab_sz > c->leb_size) {
  144. err = 12;
  145. goto out;
  146. }
  147. if (c->big_lpt && (c->lsave_lnum < c->lpt_first ||
  148. c->lsave_lnum > c->lpt_last || c->lsave_offs < 0 ||
  149. c->lsave_offs + c->lsave_sz > c->leb_size)) {
  150. err = 13;
  151. goto out;
  152. }
  153. if (c->lscan_lnum < c->main_first || c->lscan_lnum >= c->leb_cnt) {
  154. err = 14;
  155. goto out;
  156. }
  157. if (c->lst.empty_lebs < 0 || c->lst.empty_lebs > c->main_lebs - 2) {
  158. err = 15;
  159. goto out;
  160. }
  161. if (c->lst.idx_lebs < 0 || c->lst.idx_lebs > c->main_lebs - 1) {
  162. err = 16;
  163. goto out;
  164. }
  165. if (c->lst.total_free < 0 || c->lst.total_free > main_sz ||
  166. c->lst.total_free & 7) {
  167. err = 17;
  168. goto out;
  169. }
  170. if (c->lst.total_dirty < 0 || (c->lst.total_dirty & 7)) {
  171. err = 18;
  172. goto out;
  173. }
  174. if (c->lst.total_used < 0 || (c->lst.total_used & 7)) {
  175. err = 19;
  176. goto out;
  177. }
  178. if (c->lst.total_free + c->lst.total_dirty +
  179. c->lst.total_used > main_sz) {
  180. err = 20;
  181. goto out;
  182. }
  183. if (c->lst.total_dead + c->lst.total_dark +
  184. c->lst.total_used + c->old_idx_sz > main_sz) {
  185. err = 21;
  186. goto out;
  187. }
  188. if (c->lst.total_dead < 0 ||
  189. c->lst.total_dead > c->lst.total_free + c->lst.total_dirty ||
  190. c->lst.total_dead & 7) {
  191. err = 22;
  192. goto out;
  193. }
  194. if (c->lst.total_dark < 0 ||
  195. c->lst.total_dark > c->lst.total_free + c->lst.total_dirty ||
  196. c->lst.total_dark & 7) {
  197. err = 23;
  198. goto out;
  199. }
  200. return 0;
  201. out:
  202. ubifs_err("bad master node at offset %d error %d", c->mst_offs, err);
  203. dbg_dump_node(c, c->mst_node);
  204. return -EINVAL;
  205. }
  206. /**
  207. * ubifs_read_master - read master node.
  208. * @c: UBIFS file-system description object
  209. *
  210. * This function finds and reads the master node during file-system mount. If
  211. * the flash is empty, it creates default master node as well. Returns zero in
  212. * case of success and a negative error code in case of failure.
  213. */
  214. int ubifs_read_master(struct ubifs_info *c)
  215. {
  216. int err, old_leb_cnt;
  217. c->mst_node = kzalloc(c->mst_node_alsz, GFP_KERNEL);
  218. if (!c->mst_node)
  219. return -ENOMEM;
  220. err = scan_for_master(c);
  221. if (err) {
  222. err = ubifs_recover_master_node(c);
  223. if (err)
  224. /*
  225. * Note, we do not free 'c->mst_node' here because the
  226. * unmount routine will take care of this.
  227. */
  228. return err;
  229. }
  230. /* Make sure that the recovery flag is clear */
  231. c->mst_node->flags &= cpu_to_le32(~UBIFS_MST_RCVRY);
  232. c->max_sqnum = le64_to_cpu(c->mst_node->ch.sqnum);
  233. c->highest_inum = le64_to_cpu(c->mst_node->highest_inum);
  234. c->cmt_no = le64_to_cpu(c->mst_node->cmt_no);
  235. c->zroot.lnum = le32_to_cpu(c->mst_node->root_lnum);
  236. c->zroot.offs = le32_to_cpu(c->mst_node->root_offs);
  237. c->zroot.len = le32_to_cpu(c->mst_node->root_len);
  238. c->lhead_lnum = le32_to_cpu(c->mst_node->log_lnum);
  239. c->gc_lnum = le32_to_cpu(c->mst_node->gc_lnum);
  240. c->ihead_lnum = le32_to_cpu(c->mst_node->ihead_lnum);
  241. c->ihead_offs = le32_to_cpu(c->mst_node->ihead_offs);
  242. c->old_idx_sz = le64_to_cpu(c->mst_node->index_size);
  243. c->lpt_lnum = le32_to_cpu(c->mst_node->lpt_lnum);
  244. c->lpt_offs = le32_to_cpu(c->mst_node->lpt_offs);
  245. c->nhead_lnum = le32_to_cpu(c->mst_node->nhead_lnum);
  246. c->nhead_offs = le32_to_cpu(c->mst_node->nhead_offs);
  247. c->ltab_lnum = le32_to_cpu(c->mst_node->ltab_lnum);
  248. c->ltab_offs = le32_to_cpu(c->mst_node->ltab_offs);
  249. c->lsave_lnum = le32_to_cpu(c->mst_node->lsave_lnum);
  250. c->lsave_offs = le32_to_cpu(c->mst_node->lsave_offs);
  251. c->lscan_lnum = le32_to_cpu(c->mst_node->lscan_lnum);
  252. c->lst.empty_lebs = le32_to_cpu(c->mst_node->empty_lebs);
  253. c->lst.idx_lebs = le32_to_cpu(c->mst_node->idx_lebs);
  254. old_leb_cnt = le32_to_cpu(c->mst_node->leb_cnt);
  255. c->lst.total_free = le64_to_cpu(c->mst_node->total_free);
  256. c->lst.total_dirty = le64_to_cpu(c->mst_node->total_dirty);
  257. c->lst.total_used = le64_to_cpu(c->mst_node->total_used);
  258. c->lst.total_dead = le64_to_cpu(c->mst_node->total_dead);
  259. c->lst.total_dark = le64_to_cpu(c->mst_node->total_dark);
  260. c->calc_idx_sz = c->old_idx_sz;
  261. if (c->mst_node->flags & cpu_to_le32(UBIFS_MST_NO_ORPHS))
  262. c->no_orphs = 1;
  263. if (old_leb_cnt != c->leb_cnt) {
  264. /* The file system has been resized */
  265. int growth = c->leb_cnt - old_leb_cnt;
  266. if (c->leb_cnt < old_leb_cnt ||
  267. c->leb_cnt < UBIFS_MIN_LEB_CNT) {
  268. ubifs_err("bad leb_cnt on master node");
  269. dbg_dump_node(c, c->mst_node);
  270. return -EINVAL;
  271. }
  272. dbg_mnt("Auto resizing (master) from %d LEBs to %d LEBs",
  273. old_leb_cnt, c->leb_cnt);
  274. c->lst.empty_lebs += growth;
  275. c->lst.total_free += growth * (long long)c->leb_size;
  276. c->lst.total_dark += growth * (long long)c->dark_wm;
  277. /*
  278. * Reflect changes back onto the master node. N.B. the master
  279. * node gets written immediately whenever mounting (or
  280. * remounting) in read-write mode, so we do not need to write it
  281. * here.
  282. */
  283. c->mst_node->leb_cnt = cpu_to_le32(c->leb_cnt);
  284. c->mst_node->empty_lebs = cpu_to_le32(c->lst.empty_lebs);
  285. c->mst_node->total_free = cpu_to_le64(c->lst.total_free);
  286. c->mst_node->total_dark = cpu_to_le64(c->lst.total_dark);
  287. }
  288. err = validate_master(c);
  289. if (err)
  290. return err;
  291. err = dbg_old_index_check_init(c, &c->zroot);
  292. return err;
  293. }