sb.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. /*
  23. * This file implements UBIFS superblock. The superblock is stored at the first
  24. * LEB of the volume and is never changed by UBIFS. Only user-space tools may
  25. * change it. The superblock node mostly contains geometry information.
  26. */
  27. #include "ubifs.h"
  28. /*
  29. * Default journal size in logical eraseblocks as a percent of total
  30. * flash size.
  31. */
  32. #define DEFAULT_JNL_PERCENT 5
  33. /* Default maximum journal size in bytes */
  34. #define DEFAULT_MAX_JNL (32*1024*1024)
  35. /* Default indexing tree fanout */
  36. #define DEFAULT_FANOUT 8
  37. /* Default number of data journal heads */
  38. #define DEFAULT_JHEADS_CNT 1
  39. /* Default positions of different LEBs in the main area */
  40. #define DEFAULT_IDX_LEB 0
  41. #define DEFAULT_DATA_LEB 1
  42. #define DEFAULT_GC_LEB 2
  43. /* Default number of LEB numbers in LPT's save table */
  44. #define DEFAULT_LSAVE_CNT 256
  45. /* Default reserved pool size as a percent of maximum free space */
  46. #define DEFAULT_RP_PERCENT 5
  47. /* The default maximum size of reserved pool in bytes */
  48. #define DEFAULT_MAX_RP_SIZE (5*1024*1024)
  49. /* Default time granularity in nanoseconds */
  50. #define DEFAULT_TIME_GRAN 1000000000
  51. /**
  52. * validate_sb - validate superblock node.
  53. * @c: UBIFS file-system description object
  54. * @sup: superblock node
  55. *
  56. * This function validates superblock node @sup. Since most of data was read
  57. * from the superblock and stored in @c, the function validates fields in @c
  58. * instead. Returns zero in case of success and %-EINVAL in case of validation
  59. * failure.
  60. */
  61. static int validate_sb(struct ubifs_info *c, struct ubifs_sb_node *sup)
  62. {
  63. long long max_bytes;
  64. int err = 1, min_leb_cnt;
  65. if (!c->key_hash) {
  66. err = 2;
  67. goto failed;
  68. }
  69. if (sup->key_fmt != UBIFS_SIMPLE_KEY_FMT) {
  70. err = 3;
  71. goto failed;
  72. }
  73. if (le32_to_cpu(sup->min_io_size) != c->min_io_size) {
  74. ubifs_err("min. I/O unit mismatch: %d in superblock, %d real",
  75. le32_to_cpu(sup->min_io_size), c->min_io_size);
  76. goto failed;
  77. }
  78. if (le32_to_cpu(sup->leb_size) != c->leb_size) {
  79. ubifs_err("LEB size mismatch: %d in superblock, %d real",
  80. le32_to_cpu(sup->leb_size), c->leb_size);
  81. goto failed;
  82. }
  83. if (c->log_lebs < UBIFS_MIN_LOG_LEBS ||
  84. c->lpt_lebs < UBIFS_MIN_LPT_LEBS ||
  85. c->orph_lebs < UBIFS_MIN_ORPH_LEBS ||
  86. c->main_lebs < UBIFS_MIN_MAIN_LEBS) {
  87. err = 4;
  88. goto failed;
  89. }
  90. /*
  91. * Calculate minimum allowed amount of main area LEBs. This is very
  92. * similar to %UBIFS_MIN_LEB_CNT, but we take into account real what we
  93. * have just read from the superblock.
  94. */
  95. min_leb_cnt = UBIFS_SB_LEBS + UBIFS_MST_LEBS + c->log_lebs;
  96. min_leb_cnt += c->lpt_lebs + c->orph_lebs + c->jhead_cnt + 6;
  97. if (c->leb_cnt < min_leb_cnt || c->leb_cnt > c->vi.size) {
  98. ubifs_err("bad LEB count: %d in superblock, %d on UBI volume, "
  99. "%d minimum required", c->leb_cnt, c->vi.size,
  100. min_leb_cnt);
  101. goto failed;
  102. }
  103. if (c->max_leb_cnt < c->leb_cnt) {
  104. ubifs_err("max. LEB count %d less than LEB count %d",
  105. c->max_leb_cnt, c->leb_cnt);
  106. goto failed;
  107. }
  108. if (c->main_lebs < UBIFS_MIN_MAIN_LEBS) {
  109. err = 7;
  110. goto failed;
  111. }
  112. if (c->max_bud_bytes < (long long)c->leb_size * UBIFS_MIN_BUD_LEBS ||
  113. c->max_bud_bytes > (long long)c->leb_size * c->main_lebs) {
  114. err = 8;
  115. goto failed;
  116. }
  117. if (c->jhead_cnt < NONDATA_JHEADS_CNT + 1 ||
  118. c->jhead_cnt > NONDATA_JHEADS_CNT + UBIFS_MAX_JHEADS) {
  119. err = 9;
  120. goto failed;
  121. }
  122. if (c->fanout < UBIFS_MIN_FANOUT ||
  123. ubifs_idx_node_sz(c, c->fanout) > c->leb_size) {
  124. err = 10;
  125. goto failed;
  126. }
  127. if (c->lsave_cnt < 0 || (c->lsave_cnt > DEFAULT_LSAVE_CNT &&
  128. c->lsave_cnt > c->max_leb_cnt - UBIFS_SB_LEBS - UBIFS_MST_LEBS -
  129. c->log_lebs - c->lpt_lebs - c->orph_lebs)) {
  130. err = 11;
  131. goto failed;
  132. }
  133. if (UBIFS_SB_LEBS + UBIFS_MST_LEBS + c->log_lebs + c->lpt_lebs +
  134. c->orph_lebs + c->main_lebs != c->leb_cnt) {
  135. err = 12;
  136. goto failed;
  137. }
  138. if (c->default_compr < 0 || c->default_compr >= UBIFS_COMPR_TYPES_CNT) {
  139. err = 13;
  140. goto failed;
  141. }
  142. max_bytes = c->main_lebs * (long long)c->leb_size;
  143. if (c->rp_size < 0 || max_bytes < c->rp_size) {
  144. err = 14;
  145. goto failed;
  146. }
  147. if (le32_to_cpu(sup->time_gran) > 1000000000 ||
  148. le32_to_cpu(sup->time_gran) < 1) {
  149. err = 15;
  150. goto failed;
  151. }
  152. return 0;
  153. failed:
  154. ubifs_err("bad superblock, error %d", err);
  155. dbg_dump_node(c, sup);
  156. return -EINVAL;
  157. }
  158. /**
  159. * ubifs_read_sb_node - read superblock node.
  160. * @c: UBIFS file-system description object
  161. *
  162. * This function returns a pointer to the superblock node or a negative error
  163. * code.
  164. */
  165. struct ubifs_sb_node *ubifs_read_sb_node(struct ubifs_info *c)
  166. {
  167. struct ubifs_sb_node *sup;
  168. int err;
  169. sup = kmalloc(ALIGN(UBIFS_SB_NODE_SZ, c->min_io_size), GFP_NOFS);
  170. if (!sup)
  171. return ERR_PTR(-ENOMEM);
  172. err = ubifs_read_node(c, sup, UBIFS_SB_NODE, UBIFS_SB_NODE_SZ,
  173. UBIFS_SB_LNUM, 0);
  174. if (err) {
  175. kfree(sup);
  176. return ERR_PTR(err);
  177. }
  178. return sup;
  179. }
  180. /**
  181. * ubifs_read_superblock - read superblock.
  182. * @c: UBIFS file-system description object
  183. *
  184. * This function finds, reads and checks the superblock. If an empty UBI volume
  185. * is being mounted, this function creates default superblock. Returns zero in
  186. * case of success, and a negative error code in case of failure.
  187. */
  188. int ubifs_read_superblock(struct ubifs_info *c)
  189. {
  190. int err, sup_flags;
  191. struct ubifs_sb_node *sup;
  192. if (c->empty) {
  193. printf("No UBIFS filesystem found!\n");
  194. return -1;
  195. }
  196. sup = ubifs_read_sb_node(c);
  197. if (IS_ERR(sup))
  198. return PTR_ERR(sup);
  199. /*
  200. * The software supports all previous versions but not future versions,
  201. * due to the unavailability of time-travelling equipment.
  202. */
  203. c->fmt_version = le32_to_cpu(sup->fmt_version);
  204. if (c->fmt_version > UBIFS_FORMAT_VERSION) {
  205. ubifs_err("on-flash format version is %d, but software only "
  206. "supports up to version %d", c->fmt_version,
  207. UBIFS_FORMAT_VERSION);
  208. err = -EINVAL;
  209. goto out;
  210. }
  211. if (c->fmt_version < 3) {
  212. ubifs_err("on-flash format version %d is not supported",
  213. c->fmt_version);
  214. err = -EINVAL;
  215. goto out;
  216. }
  217. switch (sup->key_hash) {
  218. case UBIFS_KEY_HASH_R5:
  219. c->key_hash = key_r5_hash;
  220. c->key_hash_type = UBIFS_KEY_HASH_R5;
  221. break;
  222. case UBIFS_KEY_HASH_TEST:
  223. c->key_hash = key_test_hash;
  224. c->key_hash_type = UBIFS_KEY_HASH_TEST;
  225. break;
  226. };
  227. c->key_fmt = sup->key_fmt;
  228. switch (c->key_fmt) {
  229. case UBIFS_SIMPLE_KEY_FMT:
  230. c->key_len = UBIFS_SK_LEN;
  231. break;
  232. default:
  233. ubifs_err("unsupported key format");
  234. err = -EINVAL;
  235. goto out;
  236. }
  237. c->leb_cnt = le32_to_cpu(sup->leb_cnt);
  238. c->max_leb_cnt = le32_to_cpu(sup->max_leb_cnt);
  239. c->max_bud_bytes = le64_to_cpu(sup->max_bud_bytes);
  240. c->log_lebs = le32_to_cpu(sup->log_lebs);
  241. c->lpt_lebs = le32_to_cpu(sup->lpt_lebs);
  242. c->orph_lebs = le32_to_cpu(sup->orph_lebs);
  243. c->jhead_cnt = le32_to_cpu(sup->jhead_cnt) + NONDATA_JHEADS_CNT;
  244. c->fanout = le32_to_cpu(sup->fanout);
  245. c->lsave_cnt = le32_to_cpu(sup->lsave_cnt);
  246. c->default_compr = le16_to_cpu(sup->default_compr);
  247. c->rp_size = le64_to_cpu(sup->rp_size);
  248. c->rp_uid = le32_to_cpu(sup->rp_uid);
  249. c->rp_gid = le32_to_cpu(sup->rp_gid);
  250. sup_flags = le32_to_cpu(sup->flags);
  251. c->vfs_sb->s_time_gran = le32_to_cpu(sup->time_gran);
  252. memcpy(&c->uuid, &sup->uuid, 16);
  253. c->big_lpt = !!(sup_flags & UBIFS_FLG_BIGLPT);
  254. /* Automatically increase file system size to the maximum size */
  255. c->old_leb_cnt = c->leb_cnt;
  256. if (c->leb_cnt < c->vi.size && c->leb_cnt < c->max_leb_cnt) {
  257. c->leb_cnt = min_t(int, c->max_leb_cnt, c->vi.size);
  258. dbg_mnt("Auto resizing (ro) from %d LEBs to %d LEBs",
  259. c->old_leb_cnt, c->leb_cnt);
  260. }
  261. c->log_bytes = (long long)c->log_lebs * c->leb_size;
  262. c->log_last = UBIFS_LOG_LNUM + c->log_lebs - 1;
  263. c->lpt_first = UBIFS_LOG_LNUM + c->log_lebs;
  264. c->lpt_last = c->lpt_first + c->lpt_lebs - 1;
  265. c->orph_first = c->lpt_last + 1;
  266. c->orph_last = c->orph_first + c->orph_lebs - 1;
  267. c->main_lebs = c->leb_cnt - UBIFS_SB_LEBS - UBIFS_MST_LEBS;
  268. c->main_lebs -= c->log_lebs + c->lpt_lebs + c->orph_lebs;
  269. c->main_first = c->leb_cnt - c->main_lebs;
  270. c->report_rp_size = ubifs_reported_space(c, c->rp_size);
  271. err = validate_sb(c, sup);
  272. out:
  273. kfree(sup);
  274. return err;
  275. }