io.c 25 KB

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