io.c 33 KB

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