io.c 34 KB

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