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