io.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052
  1. /*
  2. * This file is part of UBIFS.
  3. *
  4. * Copyright (C) 2006-2008 Nokia Corporation.
  5. * Copyright (C) 2006, 2007 University of Szeged, Hungary
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License version 2 as published by
  9. * the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  14. * more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along with
  17. * this program; if not, write to the Free Software Foundation, Inc., 51
  18. * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  19. *
  20. * Authors: Artem Bityutskiy (Битюцкий Артём)
  21. * Adrian Hunter
  22. * Zoltan Sogor
  23. */
  24. /*
  25. * This file implements UBIFS I/O subsystem which provides various I/O-related
  26. * helper functions (reading/writing/checking/validating nodes) and implements
  27. * write-buffering support. Write buffers help to save space which otherwise
  28. * would have been wasted for padding to the nearest minimal I/O unit boundary.
  29. * Instead, data first goes to the write-buffer and is flushed when the
  30. * buffer is full or when it is not used for some time (by timer). This is
  31. * similar to the mechanism is used by JFFS2.
  32. *
  33. * UBIFS distinguishes between minimum write size (@c->min_io_size) and maximum
  34. * write size (@c->max_write_size). The latter is the maximum amount of bytes
  35. * the underlying flash is able to program at a time, and writing in
  36. * @c->max_write_size units should presumably be faster. Obviously,
  37. * @c->min_io_size <= @c->max_write_size. Write-buffers are of
  38. * @c->max_write_size bytes in size for maximum performance. However, when a
  39. * write-buffer is flushed, only the portion of it (aligned to @c->min_io_size
  40. * boundary) which contains data is written, not the whole write-buffer,
  41. * because this is more space-efficient.
  42. *
  43. * This optimization adds few complications to the code. Indeed, on the one
  44. * hand, we want to write in optimal @c->max_write_size bytes chunks, which
  45. * also means aligning writes at the @c->max_write_size bytes offsets. On the
  46. * other hand, we do not want to waste space when synchronizing the write
  47. * buffer, so during synchronization we writes in smaller chunks. And this makes
  48. * the next write offset to be not aligned to @c->max_write_size bytes. So the
  49. * have to make sure that the write-buffer offset (@wbuf->offs) becomes aligned
  50. * to @c->max_write_size bytes again. We do this by temporarily shrinking
  51. * write-buffer size (@wbuf->size).
  52. *
  53. * Write-buffers are defined by 'struct ubifs_wbuf' objects and protected by
  54. * mutexes defined inside these objects. Since sometimes upper-level code
  55. * has to lock the write-buffer (e.g. journal space reservation code), many
  56. * functions related to write-buffers have "nolock" suffix which means that the
  57. * caller has to lock the write-buffer before calling this function.
  58. *
  59. * UBIFS stores nodes at 64 bit-aligned addresses. If the node length is not
  60. * aligned, UBIFS starts the next node from the aligned address, and the padded
  61. * bytes may contain any rubbish. In other words, UBIFS does not put padding
  62. * bytes in those small gaps. Common headers of nodes store real node lengths,
  63. * not aligned lengths. Indexing nodes also store real lengths in branches.
  64. *
  65. * UBIFS uses padding when it pads to the next min. I/O unit. In this case it
  66. * uses padding nodes or padding bytes, if the padding node does not fit.
  67. *
  68. * All UBIFS nodes are protected by CRC checksums and UBIFS checks CRC when
  69. * they are read from the flash media.
  70. */
  71. #include <linux/crc32.h>
  72. #include <linux/slab.h>
  73. #include "ubifs.h"
  74. /**
  75. * ubifs_ro_mode - switch UBIFS to read read-only mode.
  76. * @c: UBIFS file-system description object
  77. * @err: error code which is the reason of switching to R/O mode
  78. */
  79. void ubifs_ro_mode(struct ubifs_info *c, int err)
  80. {
  81. if (!c->ro_error) {
  82. c->ro_error = 1;
  83. c->no_chk_data_crc = 0;
  84. c->vfs_sb->s_flags |= MS_RDONLY;
  85. ubifs_warn("switched to read-only mode, error %d", err);
  86. dbg_dump_stack();
  87. }
  88. }
  89. /**
  90. * ubifs_check_node - check node.
  91. * @c: UBIFS file-system description object
  92. * @buf: node to check
  93. * @lnum: logical eraseblock number
  94. * @offs: offset within the logical eraseblock
  95. * @quiet: print no messages
  96. * @must_chk_crc: indicates whether to always check the CRC
  97. *
  98. * This function checks node magic number and CRC checksum. This function also
  99. * validates node length to prevent UBIFS from becoming crazy when an attacker
  100. * feeds it a file-system image with incorrect nodes. For example, too large
  101. * node length in the common header could cause UBIFS to read memory outside of
  102. * allocated buffer when checking the CRC checksum.
  103. *
  104. * This function may skip data nodes CRC checking if @c->no_chk_data_crc is
  105. * true, which is controlled by corresponding UBIFS mount option. However, if
  106. * @must_chk_crc is true, then @c->no_chk_data_crc is ignored and CRC is
  107. * checked. Similarly, if @c->mounting or @c->remounting_rw is true (we are
  108. * mounting or re-mounting to R/W mode), @c->no_chk_data_crc is ignored and CRC
  109. * is checked. This is because during mounting or re-mounting from R/O mode to
  110. * R/W mode we may read journal nodes (when replying the journal or doing the
  111. * recovery) and the journal nodes may potentially be corrupted, so checking is
  112. * required.
  113. *
  114. * This function returns zero in case of success and %-EUCLEAN in case of bad
  115. * CRC or magic.
  116. */
  117. int ubifs_check_node(const struct ubifs_info *c, const void *buf, int lnum,
  118. int offs, int quiet, int must_chk_crc)
  119. {
  120. int err = -EINVAL, type, node_len;
  121. uint32_t crc, node_crc, magic;
  122. const struct ubifs_ch *ch = buf;
  123. ubifs_assert(lnum >= 0 && lnum < c->leb_cnt && offs >= 0);
  124. ubifs_assert(!(offs & 7) && offs < c->leb_size);
  125. magic = le32_to_cpu(ch->magic);
  126. if (magic != UBIFS_NODE_MAGIC) {
  127. if (!quiet)
  128. ubifs_err("bad magic %#08x, expected %#08x",
  129. magic, UBIFS_NODE_MAGIC);
  130. err = -EUCLEAN;
  131. goto out;
  132. }
  133. type = ch->node_type;
  134. if (type < 0 || type >= UBIFS_NODE_TYPES_CNT) {
  135. if (!quiet)
  136. ubifs_err("bad node type %d", type);
  137. goto out;
  138. }
  139. node_len = le32_to_cpu(ch->len);
  140. if (node_len + offs > c->leb_size)
  141. goto out_len;
  142. if (c->ranges[type].max_len == 0) {
  143. if (node_len != c->ranges[type].len)
  144. goto out_len;
  145. } else if (node_len < c->ranges[type].min_len ||
  146. node_len > c->ranges[type].max_len)
  147. goto out_len;
  148. if (!must_chk_crc && type == UBIFS_DATA_NODE && !c->mounting &&
  149. !c->remounting_rw && c->no_chk_data_crc)
  150. return 0;
  151. crc = crc32(UBIFS_CRC32_INIT, buf + 8, node_len - 8);
  152. node_crc = le32_to_cpu(ch->crc);
  153. if (crc != node_crc) {
  154. if (!quiet)
  155. ubifs_err("bad CRC: calculated %#08x, read %#08x",
  156. crc, node_crc);
  157. err = -EUCLEAN;
  158. goto out;
  159. }
  160. return 0;
  161. out_len:
  162. if (!quiet)
  163. ubifs_err("bad node length %d", node_len);
  164. out:
  165. if (!quiet) {
  166. ubifs_err("bad node at LEB %d:%d", lnum, offs);
  167. dbg_dump_node(c, buf);
  168. dbg_dump_stack();
  169. }
  170. return err;
  171. }
  172. /**
  173. * ubifs_pad - pad flash space.
  174. * @c: UBIFS file-system description object
  175. * @buf: buffer to put padding to
  176. * @pad: how many bytes to pad
  177. *
  178. * The flash media obliges us to write only in chunks of %c->min_io_size and
  179. * when we have to write less data we add padding node to the write-buffer and
  180. * pad it to the next minimal I/O unit's boundary. Padding nodes help when the
  181. * media is being scanned. If the amount of wasted space is not enough to fit a
  182. * padding node which takes %UBIFS_PAD_NODE_SZ bytes, we write padding bytes
  183. * pattern (%UBIFS_PADDING_BYTE).
  184. *
  185. * Padding nodes are also used to fill gaps when the "commit-in-gaps" method is
  186. * used.
  187. */
  188. void ubifs_pad(const struct ubifs_info *c, void *buf, int pad)
  189. {
  190. uint32_t crc;
  191. ubifs_assert(pad >= 0 && !(pad & 7));
  192. if (pad >= UBIFS_PAD_NODE_SZ) {
  193. struct ubifs_ch *ch = buf;
  194. struct ubifs_pad_node *pad_node = buf;
  195. ch->magic = cpu_to_le32(UBIFS_NODE_MAGIC);
  196. ch->node_type = UBIFS_PAD_NODE;
  197. ch->group_type = UBIFS_NO_NODE_GROUP;
  198. ch->padding[0] = ch->padding[1] = 0;
  199. ch->sqnum = 0;
  200. ch->len = cpu_to_le32(UBIFS_PAD_NODE_SZ);
  201. pad -= UBIFS_PAD_NODE_SZ;
  202. pad_node->pad_len = cpu_to_le32(pad);
  203. crc = crc32(UBIFS_CRC32_INIT, buf + 8, UBIFS_PAD_NODE_SZ - 8);
  204. ch->crc = cpu_to_le32(crc);
  205. memset(buf + UBIFS_PAD_NODE_SZ, 0, pad);
  206. } else if (pad > 0)
  207. /* Too little space, padding node won't fit */
  208. memset(buf, UBIFS_PADDING_BYTE, pad);
  209. }
  210. /**
  211. * next_sqnum - get next sequence number.
  212. * @c: UBIFS file-system description object
  213. */
  214. static unsigned long long next_sqnum(struct ubifs_info *c)
  215. {
  216. unsigned long long sqnum;
  217. spin_lock(&c->cnt_lock);
  218. sqnum = ++c->max_sqnum;
  219. spin_unlock(&c->cnt_lock);
  220. if (unlikely(sqnum >= SQNUM_WARN_WATERMARK)) {
  221. if (sqnum >= SQNUM_WATERMARK) {
  222. ubifs_err("sequence number overflow %llu, end of life",
  223. sqnum);
  224. ubifs_ro_mode(c, -EINVAL);
  225. }
  226. ubifs_warn("running out of sequence numbers, end of life soon");
  227. }
  228. return sqnum;
  229. }
  230. /**
  231. * ubifs_prepare_node - prepare node to be written to flash.
  232. * @c: UBIFS file-system description object
  233. * @node: the node to pad
  234. * @len: node length
  235. * @pad: if the buffer has to be padded
  236. *
  237. * This function prepares node at @node to be written to the media - it
  238. * calculates node CRC, fills the common header, and adds proper padding up to
  239. * the next minimum I/O unit if @pad is not zero.
  240. */
  241. void ubifs_prepare_node(struct ubifs_info *c, void *node, int len, int pad)
  242. {
  243. uint32_t crc;
  244. struct ubifs_ch *ch = node;
  245. unsigned long long sqnum = next_sqnum(c);
  246. ubifs_assert(len >= UBIFS_CH_SZ);
  247. ch->magic = cpu_to_le32(UBIFS_NODE_MAGIC);
  248. ch->len = cpu_to_le32(len);
  249. ch->group_type = UBIFS_NO_NODE_GROUP;
  250. ch->sqnum = cpu_to_le64(sqnum);
  251. ch->padding[0] = ch->padding[1] = 0;
  252. crc = crc32(UBIFS_CRC32_INIT, node + 8, len - 8);
  253. ch->crc = cpu_to_le32(crc);
  254. if (pad) {
  255. len = ALIGN(len, 8);
  256. pad = ALIGN(len, c->min_io_size) - len;
  257. ubifs_pad(c, node + len, pad);
  258. }
  259. }
  260. /**
  261. * ubifs_prep_grp_node - prepare node of a group to be written to flash.
  262. * @c: UBIFS file-system description object
  263. * @node: the node to pad
  264. * @len: node length
  265. * @last: indicates the last node of the group
  266. *
  267. * This function prepares node at @node to be written to the media - it
  268. * calculates node CRC and fills the common header.
  269. */
  270. void ubifs_prep_grp_node(struct ubifs_info *c, void *node, int len, int last)
  271. {
  272. uint32_t crc;
  273. struct ubifs_ch *ch = node;
  274. unsigned long long sqnum = next_sqnum(c);
  275. ubifs_assert(len >= UBIFS_CH_SZ);
  276. ch->magic = cpu_to_le32(UBIFS_NODE_MAGIC);
  277. ch->len = cpu_to_le32(len);
  278. if (last)
  279. ch->group_type = UBIFS_LAST_OF_NODE_GROUP;
  280. else
  281. ch->group_type = UBIFS_IN_NODE_GROUP;
  282. ch->sqnum = cpu_to_le64(sqnum);
  283. ch->padding[0] = ch->padding[1] = 0;
  284. crc = crc32(UBIFS_CRC32_INIT, node + 8, len - 8);
  285. ch->crc = cpu_to_le32(crc);
  286. }
  287. /**
  288. * wbuf_timer_callback - write-buffer timer callback function.
  289. * @data: timer data (write-buffer descriptor)
  290. *
  291. * This function is called when the write-buffer timer expires.
  292. */
  293. static enum hrtimer_restart wbuf_timer_callback_nolock(struct hrtimer *timer)
  294. {
  295. struct ubifs_wbuf *wbuf = container_of(timer, struct ubifs_wbuf, timer);
  296. dbg_io("jhead %s", dbg_jhead(wbuf->jhead));
  297. wbuf->need_sync = 1;
  298. wbuf->c->need_wbuf_sync = 1;
  299. ubifs_wake_up_bgt(wbuf->c);
  300. return HRTIMER_NORESTART;
  301. }
  302. /**
  303. * new_wbuf_timer - start new write-buffer timer.
  304. * @wbuf: write-buffer descriptor
  305. */
  306. static void new_wbuf_timer_nolock(struct ubifs_wbuf *wbuf)
  307. {
  308. ubifs_assert(!hrtimer_active(&wbuf->timer));
  309. if (wbuf->no_timer)
  310. return;
  311. dbg_io("set timer for jhead %s, %llu-%llu millisecs",
  312. dbg_jhead(wbuf->jhead),
  313. div_u64(ktime_to_ns(wbuf->softlimit), USEC_PER_SEC),
  314. div_u64(ktime_to_ns(wbuf->softlimit) + wbuf->delta,
  315. USEC_PER_SEC));
  316. hrtimer_start_range_ns(&wbuf->timer, wbuf->softlimit, wbuf->delta,
  317. HRTIMER_MODE_REL);
  318. }
  319. /**
  320. * cancel_wbuf_timer - cancel write-buffer timer.
  321. * @wbuf: write-buffer descriptor
  322. */
  323. static void cancel_wbuf_timer_nolock(struct ubifs_wbuf *wbuf)
  324. {
  325. if (wbuf->no_timer)
  326. return;
  327. wbuf->need_sync = 0;
  328. hrtimer_cancel(&wbuf->timer);
  329. }
  330. /**
  331. * ubifs_wbuf_sync_nolock - synchronize write-buffer.
  332. * @wbuf: write-buffer to synchronize
  333. *
  334. * This function synchronizes write-buffer @buf and returns zero in case of
  335. * success or a negative error code in case of failure.
  336. *
  337. * Note, although write-buffers are of @c->max_write_size, this function does
  338. * not necessarily writes all @c->max_write_size bytes to the flash. Instead,
  339. * if the write-buffer is only partially filled with data, only the used part
  340. * of the write-buffer (aligned on @c->min_io_size boundary) is synchronized.
  341. * This way we waste less space.
  342. */
  343. int ubifs_wbuf_sync_nolock(struct ubifs_wbuf *wbuf)
  344. {
  345. struct ubifs_info *c = wbuf->c;
  346. int err, dirt, sync_len;
  347. cancel_wbuf_timer_nolock(wbuf);
  348. if (!wbuf->used || wbuf->lnum == -1)
  349. /* Write-buffer is empty or not seeked */
  350. return 0;
  351. dbg_io("LEB %d:%d, %d bytes, jhead %s",
  352. wbuf->lnum, wbuf->offs, wbuf->used, dbg_jhead(wbuf->jhead));
  353. ubifs_assert(!(wbuf->avail & 7));
  354. ubifs_assert(wbuf->offs + wbuf->size <= c->leb_size);
  355. ubifs_assert(wbuf->size >= c->min_io_size);
  356. ubifs_assert(wbuf->size <= c->max_write_size);
  357. ubifs_assert(wbuf->size % c->min_io_size == 0);
  358. ubifs_assert(!c->ro_media && !c->ro_mount);
  359. if (c->leb_size - wbuf->offs >= c->max_write_size)
  360. ubifs_assert(!((wbuf->offs + wbuf->size) % c->max_write_size));
  361. if (c->ro_error)
  362. return -EROFS;
  363. /*
  364. * Do not write whole write buffer but write only the minimum necessary
  365. * amount of min. I/O units.
  366. */
  367. sync_len = ALIGN(wbuf->used, c->min_io_size);
  368. dirt = sync_len - wbuf->used;
  369. if (dirt)
  370. ubifs_pad(c, wbuf->buf + wbuf->used, dirt);
  371. err = ubi_leb_write(c->ubi, wbuf->lnum, wbuf->buf, wbuf->offs,
  372. sync_len, wbuf->dtype);
  373. if (err) {
  374. ubifs_err("cannot write %d bytes to LEB %d:%d",
  375. sync_len, wbuf->lnum, wbuf->offs);
  376. dbg_dump_stack();
  377. return err;
  378. }
  379. spin_lock(&wbuf->lock);
  380. wbuf->offs += sync_len;
  381. /*
  382. * Now @wbuf->offs is not necessarily aligned to @c->max_write_size.
  383. * But our goal is to optimize writes and make sure we write in
  384. * @c->max_write_size chunks and to @c->max_write_size-aligned offset.
  385. * Thus, if @wbuf->offs is not aligned to @c->max_write_size now, make
  386. * sure that @wbuf->offs + @wbuf->size is aligned to
  387. * @c->max_write_size. This way we make sure that after next
  388. * write-buffer flush we are again at the optimal offset (aligned to
  389. * @c->max_write_size).
  390. */
  391. if (c->leb_size - wbuf->offs < c->max_write_size)
  392. wbuf->size = c->leb_size - wbuf->offs;
  393. else if (wbuf->offs & (c->max_write_size - 1))
  394. wbuf->size = ALIGN(wbuf->offs, c->max_write_size) - wbuf->offs;
  395. else
  396. wbuf->size = c->max_write_size;
  397. wbuf->avail = wbuf->size;
  398. wbuf->used = 0;
  399. wbuf->next_ino = 0;
  400. spin_unlock(&wbuf->lock);
  401. if (wbuf->sync_callback)
  402. err = wbuf->sync_callback(c, wbuf->lnum,
  403. c->leb_size - wbuf->offs, dirt);
  404. return err;
  405. }
  406. /**
  407. * ubifs_wbuf_seek_nolock - seek write-buffer.
  408. * @wbuf: write-buffer
  409. * @lnum: logical eraseblock number to seek to
  410. * @offs: logical eraseblock offset to seek to
  411. * @dtype: data type
  412. *
  413. * This function targets the write-buffer to logical eraseblock @lnum:@offs.
  414. * The write-buffer has to be empty. Returns zero in case of success and a
  415. * negative error code in case of failure.
  416. */
  417. int ubifs_wbuf_seek_nolock(struct ubifs_wbuf *wbuf, int lnum, int offs,
  418. int dtype)
  419. {
  420. const struct ubifs_info *c = wbuf->c;
  421. dbg_io("LEB %d:%d, jhead %s", lnum, offs, dbg_jhead(wbuf->jhead));
  422. ubifs_assert(lnum >= 0 && lnum < c->leb_cnt);
  423. ubifs_assert(offs >= 0 && offs <= c->leb_size);
  424. ubifs_assert(offs % c->min_io_size == 0 && !(offs & 7));
  425. ubifs_assert(lnum != wbuf->lnum);
  426. ubifs_assert(wbuf->used == 0);
  427. spin_lock(&wbuf->lock);
  428. wbuf->lnum = lnum;
  429. wbuf->offs = offs;
  430. if (c->leb_size - wbuf->offs < c->max_write_size)
  431. wbuf->size = c->leb_size - wbuf->offs;
  432. else if (wbuf->offs & (c->max_write_size - 1))
  433. wbuf->size = ALIGN(wbuf->offs, c->max_write_size) - wbuf->offs;
  434. else
  435. wbuf->size = c->max_write_size;
  436. wbuf->avail = wbuf->size;
  437. wbuf->used = 0;
  438. spin_unlock(&wbuf->lock);
  439. wbuf->dtype = dtype;
  440. return 0;
  441. }
  442. /**
  443. * ubifs_bg_wbufs_sync - synchronize write-buffers.
  444. * @c: UBIFS file-system description object
  445. *
  446. * This function is called by background thread to synchronize write-buffers.
  447. * Returns zero in case of success and a negative error code in case of
  448. * failure.
  449. */
  450. int ubifs_bg_wbufs_sync(struct ubifs_info *c)
  451. {
  452. int err, i;
  453. ubifs_assert(!c->ro_media && !c->ro_mount);
  454. if (!c->need_wbuf_sync)
  455. return 0;
  456. c->need_wbuf_sync = 0;
  457. if (c->ro_error) {
  458. err = -EROFS;
  459. goto out_timers;
  460. }
  461. dbg_io("synchronize");
  462. for (i = 0; i < c->jhead_cnt; i++) {
  463. struct ubifs_wbuf *wbuf = &c->jheads[i].wbuf;
  464. cond_resched();
  465. /*
  466. * If the mutex is locked then wbuf is being changed, so
  467. * synchronization is not necessary.
  468. */
  469. if (mutex_is_locked(&wbuf->io_mutex))
  470. continue;
  471. mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead);
  472. if (!wbuf->need_sync) {
  473. mutex_unlock(&wbuf->io_mutex);
  474. continue;
  475. }
  476. err = ubifs_wbuf_sync_nolock(wbuf);
  477. mutex_unlock(&wbuf->io_mutex);
  478. if (err) {
  479. ubifs_err("cannot sync write-buffer, error %d", err);
  480. ubifs_ro_mode(c, err);
  481. goto out_timers;
  482. }
  483. }
  484. return 0;
  485. out_timers:
  486. /* Cancel all timers to prevent repeated errors */
  487. for (i = 0; i < c->jhead_cnt; i++) {
  488. struct ubifs_wbuf *wbuf = &c->jheads[i].wbuf;
  489. mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead);
  490. cancel_wbuf_timer_nolock(wbuf);
  491. mutex_unlock(&wbuf->io_mutex);
  492. }
  493. return err;
  494. }
  495. /**
  496. * ubifs_wbuf_write_nolock - write data to flash via write-buffer.
  497. * @wbuf: write-buffer
  498. * @buf: node to write
  499. * @len: node length
  500. *
  501. * This function writes data to flash via write-buffer @wbuf. This means that
  502. * the last piece of the node won't reach the flash media immediately if it
  503. * does not take whole max. write unit (@c->max_write_size). Instead, the node
  504. * will sit in RAM until the write-buffer is synchronized (e.g., by timer, or
  505. * because more data are appended to the write-buffer).
  506. *
  507. * This function returns zero in case of success and a negative error code in
  508. * case of failure. If the node cannot be written because there is no more
  509. * space in this logical eraseblock, %-ENOSPC is returned.
  510. */
  511. int ubifs_wbuf_write_nolock(struct ubifs_wbuf *wbuf, void *buf, int len)
  512. {
  513. struct ubifs_info *c = wbuf->c;
  514. int err, written, n, aligned_len = ALIGN(len, 8);
  515. dbg_io("%d bytes (%s) to jhead %s wbuf at LEB %d:%d", len,
  516. dbg_ntype(((struct ubifs_ch *)buf)->node_type),
  517. dbg_jhead(wbuf->jhead), wbuf->lnum, wbuf->offs + wbuf->used);
  518. ubifs_assert(len > 0 && wbuf->lnum >= 0 && wbuf->lnum < c->leb_cnt);
  519. ubifs_assert(wbuf->offs >= 0 && wbuf->offs % c->min_io_size == 0);
  520. ubifs_assert(!(wbuf->offs & 7) && wbuf->offs <= c->leb_size);
  521. ubifs_assert(wbuf->avail > 0 && wbuf->avail <= wbuf->size);
  522. ubifs_assert(wbuf->size >= c->min_io_size);
  523. ubifs_assert(wbuf->size <= c->max_write_size);
  524. ubifs_assert(wbuf->size % c->min_io_size == 0);
  525. ubifs_assert(mutex_is_locked(&wbuf->io_mutex));
  526. ubifs_assert(!c->ro_media && !c->ro_mount);
  527. if (c->leb_size - wbuf->offs >= c->max_write_size)
  528. ubifs_assert(!((wbuf->offs + wbuf->size) % c->max_write_size));
  529. if (c->leb_size - wbuf->offs - wbuf->used < aligned_len) {
  530. err = -ENOSPC;
  531. goto out;
  532. }
  533. cancel_wbuf_timer_nolock(wbuf);
  534. if (c->ro_error)
  535. return -EROFS;
  536. if (aligned_len <= wbuf->avail) {
  537. /*
  538. * The node is not very large and fits entirely within
  539. * write-buffer.
  540. */
  541. memcpy(wbuf->buf + wbuf->used, buf, len);
  542. if (aligned_len == wbuf->avail) {
  543. dbg_io("flush jhead %s wbuf to LEB %d:%d",
  544. dbg_jhead(wbuf->jhead), wbuf->lnum, wbuf->offs);
  545. err = ubi_leb_write(c->ubi, wbuf->lnum, wbuf->buf,
  546. wbuf->offs, wbuf->size,
  547. wbuf->dtype);
  548. if (err)
  549. goto out;
  550. spin_lock(&wbuf->lock);
  551. wbuf->offs += wbuf->size;
  552. if (c->leb_size - wbuf->offs >= c->max_write_size)
  553. wbuf->size = c->max_write_size;
  554. else
  555. wbuf->size = c->leb_size - wbuf->offs;
  556. wbuf->avail = wbuf->size;
  557. wbuf->used = 0;
  558. wbuf->next_ino = 0;
  559. spin_unlock(&wbuf->lock);
  560. } else {
  561. spin_lock(&wbuf->lock);
  562. wbuf->avail -= aligned_len;
  563. wbuf->used += aligned_len;
  564. spin_unlock(&wbuf->lock);
  565. }
  566. goto exit;
  567. }
  568. written = 0;
  569. if (wbuf->used) {
  570. /*
  571. * The node is large enough and does not fit entirely within
  572. * current available space. We have to fill and flush
  573. * write-buffer and switch to the next max. write unit.
  574. */
  575. dbg_io("flush jhead %s wbuf to LEB %d:%d",
  576. dbg_jhead(wbuf->jhead), wbuf->lnum, wbuf->offs);
  577. memcpy(wbuf->buf + wbuf->used, buf, wbuf->avail);
  578. err = ubi_leb_write(c->ubi, wbuf->lnum, wbuf->buf, wbuf->offs,
  579. wbuf->size, wbuf->dtype);
  580. if (err)
  581. goto out;
  582. wbuf->offs += wbuf->size;
  583. len -= wbuf->avail;
  584. aligned_len -= wbuf->avail;
  585. written += wbuf->avail;
  586. } else if (wbuf->offs & (c->max_write_size - 1)) {
  587. /*
  588. * The write-buffer offset is not aligned to
  589. * @c->max_write_size and @wbuf->size is less than
  590. * @c->max_write_size. Write @wbuf->size bytes to make sure the
  591. * following writes are done in optimal @c->max_write_size
  592. * chunks.
  593. */
  594. dbg_io("write %d bytes to LEB %d:%d",
  595. wbuf->size, wbuf->lnum, wbuf->offs);
  596. err = ubi_leb_write(c->ubi, wbuf->lnum, buf, wbuf->offs,
  597. wbuf->size, wbuf->dtype);
  598. if (err)
  599. goto out;
  600. wbuf->offs += wbuf->size;
  601. len -= wbuf->size;
  602. aligned_len -= wbuf->size;
  603. written += wbuf->size;
  604. }
  605. /*
  606. * The remaining data may take more whole max. write units, so write the
  607. * remains multiple to max. write unit size directly to the flash media.
  608. * We align node length to 8-byte boundary because we anyway flash wbuf
  609. * if the remaining space is less than 8 bytes.
  610. */
  611. n = aligned_len >> c->max_write_shift;
  612. if (n) {
  613. n <<= c->max_write_shift;
  614. dbg_io("write %d bytes to LEB %d:%d", n, wbuf->lnum,
  615. wbuf->offs);
  616. err = ubi_leb_write(c->ubi, wbuf->lnum, buf + written,
  617. wbuf->offs, n, wbuf->dtype);
  618. if (err)
  619. goto out;
  620. wbuf->offs += n;
  621. aligned_len -= n;
  622. len -= n;
  623. written += n;
  624. }
  625. spin_lock(&wbuf->lock);
  626. if (aligned_len)
  627. /*
  628. * And now we have what's left and what does not take whole
  629. * max. write unit, so write it to the write-buffer and we are
  630. * done.
  631. */
  632. memcpy(wbuf->buf, buf + written, len);
  633. if (c->leb_size - wbuf->offs >= c->max_write_size)
  634. wbuf->size = c->max_write_size;
  635. else
  636. wbuf->size = c->leb_size - wbuf->offs;
  637. wbuf->avail = wbuf->size - aligned_len;
  638. wbuf->used = aligned_len;
  639. wbuf->next_ino = 0;
  640. spin_unlock(&wbuf->lock);
  641. exit:
  642. if (wbuf->sync_callback) {
  643. int free = c->leb_size - wbuf->offs - wbuf->used;
  644. err = wbuf->sync_callback(c, wbuf->lnum, free, 0);
  645. if (err)
  646. goto out;
  647. }
  648. if (wbuf->used)
  649. new_wbuf_timer_nolock(wbuf);
  650. return 0;
  651. out:
  652. ubifs_err("cannot write %d bytes to LEB %d:%d, error %d",
  653. len, wbuf->lnum, wbuf->offs, err);
  654. dbg_dump_node(c, buf);
  655. dbg_dump_stack();
  656. dbg_dump_leb(c, wbuf->lnum);
  657. return err;
  658. }
  659. /**
  660. * ubifs_write_node - write node to the media.
  661. * @c: UBIFS file-system description object
  662. * @buf: the node to write
  663. * @len: node length
  664. * @lnum: logical eraseblock number
  665. * @offs: offset within the logical eraseblock
  666. * @dtype: node life-time hint (%UBI_LONGTERM, %UBI_SHORTTERM, %UBI_UNKNOWN)
  667. *
  668. * This function automatically fills node magic number, assigns sequence
  669. * number, and calculates node CRC checksum. The length of the @buf buffer has
  670. * to be aligned to the minimal I/O unit size. This function automatically
  671. * appends padding node and padding bytes if needed. Returns zero in case of
  672. * success and a negative error code in case of failure.
  673. */
  674. int ubifs_write_node(struct ubifs_info *c, void *buf, int len, int lnum,
  675. int offs, int dtype)
  676. {
  677. int err, buf_len = ALIGN(len, c->min_io_size);
  678. dbg_io("LEB %d:%d, %s, length %d (aligned %d)",
  679. lnum, offs, dbg_ntype(((struct ubifs_ch *)buf)->node_type), len,
  680. buf_len);
  681. ubifs_assert(lnum >= 0 && lnum < c->leb_cnt && offs >= 0);
  682. ubifs_assert(offs % c->min_io_size == 0 && offs < c->leb_size);
  683. ubifs_assert(!c->ro_media && !c->ro_mount);
  684. if (c->ro_error)
  685. return -EROFS;
  686. ubifs_prepare_node(c, buf, len, 1);
  687. err = ubi_leb_write(c->ubi, lnum, buf, offs, buf_len, dtype);
  688. if (err) {
  689. ubifs_err("cannot write %d bytes to LEB %d:%d, error %d",
  690. buf_len, lnum, offs, err);
  691. dbg_dump_node(c, buf);
  692. dbg_dump_stack();
  693. }
  694. return err;
  695. }
  696. /**
  697. * ubifs_read_node_wbuf - read node from the media or write-buffer.
  698. * @wbuf: wbuf to check for un-written data
  699. * @buf: buffer to read to
  700. * @type: node type
  701. * @len: node length
  702. * @lnum: logical eraseblock number
  703. * @offs: offset within the logical eraseblock
  704. *
  705. * This function reads a node of known type and length, checks it and stores
  706. * in @buf. If the node partially or fully sits in the write-buffer, this
  707. * function takes data from the buffer, otherwise it reads the flash media.
  708. * Returns zero in case of success, %-EUCLEAN if CRC mismatched and a negative
  709. * error code in case of failure.
  710. */
  711. int ubifs_read_node_wbuf(struct ubifs_wbuf *wbuf, void *buf, int type, int len,
  712. int lnum, int offs)
  713. {
  714. const struct ubifs_info *c = wbuf->c;
  715. int err, rlen, overlap;
  716. struct ubifs_ch *ch = buf;
  717. dbg_io("LEB %d:%d, %s, length %d, jhead %s", lnum, offs,
  718. dbg_ntype(type), len, dbg_jhead(wbuf->jhead));
  719. ubifs_assert(wbuf && lnum >= 0 && lnum < c->leb_cnt && offs >= 0);
  720. ubifs_assert(!(offs & 7) && offs < c->leb_size);
  721. ubifs_assert(type >= 0 && type < UBIFS_NODE_TYPES_CNT);
  722. spin_lock(&wbuf->lock);
  723. overlap = (lnum == wbuf->lnum && offs + len > wbuf->offs);
  724. if (!overlap) {
  725. /* We may safely unlock the write-buffer and read the data */
  726. spin_unlock(&wbuf->lock);
  727. return ubifs_read_node(c, buf, type, len, lnum, offs);
  728. }
  729. /* Don't read under wbuf */
  730. rlen = wbuf->offs - offs;
  731. if (rlen < 0)
  732. rlen = 0;
  733. /* Copy the rest from the write-buffer */
  734. memcpy(buf + rlen, wbuf->buf + offs + rlen - wbuf->offs, len - rlen);
  735. spin_unlock(&wbuf->lock);
  736. if (rlen > 0) {
  737. /* Read everything that goes before write-buffer */
  738. err = ubi_read(c->ubi, lnum, buf, offs, rlen);
  739. if (err && err != -EBADMSG) {
  740. ubifs_err("failed to read node %d from LEB %d:%d, "
  741. "error %d", type, lnum, offs, err);
  742. dbg_dump_stack();
  743. return err;
  744. }
  745. }
  746. if (type != ch->node_type) {
  747. ubifs_err("bad node type (%d but expected %d)",
  748. ch->node_type, type);
  749. goto out;
  750. }
  751. err = ubifs_check_node(c, buf, lnum, offs, 0, 0);
  752. if (err) {
  753. ubifs_err("expected node type %d", type);
  754. return err;
  755. }
  756. rlen = le32_to_cpu(ch->len);
  757. if (rlen != len) {
  758. ubifs_err("bad node length %d, expected %d", rlen, len);
  759. goto out;
  760. }
  761. return 0;
  762. out:
  763. ubifs_err("bad node at LEB %d:%d", lnum, offs);
  764. dbg_dump_node(c, buf);
  765. dbg_dump_stack();
  766. return -EINVAL;
  767. }
  768. /**
  769. * ubifs_read_node - read node.
  770. * @c: UBIFS file-system description object
  771. * @buf: buffer to read to
  772. * @type: node type
  773. * @len: node length (not aligned)
  774. * @lnum: logical eraseblock number
  775. * @offs: offset within the logical eraseblock
  776. *
  777. * This function reads a node of known type and and length, checks it and
  778. * stores in @buf. Returns zero in case of success, %-EUCLEAN if CRC mismatched
  779. * and a negative error code in case of failure.
  780. */
  781. int ubifs_read_node(const struct ubifs_info *c, void *buf, int type, int len,
  782. int lnum, int offs)
  783. {
  784. int err, l;
  785. struct ubifs_ch *ch = buf;
  786. dbg_io("LEB %d:%d, %s, length %d", lnum, offs, dbg_ntype(type), len);
  787. ubifs_assert(lnum >= 0 && lnum < c->leb_cnt && offs >= 0);
  788. ubifs_assert(len >= UBIFS_CH_SZ && offs + len <= c->leb_size);
  789. ubifs_assert(!(offs & 7) && offs < c->leb_size);
  790. ubifs_assert(type >= 0 && type < UBIFS_NODE_TYPES_CNT);
  791. err = ubi_read(c->ubi, lnum, buf, offs, len);
  792. if (err && err != -EBADMSG) {
  793. ubifs_err("cannot read node %d from LEB %d:%d, error %d",
  794. type, lnum, offs, err);
  795. return err;
  796. }
  797. if (type != ch->node_type) {
  798. ubifs_err("bad node type (%d but expected %d)",
  799. ch->node_type, type);
  800. goto out;
  801. }
  802. err = ubifs_check_node(c, buf, lnum, offs, 0, 0);
  803. if (err) {
  804. ubifs_err("expected node type %d", type);
  805. return err;
  806. }
  807. l = le32_to_cpu(ch->len);
  808. if (l != len) {
  809. ubifs_err("bad node length %d, expected %d", l, len);
  810. goto out;
  811. }
  812. return 0;
  813. out:
  814. ubifs_err("bad node at LEB %d:%d, LEB mapping status %d", lnum, offs,
  815. ubi_is_mapped(c->ubi, lnum));
  816. dbg_dump_node(c, buf);
  817. dbg_dump_stack();
  818. return -EINVAL;
  819. }
  820. /**
  821. * ubifs_wbuf_init - initialize write-buffer.
  822. * @c: UBIFS file-system description object
  823. * @wbuf: write-buffer to initialize
  824. *
  825. * This function initializes write-buffer. Returns zero in case of success
  826. * %-ENOMEM in case of failure.
  827. */
  828. int ubifs_wbuf_init(struct ubifs_info *c, struct ubifs_wbuf *wbuf)
  829. {
  830. size_t size;
  831. wbuf->buf = kmalloc(c->max_write_size, GFP_KERNEL);
  832. if (!wbuf->buf)
  833. return -ENOMEM;
  834. size = (c->max_write_size / UBIFS_CH_SZ + 1) * sizeof(ino_t);
  835. wbuf->inodes = kmalloc(size, GFP_KERNEL);
  836. if (!wbuf->inodes) {
  837. kfree(wbuf->buf);
  838. wbuf->buf = NULL;
  839. return -ENOMEM;
  840. }
  841. wbuf->used = 0;
  842. wbuf->lnum = wbuf->offs = -1;
  843. /*
  844. * If the LEB starts at the max. write size aligned address, then
  845. * write-buffer size has to be set to @c->max_write_size. Otherwise,
  846. * set it to something smaller so that it ends at the closest max.
  847. * write size boundary.
  848. */
  849. size = c->max_write_size - (c->leb_start % c->max_write_size);
  850. wbuf->avail = wbuf->size = size;
  851. wbuf->dtype = UBI_UNKNOWN;
  852. wbuf->sync_callback = NULL;
  853. mutex_init(&wbuf->io_mutex);
  854. spin_lock_init(&wbuf->lock);
  855. wbuf->c = c;
  856. wbuf->next_ino = 0;
  857. hrtimer_init(&wbuf->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  858. wbuf->timer.function = wbuf_timer_callback_nolock;
  859. wbuf->softlimit = ktime_set(WBUF_TIMEOUT_SOFTLIMIT, 0);
  860. wbuf->delta = WBUF_TIMEOUT_HARDLIMIT - WBUF_TIMEOUT_SOFTLIMIT;
  861. wbuf->delta *= 1000000000ULL;
  862. ubifs_assert(wbuf->delta <= ULONG_MAX);
  863. return 0;
  864. }
  865. /**
  866. * ubifs_wbuf_add_ino_nolock - add an inode number into the wbuf inode array.
  867. * @wbuf: the write-buffer where to add
  868. * @inum: the inode number
  869. *
  870. * This function adds an inode number to the inode array of the write-buffer.
  871. */
  872. void ubifs_wbuf_add_ino_nolock(struct ubifs_wbuf *wbuf, ino_t inum)
  873. {
  874. if (!wbuf->buf)
  875. /* NOR flash or something similar */
  876. return;
  877. spin_lock(&wbuf->lock);
  878. if (wbuf->used)
  879. wbuf->inodes[wbuf->next_ino++] = inum;
  880. spin_unlock(&wbuf->lock);
  881. }
  882. /**
  883. * wbuf_has_ino - returns if the wbuf contains data from the inode.
  884. * @wbuf: the write-buffer
  885. * @inum: the inode number
  886. *
  887. * This function returns with %1 if the write-buffer contains some data from the
  888. * given inode otherwise it returns with %0.
  889. */
  890. static int wbuf_has_ino(struct ubifs_wbuf *wbuf, ino_t inum)
  891. {
  892. int i, ret = 0;
  893. spin_lock(&wbuf->lock);
  894. for (i = 0; i < wbuf->next_ino; i++)
  895. if (inum == wbuf->inodes[i]) {
  896. ret = 1;
  897. break;
  898. }
  899. spin_unlock(&wbuf->lock);
  900. return ret;
  901. }
  902. /**
  903. * ubifs_sync_wbufs_by_inode - synchronize write-buffers for an inode.
  904. * @c: UBIFS file-system description object
  905. * @inode: inode to synchronize
  906. *
  907. * This function synchronizes write-buffers which contain nodes belonging to
  908. * @inode. Returns zero in case of success and a negative error code in case of
  909. * failure.
  910. */
  911. int ubifs_sync_wbufs_by_inode(struct ubifs_info *c, struct inode *inode)
  912. {
  913. int i, err = 0;
  914. for (i = 0; i < c->jhead_cnt; i++) {
  915. struct ubifs_wbuf *wbuf = &c->jheads[i].wbuf;
  916. if (i == GCHD)
  917. /*
  918. * GC head is special, do not look at it. Even if the
  919. * head contains something related to this inode, it is
  920. * a _copy_ of corresponding on-flash node which sits
  921. * somewhere else.
  922. */
  923. continue;
  924. if (!wbuf_has_ino(wbuf, inode->i_ino))
  925. continue;
  926. mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead);
  927. if (wbuf_has_ino(wbuf, inode->i_ino))
  928. err = ubifs_wbuf_sync_nolock(wbuf);
  929. mutex_unlock(&wbuf->io_mutex);
  930. if (err) {
  931. ubifs_ro_mode(c, err);
  932. return err;
  933. }
  934. }
  935. return 0;
  936. }