io.c 34 KB

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