io.c 33 KB

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