io.c 25 KB

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