io.c 27 KB

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