master.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /*
  2. * This file is part of UBIFS.
  3. *
  4. * Copyright (C) 2006-2008 Nokia Corporation.
  5. *
  6. * SPDX-License-Identifier: GPL-2.0+
  7. *
  8. * Authors: Artem Bityutskiy (Битюцкий Артём)
  9. * Adrian Hunter
  10. */
  11. /* This file implements reading and writing the master node */
  12. #include "ubifs.h"
  13. #ifdef __UBOOT__
  14. #include <linux/compat.h>
  15. #include <linux/err.h>
  16. #include <ubi_uboot.h>
  17. #endif
  18. /**
  19. * scan_for_master - search the valid master node.
  20. * @c: UBIFS file-system description object
  21. *
  22. * This function scans the master node LEBs and search for the latest master
  23. * node. Returns zero in case of success, %-EUCLEAN if there master area is
  24. * corrupted and requires recovery, and a negative error code in case of
  25. * failure.
  26. */
  27. static int scan_for_master(struct ubifs_info *c)
  28. {
  29. struct ubifs_scan_leb *sleb;
  30. struct ubifs_scan_node *snod;
  31. int lnum, offs = 0, nodes_cnt;
  32. lnum = UBIFS_MST_LNUM;
  33. sleb = ubifs_scan(c, lnum, 0, c->sbuf, 1);
  34. if (IS_ERR(sleb))
  35. return PTR_ERR(sleb);
  36. nodes_cnt = sleb->nodes_cnt;
  37. if (nodes_cnt > 0) {
  38. snod = list_entry(sleb->nodes.prev, struct ubifs_scan_node,
  39. list);
  40. if (snod->type != UBIFS_MST_NODE)
  41. goto out_dump;
  42. memcpy(c->mst_node, snod->node, snod->len);
  43. offs = snod->offs;
  44. }
  45. ubifs_scan_destroy(sleb);
  46. lnum += 1;
  47. sleb = ubifs_scan(c, lnum, 0, c->sbuf, 1);
  48. if (IS_ERR(sleb))
  49. return PTR_ERR(sleb);
  50. if (sleb->nodes_cnt != nodes_cnt)
  51. goto out;
  52. if (!sleb->nodes_cnt)
  53. goto out;
  54. snod = list_entry(sleb->nodes.prev, struct ubifs_scan_node, list);
  55. if (snod->type != UBIFS_MST_NODE)
  56. goto out_dump;
  57. if (snod->offs != offs)
  58. goto out;
  59. if (memcmp((void *)c->mst_node + UBIFS_CH_SZ,
  60. (void *)snod->node + UBIFS_CH_SZ,
  61. UBIFS_MST_NODE_SZ - UBIFS_CH_SZ))
  62. goto out;
  63. c->mst_offs = offs;
  64. ubifs_scan_destroy(sleb);
  65. return 0;
  66. out:
  67. ubifs_scan_destroy(sleb);
  68. return -EUCLEAN;
  69. out_dump:
  70. ubifs_err("unexpected node type %d master LEB %d:%d",
  71. snod->type, lnum, snod->offs);
  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->bi.old_idx_sz & 7 || c->bi.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->bi.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. ubifs_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. if (err == -EUCLEAN)
  223. err = ubifs_recover_master_node(c);
  224. if (err)
  225. /*
  226. * Note, we do not free 'c->mst_node' here because the
  227. * unmount routine will take care of this.
  228. */
  229. return err;
  230. }
  231. /* Make sure that the recovery flag is clear */
  232. c->mst_node->flags &= cpu_to_le32(~UBIFS_MST_RCVRY);
  233. c->max_sqnum = le64_to_cpu(c->mst_node->ch.sqnum);
  234. c->highest_inum = le64_to_cpu(c->mst_node->highest_inum);
  235. c->cmt_no = le64_to_cpu(c->mst_node->cmt_no);
  236. c->zroot.lnum = le32_to_cpu(c->mst_node->root_lnum);
  237. c->zroot.offs = le32_to_cpu(c->mst_node->root_offs);
  238. c->zroot.len = le32_to_cpu(c->mst_node->root_len);
  239. c->lhead_lnum = le32_to_cpu(c->mst_node->log_lnum);
  240. c->gc_lnum = le32_to_cpu(c->mst_node->gc_lnum);
  241. c->ihead_lnum = le32_to_cpu(c->mst_node->ihead_lnum);
  242. c->ihead_offs = le32_to_cpu(c->mst_node->ihead_offs);
  243. c->bi.old_idx_sz = le64_to_cpu(c->mst_node->index_size);
  244. c->lpt_lnum = le32_to_cpu(c->mst_node->lpt_lnum);
  245. c->lpt_offs = le32_to_cpu(c->mst_node->lpt_offs);
  246. c->nhead_lnum = le32_to_cpu(c->mst_node->nhead_lnum);
  247. c->nhead_offs = le32_to_cpu(c->mst_node->nhead_offs);
  248. c->ltab_lnum = le32_to_cpu(c->mst_node->ltab_lnum);
  249. c->ltab_offs = le32_to_cpu(c->mst_node->ltab_offs);
  250. c->lsave_lnum = le32_to_cpu(c->mst_node->lsave_lnum);
  251. c->lsave_offs = le32_to_cpu(c->mst_node->lsave_offs);
  252. c->lscan_lnum = le32_to_cpu(c->mst_node->lscan_lnum);
  253. c->lst.empty_lebs = le32_to_cpu(c->mst_node->empty_lebs);
  254. c->lst.idx_lebs = le32_to_cpu(c->mst_node->idx_lebs);
  255. old_leb_cnt = le32_to_cpu(c->mst_node->leb_cnt);
  256. c->lst.total_free = le64_to_cpu(c->mst_node->total_free);
  257. c->lst.total_dirty = le64_to_cpu(c->mst_node->total_dirty);
  258. c->lst.total_used = le64_to_cpu(c->mst_node->total_used);
  259. c->lst.total_dead = le64_to_cpu(c->mst_node->total_dead);
  260. c->lst.total_dark = le64_to_cpu(c->mst_node->total_dark);
  261. c->calc_idx_sz = c->bi.old_idx_sz;
  262. if (c->mst_node->flags & cpu_to_le32(UBIFS_MST_NO_ORPHS))
  263. c->no_orphs = 1;
  264. if (old_leb_cnt != c->leb_cnt) {
  265. /* The file system has been resized */
  266. int growth = c->leb_cnt - old_leb_cnt;
  267. if (c->leb_cnt < old_leb_cnt ||
  268. c->leb_cnt < UBIFS_MIN_LEB_CNT) {
  269. ubifs_err("bad leb_cnt on master node");
  270. ubifs_dump_node(c, c->mst_node);
  271. return -EINVAL;
  272. }
  273. dbg_mnt("Auto resizing (master) from %d LEBs to %d LEBs",
  274. old_leb_cnt, c->leb_cnt);
  275. c->lst.empty_lebs += growth;
  276. c->lst.total_free += growth * (long long)c->leb_size;
  277. c->lst.total_dark += growth * (long long)c->dark_wm;
  278. /*
  279. * Reflect changes back onto the master node. N.B. the master
  280. * node gets written immediately whenever mounting (or
  281. * remounting) in read-write mode, so we do not need to write it
  282. * here.
  283. */
  284. c->mst_node->leb_cnt = cpu_to_le32(c->leb_cnt);
  285. c->mst_node->empty_lebs = cpu_to_le32(c->lst.empty_lebs);
  286. c->mst_node->total_free = cpu_to_le64(c->lst.total_free);
  287. c->mst_node->total_dark = cpu_to_le64(c->lst.total_dark);
  288. }
  289. err = validate_master(c);
  290. if (err)
  291. return err;
  292. #ifndef __UBOOT__
  293. err = dbg_old_index_check_init(c, &c->zroot);
  294. #endif
  295. return err;
  296. }
  297. #ifndef __UBOOT__
  298. /**
  299. * ubifs_write_master - write master node.
  300. * @c: UBIFS file-system description object
  301. *
  302. * This function writes the master node. The caller has to take the
  303. * @c->mst_mutex lock before calling this function. Returns zero in case of
  304. * success and a negative error code in case of failure. The master node is
  305. * written twice to enable recovery.
  306. */
  307. int ubifs_write_master(struct ubifs_info *c)
  308. {
  309. int err, lnum, offs, len;
  310. ubifs_assert(!c->ro_media && !c->ro_mount);
  311. if (c->ro_error)
  312. return -EROFS;
  313. lnum = UBIFS_MST_LNUM;
  314. offs = c->mst_offs + c->mst_node_alsz;
  315. len = UBIFS_MST_NODE_SZ;
  316. if (offs + UBIFS_MST_NODE_SZ > c->leb_size) {
  317. err = ubifs_leb_unmap(c, lnum);
  318. if (err)
  319. return err;
  320. offs = 0;
  321. }
  322. c->mst_offs = offs;
  323. c->mst_node->highest_inum = cpu_to_le64(c->highest_inum);
  324. err = ubifs_write_node(c, c->mst_node, len, lnum, offs);
  325. if (err)
  326. return err;
  327. lnum += 1;
  328. if (offs == 0) {
  329. err = ubifs_leb_unmap(c, lnum);
  330. if (err)
  331. return err;
  332. }
  333. err = ubifs_write_node(c, c->mst_node, len, lnum, offs);
  334. return err;
  335. }
  336. #endif