sb.c 23 KB

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