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