vmt.c 21 KB

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