io.c 36 KB

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