sb.c 18 KB

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