io.c 33 KB

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