master.c 10.0 KB

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