sb.c 25 KB

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