vmt.c 21 KB

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