vmt.c 23 KB

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