master.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * This file is part of UBIFS.
  4. *
  5. * Copyright (C) 2006-2008 Nokia Corporation.
  6. *
  7. * Authors: Artem Bityutskiy (Битюцкий Артём)
  8. * Adrian Hunter
  9. */
  10. /* This file implements reading and writing the master node */
  11. #include "ubifs.h"
  12. #ifdef __UBOOT__
  13. #include <linux/compat.h>
  14. #include <linux/err.h>
  15. #include <ubi_uboot.h>
  16. #endif
  17. /**
  18. * scan_for_master - search the valid master node.
  19. * @c: UBIFS file-system description object
  20. *
  21. * This function scans the master node LEBs and search for the latest master
  22. * node. Returns zero in case of success, %-EUCLEAN if there master area is
  23. * corrupted and requires recovery, and a negative error code in case of
  24. * failure.
  25. */
  26. static int scan_for_master(struct ubifs_info *c)
  27. {
  28. struct ubifs_scan_leb *sleb;
  29. struct ubifs_scan_node *snod;
  30. int lnum, offs = 0, nodes_cnt;
  31. lnum = UBIFS_MST_LNUM;
  32. sleb = ubifs_scan(c, lnum, 0, c->sbuf, 1);
  33. if (IS_ERR(sleb))
  34. return PTR_ERR(sleb);
  35. nodes_cnt = sleb->nodes_cnt;
  36. if (nodes_cnt > 0) {
  37. snod = list_entry(sleb->nodes.prev, struct ubifs_scan_node,
  38. list);
  39. if (snod->type != UBIFS_MST_NODE)
  40. goto out_dump;
  41. memcpy(c->mst_node, snod->node, snod->len);
  42. offs = snod->offs;
  43. }
  44. ubifs_scan_destroy(sleb);
  45. lnum += 1;
  46. sleb = ubifs_scan(c, lnum, 0, c->sbuf, 1);
  47. if (IS_ERR(sleb))
  48. return PTR_ERR(sleb);
  49. if (sleb->nodes_cnt != nodes_cnt)
  50. goto out;
  51. if (!sleb->nodes_cnt)
  52. goto out;
  53. snod = list_entry(sleb->nodes.prev, struct ubifs_scan_node, list);
  54. if (snod->type != UBIFS_MST_NODE)
  55. goto out_dump;
  56. if (snod->offs != offs)
  57. goto out;
  58. if (memcmp((void *)c->mst_node + UBIFS_CH_SZ,
  59. (void *)snod->node + UBIFS_CH_SZ,
  60. UBIFS_MST_NODE_SZ - UBIFS_CH_SZ))
  61. goto out;
  62. c->mst_offs = offs;
  63. ubifs_scan_destroy(sleb);
  64. return 0;
  65. out:
  66. ubifs_scan_destroy(sleb);
  67. return -EUCLEAN;
  68. out_dump:
  69. ubifs_err(c, "unexpected node type %d master LEB %d:%d",
  70. snod->type, lnum, snod->offs);
  71. ubifs_scan_destroy(sleb);
  72. return -EINVAL;
  73. }
  74. /**
  75. * validate_master - validate master node.
  76. * @c: UBIFS file-system description object
  77. *
  78. * This function validates data which was read from master node. Returns zero
  79. * if the data is all right and %-EINVAL if not.
  80. */
  81. static int validate_master(const struct ubifs_info *c)
  82. {
  83. long long main_sz;
  84. int err;
  85. if (c->max_sqnum >= SQNUM_WATERMARK) {
  86. err = 1;
  87. goto out;
  88. }
  89. if (c->cmt_no >= c->max_sqnum) {
  90. err = 2;
  91. goto out;
  92. }
  93. if (c->highest_inum >= INUM_WATERMARK) {
  94. err = 3;
  95. goto out;
  96. }
  97. if (c->lhead_lnum < UBIFS_LOG_LNUM ||
  98. c->lhead_lnum >= UBIFS_LOG_LNUM + c->log_lebs ||
  99. c->lhead_offs < 0 || c->lhead_offs >= c->leb_size ||
  100. c->lhead_offs & (c->min_io_size - 1)) {
  101. err = 4;
  102. goto out;
  103. }
  104. if (c->zroot.lnum >= c->leb_cnt || c->zroot.lnum < c->main_first ||
  105. c->zroot.offs >= c->leb_size || c->zroot.offs & 7) {
  106. err = 5;
  107. goto out;
  108. }
  109. if (c->zroot.len < c->ranges[UBIFS_IDX_NODE].min_len ||
  110. c->zroot.len > c->ranges[UBIFS_IDX_NODE].max_len) {
  111. err = 6;
  112. goto out;
  113. }
  114. if (c->gc_lnum >= c->leb_cnt || c->gc_lnum < c->main_first) {
  115. err = 7;
  116. goto out;
  117. }
  118. if (c->ihead_lnum >= c->leb_cnt || c->ihead_lnum < c->main_first ||
  119. c->ihead_offs % c->min_io_size || c->ihead_offs < 0 ||
  120. c->ihead_offs > c->leb_size || c->ihead_offs & 7) {
  121. err = 8;
  122. goto out;
  123. }
  124. main_sz = (long long)c->main_lebs * c->leb_size;
  125. if (c->bi.old_idx_sz & 7 || c->bi.old_idx_sz >= main_sz) {
  126. err = 9;
  127. goto out;
  128. }
  129. if (c->lpt_lnum < c->lpt_first || c->lpt_lnum > c->lpt_last ||
  130. c->lpt_offs < 0 || c->lpt_offs + c->nnode_sz > c->leb_size) {
  131. err = 10;
  132. goto out;
  133. }
  134. if (c->nhead_lnum < c->lpt_first || c->nhead_lnum > c->lpt_last ||
  135. c->nhead_offs < 0 || c->nhead_offs % c->min_io_size ||
  136. c->nhead_offs > c->leb_size) {
  137. err = 11;
  138. goto out;
  139. }
  140. if (c->ltab_lnum < c->lpt_first || c->ltab_lnum > c->lpt_last ||
  141. c->ltab_offs < 0 ||
  142. c->ltab_offs + c->ltab_sz > c->leb_size) {
  143. err = 12;
  144. goto out;
  145. }
  146. if (c->big_lpt && (c->lsave_lnum < c->lpt_first ||
  147. c->lsave_lnum > c->lpt_last || c->lsave_offs < 0 ||
  148. c->lsave_offs + c->lsave_sz > c->leb_size)) {
  149. err = 13;
  150. goto out;
  151. }
  152. if (c->lscan_lnum < c->main_first || c->lscan_lnum >= c->leb_cnt) {
  153. err = 14;
  154. goto out;
  155. }
  156. if (c->lst.empty_lebs < 0 || c->lst.empty_lebs > c->main_lebs - 2) {
  157. err = 15;
  158. goto out;
  159. }
  160. if (c->lst.idx_lebs < 0 || c->lst.idx_lebs > c->main_lebs - 1) {
  161. err = 16;
  162. goto out;
  163. }
  164. if (c->lst.total_free < 0 || c->lst.total_free > main_sz ||
  165. c->lst.total_free & 7) {
  166. err = 17;
  167. goto out;
  168. }
  169. if (c->lst.total_dirty < 0 || (c->lst.total_dirty & 7)) {
  170. err = 18;
  171. goto out;
  172. }
  173. if (c->lst.total_used < 0 || (c->lst.total_used & 7)) {
  174. err = 19;
  175. goto out;
  176. }
  177. if (c->lst.total_free + c->lst.total_dirty +
  178. c->lst.total_used > main_sz) {
  179. err = 20;
  180. goto out;
  181. }
  182. if (c->lst.total_dead + c->lst.total_dark +
  183. c->lst.total_used + c->bi.old_idx_sz > main_sz) {
  184. err = 21;
  185. goto out;
  186. }
  187. if (c->lst.total_dead < 0 ||
  188. c->lst.total_dead > c->lst.total_free + c->lst.total_dirty ||
  189. c->lst.total_dead & 7) {
  190. err = 22;
  191. goto out;
  192. }
  193. if (c->lst.total_dark < 0 ||
  194. c->lst.total_dark > c->lst.total_free + c->lst.total_dirty ||
  195. c->lst.total_dark & 7) {
  196. err = 23;
  197. goto out;
  198. }
  199. return 0;
  200. out:
  201. ubifs_err(c, "bad master node at offset %d error %d", c->mst_offs, err);
  202. ubifs_dump_node(c, c->mst_node);
  203. return -EINVAL;
  204. }
  205. /**
  206. * ubifs_read_master - read master node.
  207. * @c: UBIFS file-system description object
  208. *
  209. * This function finds and reads the master node during file-system mount. If
  210. * the flash is empty, it creates default master node as well. Returns zero in
  211. * case of success and a negative error code in case of failure.
  212. */
  213. int ubifs_read_master(struct ubifs_info *c)
  214. {
  215. int err, old_leb_cnt;
  216. c->mst_node = kzalloc(c->mst_node_alsz, GFP_KERNEL);
  217. if (!c->mst_node)
  218. return -ENOMEM;
  219. err = scan_for_master(c);
  220. if (err) {
  221. if (err == -EUCLEAN)
  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->bi.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->bi.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(c, "bad leb_cnt on master node");
  269. ubifs_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. #ifndef __UBOOT__
  292. err = dbg_old_index_check_init(c, &c->zroot);
  293. #endif
  294. return err;
  295. }
  296. #ifndef __UBOOT__
  297. /**
  298. * ubifs_write_master - write master node.
  299. * @c: UBIFS file-system description object
  300. *
  301. * This function writes the master node. Returns zero in case of success and a
  302. * negative error code in case of failure. The master node is written twice to
  303. * enable recovery.
  304. */
  305. int ubifs_write_master(struct ubifs_info *c)
  306. {
  307. int err, lnum, offs, len;
  308. ubifs_assert(!c->ro_media && !c->ro_mount);
  309. if (c->ro_error)
  310. return -EROFS;
  311. lnum = UBIFS_MST_LNUM;
  312. offs = c->mst_offs + c->mst_node_alsz;
  313. len = UBIFS_MST_NODE_SZ;
  314. if (offs + UBIFS_MST_NODE_SZ > c->leb_size) {
  315. err = ubifs_leb_unmap(c, lnum);
  316. if (err)
  317. return err;
  318. offs = 0;
  319. }
  320. c->mst_offs = offs;
  321. c->mst_node->highest_inum = cpu_to_le64(c->highest_inum);
  322. err = ubifs_write_node(c, c->mst_node, len, lnum, offs);
  323. if (err)
  324. return err;
  325. lnum += 1;
  326. if (offs == 0) {
  327. err = ubifs_leb_unmap(c, lnum);
  328. if (err)
  329. return err;
  330. }
  331. err = ubifs_write_node(c, c->mst_node, len, lnum, offs);
  332. return err;
  333. }
  334. #endif