vmt.c 24 KB

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