upd.c 12.6 KB
Newer Older
A
Artem B. Bityutskiy 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
 * Copyright (c) International Business Machines Corp., 2006
 * Copyright (c) Nokia Corporation, 2006
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
 * the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 * Author: Artem Bityutskiy (Битюцкий Артём)
 *
 * Jan 2007: Alexander Schmidt, hacked per-volume update.
 */

/*
25 26
 * This file contains implementation of the volume update and atomic LEB change
 * functionality.
A
Artem B. Bityutskiy 已提交
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
 *
 * The update operation is based on the per-volume update marker which is
 * stored in the volume table. The update marker is set before the update
 * starts, and removed after the update has been finished. So if the update was
 * interrupted by an unclean re-boot or due to some other reasons, the update
 * marker stays on the flash media and UBI finds it when it attaches the MTD
 * device next time. If the update marker is set for a volume, the volume is
 * treated as damaged and most I/O operations are prohibited. Only a new update
 * operation is allowed.
 *
 * Note, in general it is possible to implement the update operation as a
 * transaction with a roll-back capability.
 */

#include <linux/err.h>
42
#include <linux/uaccess.h>
A
Artem Bityutskiy 已提交
43
#include <linux/math64.h>
A
Artem B. Bityutskiy 已提交
44 45 46 47 48
#include "ubi.h"

/**
 * set_update_marker - set update marker.
 * @ubi: UBI device description object
49
 * @vol: volume description object
A
Artem B. Bityutskiy 已提交
50
 *
51
 * This function sets the update marker flag for volume @vol. Returns zero
A
Artem B. Bityutskiy 已提交
52 53
 * in case of success and a negative error code in case of failure.
 */
54
static int set_update_marker(struct ubi_device *ubi, struct ubi_volume *vol)
A
Artem B. Bityutskiy 已提交
55 56 57 58
{
	int err;
	struct ubi_vtbl_record vtbl_rec;

59
	dbg_gen("set update marker for volume %d", vol->vol_id);
A
Artem B. Bityutskiy 已提交
60 61

	if (vol->upd_marker) {
62
		ubi_assert(ubi->vtbl[vol->vol_id].upd_marker);
63
		dbg_gen("already set");
A
Artem B. Bityutskiy 已提交
64 65 66
		return 0;
	}

67
	vtbl_rec = ubi->vtbl[vol->vol_id];
A
Artem B. Bityutskiy 已提交
68 69
	vtbl_rec.upd_marker = 1;

70
	mutex_lock(&ubi->device_mutex);
71
	err = ubi_change_vtbl_record(ubi, vol->vol_id, &vtbl_rec);
A
Artem B. Bityutskiy 已提交
72
	vol->upd_marker = 1;
A
Artem Bityutskiy 已提交
73
	mutex_unlock(&ubi->device_mutex);
A
Artem B. Bityutskiy 已提交
74 75 76 77 78 79
	return err;
}

/**
 * clear_update_marker - clear update marker.
 * @ubi: UBI device description object
80
 * @vol: volume description object
A
Artem B. Bityutskiy 已提交
81 82
 * @bytes: new data size in bytes
 *
83
 * This function clears the update marker for volume @vol, sets new volume
A
Artem B. Bityutskiy 已提交
84 85 86
 * data size and clears the "corrupted" flag (static volumes only). Returns
 * zero in case of success and a negative error code in case of failure.
 */
87 88
static int clear_update_marker(struct ubi_device *ubi, struct ubi_volume *vol,
			       long long bytes)
A
Artem B. Bityutskiy 已提交
89 90 91 92
{
	int err;
	struct ubi_vtbl_record vtbl_rec;

93
	dbg_gen("clear update marker for volume %d", vol->vol_id);
A
Artem B. Bityutskiy 已提交
94

95
	vtbl_rec = ubi->vtbl[vol->vol_id];
A
Artem B. Bityutskiy 已提交
96 97 98 99 100
	ubi_assert(vol->upd_marker && vtbl_rec.upd_marker);
	vtbl_rec.upd_marker = 0;

	if (vol->vol_type == UBI_STATIC_VOLUME) {
		vol->corrupted = 0;
A
Artem Bityutskiy 已提交
101 102 103
		vol->used_bytes = bytes;
		vol->used_ebs = div_u64_rem(bytes, vol->usable_leb_size,
					    &vol->last_eb_bytes);
A
Artem B. Bityutskiy 已提交
104 105 106 107 108 109
		if (vol->last_eb_bytes)
			vol->used_ebs += 1;
		else
			vol->last_eb_bytes = vol->usable_leb_size;
	}

110
	mutex_lock(&ubi->device_mutex);
111
	err = ubi_change_vtbl_record(ubi, vol->vol_id, &vtbl_rec);
A
Artem B. Bityutskiy 已提交
112
	vol->upd_marker = 0;
A
Artem Bityutskiy 已提交
113
	mutex_unlock(&ubi->device_mutex);
A
Artem B. Bityutskiy 已提交
114 115 116 117 118 119
	return err;
}

