io.c 27 KB

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