vmt.c 21 KB

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