io.c 26 KB

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