sb.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819
  1. // SPDX-License-Identifier: GPL-2.0+
  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. #ifndef __UBOOT__
  17. #include <linux/slab.h>
  18. #include <linux/random.h>
  19. #include <linux/math64.h>
  20. #else
  21. #include <linux/compat.h>
  22. #include <linux/err.h>
  23. #include <ubi_uboot.h>
  24. #include <linux/stat.h>
  25. #endif
  26. /*
  27. * Default journal size in logical eraseblocks as a percent of total
  28. * flash size.
  29. */
  30. #define DEFAULT_JNL_PERCENT 5
  31. /* Default maximum journal size in bytes */
  32. #define DEFAULT_MAX_JNL (32*1024*1024)
  33. /* Default indexing tree fanout */
  34. #define DEFAULT_FANOUT 8
  35. /* Default number of data journal heads */
  36. #define DEFAULT_JHEADS_CNT 1
  37. /* Default positions of different LEBs in the main area */
  38. #define DEFAULT_IDX_LEB 0
  39. #define DEFAULT_DATA_LEB 1
  40. #define DEFAULT_GC_LEB 2
  41. /* Default number of LEB numbers in LPT's save table */
  42. #define DEFAULT_LSAVE_CNT 256
  43. /* Default reserved pool size as a percent of maximum free space */
  44. #define DEFAULT_RP_PERCENT 5
  45. /* The default maximum size of reserved pool in bytes */
  46. #define DEFAULT_MAX_RP_SIZE (5*1024*1024)
  47. /* Default time granularity in nanoseconds */
  48. #define DEFAULT_TIME_GRAN 1000000000
  49. #ifndef __UBOOT__
  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. long long tmp64, main_bytes;
  70. __le64 tmp_le64;
  71. /* Some functions called from here depend on the @c->key_len filed */
  72. c->key_len = UBIFS_SK_LEN;
  73. /*
  74. * First of all, we have to calculate default file-system geometry -
  75. * log size, journal size, etc.
  76. */
  77. if (c->leb_cnt < 0x7FFFFFFF / DEFAULT_JNL_PERCENT)
  78. /* We can first multiply then divide and have no overflow */
  79. jnl_lebs = c->leb_cnt * DEFAULT_JNL_PERCENT / 100;
  80. else
  81. jnl_lebs = (c->leb_cnt / 100) * DEFAULT_JNL_PERCENT;
  82. if (jnl_lebs < UBIFS_MIN_JNL_LEBS)
  83. jnl_lebs = UBIFS_MIN_JNL_LEBS;
  84. if (jnl_lebs * c->leb_size > DEFAULT_MAX_JNL)
  85. jnl_lebs = DEFAULT_MAX_JNL / c->leb_size;
  86. /*
  87. * The log should be large enough to fit reference nodes for all bud
  88. * LEBs. Because buds do not have to start from the beginning of LEBs
  89. * (half of the LEB may contain committed data), the log should
  90. * generally be larger, make it twice as large.
  91. */
  92. tmp = 2 * (c->ref_node_alsz * jnl_lebs) + c->leb_size - 1;
  93. log_lebs = tmp / c->leb_size;
  94. /* Plus one LEB reserved for commit */
  95. log_lebs += 1;
  96. if (c->leb_cnt - min_leb_cnt > 8) {
  97. /* And some extra space to allow writes while committing */
  98. log_lebs += 1;
  99. min_leb_cnt += 1;
  100. }
  101. max_buds = jnl_lebs - log_lebs;
  102. if (max_buds < UBIFS_MIN_BUD_LEBS)
  103. max_buds = UBIFS_MIN_BUD_LEBS;
  104. /*
  105. * Orphan nodes are stored in a separate area. One node can store a lot
  106. * of orphan inode numbers, but when new orphan comes we just add a new
  107. * orphan node. At some point the nodes are consolidated into one
  108. * orphan node.
  109. */
  110. orph_lebs = UBIFS_MIN_ORPH_LEBS;
  111. if (c->leb_cnt - min_leb_cnt > 1)
  112. /*
  113. * For debugging purposes it is better to have at least 2
  114. * orphan LEBs, because the orphan subsystem would need to do
  115. * consolidations and would be stressed more.
  116. */
  117. orph_lebs += 1;
  118. main_lebs = c->leb_cnt - UBIFS_SB_LEBS - UBIFS_MST_LEBS - log_lebs;
  119. main_lebs -= orph_lebs;
  120. lpt_first = UBIFS_LOG_LNUM + log_lebs;
  121. c->lsave_cnt = DEFAULT_LSAVE_CNT;
  122. c->max_leb_cnt = c->leb_cnt;
  123. err = ubifs_create_dflt_lpt(c, &main_lebs, lpt_first, &lpt_lebs,
  124. &big_lpt);
  125. if (err)
  126. return err;
  127. dbg_gen("LEB Properties Tree created (LEBs %d-%d)", lpt_first,
  128. lpt_first + lpt_lebs - 1);
  129. main_first = c->leb_cnt - main_lebs;
  130. /* Create default superblock */
  131. tmp = ALIGN(UBIFS_SB_NODE_SZ, c->min_io_size);
  132. sup = kzalloc(tmp, GFP_KERNEL);
  133. if (!sup)
  134. return -ENOMEM;
  135. tmp64 = (long long)max_buds * c->leb_size;
  136. if (big_lpt)
  137. sup_flags |= UBIFS_FLG_BIGLPT;
  138. sup->ch.node_type = UBIFS_SB_NODE;
  139. sup->key_hash = UBIFS_KEY_HASH_R5;
  140. sup->flags = cpu_to_le32(sup_flags);
  141. sup->min_io_size = cpu_to_le32(c->min_io_size);
  142. sup->leb_size = cpu_to_le32(c->leb_size);
  143. sup->leb_cnt = cpu_to_le32(c->leb_cnt);
  144. sup->max_leb_cnt = cpu_to_le32(c->max_leb_cnt);
  145. sup->max_bud_bytes = cpu_to_le64(tmp64);
  146. sup->log_lebs = cpu_to_le32(log_lebs);
  147. sup->lpt_lebs = cpu_to_le32(lpt_lebs);
  148. sup->orph_lebs = cpu_to_le32(orph_lebs);
  149. sup->jhead_cnt = cpu_to_le32(DEFAULT_JHEADS_CNT);
  150. sup->fanout = cpu_to_le32(DEFAULT_FANOUT);
  151. sup->lsave_cnt = cpu_to_le32(c->lsave_cnt);
  152. sup->fmt_version = cpu_to_le32(UBIFS_FORMAT_VERSION);
  153. sup->time_gran = cpu_to_le32(DEFAULT_TIME_GRAN);
  154. if (c->mount_opts.override_compr)
  155. sup->default_compr = cpu_to_le16(c->mount_opts.compr_type);
  156. else
  157. sup->default_compr = cpu_to_le16(UBIFS_COMPR_LZO);
  158. generate_random_uuid(sup->uuid);
  159. main_bytes = (long long)main_lebs * c->leb_size;
  160. tmp64 = div_u64(main_bytes * DEFAULT_RP_PERCENT, 100);
  161. if (tmp64 > DEFAULT_MAX_RP_SIZE)
  162. tmp64 = DEFAULT_MAX_RP_SIZE;
  163. sup->rp_size = cpu_to_le64(tmp64);
  164. sup->ro_compat_version = cpu_to_le32(UBIFS_RO_COMPAT_VERSION);
  165. err = ubifs_write_node(c, sup, UBIFS_SB_NODE_SZ, 0, 0);
  166. kfree(sup);
  167. if (err)
  168. return err;
  169. dbg_gen("default superblock created at LEB 0:0");
  170. /* Create default master node */
  171. mst = kzalloc(c->mst_node_alsz, GFP_KERNEL);
  172. if (!mst)
  173. return -ENOMEM;
  174. mst->ch.node_type = UBIFS_MST_NODE;
  175. mst->log_lnum = cpu_to_le32(UBIFS_LOG_LNUM);
  176. mst->highest_inum = cpu_to_le64(UBIFS_FIRST_INO);
  177. mst->cmt_no = 0;
  178. mst->root_lnum = cpu_to_le32(main_first + DEFAULT_IDX_LEB);
  179. mst->root_offs = 0;
  180. tmp = ubifs_idx_node_sz(c, 1);
  181. mst->root_len = cpu_to_le32(tmp);
  182. mst->gc_lnum = cpu_to_le32(main_first + DEFAULT_GC_LEB);
  183. mst->ihead_lnum = cpu_to_le32(main_first + DEFAULT_IDX_LEB);
  184. mst->ihead_offs = cpu_to_le32(ALIGN(tmp, c->min_io_size));
  185. mst->index_size = cpu_to_le64(ALIGN(tmp, 8));
  186. mst->lpt_lnum = cpu_to_le32(c->lpt_lnum);
  187. mst->lpt_offs = cpu_to_le32(c->lpt_offs);
  188. mst->nhead_lnum = cpu_to_le32(c->nhead_lnum);
  189. mst->nhead_offs = cpu_to_le32(c->nhead_offs);
  190. mst->ltab_lnum = cpu_to_le32(c->ltab_lnum);
  191. mst->ltab_offs = cpu_to_le32(c->ltab_offs);
  192. mst->lsave_lnum = cpu_to_le32(c->lsave_lnum);
  193. mst->lsave_offs = cpu_to_le32(c->lsave_offs);
  194. mst->lscan_lnum = cpu_to_le32(main_first);
  195. mst->empty_lebs = cpu_to_le32(main_lebs - 2);
  196. mst->idx_lebs = cpu_to_le32(1);
  197. mst->leb_cnt = cpu_to_le32(c->leb_cnt);
  198. /* Calculate lprops statistics */
  199. tmp64 = main_bytes;
  200. tmp64 -= ALIGN(ubifs_idx_node_sz(c, 1), c->min_io_size);
  201. tmp64 -= ALIGN(UBIFS_INO_NODE_SZ, c->min_io_size);
  202. mst->total_free = cpu_to_le64(tmp64);
  203. tmp64 = ALIGN(ubifs_idx_node_sz(c, 1), c->min_io_size);
  204. ino_waste = ALIGN(UBIFS_INO_NODE_SZ, c->min_io_size) -
  205. UBIFS_INO_NODE_SZ;
  206. tmp64 += ino_waste;
  207. tmp64 -= ALIGN(ubifs_idx_node_sz(c, 1), 8);
  208. mst->total_dirty = cpu_to_le64(tmp64);
  209. /* The indexing LEB does not contribute to dark space */
  210. tmp64 = ((long long)(c->main_lebs - 1) * c->dark_wm);
  211. mst->total_dark = cpu_to_le64(tmp64);
  212. mst->total_used = cpu_to_le64(UBIFS_INO_NODE_SZ);
  213. err = ubifs_write_node(c, mst, UBIFS_MST_NODE_SZ, UBIFS_MST_LNUM, 0);
  214. if (err) {
  215. kfree(mst);
  216. return err;
  217. }
  218. err = ubifs_write_node(c, mst, UBIFS_MST_NODE_SZ, UBIFS_MST_LNUM + 1,
  219. 0);
  220. kfree(mst);
  221. if (err)
  222. return err;
  223. dbg_gen("default master node created at LEB %d:0", UBIFS_MST_LNUM);
  224. /* Create the root indexing node */
  225. tmp = ubifs_idx_node_sz(c, 1);
  226. idx = kzalloc(ALIGN(tmp, c->min_io_size), GFP_KERNEL);
  227. if (!idx)
  228. return -ENOMEM;
  229. c->key_fmt = UBIFS_SIMPLE_KEY_FMT;
  230. c->key_hash = key_r5_hash;
  231. idx->ch.node_type = UBIFS_IDX_NODE;
  232. idx->child_cnt = cpu_to_le16(1);
  233. ino_key_init(c, &key, UBIFS_ROOT_INO);
  234. br = ubifs_idx_branch(c, idx, 0);
  235. key_write_idx(c, &key, &br->key);
  236. br->lnum = cpu_to_le32(main_first + DEFAULT_DATA_LEB);
  237. br->len = cpu_to_le32(UBIFS_INO_NODE_SZ);
  238. err = ubifs_write_node(c, idx, tmp, main_first + DEFAULT_IDX_LEB, 0);
  239. kfree(idx);
  240. if (err)
  241. return err;
  242. dbg_gen("default root indexing node created LEB %d:0",
  243. main_first + DEFAULT_IDX_LEB);
  244. /* Create default root inode */
  245. tmp = ALIGN(UBIFS_INO_NODE_SZ, c->min_io_size);
  246. ino = kzalloc(tmp, GFP_KERNEL);
  247. if (!ino)
  248. return -ENOMEM;
  249. ino_key_init_flash(c, &ino->key, UBIFS_ROOT_INO);
  250. ino->ch.node_type = UBIFS_INO_NODE;
  251. ino->creat_sqnum = cpu_to_le64(++c->max_sqnum);
  252. ino->nlink = cpu_to_le32(2);
  253. tmp_le64 = cpu_to_le64(CURRENT_TIME_SEC.tv_sec);
  254. ino->atime_sec = tmp_le64;
  255. ino->ctime_sec = tmp_le64;
  256. ino->mtime_sec = tmp_le64;
  257. ino->atime_nsec = 0;
  258. ino->ctime_nsec = 0;
  259. ino->mtime_nsec = 0;
  260. ino->mode = cpu_to_le32(S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO);
  261. ino->size = cpu_to_le64(UBIFS_INO_NODE_SZ);
  262. /* Set compression enabled by default */
  263. ino->flags = cpu_to_le32(UBIFS_COMPR_FL);
  264. err = ubifs_write_node(c, ino, UBIFS_INO_NODE_SZ,
  265. main_first + DEFAULT_DATA_LEB, 0);
  266. kfree(ino);
  267. if (err)
  268. return err;
  269. dbg_gen("root inode created at LEB %d:0",
  270. main_first + DEFAULT_DATA_LEB);
  271. /*
  272. * The first node in the log has to be the commit start node. This is
  273. * always the case during normal file-system operation. Write a fake
  274. * commit start node to the log.
  275. */
  276. tmp = ALIGN(UBIFS_CS_NODE_SZ, c->min_io_size);
  277. cs = kzalloc(tmp, GFP_KERNEL);
  278. if (!cs)
  279. return -ENOMEM;
  280. cs->ch.node_type = UBIFS_CS_NODE;
  281. err = ubifs_write_node(c, cs, UBIFS_CS_NODE_SZ, UBIFS_LOG_LNUM, 0);
  282. kfree(cs);
  283. if (err)
  284. return err;
  285. ubifs_msg(c, "default file-system created");
  286. return 0;
  287. }
  288. #endif
  289. /**
  290. * validate_sb - validate superblock node.
  291. * @c: UBIFS file-system description object
  292. * @sup: superblock node
  293. *
  294. * This function validates superblock node @sup. Since most of data was read
  295. * from the superblock and stored in @c, the function validates fields in @c
  296. * instead. Returns zero in case of success and %-EINVAL in case of validation
  297. * failure.
  298. */
  299. static int validate_sb(struct ubifs_info *c, struct ubifs_sb_node *sup)
  300. {
  301. long long max_bytes;
  302. int err = 1, min_leb_cnt;
  303. if (!c->key_hash) {
  304. err = 2;
  305. goto failed;
  306. }
  307. if (sup->key_fmt != UBIFS_SIMPLE_KEY_FMT) {
  308. err = 3;
  309. goto failed;
  310. }
  311. if (le32_to_cpu(sup->min_io_size) != c->min_io_size) {
  312. ubifs_err(c, "min. I/O unit mismatch: %d in superblock, %d real",
  313. le32_to_cpu(sup->min_io_size), c->min_io_size);
  314. goto failed;
  315. }
  316. if (le32_to_cpu(sup->leb_size) != c->leb_size) {
  317. ubifs_err(c, "LEB size mismatch: %d in superblock, %d real",
  318. le32_to_cpu(sup->leb_size), c->leb_size);
  319. goto failed;
  320. }
  321. if (c->log_lebs < UBIFS_MIN_LOG_LEBS ||
  322. c->lpt_lebs < UBIFS_MIN_LPT_LEBS ||
  323. c->orph_lebs < UBIFS_MIN_ORPH_LEBS ||
  324. c->main_lebs < UBIFS_MIN_MAIN_LEBS) {
  325. err = 4;
  326. goto failed;
  327. }
  328. /*
  329. * Calculate minimum allowed amount of main area LEBs. This is very
  330. * similar to %UBIFS_MIN_LEB_CNT, but we take into account real what we
  331. * have just read from the superblock.
  332. */
  333. min_leb_cnt = UBIFS_SB_LEBS + UBIFS_MST_LEBS + c->log_lebs;
  334. min_leb_cnt += c->lpt_lebs + c->orph_lebs + c->jhead_cnt + 6;
  335. if (c->leb_cnt < min_leb_cnt || c->leb_cnt > c->vi.size) {
  336. ubifs_err(c, "bad LEB count: %d in superblock, %d on UBI volume, %d minimum required",
  337. c->leb_cnt, c->vi.size, min_leb_cnt);
  338. goto failed;
  339. }
  340. if (c->max_leb_cnt < c->leb_cnt) {
  341. ubifs_err(c, "max. LEB count %d less than LEB count %d",
  342. c->max_leb_cnt, c->leb_cnt);
  343. goto failed;
  344. }
  345. if (c->main_lebs < UBIFS_MIN_MAIN_LEBS) {
  346. ubifs_err(c, "too few main LEBs count %d, must be at least %d",
  347. c->main_lebs, UBIFS_MIN_MAIN_LEBS);
  348. goto failed;
  349. }
  350. max_bytes = (long long)c->leb_size * UBIFS_MIN_BUD_LEBS;
  351. if (c->max_bud_bytes < max_bytes) {
  352. ubifs_err(c, "too small journal (%lld bytes), must be at least %lld bytes",
  353. c->max_bud_bytes, max_bytes);
  354. goto failed;
  355. }
  356. max_bytes = (long long)c->leb_size * c->main_lebs;
  357. if (c->max_bud_bytes > max_bytes) {
  358. ubifs_err(c, "too large journal size (%lld bytes), only %lld bytes available in the main area",
  359. c->max_bud_bytes, max_bytes);
  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 >= UBIFS_COMPR_TYPES_CNT) {
  384. err = 13;
  385. goto failed;
  386. }
  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(c, "bad superblock, error %d", err);
  399. ubifs_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. Note, the user of this function is responsible of kfree()'ing the
  408. * returned superblock buffer.
  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);
  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. #ifndef __UBOOT__
  452. err = create_default_filesystem(c);
  453. if (err)
  454. return err;
  455. #else
  456. printf("No UBIFS filesystem found!\n");
  457. return -1;
  458. #endif
  459. }
  460. sup = ubifs_read_sb_node(c);
  461. if (IS_ERR(sup))
  462. return PTR_ERR(sup);
  463. c->fmt_version = le32_to_cpu(sup->fmt_version);
  464. c->ro_compat_version = le32_to_cpu(sup->ro_compat_version);
  465. /*
  466. * The software supports all previous versions but not future versions,
  467. * due to the unavailability of time-travelling equipment.
  468. */
  469. if (c->fmt_version > UBIFS_FORMAT_VERSION) {
  470. ubifs_assert(!c->ro_media || c->ro_mount);
  471. if (!c->ro_mount ||
  472. c->ro_compat_version > UBIFS_RO_COMPAT_VERSION) {
  473. ubifs_err(c, "on-flash format version is w%d/r%d, but software only supports up to version w%d/r%d",
  474. c->fmt_version, c->ro_compat_version,
  475. UBIFS_FORMAT_VERSION,
  476. UBIFS_RO_COMPAT_VERSION);
  477. if (c->ro_compat_version <= UBIFS_RO_COMPAT_VERSION) {
  478. ubifs_msg(c, "only R/O mounting is possible");
  479. err = -EROFS;
  480. } else
  481. err = -EINVAL;
  482. goto out;
  483. }
  484. /*
  485. * The FS is mounted R/O, and the media format is
  486. * R/O-compatible with the UBIFS implementation, so we can
  487. * mount.
  488. */
  489. c->rw_incompat = 1;
  490. }
  491. if (c->fmt_version < 3) {
  492. ubifs_err(c, "on-flash format version %d is not supported",
  493. c->fmt_version);
  494. err = -EINVAL;
  495. goto out;
  496. }
  497. switch (sup->key_hash) {
  498. case UBIFS_KEY_HASH_R5:
  499. c->key_hash = key_r5_hash;
  500. c->key_hash_type = UBIFS_KEY_HASH_R5;
  501. break;
  502. case UBIFS_KEY_HASH_TEST:
  503. c->key_hash = key_test_hash;
  504. c->key_hash_type = UBIFS_KEY_HASH_TEST;
  505. break;
  506. };
  507. c->key_fmt = sup->key_fmt;
  508. switch (c->key_fmt) {
  509. case UBIFS_SIMPLE_KEY_FMT:
  510. c->key_len = UBIFS_SK_LEN;
  511. break;
  512. default:
  513. ubifs_err(c, "unsupported key format");
  514. err = -EINVAL;
  515. goto out;
  516. }
  517. c->leb_cnt = le32_to_cpu(sup->leb_cnt);
  518. c->max_leb_cnt = le32_to_cpu(sup->max_leb_cnt);
  519. c->max_bud_bytes = le64_to_cpu(sup->max_bud_bytes);
  520. c->log_lebs = le32_to_cpu(sup->log_lebs);
  521. c->lpt_lebs = le32_to_cpu(sup->lpt_lebs);
  522. c->orph_lebs = le32_to_cpu(sup->orph_lebs);
  523. c->jhead_cnt = le32_to_cpu(sup->jhead_cnt) + NONDATA_JHEADS_CNT;
  524. c->fanout = le32_to_cpu(sup->fanout);
  525. c->lsave_cnt = le32_to_cpu(sup->lsave_cnt);
  526. c->rp_size = le64_to_cpu(sup->rp_size);
  527. #ifndef __UBOOT__
  528. c->rp_uid = make_kuid(&init_user_ns, le32_to_cpu(sup->rp_uid));
  529. c->rp_gid = make_kgid(&init_user_ns, le32_to_cpu(sup->rp_gid));
  530. #else
  531. c->rp_uid.val = le32_to_cpu(sup->rp_uid);
  532. c->rp_gid.val = le32_to_cpu(sup->rp_gid);
  533. #endif
  534. sup_flags = le32_to_cpu(sup->flags);
  535. if (!c->mount_opts.override_compr)
  536. c->default_compr = le16_to_cpu(sup->default_compr);
  537. c->vfs_sb->s_time_gran = le32_to_cpu(sup->time_gran);
  538. memcpy(&c->uuid, &sup->uuid, 16);
  539. c->big_lpt = !!(sup_flags & UBIFS_FLG_BIGLPT);
  540. c->space_fixup = !!(sup_flags & UBIFS_FLG_SPACE_FIXUP);
  541. /* Automatically increase file system size to the maximum size */
  542. c->old_leb_cnt = c->leb_cnt;
  543. if (c->leb_cnt < c->vi.size && c->leb_cnt < c->max_leb_cnt) {
  544. c->leb_cnt = min_t(int, c->max_leb_cnt, c->vi.size);
  545. if (c->ro_mount)
  546. dbg_mnt("Auto resizing (ro) from %d LEBs to %d LEBs",
  547. c->old_leb_cnt, c->leb_cnt);
  548. #ifndef __UBOOT__
  549. else {
  550. dbg_mnt("Auto resizing (sb) from %d LEBs to %d LEBs",
  551. c->old_leb_cnt, c->leb_cnt);
  552. sup->leb_cnt = cpu_to_le32(c->leb_cnt);
  553. err = ubifs_write_sb_node(c, sup);
  554. if (err)
  555. goto out;
  556. c->old_leb_cnt = c->leb_cnt;
  557. }
  558. #endif
  559. }
  560. c->log_bytes = (long long)c->log_lebs * c->leb_size;
  561. c->log_last = UBIFS_LOG_LNUM + c->log_lebs - 1;
  562. c->lpt_first = UBIFS_LOG_LNUM + c->log_lebs;
  563. c->lpt_last = c->lpt_first + c->lpt_lebs - 1;
  564. c->orph_first = c->lpt_last + 1;
  565. c->orph_last = c->orph_first + c->orph_lebs - 1;
  566. c->main_lebs = c->leb_cnt - UBIFS_SB_LEBS - UBIFS_MST_LEBS;
  567. c->main_lebs -= c->log_lebs + c->lpt_lebs + c->orph_lebs;
  568. c->main_first = c->leb_cnt - c->main_lebs;
  569. err = validate_sb(c, sup);
  570. out:
  571. kfree(sup);
  572. return err;
  573. }
  574. /**
  575. * fixup_leb - fixup/unmap an LEB containing free space.
  576. * @c: UBIFS file-system description object
  577. * @lnum: the LEB number to fix up
  578. * @len: number of used bytes in LEB (starting at offset 0)
  579. *
  580. * This function reads the contents of the given LEB number @lnum, then fixes
  581. * it up, so that empty min. I/O units in the end of LEB are actually erased on
  582. * flash (rather than being just all-0xff real data). If the LEB is completely
  583. * empty, it is simply unmapped.
  584. */
  585. static int fixup_leb(struct ubifs_info *c, int lnum, int len)
  586. {
  587. int err;
  588. ubifs_assert(len >= 0);
  589. ubifs_assert(len % c->min_io_size == 0);
  590. ubifs_assert(len < c->leb_size);
  591. if (len == 0) {
  592. dbg_mnt("unmap empty LEB %d", lnum);
  593. return ubifs_leb_unmap(c, lnum);
  594. }
  595. dbg_mnt("fixup LEB %d, data len %d", lnum, len);
  596. err = ubifs_leb_read(c, lnum, c->sbuf, 0, len, 1);
  597. if (err)
  598. return err;
  599. return ubifs_leb_change(c, lnum, c->sbuf, len);
  600. }
  601. /**
  602. * fixup_free_space - find & remap all LEBs containing free space.
  603. * @c: UBIFS file-system description object
  604. *
  605. * This function walks through all LEBs in the filesystem and fiexes up those
  606. * containing free/empty space.
  607. */
  608. static int fixup_free_space(struct ubifs_info *c)
  609. {
  610. int lnum, err = 0;
  611. struct ubifs_lprops *lprops;
  612. ubifs_get_lprops(c);
  613. /* Fixup LEBs in the master area */
  614. for (lnum = UBIFS_MST_LNUM; lnum < UBIFS_LOG_LNUM; lnum++) {
  615. err = fixup_leb(c, lnum, c->mst_offs + c->mst_node_alsz);
  616. if (err)
  617. goto out;
  618. }
  619. /* Unmap unused log LEBs */
  620. lnum = ubifs_next_log_lnum(c, c->lhead_lnum);
  621. while (lnum != c->ltail_lnum) {
  622. err = fixup_leb(c, lnum, 0);
  623. if (err)
  624. goto out;
  625. lnum = ubifs_next_log_lnum(c, lnum);
  626. }
  627. /*
  628. * Fixup the log head which contains the only a CS node at the
  629. * beginning.
  630. */
  631. err = fixup_leb(c, c->lhead_lnum,
  632. ALIGN(UBIFS_CS_NODE_SZ, c->min_io_size));
  633. if (err)
  634. goto out;
  635. /* Fixup LEBs in the LPT area */
  636. for (lnum = c->lpt_first; lnum <= c->lpt_last; lnum++) {
  637. int free = c->ltab[lnum - c->lpt_first].free;
  638. if (free > 0) {
  639. err = fixup_leb(c, lnum, c->leb_size - free);
  640. if (err)
  641. goto out;
  642. }
  643. }
  644. /* Unmap LEBs in the orphans area */
  645. for (lnum = c->orph_first; lnum <= c->orph_last; lnum++) {
  646. err = fixup_leb(c, lnum, 0);
  647. if (err)
  648. goto out;
  649. }
  650. /* Fixup LEBs in the main area */
  651. for (lnum = c->main_first; lnum < c->leb_cnt; lnum++) {
  652. lprops = ubifs_lpt_lookup(c, lnum);
  653. if (IS_ERR(lprops)) {
  654. err = PTR_ERR(lprops);
  655. goto out;
  656. }
  657. if (lprops->free > 0) {
  658. err = fixup_leb(c, lnum, c->leb_size - lprops->free);
  659. if (err)
  660. goto out;
  661. }
  662. }
  663. out:
  664. ubifs_release_lprops(c);
  665. return err;
  666. }
  667. /**
  668. * ubifs_fixup_free_space - find & fix all LEBs with free space.
  669. * @c: UBIFS file-system description object
  670. *
  671. * This function fixes up LEBs containing free space on first mount, if the
  672. * appropriate flag was set when the FS was created. Each LEB with one or more
  673. * empty min. I/O unit (i.e. free-space-count > 0) is re-written, to make sure
  674. * the free space is actually erased. E.g., this is necessary for some NAND
  675. * chips, since the free space may have been programmed like real "0xff" data
  676. * (generating a non-0xff ECC), causing future writes to the not-really-erased
  677. * NAND pages to behave badly. After the space is fixed up, the superblock flag
  678. * is cleared, so that this is skipped for all future mounts.
  679. */
  680. int ubifs_fixup_free_space(struct ubifs_info *c)
  681. {
  682. int err;
  683. struct ubifs_sb_node *sup;
  684. ubifs_assert(c->space_fixup);
  685. ubifs_assert(!c->ro_mount);
  686. ubifs_msg(c, "start fixing up free space");
  687. err = fixup_free_space(c);
  688. if (err)
  689. return err;
  690. sup = ubifs_read_sb_node(c);
  691. if (IS_ERR(sup))
  692. return PTR_ERR(sup);
  693. /* Free-space fixup is no longer required */
  694. c->space_fixup = 0;
  695. sup->flags &= cpu_to_le32(~UBIFS_FLG_SPACE_FIXUP);
  696. err = ubifs_write_sb_node(c, sup);
  697. kfree(sup);
  698. if (err)
  699. return err;
  700. ubifs_msg(c, "free space fixup complete");
  701. return err;
  702. }