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