io.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059
  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. * UBIFS distinguishes between minimum write size (@c->min_io_size) and maximum
  34. * write size (@c->max_write_size). The latter is the maximum amount of bytes
  35. * the underlying flash is able to program at a time, and writing in
  36. * @c->max_write_size units should presumably be faster. Obviously,
  37. * @c->min_io_size <= @c->max_write_size. Write-buffers are of
  38. * @c->max_write_size bytes in size for maximum performance. However, when a
  39. * write-buffer is flushed, only the portion of it (aligned to @c->min_io_size
  40. * boundary) which contains data is written, not the whole write-buffer,
  41. * because this is more space-efficient.
  42. *
  43. * This optimization adds few complications to the code. Indeed, on the one
  44. * hand, we want to write in optimal @c->max_write_size bytes chunks, which
  45. * also means aligning writes at the @c->max_write_size bytes offsets. On the
  46. * other hand, we do not want to waste space when synchronizing the write
  47. * buffer, so during synchronization we writes in smaller chunks. And this makes
  48. * the next write offset to be not aligned to @c->max_write_size bytes. So the
  49. * have to make sure that the write-buffer offset (@wbuf->offs) becomes aligned
  50. * to @c->max_write_size bytes again. We do this by temporarily shrinking
  51. * write-buffer size (@wbuf->size).
  52. *
  53. * Write-buffers are defined by 'struct ubifs_wbuf' objects and protected by
  54. * mutexes defined inside these objects. Since sometimes upper-level code
  55. * has to lock the write-buffer (e.g. journal space reservation code), many
  56. * functions related to write-buffers have "nolock" suffix which means that the
  57. * caller has to lock the write-buffer before calling this function.
  58. *
  59. * UBIFS stores nodes at 64 bit-aligned addresses. If the node length is not
  60. * aligned, UBIFS starts the next node from the aligned address, and the padded
  61. * bytes may contain any rubbish. In other words, UBIFS does not put padding
  62. * bytes in those small gaps. Common headers of nodes store real node lengths,
  63. * not aligned lengths. Indexing nodes also store real lengths in branches.
  64. *
  65. * UBIFS uses padding when it pads to the next min. I/O unit. In this case it
  66. * uses padding nodes or padding bytes, if the padding node does not fit.
  67. *
  68. * All UBIFS nodes are protected by CRC checksums and UBIFS checks CRC when
  69. * they are read from the flash media.
  70. */
  71. #include <linux/crc32.h>
  72. #include <linux/slab.h>
  73. #include "ubifs.h"
  74. /**
  75. * ubifs_ro_mode - switch UBIFS to read read-only mode.
  76. * @c: UBIFS file-system description object
  77. * @err: error code which is the reason of switching to R/O mode
  78. */
  79. void ubifs_ro_mode(struct ubifs_info *c, int err)
  80. {
  81. if (!c->ro_error) {
  82. c->ro_error = 1;
  83. c->no_chk_data_crc = 0;
  84. c->vfs_sb->s_flags |= MS_RDONLY;
  85. ubifs_warn("switched to read-only mode, error %d", err);
  86. dbg_dump_stack();
  87. }
  88. }
  89. /**
  90. * ubifs_check_node - check node.
  91. * @c: UBIFS file-system description object
  92. * @buf: node to check
  93. * @lnum: logical eraseblock number
  94. * @offs: offset within the logical eraseblock
  95. * @quiet: print no messages
  96. * @must_chk_crc: indicates whether to always check the CRC
  97. *
  98. * This function checks node magic number and CRC checksum. This function also
  99. * validates node length to prevent UBIFS from becoming crazy when an attacker
  100. * feeds it a file-system image with incorrect nodes. For example, too large
  101. * node length in the common header could cause UBIFS to read memory outside of
  102. * allocated buffer when checking the CRC checksum.
  103. *
  104. * This function may skip data nodes CRC checking if @c->no_chk_data_crc is
  105. * true, which is controlled by corresponding UBIFS mount option. However, if
  106. * @must_chk_crc is true, then @c->no_chk_data_crc is ignored and CRC is
  107. * checked. Similarly, if @c->mounting or @c->remounting_rw is true (we are
  108. * mounting or re-mounting to R/W mode), @c->no_chk_data_crc is ignored and CRC
  109. * is checked. This is because during mounting or re-mounting from R/O mode to
  110. * R/W mode we may read journal nodes (when replying the journal or doing the
  111. * recovery) and the journal nodes may potentially be corrupted, so checking is
  112. * required.
  113. *
  114. * This function returns zero in case of success and %-EUCLEAN in case of bad
  115. * CRC or magic.
  116. */
  117. int ubifs_check_node(const struct ubifs_info *c, const void *buf, int lnum,
  118. int offs, int quiet, int must_chk_crc)
  119. {
  120. int err = -EINVAL, type, node_len;
  121. uint32_t crc, node_crc, magic;
  122. const struct ubifs_ch *ch = buf;
  123. ubifs_assert(lnum >= 0 && lnum < c->leb_cnt && offs >= 0);
  124. ubifs_assert(!(offs & 7) && offs < c->leb_size);
  125. magic = le32_to_cpu(ch->magic);
  126. if (magic != UBIFS_NODE_MAGIC) {
  127. if (!quiet)
  128. ubifs_err("bad magic %#08x, expected %#08x",
  129. magic, UBIFS_NODE_MAGIC);
  130. err = -EUCLEAN;
  131. goto out;
  132. }
  133. type = ch->node_type;
  134. if (type < 0 || type >= UBIFS_NODE_TYPES_CNT) {
  135. if (!quiet)
  136. ubifs_err("bad node type %d", type);
  137. goto out;
  138. }
  139. node_len = le32_to_cpu(ch->len);
  140. if (node_len + offs > c->leb_size)
  141. goto out_len;
  142. if (c->ranges[type].max_len == 0) {
  143. if (node_len != c->ranges[type].len)
  144. goto out_len;
  145. } else if (node_len < c->ranges[type].min_len ||
  146. node_len > c->ranges[type].max_len)
  147. goto out_len;
  148. if (!must_chk_crc && type == UBIFS_DATA_NODE && !c->mounting &&
  149. !c->remounting_rw && c->no_chk_data_crc)
  150. return 0;
  151. crc = crc32(UBIFS_CRC32_INIT, buf + 8, node_len - 8);
  152. node_crc = le32_to_cpu(ch->crc);
  153. if (crc != node_crc) {
  154. if (!quiet)
  155. ubifs_err("bad CRC: calculated %#08x, read %#08x",
  156. crc, node_crc);
  157. err = -EUCLEAN;
  158. goto out;
  159. }
  160. return 0;
  161. out_len:
  162. if (!quiet)
  163. ubifs_err("bad node length %d", node_len);
  164. out:
  165. if (!quiet) {
  166. ubifs_err("bad node at LEB %d:%d", lnum, offs);
  167. dbg_dump_node(c, buf);
  168. dbg_dump_stack();
  169. }
  170. return err;
  171. }
  172. /**
  173. * ubifs_pad - pad flash space.
  174. * @c: UBIFS file-system description object
  175. * @buf: buffer to put padding to
  176. * @pad: how many bytes to pad
  177. *
  178. * The flash media obliges us to write only in chunks of %c->min_io_size and
  179. * when we have to write less data we add padding node to the write-buffer and
  180. * pad it to the next minimal I/O unit's boundary. Padding nodes help when the
  181. * media is being scanned. If the amount of wasted space is not enough to fit a
  182. * padding node which takes %UBIFS_PAD_NODE_SZ bytes, we write padding bytes
  183. * pattern (%UBIFS_PADDING_BYTE).
  184. *
  185. * Padding nodes are also used to fill gaps when the "commit-in-gaps" method is
  186. * used.
  187. */
  188. void ubifs_pad(const struct ubifs_info *c, void *buf, int pad)
  189. {
  190. uint32_t crc;
  191. ubifs_assert(pad >= 0 && !(pad & 7));
  192. if (pad >= UBIFS_PAD_NODE_SZ) {
  193. struct ubifs_ch *ch = buf;
  194. struct ubifs_pad_node *pad_node = buf;
  195. ch->magic = cpu_to_le32(UBIFS_NODE_MAGIC);
  196. ch->node_type = UBIFS_PAD_NODE;
  197. ch->group_type = UBIFS_NO_NODE_GROUP;
  198. ch->padding[0] = ch->padding[1] = 0;
  199. ch->sqnum = 0;
  200. ch->len = cpu_to_le32(UBIFS_PAD_NODE_SZ);
  201. pad -= UBIFS_PAD_NODE_SZ;
  202. pad_node->pad_len = cpu_to_le32(pad);
  203. crc = crc32(UBIFS_CRC32_INIT, buf + 8, UBIFS_PAD_NODE_SZ - 8);
  204. ch->crc = cpu_to_le32(crc);
  205. memset(buf + UBIFS_PAD_NODE_SZ, 0, pad);
  206. } else if (pad > 0)
  207. /* Too little space, padding node won't fit */
  208. memset(buf, UBIFS_PADDING_BYTE, pad);
  209. }
  210. /**
  211. * next_sqnum - get next sequence number.
  212. * @c: UBIFS file-system description object
  213. */
  214. static unsigned long long next_sqnum(struct ubifs_info *c)
  215. {
  216. unsigned long long sqnum;
  217. spin_lock(&c->cnt_lock);
  218. sqnum = ++c->max_sqnum;
  219. spin_unlock(&c->cnt_lock);
  220. if (unlikely(sqnum >= SQNUM_WARN_WATERMARK)) {
  221. if (sqnum >= SQNUM_WATERMARK) {
  222. ubifs_err("sequence number overflow %llu, end of life",
  223. sqnum);
  224. ubifs_ro_mode(c, -EINVAL);
  225. }
  226. ubifs_warn("running out of sequence numbers, end of life soon");
  227. }
  228. return sqnum;
  229. }
  230. /**
  231. * ubifs_prepare_node - prepare node to be written to flash.
  232. * @c: UBIFS file-system description object
  233. * @node: the node to pad
  234. * @len: node length
  235. * @pad: if the buffer has to be padded
  236. *
  237. * This function prepares node at @node to be written to the media - it
  238. * calculates node CRC, fills the common header, and adds proper padding up to
  239. * the next minimum I/O unit if @pad is not zero.
  240. */
  241. void ubifs_prepare_node(struct ubifs_info *c, void *node, int len, int pad)
  242. {
  243. uint32_t crc;
  244. struct ubifs_ch *ch = node;
  245. unsigned long long sqnum = next_sqnum(c);
  246. ubifs_assert(len >= UBIFS_CH_SZ);
  247. ch->magic = cpu_to_le32(UBIFS_NODE_MAGIC);
  248. ch->len = cpu_to_le32(len);
  249. ch->group_type = UBIFS_NO_NODE_GROUP;
  250. ch->sqnum = cpu_to_le64(sqnum);
  251. ch->padding[0] = ch->padding[1] = 0;
  252. crc = crc32(UBIFS_CRC32_INIT, node + 8, len - 8);
  253. ch->crc = cpu_to_le32(crc);
  254. if (pad) {
  255. len = ALIGN(len, 8);
  256. pad = ALIGN(len, c->min_io_size) - len;
  257. ubifs_pad(c, node + len, pad);
  258. }
  259. }
  260. /**
  261. * ubifs_prep_grp_node - prepare node of a group to be written to flash.
  262. * @c: UBIFS file-system description object
  263. * @node: the node to pad
  264. * @len: node length
  265. * @last: indicates the last node of the group
  266. *
  267. * This function prepares node at @node to be written to the media - it
  268. * calculates node CRC and fills the common header.
  269. */
  270. void ubifs_prep_grp_node(struct ubifs_info *c, void *node, int len, int last)
  271. {
  272. uint32_t crc;
  273. struct ubifs_ch *ch = node;
  274. unsigned long long sqnum = next_sqnum(c);
  275. ubifs_assert(len >= UBIFS_CH_SZ);
  276. ch->magic = cpu_to_le32(UBIFS_NODE_MAGIC);
  277. ch->len = cpu_to_le32(len);
  278. if (last)
  279. ch->group_type = UBIFS_LAST_OF_NODE_GROUP;
  280. else
  281. ch->group_type = UBIFS_IN_NODE_GROUP;
  282. ch->sqnum = cpu_to_le64(sqnum);
  283. ch->padding[0] = ch->padding[1] = 0;
  284. crc = crc32(UBIFS_CRC32_INIT, node + 8, len - 8);
  285. ch->crc = cpu_to_le32(crc);
  286. }
  287. /**
  288. * wbuf_timer_callback - write-buffer timer callback function.
  289. * @data: timer data (write-buffer descriptor)
  290. *
  291. * This function is called when the write-buffer timer expires.
  292. */
  293. static enum hrtimer_restart wbuf_timer_callback_nolock(struct hrtimer *timer)
  294. {
  295. struct ubifs_wbuf *wbuf = container_of(timer, struct ubifs_wbuf, timer);
  296. dbg_io("jhead %s", dbg_jhead(wbuf->jhead));
  297. wbuf->need_sync = 1;
  298. wbuf->c->need_wbuf_sync = 1;
  299. ubifs_wake_up_bgt(wbuf->c);
  300. return HRTIMER_NORESTART;
  301. }
  302. /**
  303. * new_wbuf_timer - start new write-buffer timer.
  304. * @wbuf: write-buffer descriptor
  305. */
  306. static void new_wbuf_timer_nolock(struct ubifs_wbuf *wbuf)
  307. {
  308. ubifs_assert(!hrtimer_active(&wbuf->timer));
  309. if (wbuf->no_timer)
  310. return;
  311. dbg_io("set timer for jhead %s, %llu-%llu millisecs",
  312. dbg_jhead(wbuf->jhead),
  313. div_u64(ktime_to_ns(wbuf->softlimit), USEC_PER_SEC),
  314. div_u64(ktime_to_ns(wbuf->softlimit) + wbuf->delta,
  315. USEC_PER_SEC));
  316. hrtimer_start_range_ns(&wbuf->timer, wbuf->softlimit, wbuf->delta,
  317. HRTIMER_MODE_REL);
  318. }
  319. /**
  320. * cancel_wbuf_timer - cancel write-buffer timer.
  321. * @wbuf: write-buffer descriptor
  322. */
  323. static void cancel_wbuf_timer_nolock(struct ubifs_wbuf *wbuf)
  324. {
  325. if (wbuf->no_timer)
  326. return;
  327. wbuf->need_sync = 0;
  328. hrtimer_cancel(&wbuf->timer);
  329. }
  330. /**
  331. * ubifs_wbuf_sync_nolock - synchronize write-buffer.
  332. * @wbuf: write-buffer to synchronize
  333. *
  334. * This function synchronizes write-buffer @buf and returns zero in case of
  335. * success or a negative error code in case of failure.
  336. *
  337. * Note, although write-buffers are of @c->max_write_size, this function does
  338. * not necessarily writes all @c->max_write_size bytes to the flash. Instead,
  339. * if the write-buffer is only partially filled with data, only the used part
  340. * of the write-buffer (aligned on @c->min_io_size boundary) is synchronized.
  341. * This way we waste less space.
  342. */
  343. int ubifs_wbuf_sync_nolock(struct ubifs_wbuf *wbuf)
  344. {
  345. struct ubifs_info *c = wbuf->c;
  346. int err, dirt, sync_len;
  347. cancel_wbuf_timer_nolock(wbuf);
  348. if (!wbuf->used || wbuf->lnum == -1)
  349. /* Write-buffer is empty or not seeked */
  350. return 0;
  351. dbg_io("LEB %d:%d, %d bytes, jhead %s",
  352. wbuf->lnum, wbuf->offs, wbuf->used, dbg_jhead(wbuf->jhead));
  353. ubifs_assert(!(wbuf->avail & 7));
  354. ubifs_assert(wbuf->offs + wbuf->size <= c->leb_size);
  355. ubifs_assert(wbuf->size >= c->min_io_size);
  356. ubifs_assert(wbuf->size <= c->max_write_size);
  357. ubifs_assert(wbuf->size % c->min_io_size == 0);
  358. ubifs_assert(!c->ro_media && !c->ro_mount);
  359. if (c->leb_size - wbuf->offs >= c->max_write_size)
  360. ubifs_assert(!((wbuf->offs + wbuf->size) % c->max_write_size ));
  361. if (c->ro_error)
  362. return -EROFS;
  363. /*
  364. * Do not write whole write buffer but write only the minimum necessary
  365. * amount of min. I/O units.
  366. */
  367. sync_len = ALIGN(wbuf->used, c->min_io_size);
  368. dirt = sync_len - wbuf->used;
  369. if (dirt)
  370. ubifs_pad(c, wbuf->buf + wbuf->used, dirt);
  371. err = ubi_leb_write(c->ubi, wbuf->lnum, wbuf->buf, wbuf->offs,
  372. sync_len, wbuf->dtype);
  373. if (err) {
  374. ubifs_err("cannot write %d bytes to LEB %d:%d",
  375. sync_len, wbuf->lnum, wbuf->offs);
  376. dbg_dump_stack();
  377. return err;
  378. }
  379. spin_lock(&wbuf->lock);
  380. wbuf->offs += sync_len;
  381. /*
  382. * Now @wbuf->offs is not necessarily aligned to @c->max_write_size.
  383. * But our goal is to optimize writes and make sure we write in
  384. * @c->max_write_size chunks and to @c->max_write_size-aligned offset.
  385. * Thus, if @wbuf->offs is not aligned to @c->max_write_size now, make
  386. * sure that @wbuf->offs + @wbuf->size is aligned to
  387. * @c->max_write_size. This way we make sure that after next
  388. * write-buffer flush we are again at the optimal offset (aligned to
  389. * @c->max_write_size).
  390. */
  391. if (c->leb_size - wbuf->offs < c->max_write_size)
  392. wbuf->size = c->leb_size - wbuf->offs;
  393. else if (wbuf->offs & (c->max_write_size - 1))
  394. wbuf->size = ALIGN(wbuf->offs, c->max_write_size) - wbuf->offs;
  395. else
  396. wbuf->size = c->max_write_size;
  397. wbuf->avail = wbuf->size;
  398. wbuf->used = 0;
  399. wbuf->next_ino = 0;
  400. spin_unlock(&wbuf->lock);
  401. if (wbuf->sync_callback)
  402. err = wbuf->sync_callback(c, wbuf->lnum,
  403. c->leb_size - wbuf->offs, dirt);
  404. return err;
  405. }
  406. /**
  407. * ubifs_wbuf_seek_nolock - seek write-buffer.
  408. * @wbuf: write-buffer
  409. * @lnum: logical eraseblock number to seek to
  410. * @offs: logical eraseblock offset to seek to
  411. * @dtype: data type
  412. *
  413. * This function targets the write-buffer to logical eraseblock @lnum:@offs.
  414. * The write-buffer is synchronized if it is not empty. Returns zero in case of
  415. * success and a negative error code in case of failure.
  416. */
  417. int ubifs_wbuf_seek_nolock(struct ubifs_wbuf *wbuf, int lnum, int offs,
  418. int dtype)
  419. {
  420. const struct ubifs_info *c = wbuf->c;
  421. dbg_io("LEB %d:%d, jhead %s", lnum, offs, dbg_jhead(wbuf->jhead));
  422. ubifs_assert(lnum >= 0 && lnum < c->leb_cnt);
  423. ubifs_assert(offs >= 0 && offs <= c->leb_size);
  424. ubifs_assert(offs % c->min_io_size == 0 && !(offs & 7));
  425. ubifs_assert(lnum != wbuf->lnum);
  426. if (wbuf->used > 0) {
  427. int err = ubifs_wbuf_sync_nolock(wbuf);
  428. if (err)
  429. return err;
  430. }
  431. spin_lock(&wbuf->lock);
  432. wbuf->lnum = lnum;
  433. wbuf->offs = offs;
  434. if (c->leb_size - wbuf->offs < c->max_write_size)
  435. wbuf->size = c->leb_size - wbuf->offs;
  436. else if (wbuf->offs & (c->max_write_size - 1))
  437. wbuf->size = ALIGN(wbuf->offs, c->max_write_size) - wbuf->offs;
  438. else
  439. wbuf->size = c->max_write_size;
  440. wbuf->avail = wbuf->size;
  441. wbuf->used = 0;
  442. spin_unlock(&wbuf->lock);
  443. wbuf->dtype = dtype;
  444. return 0;
  445. }
  446. /**
  447. * ubifs_bg_wbufs_sync - synchronize write-buffers.
  448. * @c: UBIFS file-system description object
  449. *
  450. * This function is called by background thread to synchronize write-buffers.
  451. * Returns zero in case of success and a negative error code in case of
  452. * failure.
  453. */
  454. int ubifs_bg_wbufs_sync(struct ubifs_info *c)
  455. {
  456. int err, i;
  457. ubifs_assert(!c->ro_media && !c->ro_mount);
  458. if (!c->need_wbuf_sync)
  459. return 0;
  460. c->need_wbuf_sync = 0;
  461. if (c->ro_error) {
  462. err = -EROFS;
  463. goto out_timers;
  464. }
  465. dbg_io("synchronize");
  466. for (i = 0; i < c->jhead_cnt; i++) {
  467. struct ubifs_wbuf *wbuf = &c->jheads[i].wbuf;
  468. cond_resched();
  469. /*
  470. * If the mutex is locked then wbuf is being changed, so
  471. * synchronization is not necessary.
  472. */
  473. if (mutex_is_locked(&wbuf->io_mutex))
  474. continue;
  475. mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead);
  476. if (!wbuf->need_sync) {
  477. mutex_unlock(&wbuf->io_mutex);
  478. continue;
  479. }
  480. err = ubifs_wbuf_sync_nolock(wbuf);
  481. mutex_unlock(&wbuf->io_mutex);
  482. if (err) {
  483. ubifs_err("cannot sync write-buffer, error %d", err);
  484. ubifs_ro_mode(c, err);
  485. goto out_timers;
  486. }
  487. }
  488. return 0;
  489. out_timers:
  490. /* Cancel all timers to prevent repeated errors */
  491. for (i = 0; i < c->jhead_cnt; i++) {
  492. struct ubifs_wbuf *wbuf = &c->jheads[i].wbuf;
  493. mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead);
  494. cancel_wbuf_timer_nolock(wbuf);
  495. mutex_unlock(&wbuf->io_mutex);
  496. }
  497. return err;
  498. }
  499. /**
  500. * ubifs_wbuf_write_nolock - write data to flash via write-buffer.
  501. * @wbuf: write-buffer
  502. * @buf: node to write
  503. * @len: node length
  504. *
  505. * This function writes data to flash via write-buffer @wbuf. This means that
  506. * the last piece of the node won't reach the flash media immediately if it
  507. * does not take whole max. write unit (@c->max_write_size). Instead, the node
  508. * will sit in RAM until the write-buffer is synchronized (e.g., by timer, or
  509. * because more data are appended to the write-buffer).
  510. *
  511. * This function returns zero in case of success and a negative error code in
  512. * case of failure. If the node cannot be written because there is no more
  513. * space in this logical eraseblock, %-ENOSPC is returned.
  514. */
  515. int ubifs_wbuf_write_nolock(struct ubifs_wbuf *wbuf, void *buf, int len)
  516. {
  517. struct ubifs_info *c = wbuf->c;
  518. int err, written, n, aligned_len = ALIGN(len, 8), offs;
  519. dbg_io("%d bytes (%s) to jhead %s wbuf at LEB %d:%d", len,
  520. dbg_ntype(((struct ubifs_ch *)buf)->node_type),
  521. dbg_jhead(wbuf->jhead), wbuf->lnum, wbuf->offs + wbuf->used);
  522. ubifs_assert(len > 0 && wbuf->lnum >= 0 && wbuf->lnum < c->leb_cnt);
  523. ubifs_assert(wbuf->offs >= 0 && wbuf->offs % c->min_io_size == 0);
  524. ubifs_assert(!(wbuf->offs & 7) && wbuf->offs <= c->leb_size);
  525. ubifs_assert(wbuf->avail > 0 && wbuf->avail <= wbuf->size);
  526. ubifs_assert(wbuf->size >= c->min_io_size);
  527. ubifs_assert(wbuf->size <= c->max_write_size);
  528. ubifs_assert(wbuf->size % c->min_io_size == 0);
  529. ubifs_assert(mutex_is_locked(&wbuf->io_mutex));
  530. ubifs_assert(!c->ro_media && !c->ro_mount);
  531. if (c->leb_size - wbuf->offs >= c->max_write_size)
  532. ubifs_assert(!((wbuf->offs + wbuf->size) % c->max_write_size ));
  533. if (c->leb_size - wbuf->offs - wbuf->used < aligned_len) {
  534. err = -ENOSPC;
  535. goto out;
  536. }
  537. cancel_wbuf_timer_nolock(wbuf);
  538. if (c->ro_error)
  539. return -EROFS;
  540. if (aligned_len <= wbuf->avail) {
  541. /*
  542. * The node is not very large and fits entirely within
  543. * write-buffer.
  544. */
  545. memcpy(wbuf->buf + wbuf->used, buf, len);
  546. if (aligned_len == wbuf->avail) {
  547. dbg_io("flush jhead %s wbuf to LEB %d:%d",
  548. dbg_jhead(wbuf->jhead), wbuf->lnum, wbuf->offs);
  549. err = ubi_leb_write(c->ubi, wbuf->lnum, wbuf->buf,
  550. wbuf->offs, wbuf->size,
  551. wbuf->dtype);
  552. if (err)
  553. goto out;
  554. spin_lock(&wbuf->lock);
  555. wbuf->offs += wbuf->size;
  556. if (c->leb_size - wbuf->offs >= c->max_write_size)
  557. wbuf->size = c->max_write_size;
  558. else
  559. wbuf->size = c->leb_size - wbuf->offs;
  560. wbuf->avail = wbuf->size;
  561. wbuf->used = 0;
  562. wbuf->next_ino = 0;
  563. spin_unlock(&wbuf->lock);
  564. } else {
  565. spin_lock(&wbuf->lock);
  566. wbuf->avail -= aligned_len;
  567. wbuf->used += aligned_len;
  568. spin_unlock(&wbuf->lock);
  569. }
  570. goto exit;
  571. }
  572. offs = wbuf->offs;
  573. written = 0;
  574. if (wbuf->used) {
  575. /*
  576. * The node is large enough and does not fit entirely within
  577. * current available space. We have to fill and flush
  578. * write-buffer and switch to the next max. write unit.
  579. */
  580. dbg_io("flush jhead %s wbuf to LEB %d:%d",
  581. dbg_jhead(wbuf->jhead), wbuf->lnum, wbuf->offs);
  582. memcpy(wbuf->buf + wbuf->used, buf, wbuf->avail);
  583. err = ubi_leb_write(c->ubi, wbuf->lnum, wbuf->buf, wbuf->offs,
  584. wbuf->size, wbuf->dtype);
  585. if (err)
  586. goto out;
  587. offs += wbuf->size;
  588. len -= wbuf->avail;
  589. aligned_len -= wbuf->avail;
  590. written += wbuf->avail;
  591. } else if (wbuf->offs & (c->max_write_size - 1)) {
  592. /*
  593. * The write-buffer offset is not aligned to
  594. * @c->max_write_size and @wbuf->size is less than
  595. * @c->max_write_size. Write @wbuf->size bytes to make sure the
  596. * following writes are done in optimal @c->max_write_size
  597. * chunks.
  598. */
  599. dbg_io("write %d bytes to LEB %d:%d",
  600. wbuf->size, wbuf->lnum, wbuf->offs);
  601. err = ubi_leb_write(c->ubi, wbuf->lnum, buf, wbuf->offs,
  602. wbuf->size, wbuf->dtype);
  603. if (err)
  604. goto out;
  605. offs += wbuf->size;
  606. len -= wbuf->size;
  607. aligned_len -= wbuf->size;
  608. written += wbuf->size;
  609. }
  610. /*
  611. * The remaining data may take more whole max. write units, so write the
  612. * remains multiple to max. write unit size directly to the flash media.
  613. * We align node length to 8-byte boundary because we anyway flash wbuf
  614. * if the remaining space is less than 8 bytes.
  615. */
  616. n = aligned_len >> c->max_write_shift;
  617. if (n) {
  618. n <<= c->max_write_shift;
  619. dbg_io("write %d bytes to LEB %d:%d", n, wbuf->lnum, offs);
  620. err = ubi_leb_write(c->ubi, wbuf->lnum, buf + written, offs, n,
  621. wbuf->dtype);
  622. if (err)
  623. goto out;
  624. offs += n;
  625. aligned_len -= n;
  626. len -= n;
  627. written += n;
  628. }
  629. spin_lock(&wbuf->lock);
  630. if (aligned_len)
  631. /*
  632. * And now we have what's left and what does not take whole
  633. * max. write unit, so write it to the write-buffer and we are
  634. * done.
  635. */
  636. memcpy(wbuf->buf, buf + written, len);
  637. wbuf->offs = offs;
  638. if (c->leb_size - wbuf->offs >= c->max_write_size)
  639. wbuf->size = c->max_write_size;
  640. else
  641. wbuf->size = c->leb_size - wbuf->offs;
  642. wbuf->avail = wbuf->size - aligned_len;
  643. wbuf->used = aligned_len;
  644. wbuf->next_ino = 0;
  645. spin_unlock(&wbuf->lock);
  646. exit:
  647. if (wbuf->sync_callback) {
  648. int free = c->leb_size - wbuf->offs - wbuf->used;
  649. err = wbuf->sync_callback(c, wbuf->lnum, free, 0);
  650. if (err)
  651. goto out;
  652. }
  653. if (wbuf->used)
  654. new_wbuf_timer_nolock(wbuf);
  655. return 0;
  656. out:
  657. ubifs_err("cannot write %d bytes to LEB %d:%d, error %d",
  658. len, wbuf->lnum, wbuf->offs, err);
  659. dbg_dump_node(c, buf);
  660. dbg_dump_stack();
  661. dbg_dump_leb(c, wbuf->lnum);
  662. return err;
  663. }
  664. /**
  665. * ubifs_write_node - write node to the media.
  666. * @c: UBIFS file-system description object
  667. * @buf: the node to write
  668. * @len: node length
  669. * @lnum: logical eraseblock number
  670. * @offs: offset within the logical eraseblock
  671. * @dtype: node life-time hint (%UBI_LONGTERM, %UBI_SHORTTERM, %UBI_UNKNOWN)
  672. *
  673. * This function automatically fills node magic number, assigns sequence
  674. * number, and calculates node CRC checksum. The length of the @buf buffer has
  675. * to be aligned to the minimal I/O unit size. This function automatically
  676. * appends padding node and padding bytes if needed. Returns zero in case of
  677. * success and a negative error code in case of failure.
  678. */
  679. int ubifs_write_node(struct ubifs_info *c, void *buf, int len, int lnum,
  680. int offs, int dtype)
  681. {
  682. int err, buf_len = ALIGN(len, c->min_io_size);
  683. dbg_io("LEB %d:%d, %s, length %d (aligned %d)",
  684. lnum, offs, dbg_ntype(((struct ubifs_ch *)buf)->node_type), len,
  685. buf_len);
  686. ubifs_assert(lnum >= 0 && lnum < c->leb_cnt && offs >= 0);
  687. ubifs_assert(offs % c->min_io_size == 0 && offs < c->leb_size);
  688. ubifs_assert(!c->ro_media && !c->ro_mount);
  689. if (c->ro_error)
  690. return -EROFS;
  691. ubifs_prepare_node(c, buf, len, 1);
  692. err = ubi_leb_write(c->ubi, lnum, buf, offs, buf_len, dtype);
  693. if (err) {
  694. ubifs_err("cannot write %d bytes to LEB %d:%d, error %d",
  695. buf_len, lnum, offs, err);
  696. dbg_dump_node(c, buf);
  697. dbg_dump_stack();
  698. }
  699. return err;
  700. }
  701. /**
  702. * ubifs_read_node_wbuf - read node from the media or write-buffer.
  703. * @wbuf: wbuf to check for un-written data
  704. * @buf: buffer to read to
  705. * @type: node type
  706. * @len: node length
  707. * @lnum: logical eraseblock number
  708. * @offs: offset within the logical eraseblock
  709. *
  710. * This function reads a node of known type and length, checks it and stores
  711. * in @buf. If the node partially or fully sits in the write-buffer, this
  712. * function takes data from the buffer, otherwise it reads the flash media.
  713. * Returns zero in case of success, %-EUCLEAN if CRC mismatched and a negative
  714. * error code in case of failure.
  715. */
  716. int ubifs_read_node_wbuf(struct ubifs_wbuf *wbuf, void *buf, int type, int len,
  717. int lnum, int offs)
  718. {
  719. const struct ubifs_info *c = wbuf->c;
  720. int err, rlen, overlap;
  721. struct ubifs_ch *ch = buf;
  722. dbg_io("LEB %d:%d, %s, length %d, jhead %s", lnum, offs,
  723. dbg_ntype(type), len, dbg_jhead(wbuf->jhead));
  724. ubifs_assert(wbuf && lnum >= 0 && lnum < c->leb_cnt && offs >= 0);
  725. ubifs_assert(!(offs & 7) && offs < c->leb_size);
  726. ubifs_assert(type >= 0 && type < UBIFS_NODE_TYPES_CNT);
  727. spin_lock(&wbuf->lock);
  728. overlap = (lnum == wbuf->lnum && offs + len > wbuf->offs);
  729. if (!overlap) {
  730. /* We may safely unlock the write-buffer and read the data */
  731. spin_unlock(&wbuf->lock);
  732. return ubifs_read_node(c, buf, type, len, lnum, offs);
  733. }
  734. /* Don't read under wbuf */
  735. rlen = wbuf->offs - offs;
  736. if (rlen < 0)
  737. rlen = 0;
  738. /* Copy the rest from the write-buffer */
  739. memcpy(buf + rlen, wbuf->buf + offs + rlen - wbuf->offs, len - rlen);
  740. spin_unlock(&wbuf->lock);
  741. if (rlen > 0) {
  742. /* Read everything that goes before write-buffer */
  743. err = ubi_read(c->ubi, lnum, buf, offs, rlen);
  744. if (err && err != -EBADMSG) {
  745. ubifs_err("failed to read node %d from LEB %d:%d, "
  746. "error %d", type, lnum, offs, err);
  747. dbg_dump_stack();
  748. return err;
  749. }
  750. }
  751. if (type != ch->node_type) {
  752. ubifs_err("bad node type (%d but expected %d)",
  753. ch->node_type, type);
  754. goto out;
  755. }
  756. err = ubifs_check_node(c, buf, lnum, offs, 0, 0);
  757. if (err) {
  758. ubifs_err("expected node type %d", type);
  759. return err;
  760. }
  761. rlen = le32_to_cpu(ch->len);
  762. if (rlen != len) {
  763. ubifs_err("bad node length %d, expected %d", rlen, len);
  764. goto out;
  765. }
  766. return 0;
  767. out:
  768. ubifs_err("bad node at LEB %d:%d", lnum, offs);
  769. dbg_dump_node(c, buf);
  770. dbg_dump_stack();
  771. return -EINVAL;
  772. }
  773. /**
  774. * ubifs_read_node - read node.
  775. * @c: UBIFS file-system description object
  776. * @buf: buffer to read to
  777. * @type: node type
  778. * @len: node length (not aligned)
  779. * @lnum: logical eraseblock number
  780. * @offs: offset within the logical eraseblock
  781. *
  782. * This function reads a node of known type and and length, checks it and
  783. * stores in @buf. Returns zero in case of success, %-EUCLEAN if CRC mismatched
  784. * and a negative error code in case of failure.
  785. */
  786. int ubifs_read_node(const struct ubifs_info *c, void *buf, int type, int len,
  787. int lnum, int offs)
  788. {
  789. int err, l;
  790. struct ubifs_ch *ch = buf;
  791. dbg_io("LEB %d:%d, %s, length %d", lnum, offs, dbg_ntype(type), len);
  792. ubifs_assert(lnum >= 0 && lnum < c->leb_cnt && offs >= 0);
  793. ubifs_assert(len >= UBIFS_CH_SZ && offs + len <= c->leb_size);
  794. ubifs_assert(!(offs & 7) && offs < c->leb_size);
  795. ubifs_assert(type >= 0 && type < UBIFS_NODE_TYPES_CNT);
  796. err = ubi_read(c->ubi, lnum, buf, offs, len);
  797. if (err && err != -EBADMSG) {
  798. ubifs_err("cannot read node %d from LEB %d:%d, error %d",
  799. type, lnum, offs, err);
  800. return err;
  801. }
  802. if (type != ch->node_type) {
  803. ubifs_err("bad node type (%d but expected %d)",
  804. ch->node_type, type);
  805. goto out;
  806. }
  807. err = ubifs_check_node(c, buf, lnum, offs, 0, 0);
  808. if (err) {
  809. ubifs_err("expected node type %d", type);
  810. return err;
  811. }
  812. l = le32_to_cpu(ch->len);
  813. if (l != len) {
  814. ubifs_err("bad node length %d, expected %d", l, len);
  815. goto out;
  816. }
  817. return 0;
  818. out:
  819. ubifs_err("bad node at LEB %d:%d, LEB mapping status %d", lnum, offs,
  820. ubi_is_mapped(c->ubi, lnum));
  821. dbg_dump_node(c, buf);
  822. dbg_dump_stack();
  823. return -EINVAL;
  824. }
  825. /**
  826. * ubifs_wbuf_init - initialize write-buffer.
  827. * @c: UBIFS file-system description object
  828. * @wbuf: write-buffer to initialize
  829. *
  830. * This function initializes write-buffer. Returns zero in case of success
  831. * %-ENOMEM in case of failure.
  832. */
  833. int ubifs_wbuf_init(struct ubifs_info *c, struct ubifs_wbuf *wbuf)
  834. {
  835. size_t size;
  836. wbuf->buf = kmalloc(c->max_write_size, GFP_KERNEL);
  837. if (!wbuf->buf)
  838. return -ENOMEM;
  839. size = (c->max_write_size / UBIFS_CH_SZ + 1) * sizeof(ino_t);
  840. wbuf->inodes = kmalloc(size, GFP_KERNEL);
  841. if (!wbuf->inodes) {
  842. kfree(wbuf->buf);
  843. wbuf->buf = NULL;
  844. return -ENOMEM;
  845. }
  846. wbuf->used = 0;
  847. wbuf->lnum = wbuf->offs = -1;
  848. /*
  849. * If the LEB starts at the max. write size aligned address, then
  850. * write-buffer size has to be set to @c->max_write_size. Otherwise,
  851. * set it to something smaller so that it ends at the closest max.
  852. * write size boundary.
  853. */
  854. size = c->max_write_size - (c->leb_start % c->max_write_size);
  855. wbuf->avail = wbuf->size = size;
  856. wbuf->dtype = UBI_UNKNOWN;
  857. wbuf->sync_callback = NULL;
  858. mutex_init(&wbuf->io_mutex);
  859. spin_lock_init(&wbuf->lock);
  860. wbuf->c = c;
  861. wbuf->next_ino = 0;
  862. hrtimer_init(&wbuf->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  863. wbuf->timer.function = wbuf_timer_callback_nolock;
  864. wbuf->softlimit = ktime_set(WBUF_TIMEOUT_SOFTLIMIT, 0);
  865. wbuf->delta = WBUF_TIMEOUT_HARDLIMIT - WBUF_TIMEOUT_SOFTLIMIT;
  866. wbuf->delta *= 1000000000ULL;
  867. ubifs_assert(wbuf->delta <= ULONG_MAX);
  868. return 0;
  869. }
  870. /**
  871. * ubifs_wbuf_add_ino_nolock - add an inode number into the wbuf inode array.
  872. * @wbuf: the write-buffer where to add
  873. * @inum: the inode number
  874. *
  875. * This function adds an inode number to the inode array of the write-buffer.
  876. */
  877. void ubifs_wbuf_add_ino_nolock(struct ubifs_wbuf *wbuf, ino_t inum)
  878. {
  879. if (!wbuf->buf)
  880. /* NOR flash or something similar */
  881. return;
  882. spin_lock(&wbuf->lock);
  883. if (wbuf->used)
  884. wbuf->inodes[wbuf->next_ino++] = inum;
  885. spin_unlock(&wbuf->lock);
  886. }
  887. /**
  888. * wbuf_has_ino - returns if the wbuf contains data from the inode.
  889. * @wbuf: the write-buffer
  890. * @inum: the inode number
  891. *
  892. * This function returns with %1 if the write-buffer contains some data from the
  893. * given inode otherwise it returns with %0.
  894. */
  895. static int wbuf_has_ino(struct ubifs_wbuf *wbuf, ino_t inum)
  896. {
  897. int i, ret = 0;
  898. spin_lock(&wbuf->lock);
  899. for (i = 0; i < wbuf->next_ino; i++)
  900. if (inum == wbuf->inodes[i]) {
  901. ret = 1;
  902. break;
  903. }
  904. spin_unlock(&wbuf->lock);
  905. return ret;
  906. }
  907. /**
  908. * ubifs_sync_wbufs_by_inode - synchronize write-buffers for an inode.
  909. * @c: UBIFS file-system description object
  910. * @inode: inode to synchronize
  911. *
  912. * This function synchronizes write-buffers which contain nodes belonging to
  913. * @inode. Returns zero in case of success and a negative error code in case of
  914. * failure.
  915. */
  916. int ubifs_sync_wbufs_by_inode(struct ubifs_info *c, struct inode *inode)
  917. {
  918. int i, err = 0;
  919. for (i = 0; i < c->jhead_cnt; i++) {
  920. struct ubifs_wbuf *wbuf = &c->jheads[i].wbuf;
  921. if (i == GCHD)
  922. /*
  923. * GC head is special, do not look at it. Even if the
  924. * head contains something related to this inode, it is
  925. * a _copy_ of corresponding on-flash node which sits
  926. * somewhere else.
  927. */
  928. continue;
  929. if (!wbuf_has_ino(wbuf, inode->i_ino))
  930. continue;
  931. mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead);
  932. if (wbuf_has_ino(wbuf, inode->i_ino))
  933. err = ubifs_wbuf_sync_nolock(wbuf);
  934. mutex_unlock(&wbuf->io_mutex);
  935. if (err) {
  936. ubifs_ro_mode(c, err);
  937. return err;
  938. }
  939. }
  940. return 0;
  941. }