/**
 * ubi_start_update - start volume update.
 * @ubi: UBI device description object
120
 * @vol: volume description object
A
Artem B. Bityutskiy 已提交
121 122 123 124 125 126
 * @bytes: update bytes
 *
 * This function starts volume update operation. If @bytes is zero, the volume
 * is just wiped out. Returns zero in case of success and a negative error code
 * in case of failure.
 */
127 128
int ubi_start_update(struct ubi_device *ubi, struct ubi_volume *vol,
		     long long bytes)
A
Artem B. Bityutskiy 已提交
129 130 131
{
	int i, err;

132
	dbg_gen("start update of volume %d, %llu bytes", vol->vol_id, bytes);
133
	ubi_assert(!vol->updating && !vol->changing_leb);
A
Artem B. Bityutskiy 已提交
134 135
	vol->updating = 1;

R
Richard Weinberger 已提交
136 137 138 139
	vol->upd_buf = vmalloc(ubi->leb_size);
	if (!vol->upd_buf)
		return -ENOMEM;

140
	err = set_update_marker(ubi, vol);
A
Artem B. Bityutskiy 已提交
141 142 143 144 145
	if (err)
		return err;

	/* Before updating - wipe out the volume */
	for (i = 0; i < vol->reserved_pebs; i++) {
146
		err = ubi_eba_unmap_leb(ubi, vol, i);
A
Artem B. Bityutskiy 已提交
147 148 149 150 151
		if (err)
			return err;
	}

	if (bytes == 0) {
152
		err = ubi_wl_flush(ubi, UBI_ALL, UBI_ALL);
153 154 155
		if (err)
			return err;

156
		err = clear_update_marker(ubi, vol, 0);
A
Artem B. Bityutskiy 已提交
157 158
		if (err)
			return err;
R
Richard Weinberger 已提交
159 160

		vfree(vol->upd_buf);
161
		vol->updating = 0;
162
		return 0;
A
Artem B. Bityutskiy 已提交
163 164
	}

A
Artem Bityutskiy 已提交
165 166
	vol->upd_ebs = div_u64(bytes + vol->usable_leb_size - 1,
			       vol->usable_leb_size);
A
Artem B. Bityutskiy 已提交
167 168 169 170 171
	vol->upd_bytes = bytes;
	vol->upd_received = 0;
	return 0;
}

172 173 174 175 176 177 178 179 180 181 182 183 184 185
/**
 * ubi_start_leb_change - start atomic LEB change.
 * @ubi: UBI device description object
 * @vol: volume description object
 * @req: operation request
 *
 * This function starts atomic LEB change operation. Returns zero in case of
 * success and a negative error code in case of failure.
 */
int ubi_start_leb_change(struct ubi_device *ubi, struct ubi_volume *vol,
			 const struct ubi_leb_change_req *req)
{
	ubi_assert(!vol->updating && !vol->changing_leb);

186
	dbg_gen("start changing LEB %d:%d, %u bytes",
187 188
		vol->vol_id, req->lnum, req->bytes);
	if (req->bytes == 0)
R
Richard Weinberger 已提交
189
		return ubi_eba_atomic_leb_change(ubi, vol, req->lnum, NULL, 0);
190 191 192 193 194 195

	vol->upd_bytes = req->bytes;
	vol->upd_received = 0;
	vol->changing_leb = 1;
	vol->ch_lnum = req->lnum;

196
	vol->upd_buf = vmalloc(ALIGN((int)req->bytes, ubi->min_io_size));
197 198 199 200 201 202
	if (!vol->upd_buf)
		return -ENOMEM;

	return 0;
}

