io.c 26 KB

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