vmt.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893
  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 <linux/math64.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. 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_gen("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_gen("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. vol->reserved_pebs += div_u64(req->bytes + vol->usable_leb_size - 1,
  233. vol->usable_leb_size);
  234. /* Reserve physical eraseblocks */
  235. if (vol->reserved_pebs > ubi->avail_pebs) {
  236. dbg_err("not enough PEBs, only %d available", ubi->avail_pebs);
  237. err = -ENOSPC;
  238. goto out_unlock;
  239. }
  240. ubi->avail_pebs -= vol->reserved_pebs;
  241. ubi->rsvd_pebs += vol->reserved_pebs;
  242. spin_unlock(&ubi->volumes_lock);
  243. vol->vol_id = vol_id;
  244. vol->alignment = req->alignment;
  245. vol->data_pad = ubi->leb_size % vol->alignment;
  246. vol->vol_type = req->vol_type;
  247. vol->name_len = req->name_len;
  248. memcpy(vol->name, req->name, vol->name_len);
  249. vol->ubi = ubi;
  250. /*
  251. * Finish all pending erases because there may be some LEBs belonging
  252. * to the same volume ID.
  253. */
  254. err = ubi_wl_flush(ubi);
  255. if (err)
  256. goto out_acc;
  257. vol->eba_tbl = kmalloc(vol->reserved_pebs * sizeof(int), GFP_KERNEL);
  258. if (!vol->eba_tbl) {
  259. err = -ENOMEM;
  260. goto out_acc;
  261. }
  262. for (i = 0; i < vol->reserved_pebs; i++)
  263. vol->eba_tbl[i] = UBI_LEB_UNMAPPED;
  264. if (vol->vol_type == UBI_DYNAMIC_VOLUME) {
  265. vol->used_ebs = vol->reserved_pebs;
  266. vol->last_eb_bytes = vol->usable_leb_size;
  267. vol->used_bytes =
  268. (long long)vol->used_ebs * vol->usable_leb_size;
  269. } else {
  270. vol->used_ebs = div_u64_rem(vol->used_bytes,
  271. vol->usable_leb_size,
  272. &vol->last_eb_bytes);
  273. if (vol->last_eb_bytes != 0)
  274. vol->used_ebs += 1;
  275. else
  276. vol->last_eb_bytes = vol->usable_leb_size;
  277. }
  278. /* Register character device for the volume */
  279. cdev_init(&vol->cdev, &ubi_vol_cdev_operations);
  280. vol->cdev.owner = THIS_MODULE;
  281. dev = MKDEV(MAJOR(ubi->cdev.dev), vol_id + 1);
  282. err = cdev_add(&vol->cdev, dev, 1);
  283. if (err) {
  284. ubi_err("cannot add character device");
  285. goto out_mapping;
  286. }
  287. err = ubi_create_gluebi(ubi, vol);
  288. if (err)
  289. goto out_cdev;
  290. vol->dev.release = vol_release;
  291. vol->dev.parent = &ubi->dev;
  292. vol->dev.devt = dev;
  293. vol->dev.class = ubi_class;
  294. dev_set_name(&vol->dev, "%s_%d", ubi->ubi_name, vol->vol_id);
  295. err = device_register(&vol->dev);
  296. if (err) {
  297. ubi_err("cannot register device");
  298. goto out_gluebi;
  299. }
  300. err = volume_sysfs_init(ubi, vol);
  301. if (err)
  302. goto out_sysfs;
  303. /* Fill volume table record */
  304. memset(&vtbl_rec, 0, sizeof(struct ubi_vtbl_record));
  305. vtbl_rec.reserved_pebs = cpu_to_be32(vol->reserved_pebs);
  306. vtbl_rec.alignment = cpu_to_be32(vol->alignment);
  307. vtbl_rec.data_pad = cpu_to_be32(vol->data_pad);
  308. vtbl_rec.name_len = cpu_to_be16(vol->name_len);
  309. if (vol->vol_type == UBI_DYNAMIC_VOLUME)
  310. vtbl_rec.vol_type = UBI_VID_DYNAMIC;
  311. else
  312. vtbl_rec.vol_type = UBI_VID_STATIC;
  313. memcpy(vtbl_rec.name, vol->name, vol->name_len);
  314. err = ubi_change_vtbl_record(ubi, vol_id, &vtbl_rec);
  315. if (err)
  316. goto out_sysfs;
  317. spin_lock(&ubi->volumes_lock);
  318. ubi->volumes[vol_id] = vol;
  319. ubi->vol_count += 1;
  320. spin_unlock(&ubi->volumes_lock);
  321. err = paranoid_check_volumes(ubi);
  322. return err;
  323. out_sysfs:
  324. /*
  325. * We have registered our device, we should not free the volume
  326. * description object in this function in case of an error - it is
  327. * freed by the release function.
  328. *
  329. * Get device reference to prevent the release function from being
  330. * called just after sysfs has been closed.
  331. */
  332. do_free = 0;
  333. get_device(&vol->dev);
  334. volume_sysfs_close(vol);
  335. out_gluebi:
  336. if (ubi_destroy_gluebi(vol))
  337. dbg_err("cannot destroy gluebi for volume %d:%d",
  338. ubi->ubi_num, vol_id);
  339. out_cdev:
  340. cdev_del(&vol->cdev);
  341. out_mapping:
  342. if (do_free)
  343. kfree(vol->eba_tbl);
  344. out_acc:
  345. spin_lock(&ubi->volumes_lock);
  346. ubi->rsvd_pebs -= vol->reserved_pebs;
  347. ubi->avail_pebs += vol->reserved_pebs;
  348. out_unlock:
  349. spin_unlock(&ubi->volumes_lock);
  350. if (do_free)
  351. kfree(vol);
  352. else
  353. put_device(&vol->dev);
  354. ubi_err("cannot create volume %d, error %d", vol_id, err);
  355. return err;
  356. }
  357. /**
  358. * ubi_remove_volume - remove volume.
  359. * @desc: volume descriptor
  360. * @no_vtbl: do not change volume table if not zero
  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, int no_vtbl)
  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_gen("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. if (!no_vtbl) {
  392. err = ubi_change_vtbl_record(ubi, vol_id, NULL);
  393. if (err)
  394. goto out_err;
  395. }
  396. for (i = 0; i < vol->reserved_pebs; i++) {
  397. err = ubi_eba_unmap_leb(ubi, vol, i);
  398. if (err)
  399. goto out_err;
  400. }
  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. if (!no_vtbl)
  418. err = paranoid_check_volumes(ubi);
  419. return err;
  420. out_err:
  421. ubi_err("cannot remove volume %d, error %d", vol_id, err);
  422. spin_lock(&ubi->volumes_lock);
  423. ubi->volumes[vol_id] = vol;
  424. out_unlock:
  425. spin_unlock(&ubi->volumes_lock);
  426. return err;
  427. }
  428. /**
  429. * ubi_resize_volume - re-size volume.
  430. * @desc: volume descriptor
  431. * @reserved_pebs: new size in physical eraseblocks
  432. *
  433. * This function re-sizes the volume and returns zero in case of success, and a
  434. * negative error code in case of failure. The caller has to have the
  435. * @ubi->volumes_mutex locked.
  436. */
  437. int ubi_resize_volume(struct ubi_volume_desc *desc, int reserved_pebs)
  438. {
  439. int i, err, pebs, *new_mapping;
  440. struct ubi_volume *vol = desc->vol;
  441. struct ubi_device *ubi = vol->ubi;
  442. struct ubi_vtbl_record vtbl_rec;
  443. int vol_id = vol->vol_id;
  444. if (ubi->ro_mode)
  445. return -EROFS;
  446. dbg_gen("re-size volume %d to from %d to %d PEBs",
  447. vol_id, vol->reserved_pebs, reserved_pebs);
  448. if (vol->vol_type == UBI_STATIC_VOLUME &&
  449. reserved_pebs < vol->used_ebs) {
  450. dbg_err("too small size %d, %d LEBs contain data",
  451. reserved_pebs, vol->used_ebs);
  452. return -EINVAL;
  453. }
  454. /* If the size is the same, we have nothing to do */
  455. if (reserved_pebs == vol->reserved_pebs)
  456. return 0;
  457. new_mapping = kmalloc(reserved_pebs * sizeof(int), GFP_KERNEL);
  458. if (!new_mapping)
  459. return -ENOMEM;
  460. for (i = 0; i < reserved_pebs; i++)
  461. new_mapping[i] = UBI_LEB_UNMAPPED;
  462. spin_lock(&ubi->volumes_lock);
  463. if (vol->ref_count > 1) {
  464. spin_unlock(&ubi->volumes_lock);
  465. err = -EBUSY;
  466. goto out_free;
  467. }
  468. spin_unlock(&ubi->volumes_lock);
  469. /* Reserve physical eraseblocks */
  470. pebs = reserved_pebs - vol->reserved_pebs;
  471. if (pebs > 0) {
  472. spin_lock(&ubi->volumes_lock);
  473. if (pebs > ubi->avail_pebs) {
  474. dbg_err("not enough PEBs: requested %d, available %d",
  475. pebs, ubi->avail_pebs);
  476. spin_unlock(&ubi->volumes_lock);
  477. err = -ENOSPC;
  478. goto out_free;
  479. }
  480. ubi->avail_pebs -= pebs;
  481. ubi->rsvd_pebs += pebs;
  482. for (i = 0; i < vol->reserved_pebs; i++)
  483. new_mapping[i] = vol->eba_tbl[i];
  484. kfree(vol->eba_tbl);
  485. vol->eba_tbl = new_mapping;
  486. spin_unlock(&ubi->volumes_lock);
  487. }
  488. /* Change volume table record */
  489. memcpy(&vtbl_rec, &ubi->vtbl[vol_id], sizeof(struct ubi_vtbl_record));
  490. vtbl_rec.reserved_pebs = cpu_to_be32(reserved_pebs);
  491. err = ubi_change_vtbl_record(ubi, vol_id, &vtbl_rec);
  492. if (err)
  493. goto out_acc;
  494. if (pebs < 0) {
  495. for (i = 0; i < -pebs; i++) {
  496. err = ubi_eba_unmap_leb(ubi, vol, reserved_pebs + i);
  497. if (err)
  498. goto out_acc;
  499. }
  500. spin_lock(&ubi->volumes_lock);
  501. ubi->rsvd_pebs += pebs;
  502. ubi->avail_pebs -= pebs;
  503. pebs = ubi->beb_rsvd_level - ubi->beb_rsvd_pebs;
  504. if (pebs > 0) {
  505. pebs = ubi->avail_pebs >= pebs ? pebs : ubi->avail_pebs;
  506. ubi->avail_pebs -= pebs;
  507. ubi->rsvd_pebs += pebs;
  508. ubi->beb_rsvd_pebs += pebs;
  509. if (pebs > 0)
  510. ubi_msg("reserve more %d PEBs", pebs);
  511. }
  512. for (i = 0; i < reserved_pebs; i++)
  513. new_mapping[i] = vol->eba_tbl[i];
  514. kfree(vol->eba_tbl);
  515. vol->eba_tbl = new_mapping;
  516. spin_unlock(&ubi->volumes_lock);
  517. }
  518. vol->reserved_pebs = reserved_pebs;
  519. if (vol->vol_type == UBI_DYNAMIC_VOLUME) {
  520. vol->used_ebs = reserved_pebs;
  521. vol->last_eb_bytes = vol->usable_leb_size;
  522. vol->used_bytes =
  523. (long long)vol->used_ebs * vol->usable_leb_size;
  524. }
  525. err = paranoid_check_volumes(ubi);
  526. return err;
  527. out_acc:
  528. if (pebs > 0) {
  529. spin_lock(&ubi->volumes_lock);
  530. ubi->rsvd_pebs -= pebs;
  531. ubi->avail_pebs += pebs;
  532. spin_unlock(&ubi->volumes_lock);
  533. }
  534. out_free:
  535. kfree(new_mapping);
  536. return err;
  537. }
  538. /**
  539. * ubi_rename_volumes - re-name UBI volumes.
  540. * @ubi: UBI device description object
  541. * @rename_list: list of &struct ubi_rename_entry objects
  542. *
  543. * This function re-names or removes volumes specified in the re-name list.
  544. * Returns zero in case of success and a negative error code in case of
  545. * failure.
  546. */
  547. int ubi_rename_volumes(struct ubi_device *ubi, struct list_head *rename_list)
  548. {
  549. int err;
  550. struct ubi_rename_entry *re;
  551. err = ubi_vtbl_rename_volumes(ubi, rename_list);
  552. if (err)
  553. return err;
  554. list_for_each_entry(re, rename_list, list) {
  555. if (re->remove) {
  556. err = ubi_remove_volume(re->desc, 1);
  557. if (err)
  558. break;
  559. } else {
  560. struct ubi_volume *vol = re->desc->vol;
  561. spin_lock(&ubi->volumes_lock);
  562. vol->name_len = re->new_name_len;
  563. memcpy(vol->name, re->new_name, re->new_name_len + 1);
  564. spin_unlock(&ubi->volumes_lock);
  565. }
  566. }
  567. if (!err)
  568. err = paranoid_check_volumes(ubi);
  569. return err;
  570. }
  571. /**
  572. * ubi_add_volume - add volume.
  573. * @ubi: UBI device description object
  574. * @vol: volume description object
  575. *
  576. * This function adds an existing volume and initializes all its data
  577. * structures. Returns zero in case of success and a negative error code in
  578. * case of failure.
  579. */
  580. int ubi_add_volume(struct ubi_device *ubi, struct ubi_volume *vol)
  581. {
  582. int err, vol_id = vol->vol_id;
  583. dev_t dev;
  584. dbg_gen("add volume %d", vol_id);
  585. /* Register character device for the volume */
  586. cdev_init(&vol->cdev, &ubi_vol_cdev_operations);
  587. vol->cdev.owner = THIS_MODULE;
  588. dev = MKDEV(MAJOR(ubi->cdev.dev), vol->vol_id + 1);
  589. err = cdev_add(&vol->cdev, dev, 1);
  590. if (err) {
  591. ubi_err("cannot add character device for volume %d, error %d",
  592. vol_id, err);
  593. return err;
  594. }
  595. err = ubi_create_gluebi(ubi, vol);
  596. if (err)
  597. goto out_cdev;
  598. vol->dev.release = vol_release;
  599. vol->dev.parent = &ubi->dev;
  600. vol->dev.devt = dev;
  601. vol->dev.class = ubi_class;
  602. dev_set_name(&vol->dev, "%s_%d", ubi->ubi_name, vol->vol_id);
  603. err = device_register(&vol->dev);
  604. if (err)
  605. goto out_gluebi;
  606. err = volume_sysfs_init(ubi, vol);
  607. if (err) {
  608. cdev_del(&vol->cdev);
  609. err = ubi_destroy_gluebi(vol);
  610. volume_sysfs_close(vol);
  611. return err;
  612. }
  613. err = paranoid_check_volumes(ubi);
  614. return err;
  615. out_gluebi:
  616. err = ubi_destroy_gluebi(vol);
  617. out_cdev:
  618. cdev_del(&vol->cdev);
  619. return err;
  620. }
  621. /**
  622. * ubi_free_volume - free volume.
  623. * @ubi: UBI device description object
  624. * @vol: volume description object
  625. *
  626. * This function frees all resources for volume @vol but does not remove it.
  627. * Used only when the UBI device is detached.
  628. */
  629. void ubi_free_volume(struct ubi_device *ubi, struct ubi_volume *vol)
  630. {
  631. int err;
  632. dbg_gen("free volume %d", vol->vol_id);
  633. ubi->volumes[vol->vol_id] = NULL;
  634. err = ubi_destroy_gluebi(vol);
  635. cdev_del(&vol->cdev);
  636. volume_sysfs_close(vol);
  637. }
  638. #ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
  639. /**
  640. * paranoid_check_volume - check volume information.
  641. * @ubi: UBI device description object
  642. * @vol_id: volume ID
  643. *
  644. * Returns zero if volume is all right and a a negative error code if not.
  645. */
  646. static int paranoid_check_volume(struct ubi_device *ubi, int vol_id)
  647. {
  648. int idx = vol_id2idx(ubi, vol_id);
  649. int reserved_pebs, alignment, data_pad, vol_type, name_len, upd_marker;
  650. const struct ubi_volume *vol;
  651. long long n;
  652. const char *name;
  653. spin_lock(&ubi->volumes_lock);
  654. reserved_pebs = be32_to_cpu(ubi->vtbl[vol_id].reserved_pebs);
  655. vol = ubi->volumes[idx];
  656. if (!vol) {
  657. if (reserved_pebs) {
  658. ubi_err("no volume info, but volume exists");
  659. goto fail;
  660. }
  661. spin_unlock(&ubi->volumes_lock);
  662. return 0;
  663. }
  664. if (vol->reserved_pebs < 0 || vol->alignment < 0 || vol->data_pad < 0 ||
  665. vol->name_len < 0) {
  666. ubi_err("negative values");
  667. goto fail;
  668. }
  669. if (vol->alignment > ubi->leb_size || vol->alignment == 0) {
  670. ubi_err("bad alignment");
  671. goto fail;
  672. }
  673. n = vol->alignment & (ubi->min_io_size - 1);
  674. if (vol->alignment != 1 && n) {
  675. ubi_err("alignment is not multiple of min I/O unit");
  676. goto fail;
  677. }
  678. n = ubi->leb_size % vol->alignment;
  679. if (vol->data_pad != n) {
  680. ubi_err("bad data_pad, has to be %lld", n);
  681. goto fail;
  682. }
  683. if (vol->vol_type != UBI_DYNAMIC_VOLUME &&
  684. vol->vol_type != UBI_STATIC_VOLUME) {
  685. ubi_err("bad vol_type");
  686. goto fail;
  687. }
  688. if (vol->upd_marker && vol->corrupted) {
  689. dbg_err("update marker and corrupted simultaneously");
  690. goto fail;
  691. }
  692. if (vol->reserved_pebs > ubi->good_peb_count) {
  693. ubi_err("too large reserved_pebs");
  694. goto fail;
  695. }
  696. n = ubi->leb_size - vol->data_pad;
  697. if (vol->usable_leb_size != ubi->leb_size - vol->data_pad) {
  698. ubi_err("bad usable_leb_size, has to be %lld", n);
  699. goto fail;
  700. }
  701. if (vol->name_len > UBI_VOL_NAME_MAX) {
  702. ubi_err("too long volume name, max is %d", UBI_VOL_NAME_MAX);
  703. goto fail;
  704. }
  705. if (!vol->name) {
  706. ubi_err("NULL volume name");
  707. goto fail;
  708. }
  709. n = strnlen(vol->name, vol->name_len + 1);
  710. if (n != vol->name_len) {
  711. ubi_err("bad name_len %lld", n);
  712. goto fail;
  713. }
  714. n = (long long)vol->used_ebs * vol->usable_leb_size;
  715. if (vol->vol_type == UBI_DYNAMIC_VOLUME) {
  716. if (vol->corrupted) {
  717. ubi_err("corrupted dynamic volume");
  718. goto fail;
  719. }
  720. if (vol->used_ebs != vol->reserved_pebs) {
  721. ubi_err("bad used_ebs");
  722. goto fail;
  723. }
  724. if (vol->last_eb_bytes != vol->usable_leb_size) {
  725. ubi_err("bad last_eb_bytes");
  726. goto fail;
  727. }
  728. if (vol->used_bytes != n) {
  729. ubi_err("bad used_bytes");
  730. goto fail;
  731. }
  732. } else {
  733. if (vol->used_ebs < 0 || vol->used_ebs > vol->reserved_pebs) {
  734. ubi_err("bad used_ebs");
  735. goto fail;
  736. }
  737. if (vol->last_eb_bytes < 0 ||
  738. vol->last_eb_bytes > vol->usable_leb_size) {
  739. ubi_err("bad last_eb_bytes");
  740. goto fail;
  741. }
  742. if (vol->used_bytes < 0 || vol->used_bytes > n ||
  743. vol->used_bytes < n - vol->usable_leb_size) {
  744. ubi_err("bad used_bytes");
  745. goto fail;
  746. }
  747. }
  748. alignment = be32_to_cpu(ubi->vtbl[vol_id].alignment);
  749. data_pad = be32_to_cpu(ubi->vtbl[vol_id].data_pad);
  750. name_len = be16_to_cpu(ubi->vtbl[vol_id].name_len);
  751. upd_marker = ubi->vtbl[vol_id].upd_marker;
  752. name = &ubi->vtbl[vol_id].name[0];
  753. if (ubi->vtbl[vol_id].vol_type == UBI_VID_DYNAMIC)
  754. vol_type = UBI_DYNAMIC_VOLUME;
  755. else
  756. vol_type = UBI_STATIC_VOLUME;
  757. if (alignment != vol->alignment || data_pad != vol->data_pad ||
  758. upd_marker != vol->upd_marker || vol_type != vol->vol_type ||
  759. name_len != vol->name_len || strncmp(name, vol->name, name_len)) {
  760. ubi_err("volume info is different");
  761. goto fail;
  762. }
  763. spin_unlock(&ubi->volumes_lock);
  764. return 0;
  765. fail:
  766. ubi_err("paranoid check failed for volume %d", vol_id);
  767. if (vol)
  768. ubi_dbg_dump_vol_info(vol);
  769. ubi_dbg_dump_vtbl_record(&ubi->vtbl[vol_id], vol_id);
  770. spin_unlock(&ubi->volumes_lock);
  771. return -EINVAL;
  772. }
  773. /**
  774. * paranoid_check_volumes - check information about all volumes.
  775. * @ubi: UBI device description object
  776. *
  777. * Returns zero if volumes are all right and a a negative error code if not.
  778. */
  779. static int paranoid_check_volumes(struct ubi_device *ubi)
  780. {
  781. int i, err = 0;
  782. for (i = 0; i < ubi->vtbl_slots; i++) {
  783. err = paranoid_check_volume(ubi, i);
  784. if (err)
  785. break;
  786. }
  787. return err;
  788. }
  789. #endif