master.c 10 KB

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