vmt.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  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. * This file contains implementation of volume creation, deletion, updating and
  22. * resizing.
  23. */
  24. #include <linux/err.h>
  25. #include <linux/math64.h>
  26. #include <linux/slab.h>
  27. #include <linux/export.h>
  28. #include "ubi.h"
  29. static int self_check_volumes(struct ubi_device *ubi);
  30. static ssize_t vol_attribute_show(struct device *dev,
  31. struct device_attribute *attr, char *buf);
  32. /* Device attributes corresponding to files in '/<sysfs>/class/ubi/ubiX_Y' */
  33. static struct device_attribute attr_vol_reserved_ebs =
  34. __ATTR(reserved_ebs, S_IRUGO, vol_attribute_show, NULL);
  35. static struct device_attribute attr_vol_type =
  36. __ATTR(type, S_IRUGO, vol_attribute_show, NULL);
  37. static struct device_attribute attr_vol_name =
  38. __ATTR(name, S_IRUGO, vol_attribute_show, NULL);
  39. static struct device_attribute attr_vol_corrupted =
  40. __ATTR(corrupted, S_IRUGO, vol_attribute_show, NULL);
  41. static struct device_attribute attr_vol_alignment =
  42. __ATTR(alignment, S_IRUGO, vol_attribute_show, NULL);
  43. static struct device_attribute attr_vol_usable_eb_size =
  44. __ATTR(usable_eb_size, S_IRUGO, vol_attribute_show, NULL);
  45. static struct device_attribute attr_vol_data_bytes =
  46. __ATTR(data_bytes, S_IRUGO, vol_attribute_show, NULL);
  47. static struct device_attribute attr_vol_upd_marker =
  48. __ATTR(upd_marker, S_IRUGO, vol_attribute_show, NULL);
  49. /*
  50. * "Show" method for files in '/<sysfs>/class/ubi/ubiX_Y/'.
  51. *
  52. * Consider a situation:
  53. * A. process 1 opens a sysfs file related to volume Y, say
  54. * /<sysfs>/class/ubi/ubiX_Y/reserved_ebs;
  55. * B. process 2 removes volume Y;
  56. * C. process 1 starts reading the /<sysfs>/class/ubi/ubiX_Y/reserved_ebs file;
  57. *
  58. * In this situation, this function will return %-ENODEV because it will find
  59. * out that the volume was removed from the @ubi->volumes array.
  60. */
  61. static ssize_t vol_attribute_show(struct device *dev,
  62. struct device_attribute *attr, char *buf)
  63. {
  64. int ret;
  65. struct ubi_volume *vol = container_of(dev, struct ubi_volume, dev);
  66. struct ubi_device *ubi;
  67. ubi = ubi_get_device(vol->ubi->ubi_num);
  68. if (!ubi)
  69. return -ENODEV;
  70. spin_lock(&ubi->volumes_lock);
  71. if (!ubi->volumes[vol->vol_id]) {
  72. spin_unlock(&ubi->volumes_lock);
  73. ubi_put_device(ubi);
  74. return -ENODEV;
  75. }
  76. /* Take a reference to prevent volume removal */
  77. vol->ref_count += 1;
  78. spin_unlock(&ubi->volumes_lock);
  79. if (attr == &attr_vol_reserved_ebs)
  80. ret = sprintf(buf, "%d\n", vol->reserved_pebs);
  81. else if (attr == &attr_vol_type) {
  82. const char *tp;
  83. if (vol->vol_type == UBI_DYNAMIC_VOLUME)
  84. tp = "dynamic";
  85. else
  86. tp = "static";
  87. ret = sprintf(buf, "%s\n", tp);
  88. } else if (attr == &attr_vol_name)
  89. ret = sprintf(buf, "%s\n", vol->name);
  90. else if (attr == &attr_vol_corrupted)
  91. ret = sprintf(buf, "%d\n", vol->corrupted);
  92. else if (attr == &attr_vol_alignment)
  93. ret = sprintf(buf, "%d\n", vol->alignment);
  94. else if (attr == &attr_vol_usable_eb_size)
  95. ret = sprintf(buf, "%d\n", vol->usable_leb_size);
  96. else if (attr == &attr_vol_data_bytes)
  97. ret = sprintf(buf, "%lld\n", vol->used_bytes);
  98. else if (attr == &attr_vol_upd_marker)
  99. ret = sprintf(buf, "%d\n", vol->upd_marker);
  100. else
  101. /* This must be a bug */
  102. ret = -EINVAL;
  103. /* We've done the operation, drop volume and UBI device references */
  104. spin_lock(&ubi->volumes_lock);
  105. vol->ref_count -= 1;
  106. ubi_assert(vol->ref_count >= 0);
  107. spin_unlock(&ubi->volumes_lock);
  108. ubi_put_device(ubi);
  109. return ret;
  110. }
  111. static struct attribute *volume_dev_attrs[] = {
  112. &attr_vol_reserved_ebs.attr,
  113. &attr_vol_type.attr,
  114. &attr_vol_name.attr,
  115. &attr_vol_corrupted.attr,
  116. &attr_vol_alignment.attr,
  117. &attr_vol_usable_eb_size.attr,
  118. &attr_vol_data_bytes.attr,
  119. &attr_vol_upd_marker.attr,
  120. NULL
  121. };
  122. ATTRIBUTE_GROUPS(volume_dev);
  123. /* Release method for volume devices */
  124. static void vol_release(struct device *dev)
  125. {
  126. struct ubi_volume *vol = container_of(dev, struct ubi_volume, dev);
  127. ubi_eba_replace_table(vol, NULL);
  128. ubi_fastmap_destroy_checkmap(vol);
  129. kfree(vol);
  130. }
  131. /**
  132. * ubi_create_volume - create volume.
  133. * @ubi: UBI device description object
  134. * @req: volume creation request
  135. *
  136. * This function creates volume described by @req. If @req->vol_id id
  137. * %UBI_VOL_NUM_AUTO, this function automatically assign ID to the new volume
  138. * and saves it in @req->vol_id. Returns zero in case of success and a negative
  139. * error code in case of failure. Note, the caller has to have the
  140. * @ubi->device_mutex locked.
  141. */
  142. int ubi_create_volume(struct ubi_device *ubi, struct ubi_mkvol_req *req)
  143. {
  144. int i, err, vol_id = req->vol_id;
  145. struct ubi_volume *vol;
  146. struct ubi_vtbl_record vtbl_rec;
  147. struct ubi_eba_table *eba_tbl = NULL;
  148. if (ubi->ro_mode)
  149. return -EROFS;
  150. vol = kzalloc(sizeof(struct ubi_volume), GFP_KERNEL);
  151. if (!vol)
  152. return -ENOMEM;
  153. device_initialize(&vol->dev);
  154. vol->dev.release = vol_release;
  155. vol->dev.parent = &ubi->dev;
  156. vol->dev.class = &ubi_class;
  157. vol->dev.groups = volume_dev_groups;
  158. if (req->flags & UBI_VOL_SKIP_CRC_CHECK_FLG)
  159. vol->skip_check = 1;
  160. spin_lock(&ubi->volumes_lock);
  161. if (vol_id == UBI_VOL_NUM_AUTO) {
  162. /* Find unused volume ID */
  163. dbg_gen("search for vacant volume ID");
  164. for (i = 0; i < ubi->vtbl_slots; i++)
  165. if (!ubi->volumes[i]) {
  166. vol_id = i;
  167. break;
  168. }
  169. if (vol_id == UBI_VOL_NUM_AUTO) {
  170. ubi_err(ubi, "out of volume IDs");
  171. err = -ENFILE;
  172. goto out_unlock;
  173. }
  174. req->vol_id = vol_id;
  175. }
  176. dbg_gen("create device %d, volume %d, %llu bytes, type %d, name %s",
  177. ubi->ubi_num, vol_id, (unsigned long long)req->bytes,
  178. (int)req->vol_type, req->name);
  179. /* Ensure that this volume does not exist */
  180. err = -EEXIST;
  181. if (ubi->volumes[vol_id]) {
  182. ubi_err(ubi, "volume %d already exists", vol_id);
  183. goto out_unlock;
  184. }
  185. /* Ensure that the name is unique */
  186. for (i = 0; i < ubi->vtbl_slots; i++)
  187. if (ubi->volumes[i] &&
  188. ubi->volumes[i]->name_len == req->name_len &&
  189. !strcmp(ubi->volumes[i]->name, req->name)) {
  190. ubi_err(ubi, "volume \"%s\" exists (ID %d)",
  191. req->name, i);
  192. goto out_unlock;
  193. }
  194. /* Calculate how many eraseblocks are requested */
  195. vol->usable_leb_size = ubi->leb_size - ubi->leb_size % req->alignment;
  196. vol->reserved_pebs = div_u64(req->bytes + vol->usable_leb_size - 1,
  197. vol->usable_leb_size);
  198. /* Reserve physical eraseblocks */
  199. if (vol->reserved_pebs > ubi->avail_pebs) {
  200. ubi_err(ubi, "not enough PEBs, only %d available",
  201. ubi->avail_pebs);
  202. if (ubi->corr_peb_count)
  203. ubi_err(ubi, "%d PEBs are corrupted and not used",
  204. ubi->corr_peb_count);
  205. err = -ENOSPC;
  206. goto out_unlock;
  207. }
  208. ubi->avail_pebs -= vol->reserved_pebs;
  209. ubi->rsvd_pebs += vol->reserved_pebs;
  210. spin_unlock(&ubi->volumes_lock);
  211. vol->vol_id = vol_id;
  212. vol->alignment = req->alignment;
  213. vol->data_pad = ubi->leb_size % vol->alignment;
  214. vol->vol_type = req->vol_type;
  215. vol->name_len = req->name_len;
  216. memcpy(vol->name, req->name, vol->name_len);
  217. vol->ubi = ubi;
  218. /*
  219. * Finish all pending erases because there may be some LEBs belonging
  220. * to the same volume ID.
  221. */
  222. err = ubi_wl_flush(ubi, vol_id, UBI_ALL);
  223. if (err)
  224. goto out_acc;
  225. eba_tbl = ubi_eba_create_table(vol, vol->reserved_pebs);
  226. if (IS_ERR(eba_tbl)) {
  227. err = PTR_ERR(eba_tbl);
  228. goto out_acc;
  229. }
  230. ubi_eba_replace_table(vol, eba_tbl);
  231. if (vol->vol_type == UBI_DYNAMIC_VOLUME) {
  232. vol->used_ebs = vol->reserved_pebs;
  233. vol->last_eb_bytes = vol->usable_leb_size;
  234. vol->used_bytes =
  235. (long long)vol->used_ebs * vol->usable_leb_size;
  236. } else {
  237. vol->used_ebs = div_u64_rem(vol->used_bytes,
  238. vol->usable_leb_size,
  239. &vol->last_eb_bytes);
  240. if (vol->last_eb_bytes != 0)
  241. vol->used_ebs += 1;
  242. else
  243. vol->last_eb_bytes = vol->usable_leb_size;
  244. }
  245. /* Make volume "available" before it becomes accessible via sysfs */
  246. spin_lock(&ubi->volumes_lock);
  247. ubi->volumes[vol_id] = vol;
  248. ubi->vol_count += 1;
  249. spin_unlock(&ubi->volumes_lock);
  250. /* Register character device for the volume */
  251. cdev_init(&vol->cdev, &ubi_vol_cdev_operations);
  252. vol->cdev.owner = THIS_MODULE;
  253. vol->dev.devt = MKDEV(MAJOR(ubi->cdev.dev), vol_id + 1);
  254. dev_set_name(&vol->dev, "%s_%d", ubi->ubi_name, vol->vol_id);
  255. err = cdev_device_add(&vol->cdev, &vol->dev);
  256. if (err) {
  257. ubi_err(ubi, "cannot add device");
  258. goto out_mapping;
  259. }
  260. /* Fill volume table record */
  261. memset(&vtbl_rec, 0, sizeof(struct ubi_vtbl_record));
  262. vtbl_rec.reserved_pebs = cpu_to_be32(vol->reserved_pebs);
  263. vtbl_rec.alignment = cpu_to_be32(vol->alignment);
  264. vtbl_rec.data_pad = cpu_to_be32(vol->data_pad);
  265. vtbl_rec.name_len = cpu_to_be16(vol->name_len);
  266. if (vol->vol_type == UBI_DYNAMIC_VOLUME)
  267. vtbl_rec.vol_type = UBI_VID_DYNAMIC;
  268. else
  269. vtbl_rec.vol_type = UBI_VID_STATIC;
  270. if (vol->skip_check)
  271. vtbl_rec.flags |= UBI_VTBL_SKIP_CRC_CHECK_FLG;
  272. memcpy(vtbl_rec.name, vol->name, vol->name_len);
  273. err = ubi_change_vtbl_record(ubi, vol_id, &vtbl_rec);
  274. if (err)
  275. goto out_sysfs;
  276. ubi_volume_notify(ubi, vol, UBI_VOLUME_ADDED);
  277. self_check_volumes(ubi);
  278. return err;
  279. out_sysfs:
  280. /*
  281. * We have registered our device, we should not free the volume
  282. * description object in this function in case of an error - it is
  283. * freed by the release function.
  284. */
  285. cdev_device_del(&vol->cdev, &vol->dev);
  286. out_mapping:
  287. spin_lock(&ubi->volumes_lock);
  288. ubi->volumes[vol_id] = NULL;
  289. ubi->vol_count -= 1;
  290. spin_unlock(&ubi->volumes_lock);
  291. ubi_eba_destroy_table(eba_tbl);
  292. out_acc:
  293. spin_lock(&ubi->volumes_lock);
  294. ubi->rsvd_pebs -= vol->reserved_pebs;
  295. ubi->avail_pebs += vol->reserved_pebs;
  296. out_unlock:
  297. spin_unlock(&ubi->volumes_lock);
  298. put_device(&vol->dev);
  299. ubi_err(ubi, "cannot create volume %d, error %d", vol_id, err);
  300. return err;
  301. }
  302. /**
  303. * ubi_remove_volume - remove volume.
  304. * @desc: volume descriptor
  305. * @no_vtbl: do not change volume table if not zero
  306. *
  307. * This function removes volume described by @desc. The volume has to be opened
  308. * in "exclusive" mode. Returns zero in case of success and a negative error
  309. * code in case of failure. The caller has to have the @ubi->device_mutex
  310. * locked.
  311. */
  312. int ubi_remove_volume(struct ubi_volume_desc *desc, int no_vtbl)
  313. {
  314. struct ubi_volume *vol = desc->vol;
  315. struct ubi_device *ubi = vol->ubi;
  316. int i, err, vol_id = vol->vol_id, reserved_pebs = vol->reserved_pebs;
  317. dbg_gen("remove device %d, volume %d", ubi->ubi_num, vol_id);
  318. ubi_assert(desc->mode == UBI_EXCLUSIVE);
  319. ubi_assert(vol == ubi->volumes[vol_id]);
  320. if (ubi->ro_mode)
  321. return -EROFS;
  322. spin_lock(&ubi->volumes_lock);
  323. if (vol->ref_count > 1) {
  324. /*
  325. * The volume is busy, probably someone is reading one of its
  326. * sysfs files.
  327. */
  328. err = -EBUSY;
  329. goto out_unlock;
  330. }
  331. ubi->volumes[vol_id] = NULL;
  332. spin_unlock(&ubi->volumes_lock);
  333. if (!no_vtbl) {
  334. err = ubi_change_vtbl_record(ubi, vol_id, NULL);
  335. if (err)
  336. goto out_err;
  337. }
  338. for (i = 0; i < vol->reserved_pebs; i++) {
  339. err = ubi_eba_unmap_leb(ubi, vol, i);
  340. if (err)
  341. goto out_err;
  342. }
  343. cdev_device_del(&vol->cdev, &vol->dev);
  344. put_device(&vol->dev);
  345. spin_lock(&ubi->volumes_lock);
  346. ubi->rsvd_pebs -= reserved_pebs;
  347. ubi->avail_pebs += reserved_pebs;
  348. ubi_update_reserved(ubi);
  349. ubi->vol_count -= 1;
  350. spin_unlock(&ubi->volumes_lock);
  351. ubi_volume_notify(ubi, vol, UBI_VOLUME_REMOVED);
  352. if (!no_vtbl)
  353. self_check_volumes(ubi);
  354. return 0;
  355. out_err:
  356. ubi_err(ubi, "cannot remove volume %d, error %d", vol_id, err);
  357. spin_lock(&ubi->volumes_lock);
  358. ubi->volumes[vol_id] = vol;
  359. out_unlock:
  360. spin_unlock(&ubi->volumes_lock);
  361. return err;
  362. }
  363. /**
  364. * ubi_resize_volume - re-size volume.
  365. * @desc: volume descriptor
  366. * @reserved_pebs: new size in physical eraseblocks
  367. *
  368. * This function re-sizes the volume and returns zero in case of success, and a
  369. * negative error code in case of failure. The caller has to have the
  370. * @ubi->device_mutex locked.
  371. */
  372. int ubi_resize_volume(struct ubi_volume_desc *desc, int reserved_pebs)
  373. {
  374. int i, err, pebs;
  375. struct ubi_volume *vol = desc->vol;
  376. struct ubi_device *ubi = vol->ubi;
  377. struct ubi_vtbl_record vtbl_rec;
  378. struct ubi_eba_table *new_eba_tbl = NULL;
  379. int vol_id = vol->vol_id;
  380. if (ubi->ro_mode)
  381. return -EROFS;
  382. dbg_gen("re-size device %d, volume %d to from %d to %d PEBs",
  383. ubi->ubi_num, vol_id, vol->reserved_pebs, reserved_pebs);
  384. if (vol->vol_type == UBI_STATIC_VOLUME &&
  385. reserved_pebs < vol->used_ebs) {
  386. ubi_err(ubi, "too small size %d, %d LEBs contain data",
  387. reserved_pebs, vol->used_ebs);
  388. return -EINVAL;
  389. }
  390. /* If the size is the same, we have nothing to do */
  391. if (reserved_pebs == vol->reserved_pebs)
  392. return 0;
  393. new_eba_tbl = ubi_eba_create_table(vol, reserved_pebs);
  394. if (IS_ERR(new_eba_tbl))
  395. return PTR_ERR(new_eba_tbl);
  396. spin_lock(&ubi->volumes_lock);
  397. if (vol->ref_count > 1) {
  398. spin_unlock(&ubi->volumes_lock);
  399. err = -EBUSY;
  400. goto out_free;
  401. }
  402. spin_unlock(&ubi->volumes_lock);
  403. /* Reserve physical eraseblocks */
  404. pebs = reserved_pebs - vol->reserved_pebs;
  405. if (pebs > 0) {
  406. spin_lock(&ubi->volumes_lock);
  407. if (pebs > ubi->avail_pebs) {
  408. ubi_err(ubi, "not enough PEBs: requested %d, available %d",
  409. pebs, ubi->avail_pebs);
  410. if (ubi->corr_peb_count)
  411. ubi_err(ubi, "%d PEBs are corrupted and not used",
  412. ubi->corr_peb_count);
  413. spin_unlock(&ubi->volumes_lock);
  414. err = -ENOSPC;
  415. goto out_free;
  416. }
  417. ubi->avail_pebs -= pebs;
  418. ubi->rsvd_pebs += pebs;
  419. ubi_eba_copy_table(vol, new_eba_tbl, vol->reserved_pebs);
  420. ubi_eba_replace_table(vol, new_eba_tbl);
  421. spin_unlock(&ubi->volumes_lock);
  422. }
  423. if (pebs < 0) {
  424. for (i = 0; i < -pebs; i++) {
  425. err = ubi_eba_unmap_leb(ubi, vol, reserved_pebs + i);
  426. if (err)
  427. goto out_acc;
  428. }
  429. spin_lock(&ubi->volumes_lock);
  430. ubi->rsvd_pebs += pebs;
  431. ubi->avail_pebs -= pebs;
  432. ubi_update_reserved(ubi);
  433. ubi_eba_copy_table(vol, new_eba_tbl, reserved_pebs);
  434. ubi_eba_replace_table(vol, new_eba_tbl);
  435. spin_unlock(&ubi->volumes_lock);
  436. }
  437. /*
  438. * When we shrink a volume we have to flush all pending (erase) work.
  439. * Otherwise it can happen that upon next attach UBI finds a LEB with
  440. * lnum > highest_lnum and refuses to attach.
  441. */
  442. if (pebs < 0) {
  443. err = ubi_wl_flush(ubi, vol_id, UBI_ALL);
  444. if (err)
  445. goto out_acc;
  446. }
  447. /* Change volume table record */
  448. vtbl_rec = ubi->vtbl[vol_id];
  449. vtbl_rec.reserved_pebs = cpu_to_be32(reserved_pebs);
  450. err = ubi_change_vtbl_record(ubi, vol_id, &vtbl_rec);
  451. if (err)
  452. goto out_acc;
  453. vol->reserved_pebs = reserved_pebs;
  454. if (vol->vol_type == UBI_DYNAMIC_VOLUME) {
  455. vol->used_ebs = reserved_pebs;
  456. vol->last_eb_bytes = vol->usable_leb_size;
  457. vol->used_bytes =
  458. (long long)vol->used_ebs * vol->usable_leb_size;
  459. }
  460. ubi_volume_notify(ubi, vol, UBI_VOLUME_RESIZED);
  461. self_check_volumes(ubi);
  462. return err;
  463. out_acc:
  464. if (pebs > 0) {
  465. spin_lock(&ubi->volumes_lock);
  466. ubi->rsvd_pebs -= pebs;
  467. ubi->avail_pebs += pebs;
  468. spin_unlock(&ubi->volumes_lock);
  469. }
  470. out_free:
  471. kfree(new_eba_tbl);
  472. return err;
  473. }
  474. /**
  475. * ubi_rename_volumes - re-name UBI volumes.
  476. * @ubi: UBI device description object
  477. * @rename_list: list of &struct ubi_rename_entry objects
  478. *
  479. * This function re-names or removes volumes specified in the re-name list.
  480. * Returns zero in case of success and a negative error code in case of
  481. * failure.
  482. */
  483. int ubi_rename_volumes(struct ubi_device *ubi, struct list_head *rename_list)
  484. {
  485. int err;
  486. struct ubi_rename_entry *re;
  487. err = ubi_vtbl_rename_volumes(ubi, rename_list);
  488. if (err)
  489. return err;
  490. list_for_each_entry(re, rename_list, list) {
  491. if (re->remove) {
  492. err = ubi_remove_volume(re->desc, 1);
  493. if (err)
  494. break;
  495. } else {
  496. struct ubi_volume *vol = re->desc->vol;
  497. spin_lock(&ubi->volumes_lock);
  498. vol->name_len = re->new_name_len;
  499. memcpy(vol->name, re->new_name, re->new_name_len + 1);
  500. spin_unlock(&ubi->volumes_lock);
  501. ubi_volume_notify(ubi, vol, UBI_VOLUME_RENAMED);
  502. }
  503. }
  504. if (!err)
  505. self_check_volumes(ubi);
  506. return err;
  507. }
  508. /**
  509. * ubi_add_volume - add volume.
  510. * @ubi: UBI device description object
  511. * @vol: volume description object
  512. *
  513. * This function adds an existing volume and initializes all its data
  514. * structures. Returns zero in case of success and a negative error code in
  515. * case of failure.
  516. */
  517. int ubi_add_volume(struct ubi_device *ubi, struct ubi_volume *vol)
  518. {
  519. int err, vol_id = vol->vol_id;
  520. dev_t dev;
  521. dbg_gen("add volume %d", vol_id);
  522. /* Register character device for the volume */
  523. cdev_init(&vol->cdev, &ubi_vol_cdev_operations);
  524. vol->cdev.owner = THIS_MODULE;
  525. dev = MKDEV(MAJOR(ubi->cdev.dev), vol->vol_id + 1);
  526. err = cdev_add(&vol->cdev, dev, 1);
  527. if (err) {
  528. ubi_err(ubi, "cannot add character device for volume %d, error %d",
  529. vol_id, err);
  530. return err;
  531. }
  532. vol->dev.release = vol_release;
  533. vol->dev.parent = &ubi->dev;
  534. vol->dev.devt = dev;
  535. vol->dev.class = &ubi_class;
  536. vol->dev.groups = volume_dev_groups;
  537. dev_set_name(&vol->dev, "%s_%d", ubi->ubi_name, vol->vol_id);
  538. err = device_register(&vol->dev);
  539. if (err)
  540. goto out_cdev;
  541. self_check_volumes(ubi);
  542. return err;
  543. out_cdev:
  544. cdev_del(&vol->cdev);
  545. return err;
  546. }
  547. /**
  548. * ubi_free_volume - free volume.
  549. * @ubi: UBI device description object
  550. * @vol: volume description object
  551. *
  552. * This function frees all resources for volume @vol but does not remove it.
  553. * Used only when the UBI device is detached.
  554. */
  555. void ubi_free_volume(struct ubi_device *ubi, struct ubi_volume *vol)
  556. {
  557. dbg_gen("free volume %d", vol->vol_id);
  558. ubi->volumes[vol->vol_id] = NULL;
  559. cdev_del(&vol->cdev);
  560. device_unregister(&vol->dev);
  561. }
  562. /**
  563. * self_check_volume - check volume information.
  564. * @ubi: UBI device description object
  565. * @vol_id: volume ID
  566. *
  567. * Returns zero if volume is all right and a a negative error code if not.
  568. */
  569. static int self_check_volume(struct ubi_device *ubi, int vol_id)
  570. {
  571. int idx = vol_id2idx(ubi, vol_id);
  572. int reserved_pebs, alignment, data_pad, vol_type, name_len, upd_marker;
  573. const struct ubi_volume *vol;
  574. long long n;
  575. const char *name;
  576. spin_lock(&ubi->volumes_lock);
  577. reserved_pebs = be32_to_cpu(ubi->vtbl[vol_id].reserved_pebs);
  578. vol = ubi->volumes[idx];
  579. if (!vol) {
  580. if (reserved_pebs) {
  581. ubi_err(ubi, "no volume info, but volume exists");
  582. goto fail;
  583. }
  584. spin_unlock(&ubi->volumes_lock);
  585. return 0;
  586. }
  587. if (vol->reserved_pebs < 0 || vol->alignment < 0 || vol->data_pad < 0 ||
  588. vol->name_len < 0) {
  589. ubi_err(ubi, "negative values");
  590. goto fail;
  591. }
  592. if (vol->alignment > ubi->leb_size || vol->alignment == 0) {
  593. ubi_err(ubi, "bad alignment");
  594. goto fail;
  595. }
  596. n = vol->alignment & (ubi->min_io_size - 1);
  597. if (vol->alignment != 1 && n) {
  598. ubi_err(ubi, "alignment is not multiple of min I/O unit");
  599. goto fail;
  600. }
  601. n = ubi->leb_size % vol->alignment;
  602. if (vol->data_pad != n) {
  603. ubi_err(ubi, "bad data_pad, has to be %lld", n);
  604. goto fail;
  605. }
  606. if (vol->vol_type != UBI_DYNAMIC_VOLUME &&
  607. vol->vol_type != UBI_STATIC_VOLUME) {
  608. ubi_err(ubi, "bad vol_type");
  609. goto fail;
  610. }
  611. if (vol->upd_marker && vol->corrupted) {
  612. ubi_err(ubi, "update marker and corrupted simultaneously");
  613. goto fail;
  614. }
  615. if (vol->reserved_pebs > ubi->good_peb_count) {
  616. ubi_err(ubi, "too large reserved_pebs");
  617. goto fail;
  618. }
  619. n = ubi->leb_size - vol->data_pad;
  620. if (vol->usable_leb_size != ubi->leb_size - vol->data_pad) {
  621. ubi_err(ubi, "bad usable_leb_size, has to be %lld", n);
  622. goto fail;
  623. }
  624. if (vol->name_len > UBI_VOL_NAME_MAX) {
  625. ubi_err(ubi, "too long volume name, max is %d",
  626. UBI_VOL_NAME_MAX);
  627. goto fail;
  628. }
  629. n = strnlen(vol->name, vol->name_len + 1);
  630. if (n != vol->name_len) {
  631. ubi_err(ubi, "bad name_len %lld", n);
  632. goto fail;
  633. }
  634. n = (long long)vol->used_ebs * vol->usable_leb_size;
  635. if (vol->vol_type == UBI_DYNAMIC_VOLUME) {
  636. if (vol->corrupted) {
  637. ubi_err(ubi, "corrupted dynamic volume");
  638. goto fail;
  639. }
  640. if (vol->used_ebs != vol->reserved_pebs) {
  641. ubi_err(ubi, "bad used_ebs");
  642. goto fail;
  643. }
  644. if (vol->last_eb_bytes != vol->usable_leb_size) {
  645. ubi_err(ubi, "bad last_eb_bytes");
  646. goto fail;
  647. }
  648. if (vol->used_bytes != n) {
  649. ubi_err(ubi, "bad used_bytes");
  650. goto fail;
  651. }
  652. if (vol->skip_check) {
  653. ubi_err(ubi, "bad skip_check");
  654. goto fail;
  655. }
  656. } else {
  657. if (vol->used_ebs < 0 || vol->used_ebs > vol->reserved_pebs) {
  658. ubi_err(ubi, "bad used_ebs");
  659. goto fail;
  660. }
  661. if (vol->last_eb_bytes < 0 ||
  662. vol->last_eb_bytes > vol->usable_leb_size) {
  663. ubi_err(ubi, "bad last_eb_bytes");
  664. goto fail;
  665. }
  666. if (vol->used_bytes < 0 || vol->used_bytes > n ||
  667. vol->used_bytes < n - vol->usable_leb_size) {
  668. ubi_err(ubi, "bad used_bytes");
  669. goto fail;
  670. }
  671. }
  672. alignment = be32_to_cpu(ubi->vtbl[vol_id].alignment);
  673. data_pad = be32_to_cpu(ubi->vtbl[vol_id].data_pad);
  674. name_len = be16_to_cpu(ubi->vtbl[vol_id].name_len);
  675. upd_marker = ubi->vtbl[vol_id].upd_marker;
  676. name = &ubi->vtbl[vol_id].name[0];
  677. if (ubi->vtbl[vol_id].vol_type == UBI_VID_DYNAMIC)
  678. vol_type = UBI_DYNAMIC_VOLUME;
  679. else
  680. vol_type = UBI_STATIC_VOLUME;
  681. if (alignment != vol->alignment || data_pad != vol->data_pad ||
  682. upd_marker != vol->upd_marker || vol_type != vol->vol_type ||
  683. name_len != vol->name_len || strncmp(name, vol->name, name_len)) {
  684. ubi_err(ubi, "volume info is different");
  685. goto fail;
  686. }
  687. spin_unlock(&ubi->volumes_lock);
  688. return 0;
  689. fail:
  690. ubi_err(ubi, "self-check failed for volume %d", vol_id);
  691. if (vol)
  692. ubi_dump_vol_info(vol);
  693. ubi_dump_vtbl_record(&ubi->vtbl[vol_id], vol_id);
  694. dump_stack();
  695. spin_unlock(&ubi->volumes_lock);
  696. return -EINVAL;
  697. }
  698. /**
  699. * self_check_volumes - check information about all volumes.
  700. * @ubi: UBI device description object
  701. *
  702. * Returns zero if volumes are all right and a a negative error code if not.
  703. */
  704. static int self_check_volumes(struct ubi_device *ubi)
  705. {
  706. int i, err = 0;
  707. if (!ubi_dbg_chk_gen(ubi))
  708. return 0;
  709. for (i = 0; i < ubi->vtbl_slots; i++) {
  710. err = self_check_volume(ubi, i);
  711. if (err)
  712. break;
  713. }
  714. return err;
  715. }