A
Artem B. Bityutskiy 已提交
203 204 205
/**
 * write_leb - write update data.
 * @ubi: UBI device description object
206
 * @vol: volume description object
A
Artem B. Bityutskiy 已提交
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
 * @lnum: logical eraseblock number
 * @buf: data to write
 * @len: data size
 * @used_ebs: how many logical eraseblocks will this volume contain (static
 * volumes only)
 *
 * This function writes update data to corresponding logical eraseblock. In
 * case of dynamic volume, this function checks if the data contains 0xFF bytes
 * at the end. If yes, the 0xFF bytes are cut and not written. So if the whole
 * buffer contains only 0xFF bytes, the LEB is left unmapped.
 *
 * The reason why we skip the trailing 0xFF bytes in case of dynamic volume is
 * that we want to make sure that more data may be appended to the logical
 * eraseblock in future. Indeed, writing 0xFF bytes may have side effects and
 * this PEB won't be writable anymore. So if one writes the file-system image
 * to the UBI volume where 0xFFs mean free space - UBI makes sure this free
 * space is writable after the update.
 *
 * We do not do this for static volumes because they are read-only. But this
 * also cannot be done because we have to store per-LEB CRC and the correct
 * data length.
 *
 * This function returns zero in case of success and a negative error code in
 * case of failure.
 */
232 233
static int write_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,
		     void *buf, int len, int used_ebs)
A
Artem B. Bityutskiy 已提交
234
{
235
	int err;
A
Artem B. Bityutskiy 已提交
236 237

	if (vol->vol_type == UBI_DYNAMIC_VOLUME) {
K
Kyungmin Park 已提交
238
		int l = ALIGN(len, ubi->min_io_size);
A
Artem B. Bityutskiy 已提交
239

K
Kyungmin Park 已提交
240 241
		memset(buf + len, 0xFF, l - len);
		len = ubi_calc_data_len(ubi, buf, l);
242
		if (len == 0) {
243
			dbg_gen("all %d bytes contain 0xFF - skip", len);
A
Artem B. Bityutskiy 已提交
244 245 246
			return 0;
		}

R
Richard Weinberger 已提交
247
		err = ubi_eba_write_leb(ubi, vol, lnum, buf, 0, len);
A
Artem B. Bityutskiy 已提交
248 249 250 251 252 253 254 255 256 257 258
	} else {
		/*
		 * When writing static volume, and this is the last logical
		 * eraseblock, the length (@len) does not have to be aligned to
		 * the minimal flash I/O unit. The 'ubi_eba_write_leb_st()'
		 * function accepts exact (unaligned) length and stores it in
		 * the VID header. And it takes care of proper alignment by
		 * padding the buffer. Here we just make sure the padding will
		 * contain zeros, not random trash.
		 */
		memset(buf + len, 0, vol->usable_leb_size - len);
R
Richard Weinberger 已提交
259
		err = ubi_eba_write_leb_st(ubi, vol, lnum, buf, len, used_ebs);
A
Artem B. Bityutskiy 已提交
260 261 262 263 264 265 266
	}

	return err;
}

/**
 * ubi_more_update_data - write more update data.
267
 * @ubi: UBI device description object
A
Artem B. Bityutskiy 已提交
268 269 270 271 272
 * @vol: volume description object
 * @buf: write data (user-space memory buffer)
 * @count: how much bytes to write
 *
 * This function writes more data to the volume which is being updated. It may
273 274 275
 * be called arbitrary number of times until all the update data arriveis. This
 * function returns %0 in case of success, number of bytes written during the
 * last call if the whole volume update has been successfully finished, and a
A
Artem B. Bityutskiy 已提交
276 277
 * negative error code in case of failure.
 */
