io.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /*
  2. * This file is part of UBIFS.
  3. *
  4. * Copyright (C) 2006-2008 Nokia Corporation.
  5. * Copyright (C) 2006, 2007 University of Szeged, Hungary
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License version 2 as published by
  9. * the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  14. * more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along with
  17. * this program; if not, write to the Free Software Foundation, Inc., 51
  18. * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  19. *
  20. * Authors: Artem Bityutskiy (Битюцкий Артём)
  21. * Adrian Hunter
  22. * Zoltan Sogor
  23. */
  24. /*
  25. * This file implements UBIFS I/O subsystem which provides various I/O-related
  26. * helper functions (reading/writing/checking/validating nodes) and implements
  27. * write-buffering support. Write buffers help to save space which otherwise
  28. * would have been wasted for padding to the nearest minimal I/O unit boundary.
  29. * Instead, data first goes to the write-buffer and is flushed when the
  30. * buffer is full or when it is not used for some time (by timer). This is
  31. * similar to the mechanism is used by JFFS2.
  32. *
  33. * Write-buffers are defined by 'struct ubifs_wbuf' objects and protected by
  34. * mutexes defined inside these objects. Since sometimes upper-level code
  35. * has to lock the write-buffer (e.g. journal space reservation code), many
  36. * functions related to write-buffers have "nolock" suffix which means that the
  37. * caller has to lock the write-buffer before calling this function.
  38. *
  39. * UBIFS stores nodes at 64 bit-aligned addresses. If the node length is not
  40. * aligned, UBIFS starts the next node from the aligned address, and the padded
  41. * bytes may contain any rubbish. In other words, UBIFS does not put padding
  42. * bytes in those small gaps. Common headers of nodes store real node lengths,
  43. * not aligned lengths. Indexing nodes also store real lengths in branches.
  44. *
  45. * UBIFS uses padding when it pads to the next min. I/O unit. In this case it
  46. * uses padding nodes or padding bytes, if the padding node does not fit.
  47. *
  48. * All UBIFS nodes are protected by CRC checksums and UBIFS checks all nodes
  49. * every time they are read from the flash media.
  50. */
  51. #include "ubifs.h"
  52. /**
  53. * ubifs_ro_mode - switch UBIFS to read read-only mode.
  54. * @c: UBIFS file-system description object
  55. * @err: error code which is the reason of switching to R/O mode
  56. */
  57. void ubifs_ro_mode(struct ubifs_info *c, int err)
  58. {
  59. if (!c->ro_media) {
  60. c->ro_media = 1;
  61. c->no_chk_data_crc = 0;
  62. ubifs_warn("switched to read-only mode, error %d", err);
  63. dbg_dump_stack();
  64. }
  65. }
  66. /**
  67. * ubifs_check_node - check node.
  68. * @c: UBIFS file-system description object
  69. * @buf: node to check
  70. * @lnum: logical eraseblock number
  71. * @offs: offset within the logical eraseblock
  72. * @quiet: print no messages
  73. * @must_chk_crc: indicates whether to always check the CRC
  74. *
  75. * This function checks node magic number and CRC checksum. This function also
  76. * validates node length to prevent UBIFS from becoming crazy when an attacker
  77. * feeds it a file-system image with incorrect nodes. For example, too large
  78. * node length in the common header could cause UBIFS to read memory outside of
  79. * allocated buffer when checking the CRC checksum.
  80. *
  81. * This function may skip data nodes CRC checking if @c->no_chk_data_crc is
  82. * true, which is controlled by corresponding UBIFS mount option. However, if
  83. * @must_chk_crc is true, then @c->no_chk_data_crc is ignored and CRC is
  84. * checked. Similarly, if @c->always_chk_crc is true, @c->no_chk_data_crc is
  85. * ignored and CRC is checked.
  86. *
  87. * This function returns zero in case of success and %-EUCLEAN in case of bad
  88. * CRC or magic.
  89. */
  90. int ubifs_check_node(const struct ubifs_info *c, const void *buf, int lnum,
  91. int offs, int quiet, int must_chk_crc)
  92. {
  93. int err = -EINVAL, type, node_len;
  94. uint32_t crc, node_crc, magic;
  95. const struct ubifs_ch *ch = buf;
  96. ubifs_assert(lnum >= 0 && lnum < c->leb_cnt && offs >= 0);
  97. ubifs_assert(!(offs & 7) && offs < c->leb_size);
  98. magic = le32_to_cpu(ch->magic);
  99. if (magic != UBIFS_NODE_MAGIC) {
  100. if (!quiet)
  101. ubifs_err("bad magic %#08x, expected %#08x",
  102. magic, UBIFS_NODE_MAGIC);
  103. err = -EUCLEAN;
  104. goto out;
  105. }
  106. type = ch->node_type;
  107. if (type < 0 || type >= UBIFS_NODE_TYPES_CNT) {
  108. if (!quiet)
  109. ubifs_err("bad node type %d", type);
  110. goto out;
  111. }
  112. node_len = le32_to_cpu(ch->len);
  113. if (node_len + offs > c->leb_size)
  114. goto out_len;
  115. if (c->ranges[type].max_len == 0) {
  116. if (node_len != c->ranges[type].len)
  117. goto out_len;
  118. } else if (node_len < c->ranges[type].min_len ||
  119. node_len > c->ranges[type].max_len)
  120. goto out_len;
  121. if (!must_chk_crc && type == UBIFS_DATA_NODE && !c->always_chk_crc &&
  122. c->no_chk_data_crc)
  123. return 0;
  124. crc = crc32(UBIFS_CRC32_INIT, buf + 8, node_len - 8);
  125. node_crc = le32_to_cpu(ch->crc);
  126. if (crc != node_crc) {
  127. if (!quiet)
  128. ubifs_err("bad CRC: calculated %#08x, read %#08x",
  129. crc, node_crc);
  130. err = -EUCLEAN;
  131. goto out;
  132. }
  133. return 0;
  134. out_len:
  135. if (!quiet)
  136. ubifs_err("bad node length %d", node_len);
  137. out:
  138. if (!quiet) {
  139. ubifs_err("bad node at LEB %d:%d", lnum, offs);
  140. dbg_dump_node(c, buf);
  141. dbg_dump_stack();
  142. }
  143. return err;
  144. }
  145. /**
  146. * ubifs_pad - pad flash space.
  147. * @c: UBIFS file-system description object
  148. * @buf: buffer to put padding to
  149. * @pad: how many bytes to pad
  150. *
  151. * The flash media obliges us to write only in chunks of %c->min_io_size and
  152. * when we have to write less data we add padding node to the write-buffer and
  153. * pad it to the next minimal I/O unit's boundary. Padding nodes help when the
  154. * media is being scanned. If the amount of wasted space is not enough to fit a
  155. * padding node which takes %UBIFS_PAD_NODE_SZ bytes, we write padding bytes
  156. * pattern (%UBIFS_PADDING_BYTE).
  157. *
  158. * Padding nodes are also used to fill gaps when the "commit-in-gaps" method is
  159. * used.
  160. */
  161. void ubifs_pad(const struct ubifs_info *c, void *buf, int pad)
  162. {
  163. uint32_t crc;
  164. ubifs_assert(pad >= 0 && !(pad & 7));
  165. if (pad >= UBIFS_PAD_NODE_SZ) {
  166. struct ubifs_ch *ch = buf;
  167. struct ubifs_pad_node *pad_node = buf;
  168. ch->magic = cpu_to_le32(UBIFS_NODE_MAGIC);
  169. ch->node_type = UBIFS_PAD_NODE;
  170. ch->group_type = UBIFS_NO_NODE_GROUP;
  171. ch->padding[0] = ch->padding[1] = 0;
  172. ch->sqnum = 0;
  173. ch->len = cpu_to_le32(UBIFS_PAD_NODE_SZ);
  174. pad -= UBIFS_PAD_NODE_SZ;
  175. pad_node->pad_len = cpu_to_le32(pad);
  176. crc = crc32(UBIFS_CRC32_INIT, buf + 8, UBIFS_PAD_NODE_SZ - 8);
  177. ch->crc = cpu_to_le32(crc);
  178. memset(buf + UBIFS_PAD_NODE_SZ, 0, pad);
  179. } else if (pad > 0)
  180. /* Too little space, padding node won't fit */
  181. memset(buf, UBIFS_PADDING_BYTE, pad);
  182. }
  183. /**
  184. * next_sqnum - get next sequence number.
  185. * @c: UBIFS file-system description object
  186. */
  187. static unsigned long long next_sqnum(struct ubifs_info *c)
  188. {
  189. unsigned long long sqnum;
  190. spin_lock(&c->cnt_lock);
  191. sqnum = ++c->max_sqnum;
  192. spin_unlock(&c->cnt_lock);
  193. if (unlikely(sqnum >= SQNUM_WARN_WATERMARK)) {
  194. if (sqnum >= SQNUM_WATERMARK) {
  195. ubifs_err("sequence number overflow %llu, end of life",
  196. sqnum);
  197. ubifs_ro_mode(c, -EINVAL);
  198. }
  199. ubifs_warn("running out of sequence numbers, end of life soon");
  200. }
  201. return sqnum;
  202. }
  203. /**
  204. * ubifs_prepare_node - prepare node to be written to flash.
  205. * @c: UBIFS file-system description object
  206. * @node: the node to pad
  207. * @len: node length
  208. * @pad: if the buffer has to be padded
  209. *
  210. * This function prepares node at @node to be written to the media - it
  211. * calculates node CRC, fills the common header, and adds proper padding up to
  212. * the next minimum I/O unit if @pad is not zero.
  213. */
  214. void ubifs_prepare_node(struct ubifs_info *c, void *node, int len, int pad)
  215. {
  216. uint32_t crc;
  217. struct ubifs_ch *ch = node;
  218. unsigned long long sqnum = next_sqnum(c);
  219. ubifs_assert(len >= UBIFS_CH_SZ);
  220. ch->magic = cpu_to_le32(UBIFS_NODE_MAGIC);
  221. ch->len = cpu_to_le32(len);
  222. ch->group_type = UBIFS_NO_NODE_GROUP;
  223. ch->sqnum = cpu_to_le64(sqnum);
  224. ch->padding[0] = ch->padding[1] = 0;
  225. crc = crc32(UBIFS_CRC32_INIT, node + 8, len - 8);
  226. ch->crc = cpu_to_le32(crc);
  227. if (pad) {
  228. len = ALIGN(len, 8);
  229. pad = ALIGN(len, c->min_io_size) - len;
  230. ubifs_pad(c, node + len, pad);
  231. }
  232. }
  233. /**
  234. * ubifs_read_node - read node.
  235. * @c: UBIFS file-system description object
  236. * @buf: buffer to read to
  237. * @type: node type
  238. * @len: node length (not aligned)
  239. * @lnum: logical eraseblock number
  240. * @offs: offset within the logical eraseblock
  241. *
  242. * This function reads a node of known type and and length, checks it and
  243. * stores in @buf. Returns zero in case of success, %-EUCLEAN if CRC mismatched
  244. * and a negative error code in case of failure.
  245. */
  246. int ubifs_read_node(const struct ubifs_info *c, void *buf, int type, int len,
  247. int lnum, int offs)
  248. {
  249. int err, l;
  250. struct ubifs_ch *ch = buf;
  251. dbg_io("LEB %d:%d, %s, length %d", lnum, offs, dbg_ntype(type), len);
  252. ubifs_assert(lnum >= 0 && lnum < c->leb_cnt && offs >= 0);
  253. ubifs_assert(len >= UBIFS_CH_SZ && offs + len <= c->leb_size);
  254. ubifs_assert(!(offs & 7) && offs < c->leb_size);
  255. ubifs_assert(type >= 0 && type < UBIFS_NODE_TYPES_CNT);
  256. err = ubi_read(c->ubi, lnum, buf, offs, len);
  257. if (err && err != -EBADMSG) {
  258. ubifs_err("cannot read node %d from LEB %d:%d, error %d",
  259. type, lnum, offs, err);
  260. return err;
  261. }
  262. if (type != ch->node_type) {
  263. ubifs_err("bad node type (%d but expected %d)",
  264. ch->node_type, type);
  265. goto out;
  266. }
  267. err = ubifs_check_node(c, buf, lnum, offs, 0, 0);
  268. if (err) {
  269. ubifs_err("expected node type %d", type);
  270. return err;
  271. }
  272. l = le32_to_cpu(ch->len);
  273. if (l != len) {
  274. ubifs_err("bad node length %d, expected %d", l, len);
  275. goto out;
  276. }
  277. return 0;
  278. out:
  279. ubifs_err("bad node at LEB %d:%d", lnum, offs);
  280. dbg_dump_node(c, buf);
  281. dbg_dump_stack();
  282. return -EINVAL;
  283. }