io.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152
  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->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->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->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->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(lnum >= 0 && lnum < c->leb_cnt && offs >= 0);
  227. ubifs_assert(!(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(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(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(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. * @wbuf: write-buffer descriptor
  408. */
  409. static void new_wbuf_timer_nolock(struct ubifs_wbuf *wbuf)
  410. {
  411. ktime_t softlimit = ms_to_ktime(dirty_writeback_interval * 10);
  412. unsigned long long delta = dirty_writeback_interval;
  413. /* centi to milli, milli to nano, then 10% */
  414. delta *= 10ULL * NSEC_PER_MSEC / 10ULL;
  415. ubifs_assert(!hrtimer_active(&wbuf->timer));
  416. ubifs_assert(delta <= ULONG_MAX);
  417. if (wbuf->no_timer)
  418. return;
  419. dbg_io("set timer for jhead %s, %llu-%llu millisecs",
  420. dbg_jhead(wbuf->jhead),
  421. div_u64(ktime_to_ns(softlimit), USEC_PER_SEC),
  422. div_u64(ktime_to_ns(softlimit) + delta, USEC_PER_SEC));
  423. hrtimer_start_range_ns(&wbuf->timer, softlimit, delta,
  424. HRTIMER_MODE_REL);
  425. }
  426. /**
  427. * cancel_wbuf_timer - cancel write-buffer timer.
  428. * @wbuf: write-buffer descriptor
  429. */
  430. static void cancel_wbuf_timer_nolock(struct ubifs_wbuf *wbuf)
  431. {
  432. if (wbuf->no_timer)
  433. return;
  434. wbuf->need_sync = 0;
  435. hrtimer_cancel(&wbuf->timer);
  436. }
  437. /**
  438. * ubifs_wbuf_sync_nolock - synchronize write-buffer.
  439. * @wbuf: write-buffer to synchronize
  440. *
  441. * This function synchronizes write-buffer @buf and returns zero in case of
  442. * success or a negative error code in case of failure.
  443. *
  444. * Note, although write-buffers are of @c->max_write_size, this function does
  445. * not necessarily writes all @c->max_write_size bytes to the flash. Instead,
  446. * if the write-buffer is only partially filled with data, only the used part
  447. * of the write-buffer (aligned on @c->min_io_size boundary) is synchronized.
  448. * This way we waste less space.
  449. */
  450. int ubifs_wbuf_sync_nolock(struct ubifs_wbuf *wbuf)
  451. {
  452. struct ubifs_info *c = wbuf->c;
  453. int err, dirt, sync_len;
  454. cancel_wbuf_timer_nolock(wbuf);
  455. if (!wbuf->used || wbuf->lnum == -1)
  456. /* Write-buffer is empty or not seeked */
  457. return 0;
  458. dbg_io("LEB %d:%d, %d bytes, jhead %s",
  459. wbuf->lnum, wbuf->offs, wbuf->used, dbg_jhead(wbuf->jhead));
  460. ubifs_assert(!(wbuf->avail & 7));
  461. ubifs_assert(wbuf->offs + wbuf->size <= c->leb_size);
  462. ubifs_assert(wbuf->size >= c->min_io_size);
  463. ubifs_assert(wbuf->size <= c->max_write_size);
  464. ubifs_assert(wbuf->size % c->min_io_size == 0);
  465. ubifs_assert(!c->ro_media && !c->ro_mount);
  466. if (c->leb_size - wbuf->offs >= c->max_write_size)
  467. ubifs_assert(!((wbuf->offs + wbuf->size) % c->max_write_size));
  468. if (c->ro_error)
  469. return -EROFS;
  470. /*
  471. * Do not write whole write buffer but write only the minimum necessary
  472. * amount of min. I/O units.
  473. */
  474. sync_len = ALIGN(wbuf->used, c->min_io_size);
  475. dirt = sync_len - wbuf->used;
  476. if (dirt)
  477. ubifs_pad(c, wbuf->buf + wbuf->used, dirt);
  478. err = ubifs_leb_write(c, wbuf->lnum, wbuf->buf, wbuf->offs, sync_len);
  479. if (err)
  480. return err;
  481. spin_lock(&wbuf->lock);
  482. wbuf->offs += sync_len;
  483. /*
  484. * Now @wbuf->offs is not necessarily aligned to @c->max_write_size.
  485. * But our goal is to optimize writes and make sure we write in
  486. * @c->max_write_size chunks and to @c->max_write_size-aligned offset.
  487. * Thus, if @wbuf->offs is not aligned to @c->max_write_size now, make
  488. * sure that @wbuf->offs + @wbuf->size is aligned to
  489. * @c->max_write_size. This way we make sure that after next
  490. * write-buffer flush we are again at the optimal offset (aligned to
  491. * @c->max_write_size).
  492. */
  493. if (c->leb_size - wbuf->offs < c->max_write_size)
  494. wbuf->size = c->leb_size - wbuf->offs;
  495. else if (wbuf->offs & (c->max_write_size - 1))
  496. wbuf->size = ALIGN(wbuf->offs, c->max_write_size) - wbuf->offs;
  497. else
  498. wbuf->size = c->max_write_size;
  499. wbuf->avail = wbuf->size;
  500. wbuf->used = 0;
  501. wbuf->next_ino = 0;
  502. spin_unlock(&wbuf->lock);
  503. if (wbuf->sync_callback)
  504. err = wbuf->sync_callback(c, wbuf->lnum,
  505. c->leb_size - wbuf->offs, dirt);
  506. return err;
  507. }
  508. /**
  509. * ubifs_wbuf_seek_nolock - seek write-buffer.
  510. * @wbuf: write-buffer
  511. * @lnum: logical eraseblock number to seek to
  512. * @offs: logical eraseblock offset to seek to
  513. *
  514. * This function targets the write-buffer to logical eraseblock @lnum:@offs.
  515. * The write-buffer has to be empty. Returns zero in case of success and a
  516. * negative error code in case of failure.
  517. */
  518. int ubifs_wbuf_seek_nolock(struct ubifs_wbuf *wbuf, int lnum, int offs)
  519. {
  520. const struct ubifs_info *c = wbuf->c;
  521. dbg_io("LEB %d:%d, jhead %s", lnum, offs, dbg_jhead(wbuf->jhead));
  522. ubifs_assert(lnum >= 0 && lnum < c->leb_cnt);
  523. ubifs_assert(offs >= 0 && offs <= c->leb_size);
  524. ubifs_assert(offs % c->min_io_size == 0 && !(offs & 7));
  525. ubifs_assert(lnum != wbuf->lnum);
  526. ubifs_assert(wbuf->used == 0);
  527. spin_lock(&wbuf->lock);
  528. wbuf->lnum = lnum;
  529. wbuf->offs = offs;
  530. if (c->leb_size - wbuf->offs < c->max_write_size)
  531. wbuf->size = c->leb_size - wbuf->offs;
  532. else if (wbuf->offs & (c->max_write_size - 1))
  533. wbuf->size = ALIGN(wbuf->offs, c->max_write_size) - wbuf->offs;
  534. else
  535. wbuf->size = c->max_write_size;
  536. wbuf->avail = wbuf->size;
  537. wbuf->used = 0;
  538. spin_unlock(&wbuf->lock);
  539. return 0;
  540. }
  541. /**
  542. * ubifs_bg_wbufs_sync - synchronize write-buffers.
  543. * @c: UBIFS file-system description object
  544. *
  545. * This function is called by background thread to synchronize write-buffers.
  546. * Returns zero in case of success and a negative error code in case of
  547. * failure.
  548. */
  549. int ubifs_bg_wbufs_sync(struct ubifs_info *c)
  550. {
  551. int err, i;
  552. ubifs_assert(!c->ro_media && !c->ro_mount);
  553. if (!c->need_wbuf_sync)
  554. return 0;
  555. c->need_wbuf_sync = 0;
  556. if (c->ro_error) {
  557. err = -EROFS;
  558. goto out_timers;
  559. }
  560. dbg_io("synchronize");
  561. for (i = 0; i < c->jhead_cnt; i++) {
  562. struct ubifs_wbuf *wbuf = &c->jheads[i].wbuf;
  563. cond_resched();
  564. /*
  565. * If the mutex is locked then wbuf is being changed, so
  566. * synchronization is not necessary.
  567. */
  568. if (mutex_is_locked(&wbuf->io_mutex))
  569. continue;
  570. mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead);
  571. if (!wbuf->need_sync) {
  572. mutex_unlock(&wbuf->io_mutex);
  573. continue;
  574. }
  575. err = ubifs_wbuf_sync_nolock(wbuf);
  576. mutex_unlock(&wbuf->io_mutex);
  577. if (err) {
  578. ubifs_err(c, "cannot sync write-buffer, error %d", err);
  579. ubifs_ro_mode(c, err);
  580. goto out_timers;
  581. }
  582. }
  583. return 0;
  584. out_timers:
  585. /* Cancel all timers to prevent repeated errors */
  586. for (i = 0; i < c->jhead_cnt; i++) {
  587. struct ubifs_wbuf *wbuf = &c->jheads[i].wbuf;
  588. mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead);
  589. cancel_wbuf_timer_nolock(wbuf);
  590. mutex_unlock(&wbuf->io_mutex);
  591. }
  592. return err;
  593. }
  594. /**
  595. * ubifs_wbuf_write_nolock - write data to flash via write-buffer.
  596. * @wbuf: write-buffer
  597. * @buf: node to write
  598. * @len: node length
  599. *
  600. * This function writes data to flash via write-buffer @wbuf. This means that
  601. * the last piece of the node won't reach the flash media immediately if it
  602. * does not take whole max. write unit (@c->max_write_size). Instead, the node
  603. * will sit in RAM until the write-buffer is synchronized (e.g., by timer, or
  604. * because more data are appended to the write-buffer).
  605. *
  606. * This function returns zero in case of success and a negative error code in
  607. * case of failure. If the node cannot be written because there is no more
  608. * space in this logical eraseblock, %-ENOSPC is returned.
  609. */
  610. int ubifs_wbuf_write_nolock(struct ubifs_wbuf *wbuf, void *buf, int len)
  611. {
  612. struct ubifs_info *c = wbuf->c;
  613. int err, written, n, aligned_len = ALIGN(len, 8);
  614. dbg_io("%d bytes (%s) to jhead %s wbuf at LEB %d:%d", len,
  615. dbg_ntype(((struct ubifs_ch *)buf)->node_type),
  616. dbg_jhead(wbuf->jhead), wbuf->lnum, wbuf->offs + wbuf->used);
  617. ubifs_assert(len > 0 && wbuf->lnum >= 0 && wbuf->lnum < c->leb_cnt);
  618. ubifs_assert(wbuf->offs >= 0 && wbuf->offs % c->min_io_size == 0);
  619. ubifs_assert(!(wbuf->offs & 7) && wbuf->offs <= c->leb_size);
  620. ubifs_assert(wbuf->avail > 0 && wbuf->avail <= wbuf->size);
  621. ubifs_assert(wbuf->size >= c->min_io_size);
  622. ubifs_assert(wbuf->size <= c->max_write_size);
  623. ubifs_assert(wbuf->size % c->min_io_size == 0);
  624. ubifs_assert(mutex_is_locked(&wbuf->io_mutex));
  625. ubifs_assert(!c->ro_media && !c->ro_mount);
  626. ubifs_assert(!c->space_fixup);
  627. if (c->leb_size - wbuf->offs >= c->max_write_size)
  628. ubifs_assert(!((wbuf->offs + wbuf->size) % c->max_write_size));
  629. if (c->leb_size - wbuf->offs - wbuf->used < aligned_len) {
  630. err = -ENOSPC;
  631. goto out;
  632. }
  633. cancel_wbuf_timer_nolock(wbuf);
  634. if (c->ro_error)
  635. return -EROFS;
  636. if (aligned_len <= wbuf->avail) {
  637. /*
  638. * The node is not very large and fits entirely within
  639. * write-buffer.
  640. */
  641. memcpy(wbuf->buf + wbuf->used, buf, len);
  642. if (aligned_len == wbuf->avail) {
  643. dbg_io("flush jhead %s wbuf to LEB %d:%d",
  644. dbg_jhead(wbuf->jhead), wbuf->lnum, wbuf->offs);
  645. err = ubifs_leb_write(c, wbuf->lnum, wbuf->buf,
  646. wbuf->offs, wbuf->size);
  647. if (err)
  648. goto out;
  649. spin_lock(&wbuf->lock);
  650. wbuf->offs += wbuf->size;
  651. if (c->leb_size - wbuf->offs >= c->max_write_size)
  652. wbuf->size = c->max_write_size;
  653. else
  654. wbuf->size = c->leb_size - wbuf->offs;
  655. wbuf->avail = wbuf->size;
  656. wbuf->used = 0;
  657. wbuf->next_ino = 0;
  658. spin_unlock(&wbuf->lock);
  659. } else {
  660. spin_lock(&wbuf->lock);
  661. wbuf->avail -= aligned_len;
  662. wbuf->used += aligned_len;
  663. spin_unlock(&wbuf->lock);
  664. }
  665. goto exit;
  666. }
  667. written = 0;
  668. if (wbuf->used) {
  669. /*
  670. * The node is large enough and does not fit entirely within
  671. * current available space. We have to fill and flush
  672. * write-buffer and switch to the next max. write unit.
  673. */
  674. dbg_io("flush jhead %s wbuf to LEB %d:%d",
  675. dbg_jhead(wbuf->jhead), wbuf->lnum, wbuf->offs);
  676. memcpy(wbuf->buf + wbuf->used, buf, wbuf->avail);
  677. err = ubifs_leb_write(c, wbuf->lnum, wbuf->buf, wbuf->offs,
  678. wbuf->size);
  679. if (err)
  680. goto out;
  681. wbuf->offs += wbuf->size;
  682. len -= wbuf->avail;
  683. aligned_len -= wbuf->avail;
  684. written += wbuf->avail;
  685. } else if (wbuf->offs & (c->max_write_size - 1)) {
  686. /*
  687. * The write-buffer offset is not aligned to
  688. * @c->max_write_size and @wbuf->size is less than
  689. * @c->max_write_size. Write @wbuf->size bytes to make sure the
  690. * following writes are done in optimal @c->max_write_size
  691. * chunks.
  692. */
  693. dbg_io("write %d bytes to LEB %d:%d",
  694. wbuf->size, wbuf->lnum, wbuf->offs);
  695. err = ubifs_leb_write(c, wbuf->lnum, buf, wbuf->offs,
  696. wbuf->size);
  697. if (err)
  698. goto out;
  699. wbuf->offs += wbuf->size;
  700. len -= wbuf->size;
  701. aligned_len -= wbuf->size;
  702. written += wbuf->size;
  703. }
  704. /*
  705. * The remaining data may take more whole max. write units, so write the
  706. * remains multiple to max. write unit size directly to the flash media.
  707. * We align node length to 8-byte boundary because we anyway flash wbuf
  708. * if the remaining space is less than 8 bytes.
  709. */
  710. n = aligned_len >> c->max_write_shift;
  711. if (n) {
  712. n <<= c->max_write_shift;
  713. dbg_io("write %d bytes to LEB %d:%d", n, wbuf->lnum,
  714. wbuf->offs);
  715. err = ubifs_leb_write(c, wbuf->lnum, buf + written,
  716. wbuf->offs, n);
  717. if (err)
  718. goto out;
  719. wbuf->offs += n;
  720. aligned_len -= n;
  721. len -= n;
  722. written += n;
  723. }
  724. spin_lock(&wbuf->lock);
  725. if (aligned_len)
  726. /*
  727. * And now we have what's left and what does not take whole
  728. * max. write unit, so write it to the write-buffer and we are
  729. * done.
  730. */
  731. memcpy(wbuf->buf, buf + written, len);
  732. if (c->leb_size - wbuf->offs >= c->max_write_size)
  733. wbuf->size = c->max_write_size;
  734. else
  735. wbuf->size = c->leb_size - wbuf->offs;
  736. wbuf->avail = wbuf->size - aligned_len;
  737. wbuf->used = aligned_len;
  738. wbuf->next_ino = 0;
  739. spin_unlock(&wbuf->lock);
  740. exit:
  741. if (wbuf->sync_callback) {
  742. int free = c->leb_size - wbuf->offs - wbuf->used;
  743. err = wbuf->sync_callback(c, wbuf->lnum, free, 0);
  744. if (err)
  745. goto out;
  746. }
  747. if (wbuf->used)
  748. new_wbuf_timer_nolock(wbuf);
  749. return 0;
  750. out:
  751. ubifs_err(c, "cannot write %d bytes to LEB %d:%d, error %d",
  752. len, wbuf->lnum, wbuf->offs, err);
  753. ubifs_dump_node(c, buf);
  754. dump_stack();
  755. ubifs_dump_leb(c, wbuf->lnum);
  756. return err;
  757. }
  758. /**
  759. * ubifs_write_node - write node to the media.
  760. * @c: UBIFS file-system description object
  761. * @buf: the node to write
  762. * @len: node length
  763. * @lnum: logical eraseblock number
  764. * @offs: offset within the logical eraseblock
  765. *
  766. * This function automatically fills node magic number, assigns sequence
  767. * number, and calculates node CRC checksum. The length of the @buf buffer has
  768. * to be aligned to the minimal I/O unit size. This function automatically
  769. * appends padding node and padding bytes if needed. Returns zero in case of
  770. * success and a negative error code in case of failure.
  771. */
  772. int ubifs_write_node(struct ubifs_info *c, void *buf, int len, int lnum,
  773. int offs)
  774. {
  775. int err, buf_len = ALIGN(len, c->min_io_size);
  776. dbg_io("LEB %d:%d, %s, length %d (aligned %d)",
  777. lnum, offs, dbg_ntype(((struct ubifs_ch *)buf)->node_type), len,
  778. buf_len);
  779. ubifs_assert(lnum >= 0 && lnum < c->leb_cnt && offs >= 0);
  780. ubifs_assert(offs % c->min_io_size == 0 && offs < c->leb_size);
  781. ubifs_assert(!c->ro_media && !c->ro_mount);
  782. ubifs_assert(!c->space_fixup);
  783. if (c->ro_error)
  784. return -EROFS;
  785. ubifs_prepare_node(c, buf, len, 1);
  786. err = ubifs_leb_write(c, lnum, buf, offs, buf_len);
  787. if (err)
  788. ubifs_dump_node(c, buf);
  789. return err;
  790. }
  791. /**
  792. * ubifs_read_node_wbuf - read node from the media or write-buffer.
  793. * @wbuf: wbuf to check for un-written data
  794. * @buf: buffer to read to
  795. * @type: node type
  796. * @len: node length
  797. * @lnum: logical eraseblock number
  798. * @offs: offset within the logical eraseblock
  799. *
  800. * This function reads a node of known type and length, checks it and stores
  801. * in @buf. If the node partially or fully sits in the write-buffer, this
  802. * function takes data from the buffer, otherwise it reads the flash media.
  803. * Returns zero in case of success, %-EUCLEAN if CRC mismatched and a negative
  804. * error code in case of failure.
  805. */
  806. int ubifs_read_node_wbuf(struct ubifs_wbuf *wbuf, void *buf, int type, int len,
  807. int lnum, int offs)
  808. {
  809. const struct ubifs_info *c = wbuf->c;
  810. int err, rlen, overlap;
  811. struct ubifs_ch *ch = buf;
  812. dbg_io("LEB %d:%d, %s, length %d, jhead %s", lnum, offs,
  813. dbg_ntype(type), len, dbg_jhead(wbuf->jhead));
  814. ubifs_assert(wbuf && lnum >= 0 && lnum < c->leb_cnt && offs >= 0);
  815. ubifs_assert(!(offs & 7) && offs < c->leb_size);
  816. ubifs_assert(type >= 0 && type < UBIFS_NODE_TYPES_CNT);
  817. spin_lock(&wbuf->lock);
  818. overlap = (lnum == wbuf->lnum && offs + len > wbuf->offs);
  819. if (!overlap) {
  820. /* We may safely unlock the write-buffer and read the data */
  821. spin_unlock(&wbuf->lock);
  822. return ubifs_read_node(c, buf, type, len, lnum, offs);
  823. }
  824. /* Don't read under wbuf */
  825. rlen = wbuf->offs - offs;
  826. if (rlen < 0)
  827. rlen = 0;
  828. /* Copy the rest from the write-buffer */
  829. memcpy(buf + rlen, wbuf->buf + offs + rlen - wbuf->offs, len - rlen);
  830. spin_unlock(&wbuf->lock);
  831. if (rlen > 0) {
  832. /* Read everything that goes before write-buffer */
  833. err = ubifs_leb_read(c, lnum, buf, offs, rlen, 0);
  834. if (err && err != -EBADMSG)
  835. return err;
  836. }
  837. if (type != ch->node_type) {
  838. ubifs_err(c, "bad node type (%d but expected %d)",
  839. ch->node_type, type);
  840. goto out;
  841. }
  842. err = ubifs_check_node(c, buf, lnum, offs, 0, 0);
  843. if (err) {
  844. ubifs_err(c, "expected node type %d", type);
  845. return err;
  846. }
  847. rlen = le32_to_cpu(ch->len);
  848. if (rlen != len) {
  849. ubifs_err(c, "bad node length %d, expected %d", rlen, len);
  850. goto out;
  851. }
  852. return 0;
  853. out:
  854. ubifs_err(c, "bad node at LEB %d:%d", lnum, offs);
  855. ubifs_dump_node(c, buf);
  856. dump_stack();
  857. return -EINVAL;
  858. }
  859. /**
  860. * ubifs_read_node - read node.
  861. * @c: UBIFS file-system description object
  862. * @buf: buffer to read to
  863. * @type: node type
  864. * @len: node length (not aligned)
  865. * @lnum: logical eraseblock number
  866. * @offs: offset within the logical eraseblock
  867. *
  868. * This function reads a node of known type and and length, checks it and
  869. * stores in @buf. Returns zero in case of success, %-EUCLEAN if CRC mismatched
  870. * and a negative error code in case of failure.
  871. */
  872. int ubifs_read_node(const struct ubifs_info *c, void *buf, int type, int len,
  873. int lnum, int offs)
  874. {
  875. int err, l;
  876. struct ubifs_ch *ch = buf;
  877. dbg_io("LEB %d:%d, %s, length %d", lnum, offs, dbg_ntype(type), len);
  878. ubifs_assert(lnum >= 0 && lnum < c->leb_cnt && offs >= 0);
  879. ubifs_assert(len >= UBIFS_CH_SZ && offs + len <= c->leb_size);
  880. ubifs_assert(!(offs & 7) && offs < c->leb_size);
  881. ubifs_assert(type >= 0 && type < UBIFS_NODE_TYPES_CNT);
  882. err = ubifs_leb_read(c, lnum, buf, offs, len, 0);
  883. if (err && err != -EBADMSG)
  884. return err;
  885. if (type != ch->node_type) {
  886. ubifs_errc(c, "bad node type (%d but expected %d)",
  887. ch->node_type, type);
  888. goto out;
  889. }
  890. err = ubifs_check_node(c, buf, lnum, offs, 0, 0);
  891. if (err) {
  892. ubifs_errc(c, "expected node type %d", type);
  893. return err;
  894. }
  895. l = le32_to_cpu(ch->len);
  896. if (l != len) {
  897. ubifs_errc(c, "bad node length %d, expected %d", l, len);
  898. goto out;
  899. }
  900. return 0;
  901. out:
  902. ubifs_errc(c, "bad node at LEB %d:%d, LEB mapping status %d", lnum,
  903. offs, ubi_is_mapped(c->ubi, lnum));
  904. if (!c->probing) {
  905. ubifs_dump_node(c, buf);
  906. dump_stack();
  907. }
  908. return -EINVAL;
  909. }
  910. /**
  911. * ubifs_wbuf_init - initialize write-buffer.
  912. * @c: UBIFS file-system description object
  913. * @wbuf: write-buffer to initialize
  914. *
  915. * This function initializes write-buffer. Returns zero in case of success
  916. * %-ENOMEM in case of failure.
  917. */
  918. int ubifs_wbuf_init(struct ubifs_info *c, struct ubifs_wbuf *wbuf)
  919. {
  920. size_t size;
  921. wbuf->buf = kmalloc(c->max_write_size, GFP_KERNEL);
  922. if (!wbuf->buf)
  923. return -ENOMEM;
  924. size = (c->max_write_size / UBIFS_CH_SZ + 1) * sizeof(ino_t);
  925. wbuf->inodes = kmalloc(size, GFP_KERNEL);
  926. if (!wbuf->inodes) {
  927. kfree(wbuf->buf);
  928. wbuf->buf = NULL;
  929. return -ENOMEM;
  930. }
  931. wbuf->used = 0;
  932. wbuf->lnum = wbuf->offs = -1;
  933. /*
  934. * If the LEB starts at the max. write size aligned address, then
  935. * write-buffer size has to be set to @c->max_write_size. Otherwise,
  936. * set it to something smaller so that it ends at the closest max.
  937. * write size boundary.
  938. */
  939. size = c->max_write_size - (c->leb_start % c->max_write_size);
  940. wbuf->avail = wbuf->size = size;
  941. wbuf->sync_callback = NULL;
  942. mutex_init(&wbuf->io_mutex);
  943. spin_lock_init(&wbuf->lock);
  944. wbuf->c = c;
  945. wbuf->next_ino = 0;
  946. hrtimer_init(&wbuf->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  947. wbuf->timer.function = wbuf_timer_callback_nolock;
  948. return 0;
  949. }
  950. /**
  951. * ubifs_wbuf_add_ino_nolock - add an inode number into the wbuf inode array.
  952. * @wbuf: the write-buffer where to add
  953. * @inum: the inode number
  954. *
  955. * This function adds an inode number to the inode array of the write-buffer.
  956. */
  957. void ubifs_wbuf_add_ino_nolock(struct ubifs_wbuf *wbuf, ino_t inum)
  958. {
  959. if (!wbuf->buf)
  960. /* NOR flash or something similar */
  961. return;
  962. spin_lock(&wbuf->lock);
  963. if (wbuf->used)
  964. wbuf->inodes[wbuf->next_ino++] = inum;
  965. spin_unlock(&wbuf->lock);
  966. }
  967. /**
  968. * wbuf_has_ino - returns if the wbuf contains data from the inode.
  969. * @wbuf: the write-buffer
  970. * @inum: the inode number
  971. *
  972. * This function returns with %1 if the write-buffer contains some data from the
  973. * given inode otherwise it returns with %0.
  974. */
  975. static int wbuf_has_ino(struct ubifs_wbuf *wbuf, ino_t inum)
  976. {
  977. int i, ret = 0;
  978. spin_lock(&wbuf->lock);
  979. for (i = 0; i < wbuf->next_ino; i++)
  980. if (inum == wbuf->inodes[i]) {
  981. ret = 1;
  982. break;
  983. }
  984. spin_unlock(&wbuf->lock);
  985. return ret;
  986. }
  987. /**
  988. * ubifs_sync_wbufs_by_inode - synchronize write-buffers for an inode.
  989. * @c: UBIFS file-system description object
  990. * @inode: inode to synchronize
  991. *
  992. * This function synchronizes write-buffers which contain nodes belonging to
  993. * @inode. Returns zero in case of success and a negative error code in case of
  994. * failure.
  995. */
  996. int ubifs_sync_wbufs_by_inode(struct ubifs_info *c, struct inode *inode)
  997. {
  998. int i, err = 0;
  999. for (i = 0; i < c->jhead_cnt; i++) {
  1000. struct ubifs_wbuf *wbuf = &c->jheads[i].wbuf;
  1001. if (i == GCHD)
  1002. /*
  1003. * GC head is special, do not look at it. Even if the
  1004. * head contains something related to this inode, it is
  1005. * a _copy_ of corresponding on-flash node which sits
  1006. * somewhere else.
  1007. */
  1008. continue;
  1009. if (!wbuf_has_ino(wbuf, inode->i_ino))
  1010. continue;
  1011. mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead);
  1012. if (wbuf_has_ino(wbuf, inode->i_ino))
  1013. err = ubifs_wbuf_sync_nolock(wbuf);
  1014. mutex_unlock(&wbuf->io_mutex);
  1015. if (err) {
  1016. ubifs_ro_mode(c, err);
  1017. return err;
  1018. }
  1019. }
  1020. return 0;
  1021. }