sb.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  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. sup->ro_compat_version = cpu_to_le32(UBIFS_RO_COMPAT_VERSION);
  170. err = ubifs_write_node(c, sup, UBIFS_SB_NODE_SZ, 0, 0, UBI_LONGTERM);
  171. kfree(sup);
  172. if (err)
  173. return err;
  174. dbg_gen("default superblock created at LEB 0:0");
  175. /* Create default master node */
  176. mst = kzalloc(c->mst_node_alsz, GFP_KERNEL);
  177. if (!mst)
  178. return -ENOMEM;
  179. mst->ch.node_type = UBIFS_MST_NODE;
  180. mst->log_lnum = cpu_to_le32(UBIFS_LOG_LNUM);
  181. mst->highest_inum = cpu_to_le64(UBIFS_FIRST_INO);
  182. mst->cmt_no = 0;
  183. mst->root_lnum = cpu_to_le32(main_first + DEFAULT_IDX_LEB);
  184. mst->root_offs = 0;
  185. tmp = ubifs_idx_node_sz(c, 1);
  186. mst->root_len = cpu_to_le32(tmp);
  187. mst->gc_lnum = cpu_to_le32(main_first + DEFAULT_GC_LEB);
  188. mst->ihead_lnum = cpu_to_le32(main_first + DEFAULT_IDX_LEB);
  189. mst->ihead_offs = cpu_to_le32(ALIGN(tmp, c->min_io_size));
  190. mst->index_size = cpu_to_le64(ALIGN(tmp, 8));
  191. mst->lpt_lnum = cpu_to_le32(c->lpt_lnum);
  192. mst->lpt_offs = cpu_to_le32(c->lpt_offs);
  193. mst->nhead_lnum = cpu_to_le32(c->nhead_lnum);
  194. mst->nhead_offs = cpu_to_le32(c->nhead_offs);
  195. mst->ltab_lnum = cpu_to_le32(c->ltab_lnum);
  196. mst->ltab_offs = cpu_to_le32(c->ltab_offs);
  197. mst->lsave_lnum = cpu_to_le32(c->lsave_lnum);
  198. mst->lsave_offs = cpu_to_le32(c->lsave_offs);
  199. mst->lscan_lnum = cpu_to_le32(main_first);
  200. mst->empty_lebs = cpu_to_le32(main_lebs - 2);
  201. mst->idx_lebs = cpu_to_le32(1);
  202. mst->leb_cnt = cpu_to_le32(c->leb_cnt);
  203. /* Calculate lprops statistics */
  204. tmp64 = main_bytes;
  205. tmp64 -= ALIGN(ubifs_idx_node_sz(c, 1), c->min_io_size);
  206. tmp64 -= ALIGN(UBIFS_INO_NODE_SZ, c->min_io_size);
  207. mst->total_free = cpu_to_le64(tmp64);
  208. tmp64 = ALIGN(ubifs_idx_node_sz(c, 1), c->min_io_size);
  209. ino_waste = ALIGN(UBIFS_INO_NODE_SZ, c->min_io_size) -
  210. UBIFS_INO_NODE_SZ;
  211. tmp64 += ino_waste;
  212. tmp64 -= ALIGN(ubifs_idx_node_sz(c, 1), 8);
  213. mst->total_dirty = cpu_to_le64(tmp64);
  214. /* The indexing LEB does not contribute to dark space */
  215. tmp64 = (c->main_lebs - 1) * c->dark_wm;
  216. mst->total_dark = cpu_to_le64(tmp64);
  217. mst->total_used = cpu_to_le64(UBIFS_INO_NODE_SZ);
  218. err = ubifs_write_node(c, mst, UBIFS_MST_NODE_SZ, UBIFS_MST_LNUM, 0,
  219. UBI_UNKNOWN);
  220. if (err) {
  221. kfree(mst);
  222. return err;
  223. }
  224. err = ubifs_write_node(c, mst, UBIFS_MST_NODE_SZ, UBIFS_MST_LNUM + 1, 0,
  225. UBI_UNKNOWN);
  226. kfree(mst);
  227. if (err)
  228. return err;
  229. dbg_gen("default master node created at LEB %d:0", UBIFS_MST_LNUM);
  230. /* Create the root indexing node */
  231. tmp = ubifs_idx_node_sz(c, 1);
  232. idx = kzalloc(ALIGN(tmp, c->min_io_size), GFP_KERNEL);
  233. if (!idx)
  234. return -ENOMEM;
  235. c->key_fmt = UBIFS_SIMPLE_KEY_FMT;
  236. c->key_hash = key_r5_hash;
  237. idx->ch.node_type = UBIFS_IDX_NODE;
  238. idx->child_cnt = cpu_to_le16(1);
  239. ino_key_init(c, &key, UBIFS_ROOT_INO);
  240. br = ubifs_idx_branch(c, idx, 0);
  241. key_write_idx(c, &key, &br->key);
  242. br->lnum = cpu_to_le32(main_first + DEFAULT_DATA_LEB);
  243. br->len = cpu_to_le32(UBIFS_INO_NODE_SZ);
  244. err = ubifs_write_node(c, idx, tmp, main_first + DEFAULT_IDX_LEB, 0,
  245. UBI_UNKNOWN);
  246. kfree(idx);
  247. if (err)
  248. return err;
  249. dbg_gen("default root indexing node created LEB %d:0",
  250. main_first + DEFAULT_IDX_LEB);
  251. /* Create default root inode */
  252. tmp = ALIGN(UBIFS_INO_NODE_SZ, c->min_io_size);
  253. ino = kzalloc(tmp, GFP_KERNEL);
  254. if (!ino)
  255. return -ENOMEM;
  256. ino_key_init_flash(c, &ino->key, UBIFS_ROOT_INO);
  257. ino->ch.node_type = UBIFS_INO_NODE;
  258. ino->creat_sqnum = cpu_to_le64(++c->max_sqnum);
  259. ino->nlink = cpu_to_le32(2);
  260. tmp_le64 = cpu_to_le64(CURRENT_TIME_SEC.tv_sec);
  261. ino->atime_sec = tmp_le64;
  262. ino->ctime_sec = tmp_le64;
  263. ino->mtime_sec = tmp_le64;
  264. ino->atime_nsec = 0;
  265. ino->ctime_nsec = 0;
  266. ino->mtime_nsec = 0;
  267. ino->mode = cpu_to_le32(S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO);
  268. ino->size = cpu_to_le64(UBIFS_INO_NODE_SZ);
  269. /* Set compression enabled by default */
  270. ino->flags = cpu_to_le32(UBIFS_COMPR_FL);
  271. err = ubifs_write_node(c, ino, UBIFS_INO_NODE_SZ,
  272. main_first + DEFAULT_DATA_LEB, 0,
  273. UBI_UNKNOWN);
  274. kfree(ino);
  275. if (err)
  276. return err;
  277. dbg_gen("root inode created at LEB %d:0",
  278. main_first + DEFAULT_DATA_LEB);
  279. /*
  280. * The first node in the log has to be the commit start node. This is
  281. * always the case during normal file-system operation. Write a fake
  282. * commit start node to the log.
  283. */
  284. tmp = ALIGN(UBIFS_CS_NODE_SZ, c->min_io_size);
  285. cs = kzalloc(tmp, GFP_KERNEL);
  286. if (!cs)
  287. return -ENOMEM;
  288. cs->ch.node_type = UBIFS_CS_NODE;
  289. err = ubifs_write_node(c, cs, UBIFS_CS_NODE_SZ, UBIFS_LOG_LNUM,
  290. 0, UBI_UNKNOWN);
  291. kfree(cs);
  292. ubifs_msg("default file-system created");
  293. return 0;
  294. }
  295. /**
  296. * validate_sb - validate superblock node.
  297. * @c: UBIFS file-system description object
  298. * @sup: superblock node
  299. *
  300. * This function validates superblock node @sup. Since most of data was read
  301. * from the superblock and stored in @c, the function validates fields in @c
  302. * instead. Returns zero in case of success and %-EINVAL in case of validation
  303. * failure.
  304. */
  305. static int validate_sb(struct ubifs_info *c, struct ubifs_sb_node *sup)
  306. {
  307. long long max_bytes;
  308. int err = 1, min_leb_cnt;
  309. if (!c->key_hash) {
  310. err = 2;
  311. goto failed;
  312. }
  313. if (sup->key_fmt != UBIFS_SIMPLE_KEY_FMT) {
  314. err = 3;
  315. goto failed;
  316. }
  317. if (le32_to_cpu(sup->min_io_size) != c->min_io_size) {
  318. ubifs_err("min. I/O unit mismatch: %d in superblock, %d real",
  319. le32_to_cpu(sup->min_io_size), c->min_io_size);
  320. goto failed;
  321. }
  322. if (le32_to_cpu(sup->leb_size) != c->leb_size) {
  323. ubifs_err("LEB size mismatch: %d in superblock, %d real",
  324. le32_to_cpu(sup->leb_size), c->leb_size);
  325. goto failed;
  326. }
  327. if (c->log_lebs < UBIFS_MIN_LOG_LEBS ||
  328. c->lpt_lebs < UBIFS_MIN_LPT_LEBS ||
  329. c->orph_lebs < UBIFS_MIN_ORPH_LEBS ||
  330. c->main_lebs < UBIFS_MIN_MAIN_LEBS) {
  331. err = 4;
  332. goto failed;
  333. }
  334. /*
  335. * Calculate minimum allowed amount of main area LEBs. This is very
  336. * similar to %UBIFS_MIN_LEB_CNT, but we take into account real what we
  337. * have just read from the superblock.
  338. */
  339. min_leb_cnt = UBIFS_SB_LEBS + UBIFS_MST_LEBS + c->log_lebs;
  340. min_leb_cnt += c->lpt_lebs + c->orph_lebs + c->jhead_cnt + 6;
  341. if (c->leb_cnt < min_leb_cnt || c->leb_cnt > c->vi.size) {
  342. ubifs_err("bad LEB count: %d in superblock, %d on UBI volume, "
  343. "%d minimum required", c->leb_cnt, c->vi.size,
  344. min_leb_cnt);
  345. goto failed;
  346. }
  347. if (c->max_leb_cnt < c->leb_cnt) {
  348. ubifs_err("max. LEB count %d less than LEB count %d",
  349. c->max_leb_cnt, c->leb_cnt);
  350. goto failed;
  351. }
  352. if (c->main_lebs < UBIFS_MIN_MAIN_LEBS) {
  353. err = 7;
  354. goto failed;
  355. }
  356. if (c->max_bud_bytes < (long long)c->leb_size * UBIFS_MIN_BUD_LEBS ||
  357. c->max_bud_bytes > (long long)c->leb_size * c->main_lebs) {
  358. err = 8;
  359. goto failed;
  360. }
  361. if (c->jhead_cnt < NONDATA_JHEADS_CNT + 1 ||
  362. c->jhead_cnt > NONDATA_JHEADS_CNT + UBIFS_MAX_JHEADS) {
  363. err = 9;
  364. goto failed;
  365. }
  366. if (c->fanout < UBIFS_MIN_FANOUT ||
  367. ubifs_idx_node_sz(c, c->fanout) > c->leb_size) {
  368. err = 10;
  369. goto failed;
  370. }
  371. if (c->lsave_cnt < 0 || (c->lsave_cnt > DEFAULT_LSAVE_CNT &&
  372. c->lsave_cnt > c->max_leb_cnt - UBIFS_SB_LEBS - UBIFS_MST_LEBS -
  373. c->log_lebs - c->lpt_lebs - c->orph_lebs)) {
  374. err = 11;
  375. goto failed;
  376. }
  377. if (UBIFS_SB_LEBS + UBIFS_MST_LEBS + c->log_lebs + c->lpt_lebs +
  378. c->orph_lebs + c->main_lebs != c->leb_cnt) {
  379. err = 12;
  380. goto failed;
  381. }
  382. if (c->default_compr < 0 || c->default_compr >= UBIFS_COMPR_TYPES_CNT) {
  383. err = 13;
  384. goto failed;
  385. }
  386. max_bytes = c->main_lebs * (long long)c->leb_size;
  387. if (c->rp_size < 0 || max_bytes < c->rp_size) {
  388. err = 14;
  389. goto failed;
  390. }
  391. if (le32_to_cpu(sup->time_gran) > 1000000000 ||
  392. le32_to_cpu(sup->time_gran) < 1) {
  393. err = 15;
  394. goto failed;
  395. }
  396. return 0;
  397. failed:
  398. ubifs_err("bad superblock, error %d", err);
  399. dbg_dump_node(c, sup);
  400. return -EINVAL;
  401. }
  402. /**
  403. * ubifs_read_sb_node - read superblock node.
  404. * @c: UBIFS file-system description object
  405. *
  406. * This function returns a pointer to the superblock node or a negative error
  407. * code.
  408. */
  409. struct ubifs_sb_node *ubifs_read_sb_node(struct ubifs_info *c)
  410. {
  411. struct ubifs_sb_node *sup;
  412. int err;
  413. sup = kmalloc(ALIGN(UBIFS_SB_NODE_SZ, c->min_io_size), GFP_NOFS);
  414. if (!sup)
  415. return ERR_PTR(-ENOMEM);
  416. err = ubifs_read_node(c, sup, UBIFS_SB_NODE, UBIFS_SB_NODE_SZ,
  417. UBIFS_SB_LNUM, 0);
  418. if (err) {
  419. kfree(sup);
  420. return ERR_PTR(err);
  421. }
  422. return sup;
  423. }
  424. /**
  425. * ubifs_write_sb_node - write superblock node.
  426. * @c: UBIFS file-system description object
  427. * @sup: superblock node read with 'ubifs_read_sb_node()'
  428. *
  429. * This function returns %0 on success and a negative error code on failure.
  430. */
  431. int ubifs_write_sb_node(struct ubifs_info *c, struct ubifs_sb_node *sup)
  432. {
  433. int len = ALIGN(UBIFS_SB_NODE_SZ, c->min_io_size);
  434. ubifs_prepare_node(c, sup, UBIFS_SB_NODE_SZ, 1);
  435. return ubifs_leb_change(c, UBIFS_SB_LNUM, sup, len, UBI_LONGTERM);
  436. }
  437. /**
  438. * ubifs_read_superblock - read superblock.
  439. * @c: UBIFS file-system description object
  440. *
  441. * This function finds, reads and checks the superblock. If an empty UBI volume
  442. * is being mounted, this function creates default superblock. Returns zero in
  443. * case of success, and a negative error code in case of failure.
  444. */
  445. int ubifs_read_superblock(struct ubifs_info *c)
  446. {
  447. int err, sup_flags;
  448. struct ubifs_sb_node *sup;
  449. if (c->empty) {
  450. err = create_default_filesystem(c);
  451. if (err)
  452. return err;
  453. }
  454. sup = ubifs_read_sb_node(c);
  455. if (IS_ERR(sup))
  456. return PTR_ERR(sup);
  457. c->fmt_version = le32_to_cpu(sup->fmt_version);
  458. c->ro_compat_version = le32_to_cpu(sup->ro_compat_version);
  459. /*
  460. * The software supports all previous versions but not future versions,
  461. * due to the unavailability of time-travelling equipment.
  462. */
  463. if (c->fmt_version > UBIFS_FORMAT_VERSION) {
  464. struct super_block *sb = c->vfs_sb;
  465. int mounting_ro = sb->s_flags & MS_RDONLY;
  466. ubifs_assert(!c->ro_media || mounting_ro);
  467. if (!mounting_ro ||
  468. c->ro_compat_version > UBIFS_RO_COMPAT_VERSION) {
  469. ubifs_err("on-flash format version is w%d/r%d, but "
  470. "software only supports up to version "
  471. "w%d/r%d", c->fmt_version,
  472. c->ro_compat_version, UBIFS_FORMAT_VERSION,
  473. UBIFS_RO_COMPAT_VERSION);
  474. if (c->ro_compat_version <= UBIFS_RO_COMPAT_VERSION) {
  475. ubifs_msg("only R/O mounting is possible");
  476. err = -EROFS;
  477. } else
  478. err = -EINVAL;
  479. goto out;
  480. }
  481. /*
  482. * The FS is mounted R/O, and the media format is
  483. * R/O-compatible with the UBIFS implementation, so we can
  484. * mount.
  485. */
  486. c->rw_incompat = 1;
  487. }
  488. if (c->fmt_version < 3) {
  489. ubifs_err("on-flash format version %d is not supported",
  490. c->fmt_version);
  491. err = -EINVAL;
  492. goto out;
  493. }
  494. switch (sup->key_hash) {
  495. case UBIFS_KEY_HASH_R5:
  496. c->key_hash = key_r5_hash;
  497. c->key_hash_type = UBIFS_KEY_HASH_R5;
  498. break;
  499. case UBIFS_KEY_HASH_TEST:
  500. c->key_hash = key_test_hash;
  501. c->key_hash_type = UBIFS_KEY_HASH_TEST;
  502. break;
  503. };
  504. c->key_fmt = sup->key_fmt;
  505. switch (c->key_fmt) {
  506. case UBIFS_SIMPLE_KEY_FMT:
  507. c->key_len = UBIFS_SK_LEN;
  508. break;
  509. default:
  510. ubifs_err("unsupported key format");
  511. err = -EINVAL;
  512. goto out;
  513. }
  514. c->leb_cnt = le32_to_cpu(sup->leb_cnt);
  515. c->max_leb_cnt = le32_to_cpu(sup->max_leb_cnt);
  516. c->max_bud_bytes = le64_to_cpu(sup->max_bud_bytes);
  517. c->log_lebs = le32_to_cpu(sup->log_lebs);
  518. c->lpt_lebs = le32_to_cpu(sup->lpt_lebs);
  519. c->orph_lebs = le32_to_cpu(sup->orph_lebs);
  520. c->jhead_cnt = le32_to_cpu(sup->jhead_cnt) + NONDATA_JHEADS_CNT;
  521. c->fanout = le32_to_cpu(sup->fanout);
  522. c->lsave_cnt = le32_to_cpu(sup->lsave_cnt);
  523. c->rp_size = le64_to_cpu(sup->rp_size);
  524. c->rp_uid = le32_to_cpu(sup->rp_uid);
  525. c->rp_gid = le32_to_cpu(sup->rp_gid);
  526. sup_flags = le32_to_cpu(sup->flags);
  527. if (!c->mount_opts.override_compr)
  528. c->default_compr = le16_to_cpu(sup->default_compr);
  529. c->vfs_sb->s_time_gran = le32_to_cpu(sup->time_gran);
  530. memcpy(&c->uuid, &sup->uuid, 16);
  531. c->big_lpt = !!(sup_flags & UBIFS_FLG_BIGLPT);
  532. /* Automatically increase file system size to the maximum size */
  533. c->old_leb_cnt = c->leb_cnt;
  534. if (c->leb_cnt < c->vi.size && c->leb_cnt < c->max_leb_cnt) {
  535. c->leb_cnt = min_t(int, c->max_leb_cnt, c->vi.size);
  536. if (c->vfs_sb->s_flags & MS_RDONLY)
  537. dbg_mnt("Auto resizing (ro) from %d LEBs to %d LEBs",
  538. c->old_leb_cnt, c->leb_cnt);
  539. else {
  540. dbg_mnt("Auto resizing (sb) from %d LEBs to %d LEBs",
  541. c->old_leb_cnt, c->leb_cnt);
  542. sup->leb_cnt = cpu_to_le32(c->leb_cnt);
  543. err = ubifs_write_sb_node(c, sup);
  544. if (err)
  545. goto out;
  546. c->old_leb_cnt = c->leb_cnt;
  547. }
  548. }
  549. c->log_bytes = (long long)c->log_lebs * c->leb_size;
  550. c->log_last = UBIFS_LOG_LNUM + c->log_lebs - 1;
  551. c->lpt_first = UBIFS_LOG_LNUM + c->log_lebs;
  552. c->lpt_last = c->lpt_first + c->lpt_lebs - 1;
  553. c->orph_first = c->lpt_last + 1;
  554. c->orph_last = c->orph_first + c->orph_lebs - 1;
  555. c->main_lebs = c->leb_cnt - UBIFS_SB_LEBS - UBIFS_MST_LEBS;
  556. c->main_lebs -= c->log_lebs + c->lpt_lebs + c->orph_lebs;
  557. c->main_first = c->leb_cnt - c->main_lebs;
  558. err = validate_sb(c, sup);
  559. out:
  560. kfree(sup);
  561. return err;
  562. }