vmt.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854
  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. ubi->vol_count += 1;
  322. spin_unlock(&ubi->volumes_lock);
  323. paranoid_check_volumes(ubi);
  324. return 0;
  325. out_sysfs:
  326. /*
  327. * We have registered our device, we should not free the volume*
  328. * description object in this function in case of an error - it is
  329. * freed by the release function.
  330. *
  331. * Get device reference to prevent the release function from being
  332. * called just after sysfs has been closed.
  333. */
  334. dont_free = 1;
  335. get_device(&vol->dev);
  336. volume_sysfs_close(vol);
  337. out_gluebi:
  338. if (ubi_destroy_gluebi(vol))
  339. dbg_err("cannot destroy gluebi for volume %d:%d",
  340. ubi->ubi_num, vol_id);
  341. out_cdev:
  342. cdev_del(&vol->cdev);
  343. out_mapping:
  344. kfree(vol->eba_tbl);
  345. out_acc:
  346. spin_lock(&ubi->volumes_lock);
  347. ubi->rsvd_pebs -= vol->reserved_pebs;
  348. ubi->avail_pebs += vol->reserved_pebs;
  349. out_unlock:
  350. spin_unlock(&ubi->volumes_lock);
  351. if (dont_free)
  352. put_device(&vol->dev);
  353. else
  354. kfree(vol);
  355. ubi_err("cannot create volume %d, error %d", vol_id, err);
  356. return err;
  357. }
  358. /**
  359. * ubi_remove_volume - remove volume.
  360. * @desc: volume descriptor
  361. *
  362. * This function removes volume described by @desc. The volume has to be opened
  363. * in "exclusive" mode. Returns zero in case of success and a negative error
  364. * code in case of failure. The caller has to have the @ubi->volumes_mutex
  365. * locked.
  366. */
  367. int ubi_remove_volume(struct ubi_volume_desc *desc)
  368. {
  369. struct ubi_volume *vol = desc->vol;
  370. struct ubi_device *ubi = vol->ubi;
  371. int i, err, vol_id = vol->vol_id, reserved_pebs = vol->reserved_pebs;
  372. dbg_msg("remove UBI volume %d", vol_id);
  373. ubi_assert(desc->mode == UBI_EXCLUSIVE);
  374. ubi_assert(vol == ubi->volumes[vol_id]);
  375. if (ubi->ro_mode)
  376. return -EROFS;
  377. spin_lock(&ubi->volumes_lock);
  378. if (vol->ref_count > 1) {
  379. /*
  380. * The volume is busy, probably someone is reading one of its
  381. * sysfs files.
  382. */
  383. err = -EBUSY;
  384. goto out_unlock;
  385. }
  386. ubi->volumes[vol_id] = NULL;
  387. spin_unlock(&ubi->volumes_lock);
  388. err = ubi_destroy_gluebi(vol);
  389. if (err)
  390. goto out_err;
  391. err = ubi_change_vtbl_record(ubi, vol_id, NULL);
  392. if (err)
  393. goto out_err;
  394. for (i = 0; i < vol->reserved_pebs; i++) {
  395. err = ubi_eba_unmap_leb(ubi, vol, i);
  396. if (err)
  397. goto out_err;
  398. }
  399. kfree(vol->eba_tbl);
  400. vol->eba_tbl = NULL;
  401. cdev_del(&vol->cdev);
  402. volume_sysfs_close(vol);
  403. spin_lock(&ubi->volumes_lock);
  404. ubi->rsvd_pebs -= reserved_pebs;
  405. ubi->avail_pebs += reserved_pebs;
  406. i = ubi->beb_rsvd_level - ubi->beb_rsvd_pebs;
  407. if (i > 0) {
  408. i = ubi->avail_pebs >= i ? i : ubi->avail_pebs;
  409. ubi->avail_pebs -= i;
  410. ubi->rsvd_pebs += i;
  411. ubi->beb_rsvd_pebs += i;
  412. if (i > 0)
  413. ubi_msg("reserve more %d PEBs", i);
  414. }
  415. ubi->vol_count -= 1;
  416. spin_unlock(&ubi->volumes_lock);
  417. paranoid_check_volumes(ubi);
  418. return 0;
  419. out_err:
  420. ubi_err("cannot remove volume %d, error %d", vol_id, err);
  421. spin_lock(&ubi->volumes_lock);
  422. ubi->volumes[vol_id] = vol;
  423. out_unlock:
  424. spin_unlock(&ubi->volumes_lock);
  425. return err;
  426. }
  427. /**
  428. * ubi_resize_volume - re-size volume.
  429. * @desc: volume descriptor
  430. * @reserved_pebs: new size in physical eraseblocks
  431. *
  432. * This function re-sizes the volume and returns zero in case of success, and a
  433. * negative error code in case of failure. The caller has to have the
  434. * @ubi->volumes_mutex locked.
  435. */
  436. int ubi_resize_volume(struct ubi_volume_desc *desc, int reserved_pebs)
  437. {
  438. int i, err, pebs, *new_mapping;
  439. struct ubi_volume *vol = desc->vol;
  440. struct ubi_device *ubi = vol->ubi;
  441. struct ubi_vtbl_record vtbl_rec;
  442. int vol_id = vol->vol_id;
  443. if (ubi->ro_mode)
  444. return -EROFS;
  445. dbg_msg("re-size volume %d to from %d to %d PEBs",
  446. vol_id, vol->reserved_pebs, reserved_pebs);
  447. if (vol->vol_type == UBI_STATIC_VOLUME &&
  448. reserved_pebs < vol->used_ebs) {
  449. dbg_err("too small size %d, %d LEBs contain data",
  450. reserved_pebs, vol->used_ebs);
  451. return -EINVAL;
  452. }
  453. /* If the size is the same, we have nothing to do */
  454. if (reserved_pebs == vol->reserved_pebs)
  455. return 0;
  456. new_mapping = kmalloc(reserved_pebs * sizeof(int), GFP_KERNEL);
  457. if (!new_mapping)
  458. return -ENOMEM;
  459. for (i = 0; i < reserved_pebs; i++)
  460. new_mapping[i] = UBI_LEB_UNMAPPED;
  461. spin_lock(&ubi->volumes_lock);
  462. if (vol->ref_count > 1) {
  463. spin_unlock(&ubi->volumes_lock);
  464. err = -EBUSY;
  465. goto out_free;
  466. }
  467. spin_unlock(&ubi->volumes_lock);
  468. /* Reserve physical eraseblocks */
  469. pebs = reserved_pebs - vol->reserved_pebs;
  470. if (pebs > 0) {
  471. spin_lock(&ubi->volumes_lock);
  472. if (pebs > ubi->avail_pebs) {
  473. dbg_err("not enough PEBs: requested %d, available %d",
  474. pebs, ubi->avail_pebs);
  475. spin_unlock(&ubi->volumes_lock);
  476. err = -ENOSPC;
  477. goto out_free;
  478. }
  479. ubi->avail_pebs -= pebs;
  480. ubi->rsvd_pebs += pebs;
  481. for (i = 0; i < vol->reserved_pebs; i++)
  482. new_mapping[i] = vol->eba_tbl[i];
  483. kfree(vol->eba_tbl);
  484. vol->eba_tbl = new_mapping;
  485. spin_unlock(&ubi->volumes_lock);
  486. }
  487. /* Change volume table record */
  488. memcpy(&vtbl_rec, &ubi->vtbl[vol_id], sizeof(struct ubi_vtbl_record));
  489. vtbl_rec.reserved_pebs = cpu_to_be32(reserved_pebs);
  490. err = ubi_change_vtbl_record(ubi, vol_id, &vtbl_rec);
  491. if (err)
  492. goto out_acc;
  493. if (pebs < 0) {
  494. for (i = 0; i < -pebs; i++) {
  495. err = ubi_eba_unmap_leb(ubi, vol, reserved_pebs + i);
  496. if (err)
  497. goto out_acc;
  498. }
  499. spin_lock(&ubi->volumes_lock);
  500. ubi->rsvd_pebs += pebs;
  501. ubi->avail_pebs -= pebs;
  502. pebs = ubi->beb_rsvd_level - ubi->beb_rsvd_pebs;
  503. if (pebs > 0) {
  504. pebs = ubi->avail_pebs >= pebs ? pebs : ubi->avail_pebs;
  505. ubi->avail_pebs -= pebs;
  506. ubi->rsvd_pebs += pebs;
  507. ubi->beb_rsvd_pebs += pebs;
  508. if (pebs > 0)
  509. ubi_msg("reserve more %d PEBs", pebs);
  510. }
  511. for (i = 0; i < reserved_pebs; i++)
  512. new_mapping[i] = vol->eba_tbl[i];
  513. kfree(vol->eba_tbl);
  514. vol->eba_tbl = new_mapping;
  515. spin_unlock(&ubi->volumes_lock);
  516. }
  517. vol->reserved_pebs = reserved_pebs;
  518. if (vol->vol_type == UBI_DYNAMIC_VOLUME) {
  519. vol->used_ebs = reserved_pebs;
  520. vol->last_eb_bytes = vol->usable_leb_size;
  521. vol->used_bytes =
  522. (long long)vol->used_ebs * vol->usable_leb_size;
  523. }
  524. paranoid_check_volumes(ubi);
  525. return 0;
  526. out_acc:
  527. if (pebs > 0) {
  528. spin_lock(&ubi->volumes_lock);
  529. ubi->rsvd_pebs -= pebs;
  530. ubi->avail_pebs += pebs;
  531. spin_unlock(&ubi->volumes_lock);
  532. }
  533. out_free:
  534. kfree(new_mapping);
  535. return err;
  536. }
  537. /**
  538. * ubi_add_volume - add volume.
  539. * @ubi: UBI device description object
  540. * @vol: volume description object
  541. *
  542. * This function adds an existing volume and initializes all its data
  543. * structures. Returns zero in case of success and a negative error code in
  544. * case of failure.
  545. */
  546. int ubi_add_volume(struct ubi_device *ubi, struct ubi_volume *vol)
  547. {
  548. int err, vol_id = vol->vol_id;
  549. dev_t dev;
  550. dbg_msg("add volume %d", vol_id);
  551. ubi_dbg_dump_vol_info(vol);
  552. /* Register character device for the volume */
  553. cdev_init(&vol->cdev, &ubi_vol_cdev_operations);
  554. vol->cdev.owner = THIS_MODULE;
  555. dev = MKDEV(MAJOR(ubi->cdev.dev), vol->vol_id + 1);
  556. err = cdev_add(&vol->cdev, dev, 1);
  557. if (err) {
  558. ubi_err("cannot add character device for volume %d, error %d",
  559. vol_id, err);
  560. return err;
  561. }
  562. err = ubi_create_gluebi(ubi, vol);
  563. if (err)
  564. goto out_cdev;
  565. vol->dev.release = vol_release;
  566. vol->dev.parent = &ubi->dev;
  567. vol->dev.devt = dev;
  568. vol->dev.class = ubi_class;
  569. sprintf(&vol->dev.bus_id[0], "%s_%d", ubi->ubi_name, vol->vol_id);
  570. err = device_register(&vol->dev);
  571. if (err)
  572. goto out_gluebi;
  573. err = volume_sysfs_init(ubi, vol);
  574. if (err) {
  575. cdev_del(&vol->cdev);
  576. err = ubi_destroy_gluebi(vol);
  577. volume_sysfs_close(vol);
  578. return err;
  579. }
  580. paranoid_check_volumes(ubi);
  581. return 0;
  582. out_gluebi:
  583. err = ubi_destroy_gluebi(vol);
  584. out_cdev:
  585. cdev_del(&vol->cdev);
  586. return err;
  587. }
  588. /**
  589. * ubi_free_volume - free volume.
  590. * @ubi: UBI device description object
  591. * @vol: volume description object
  592. *
  593. * This function frees all resources for volume @vol but does not remove it.
  594. * Used only when the UBI device is detached.
  595. */
  596. void ubi_free_volume(struct ubi_device *ubi, struct ubi_volume *vol)
  597. {
  598. int err;
  599. dbg_msg("free volume %d", vol->vol_id);
  600. ubi->volumes[vol->vol_id] = NULL;
  601. err = ubi_destroy_gluebi(vol);
  602. cdev_del(&vol->cdev);
  603. volume_sysfs_close(vol);
  604. }
  605. #ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
  606. /**
  607. * paranoid_check_volume - check volume information.
  608. * @ubi: UBI device description object
  609. * @vol_id: volume ID
  610. */
  611. static void paranoid_check_volume(struct ubi_device *ubi, int vol_id)
  612. {
  613. int idx = vol_id2idx(ubi, vol_id);
  614. int reserved_pebs, alignment, data_pad, vol_type, name_len, upd_marker;
  615. const struct ubi_volume *vol;
  616. long long n;
  617. const char *name;
  618. spin_lock(&ubi->volumes_lock);
  619. reserved_pebs = be32_to_cpu(ubi->vtbl[vol_id].reserved_pebs);
  620. vol = ubi->volumes[idx];
  621. if (!vol) {
  622. if (reserved_pebs) {
  623. ubi_err("no volume info, but volume exists");
  624. goto fail;
  625. }
  626. spin_unlock(&ubi->volumes_lock);
  627. return;
  628. }
  629. if (vol->exclusive) {
  630. /*
  631. * The volume may be being created at the moment, do not check
  632. * it (e.g., it may be in the middle of ubi_create_volume().
  633. */
  634. spin_unlock(&ubi->volumes_lock);
  635. return;
  636. }
  637. if (vol->reserved_pebs < 0 || vol->alignment < 0 || vol->data_pad < 0 ||
  638. vol->name_len < 0) {
  639. ubi_err("negative values");
  640. goto fail;
  641. }
  642. if (vol->alignment > ubi->leb_size || vol->alignment == 0) {
  643. ubi_err("bad alignment");
  644. goto fail;
  645. }
  646. n = vol->alignment % ubi->min_io_size;
  647. if (vol->alignment != 1 && n) {
  648. ubi_err("alignment is not multiple of min I/O unit");
  649. goto fail;
  650. }
  651. n = ubi->leb_size % vol->alignment;
  652. if (vol->data_pad != n) {
  653. ubi_err("bad data_pad, has to be %lld", n);
  654. goto fail;
  655. }
  656. if (vol->vol_type != UBI_DYNAMIC_VOLUME &&
  657. vol->vol_type != UBI_STATIC_VOLUME) {
  658. ubi_err("bad vol_type");
  659. goto fail;
  660. }
  661. if (vol->upd_marker && vol->corrupted) {
  662. dbg_err("update marker and corrupted simultaneously");
  663. goto fail;
  664. }
  665. if (vol->reserved_pebs > ubi->good_peb_count) {
  666. ubi_err("too large reserved_pebs");
  667. goto fail;
  668. }
  669. n = ubi->leb_size - vol->data_pad;
  670. if (vol->usable_leb_size != ubi->leb_size - vol->data_pad) {
  671. ubi_err("bad usable_leb_size, has to be %lld", n);
  672. goto fail;
  673. }
  674. if (vol->name_len > UBI_VOL_NAME_MAX) {
  675. ubi_err("too long volume name, max is %d", UBI_VOL_NAME_MAX);
  676. goto fail;
  677. }
  678. if (!vol->name) {
  679. ubi_err("NULL volume name");
  680. goto fail;
  681. }
  682. n = strnlen(vol->name, vol->name_len + 1);
  683. if (n != vol->name_len) {
  684. ubi_err("bad name_len %lld", n);
  685. goto fail;
  686. }
  687. n = (long long)vol->used_ebs * vol->usable_leb_size;
  688. if (vol->vol_type == UBI_DYNAMIC_VOLUME) {
  689. if (vol->corrupted) {
  690. ubi_err("corrupted dynamic volume");
  691. goto fail;
  692. }
  693. if (vol->used_ebs != vol->reserved_pebs) {
  694. ubi_err("bad used_ebs");
  695. goto fail;
  696. }
  697. if (vol->last_eb_bytes != vol->usable_leb_size) {
  698. ubi_err("bad last_eb_bytes");
  699. goto fail;
  700. }
  701. if (vol->used_bytes != n) {
  702. ubi_err("bad used_bytes");
  703. goto fail;
  704. }
  705. } else {
  706. if (vol->used_ebs < 0 || vol->used_ebs > vol->reserved_pebs) {
  707. ubi_err("bad used_ebs");
  708. goto fail;
  709. }
  710. if (vol->last_eb_bytes < 0 ||
  711. vol->last_eb_bytes > vol->usable_leb_size) {
  712. ubi_err("bad last_eb_bytes");
  713. goto fail;
  714. }
  715. if (vol->used_bytes < 0 || vol->used_bytes > n ||
  716. vol->used_bytes < n - vol->usable_leb_size) {
  717. ubi_err("bad used_bytes");
  718. goto fail;
  719. }
  720. }
  721. alignment = be32_to_cpu(ubi->vtbl[vol_id].alignment);
  722. data_pad = be32_to_cpu(ubi->vtbl[vol_id].data_pad);
  723. name_len = be16_to_cpu(ubi->vtbl[vol_id].name_len);
  724. upd_marker = ubi->vtbl[vol_id].upd_marker;
  725. name = &ubi->vtbl[vol_id].name[0];
  726. if (ubi->vtbl[vol_id].vol_type == UBI_VID_DYNAMIC)
  727. vol_type = UBI_DYNAMIC_VOLUME;
  728. else
  729. vol_type = UBI_STATIC_VOLUME;
  730. if (alignment != vol->alignment || data_pad != vol->data_pad ||
  731. upd_marker != vol->upd_marker || vol_type != vol->vol_type ||
  732. name_len!= vol->name_len || strncmp(name, vol->name, name_len)) {
  733. ubi_err("volume info is different");
  734. goto fail;
  735. }
  736. spin_unlock(&ubi->volumes_lock);
  737. return;
  738. fail:
  739. ubi_err("paranoid check failed for volume %d", vol_id);
  740. ubi_dbg_dump_vol_info(vol);
  741. ubi_dbg_dump_vtbl_record(&ubi->vtbl[vol_id], vol_id);
  742. spin_unlock(&ubi->volumes_lock);
  743. BUG();
  744. }
  745. /**
  746. * paranoid_check_volumes - check information about all volumes.
  747. * @ubi: UBI device description object
  748. */
  749. static void paranoid_check_volumes(struct ubi_device *ubi)
  750. {
  751. int i;
  752. for (i = 0; i < ubi->vtbl_slots; i++)
  753. paranoid_check_volume(ubi, i);
  754. }
  755. #endif