sb.c 23 KB

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