vmt.c 23 KB

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