io.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215
  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 |= MS_RDONLY;
  85. ubifs_warn("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("reading %d bytes from LEB %d:%d failed, error %d",
  105. len, lnum, offs, err);
  106. dump_stack();
  107. }
  108. return err;
  109. }
  110. #if defined(FEATURE_UBIFS_PERF_INDEX)
  111. int ubifs_leb_write_log(struct ubifs_info *c, int lnum, const void *buf, int offs,
  112. int len)
  113. {
  114. int err;
  115. unsigned long long time1 = sched_clock();
  116. err = ubifs_leb_write(c, lnum, buf, offs, len);
  117. ubifs_perf_lwcount(sched_clock() - time1, len);
  118. return err;
  119. }
  120. #endif
  121. int ubifs_leb_write(struct ubifs_info *c, int lnum, const void *buf, int offs,
  122. int len)
  123. {
  124. int err;
  125. ubifs_assert(!c->ro_media && !c->ro_mount);
  126. if (c->ro_error)
  127. return -EROFS;
  128. if (!dbg_is_tst_rcvry(c))
  129. err = ubi_leb_write(c->ubi, lnum, buf, offs, len);
  130. else
  131. err = dbg_leb_write(c, lnum, buf, offs, len);
  132. if (err) {
  133. ubifs_err("writing %d bytes to LEB %d:%d failed, error %d",
  134. len, lnum, offs, err);
  135. ubifs_ro_mode(c, err);
  136. dump_stack();
  137. }
  138. return err;
  139. }
  140. int ubifs_leb_change(struct ubifs_info *c, int lnum, const void *buf, int len)
  141. {
  142. int err;
  143. ubifs_assert(!c->ro_media && !c->ro_mount);
  144. if (c->ro_error)
  145. return -EROFS;
  146. if (!dbg_is_tst_rcvry(c))
  147. err = ubi_leb_change(c->ubi, lnum, buf, len);
  148. else
  149. err = dbg_leb_change(c, lnum, buf, len);
  150. if (err) {
  151. ubifs_err("changing %d bytes in LEB %d failed, error %d",
  152. len, lnum, err);
  153. ubifs_ro_mode(c, err);
  154. dump_stack();
  155. }
  156. return err;
  157. }
  158. int ubifs_leb_unmap(struct ubifs_info *c, int lnum)
  159. {
  160. int err;
  161. ubifs_assert(!c->ro_media && !c->ro_mount);
  162. if (c->ro_error)
  163. return -EROFS;
  164. if (!dbg_is_tst_rcvry(c))
  165. err = ubi_leb_unmap(c->ubi, lnum);
  166. else
  167. err = dbg_leb_unmap(c, lnum);
  168. if (err) {
  169. ubifs_err("unmap LEB %d failed, error %d", lnum, err);
  170. ubifs_ro_mode(c, err);
  171. dump_stack();
  172. }
  173. return err;
  174. }
  175. int ubifs_leb_map(struct ubifs_info *c, int lnum)
  176. {
  177. int err;
  178. ubifs_assert(!c->ro_media && !c->ro_mount);
  179. if (c->ro_error)
  180. return -EROFS;
  181. if (!dbg_is_tst_rcvry(c))
  182. err = ubi_leb_map(c->ubi, lnum);
  183. else
  184. err = dbg_leb_map(c, lnum);
  185. if (err) {
  186. ubifs_err("mapping LEB %d failed, error %d", lnum, err);
  187. ubifs_ro_mode(c, err);
  188. dump_stack();
  189. }
  190. return err;
  191. }
  192. int ubifs_is_mapped(const struct ubifs_info *c, int lnum)
  193. {
  194. int err;
  195. err = ubi_is_mapped(c->ubi, lnum);
  196. if (err < 0) {
  197. ubifs_err("ubi_is_mapped failed for LEB %d, error %d",
  198. lnum, err);
  199. dump_stack();
  200. }
  201. return err;
  202. }
  203. /**
  204. * ubifs_check_node - check node.
  205. * @c: UBIFS file-system description object
  206. * @buf: node to check
  207. * @lnum: logical eraseblock number
  208. * @offs: offset within the logical eraseblock
  209. * @quiet: print no messages
  210. * @must_chk_crc: indicates whether to always check the CRC
  211. *
  212. * This function checks node magic number and CRC checksum. This function also
  213. * validates node length to prevent UBIFS from becoming crazy when an attacker
  214. * feeds it a file-system image with incorrect nodes. For example, too large
  215. * node length in the common header could cause UBIFS to read memory outside of
  216. * allocated buffer when checking the CRC checksum.
  217. *
  218. * This function may skip data nodes CRC checking if @c->no_chk_data_crc is
  219. * true, which is controlled by corresponding UBIFS mount option. However, if
  220. * @must_chk_crc is true, then @c->no_chk_data_crc is ignored and CRC is
  221. * checked. Similarly, if @c->mounting or @c->remounting_rw is true (we are
  222. * mounting or re-mounting to R/W mode), @c->no_chk_data_crc is ignored and CRC
  223. * is checked. This is because during mounting or re-mounting from R/O mode to
  224. * R/W mode we may read journal nodes (when replying the journal or doing the
  225. * recovery) and the journal nodes may potentially be corrupted, so checking is
  226. * required.
  227. *
  228. * This function returns zero in case of success and %-EUCLEAN in case of bad
  229. * CRC or magic.
  230. */
  231. int ubifs_check_node(const struct ubifs_info *c, const void *buf, int lnum,
  232. int offs, int quiet, int must_chk_crc)
  233. {
  234. int err = -EINVAL, type, node_len;
  235. uint32_t crc, node_crc, magic;
  236. const struct ubifs_ch *ch = buf;
  237. ubifs_assert(lnum >= 0 && lnum < c->leb_cnt && offs >= 0);
  238. ubifs_assert(!(offs & 7) && offs < c->leb_size);
  239. magic = le32_to_cpu(ch->magic);
  240. if (magic != UBIFS_NODE_MAGIC) {
  241. if (!quiet)
  242. ubifs_err("bad magic %#08x, expected %#08x",
  243. magic, UBIFS_NODE_MAGIC);
  244. err = -EUCLEAN;
  245. goto out;
  246. }
  247. type = ch->node_type;
  248. if (type < 0 || type >= UBIFS_NODE_TYPES_CNT) {
  249. if (!quiet)
  250. ubifs_err("bad node type %d", type);
  251. goto out;
  252. }
  253. node_len = le32_to_cpu(ch->len);
  254. if (node_len + offs > c->leb_size)
  255. goto out_len;
  256. if (c->ranges[type].max_len == 0) {
  257. if (node_len != c->ranges[type].len)
  258. goto out_len;
  259. } else if (node_len < c->ranges[type].min_len ||
  260. node_len > c->ranges[type].max_len)
  261. goto out_len;
  262. if (!must_chk_crc && type == UBIFS_DATA_NODE && !c->mounting &&
  263. !c->remounting_rw && c->no_chk_data_crc)
  264. return 0;
  265. crc = crc32(UBIFS_CRC32_INIT, buf + 8, node_len - 8);
  266. node_crc = le32_to_cpu(ch->crc);
  267. if (crc != node_crc) {
  268. if (!quiet)
  269. ubifs_err("bad CRC: calculated %#08x, read %#08x",
  270. crc, node_crc);
  271. err = -EUCLEAN;
  272. goto out;
  273. }
  274. return 0;
  275. out_len:
  276. if (!quiet)
  277. ubifs_err("bad node length %d", node_len);
  278. out:
  279. if (!quiet) {
  280. ubifs_err("bad node at LEB %d:%d", lnum, offs);
  281. ubifs_dump_node(c, buf);
  282. dump_stack();
  283. }
  284. return err;
  285. }
  286. /**
  287. * ubifs_pad - pad flash space.
  288. * @c: UBIFS file-system description object
  289. * @buf: buffer to put padding to
  290. * @pad: how many bytes to pad
  291. *
  292. * The flash media obliges us to write only in chunks of %c->min_io_size and
  293. * when we have to write less data we add padding node to the write-buffer and
  294. * pad it to the next minimal I/O unit's boundary. Padding nodes help when the
  295. * media is being scanned. If the amount of wasted space is not enough to fit a
  296. * padding node which takes %UBIFS_PAD_NODE_SZ bytes, we write padding bytes
  297. * pattern (%UBIFS_PADDING_BYTE).
  298. *
  299. * Padding nodes are also used to fill gaps when the "commit-in-gaps" method is
  300. * used.
  301. */
  302. void ubifs_pad(const struct ubifs_info *c, void *buf, int pad)
  303. {
  304. uint32_t crc;
  305. ubifs_assert(pad >= 0 && !(pad & 7));
  306. if (pad >= UBIFS_PAD_NODE_SZ) {
  307. struct ubifs_ch *ch = buf;
  308. struct ubifs_pad_node *pad_node = buf;
  309. ch->magic = cpu_to_le32(UBIFS_NODE_MAGIC);
  310. ch->node_type = UBIFS_PAD_NODE;
  311. ch->group_type = UBIFS_NO_NODE_GROUP;
  312. ch->padding[0] = ch->padding[1] = 0;
  313. ch->sqnum = 0;
  314. ch->len = cpu_to_le32(UBIFS_PAD_NODE_SZ);
  315. pad -= UBIFS_PAD_NODE_SZ;
  316. pad_node->pad_len = cpu_to_le32(pad);
  317. crc = crc32(UBIFS_CRC32_INIT, buf + 8, UBIFS_PAD_NODE_SZ - 8);
  318. ch->crc = cpu_to_le32(crc);
  319. memset(buf + UBIFS_PAD_NODE_SZ, 0, pad);
  320. } else if (pad > 0)
  321. /* Too little space, padding node won't fit */
  322. memset(buf, UBIFS_PADDING_BYTE, pad);
  323. }
  324. /**
  325. * next_sqnum - get next sequence number.
  326. * @c: UBIFS file-system description object
  327. */
  328. static unsigned long long next_sqnum(struct ubifs_info *c)
  329. {
  330. unsigned long long sqnum;
  331. spin_lock(&c->cnt_lock);
  332. sqnum = ++c->max_sqnum;
  333. spin_unlock(&c->cnt_lock);
  334. if (unlikely(sqnum >= SQNUM_WARN_WATERMARK)) {
  335. if (sqnum >= SQNUM_WATERMARK) {
  336. ubifs_err("sequence number overflow %llu, end of life",
  337. sqnum);
  338. ubifs_ro_mode(c, -EINVAL);
  339. }
  340. ubifs_warn("running out of sequence numbers, end of life soon");
  341. }
  342. return sqnum;
  343. }
  344. /**
  345. * ubifs_prepare_node - prepare node to be written to flash.
  346. * @c: UBIFS file-system description object
  347. * @node: the node to pad
  348. * @len: node length
  349. * @pad: if the buffer has to be padded
  350. *
  351. * This function prepares node at @node to be written to the media - it
  352. * calculates node CRC, fills the common header, and adds proper padding up to
  353. * the next minimum I/O unit if @pad is not zero.
  354. */
  355. void ubifs_prepare_node(struct ubifs_info *c, void *node, int len, int pad)
  356. {
  357. uint32_t crc;
  358. struct ubifs_ch *ch = node;
  359. unsigned long long sqnum = next_sqnum(c);
  360. ubifs_assert(len >= UBIFS_CH_SZ);
  361. ch->magic = cpu_to_le32(UBIFS_NODE_MAGIC);
  362. ch->len = cpu_to_le32(len);
  363. ch->group_type = UBIFS_NO_NODE_GROUP;
  364. ch->sqnum = cpu_to_le64(sqnum);
  365. ch->padding[0] = ch->padding[1] = 0;
  366. crc = crc32(UBIFS_CRC32_INIT, node + 8, len - 8);
  367. ch->crc = cpu_to_le32(crc);
  368. if (pad) {
  369. len = ALIGN(len, 8);
  370. pad = ALIGN(len, c->min_io_size) - len;
  371. ubifs_pad(c, node + len, pad);
  372. }
  373. }
  374. /**
  375. * ubifs_prep_grp_node - prepare node of a group to be written to flash.
  376. * @c: UBIFS file-system description object
  377. * @node: the node to pad
  378. * @len: node length
  379. * @last: indicates the last node of the group
  380. *
  381. * This function prepares node at @node to be written to the media - it
  382. * calculates node CRC and fills the common header.
  383. */
  384. void ubifs_prep_grp_node(struct ubifs_info *c, void *node, int len, int last)
  385. {
  386. uint32_t crc;
  387. struct ubifs_ch *ch = node;
  388. unsigned long long sqnum = next_sqnum(c);
  389. ubifs_assert(len >= UBIFS_CH_SZ);
  390. ch->magic = cpu_to_le32(UBIFS_NODE_MAGIC);
  391. ch->len = cpu_to_le32(len);
  392. if (last)
  393. ch->group_type = UBIFS_LAST_OF_NODE_GROUP;
  394. else
  395. ch->group_type = UBIFS_IN_NODE_GROUP;
  396. ch->sqnum = cpu_to_le64(sqnum);
  397. ch->padding[0] = ch->padding[1] = 0;
  398. crc = crc32(UBIFS_CRC32_INIT, node + 8, len - 8);
  399. ch->crc = cpu_to_le32(crc);
  400. }
  401. /**
  402. * wbuf_timer_callback - write-buffer timer callback function.
  403. * @timer: timer data (write-buffer descriptor)
  404. *
  405. * This function is called when the write-buffer timer expires.
  406. */
  407. static enum hrtimer_restart wbuf_timer_callback_nolock(struct hrtimer *timer)
  408. {
  409. struct ubifs_wbuf *wbuf = container_of(timer, struct ubifs_wbuf, timer);
  410. dbg_io("jhead %s", dbg_jhead(wbuf->jhead));
  411. wbuf->need_sync = 1;
  412. wbuf->c->need_wbuf_sync = 1;
  413. ubifs_wake_up_bgt(wbuf->c);
  414. return HRTIMER_NORESTART;
  415. }
  416. /**
  417. * new_wbuf_timer - start new write-buffer timer.
  418. * @wbuf: write-buffer descriptor
  419. */
  420. static void new_wbuf_timer_nolock(struct ubifs_wbuf *wbuf)
  421. {
  422. ubifs_assert(!hrtimer_active(&wbuf->timer));
  423. if (wbuf->no_timer)
  424. return;
  425. dbg_io("set timer for jhead %s, %llu-%llu millisecs",
  426. dbg_jhead(wbuf->jhead),
  427. div_u64(ktime_to_ns(wbuf->softlimit), USEC_PER_SEC),
  428. div_u64(ktime_to_ns(wbuf->softlimit) + wbuf->delta,
  429. USEC_PER_SEC));
  430. hrtimer_start_range_ns(&wbuf->timer, wbuf->softlimit, wbuf->delta,
  431. HRTIMER_MODE_REL);
  432. }
  433. /**
  434. * cancel_wbuf_timer - cancel write-buffer timer.
  435. * @wbuf: write-buffer descriptor
  436. */
  437. static void cancel_wbuf_timer_nolock(struct ubifs_wbuf *wbuf)
  438. {
  439. if (wbuf->no_timer)
  440. return;
  441. wbuf->need_sync = 0;
  442. hrtimer_cancel(&wbuf->timer);
  443. }
  444. /**
  445. * ubifs_wbuf_sync_nolock - synchronize write-buffer.
  446. * @wbuf: write-buffer to synchronize
  447. *
  448. * This function synchronizes write-buffer @buf and returns zero in case of
  449. * success or a negative error code in case of failure.
  450. *
  451. * Note, although write-buffers are of @c->max_write_size, this function does
  452. * not necessarily writes all @c->max_write_size bytes to the flash. Instead,
  453. * if the write-buffer is only partially filled with data, only the used part
  454. * of the write-buffer (aligned on @c->min_io_size boundary) is synchronized.
  455. * This way we waste less space.
  456. */
  457. int ubifs_wbuf_sync_nolock(struct ubifs_wbuf *wbuf)
  458. {
  459. struct ubifs_info *c = wbuf->c;
  460. int err, dirt, sync_len;
  461. cancel_wbuf_timer_nolock(wbuf);
  462. if (!wbuf->used || wbuf->lnum == -1)
  463. /* Write-buffer is empty or not seeked */
  464. return 0;
  465. dbg_io("LEB %d:%d, %d bytes, jhead %s",
  466. wbuf->lnum, wbuf->offs, wbuf->used, dbg_jhead(wbuf->jhead));
  467. ubifs_assert(!(wbuf->avail & 7));
  468. ubifs_assert(wbuf->offs + wbuf->size <= c->leb_size);
  469. ubifs_assert(wbuf->size >= c->min_io_size);
  470. ubifs_assert(wbuf->size <= c->max_write_size);
  471. ubifs_assert(wbuf->size % c->min_io_size == 0);
  472. ubifs_assert(!c->ro_media && !c->ro_mount);
  473. if (c->leb_size - wbuf->offs >= c->max_write_size)
  474. ubifs_assert(!((wbuf->offs + wbuf->size) % c->max_write_size));
  475. if (c->ro_error)
  476. return -EROFS;
  477. /*
  478. * Do not write whole write buffer but write only the minimum necessary
  479. * amount of min. I/O units.
  480. */
  481. sync_len = ALIGN(wbuf->used, c->min_io_size);
  482. dirt = sync_len - wbuf->used;
  483. if (dirt)
  484. ubifs_pad(c, wbuf->buf + wbuf->used, dirt);
  485. #if defined(FEATURE_UBIFS_PERF_INDEX)
  486. if (wbuf->jhead == DATAHD)
  487. err = ubifs_leb_write_log(c, wbuf->lnum, wbuf->buf, wbuf->offs, sync_len);
  488. else
  489. #endif
  490. err = ubifs_leb_write(c, wbuf->lnum, wbuf->buf, wbuf->offs, sync_len);
  491. if (err)
  492. return err;
  493. wbuf->w_count += sync_len; /*MTK*/
  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(lnum >= 0 && lnum < c->leb_cnt);
  536. ubifs_assert(offs >= 0 && offs <= c->leb_size);
  537. ubifs_assert(offs % c->min_io_size == 0 && !(offs & 7));
  538. ubifs_assert(lnum != wbuf->lnum);
  539. ubifs_assert(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->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("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(len > 0 && wbuf->lnum >= 0 && wbuf->lnum < c->leb_cnt);
  631. ubifs_assert(wbuf->offs >= 0 && wbuf->offs % c->min_io_size == 0);
  632. ubifs_assert(!(wbuf->offs & 7) && wbuf->offs <= c->leb_size);
  633. ubifs_assert(wbuf->avail > 0 && wbuf->avail <= wbuf->size);
  634. ubifs_assert(wbuf->size >= c->min_io_size);
  635. ubifs_assert(wbuf->size <= c->max_write_size);
  636. ubifs_assert(wbuf->size % c->min_io_size == 0);
  637. ubifs_assert(mutex_is_locked(&wbuf->io_mutex));
  638. ubifs_assert(!c->ro_media && !c->ro_mount);
  639. ubifs_assert(!c->space_fixup);
  640. if (c->leb_size - wbuf->offs >= c->max_write_size)
  641. ubifs_assert(!((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 == wbuf->avail) {
  656. dbg_io("flush jhead %s wbuf to LEB %d:%d",
  657. dbg_jhead(wbuf->jhead), wbuf->lnum, wbuf->offs);
  658. #if defined(FEATURE_UBIFS_PERF_INDEX)
  659. if (wbuf->jhead == DATAHD)
  660. err = ubifs_leb_write_log(c, wbuf->lnum, wbuf->buf,
  661. wbuf->offs, wbuf->size);
  662. else
  663. #endif
  664. err = ubifs_leb_write(c, wbuf->lnum, wbuf->buf,
  665. wbuf->offs, wbuf->size);
  666. if (err)
  667. goto out;
  668. wbuf->w_count += wbuf->size; /*MTK*/
  669. spin_lock(&wbuf->lock);
  670. wbuf->offs += wbuf->size;
  671. if (c->leb_size - wbuf->offs >= c->max_write_size)
  672. wbuf->size = c->max_write_size;
  673. else
  674. wbuf->size = c->leb_size - wbuf->offs;
  675. wbuf->avail = wbuf->size;
  676. wbuf->used = 0;
  677. wbuf->next_ino = 0;
  678. spin_unlock(&wbuf->lock);
  679. } else {
  680. spin_lock(&wbuf->lock);
  681. wbuf->avail -= aligned_len;
  682. wbuf->used += aligned_len;
  683. spin_unlock(&wbuf->lock);
  684. }
  685. goto exit;
  686. }
  687. written = 0;
  688. if (wbuf->used) {
  689. /*
  690. * The node is large enough and does not fit entirely within
  691. * current available space. We have to fill and flush
  692. * write-buffer and switch to the next max. write unit.
  693. */
  694. dbg_io("flush jhead %s wbuf to LEB %d:%d",
  695. dbg_jhead(wbuf->jhead), wbuf->lnum, wbuf->offs);
  696. memcpy(wbuf->buf + wbuf->used, buf, wbuf->avail);
  697. #if defined(FEATURE_UBIFS_PERF_INDEX)
  698. if (wbuf->jhead == DATAHD)
  699. err = ubifs_leb_write_log(c, wbuf->lnum, wbuf->buf, wbuf->offs,
  700. wbuf->size);
  701. else
  702. #endif
  703. err = ubifs_leb_write(c, wbuf->lnum, wbuf->buf, wbuf->offs,
  704. wbuf->size);
  705. if (err)
  706. goto out;
  707. wbuf->w_count += wbuf->size; /*MTK*/
  708. wbuf->offs += wbuf->size;
  709. len -= wbuf->avail;
  710. aligned_len -= wbuf->avail;
  711. written += wbuf->avail;
  712. } else if (wbuf->offs & (c->max_write_size - 1)) {
  713. /*
  714. * The write-buffer offset is not aligned to
  715. * @c->max_write_size and @wbuf->size is less than
  716. * @c->max_write_size. Write @wbuf->size bytes to make sure the
  717. * following writes are done in optimal @c->max_write_size
  718. * chunks.
  719. */
  720. dbg_io("write %d bytes to LEB %d:%d",
  721. wbuf->size, wbuf->lnum, wbuf->offs);
  722. #if defined(FEATURE_UBIFS_PERF_INDEX)
  723. if (wbuf->jhead == DATAHD)
  724. err = ubifs_leb_write_log(c, wbuf->lnum, wbuf->buf, wbuf->offs,
  725. wbuf->size);
  726. else
  727. #endif
  728. err = ubifs_leb_write(c, wbuf->lnum, buf, wbuf->offs,
  729. wbuf->size);
  730. if (err)
  731. goto out;
  732. wbuf->w_count += wbuf->size; /*MTK*/
  733. wbuf->offs += wbuf->size;
  734. len -= wbuf->size;
  735. aligned_len -= wbuf->size;
  736. written += wbuf->size;
  737. }
  738. /*
  739. * The remaining data may take more whole max. write units, so write the
  740. * remains multiple to max. write unit size directly to the flash media.
  741. * We align node length to 8-byte boundary because we anyway flash wbuf
  742. * if the remaining space is less than 8 bytes.
  743. */
  744. n = aligned_len >> c->max_write_shift;
  745. if (n) {
  746. n <<= c->max_write_shift;
  747. dbg_io("write %d bytes to LEB %d:%d", n, wbuf->lnum,
  748. wbuf->offs);
  749. #if defined(FEATURE_UBIFS_PERF_INDEX)
  750. if (wbuf->jhead == DATAHD)
  751. err = ubifs_leb_write_log(c, wbuf->lnum, buf + written,
  752. wbuf->offs, n);
  753. else
  754. #endif
  755. err = ubifs_leb_write(c, wbuf->lnum, buf + written,
  756. wbuf->offs, n);
  757. if (err)
  758. goto out;
  759. wbuf->w_count += n; /*MTK*/
  760. wbuf->offs += n;
  761. aligned_len -= n;
  762. len -= n;
  763. written += n;
  764. }
  765. spin_lock(&wbuf->lock);
  766. if (aligned_len)
  767. /*
  768. * And now we have what's left and what does not take whole
  769. * max. write unit, so write it to the write-buffer and we are
  770. * done.
  771. */
  772. memcpy(wbuf->buf, buf + written, len);
  773. if (c->leb_size - wbuf->offs >= c->max_write_size)
  774. wbuf->size = c->max_write_size;
  775. else
  776. wbuf->size = c->leb_size - wbuf->offs;
  777. wbuf->avail = wbuf->size - aligned_len;
  778. wbuf->used = aligned_len;
  779. wbuf->next_ino = 0;
  780. spin_unlock(&wbuf->lock);
  781. exit:
  782. if (wbuf->sync_callback) {
  783. int free = c->leb_size - wbuf->offs - wbuf->used;
  784. err = wbuf->sync_callback(c, wbuf->lnum, free, 0);
  785. if (err)
  786. goto out;
  787. }
  788. if (wbuf->used)
  789. new_wbuf_timer_nolock(wbuf);
  790. return 0;
  791. out:
  792. ubifs_err("cannot write %d bytes to LEB %d:%d, error %d",
  793. len, wbuf->lnum, wbuf->offs, err);
  794. ubifs_dump_node(c, buf);
  795. dump_stack();
  796. ubifs_dump_leb(c, wbuf->lnum);
  797. return err;
  798. }
  799. /**
  800. * ubifs_write_node - write node to the media.
  801. * @c: UBIFS file-system description object
  802. * @buf: the node to write
  803. * @len: node length
  804. * @lnum: logical eraseblock number
  805. * @offs: offset within the logical eraseblock
  806. *
  807. * This function automatically fills node magic number, assigns sequence
  808. * number, and calculates node CRC checksum. The length of the @buf buffer has
  809. * to be aligned to the minimal I/O unit size. This function automatically
  810. * appends padding node and padding bytes if needed. Returns zero in case of
  811. * success and a negative error code in case of failure.
  812. */
  813. int ubifs_write_node(struct ubifs_info *c, void *buf, int len, int lnum,
  814. int offs)
  815. {
  816. int err, buf_len = ALIGN(len, c->min_io_size);
  817. dbg_io("LEB %d:%d, %s, length %d (aligned %d)",
  818. lnum, offs, dbg_ntype(((struct ubifs_ch *)buf)->node_type), len,
  819. buf_len);
  820. ubifs_assert(lnum >= 0 && lnum < c->leb_cnt && offs >= 0);
  821. ubifs_assert(offs % c->min_io_size == 0 && offs < c->leb_size);
  822. ubifs_assert(!c->ro_media && !c->ro_mount);
  823. ubifs_assert(!c->space_fixup);
  824. if (c->ro_error)
  825. return -EROFS;
  826. ubifs_prepare_node(c, buf, len, 1);
  827. err = ubifs_leb_write(c, lnum, buf, offs, buf_len);
  828. if (err)
  829. ubifs_dump_node(c, buf);
  830. return err;
  831. }
  832. /**
  833. * ubifs_read_node_wbuf - read node from the media or write-buffer.
  834. * @wbuf: wbuf to check for un-written data
  835. * @buf: buffer to read to
  836. * @type: node type
  837. * @len: node length
  838. * @lnum: logical eraseblock number
  839. * @offs: offset within the logical eraseblock
  840. *
  841. * This function reads a node of known type and length, checks it and stores
  842. * in @buf. If the node partially or fully sits in the write-buffer, this
  843. * function takes data from the buffer, otherwise it reads the flash media.
  844. * Returns zero in case of success, %-EUCLEAN if CRC mismatched and a negative
  845. * error code in case of failure.
  846. */
  847. int ubifs_read_node_wbuf(struct ubifs_wbuf *wbuf, void *buf, int type, int len,
  848. int lnum, int offs)
  849. {
  850. const struct ubifs_info *c = wbuf->c;
  851. int err, rlen, overlap;
  852. struct ubifs_ch *ch = buf;
  853. #if defined(FEATURE_UBIFS_PERF_INDEX)
  854. unsigned long long time1 = sched_clock();
  855. int log_len = 0;
  856. #endif
  857. dbg_io("LEB %d:%d, %s, length %d, jhead %s", lnum, offs,
  858. dbg_ntype(type), len, dbg_jhead(wbuf->jhead));
  859. ubifs_assert(wbuf && lnum >= 0 && lnum < c->leb_cnt && offs >= 0);
  860. ubifs_assert(!(offs & 7) && offs < c->leb_size);
  861. ubifs_assert(type >= 0 && type < UBIFS_NODE_TYPES_CNT);
  862. spin_lock(&wbuf->lock);
  863. overlap = (lnum == wbuf->lnum && offs + len > wbuf->offs);
  864. if (!overlap) {
  865. /* We may safely unlock the write-buffer and read the data */
  866. spin_unlock(&wbuf->lock);
  867. return ubifs_read_node(c, buf, type, len, lnum, offs);
  868. }
  869. /* Don't read under wbuf */
  870. rlen = wbuf->offs - offs;
  871. if (rlen < 0)
  872. rlen = 0;
  873. /* Copy the rest from the write-buffer */
  874. memcpy(buf + rlen, wbuf->buf + offs + rlen - wbuf->offs, len - rlen);
  875. spin_unlock(&wbuf->lock);
  876. if (rlen > 0) {
  877. /* Read everything that goes before write-buffer */
  878. err = ubifs_leb_read(c, lnum, buf, offs, rlen, 0);
  879. if (err && err != -EBADMSG)
  880. return err;
  881. }
  882. if (type != ch->node_type) {
  883. ubifs_err("bad node type (%d but expected %d)",
  884. ch->node_type, type);
  885. goto out;
  886. }
  887. err = ubifs_check_node(c, buf, lnum, offs, 0, 0);
  888. if (err) {
  889. ubifs_err("expected node type %d", type);
  890. return err;
  891. }
  892. rlen = le32_to_cpu(ch->len);
  893. if (rlen != len) {
  894. ubifs_err("bad node length %d, expected %d", rlen, len);
  895. goto out;
  896. }
  897. #if defined(FEATURE_UBIFS_PERF_INDEX)
  898. if (log_len > 0)
  899. ubifs_perf_lrcount(sched_clock() - time1, log_len);
  900. #endif
  901. return 0;
  902. out:
  903. ubifs_err("bad node at LEB %d:%d", lnum, offs);
  904. ubifs_dump_node(c, buf);
  905. dump_stack();
  906. return -EINVAL;
  907. }
  908. /**
  909. * ubifs_read_node - read node.
  910. * @c: UBIFS file-system description object
  911. * @buf: buffer to read to
  912. * @type: node type
  913. * @len: node length (not aligned)
  914. * @lnum: logical eraseblock number
  915. * @offs: offset within the logical eraseblock
  916. *
  917. * This function reads a node of known type and and length, checks it and
  918. * stores in @buf. Returns zero in case of success, %-EUCLEAN if CRC mismatched
  919. * and a negative error code in case of failure.
  920. */
  921. int ubifs_read_node(const struct ubifs_info *c, void *buf, int type, int len,
  922. int lnum, int offs)
  923. {
  924. int err, l;
  925. struct ubifs_ch *ch = buf;
  926. #if defined(FEATURE_UBIFS_PERF_INDEX)
  927. unsigned long long time1 = sched_clock();
  928. #endif
  929. dbg_io("LEB %d:%d, %s, length %d", lnum, offs, dbg_ntype(type), len);
  930. ubifs_assert(lnum >= 0 && lnum < c->leb_cnt && offs >= 0);
  931. ubifs_assert(len >= UBIFS_CH_SZ && offs + len <= c->leb_size);
  932. ubifs_assert(!(offs & 7) && offs < c->leb_size);
  933. ubifs_assert(type >= 0 && type < UBIFS_NODE_TYPES_CNT);
  934. err = ubifs_leb_read(c, lnum, buf, offs, len, 0);
  935. if (err && err != -EBADMSG)
  936. return err;
  937. if (type != ch->node_type) {
  938. ubifs_errc(c, "bad node type (%d but expected %d)",
  939. ch->node_type, type);
  940. goto out;
  941. }
  942. err = ubifs_check_node(c, buf, lnum, offs, 0, 0);
  943. if (err) {
  944. ubifs_errc(c, "expected node type %d", type);
  945. return err;
  946. }
  947. l = le32_to_cpu(ch->len);
  948. if (l != len) {
  949. ubifs_errc(c, "bad node length %d, expected %d", l, len);
  950. goto out;
  951. }
  952. #if defined(FEATURE_UBIFS_PERF_INDEX)
  953. if (type == UBIFS_DATA_NODE)
  954. ubifs_perf_lrcount(sched_clock() - time1, len);
  955. #endif
  956. return 0;
  957. out:
  958. ubifs_errc(c, "bad node at LEB %d:%d, LEB mapping status %d", lnum,
  959. offs, ubi_is_mapped(c->ubi, lnum));
  960. if (!c->probing) {
  961. ubifs_dump_node(c, buf);
  962. dump_stack();
  963. }
  964. return -EINVAL;
  965. }
  966. /**
  967. * ubifs_wbuf_init - initialize write-buffer.
  968. * @c: UBIFS file-system description object
  969. * @wbuf: write-buffer to initialize
  970. *
  971. * This function initializes write-buffer. Returns zero in case of success
  972. * %-ENOMEM in case of failure.
  973. */
  974. int ubifs_wbuf_init(struct ubifs_info *c, struct ubifs_wbuf *wbuf)
  975. {
  976. size_t size;
  977. wbuf->buf = vmalloc(c->max_write_size);
  978. if (!wbuf->buf)
  979. return -ENOMEM;
  980. size = (c->max_write_size / UBIFS_CH_SZ + 1) * sizeof(ino_t);
  981. wbuf->inodes = vmalloc(size);
  982. if (!wbuf->inodes) {
  983. vfree(wbuf->buf);
  984. wbuf->buf = NULL;
  985. return -ENOMEM;
  986. }
  987. wbuf->used = 0;
  988. wbuf->lnum = wbuf->offs = -1;
  989. /*
  990. * If the LEB starts at the max. write size aligned address, then
  991. * write-buffer size has to be set to @c->max_write_size. Otherwise,
  992. * set it to something smaller so that it ends at the closest max.
  993. * write size boundary.
  994. */
  995. size = c->max_write_size - (c->leb_start % c->max_write_size);
  996. wbuf->avail = wbuf->size = size;
  997. wbuf->sync_callback = NULL;
  998. mutex_init(&wbuf->io_mutex);
  999. spin_lock_init(&wbuf->lock);
  1000. wbuf->c = c;
  1001. wbuf->next_ino = 0;
  1002. hrtimer_init(&wbuf->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  1003. wbuf->timer.function = wbuf_timer_callback_nolock;
  1004. wbuf->softlimit = ktime_set(WBUF_TIMEOUT_SOFTLIMIT, 0);
  1005. wbuf->delta = WBUF_TIMEOUT_HARDLIMIT - WBUF_TIMEOUT_SOFTLIMIT;
  1006. wbuf->delta *= 1000000000ULL;
  1007. ubifs_assert(wbuf->delta <= ULONG_MAX);
  1008. wbuf->w_count = 0; /*MTK*/
  1009. return 0;
  1010. }
  1011. /**
  1012. * ubifs_wbuf_add_ino_nolock - add an inode number into the wbuf inode array.
  1013. * @wbuf: the write-buffer where to add
  1014. * @inum: the inode number
  1015. *
  1016. * This function adds an inode number to the inode array of the write-buffer.
  1017. */
  1018. void ubifs_wbuf_add_ino_nolock(struct ubifs_wbuf *wbuf, ino_t inum)
  1019. {
  1020. if (!wbuf->buf)
  1021. /* NOR flash or something similar */
  1022. return;
  1023. spin_lock(&wbuf->lock);
  1024. if (wbuf->used)
  1025. wbuf->inodes[wbuf->next_ino++] = inum;
  1026. spin_unlock(&wbuf->lock);
  1027. }
  1028. /**
  1029. * wbuf_has_ino - returns if the wbuf contains data from the inode.
  1030. * @wbuf: the write-buffer
  1031. * @inum: the inode number
  1032. *
  1033. * This function returns with %1 if the write-buffer contains some data from the
  1034. * given inode otherwise it returns with %0.
  1035. */
  1036. static int wbuf_has_ino(struct ubifs_wbuf *wbuf, ino_t inum)
  1037. {
  1038. int i, ret = 0;
  1039. spin_lock(&wbuf->lock);
  1040. for (i = 0; i < wbuf->next_ino; i++)
  1041. if (inum == wbuf->inodes[i]) {
  1042. ret = 1;
  1043. break;
  1044. }
  1045. spin_unlock(&wbuf->lock);
  1046. return ret;
  1047. }
  1048. /**
  1049. * ubifs_sync_wbufs_by_inode - synchronize write-buffers for an inode.
  1050. * @c: UBIFS file-system description object
  1051. * @inode: inode to synchronize
  1052. *
  1053. * This function synchronizes write-buffers which contain nodes belonging to
  1054. * @inode. Returns zero in case of success and a negative error code in case of
  1055. * failure.
  1056. */
  1057. int ubifs_sync_wbufs_by_inode(struct ubifs_info *c, struct inode *inode)
  1058. {
  1059. int i, err = 0;
  1060. for (i = 0; i < c->jhead_cnt; i++) {
  1061. struct ubifs_wbuf *wbuf = &c->jheads[i].wbuf;
  1062. if (i == GCHD)
  1063. /*
  1064. * GC head is special, do not look at it. Even if the
  1065. * head contains something related to this inode, it is
  1066. * a _copy_ of corresponding on-flash node which sits
  1067. * somewhere else.
  1068. */
  1069. continue;
  1070. if (!wbuf_has_ino(wbuf, inode->i_ino))
  1071. continue;
  1072. mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead);
  1073. if (wbuf_has_ino(wbuf, inode->i_ino))
  1074. err = ubifs_wbuf_sync_nolock(wbuf);
  1075. mutex_unlock(&wbuf->io_mutex);
  1076. if (err) {
  1077. ubifs_ro_mode(c, err);
  1078. return err;
  1079. }
  1080. }
  1081. return 0;
  1082. }