sb.c 26 KB

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