sb.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  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. #include <linux/random.h>
  29. /*
  30. * Default journal size in logical eraseblocks as a percent of total
  31. * flash size.
  32. */
  33. #define DEFAULT_JNL_PERCENT 5
  34. /* Default maximum journal size in bytes */
  35. #define DEFAULT_MAX_JNL (32*1024*1024)
  36. /* Default indexing tree fanout */
  37. #define DEFAULT_FANOUT 8
  38. /* Default number of data journal heads */
  39. #define DEFAULT_JHEADS_CNT 1
  40. /* Default positions of different LEBs in the main area */
  41. #define DEFAULT_IDX_LEB 0
  42. #define DEFAULT_DATA_LEB 1
  43. #define DEFAULT_GC_LEB 2
  44. /* Default number of LEB numbers in LPT's save table */
  45. #define DEFAULT_LSAVE_CNT 256
  46. /* Default reserved pool size as a percent of maximum free space */
  47. #define DEFAULT_RP_PERCENT 5
  48. /* The default maximum size of reserved pool in bytes */
  49. #define DEFAULT_MAX_RP_SIZE (5*1024*1024)
  50. /* Default time granularity in nanoseconds */
  51. #define DEFAULT_TIME_GRAN 1000000000
  52. /**
  53. * create_default_filesystem - format empty UBI volume.
  54. * @c: UBIFS file-system description object
  55. *
  56. * This function creates default empty file-system. Returns zero in case of
  57. * success and a negative error code in case of failure.
  58. */
  59. static int create_default_filesystem(struct ubifs_info *c)
  60. {
  61. struct ubifs_sb_node *sup;
  62. struct ubifs_mst_node *mst;
  63. struct ubifs_idx_node *idx;
  64. struct ubifs_branch *br;
  65. struct ubifs_ino_node *ino;
  66. struct ubifs_cs_node *cs;
  67. union ubifs_key key;
  68. int err, tmp, jnl_lebs, log_lebs, max_buds, main_lebs, main_first;
  69. int lpt_lebs, lpt_first, orph_lebs, big_lpt, ino_waste, sup_flags = 0;
  70. int min_leb_cnt = UBIFS_MIN_LEB_CNT;
  71. uint64_t tmp64, main_bytes;
  72. __le64 tmp_le64;
  73. /* Some functions called from here depend on the @c->key_len filed */
  74. c->key_len = UBIFS_SK_LEN;
  75. /*
  76. * First of all, we have to calculate default file-system geometry -
  77. * log size, journal size, etc.
  78. */
  79. if (c->leb_cnt < 0x7FFFFFFF / DEFAULT_JNL_PERCENT)
  80. /* We can first multiply then divide and have no overflow */
  81. jnl_lebs = c->leb_cnt * DEFAULT_JNL_PERCENT / 100;
  82. else
  83. jnl_lebs = (c->leb_cnt / 100) * DEFAULT_JNL_PERCENT;
  84. if (jnl_lebs < UBIFS_MIN_JNL_LEBS)
  85. jnl_lebs = UBIFS_MIN_JNL_LEBS;
  86. if (jnl_lebs * c->leb_size > DEFAULT_MAX_JNL)
  87. jnl_lebs = DEFAULT_MAX_JNL / c->leb_size;
  88. /*
  89. * The log should be large enough to fit reference nodes for all bud
  90. * LEBs. Because buds do not have to start from the beginning of LEBs
  91. * (half of the LEB may contain committed data), the log should
  92. * generally be larger, make it twice as large.
  93. */
  94. tmp = 2 * (c->ref_node_alsz * jnl_lebs) + c->leb_size - 1;
  95. log_lebs = tmp / c->leb_size;
  96. /* Plus one LEB reserved for commit */
  97. log_lebs += 1;
  98. if (c->leb_cnt - min_leb_cnt > 8) {
  99. /* And some extra space to allow writes while committing */
  100. log_lebs += 1;
  101. min_leb_cnt += 1;
  102. }
  103. max_buds = jnl_lebs - log_lebs;
  104. if (max_buds < UBIFS_MIN_BUD_LEBS)
  105. max_buds = UBIFS_MIN_BUD_LEBS;
  106. /*
  107. * Orphan nodes are stored in a separate area. One node can store a lot
  108. * of orphan inode numbers, but when new orphan comes we just add a new
  109. * orphan node. At some point the nodes are consolidated into one
  110. * orphan node.
  111. */
  112. orph_lebs = UBIFS_MIN_ORPH_LEBS;
  113. #ifdef CONFIG_UBIFS_FS_DEBUG
  114. if (c->leb_cnt - min_leb_cnt > 1)
  115. /*
  116. * For debugging purposes it is better to have at least 2
  117. * orphan LEBs, because the orphan subsystem would need to do
  118. * consolidations and would be stressed more.
  119. */
  120. orph_lebs += 1;
  121. #endif
  122. main_lebs = c->leb_cnt - UBIFS_SB_LEBS - UBIFS_MST_LEBS - log_lebs;
  123. main_lebs -= orph_lebs;
  124. lpt_first = UBIFS_LOG_LNUM + log_lebs;
  125. c->lsave_cnt = DEFAULT_LSAVE_CNT;
  126. c->max_leb_cnt = c->leb_cnt;
  127. err = ubifs_create_dflt_lpt(c, &main_lebs, lpt_first, &lpt_lebs,
  128. &big_lpt);
  129. if (err)
  130. return err;
  131. dbg_gen("LEB Properties Tree created (LEBs %d-%d)", lpt_first,
  132. lpt_first + lpt_lebs - 1);
  133. main_first = c->leb_cnt - main_lebs;
  134. /* Create default superblock */
  135. tmp = ALIGN(UBIFS_SB_NODE_SZ, c->min_io_size);
  136. sup = kzalloc(tmp, GFP_KERNEL);
  137. if (!sup)
  138. return -ENOMEM;
  139. tmp64 = (uint64_t)max_buds * c->leb_size;
  140. if (big_lpt)
  141. sup_flags |= UBIFS_FLG_BIGLPT;
  142. sup->ch.node_type = UBIFS_SB_NODE;
  143. sup->key_hash = UBIFS_KEY_HASH_R5;
  144. sup->flags = cpu_to_le32(sup_flags);
  145. sup->min_io_size = cpu_to_le32(c->min_io_size);
  146. sup->leb_size = cpu_to_le32(c->leb_size);
  147. sup->leb_cnt = cpu_to_le32(c->leb_cnt);
  148. sup->max_leb_cnt = cpu_to_le32(c->max_leb_cnt);
  149. sup->max_bud_bytes = cpu_to_le64(tmp64);
  150. sup->log_lebs = cpu_to_le32(log_lebs);
  151. sup->lpt_lebs = cpu_to_le32(lpt_lebs);
  152. sup->orph_lebs = cpu_to_le32(orph_lebs);
  153. sup->jhead_cnt = cpu_to_le32(DEFAULT_JHEADS_CNT);
  154. sup->fanout = cpu_to_le32(DEFAULT_FANOUT);
  155. sup->lsave_cnt = cpu_to_le32(c->lsave_cnt);
  156. sup->fmt_version = cpu_to_le32(UBIFS_FORMAT_VERSION);
  157. sup->default_compr = cpu_to_le16(UBIFS_COMPR_LZO);
  158. sup->time_gran = cpu_to_le32(DEFAULT_TIME_GRAN);
  159. generate_random_uuid(sup->uuid);
  160. main_bytes = (uint64_t)main_lebs * c->leb_size;
  161. tmp64 = main_bytes * DEFAULT_RP_PERCENT;
  162. do_div(tmp64, 100);
  163. if (tmp64 > DEFAULT_MAX_RP_SIZE)
  164. tmp64 = DEFAULT_MAX_RP_SIZE;
  165. sup->rp_size = cpu_to_le64(tmp64);
  166. err = ubifs_write_node(c, sup, UBIFS_SB_NODE_SZ, 0, 0, UBI_LONGTERM);
  167. kfree(sup);
  168. if (err)
  169. return err;
  170. dbg_gen("default superblock created at LEB 0:0");
  171. /* Create default master node */
  172. mst = kzalloc(c->mst_node_alsz, GFP_KERNEL);
  173. if (!mst)
  174. return -ENOMEM;
  175. mst->ch.node_type = UBIFS_MST_NODE;
  176. mst->log_lnum = cpu_to_le32(UBIFS_LOG_LNUM);
  177. mst->highest_inum = cpu_to_le64(UBIFS_FIRST_INO);
  178. mst->cmt_no = 0;
  179. mst->root_lnum = cpu_to_le32(main_first + DEFAULT_IDX_LEB);
  180. mst->root_offs = 0;
  181. tmp = ubifs_idx_node_sz(c, 1);
  182. mst->root_len = cpu_to_le32(tmp);
  183. mst->gc_lnum = cpu_to_le32(main_first + DEFAULT_GC_LEB);
  184. mst->ihead_lnum = cpu_to_le32(main_first + DEFAULT_IDX_LEB);
  185. mst->ihead_offs = cpu_to_le32(ALIGN(tmp, c->min_io_size));
  186. mst->index_size = cpu_to_le64(ALIGN(tmp, 8));
  187. mst->lpt_lnum = cpu_to_le32(c->lpt_lnum);
  188. mst->lpt_offs = cpu_to_le32(c->lpt_offs);
  189. mst->nhead_lnum = cpu_to_le32(c->nhead_lnum);
  190. mst->nhead_offs = cpu_to_le32(c->nhead_offs);
  191. mst->ltab_lnum = cpu_to_le32(c->ltab_lnum);
  192. mst->ltab_offs = cpu_to_le32(c->ltab_offs);
  193. mst->lsave_lnum = cpu_to_le32(c->lsave_lnum);
  194. mst->lsave_offs = cpu_to_le32(c->lsave_offs);
  195. mst->lscan_lnum = cpu_to_le32(main_first);
  196. mst->empty_lebs = cpu_to_le32(main_lebs - 2);
  197. mst->idx_lebs = cpu_to_le32(1);
  198. mst->leb_cnt = cpu_to_le32(c->leb_cnt);
  199. /* Calculate lprops statistics */
  200. tmp64 = main_bytes;
  201. tmp64 -= ALIGN(ubifs_idx_node_sz(c, 1), c->min_io_size);
  202. tmp64 -= ALIGN(UBIFS_INO_NODE_SZ, c->min_io_size);
  203. mst->total_free = cpu_to_le64(tmp64);
  204. tmp64 = ALIGN(ubifs_idx_node_sz(c, 1), c->min_io_size);
  205. ino_waste = ALIGN(UBIFS_INO_NODE_SZ, c->min_io_size) -
  206. UBIFS_INO_NODE_SZ;
  207. tmp64 += ino_waste;
  208. tmp64 -= ALIGN(ubifs_idx_node_sz(c, 1), 8);
  209. mst->total_dirty = cpu_to_le64(tmp64);
  210. /* The indexing LEB does not contribute to dark space */
  211. tmp64 = (c->main_lebs - 1) * c->dark_wm;
  212. mst->total_dark = cpu_to_le64(tmp64);
  213. mst->total_used = cpu_to_le64(UBIFS_INO_NODE_SZ);
  214. err = ubifs_write_node(c, mst, UBIFS_MST_NODE_SZ, UBIFS_MST_LNUM, 0,
  215. UBI_UNKNOWN);
  216. if (err) {
  217. kfree(mst);
  218. return err;
  219. }
  220. err = ubifs_write_node(c, mst, UBIFS_MST_NODE_SZ, UBIFS_MST_LNUM + 1, 0,
  221. UBI_UNKNOWN);
  222. kfree(mst);
  223. if (err)
  224. return err;
  225. dbg_gen("default master node created at LEB %d:0", UBIFS_MST_LNUM);
  226. /* Create the root indexing node */
  227. tmp = ubifs_idx_node_sz(c, 1);
  228. idx = kzalloc(ALIGN(tmp, c->min_io_size), GFP_KERNEL);
  229. if (!idx)
  230. return -ENOMEM;
  231. c->key_fmt = UBIFS_SIMPLE_KEY_FMT;
  232. c->key_hash = key_r5_hash;
  233. idx->ch.node_type = UBIFS_IDX_NODE;
  234. idx->child_cnt = cpu_to_le16(1);
  235. ino_key_init(c, &key, UBIFS_ROOT_INO);
  236. br = ubifs_idx_branch(c, idx, 0);
  237. key_write_idx(c, &key, &br->key);
  238. br->lnum = cpu_to_le32(main_first + DEFAULT_DATA_LEB);
  239. br->len = cpu_to_le32(UBIFS_INO_NODE_SZ);
  240. err = ubifs_write_node(c, idx, tmp, main_first + DEFAULT_IDX_LEB, 0,
  241. UBI_UNKNOWN);
  242. kfree(idx);
  243. if (err)
  244. return err;
  245. dbg_gen("default root indexing node created LEB %d:0",
  246. main_first + DEFAULT_IDX_LEB);
  247. /* Create default root inode */
  248. tmp = ALIGN(UBIFS_INO_NODE_SZ, c->min_io_size);
  249. ino = kzalloc(tmp, GFP_KERNEL);
  250. if (!ino)
  251. return -ENOMEM;
  252. ino_key_init_flash(c, &ino->key, UBIFS_ROOT_INO);
  253. ino->ch.node_type = UBIFS_INO_NODE;
  254. ino->creat_sqnum = cpu_to_le64(++c->max_sqnum);
  255. ino->nlink = cpu_to_le32(2);
  256. tmp_le64 = cpu_to_le64(CURRENT_TIME_SEC.tv_sec);
  257. ino->atime_sec = tmp_le64;
  258. ino->ctime_sec = tmp_le64;
  259. ino->mtime_sec = tmp_le64;
  260. ino->atime_nsec = 0;
  261. ino->ctime_nsec = 0;
  262. ino->mtime_nsec = 0;
  263. ino->mode = cpu_to_le32(S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO);
  264. ino->size = cpu_to_le64(UBIFS_INO_NODE_SZ);
  265. /* Set compression enabled by default */
  266. ino->flags = cpu_to_le32(UBIFS_COMPR_FL);
  267. err = ubifs_write_node(c, ino, UBIFS_INO_NODE_SZ,
  268. main_first + DEFAULT_DATA_LEB, 0,
  269. UBI_UNKNOWN);
  270. kfree(ino);
  271. if (err)
  272. return err;
  273. dbg_gen("root inode created at LEB %d:0",
  274. main_first + DEFAULT_DATA_LEB);
  275. /*
  276. * The first node in the log has to be the commit start node. This is
  277. * always the case during normal file-system operation. Write a fake
  278. * commit start node to the log.
  279. */
  280. tmp = ALIGN(UBIFS_CS_NODE_SZ, c->min_io_size);
  281. cs = kzalloc(tmp, GFP_KERNEL);
  282. if (!cs)
  283. return -ENOMEM;
  284. cs->ch.node_type = UBIFS_CS_NODE;
  285. err = ubifs_write_node(c, cs, UBIFS_CS_NODE_SZ, UBIFS_LOG_LNUM,
  286. 0, UBI_UNKNOWN);
  287. kfree(cs);
  288. ubifs_msg("default file-system created");
  289. return 0;
  290. }
  291. /**
  292. * validate_sb - validate superblock node.
  293. * @c: UBIFS file-system description object
  294. * @sup: superblock node
  295. *
  296. * This function validates superblock node @sup. Since most of data was read
  297. * from the superblock and stored in @c, the function validates fields in @c
  298. * instead. Returns zero in case of success and %-EINVAL in case of validation
  299. * failure.
  300. */
  301. static int validate_sb(struct ubifs_info *c, struct ubifs_sb_node *sup)
  302. {
  303. long long max_bytes;
  304. int err = 1, min_leb_cnt;
  305. if (!c->key_hash) {
  306. err = 2;
  307. goto failed;
  308. }
  309. if (sup->key_fmt != UBIFS_SIMPLE_KEY_FMT) {
  310. err = 3;
  311. goto failed;
  312. }
  313. if (le32_to_cpu(sup->min_io_size) != c->min_io_size) {
  314. ubifs_err("min. I/O unit mismatch: %d in superblock, %d real",
  315. le32_to_cpu(sup->min_io_size), c->min_io_size);
  316. goto failed;
  317. }
  318. if (le32_to_cpu(sup->leb_size) != c->leb_size) {
  319. ubifs_err("LEB size mismatch: %d in superblock, %d real",
  320. le32_to_cpu(sup->leb_size), c->leb_size);
  321. goto failed;
  322. }
  323. if (c->log_lebs < UBIFS_MIN_LOG_LEBS ||
  324. c->lpt_lebs < UBIFS_MIN_LPT_LEBS ||
  325. c->orph_lebs < UBIFS_MIN_ORPH_LEBS ||
  326. c->main_lebs < UBIFS_MIN_MAIN_LEBS) {
  327. err = 4;
  328. goto failed;
  329. }
  330. /*
  331. * Calculate minimum allowed amount of main area LEBs. This is very
  332. * similar to %UBIFS_MIN_LEB_CNT, but we take into account real what we
  333. * have just read from the superblock.
  334. */
  335. min_leb_cnt = UBIFS_SB_LEBS + UBIFS_MST_LEBS + c->log_lebs;
  336. min_leb_cnt += c->lpt_lebs + c->orph_lebs + c->jhead_cnt + 6;
  337. if (c->leb_cnt < min_leb_cnt || c->leb_cnt > c->vi.size) {
  338. ubifs_err("bad LEB count: %d in superblock, %d on UBI volume, "
  339. "%d minimum required", c->leb_cnt, c->vi.size,
  340. min_leb_cnt);
  341. goto failed;
  342. }
  343. if (c->max_leb_cnt < c->leb_cnt) {
  344. ubifs_err("max. LEB count %d less than LEB count %d",
  345. c->max_leb_cnt, c->leb_cnt);
  346. goto failed;
  347. }
  348. if (c->main_lebs < UBIFS_MIN_MAIN_LEBS) {
  349. err = 7;
  350. goto failed;
  351. }
  352. if (c->max_bud_bytes < (long long)c->leb_size * UBIFS_MIN_BUD_LEBS ||
  353. c->max_bud_bytes > (long long)c->leb_size * c->main_lebs) {
  354. err = 8;
  355. goto failed;
  356. }
  357. if (c->jhead_cnt < NONDATA_JHEADS_CNT + 1 ||
  358. c->jhead_cnt > NONDATA_JHEADS_CNT + UBIFS_MAX_JHEADS) {
  359. err = 9;
  360. goto failed;
  361. }
  362. if (c->fanout < UBIFS_MIN_FANOUT ||
  363. ubifs_idx_node_sz(c, c->fanout) > c->leb_size) {
  364. err = 10;
  365. goto failed;
  366. }
  367. if (c->lsave_cnt < 0 || (c->lsave_cnt > DEFAULT_LSAVE_CNT &&
  368. c->lsave_cnt > c->max_leb_cnt - UBIFS_SB_LEBS - UBIFS_MST_LEBS -
  369. c->log_lebs - c->lpt_lebs - c->orph_lebs)) {
  370. err = 11;
  371. goto failed;
  372. }
  373. if (UBIFS_SB_LEBS + UBIFS_MST_LEBS + c->log_lebs + c->lpt_lebs +
  374. c->orph_lebs + c->main_lebs != c->leb_cnt) {
  375. err = 12;
  376. goto failed;
  377. }
  378. if (c->default_compr < 0 || c->default_compr >= UBIFS_COMPR_TYPES_CNT) {
  379. err = 13;
  380. goto failed;
  381. }
  382. max_bytes = c->main_lebs * (long long)c->leb_size;
  383. if (c->rp_size < 0 || max_bytes < c->rp_size) {
  384. err = 14;
  385. goto failed;
  386. }
  387. if (le32_to_cpu(sup->time_gran) > 1000000000 ||
  388. le32_to_cpu(sup->time_gran) < 1) {
  389. err = 15;
  390. goto failed;
  391. }
  392. return 0;
  393. failed:
  394. ubifs_err("bad superblock, error %d", err);
  395. dbg_dump_node(c, sup);
  396. return -EINVAL;
  397. }
  398. /**
  399. * ubifs_read_sb_node - read superblock node.
  400. * @c: UBIFS file-system description object
  401. *
  402. * This function returns a pointer to the superblock node or a negative error
  403. * code.
  404. */
  405. struct ubifs_sb_node *ubifs_read_sb_node(struct ubifs_info *c)
  406. {
  407. struct ubifs_sb_node *sup;
  408. int err;
  409. sup = kmalloc(ALIGN(UBIFS_SB_NODE_SZ, c->min_io_size), GFP_NOFS);
  410. if (!sup)
  411. return ERR_PTR(-ENOMEM);
  412. err = ubifs_read_node(c, sup, UBIFS_SB_NODE, UBIFS_SB_NODE_SZ,
  413. UBIFS_SB_LNUM, 0);
  414. if (err) {
  415. kfree(sup);
  416. return ERR_PTR(err);
  417. }
  418. return sup;
  419. }
  420. /**
  421. * ubifs_write_sb_node - write superblock node.
  422. * @c: UBIFS file-system description object
  423. * @sup: superblock node read with 'ubifs_read_sb_node()'
  424. *
  425. * This function returns %0 on success and a negative error code on failure.
  426. */
  427. int ubifs_write_sb_node(struct ubifs_info *c, struct ubifs_sb_node *sup)
  428. {
  429. int len = ALIGN(UBIFS_SB_NODE_SZ, c->min_io_size);
  430. ubifs_prepare_node(c, sup, UBIFS_SB_NODE_SZ, 1);
  431. return ubifs_leb_change(c, UBIFS_SB_LNUM, sup, len, UBI_LONGTERM);
  432. }
  433. /**
  434. * ubifs_read_superblock - read superblock.
  435. * @c: UBIFS file-system description object
  436. *
  437. * This function finds, reads and checks the superblock. If an empty UBI volume
  438. * is being mounted, this function creates default superblock. Returns zero in
  439. * case of success, and a negative error code in case of failure.
  440. */
  441. int ubifs_read_superblock(struct ubifs_info *c)
  442. {
  443. int err, sup_flags;
  444. struct ubifs_sb_node *sup;
  445. if (c->empty) {
  446. err = create_default_filesystem(c);
  447. if (err)
  448. return err;
  449. }
  450. sup = ubifs_read_sb_node(c);
  451. if (IS_ERR(sup))
  452. return PTR_ERR(sup);
  453. /*
  454. * The software supports all previous versions but not future versions,
  455. * due to the unavailability of time-travelling equipment.
  456. */
  457. c->fmt_version = le32_to_cpu(sup->fmt_version);
  458. if (c->fmt_version > UBIFS_FORMAT_VERSION) {
  459. ubifs_err("on-flash format version is %d, but software only "
  460. "supports up to version %d", c->fmt_version,
  461. UBIFS_FORMAT_VERSION);
  462. err = -EINVAL;
  463. goto out;
  464. }
  465. if (c->fmt_version < 3) {
  466. ubifs_err("on-flash format version %d is not supported",
  467. c->fmt_version);
  468. err = -EINVAL;
  469. goto out;
  470. }
  471. switch (sup->key_hash) {
  472. case UBIFS_KEY_HASH_R5:
  473. c->key_hash = key_r5_hash;
  474. c->key_hash_type = UBIFS_KEY_HASH_R5;
  475. break;
  476. case UBIFS_KEY_HASH_TEST:
  477. c->key_hash = key_test_hash;
  478. c->key_hash_type = UBIFS_KEY_HASH_TEST;
  479. break;
  480. };
  481. c->key_fmt = sup->key_fmt;
  482. switch (c->key_fmt) {
  483. case UBIFS_SIMPLE_KEY_FMT:
  484. c->key_len = UBIFS_SK_LEN;
  485. break;
  486. default:
  487. ubifs_err("unsupported key format");
  488. err = -EINVAL;
  489. goto out;
  490. }
  491. c->leb_cnt = le32_to_cpu(sup->leb_cnt);
  492. c->max_leb_cnt = le32_to_cpu(sup->max_leb_cnt);
  493. c->max_bud_bytes = le64_to_cpu(sup->max_bud_bytes);
  494. c->log_lebs = le32_to_cpu(sup->log_lebs);
  495. c->lpt_lebs = le32_to_cpu(sup->lpt_lebs);
  496. c->orph_lebs = le32_to_cpu(sup->orph_lebs);
  497. c->jhead_cnt = le32_to_cpu(sup->jhead_cnt) + NONDATA_JHEADS_CNT;
  498. c->fanout = le32_to_cpu(sup->fanout);
  499. c->lsave_cnt = le32_to_cpu(sup->lsave_cnt);
  500. c->default_compr = le16_to_cpu(sup->default_compr);
  501. c->rp_size = le64_to_cpu(sup->rp_size);
  502. c->rp_uid = le32_to_cpu(sup->rp_uid);
  503. c->rp_gid = le32_to_cpu(sup->rp_gid);
  504. sup_flags = le32_to_cpu(sup->flags);
  505. c->vfs_sb->s_time_gran = le32_to_cpu(sup->time_gran);
  506. memcpy(&c->uuid, &sup->uuid, 16);
  507. c->big_lpt = !!(sup_flags & UBIFS_FLG_BIGLPT);
  508. /* Automatically increase file system size to the maximum size */
  509. c->old_leb_cnt = c->leb_cnt;
  510. if (c->leb_cnt < c->vi.size && c->leb_cnt < c->max_leb_cnt) {
  511. c->leb_cnt = min_t(int, c->max_leb_cnt, c->vi.size);
  512. if (c->vfs_sb->s_flags & MS_RDONLY)
  513. dbg_mnt("Auto resizing (ro) from %d LEBs to %d LEBs",
  514. c->old_leb_cnt, c->leb_cnt);
  515. else {
  516. dbg_mnt("Auto resizing (sb) from %d LEBs to %d LEBs",
  517. c->old_leb_cnt, c->leb_cnt);
  518. sup->leb_cnt = cpu_to_le32(c->leb_cnt);
  519. err = ubifs_write_sb_node(c, sup);
  520. if (err)
  521. goto out;
  522. c->old_leb_cnt = c->leb_cnt;
  523. }
  524. }
  525. c->log_bytes = (long long)c->log_lebs * c->leb_size;
  526. c->log_last = UBIFS_LOG_LNUM + c->log_lebs - 1;
  527. c->lpt_first = UBIFS_LOG_LNUM + c->log_lebs;
  528. c->lpt_last = c->lpt_first + c->lpt_lebs - 1;
  529. c->orph_first = c->lpt_last + 1;
  530. c->orph_last = c->orph_first + c->orph_lebs - 1;
  531. c->main_lebs = c->leb_cnt - UBIFS_SB_LEBS - UBIFS_MST_LEBS;
  532. c->main_lebs -= c->log_lebs + c->lpt_lebs + c->orph_lebs;
  533. c->main_first = c->leb_cnt - c->main_lebs;
  534. c->report_rp_size = ubifs_reported_space(c, c->rp_size);
  535. err = validate_sb(c, sup);
  536. out:
  537. kfree(sup);
  538. return err;
  539. }