sb.c 19 KB

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