vmt.c 22 KB

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