Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) International Business Machines Corp., 2006 |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See |
| 12 | * the GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | * |
| 18 | * Author: Artem Bityutskiy (Битюцкий Артём) |
| 19 | */ |
| 20 | |
| 21 | /* |
| 22 | * This file contains implementation of volume creation, deletion, updating and |
| 23 | * resizing. |
| 24 | */ |
| 25 | |
| 26 | #include <linux/err.h> |
Artem Bityutskiy | 3013ee3 | 2009-01-16 19:08:43 +0200 | [diff] [blame] | 27 | #include <linux/math64.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 28 | #include <linux/slab.h> |
Paul Gortmaker | f3bcc01 | 2011-07-10 12:43:28 -0400 | [diff] [blame] | 29 | #include <linux/export.h> |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 30 | #include "ubi.h" |
| 31 | |
Artem Bityutskiy | 92d124f | 2011-03-14 18:17:40 +0200 | [diff] [blame] | 32 | #ifdef CONFIG_MTD_UBI_DEBUG |
Artem Bityutskiy | c856635 | 2008-07-16 17:40:22 +0300 | [diff] [blame] | 33 | static int paranoid_check_volumes(struct ubi_device *ubi); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 34 | #else |
Artem Bityutskiy | c856635 | 2008-07-16 17:40:22 +0300 | [diff] [blame] | 35 | #define paranoid_check_volumes(ubi) 0 |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 36 | #endif |
| 37 | |
| 38 | static ssize_t vol_attribute_show(struct device *dev, |
| 39 | struct device_attribute *attr, char *buf); |
| 40 | |
| 41 | /* Device attributes corresponding to files in '/<sysfs>/class/ubi/ubiX_Y' */ |
Artem Bityutskiy | 8bc2296 | 2007-07-22 15:25:02 +0300 | [diff] [blame] | 42 | static struct device_attribute attr_vol_reserved_ebs = |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 43 | __ATTR(reserved_ebs, S_IRUGO, vol_attribute_show, NULL); |
Artem Bityutskiy | 8bc2296 | 2007-07-22 15:25:02 +0300 | [diff] [blame] | 44 | static struct device_attribute attr_vol_type = |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 45 | __ATTR(type, S_IRUGO, vol_attribute_show, NULL); |
Artem Bityutskiy | 8bc2296 | 2007-07-22 15:25:02 +0300 | [diff] [blame] | 46 | static struct device_attribute attr_vol_name = |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 47 | __ATTR(name, S_IRUGO, vol_attribute_show, NULL); |
Artem Bityutskiy | 8bc2296 | 2007-07-22 15:25:02 +0300 | [diff] [blame] | 48 | static struct device_attribute attr_vol_corrupted = |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 49 | __ATTR(corrupted, S_IRUGO, vol_attribute_show, NULL); |
Artem Bityutskiy | 8bc2296 | 2007-07-22 15:25:02 +0300 | [diff] [blame] | 50 | static struct device_attribute attr_vol_alignment = |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 51 | __ATTR(alignment, S_IRUGO, vol_attribute_show, NULL); |
Artem Bityutskiy | 8bc2296 | 2007-07-22 15:25:02 +0300 | [diff] [blame] | 52 | static struct device_attribute attr_vol_usable_eb_size = |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 53 | __ATTR(usable_eb_size, S_IRUGO, vol_attribute_show, NULL); |
Artem Bityutskiy | 8bc2296 | 2007-07-22 15:25:02 +0300 | [diff] [blame] | 54 | static struct device_attribute attr_vol_data_bytes = |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 55 | __ATTR(data_bytes, S_IRUGO, vol_attribute_show, NULL); |
Artem Bityutskiy | 8bc2296 | 2007-07-22 15:25:02 +0300 | [diff] [blame] | 56 | static struct device_attribute attr_vol_upd_marker = |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 57 | __ATTR(upd_marker, S_IRUGO, vol_attribute_show, NULL); |
| 58 | |
| 59 | /* |
| 60 | * "Show" method for files in '/<sysfs>/class/ubi/ubiX_Y/'. |
| 61 | * |
| 62 | * Consider a situation: |
| 63 | * A. process 1 opens a sysfs file related to volume Y, say |
| 64 | * /<sysfs>/class/ubi/ubiX_Y/reserved_ebs; |
| 65 | * B. process 2 removes volume Y; |
| 66 | * C. process 1 starts reading the /<sysfs>/class/ubi/ubiX_Y/reserved_ebs file; |
| 67 | * |
Artem Bityutskiy | d05c77a | 2007-12-17 15:42:57 +0200 | [diff] [blame] | 68 | * In this situation, this function will return %-ENODEV because it will find |
| 69 | * out that the volume was removed from the @ubi->volumes array. |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 70 | */ |
| 71 | static ssize_t vol_attribute_show(struct device *dev, |
| 72 | struct device_attribute *attr, char *buf) |
| 73 | { |
Artem Bityutskiy | d05c77a | 2007-12-17 15:42:57 +0200 | [diff] [blame] | 74 | int ret; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 75 | struct ubi_volume *vol = container_of(dev, struct ubi_volume, dev); |
Artem Bityutskiy | e73f445 | 2007-12-17 17:37:26 +0200 | [diff] [blame] | 76 | struct ubi_device *ubi; |
| 77 | |
| 78 | ubi = ubi_get_device(vol->ubi->ubi_num); |
| 79 | if (!ubi) |
| 80 | return -ENODEV; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 81 | |
Artem Bityutskiy | d05c77a | 2007-12-17 15:42:57 +0200 | [diff] [blame] | 82 | spin_lock(&ubi->volumes_lock); |
| 83 | if (!ubi->volumes[vol->vol_id]) { |
| 84 | spin_unlock(&ubi->volumes_lock); |
Artem Bityutskiy | e73f445 | 2007-12-17 17:37:26 +0200 | [diff] [blame] | 85 | ubi_put_device(ubi); |
Artem Bityutskiy | d05c77a | 2007-12-17 15:42:57 +0200 | [diff] [blame] | 86 | return -ENODEV; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 87 | } |
Artem Bityutskiy | d05c77a | 2007-12-17 15:42:57 +0200 | [diff] [blame] | 88 | /* Take a reference to prevent volume removal */ |
| 89 | vol->ref_count += 1; |
| 90 | spin_unlock(&ubi->volumes_lock); |
Artem Bityutskiy | 732aeac | 2007-12-15 15:09:07 +0200 | [diff] [blame] | 91 | |
Artem Bityutskiy | 8bc2296 | 2007-07-22 15:25:02 +0300 | [diff] [blame] | 92 | if (attr == &attr_vol_reserved_ebs) |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 93 | ret = sprintf(buf, "%d\n", vol->reserved_pebs); |
Artem Bityutskiy | 8bc2296 | 2007-07-22 15:25:02 +0300 | [diff] [blame] | 94 | else if (attr == &attr_vol_type) { |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 95 | const char *tp; |
Artem Bityutskiy | 8bc2296 | 2007-07-22 15:25:02 +0300 | [diff] [blame] | 96 | |
| 97 | if (vol->vol_type == UBI_DYNAMIC_VOLUME) |
| 98 | tp = "dynamic"; |
| 99 | else |
| 100 | tp = "static"; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 101 | ret = sprintf(buf, "%s\n", tp); |
Artem Bityutskiy | 8bc2296 | 2007-07-22 15:25:02 +0300 | [diff] [blame] | 102 | } else if (attr == &attr_vol_name) |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 103 | ret = sprintf(buf, "%s\n", vol->name); |
Artem Bityutskiy | 8bc2296 | 2007-07-22 15:25:02 +0300 | [diff] [blame] | 104 | else if (attr == &attr_vol_corrupted) |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 105 | ret = sprintf(buf, "%d\n", vol->corrupted); |
Artem Bityutskiy | 8bc2296 | 2007-07-22 15:25:02 +0300 | [diff] [blame] | 106 | else if (attr == &attr_vol_alignment) |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 107 | ret = sprintf(buf, "%d\n", vol->alignment); |
Artem Bityutskiy | 732aeac | 2007-12-15 15:09:07 +0200 | [diff] [blame] | 108 | else if (attr == &attr_vol_usable_eb_size) |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 109 | ret = sprintf(buf, "%d\n", vol->usable_leb_size); |
Artem Bityutskiy | 732aeac | 2007-12-15 15:09:07 +0200 | [diff] [blame] | 110 | else if (attr == &attr_vol_data_bytes) |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 111 | ret = sprintf(buf, "%lld\n", vol->used_bytes); |
Artem Bityutskiy | 8bc2296 | 2007-07-22 15:25:02 +0300 | [diff] [blame] | 112 | else if (attr == &attr_vol_upd_marker) |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 113 | ret = sprintf(buf, "%d\n", vol->upd_marker); |
| 114 | else |
Artem Bityutskiy | d05c77a | 2007-12-17 15:42:57 +0200 | [diff] [blame] | 115 | /* This must be a bug */ |
| 116 | ret = -EINVAL; |
| 117 | |
Artem Bityutskiy | e73f445 | 2007-12-17 17:37:26 +0200 | [diff] [blame] | 118 | /* We've done the operation, drop volume and UBI device references */ |
Artem Bityutskiy | d05c77a | 2007-12-17 15:42:57 +0200 | [diff] [blame] | 119 | spin_lock(&ubi->volumes_lock); |
| 120 | vol->ref_count -= 1; |
| 121 | ubi_assert(vol->ref_count >= 0); |
| 122 | spin_unlock(&ubi->volumes_lock); |
Artem Bityutskiy | e73f445 | 2007-12-17 17:37:26 +0200 | [diff] [blame] | 123 | ubi_put_device(ubi); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 124 | return ret; |
| 125 | } |
| 126 | |
| 127 | /* Release method for volume devices */ |
| 128 | static void vol_release(struct device *dev) |
| 129 | { |
| 130 | struct ubi_volume *vol = container_of(dev, struct ubi_volume, dev); |
Artem Bityutskiy | 732aeac | 2007-12-15 15:09:07 +0200 | [diff] [blame] | 131 | |
Artem Bityutskiy | abc5e92 | 2008-06-04 16:48:12 +0300 | [diff] [blame] | 132 | kfree(vol->eba_tbl); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 133 | kfree(vol); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * volume_sysfs_init - initialize sysfs for new volume. |
| 138 | * @ubi: UBI device description object |
| 139 | * @vol: volume description object |
| 140 | * |
| 141 | * This function returns zero in case of success and a negative error code in |
| 142 | * case of failure. |
| 143 | * |
| 144 | * Note, this function does not free allocated resources in case of failure - |
| 145 | * the caller does it. This is because this would cause release() here and the |
| 146 | * caller would oops. |
| 147 | */ |
| 148 | static int volume_sysfs_init(struct ubi_device *ubi, struct ubi_volume *vol) |
| 149 | { |
| 150 | int err; |
| 151 | |
Artem Bityutskiy | 8bc2296 | 2007-07-22 15:25:02 +0300 | [diff] [blame] | 152 | err = device_create_file(&vol->dev, &attr_vol_reserved_ebs); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 153 | if (err) |
| 154 | return err; |
Artem Bityutskiy | 8bc2296 | 2007-07-22 15:25:02 +0300 | [diff] [blame] | 155 | err = device_create_file(&vol->dev, &attr_vol_type); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 156 | if (err) |
| 157 | return err; |
Artem Bityutskiy | 8bc2296 | 2007-07-22 15:25:02 +0300 | [diff] [blame] | 158 | err = device_create_file(&vol->dev, &attr_vol_name); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 159 | if (err) |
| 160 | return err; |
Artem Bityutskiy | 8bc2296 | 2007-07-22 15:25:02 +0300 | [diff] [blame] | 161 | err = device_create_file(&vol->dev, &attr_vol_corrupted); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 162 | if (err) |
| 163 | return err; |
Artem Bityutskiy | 8bc2296 | 2007-07-22 15:25:02 +0300 | [diff] [blame] | 164 | err = device_create_file(&vol->dev, &attr_vol_alignment); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 165 | if (err) |
| 166 | return err; |
Artem Bityutskiy | 8bc2296 | 2007-07-22 15:25:02 +0300 | [diff] [blame] | 167 | err = device_create_file(&vol->dev, &attr_vol_usable_eb_size); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 168 | if (err) |
| 169 | return err; |
Artem Bityutskiy | 8bc2296 | 2007-07-22 15:25:02 +0300 | [diff] [blame] | 170 | err = device_create_file(&vol->dev, &attr_vol_data_bytes); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 171 | if (err) |
| 172 | return err; |
Artem Bityutskiy | 8bc2296 | 2007-07-22 15:25:02 +0300 | [diff] [blame] | 173 | err = device_create_file(&vol->dev, &attr_vol_upd_marker); |
Artem Bityutskiy | fc75a1e | 2007-12-17 14:02:09 +0200 | [diff] [blame] | 174 | return err; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | /** |
| 178 | * volume_sysfs_close - close sysfs for a volume. |
| 179 | * @vol: volume description object |
| 180 | */ |
| 181 | static void volume_sysfs_close(struct ubi_volume *vol) |
| 182 | { |
Artem Bityutskiy | 8bc2296 | 2007-07-22 15:25:02 +0300 | [diff] [blame] | 183 | device_remove_file(&vol->dev, &attr_vol_upd_marker); |
| 184 | device_remove_file(&vol->dev, &attr_vol_data_bytes); |
| 185 | device_remove_file(&vol->dev, &attr_vol_usable_eb_size); |
| 186 | device_remove_file(&vol->dev, &attr_vol_alignment); |
| 187 | device_remove_file(&vol->dev, &attr_vol_corrupted); |
| 188 | device_remove_file(&vol->dev, &attr_vol_name); |
| 189 | device_remove_file(&vol->dev, &attr_vol_type); |
| 190 | device_remove_file(&vol->dev, &attr_vol_reserved_ebs); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 191 | device_unregister(&vol->dev); |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * ubi_create_volume - create volume. |
| 196 | * @ubi: UBI device description object |
| 197 | * @req: volume creation request |
| 198 | * |
| 199 | * This function creates volume described by @req. If @req->vol_id id |
Artem Bityutskiy | d05c77a | 2007-12-17 15:42:57 +0200 | [diff] [blame] | 200 | * %UBI_VOL_NUM_AUTO, this function automatically assign ID to the new volume |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 201 | * and saves it in @req->vol_id. Returns zero in case of success and a negative |
Artem Bityutskiy | 40e4d0c | 2007-12-17 17:08:55 +0200 | [diff] [blame] | 202 | * error code in case of failure. Note, the caller has to have the |
Artem Bityutskiy | f089c0b | 2009-05-07 11:46:49 +0300 | [diff] [blame] | 203 | * @ubi->device_mutex locked. |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 204 | */ |
| 205 | int ubi_create_volume(struct ubi_device *ubi, struct ubi_mkvol_req *req) |
| 206 | { |
Artem Bityutskiy | abc5e92 | 2008-06-04 16:48:12 +0300 | [diff] [blame] | 207 | int i, err, vol_id = req->vol_id, do_free = 1; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 208 | struct ubi_volume *vol; |
| 209 | struct ubi_vtbl_record vtbl_rec; |
Artem Bityutskiy | 49dfc29 | 2007-12-15 18:13:56 +0200 | [diff] [blame] | 210 | dev_t dev; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 211 | |
| 212 | if (ubi->ro_mode) |
| 213 | return -EROFS; |
| 214 | |
| 215 | vol = kzalloc(sizeof(struct ubi_volume), GFP_KERNEL); |
| 216 | if (!vol) |
| 217 | return -ENOMEM; |
| 218 | |
| 219 | spin_lock(&ubi->volumes_lock); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 220 | if (vol_id == UBI_VOL_NUM_AUTO) { |
| 221 | /* Find unused volume ID */ |
Artem Bityutskiy | c856635 | 2008-07-16 17:40:22 +0300 | [diff] [blame] | 222 | dbg_gen("search for vacant volume ID"); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 223 | for (i = 0; i < ubi->vtbl_slots; i++) |
| 224 | if (!ubi->volumes[i]) { |
| 225 | vol_id = i; |
| 226 | break; |
| 227 | } |
| 228 | |
| 229 | if (vol_id == UBI_VOL_NUM_AUTO) { |
| 230 | dbg_err("out of volume IDs"); |
| 231 | err = -ENFILE; |
| 232 | goto out_unlock; |
| 233 | } |
| 234 | req->vol_id = vol_id; |
| 235 | } |
| 236 | |
Artem Bityutskiy | e1cf7e6 | 2009-05-07 18:24:14 +0300 | [diff] [blame] | 237 | dbg_gen("create device %d, volume %d, %llu bytes, type %d, name %s", |
| 238 | ubi->ubi_num, vol_id, (unsigned long long)req->bytes, |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 239 | (int)req->vol_type, req->name); |
| 240 | |
| 241 | /* Ensure that this volume does not exist */ |
| 242 | err = -EEXIST; |
| 243 | if (ubi->volumes[vol_id]) { |
| 244 | dbg_err("volume %d already exists", vol_id); |
| 245 | goto out_unlock; |
| 246 | } |
| 247 | |
| 248 | /* Ensure that the name is unique */ |
| 249 | for (i = 0; i < ubi->vtbl_slots; i++) |
| 250 | if (ubi->volumes[i] && |
| 251 | ubi->volumes[i]->name_len == req->name_len && |
Artem Bityutskiy | 94784d9 | 2007-06-18 12:06:30 +0300 | [diff] [blame] | 252 | !strcmp(ubi->volumes[i]->name, req->name)) { |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 253 | dbg_err("volume \"%s\" exists (ID %d)", req->name, i); |
| 254 | goto out_unlock; |
| 255 | } |
| 256 | |
Artem Bityutskiy | 9c9ec14 | 2008-07-18 13:19:52 +0300 | [diff] [blame] | 257 | /* Calculate how many eraseblocks are requested */ |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 258 | vol->usable_leb_size = ubi->leb_size - ubi->leb_size % req->alignment; |
Artem Bityutskiy | 3013ee3 | 2009-01-16 19:08:43 +0200 | [diff] [blame] | 259 | vol->reserved_pebs += div_u64(req->bytes + vol->usable_leb_size - 1, |
| 260 | vol->usable_leb_size); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 261 | |
| 262 | /* Reserve physical eraseblocks */ |
| 263 | if (vol->reserved_pebs > ubi->avail_pebs) { |
| 264 | dbg_err("not enough PEBs, only %d available", ubi->avail_pebs); |
Artem Bityutskiy | 5fc01ab | 2010-09-03 23:08:15 +0300 | [diff] [blame] | 265 | if (ubi->corr_peb_count) |
| 266 | dbg_err("%d PEBs are corrupted and not used", |
| 267 | ubi->corr_peb_count); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 268 | err = -ENOSPC; |
| 269 | goto out_unlock; |
| 270 | } |
| 271 | ubi->avail_pebs -= vol->reserved_pebs; |
| 272 | ubi->rsvd_pebs += vol->reserved_pebs; |
Artem Bityutskiy | e73f445 | 2007-12-17 17:37:26 +0200 | [diff] [blame] | 273 | spin_unlock(&ubi->volumes_lock); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 274 | |
| 275 | vol->vol_id = vol_id; |
| 276 | vol->alignment = req->alignment; |
| 277 | vol->data_pad = ubi->leb_size % vol->alignment; |
| 278 | vol->vol_type = req->vol_type; |
| 279 | vol->name_len = req->name_len; |
Artem Bityutskiy | a6ea440 | 2008-07-13 21:46:24 +0300 | [diff] [blame] | 280 | memcpy(vol->name, req->name, vol->name_len); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 281 | vol->ubi = ubi; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 282 | |
| 283 | /* |
| 284 | * Finish all pending erases because there may be some LEBs belonging |
| 285 | * to the same volume ID. |
| 286 | */ |
| 287 | err = ubi_wl_flush(ubi); |
| 288 | if (err) |
| 289 | goto out_acc; |
| 290 | |
| 291 | vol->eba_tbl = kmalloc(vol->reserved_pebs * sizeof(int), GFP_KERNEL); |
| 292 | if (!vol->eba_tbl) { |
| 293 | err = -ENOMEM; |
| 294 | goto out_acc; |
| 295 | } |
| 296 | |
| 297 | for (i = 0; i < vol->reserved_pebs; i++) |
| 298 | vol->eba_tbl[i] = UBI_LEB_UNMAPPED; |
| 299 | |
| 300 | if (vol->vol_type == UBI_DYNAMIC_VOLUME) { |
| 301 | vol->used_ebs = vol->reserved_pebs; |
| 302 | vol->last_eb_bytes = vol->usable_leb_size; |
Vinit Agnihotri | d08c3b7 | 2007-07-10 13:04:59 +0300 | [diff] [blame] | 303 | vol->used_bytes = |
| 304 | (long long)vol->used_ebs * vol->usable_leb_size; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 305 | } else { |
Artem Bityutskiy | 3013ee3 | 2009-01-16 19:08:43 +0200 | [diff] [blame] | 306 | vol->used_ebs = div_u64_rem(vol->used_bytes, |
| 307 | vol->usable_leb_size, |
| 308 | &vol->last_eb_bytes); |
| 309 | if (vol->last_eb_bytes != 0) |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 310 | vol->used_ebs += 1; |
| 311 | else |
| 312 | vol->last_eb_bytes = vol->usable_leb_size; |
| 313 | } |
| 314 | |
| 315 | /* Register character device for the volume */ |
| 316 | cdev_init(&vol->cdev, &ubi_vol_cdev_operations); |
| 317 | vol->cdev.owner = THIS_MODULE; |
Artem Bityutskiy | 49dfc29 | 2007-12-15 18:13:56 +0200 | [diff] [blame] | 318 | dev = MKDEV(MAJOR(ubi->cdev.dev), vol_id + 1); |
| 319 | err = cdev_add(&vol->cdev, dev, 1); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 320 | if (err) { |
Artem Bityutskiy | 01f7b30 | 2007-12-15 19:56:51 +0200 | [diff] [blame] | 321 | ubi_err("cannot add character device"); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 322 | goto out_mapping; |
| 323 | } |
| 324 | |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 325 | vol->dev.release = vol_release; |
| 326 | vol->dev.parent = &ubi->dev; |
Artem Bityutskiy | 49dfc29 | 2007-12-15 18:13:56 +0200 | [diff] [blame] | 327 | vol->dev.devt = dev; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 328 | vol->dev.class = ubi_class; |
Artem Bityutskiy | fc75a1e | 2007-12-17 14:02:09 +0200 | [diff] [blame] | 329 | |
Kay Sievers | 475b44c | 2009-01-06 10:44:38 -0800 | [diff] [blame] | 330 | dev_set_name(&vol->dev, "%s_%d", ubi->ubi_name, vol->vol_id); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 331 | err = device_register(&vol->dev); |
Artem Bityutskiy | 01f7b30 | 2007-12-15 19:56:51 +0200 | [diff] [blame] | 332 | if (err) { |
| 333 | ubi_err("cannot register device"); |
Dmitry Pervushin | 518ceef | 2009-04-29 19:29:44 +0400 | [diff] [blame] | 334 | goto out_cdev; |
Artem Bityutskiy | 01f7b30 | 2007-12-15 19:56:51 +0200 | [diff] [blame] | 335 | } |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 336 | |
| 337 | err = volume_sysfs_init(ubi, vol); |
| 338 | if (err) |
| 339 | goto out_sysfs; |
| 340 | |
| 341 | /* Fill volume table record */ |
| 342 | memset(&vtbl_rec, 0, sizeof(struct ubi_vtbl_record)); |
Christoph Hellwig | 3261ebd | 2007-05-21 17:41:46 +0300 | [diff] [blame] | 343 | vtbl_rec.reserved_pebs = cpu_to_be32(vol->reserved_pebs); |
| 344 | vtbl_rec.alignment = cpu_to_be32(vol->alignment); |
| 345 | vtbl_rec.data_pad = cpu_to_be32(vol->data_pad); |
| 346 | vtbl_rec.name_len = cpu_to_be16(vol->name_len); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 347 | if (vol->vol_type == UBI_DYNAMIC_VOLUME) |
| 348 | vtbl_rec.vol_type = UBI_VID_DYNAMIC; |
| 349 | else |
| 350 | vtbl_rec.vol_type = UBI_VID_STATIC; |
Artem Bityutskiy | a6ea440 | 2008-07-13 21:46:24 +0300 | [diff] [blame] | 351 | memcpy(vtbl_rec.name, vol->name, vol->name_len); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 352 | |
| 353 | err = ubi_change_vtbl_record(ubi, vol_id, &vtbl_rec); |
| 354 | if (err) |
| 355 | goto out_sysfs; |
| 356 | |
| 357 | spin_lock(&ubi->volumes_lock); |
Artem Bityutskiy | d05c77a | 2007-12-17 15:42:57 +0200 | [diff] [blame] | 358 | ubi->volumes[vol_id] = vol; |
Artem Bityutskiy | 4b3cc34 | 2007-12-26 15:59:39 +0200 | [diff] [blame] | 359 | ubi->vol_count += 1; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 360 | spin_unlock(&ubi->volumes_lock); |
| 361 | |
Dmitry Pervushin | 0e0ee1c | 2009-04-29 19:29:38 +0400 | [diff] [blame] | 362 | ubi_volume_notify(ubi, vol, UBI_VOLUME_ADDED); |
Artem Bityutskiy | d38dce5 | 2009-05-13 14:05:24 +0300 | [diff] [blame] | 363 | if (paranoid_check_volumes(ubi)) |
| 364 | dbg_err("check failed while creating volume %d", vol_id); |
Artem Bityutskiy | c856635 | 2008-07-16 17:40:22 +0300 | [diff] [blame] | 365 | return err; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 366 | |
Artem Bityutskiy | fc75a1e | 2007-12-17 14:02:09 +0200 | [diff] [blame] | 367 | out_sysfs: |
| 368 | /* |
Artem Bityutskiy | abc5e92 | 2008-06-04 16:48:12 +0300 | [diff] [blame] | 369 | * We have registered our device, we should not free the volume |
Artem Bityutskiy | fc75a1e | 2007-12-17 14:02:09 +0200 | [diff] [blame] | 370 | * description object in this function in case of an error - it is |
| 371 | * freed by the release function. |
| 372 | * |
| 373 | * Get device reference to prevent the release function from being |
| 374 | * called just after sysfs has been closed. |
| 375 | */ |
Artem Bityutskiy | abc5e92 | 2008-06-04 16:48:12 +0300 | [diff] [blame] | 376 | do_free = 0; |
Artem Bityutskiy | fc75a1e | 2007-12-17 14:02:09 +0200 | [diff] [blame] | 377 | get_device(&vol->dev); |
| 378 | volume_sysfs_close(vol); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 379 | out_cdev: |
| 380 | cdev_del(&vol->cdev); |
| 381 | out_mapping: |
Artem Bityutskiy | abc5e92 | 2008-06-04 16:48:12 +0300 | [diff] [blame] | 382 | if (do_free) |
| 383 | kfree(vol->eba_tbl); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 384 | out_acc: |
| 385 | spin_lock(&ubi->volumes_lock); |
| 386 | ubi->rsvd_pebs -= vol->reserved_pebs; |
| 387 | ubi->avail_pebs += vol->reserved_pebs; |
| 388 | out_unlock: |
| 389 | spin_unlock(&ubi->volumes_lock); |
Artem Bityutskiy | abc5e92 | 2008-06-04 16:48:12 +0300 | [diff] [blame] | 390 | if (do_free) |
Artem Bityutskiy | fc75a1e | 2007-12-17 14:02:09 +0200 | [diff] [blame] | 391 | kfree(vol); |
Artem Bityutskiy | abc5e92 | 2008-06-04 16:48:12 +0300 | [diff] [blame] | 392 | else |
| 393 | put_device(&vol->dev); |
Artem Bityutskiy | 01f7b30 | 2007-12-15 19:56:51 +0200 | [diff] [blame] | 394 | ubi_err("cannot create volume %d, error %d", vol_id, err); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 395 | return err; |
| 396 | } |
| 397 | |
| 398 | /** |
| 399 | * ubi_remove_volume - remove volume. |
| 400 | * @desc: volume descriptor |
Artem Bityutskiy | f40ac9c | 2008-07-13 21:47:47 +0300 | [diff] [blame] | 401 | * @no_vtbl: do not change volume table if not zero |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 402 | * |
| 403 | * This function removes volume described by @desc. The volume has to be opened |
| 404 | * in "exclusive" mode. Returns zero in case of success and a negative error |
Artem Bityutskiy | f089c0b | 2009-05-07 11:46:49 +0300 | [diff] [blame] | 405 | * code in case of failure. The caller has to have the @ubi->device_mutex |
Artem Bityutskiy | 40e4d0c | 2007-12-17 17:08:55 +0200 | [diff] [blame] | 406 | * locked. |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 407 | */ |
Artem Bityutskiy | f40ac9c | 2008-07-13 21:47:47 +0300 | [diff] [blame] | 408 | int ubi_remove_volume(struct ubi_volume_desc *desc, int no_vtbl) |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 409 | { |
| 410 | struct ubi_volume *vol = desc->vol; |
| 411 | struct ubi_device *ubi = vol->ubi; |
| 412 | int i, err, vol_id = vol->vol_id, reserved_pebs = vol->reserved_pebs; |
| 413 | |
Artem Bityutskiy | e1cf7e6 | 2009-05-07 18:24:14 +0300 | [diff] [blame] | 414 | dbg_gen("remove device %d, volume %d", ubi->ubi_num, vol_id); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 415 | ubi_assert(desc->mode == UBI_EXCLUSIVE); |
| 416 | ubi_assert(vol == ubi->volumes[vol_id]); |
| 417 | |
| 418 | if (ubi->ro_mode) |
| 419 | return -EROFS; |
| 420 | |
Artem Bityutskiy | d05c77a | 2007-12-17 15:42:57 +0200 | [diff] [blame] | 421 | spin_lock(&ubi->volumes_lock); |
| 422 | if (vol->ref_count > 1) { |
| 423 | /* |
| 424 | * The volume is busy, probably someone is reading one of its |
| 425 | * sysfs files. |
| 426 | */ |
| 427 | err = -EBUSY; |
| 428 | goto out_unlock; |
| 429 | } |
| 430 | ubi->volumes[vol_id] = NULL; |
| 431 | spin_unlock(&ubi->volumes_lock); |
| 432 | |
Artem Bityutskiy | f40ac9c | 2008-07-13 21:47:47 +0300 | [diff] [blame] | 433 | if (!no_vtbl) { |
| 434 | err = ubi_change_vtbl_record(ubi, vol_id, NULL); |
| 435 | if (err) |
| 436 | goto out_err; |
| 437 | } |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 438 | |
| 439 | for (i = 0; i < vol->reserved_pebs; i++) { |
Artem Bityutskiy | 89b96b6 | 2007-12-16 20:00:38 +0200 | [diff] [blame] | 440 | err = ubi_eba_unmap_leb(ubi, vol, i); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 441 | if (err) |
Artem Bityutskiy | d05c77a | 2007-12-17 15:42:57 +0200 | [diff] [blame] | 442 | goto out_err; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 443 | } |
| 444 | |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 445 | cdev_del(&vol->cdev); |
| 446 | volume_sysfs_close(vol); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 447 | |
| 448 | spin_lock(&ubi->volumes_lock); |
| 449 | ubi->rsvd_pebs -= reserved_pebs; |
| 450 | ubi->avail_pebs += reserved_pebs; |
| 451 | i = ubi->beb_rsvd_level - ubi->beb_rsvd_pebs; |
| 452 | if (i > 0) { |
| 453 | i = ubi->avail_pebs >= i ? i : ubi->avail_pebs; |
| 454 | ubi->avail_pebs -= i; |
| 455 | ubi->rsvd_pebs += i; |
| 456 | ubi->beb_rsvd_pebs += i; |
| 457 | if (i > 0) |
| 458 | ubi_msg("reserve more %d PEBs", i); |
| 459 | } |
| 460 | ubi->vol_count -= 1; |
| 461 | spin_unlock(&ubi->volumes_lock); |
| 462 | |
Dmitry Pervushin | 0e0ee1c | 2009-04-29 19:29:38 +0400 | [diff] [blame] | 463 | ubi_volume_notify(ubi, vol, UBI_VOLUME_REMOVED); |
Artem Bityutskiy | d38dce5 | 2009-05-13 14:05:24 +0300 | [diff] [blame] | 464 | if (!no_vtbl && paranoid_check_volumes(ubi)) |
| 465 | dbg_err("check failed while removing volume %d", vol_id); |
| 466 | |
Artem Bityutskiy | c856635 | 2008-07-16 17:40:22 +0300 | [diff] [blame] | 467 | return err; |
Artem Bityutskiy | d05c77a | 2007-12-17 15:42:57 +0200 | [diff] [blame] | 468 | |
| 469 | out_err: |
| 470 | ubi_err("cannot remove volume %d, error %d", vol_id, err); |
| 471 | spin_lock(&ubi->volumes_lock); |
| 472 | ubi->volumes[vol_id] = vol; |
| 473 | out_unlock: |
| 474 | spin_unlock(&ubi->volumes_lock); |
Artem Bityutskiy | cae0a77 | 2007-12-17 12:46:48 +0200 | [diff] [blame] | 475 | return err; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | /** |
| 479 | * ubi_resize_volume - re-size volume. |
| 480 | * @desc: volume descriptor |
| 481 | * @reserved_pebs: new size in physical eraseblocks |
| 482 | * |
Artem Bityutskiy | 40e4d0c | 2007-12-17 17:08:55 +0200 | [diff] [blame] | 483 | * This function re-sizes the volume and returns zero in case of success, and a |
| 484 | * negative error code in case of failure. The caller has to have the |
Artem Bityutskiy | f089c0b | 2009-05-07 11:46:49 +0300 | [diff] [blame] | 485 | * @ubi->device_mutex locked. |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 486 | */ |
| 487 | int ubi_resize_volume(struct ubi_volume_desc *desc, int reserved_pebs) |
| 488 | { |
| 489 | int i, err, pebs, *new_mapping; |
| 490 | struct ubi_volume *vol = desc->vol; |
| 491 | struct ubi_device *ubi = vol->ubi; |
| 492 | struct ubi_vtbl_record vtbl_rec; |
| 493 | int vol_id = vol->vol_id; |
| 494 | |
| 495 | if (ubi->ro_mode) |
| 496 | return -EROFS; |
| 497 | |
Artem Bityutskiy | e1cf7e6 | 2009-05-07 18:24:14 +0300 | [diff] [blame] | 498 | dbg_gen("re-size device %d, volume %d to from %d to %d PEBs", |
| 499 | ubi->ubi_num, vol_id, vol->reserved_pebs, reserved_pebs); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 500 | |
| 501 | if (vol->vol_type == UBI_STATIC_VOLUME && |
| 502 | reserved_pebs < vol->used_ebs) { |
| 503 | dbg_err("too small size %d, %d LEBs contain data", |
| 504 | reserved_pebs, vol->used_ebs); |
| 505 | return -EINVAL; |
| 506 | } |
| 507 | |
| 508 | /* If the size is the same, we have nothing to do */ |
| 509 | if (reserved_pebs == vol->reserved_pebs) |
| 510 | return 0; |
| 511 | |
| 512 | new_mapping = kmalloc(reserved_pebs * sizeof(int), GFP_KERNEL); |
| 513 | if (!new_mapping) |
| 514 | return -ENOMEM; |
| 515 | |
| 516 | for (i = 0; i < reserved_pebs; i++) |
| 517 | new_mapping[i] = UBI_LEB_UNMAPPED; |
| 518 | |
Artem Bityutskiy | d05c77a | 2007-12-17 15:42:57 +0200 | [diff] [blame] | 519 | spin_lock(&ubi->volumes_lock); |
| 520 | if (vol->ref_count > 1) { |
| 521 | spin_unlock(&ubi->volumes_lock); |
| 522 | err = -EBUSY; |
| 523 | goto out_free; |
| 524 | } |
| 525 | spin_unlock(&ubi->volumes_lock); |
| 526 | |
Artem Bityutskiy | d05c77a | 2007-12-17 15:42:57 +0200 | [diff] [blame] | 527 | /* Reserve physical eraseblocks */ |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 528 | pebs = reserved_pebs - vol->reserved_pebs; |
| 529 | if (pebs > 0) { |
| 530 | spin_lock(&ubi->volumes_lock); |
| 531 | if (pebs > ubi->avail_pebs) { |
| 532 | dbg_err("not enough PEBs: requested %d, available %d", |
| 533 | pebs, ubi->avail_pebs); |
Artem Bityutskiy | 5fc01ab | 2010-09-03 23:08:15 +0300 | [diff] [blame] | 534 | if (ubi->corr_peb_count) |
| 535 | dbg_err("%d PEBs are corrupted and not used", |
| 536 | ubi->corr_peb_count); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 537 | spin_unlock(&ubi->volumes_lock); |
| 538 | err = -ENOSPC; |
| 539 | goto out_free; |
| 540 | } |
| 541 | ubi->avail_pebs -= pebs; |
| 542 | ubi->rsvd_pebs += pebs; |
| 543 | for (i = 0; i < vol->reserved_pebs; i++) |
| 544 | new_mapping[i] = vol->eba_tbl[i]; |
| 545 | kfree(vol->eba_tbl); |
| 546 | vol->eba_tbl = new_mapping; |
| 547 | spin_unlock(&ubi->volumes_lock); |
| 548 | } |
| 549 | |
| 550 | /* Change volume table record */ |
| 551 | memcpy(&vtbl_rec, &ubi->vtbl[vol_id], sizeof(struct ubi_vtbl_record)); |
Christoph Hellwig | 3261ebd | 2007-05-21 17:41:46 +0300 | [diff] [blame] | 552 | vtbl_rec.reserved_pebs = cpu_to_be32(reserved_pebs); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 553 | err = ubi_change_vtbl_record(ubi, vol_id, &vtbl_rec); |
| 554 | if (err) |
| 555 | goto out_acc; |
| 556 | |
| 557 | if (pebs < 0) { |
| 558 | for (i = 0; i < -pebs; i++) { |
Artem Bityutskiy | 89b96b6 | 2007-12-16 20:00:38 +0200 | [diff] [blame] | 559 | err = ubi_eba_unmap_leb(ubi, vol, reserved_pebs + i); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 560 | if (err) |
| 561 | goto out_acc; |
| 562 | } |
| 563 | spin_lock(&ubi->volumes_lock); |
| 564 | ubi->rsvd_pebs += pebs; |
| 565 | ubi->avail_pebs -= pebs; |
| 566 | pebs = ubi->beb_rsvd_level - ubi->beb_rsvd_pebs; |
| 567 | if (pebs > 0) { |
| 568 | pebs = ubi->avail_pebs >= pebs ? pebs : ubi->avail_pebs; |
| 569 | ubi->avail_pebs -= pebs; |
| 570 | ubi->rsvd_pebs += pebs; |
| 571 | ubi->beb_rsvd_pebs += pebs; |
| 572 | if (pebs > 0) |
| 573 | ubi_msg("reserve more %d PEBs", pebs); |
| 574 | } |
| 575 | for (i = 0; i < reserved_pebs; i++) |
| 576 | new_mapping[i] = vol->eba_tbl[i]; |
| 577 | kfree(vol->eba_tbl); |
| 578 | vol->eba_tbl = new_mapping; |
| 579 | spin_unlock(&ubi->volumes_lock); |
| 580 | } |
| 581 | |
| 582 | vol->reserved_pebs = reserved_pebs; |
| 583 | if (vol->vol_type == UBI_DYNAMIC_VOLUME) { |
| 584 | vol->used_ebs = reserved_pebs; |
| 585 | vol->last_eb_bytes = vol->usable_leb_size; |
Vinit Agnihotri | d08c3b7 | 2007-07-10 13:04:59 +0300 | [diff] [blame] | 586 | vol->used_bytes = |
| 587 | (long long)vol->used_ebs * vol->usable_leb_size; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 588 | } |
| 589 | |
Dmitry Pervushin | 0e0ee1c | 2009-04-29 19:29:38 +0400 | [diff] [blame] | 590 | ubi_volume_notify(ubi, vol, UBI_VOLUME_RESIZED); |
Artem Bityutskiy | d38dce5 | 2009-05-13 14:05:24 +0300 | [diff] [blame] | 591 | if (paranoid_check_volumes(ubi)) |
| 592 | dbg_err("check failed while re-sizing volume %d", vol_id); |
Artem Bityutskiy | c856635 | 2008-07-16 17:40:22 +0300 | [diff] [blame] | 593 | return err; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 594 | |
| 595 | out_acc: |
| 596 | if (pebs > 0) { |
| 597 | spin_lock(&ubi->volumes_lock); |
| 598 | ubi->rsvd_pebs -= pebs; |
| 599 | ubi->avail_pebs += pebs; |
| 600 | spin_unlock(&ubi->volumes_lock); |
| 601 | } |
| 602 | out_free: |
| 603 | kfree(new_mapping); |
| 604 | return err; |
| 605 | } |
| 606 | |
| 607 | /** |
Artem Bityutskiy | f40ac9c | 2008-07-13 21:47:47 +0300 | [diff] [blame] | 608 | * ubi_rename_volumes - re-name UBI volumes. |
| 609 | * @ubi: UBI device description object |
Artem Bityutskiy | ebaaf1a | 2008-07-18 13:34:32 +0300 | [diff] [blame] | 610 | * @rename_list: list of &struct ubi_rename_entry objects |
Artem Bityutskiy | f40ac9c | 2008-07-13 21:47:47 +0300 | [diff] [blame] | 611 | * |
| 612 | * This function re-names or removes volumes specified in the re-name list. |
| 613 | * Returns zero in case of success and a negative error code in case of |
| 614 | * failure. |
| 615 | */ |
| 616 | int ubi_rename_volumes(struct ubi_device *ubi, struct list_head *rename_list) |
| 617 | { |
| 618 | int err; |
| 619 | struct ubi_rename_entry *re; |
| 620 | |
| 621 | err = ubi_vtbl_rename_volumes(ubi, rename_list); |
| 622 | if (err) |
| 623 | return err; |
| 624 | |
| 625 | list_for_each_entry(re, rename_list, list) { |
| 626 | if (re->remove) { |
| 627 | err = ubi_remove_volume(re->desc, 1); |
| 628 | if (err) |
| 629 | break; |
| 630 | } else { |
| 631 | struct ubi_volume *vol = re->desc->vol; |
| 632 | |
| 633 | spin_lock(&ubi->volumes_lock); |
| 634 | vol->name_len = re->new_name_len; |
| 635 | memcpy(vol->name, re->new_name, re->new_name_len + 1); |
| 636 | spin_unlock(&ubi->volumes_lock); |
Dmitry Pervushin | 0e0ee1c | 2009-04-29 19:29:38 +0400 | [diff] [blame] | 637 | ubi_volume_notify(ubi, vol, UBI_VOLUME_RENAMED); |
Artem Bityutskiy | f40ac9c | 2008-07-13 21:47:47 +0300 | [diff] [blame] | 638 | } |
| 639 | } |
| 640 | |
Artem Bityutskiy | d38dce5 | 2009-05-13 14:05:24 +0300 | [diff] [blame] | 641 | if (!err && paranoid_check_volumes(ubi)) |
| 642 | ; |
Artem Bityutskiy | f40ac9c | 2008-07-13 21:47:47 +0300 | [diff] [blame] | 643 | return err; |
| 644 | } |
| 645 | |
| 646 | /** |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 647 | * ubi_add_volume - add volume. |
| 648 | * @ubi: UBI device description object |
Artem Bityutskiy | 89b96b6 | 2007-12-16 20:00:38 +0200 | [diff] [blame] | 649 | * @vol: volume description object |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 650 | * |
Artem Bityutskiy | d05c77a | 2007-12-17 15:42:57 +0200 | [diff] [blame] | 651 | * This function adds an existing volume and initializes all its data |
| 652 | * structures. Returns zero in case of success and a negative error code in |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 653 | * case of failure. |
| 654 | */ |
Artem Bityutskiy | 89b96b6 | 2007-12-16 20:00:38 +0200 | [diff] [blame] | 655 | int ubi_add_volume(struct ubi_device *ubi, struct ubi_volume *vol) |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 656 | { |
Artem Bityutskiy | 89b96b6 | 2007-12-16 20:00:38 +0200 | [diff] [blame] | 657 | int err, vol_id = vol->vol_id; |
Artem Bityutskiy | 49dfc29 | 2007-12-15 18:13:56 +0200 | [diff] [blame] | 658 | dev_t dev; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 659 | |
Artem Bityutskiy | c856635 | 2008-07-16 17:40:22 +0300 | [diff] [blame] | 660 | dbg_gen("add volume %d", vol_id); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 661 | |
| 662 | /* Register character device for the volume */ |
| 663 | cdev_init(&vol->cdev, &ubi_vol_cdev_operations); |
| 664 | vol->cdev.owner = THIS_MODULE; |
Artem Bityutskiy | 49dfc29 | 2007-12-15 18:13:56 +0200 | [diff] [blame] | 665 | dev = MKDEV(MAJOR(ubi->cdev.dev), vol->vol_id + 1); |
| 666 | err = cdev_add(&vol->cdev, dev, 1); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 667 | if (err) { |
Artem Bityutskiy | 01f7b30 | 2007-12-15 19:56:51 +0200 | [diff] [blame] | 668 | ubi_err("cannot add character device for volume %d, error %d", |
| 669 | vol_id, err); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 670 | return err; |
| 671 | } |
| 672 | |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 673 | vol->dev.release = vol_release; |
| 674 | vol->dev.parent = &ubi->dev; |
Artem Bityutskiy | 49dfc29 | 2007-12-15 18:13:56 +0200 | [diff] [blame] | 675 | vol->dev.devt = dev; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 676 | vol->dev.class = ubi_class; |
Kay Sievers | 475b44c | 2009-01-06 10:44:38 -0800 | [diff] [blame] | 677 | dev_set_name(&vol->dev, "%s_%d", ubi->ubi_name, vol->vol_id); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 678 | err = device_register(&vol->dev); |
| 679 | if (err) |
Dmitry Pervushin | 518ceef | 2009-04-29 19:29:44 +0400 | [diff] [blame] | 680 | goto out_cdev; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 681 | |
| 682 | err = volume_sysfs_init(ubi, vol); |
| 683 | if (err) { |
| 684 | cdev_del(&vol->cdev); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 685 | volume_sysfs_close(vol); |
| 686 | return err; |
| 687 | } |
| 688 | |
Artem Bityutskiy | d38dce5 | 2009-05-13 14:05:24 +0300 | [diff] [blame] | 689 | if (paranoid_check_volumes(ubi)) |
| 690 | dbg_err("check failed while adding volume %d", vol_id); |
Artem Bityutskiy | c856635 | 2008-07-16 17:40:22 +0300 | [diff] [blame] | 691 | return err; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 692 | |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 693 | out_cdev: |
| 694 | cdev_del(&vol->cdev); |
| 695 | return err; |
| 696 | } |
| 697 | |
| 698 | /** |
| 699 | * ubi_free_volume - free volume. |
| 700 | * @ubi: UBI device description object |
Artem Bityutskiy | 89b96b6 | 2007-12-16 20:00:38 +0200 | [diff] [blame] | 701 | * @vol: volume description object |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 702 | * |
Artem Bityutskiy | 89b96b6 | 2007-12-16 20:00:38 +0200 | [diff] [blame] | 703 | * This function frees all resources for volume @vol but does not remove it. |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 704 | * Used only when the UBI device is detached. |
| 705 | */ |
Artem Bityutskiy | 89b96b6 | 2007-12-16 20:00:38 +0200 | [diff] [blame] | 706 | void ubi_free_volume(struct ubi_device *ubi, struct ubi_volume *vol) |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 707 | { |
Artem Bityutskiy | c856635 | 2008-07-16 17:40:22 +0300 | [diff] [blame] | 708 | dbg_gen("free volume %d", vol->vol_id); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 709 | |
Artem Bityutskiy | 89b96b6 | 2007-12-16 20:00:38 +0200 | [diff] [blame] | 710 | ubi->volumes[vol->vol_id] = NULL; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 711 | cdev_del(&vol->cdev); |
| 712 | volume_sysfs_close(vol); |
| 713 | } |
| 714 | |
Artem Bityutskiy | 92d124f | 2011-03-14 18:17:40 +0200 | [diff] [blame] | 715 | #ifdef CONFIG_MTD_UBI_DEBUG |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 716 | |
| 717 | /** |
| 718 | * paranoid_check_volume - check volume information. |
| 719 | * @ubi: UBI device description object |
| 720 | * @vol_id: volume ID |
Artem Bityutskiy | c856635 | 2008-07-16 17:40:22 +0300 | [diff] [blame] | 721 | * |
| 722 | * Returns zero if volume is all right and a a negative error code if not. |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 723 | */ |
Artem Bityutskiy | c856635 | 2008-07-16 17:40:22 +0300 | [diff] [blame] | 724 | static int paranoid_check_volume(struct ubi_device *ubi, int vol_id) |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 725 | { |
| 726 | int idx = vol_id2idx(ubi, vol_id); |
| 727 | int reserved_pebs, alignment, data_pad, vol_type, name_len, upd_marker; |
Artem Bityutskiy | b89044b | 2007-06-18 16:29:30 +0300 | [diff] [blame] | 728 | const struct ubi_volume *vol; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 729 | long long n; |
| 730 | const char *name; |
| 731 | |
Artem Bityutskiy | b89044b | 2007-06-18 16:29:30 +0300 | [diff] [blame] | 732 | spin_lock(&ubi->volumes_lock); |
Christoph Hellwig | 3261ebd | 2007-05-21 17:41:46 +0300 | [diff] [blame] | 733 | reserved_pebs = be32_to_cpu(ubi->vtbl[vol_id].reserved_pebs); |
Artem Bityutskiy | b89044b | 2007-06-18 16:29:30 +0300 | [diff] [blame] | 734 | vol = ubi->volumes[idx]; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 735 | |
| 736 | if (!vol) { |
| 737 | if (reserved_pebs) { |
| 738 | ubi_err("no volume info, but volume exists"); |
| 739 | goto fail; |
| 740 | } |
Artem Bityutskiy | b89044b | 2007-06-18 16:29:30 +0300 | [diff] [blame] | 741 | spin_unlock(&ubi->volumes_lock); |
Artem Bityutskiy | c856635 | 2008-07-16 17:40:22 +0300 | [diff] [blame] | 742 | return 0; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 743 | } |
| 744 | |
| 745 | if (vol->reserved_pebs < 0 || vol->alignment < 0 || vol->data_pad < 0 || |
| 746 | vol->name_len < 0) { |
| 747 | ubi_err("negative values"); |
| 748 | goto fail; |
| 749 | } |
| 750 | if (vol->alignment > ubi->leb_size || vol->alignment == 0) { |
| 751 | ubi_err("bad alignment"); |
| 752 | goto fail; |
| 753 | } |
| 754 | |
Kyungmin Park | cadb40c | 2008-05-22 10:32:18 +0900 | [diff] [blame] | 755 | n = vol->alignment & (ubi->min_io_size - 1); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 756 | if (vol->alignment != 1 && n) { |
| 757 | ubi_err("alignment is not multiple of min I/O unit"); |
| 758 | goto fail; |
| 759 | } |
| 760 | |
| 761 | n = ubi->leb_size % vol->alignment; |
| 762 | if (vol->data_pad != n) { |
| 763 | ubi_err("bad data_pad, has to be %lld", n); |
| 764 | goto fail; |
| 765 | } |
| 766 | |
| 767 | if (vol->vol_type != UBI_DYNAMIC_VOLUME && |
| 768 | vol->vol_type != UBI_STATIC_VOLUME) { |
| 769 | ubi_err("bad vol_type"); |
| 770 | goto fail; |
| 771 | } |
| 772 | |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 773 | if (vol->upd_marker && vol->corrupted) { |
| 774 | dbg_err("update marker and corrupted simultaneously"); |
| 775 | goto fail; |
| 776 | } |
| 777 | |
| 778 | if (vol->reserved_pebs > ubi->good_peb_count) { |
| 779 | ubi_err("too large reserved_pebs"); |
| 780 | goto fail; |
| 781 | } |
| 782 | |
| 783 | n = ubi->leb_size - vol->data_pad; |
| 784 | if (vol->usable_leb_size != ubi->leb_size - vol->data_pad) { |
| 785 | ubi_err("bad usable_leb_size, has to be %lld", n); |
| 786 | goto fail; |
| 787 | } |
| 788 | |
| 789 | if (vol->name_len > UBI_VOL_NAME_MAX) { |
| 790 | ubi_err("too long volume name, max is %d", UBI_VOL_NAME_MAX); |
| 791 | goto fail; |
| 792 | } |
| 793 | |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 794 | n = strnlen(vol->name, vol->name_len + 1); |
| 795 | if (n != vol->name_len) { |
| 796 | ubi_err("bad name_len %lld", n); |
| 797 | goto fail; |
| 798 | } |
| 799 | |
Vinit Agnihotri | d08c3b7 | 2007-07-10 13:04:59 +0300 | [diff] [blame] | 800 | n = (long long)vol->used_ebs * vol->usable_leb_size; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 801 | if (vol->vol_type == UBI_DYNAMIC_VOLUME) { |
Artem Bityutskiy | 896c0c0 | 2008-01-16 14:24:14 +0200 | [diff] [blame] | 802 | if (vol->corrupted) { |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 803 | ubi_err("corrupted dynamic volume"); |
| 804 | goto fail; |
| 805 | } |
| 806 | if (vol->used_ebs != vol->reserved_pebs) { |
| 807 | ubi_err("bad used_ebs"); |
| 808 | goto fail; |
| 809 | } |
| 810 | if (vol->last_eb_bytes != vol->usable_leb_size) { |
| 811 | ubi_err("bad last_eb_bytes"); |
| 812 | goto fail; |
| 813 | } |
| 814 | if (vol->used_bytes != n) { |
| 815 | ubi_err("bad used_bytes"); |
| 816 | goto fail; |
| 817 | } |
| 818 | } else { |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 819 | if (vol->used_ebs < 0 || vol->used_ebs > vol->reserved_pebs) { |
| 820 | ubi_err("bad used_ebs"); |
| 821 | goto fail; |
| 822 | } |
| 823 | if (vol->last_eb_bytes < 0 || |
| 824 | vol->last_eb_bytes > vol->usable_leb_size) { |
| 825 | ubi_err("bad last_eb_bytes"); |
| 826 | goto fail; |
| 827 | } |
| 828 | if (vol->used_bytes < 0 || vol->used_bytes > n || |
| 829 | vol->used_bytes < n - vol->usable_leb_size) { |
| 830 | ubi_err("bad used_bytes"); |
| 831 | goto fail; |
| 832 | } |
| 833 | } |
| 834 | |
Christoph Hellwig | 3261ebd | 2007-05-21 17:41:46 +0300 | [diff] [blame] | 835 | alignment = be32_to_cpu(ubi->vtbl[vol_id].alignment); |
| 836 | data_pad = be32_to_cpu(ubi->vtbl[vol_id].data_pad); |
| 837 | name_len = be16_to_cpu(ubi->vtbl[vol_id].name_len); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 838 | upd_marker = ubi->vtbl[vol_id].upd_marker; |
| 839 | name = &ubi->vtbl[vol_id].name[0]; |
| 840 | if (ubi->vtbl[vol_id].vol_type == UBI_VID_DYNAMIC) |
| 841 | vol_type = UBI_DYNAMIC_VOLUME; |
| 842 | else |
| 843 | vol_type = UBI_STATIC_VOLUME; |
| 844 | |
| 845 | if (alignment != vol->alignment || data_pad != vol->data_pad || |
| 846 | upd_marker != vol->upd_marker || vol_type != vol->vol_type || |
Artem Bityutskiy | 9c9ec14 | 2008-07-18 13:19:52 +0300 | [diff] [blame] | 847 | name_len != vol->name_len || strncmp(name, vol->name, name_len)) { |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 848 | ubi_err("volume info is different"); |
| 849 | goto fail; |
| 850 | } |
| 851 | |
Artem Bityutskiy | b89044b | 2007-06-18 16:29:30 +0300 | [diff] [blame] | 852 | spin_unlock(&ubi->volumes_lock); |
Artem Bityutskiy | c856635 | 2008-07-16 17:40:22 +0300 | [diff] [blame] | 853 | return 0; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 854 | |
| 855 | fail: |
Artem Bityutskiy | 94784d9 | 2007-06-18 12:06:30 +0300 | [diff] [blame] | 856 | ubi_err("paranoid check failed for volume %d", vol_id); |
Artem Bityutskiy | f40ac9c | 2008-07-13 21:47:47 +0300 | [diff] [blame] | 857 | if (vol) |
Artem Bityutskiy | c856635 | 2008-07-16 17:40:22 +0300 | [diff] [blame] | 858 | ubi_dbg_dump_vol_info(vol); |
Artem Bityutskiy | f40ac9c | 2008-07-13 21:47:47 +0300 | [diff] [blame] | 859 | ubi_dbg_dump_vtbl_record(&ubi->vtbl[vol_id], vol_id); |
Artem Bityutskiy | cfcf0ec | 2009-05-12 20:29:15 +0300 | [diff] [blame] | 860 | dump_stack(); |
Artem Bityutskiy | b89044b | 2007-06-18 16:29:30 +0300 | [diff] [blame] | 861 | spin_unlock(&ubi->volumes_lock); |
Artem Bityutskiy | c856635 | 2008-07-16 17:40:22 +0300 | [diff] [blame] | 862 | return -EINVAL; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 863 | } |
| 864 | |
| 865 | /** |
| 866 | * paranoid_check_volumes - check information about all volumes. |
| 867 | * @ubi: UBI device description object |
Artem Bityutskiy | c856635 | 2008-07-16 17:40:22 +0300 | [diff] [blame] | 868 | * |
| 869 | * Returns zero if volumes are all right and a a negative error code if not. |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 870 | */ |
Artem Bityutskiy | c856635 | 2008-07-16 17:40:22 +0300 | [diff] [blame] | 871 | static int paranoid_check_volumes(struct ubi_device *ubi) |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 872 | { |
Artem Bityutskiy | c856635 | 2008-07-16 17:40:22 +0300 | [diff] [blame] | 873 | int i, err = 0; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 874 | |
Artem Bityutskiy | 2a734bb | 2011-05-18 14:53:05 +0300 | [diff] [blame] | 875 | if (!ubi->dbg->chk_gen) |
Artem Bityutskiy | 92d124f | 2011-03-14 18:17:40 +0200 | [diff] [blame] | 876 | return 0; |
| 877 | |
Artem Bityutskiy | c856635 | 2008-07-16 17:40:22 +0300 | [diff] [blame] | 878 | for (i = 0; i < ubi->vtbl_slots; i++) { |
| 879 | err = paranoid_check_volume(ubi, i); |
| 880 | if (err) |
| 881 | break; |
| 882 | } |
| 883 | |
| 884 | return err; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 885 | } |
| 886 | #endif |