sb.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * This file is part of UBIFS.
  4. *
  5. * Copyright (C) 2006-2008 Nokia Corporation.
  6. *
  7. * Authors: Artem Bityutskiy (Битюцкий Артём)
  8. * Adrian Hunter
  9. */
  10. /*
  11. * This file implements UBIFS superblock. The superblock is stored at the first
  12. * LEB of the volume and is never changed by UBIFS. Only user-space tools may
  13. * change it. The superblock node mostly contains geometry information.
  14. */
  15. #include "ubifs.h"
  16. #include <linux/slab.h>
  17. #include <linux/math64.h>
  18. #include <linux/uuid.h>
  19. /*
  20. * Default journal size in logical eraseblocks as a percent of total
  21. * flash size.
  22. */
  23. #define DEFAULT_JNL_PERCENT 5
  24. /* Default maximum journal size in bytes */
  25. #define DEFAULT_MAX_JNL (32*1024*1024)
  26. /* Default indexing tree fanout */
  27. #define DEFAULT_FANOUT 8
  28. /* Default number of data journal heads */
  29. #define DEFAULT_JHEADS_CNT 1
  30. /* Default positions of different LEBs in the main area */
  31. #define DEFAULT_IDX_LEB 0
  32. #define DEFAULT_DATA_LEB 1
  33. #define DEFAULT_GC_LEB 2
  34. /* Default number of LEB numbers in LPT's save table */
  35. #define DEFAULT_LSAVE_CNT 256
  36. /* Default reserved pool size as a percent of maximum free space */
  37. #define DEFAULT_RP_PERCENT 5
  38. /* The default maximum size of reserved pool in bytes */
  39. #define DEFAULT_MAX_RP_SIZE (5*1024*1024)
  40. /* Default time granularity in nanoseconds */
  41. #define DEFAULT_TIME_GRAN 1000000000
  42. static int get_default_compressor(struct ubifs_info *c)
  43. {
  44. if (ubifs_compr_present(c, UBIFS_COMPR_LZO))
  45. return UBIFS_COMPR_LZO;
  46. if (ubifs_compr_present(c, UBIFS_COMPR_ZLIB))
  47. return UBIFS_COMPR_ZLIB;
  48. return UBIFS_COMPR_NONE;
  49. }
  50. /**
  51. * create_default_filesystem - format empty UBI volume.
  52. * @c: UBIFS file-system description object
  53. *
  54. * This function creates default empty file-system. Returns zero in case of
  55. * success and a negative error code in case of failure.
  56. */
  57. static int create_default_filesystem(struct ubifs_info *c)
  58. {
  59. struct ubifs_sb_node *sup;
  60. struct ubifs_mst_node *mst;
  61. struct ubifs_idx_node *idx;
  62. struct ubifs_branch *br;
  63. struct ubifs_ino_node *ino;
  64. struct ubifs_cs_node *cs;
  65. union ubifs_key key;
  66. int err, tmp, jnl_lebs, log_lebs, max_buds, main_lebs, main_first;
  67. int lpt_lebs, lpt_first, orph_lebs, big_lpt, ino_waste, sup_flags = 0;
  68. int min_leb_cnt = UBIFS_MIN_LEB_CNT;
  69. int idx_node_size;
  70. long long tmp64, main_bytes;
  71. __le64 tmp_le64;
  72. struct timespec64 ts;
  73. u8 hash[UBIFS_HASH_ARR_SZ];
  74. u8 hash_lpt[UBIFS_HASH_ARR_SZ];
  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. 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. main_lebs = c->leb_cnt - UBIFS_SB_LEBS - UBIFS_MST_LEBS - log_lebs;
  123. main_lebs -= orph_lebs;
  124. lpt_first = UBIFS_LOG_LNUM + log_lebs;
  125. c->lsave_cnt = DEFAULT_LSAVE_CNT;
  126. c->max_leb_cnt = c->leb_cnt;
  127. err = ubifs_create_dflt_lpt(c, &main_lebs, lpt_first, &lpt_lebs,
  128. &big_lpt, hash_lpt);
  129. if (err)
  130. return err;
  131. dbg_gen("LEB Properties Tree created (LEBs %d-%d)", lpt_first,
  132. lpt_first + lpt_lebs - 1);
  133. main_first = c->leb_cnt - main_lebs;
  134. sup = kzalloc(ALIGN(UBIFS_SB_NODE_SZ, c->min_io_size), GFP_KERNEL);
  135. mst = kzalloc(c->mst_node_alsz, GFP_KERNEL);
  136. idx_node_size = ubifs_idx_node_sz(c, 1);
  137. idx = kzalloc(ALIGN(idx_node_size, c->min_io_size), GFP_KERNEL);
  138. ino = kzalloc(ALIGN(UBIFS_INO_NODE_SZ, c->min_io_size), GFP_KERNEL);
  139. cs = kzalloc(ALIGN(UBIFS_CS_NODE_SZ, c->min_io_size), GFP_KERNEL);
  140. if (!sup || !mst || !idx || !ino || !cs) {
  141. err = -ENOMEM;
  142. goto out;
  143. }
  144. /* Create default superblock */
  145. tmp64 = (long long)max_buds * c->leb_size;
  146. if (big_lpt)
  147. sup_flags |= UBIFS_FLG_BIGLPT;
  148. if (ubifs_default_version > 4)
  149. sup_flags |= UBIFS_FLG_DOUBLE_HASH;
  150. if (ubifs_authenticated(c)) {
  151. sup_flags |= UBIFS_FLG_AUTHENTICATION;
  152. sup->hash_algo = cpu_to_le16(c->auth_hash_algo);
  153. err = ubifs_hmac_wkm(c, sup->hmac_wkm);
  154. if (err)
  155. goto out;
  156. } else {
  157. sup->hash_algo = cpu_to_le16(0xffff);
  158. }
  159. sup->ch.node_type = UBIFS_SB_NODE;
  160. sup->key_hash = UBIFS_KEY_HASH_R5;
  161. sup->flags = cpu_to_le32(sup_flags);
  162. sup->min_io_size = cpu_to_le32(c->min_io_size);
  163. sup->leb_size = cpu_to_le32(c->leb_size);
  164. sup->leb_cnt = cpu_to_le32(c->leb_cnt);
  165. sup->max_leb_cnt = cpu_to_le32(c->max_leb_cnt);
  166. sup->max_bud_bytes = cpu_to_le64(tmp64);
  167. sup->log_lebs = cpu_to_le32(log_lebs);
  168. sup->lpt_lebs = cpu_to_le32(lpt_lebs);
  169. sup->orph_lebs = cpu_to_le32(orph_lebs);
  170. sup->jhead_cnt = cpu_to_le32(DEFAULT_JHEADS_CNT);
  171. sup->fanout = cpu_to_le32(DEFAULT_FANOUT);
  172. sup->lsave_cnt = cpu_to_le32(c->lsave_cnt);
  173. sup->fmt_version = cpu_to_le32(ubifs_default_version);
  174. sup->time_gran = cpu_to_le32(DEFAULT_TIME_GRAN);
  175. if (c->mount_opts.override_compr)
  176. sup->default_compr = cpu_to_le16(c->mount_opts.compr_type);
  177. else
  178. sup->default_compr = cpu_to_le16(get_default_compressor(c));
  179. generate_random_uuid(sup->uuid);
  180. main_bytes = (long long)main_lebs * c->leb_size;
  181. tmp64 = div_u64(main_bytes * DEFAULT_RP_PERCENT, 100);
  182. if (tmp64 > DEFAULT_MAX_RP_SIZE)
  183. tmp64 = DEFAULT_MAX_RP_SIZE;
  184. sup->rp_size = cpu_to_le64(tmp64);
  185. sup->ro_compat_version = cpu_to_le32(UBIFS_RO_COMPAT_VERSION);
  186. dbg_gen("default superblock created at LEB 0:0");
  187. /* Create default master node */
  188. mst->ch.node_type = UBIFS_MST_NODE;
  189. mst->log_lnum = cpu_to_le32(UBIFS_LOG_LNUM);
  190. mst->highest_inum = cpu_to_le64(UBIFS_FIRST_INO);
  191. mst->cmt_no = 0;
  192. mst->root_lnum = cpu_to_le32(main_first + DEFAULT_IDX_LEB);
  193. mst->root_offs = 0;
  194. tmp = ubifs_idx_node_sz(c, 1);
  195. mst->root_len = cpu_to_le32(tmp);
  196. mst->gc_lnum = cpu_to_le32(main_first + DEFAULT_GC_LEB);
  197. mst->ihead_lnum = cpu_to_le32(main_first + DEFAULT_IDX_LEB);
  198. mst->ihead_offs = cpu_to_le32(ALIGN(tmp, c->min_io_size));
  199. mst->index_size = cpu_to_le64(ALIGN(tmp, 8));
  200. mst->lpt_lnum = cpu_to_le32(c->lpt_lnum);
  201. mst->lpt_offs = cpu_to_le32(c->lpt_offs);
  202. mst->nhead_lnum = cpu_to_le32(c->nhead_lnum);
  203. mst->nhead_offs = cpu_to_le32(c->nhead_offs);
  204. mst->ltab_lnum = cpu_to_le32(c->ltab_lnum);
  205. mst->ltab_offs = cpu_to_le32(c->ltab_offs);
  206. mst->lsave_lnum = cpu_to_le32(c->lsave_lnum);
  207. mst->lsave_offs = cpu_to_le32(c->lsave_offs);
  208. mst->lscan_lnum = cpu_to_le32(main_first);
  209. mst->empty_lebs = cpu_to_le32(main_lebs - 2);
  210. mst->idx_lebs = cpu_to_le32(1);
  211. mst->leb_cnt = cpu_to_le32(c->leb_cnt);
  212. ubifs_copy_hash(c, hash_lpt, mst->hash_lpt);
  213. /* Calculate lprops statistics */
  214. tmp64 = main_bytes;
  215. tmp64 -= ALIGN(ubifs_idx_node_sz(c, 1), c->min_io_size);
  216. tmp64 -= ALIGN(UBIFS_INO_NODE_SZ, c->min_io_size);
  217. mst->total_free = cpu_to_le64(tmp64);
  218. tmp64 = ALIGN(ubifs_idx_node_sz(c, 1), c->min_io_size);
  219. ino_waste = ALIGN(UBIFS_INO_NODE_SZ, c->min_io_size) -
  220. UBIFS_INO_NODE_SZ;
  221. tmp64 += ino_waste;
  222. tmp64 -= ALIGN(ubifs_idx_node_sz(c, 1), 8);
  223. mst->total_dirty = cpu_to_le64(tmp64);
  224. /* The indexing LEB does not contribute to dark space */
  225. tmp64 = ((long long)(c->main_lebs - 1) * c->dark_wm);
  226. mst->total_dark = cpu_to_le64(tmp64);
  227. mst->total_used = cpu_to_le64(UBIFS_INO_NODE_SZ);
  228. dbg_gen("default master node created at LEB %d:0", UBIFS_MST_LNUM);
  229. /* Create the root indexing node */
  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. dbg_gen("default root indexing node created LEB %d:0",
  240. main_first + DEFAULT_IDX_LEB);
  241. /* Create default root inode */
  242. ino_key_init_flash(c, &ino->key, UBIFS_ROOT_INO);
  243. ino->ch.node_type = UBIFS_INO_NODE;
  244. ino->creat_sqnum = cpu_to_le64(++c->max_sqnum);
  245. ino->nlink = cpu_to_le32(2);
  246. ktime_get_coarse_real_ts64(&ts);
  247. tmp_le64 = cpu_to_le64(ts.tv_sec);
  248. ino->atime_sec = tmp_le64;
  249. ino->ctime_sec = tmp_le64;
  250. ino->mtime_sec = tmp_le64;
  251. ino->atime_nsec = 0;
  252. ino->ctime_nsec = 0;
  253. ino->mtime_nsec = 0;
  254. ino->mode = cpu_to_le32(S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO);
  255. ino->size = cpu_to_le64(UBIFS_INO_NODE_SZ);
  256. /* Set compression enabled by default */
  257. ino->flags = cpu_to_le32(UBIFS_COMPR_FL);
  258. dbg_gen("root inode created at LEB %d:0",
  259. main_first + DEFAULT_DATA_LEB);
  260. /*
  261. * The first node in the log has to be the commit start node. This is
  262. * always the case during normal file-system operation. Write a fake
  263. * commit start node to the log.
  264. */
  265. cs->ch.node_type = UBIFS_CS_NODE;
  266. err = ubifs_write_node_hmac(c, sup, UBIFS_SB_NODE_SZ, 0, 0,
  267. offsetof(struct ubifs_sb_node, hmac));
  268. if (err)
  269. goto out;
  270. err = ubifs_write_node(c, ino, UBIFS_INO_NODE_SZ,
  271. main_first + DEFAULT_DATA_LEB, 0);
  272. if (err)
  273. goto out;
  274. ubifs_node_calc_hash(c, ino, hash);
  275. ubifs_copy_hash(c, hash, ubifs_branch_hash(c, br));
  276. err = ubifs_write_node(c, idx, idx_node_size, main_first + DEFAULT_IDX_LEB, 0);
  277. if (err)
  278. goto out;
  279. ubifs_node_calc_hash(c, idx, hash);
  280. ubifs_copy_hash(c, hash, mst->hash_root_idx);
  281. err = ubifs_write_node_hmac(c, mst, UBIFS_MST_NODE_SZ, UBIFS_MST_LNUM, 0,
  282. offsetof(struct ubifs_mst_node, hmac));
  283. if (err)
  284. goto out;
  285. err = ubifs_write_node_hmac(c, mst, UBIFS_MST_NODE_SZ, UBIFS_MST_LNUM + 1,
  286. 0, offsetof(struct ubifs_mst_node, hmac));
  287. if (err)
  288. goto out;
  289. err = ubifs_write_node(c, cs, UBIFS_CS_NODE_SZ, UBIFS_LOG_LNUM, 0);
  290. if (err)
  291. goto out;
  292. ubifs_msg(c, "default file-system created");
  293. err = 0;
  294. out:
  295. kfree(sup);
  296. kfree(mst);
  297. kfree(idx);
  298. kfree(ino);
  299. kfree(cs);
  300. return err;
  301. }
  302. /**
  303. * validate_sb - validate superblock node.
  304. * @c: UBIFS file-system description object
  305. * @sup: superblock node
  306. *
  307. * This function validates superblock node @sup. Since most of data was read
  308. * from the superblock and stored in @c, the function validates fields in @c
  309. * instead. Returns zero in case of success and %-EINVAL in case of validation
  310. * failure.
  311. */
  312. static int validate_sb(struct ubifs_info *c, struct ubifs_sb_node *sup)
  313. {
  314. long long max_bytes;
  315. int err = 1, min_leb_cnt;
  316. if (!c->key_hash) {
  317. err = 2;
  318. goto failed;
  319. }
  320. if (sup->key_fmt != UBIFS_SIMPLE_KEY_FMT) {
  321. err = 3;
  322. goto failed;
  323. }
  324. if (le32_to_cpu(sup->min_io_size) != c->min_io_size) {
  325. ubifs_err(c, "min. I/O unit mismatch: %d in superblock, %d real",
  326. le32_to_cpu(sup->min_io_size), c->min_io_size);
  327. goto failed;
  328. }
  329. if (le32_to_cpu(sup->leb_size) != c->leb_size) {
  330. ubifs_err(c, "LEB size mismatch: %d in superblock, %d real",
  331. le32_to_cpu(sup->leb_size), c->leb_size);
  332. goto failed;
  333. }
  334. if (c->log_lebs < UBIFS_MIN_LOG_LEBS ||
  335. c->lpt_lebs < UBIFS_MIN_LPT_LEBS ||
  336. c->orph_lebs < UBIFS_MIN_ORPH_LEBS ||
  337. c->main_lebs < UBIFS_MIN_MAIN_LEBS) {
  338. err = 4;
  339. goto failed;
  340. }
  341. /*
  342. * Calculate minimum allowed amount of main area LEBs. This is very
  343. * similar to %UBIFS_MIN_LEB_CNT, but we take into account real what we
  344. * have just read from the superblock.
  345. */
  346. min_leb_cnt = UBIFS_SB_LEBS + UBIFS_MST_LEBS + c->log_lebs;
  347. min_leb_cnt += c->lpt_lebs + c->orph_lebs + c->jhead_cnt + 6;
  348. if (c->leb_cnt < min_leb_cnt || c->leb_cnt > c->vi.size) {
  349. ubifs_err(c, "bad LEB count: %d in superblock, %d on UBI volume, %d minimum required",
  350. c->leb_cnt, c->vi.size, min_leb_cnt);
  351. goto failed;
  352. }
  353. if (c->max_leb_cnt < c->leb_cnt) {
  354. ubifs_err(c, "max. LEB count %d less than LEB count %d",
  355. c->max_leb_cnt, c->leb_cnt);
  356. goto failed;
  357. }
  358. if (c->main_lebs < UBIFS_MIN_MAIN_LEBS) {
  359. ubifs_err(c, "too few main LEBs count %d, must be at least %d",
  360. c->main_lebs, UBIFS_MIN_MAIN_LEBS);
  361. goto failed;
  362. }
  363. max_bytes = (long long)c->leb_size * UBIFS_MIN_BUD_LEBS;
  364. if (c->max_bud_bytes < max_bytes) {
  365. ubifs_err(c, "too small journal (%lld bytes), must be at least %lld bytes",
  366. c->max_bud_bytes, max_bytes);
  367. goto failed;
  368. }
  369. max_bytes = (long long)c->leb_size * c->main_lebs;
  370. if (c->max_bud_bytes > max_bytes) {
  371. ubifs_err(c, "too large journal size (%lld bytes), only %lld bytes available in the main area",
  372. c->max_bud_bytes, max_bytes);
  373. goto failed;
  374. }
  375. if (c->jhead_cnt < NONDATA_JHEADS_CNT + 1 ||
  376. c->jhead_cnt > NONDATA_JHEADS_CNT + UBIFS_MAX_JHEADS) {
  377. err = 9;
  378. goto failed;
  379. }
  380. if (c->fanout < UBIFS_MIN_FANOUT ||
  381. ubifs_idx_node_sz(c, c->fanout) > c->leb_size) {
  382. err = 10;
  383. goto failed;
  384. }
  385. if (c->lsave_cnt < 0 || (c->lsave_cnt > DEFAULT_LSAVE_CNT &&
  386. c->lsave_cnt > c->max_leb_cnt - UBIFS_SB_LEBS - UBIFS_MST_LEBS -
  387. c->log_lebs - c->lpt_lebs - c->orph_lebs)) {
  388. err = 11;
  389. goto failed;
  390. }
  391. if (UBIFS_SB_LEBS + UBIFS_MST_LEBS + c->log_lebs + c->lpt_lebs +
  392. c->orph_lebs + c->main_lebs != c->leb_cnt) {
  393. err = 12;
  394. goto failed;
  395. }
  396. if (c->default_compr >= UBIFS_COMPR_TYPES_CNT) {
  397. err = 13;
  398. goto failed;
  399. }
  400. if (c->rp_size < 0 || max_bytes < c->rp_size) {
  401. err = 14;
  402. goto failed;
  403. }
  404. if (le32_to_cpu(sup->time_gran) > 1000000000 ||
  405. le32_to_cpu(sup->time_gran) < 1) {
  406. err = 15;
  407. goto failed;
  408. }
  409. if (!c->double_hash && c->fmt_version >= 5) {
  410. err = 16;
  411. goto failed;
  412. }
  413. if (c->encrypted && c->fmt_version < 5) {
  414. err = 17;
  415. goto failed;
  416. }
  417. return 0;
  418. failed:
  419. ubifs_err(c, "bad superblock, error %d", err);
  420. ubifs_dump_node(c, sup);
  421. return -EINVAL;
  422. }
  423. /**
  424. * ubifs_read_sb_node - read superblock node.
  425. * @c: UBIFS file-system description object
  426. *
  427. * This function returns a pointer to the superblock node or a negative error
  428. * code. Note, the user of this function is responsible of kfree()'ing the
  429. * returned superblock buffer.
  430. */
  431. static struct ubifs_sb_node *ubifs_read_sb_node(struct ubifs_info *c)
  432. {
  433. struct ubifs_sb_node *sup;
  434. int err;
  435. sup = kmalloc(ALIGN(UBIFS_SB_NODE_SZ, c->min_io_size), GFP_NOFS);
  436. if (!sup)
  437. return ERR_PTR(-ENOMEM);
  438. err = ubifs_read_node(c, sup, UBIFS_SB_NODE, UBIFS_SB_NODE_SZ,
  439. UBIFS_SB_LNUM, 0);
  440. if (err) {
  441. kfree(sup);
  442. return ERR_PTR(err);
  443. }
  444. return sup;
  445. }
  446. static int authenticate_sb_node(struct ubifs_info *c,
  447. const struct ubifs_sb_node *sup)
  448. {
  449. unsigned int sup_flags = le32_to_cpu(sup->flags);
  450. u8 hmac_wkm[UBIFS_HMAC_ARR_SZ];
  451. int authenticated = !!(sup_flags & UBIFS_FLG_AUTHENTICATION);
  452. int hash_algo;
  453. int err;
  454. if (c->authenticated && !authenticated) {
  455. ubifs_err(c, "authenticated FS forced, but found FS without authentication");
  456. return -EINVAL;
  457. }
  458. if (!c->authenticated && authenticated) {
  459. ubifs_err(c, "authenticated FS found, but no key given");
  460. return -EINVAL;
  461. }
  462. ubifs_msg(c, "Mounting in %sauthenticated mode",
  463. c->authenticated ? "" : "un");
  464. if (!c->authenticated)
  465. return 0;
  466. if (!IS_ENABLED(CONFIG_UBIFS_FS_AUTHENTICATION))
  467. return -EOPNOTSUPP;
  468. hash_algo = le16_to_cpu(sup->hash_algo);
  469. if (hash_algo >= HASH_ALGO__LAST) {
  470. ubifs_err(c, "superblock uses unknown hash algo %d",
  471. hash_algo);
  472. return -EINVAL;
  473. }
  474. if (strcmp(hash_algo_name[hash_algo], c->auth_hash_name)) {
  475. ubifs_err(c, "This filesystem uses %s for hashing,"
  476. " but %s is specified", hash_algo_name[hash_algo],
  477. c->auth_hash_name);
  478. return -EINVAL;
  479. }
  480. /*
  481. * The super block node can either be authenticated by a HMAC or
  482. * by a signature in a ubifs_sig_node directly following the
  483. * super block node to support offline image creation.
  484. */
  485. if (ubifs_hmac_zero(c, sup->hmac)) {
  486. err = ubifs_sb_verify_signature(c, sup);
  487. } else {
  488. err = ubifs_hmac_wkm(c, hmac_wkm);
  489. if (err)
  490. return err;
  491. if (ubifs_check_hmac(c, hmac_wkm, sup->hmac_wkm)) {
  492. ubifs_err(c, "provided key does not fit");
  493. return -ENOKEY;
  494. }
  495. err = ubifs_node_verify_hmac(c, sup, sizeof(*sup),
  496. offsetof(struct ubifs_sb_node,
  497. hmac));
  498. }
  499. if (err)
  500. ubifs_err(c, "Failed to authenticate superblock: %d", err);
  501. return err;
  502. }
  503. /**
  504. * ubifs_write_sb_node - write superblock node.
  505. * @c: UBIFS file-system description object
  506. * @sup: superblock node read with 'ubifs_read_sb_node()'
  507. *
  508. * This function returns %0 on success and a negative error code on failure.
  509. */
  510. int ubifs_write_sb_node(struct ubifs_info *c, struct ubifs_sb_node *sup)
  511. {
  512. int len = ALIGN(UBIFS_SB_NODE_SZ, c->min_io_size);
  513. int err;
  514. err = ubifs_prepare_node_hmac(c, sup, UBIFS_SB_NODE_SZ,
  515. offsetof(struct ubifs_sb_node, hmac), 1);
  516. if (err)
  517. return err;
  518. return ubifs_leb_change(c, UBIFS_SB_LNUM, sup, len);
  519. }
  520. /**
  521. * ubifs_read_superblock - read superblock.
  522. * @c: UBIFS file-system description object
  523. *
  524. * This function finds, reads and checks the superblock. If an empty UBI volume
  525. * is being mounted, this function creates default superblock. Returns zero in
  526. * case of success, and a negative error code in case of failure.
  527. */
  528. int ubifs_read_superblock(struct ubifs_info *c)
  529. {
  530. int err, sup_flags;
  531. struct ubifs_sb_node *sup;
  532. if (c->empty) {
  533. err = create_default_filesystem(c);
  534. if (err)
  535. return err;
  536. }
  537. sup = ubifs_read_sb_node(c);
  538. if (IS_ERR(sup))
  539. return PTR_ERR(sup);
  540. c->sup_node = sup;
  541. c->fmt_version = le32_to_cpu(sup->fmt_version);
  542. c->ro_compat_version = le32_to_cpu(sup->ro_compat_version);
  543. /*
  544. * The software supports all previous versions but not future versions,
  545. * due to the unavailability of time-travelling equipment.
  546. */
  547. if (c->fmt_version > UBIFS_FORMAT_VERSION) {
  548. ubifs_assert(c, !c->ro_media || c->ro_mount);
  549. if (!c->ro_mount ||
  550. c->ro_compat_version > UBIFS_RO_COMPAT_VERSION) {
  551. ubifs_err(c, "on-flash format version is w%d/r%d, but software only supports up to version w%d/r%d",
  552. c->fmt_version, c->ro_compat_version,
  553. UBIFS_FORMAT_VERSION,
  554. UBIFS_RO_COMPAT_VERSION);
  555. if (c->ro_compat_version <= UBIFS_RO_COMPAT_VERSION) {
  556. ubifs_msg(c, "only R/O mounting is possible");
  557. err = -EROFS;
  558. } else
  559. err = -EINVAL;
  560. goto out;
  561. }
  562. /*
  563. * The FS is mounted R/O, and the media format is
  564. * R/O-compatible with the UBIFS implementation, so we can
  565. * mount.
  566. */
  567. c->rw_incompat = 1;
  568. }
  569. if (c->fmt_version < 3) {
  570. ubifs_err(c, "on-flash format version %d is not supported",
  571. c->fmt_version);
  572. err = -EINVAL;
  573. goto out;
  574. }
  575. switch (sup->key_hash) {
  576. case UBIFS_KEY_HASH_R5:
  577. c->key_hash = key_r5_hash;
  578. c->key_hash_type = UBIFS_KEY_HASH_R5;
  579. break;
  580. case UBIFS_KEY_HASH_TEST:
  581. c->key_hash = key_test_hash;
  582. c->key_hash_type = UBIFS_KEY_HASH_TEST;
  583. break;
  584. }
  585. c->key_fmt = sup->key_fmt;
  586. switch (c->key_fmt) {
  587. case UBIFS_SIMPLE_KEY_FMT:
  588. c->key_len = UBIFS_SK_LEN;
  589. break;
  590. default:
  591. ubifs_err(c, "unsupported key format");
  592. err = -EINVAL;
  593. goto out;
  594. }
  595. c->leb_cnt = le32_to_cpu(sup->leb_cnt);
  596. c->max_leb_cnt = le32_to_cpu(sup->max_leb_cnt);
  597. c->max_bud_bytes = le64_to_cpu(sup->max_bud_bytes);
  598. c->log_lebs = le32_to_cpu(sup->log_lebs);
  599. c->lpt_lebs = le32_to_cpu(sup->lpt_lebs);
  600. c->orph_lebs = le32_to_cpu(sup->orph_lebs);
  601. c->jhead_cnt = le32_to_cpu(sup->jhead_cnt) + NONDATA_JHEADS_CNT;
  602. c->fanout = le32_to_cpu(sup->fanout);
  603. c->lsave_cnt = le32_to_cpu(sup->lsave_cnt);
  604. c->rp_size = le64_to_cpu(sup->rp_size);
  605. c->rp_uid = make_kuid(&init_user_ns, le32_to_cpu(sup->rp_uid));
  606. c->rp_gid = make_kgid(&init_user_ns, le32_to_cpu(sup->rp_gid));
  607. sup_flags = le32_to_cpu(sup->flags);
  608. if (!c->mount_opts.override_compr)
  609. c->default_compr = le16_to_cpu(sup->default_compr);
  610. c->vfs_sb->s_time_gran = le32_to_cpu(sup->time_gran);
  611. memcpy(&c->uuid, &sup->uuid, 16);
  612. c->big_lpt = !!(sup_flags & UBIFS_FLG_BIGLPT);
  613. c->space_fixup = !!(sup_flags & UBIFS_FLG_SPACE_FIXUP);
  614. c->double_hash = !!(sup_flags & UBIFS_FLG_DOUBLE_HASH);
  615. c->encrypted = !!(sup_flags & UBIFS_FLG_ENCRYPTION);
  616. err = authenticate_sb_node(c, sup);
  617. if (err)
  618. goto out;
  619. if ((sup_flags & ~UBIFS_FLG_MASK) != 0) {
  620. ubifs_err(c, "Unknown feature flags found: %#x",
  621. sup_flags & ~UBIFS_FLG_MASK);
  622. err = -EINVAL;
  623. goto out;
  624. }
  625. if (!IS_ENABLED(CONFIG_FS_ENCRYPTION) && c->encrypted) {
  626. ubifs_err(c, "file system contains encrypted files but UBIFS"
  627. " was built without crypto support.");
  628. err = -EINVAL;
  629. goto out;
  630. }
  631. /* Automatically increase file system size to the maximum size */
  632. if (c->leb_cnt < c->vi.size && c->leb_cnt < c->max_leb_cnt) {
  633. int old_leb_cnt = c->leb_cnt;
  634. c->leb_cnt = min_t(int, c->max_leb_cnt, c->vi.size);
  635. sup->leb_cnt = cpu_to_le32(c->leb_cnt);
  636. c->superblock_need_write = 1;
  637. dbg_mnt("Auto resizing from %d LEBs to %d LEBs",
  638. old_leb_cnt, c->leb_cnt);
  639. }
  640. c->log_bytes = (long long)c->log_lebs * c->leb_size;
  641. c->log_last = UBIFS_LOG_LNUM + c->log_lebs - 1;
  642. c->lpt_first = UBIFS_LOG_LNUM + c->log_lebs;
  643. c->lpt_last = c->lpt_first + c->lpt_lebs - 1;
  644. c->orph_first = c->lpt_last + 1;
  645. c->orph_last = c->orph_first + c->orph_lebs - 1;
  646. c->main_lebs = c->leb_cnt - UBIFS_SB_LEBS - UBIFS_MST_LEBS;
  647. c->main_lebs -= c->log_lebs + c->lpt_lebs + c->orph_lebs;
  648. c->main_first = c->leb_cnt - c->main_lebs;
  649. err = validate_sb(c, sup);
  650. out:
  651. return err;
  652. }
  653. /**
  654. * fixup_leb - fixup/unmap an LEB containing free space.
  655. * @c: UBIFS file-system description object
  656. * @lnum: the LEB number to fix up
  657. * @len: number of used bytes in LEB (starting at offset 0)
  658. *
  659. * This function reads the contents of the given LEB number @lnum, then fixes
  660. * it up, so that empty min. I/O units in the end of LEB are actually erased on
  661. * flash (rather than being just all-0xff real data). If the LEB is completely
  662. * empty, it is simply unmapped.
  663. */
  664. static int fixup_leb(struct ubifs_info *c, int lnum, int len)
  665. {
  666. int err;
  667. ubifs_assert(c, len >= 0);
  668. ubifs_assert(c, len % c->min_io_size == 0);
  669. ubifs_assert(c, len < c->leb_size);
  670. if (len == 0) {
  671. dbg_mnt("unmap empty LEB %d", lnum);
  672. return ubifs_leb_unmap(c, lnum);
  673. }
  674. dbg_mnt("fixup LEB %d, data len %d", lnum, len);
  675. err = ubifs_leb_read(c, lnum, c->sbuf, 0, len, 1);
  676. if (err)
  677. return err;
  678. return ubifs_leb_change(c, lnum, c->sbuf, len);
  679. }
  680. /**
  681. * fixup_free_space - find & remap all LEBs containing free space.
  682. * @c: UBIFS file-system description object
  683. *
  684. * This function walks through all LEBs in the filesystem and fiexes up those
  685. * containing free/empty space.
  686. */
  687. static int fixup_free_space(struct ubifs_info *c)
  688. {
  689. int lnum, err = 0;
  690. struct ubifs_lprops *lprops;
  691. ubifs_get_lprops(c);
  692. /* Fixup LEBs in the master area */
  693. for (lnum = UBIFS_MST_LNUM; lnum < UBIFS_LOG_LNUM; lnum++) {
  694. err = fixup_leb(c, lnum, c->mst_offs + c->mst_node_alsz);
  695. if (err)
  696. goto out;
  697. }
  698. /* Unmap unused log LEBs */
  699. lnum = ubifs_next_log_lnum(c, c->lhead_lnum);
  700. while (lnum != c->ltail_lnum) {
  701. err = fixup_leb(c, lnum, 0);
  702. if (err)
  703. goto out;
  704. lnum = ubifs_next_log_lnum(c, lnum);
  705. }
  706. /*
  707. * Fixup the log head which contains the only a CS node at the
  708. * beginning.
  709. */
  710. err = fixup_leb(c, c->lhead_lnum,
  711. ALIGN(UBIFS_CS_NODE_SZ, c->min_io_size));
  712. if (err)
  713. goto out;
  714. /* Fixup LEBs in the LPT area */
  715. for (lnum = c->lpt_first; lnum <= c->lpt_last; lnum++) {
  716. int free = c->ltab[lnum - c->lpt_first].free;
  717. if (free > 0) {
  718. err = fixup_leb(c, lnum, c->leb_size - free);
  719. if (err)
  720. goto out;
  721. }
  722. }
  723. /* Unmap LEBs in the orphans area */
  724. for (lnum = c->orph_first; lnum <= c->orph_last; lnum++) {
  725. err = fixup_leb(c, lnum, 0);
  726. if (err)
  727. goto out;
  728. }
  729. /* Fixup LEBs in the main area */
  730. for (lnum = c->main_first; lnum < c->leb_cnt; lnum++) {
  731. lprops = ubifs_lpt_lookup(c, lnum);
  732. if (IS_ERR(lprops)) {
  733. err = PTR_ERR(lprops);
  734. goto out;
  735. }
  736. if (lprops->free > 0) {
  737. err = fixup_leb(c, lnum, c->leb_size - lprops->free);
  738. if (err)
  739. goto out;
  740. }
  741. }
  742. out:
  743. ubifs_release_lprops(c);
  744. return err;
  745. }
  746. /**
  747. * ubifs_fixup_free_space - find & fix all LEBs with free space.
  748. * @c: UBIFS file-system description object
  749. *
  750. * This function fixes up LEBs containing free space on first mount, if the
  751. * appropriate flag was set when the FS was created. Each LEB with one or more
  752. * empty min. I/O unit (i.e. free-space-count > 0) is re-written, to make sure
  753. * the free space is actually erased. E.g., this is necessary for some NAND
  754. * chips, since the free space may have been programmed like real "0xff" data
  755. * (generating a non-0xff ECC), causing future writes to the not-really-erased
  756. * NAND pages to behave badly. After the space is fixed up, the superblock flag
  757. * is cleared, so that this is skipped for all future mounts.
  758. */
  759. int ubifs_fixup_free_space(struct ubifs_info *c)
  760. {
  761. int err;
  762. struct ubifs_sb_node *sup = c->sup_node;
  763. ubifs_assert(c, c->space_fixup);
  764. ubifs_assert(c, !c->ro_mount);
  765. ubifs_msg(c, "start fixing up free space");
  766. err = fixup_free_space(c);
  767. if (err)
  768. return err;
  769. /* Free-space fixup is no longer required */
  770. c->space_fixup = 0;
  771. sup->flags &= cpu_to_le32(~UBIFS_FLG_SPACE_FIXUP);
  772. c->superblock_need_write = 1;
  773. ubifs_msg(c, "free space fixup complete");
  774. return err;
  775. }
  776. int ubifs_enable_encryption(struct ubifs_info *c)
  777. {
  778. int err;
  779. struct ubifs_sb_node *sup = c->sup_node;
  780. if (!IS_ENABLED(CONFIG_FS_ENCRYPTION))
  781. return -EOPNOTSUPP;
  782. if (c->encrypted)
  783. return 0;
  784. if (c->ro_mount || c->ro_media)
  785. return -EROFS;
  786. if (c->fmt_version < 5) {
  787. ubifs_err(c, "on-flash format version 5 is needed for encryption");
  788. return -EINVAL;
  789. }
  790. sup->flags |= cpu_to_le32(UBIFS_FLG_ENCRYPTION);
  791. err = ubifs_write_sb_node(c, sup);
  792. if (!err)
  793. c->encrypted = 1;
  794. return err;
  795. }