vmt.c 21 KB

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