278
int ubi_more_update_data(struct ubi_device *ubi, struct ubi_volume *vol,
A
Artem B. Bityutskiy 已提交
279 280 281 282
			 const void __user *buf, int count)
{
	int lnum, offs, err = 0, len, to_write = count;

283
	dbg_gen("write %d of %lld bytes, %lld already passed",
A
Artem B. Bityutskiy 已提交
284 285 286 287 288
		count, vol->upd_bytes, vol->upd_received);

	if (ubi->ro_mode)
		return -EROFS;

A
Artem Bityutskiy 已提交
289
	lnum = div_u64_rem(vol->upd_received,  vol->usable_leb_size, &offs);
A
Artem B. Bityutskiy 已提交
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
	if (vol->upd_received + count > vol->upd_bytes)
		to_write = count = vol->upd_bytes - vol->upd_received;

	/*
	 * When updating volumes, we accumulate whole logical eraseblock of
	 * data and write it at once.
	 */
	if (offs != 0) {
		/*
		 * This is a write to the middle of the logical eraseblock. We
		 * copy the data to our update buffer and wait for more data or
		 * flush it if the whole eraseblock is written or the update
		 * is finished.
		 */

		len = vol->usable_leb_size - offs;
		if (len > count)
			len = count;

		err = copy_from_user(vol->upd_buf + offs, buf, len);
		if (err)
			return -EFAULT;

		if (offs + len == vol->usable_leb_size ||
		    vol->upd_received + len == vol->upd_bytes) {
			int flush_len = offs + len;

			/*
			 * OK, we gathered either the whole eraseblock or this
			 * is the last chunk, it's time to flush the buffer.
			 */
			ubi_assert(flush_len <= vol->usable_leb_size);
322 323
			err = write_leb(ubi, vol, lnum, vol->upd_buf, flush_len,
					vol->upd_ebs);
A
Artem B. Bityutskiy 已提交
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
			if (err)
				return err;
		}

		vol->upd_received += len;
		count -= len;
		buf += len;
		lnum += 1;
	}

	/*
	 * If we've got more to write, let's continue. At this point we know we
	 * are starting from the beginning of an eraseblock.
	 */
	while (count) {
		if (count > vol->usable_leb_size)
			len = vol->usable_leb_size;
		else
			len = count;

		err = copy_from_user(vol->upd_buf, buf, len);
		if (err)
			return -EFAULT;

		if (len == vol->usable_leb_size ||
		    vol->upd_received + len == vol->upd_bytes) {
350 351
			err = write_leb(ubi, vol, lnum, vol->upd_buf,
					len, vol->upd_ebs);
A
Artem B. Bityutskiy 已提交
352 353 354 355 356 357 358 359 360 361 362 363
			if (err)
				break;
		}

		vol->upd_received += len;
		count -= len;
		lnum += 1;
		buf += len;
	}

	ubi_assert(vol->upd_received <= vol->upd_bytes);
	if (vol->upd_received == vol->upd_bytes) {
364
		err = ubi_wl_flush(ubi, UBI_ALL, UBI_ALL);
365 366
		if (err)
			return err;
A
Artem B. Bityutskiy 已提交
367
		/* The update is finished, clear the update marker */
368
		err = clear_update_marker(ubi, vol, vol->upd_bytes);
A
Artem B. Bityutskiy 已提交
369 370
		if (err)
			return err;
371 372 373
		vol->updating = 0;
		err = to_write;
		vfree(vol->upd_buf);
A
Artem B. Bityutskiy 已提交
374 375 376 377
	}

	return err;
}
378 379 380

/**
 * ubi_more_leb_change_data - accept more data for atomic LEB change.
381
 * @ubi: UBI device description object
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
 * @vol: volume description object
 * @buf: write data (user-space memory buffer)
 * @count: how much bytes to write
 *
 * This function accepts more data to the volume which is being under the
 * "atomic LEB change" operation. It may be called arbitrary number of times
 * until all data arrives. This function returns %0 in case of success, number
 * of bytes written during the last call if the whole "atomic LEB change"
 * operation has been successfully finished, and a negative error code in case
 * of failure.
 */
int ubi_more_leb_change_data(struct ubi_device *ubi, struct ubi_volume *vol,
			     const void __user *buf, int count)
{
	int err;

398
	dbg_gen("write %d of %lld bytes, %lld already passed",
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
		count, vol->upd_bytes, vol->upd_received);

	if (ubi->ro_mode)
		return -EROFS;

	if (vol->upd_received + count > vol->upd_bytes)
		count = vol->upd_bytes - vol->upd_received;

	err = copy_from_user(vol->upd_buf + vol->upd_received, buf, count);
	if (err)
		return -EFAULT;

	vol->upd_received += count;

	if (vol->upd_received == vol->upd_bytes) {
		int len = ALIGN((int)vol->upd_bytes, ubi->min_io_size);

416 417
		memset(vol->upd_buf + vol->upd_bytes, 0xFF,
		       len - vol->upd_bytes);
418 419
		len = ubi_calc_data_len(ubi, vol->upd_buf, len);
		err = ubi_eba_atomic_leb_change(ubi, vol, vol->ch_lnum,
R
Richard Weinberger 已提交
420
						vol->upd_buf, len);
421 422 423 424 425 426 427 428 429 430 431 432 433
		if (err)
			return err;
	}

	ubi_assert(vol->upd_received <= vol->upd_bytes);
	if (vol->upd_received == vol->upd_bytes) {
		vol->changing_leb = 0;
		err = count;
		vfree(vol->upd_buf);
	}

	return err;
}