sb.c 23 KB

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