io.c 33 KB

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