sb.c 18 KB

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