sb.c 23 KB

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