vmt.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898
  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->device_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("create device %d, volume %d, %llu bytes, type %d, name %s",
  214. ubi->ubi_num, 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. if (paranoid_check_volumes(ubi))
  322. dbg_err("check failed while creating volume %d", vol_id);
  323. return err;
  324. out_sysfs:
  325. /*
  326. * We have registered our device, we should not free the volume
  327. * description object in this function in case of an error - it is
  328. * freed by the release function.
  329. *
  330. * Get device reference to prevent the release function from being
  331. * called just after sysfs has been closed.
  332. */
  333. do_free = 0;
  334. get_device(&vol->dev);
  335. volume_sysfs_close(vol);
  336. out_gluebi:
  337. if (ubi_destroy_gluebi(vol))
  338. dbg_err("cannot destroy gluebi for volume %d:%d",
  339. ubi->ubi_num, vol_id);
  340. out_cdev:
  341. cdev_del(&vol->cdev);
  342. out_mapping:
  343. if (do_free)
  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 (do_free)
  352. kfree(vol);
  353. else
  354. put_device(&vol->dev);
  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. * @no_vtbl: do not change volume table if not zero
  362. *
  363. * This function removes volume described by @desc. The volume has to be opened
  364. * in "exclusive" mode. Returns zero in case of success and a negative error
  365. * code in case of failure. The caller has to have the @ubi->device_mutex
  366. * locked.
  367. */
  368. int ubi_remove_volume(struct ubi_volume_desc *desc, int no_vtbl)
  369. {
  370. struct ubi_volume *vol = desc->vol;
  371. struct ubi_device *ubi = vol->ubi;
  372. int i, err, vol_id = vol->vol_id, reserved_pebs = vol->reserved_pebs;
  373. dbg_gen("remove device %d, volume %d", ubi->ubi_num, vol_id);
  374. ubi_assert(desc->mode == UBI_EXCLUSIVE);
  375. ubi_assert(vol == ubi->volumes[vol_id]);
  376. if (ubi->ro_mode)
  377. return -EROFS;
  378. spin_lock(&ubi->volumes_lock);
  379. if (vol->ref_count > 1) {
  380. /*
  381. * The volume is busy, probably someone is reading one of its
  382. * sysfs files.
  383. */
  384. err = -EBUSY;
  385. goto out_unlock;
  386. }
  387. ubi->volumes[vol_id] = NULL;
  388. spin_unlock(&ubi->volumes_lock);
  389. err = ubi_destroy_gluebi(vol);
  390. if (err)
  391. goto out_err;
  392. if (!no_vtbl) {
  393. err = ubi_change_vtbl_record(ubi, vol_id, NULL);
  394. if (err)
  395. goto out_err;
  396. }
  397. for (i = 0; i < vol->reserved_pebs; i++) {
  398. err = ubi_eba_unmap_leb(ubi, vol, i);
  399. if (err)
  400. goto out_err;
  401. }
  402. cdev_del(&vol->cdev);
  403. volume_sysfs_close(vol);
  404. spin_lock(&ubi->volumes_lock);
  405. ubi->rsvd_pebs -= reserved_pebs;
  406. ubi->avail_pebs += reserved_pebs;
  407. i = ubi->beb_rsvd_level - ubi->beb_rsvd_pebs;
  408. if (i > 0) {
  409. i = ubi->avail_pebs >= i ? i : ubi->avail_pebs;
  410. ubi->avail_pebs -= i;
  411. ubi->rsvd_pebs += i;
  412. ubi->beb_rsvd_pebs += i;
  413. if (i > 0)
  414. ubi_msg("reserve more %d PEBs", i);
  415. }
  416. ubi->vol_count -= 1;
  417. spin_unlock(&ubi->volumes_lock);
  418. if (!no_vtbl && paranoid_check_volumes(ubi))
  419. dbg_err("check failed while removing volume %d", vol_id);
  420. return err;
  421. out_err:
  422. ubi_err("cannot remove volume %d, error %d", vol_id, err);
  423. spin_lock(&ubi->volumes_lock);
  424. ubi->volumes[vol_id] = vol;
  425. out_unlock:
  426. spin_unlock(&ubi->volumes_lock);
  427. return err;
  428. }
  429. /**
  430. * ubi_resize_volume - re-size volume.
  431. * @desc: volume descriptor
  432. * @reserved_pebs: new size in physical eraseblocks
  433. *
  434. * This function re-sizes the volume and returns zero in case of success, and a
  435. * negative error code in case of failure. The caller has to have the
  436. * @ubi->device_mutex locked.
  437. */
  438. int ubi_resize_volume(struct ubi_volume_desc *desc, int reserved_pebs)
  439. {
  440. int i, err, pebs, *new_mapping;
  441. struct ubi_volume *vol = desc->vol;
  442. struct ubi_device *ubi = vol->ubi;
  443. struct ubi_vtbl_record vtbl_rec;
  444. int vol_id = vol->vol_id;
  445. if (ubi->ro_mode)
  446. return -EROFS;
  447. dbg_gen("re-size device %d, volume %d to from %d to %d PEBs",
  448. ubi->ubi_num, vol_id, vol->reserved_pebs, reserved_pebs);
  449. if (vol->vol_type == UBI_STATIC_VOLUME &&
  450. reserved_pebs < vol->used_ebs) {
  451. dbg_err("too small size %d, %d LEBs contain data",
  452. reserved_pebs, vol->used_ebs);
  453. return -EINVAL;
  454. }
  455. /* If the size is the same, we have nothing to do */
  456. if (reserved_pebs == vol->reserved_pebs)
  457. return 0;
  458. new_mapping = kmalloc(reserved_pebs * sizeof(int), GFP_KERNEL);
  459. if (!new_mapping)
  460. return -ENOMEM;
  461. for (i = 0; i < reserved_pebs; i++)
  462. new_mapping[i] = UBI_LEB_UNMAPPED;
  463. spin_lock(&ubi->volumes_lock);
  464. if (vol->ref_count > 1) {
  465. spin_unlock(&ubi->volumes_lock);
  466. err = -EBUSY;
  467. goto out_free;
  468. }
  469. spin_unlock(&ubi->volumes_lock);
  470. /* Reserve physical eraseblocks */
  471. pebs = reserved_pebs - vol->reserved_pebs;
  472. if (pebs > 0) {
  473. spin_lock(&ubi->volumes_lock);
  474. if (pebs > ubi->avail_pebs) {
  475. dbg_err("not enough PEBs: requested %d, available %d",
  476. pebs, ubi->avail_pebs);
  477. spin_unlock(&ubi->volumes_lock);
  478. err = -ENOSPC;
  479. goto out_free;
  480. }
  481. ubi->avail_pebs -= pebs;
  482. ubi->rsvd_pebs += pebs;
  483. for (i = 0; i < vol->reserved_pebs; i++)
  484. new_mapping[i] = vol->eba_tbl[i];
  485. kfree(vol->eba_tbl);
  486. vol->eba_tbl = new_mapping;
  487. spin_unlock(&ubi->volumes_lock);
  488. }
  489. /* Change volume table record */
  490. memcpy(&vtbl_rec, &ubi->vtbl[vol_id], sizeof(struct ubi_vtbl_record));
  491. vtbl_rec.reserved_pebs = cpu_to_be32(reserved_pebs);
  492. err = ubi_change_vtbl_record(ubi, vol_id, &vtbl_rec);
  493. if (err)
  494. goto out_acc;
  495. if (pebs < 0) {
  496. for (i = 0; i < -pebs; i++) {
  497. err = ubi_eba_unmap_leb(ubi, vol, reserved_pebs + i);
  498. if (err)
  499. goto out_acc;
  500. }
  501. spin_lock(&ubi->volumes_lock);
  502. ubi->rsvd_pebs += pebs;
  503. ubi->avail_pebs -= pebs;
  504. pebs = ubi->beb_rsvd_level - ubi->beb_rsvd_pebs;
  505. if (pebs > 0) {
  506. pebs = ubi->avail_pebs >= pebs ? pebs : ubi->avail_pebs;
  507. ubi->avail_pebs -= pebs;
  508. ubi->rsvd_pebs += pebs;
  509. ubi->beb_rsvd_pebs += pebs;
  510. if (pebs > 0)
  511. ubi_msg("reserve more %d PEBs", pebs);
  512. }
  513. for (i = 0; i < reserved_pebs; i++)
  514. new_mapping[i] = vol->eba_tbl[i];
  515. kfree(vol->eba_tbl);
  516. vol->eba_tbl = new_mapping;
  517. spin_unlock(&ubi->volumes_lock);
  518. }
  519. vol->reserved_pebs = reserved_pebs;
  520. if (vol->vol_type == UBI_DYNAMIC_VOLUME) {
  521. vol->used_ebs = reserved_pebs;
  522. vol->last_eb_bytes = vol->usable_leb_size;
  523. vol->used_bytes =
  524. (long long)vol->used_ebs * vol->usable_leb_size;
  525. }
  526. if (paranoid_check_volumes(ubi))
  527. dbg_err("check failed while re-sizing volume %d", vol_id);
  528. return err;
  529. out_acc:
  530. if (pebs > 0) {
  531. spin_lock(&ubi->volumes_lock);
  532. ubi->rsvd_pebs -= pebs;
  533. ubi->avail_pebs += pebs;
  534. spin_unlock(&ubi->volumes_lock);
  535. }
  536. out_free:
  537. kfree(new_mapping);
  538. return err;
  539. }
  540. /**
  541. * ubi_rename_volumes - re-name UBI volumes.
  542. * @ubi: UBI device description object
  543. * @rename_list: list of &struct ubi_rename_entry objects
  544. *
  545. * This function re-names or removes volumes specified in the re-name list.
  546. * Returns zero in case of success and a negative error code in case of
  547. * failure.
  548. */
  549. int ubi_rename_volumes(struct ubi_device *ubi, struct list_head *rename_list)
  550. {
  551. int err;
  552. struct ubi_rename_entry *re;
  553. err = ubi_vtbl_rename_volumes(ubi, rename_list);
  554. if (err)
  555. return err;
  556. list_for_each_entry(re, rename_list, list) {
  557. if (re->remove) {
  558. err = ubi_remove_volume(re->desc, 1);
  559. if (err)
  560. break;
  561. } else {
  562. struct ubi_volume *vol = re->desc->vol;
  563. spin_lock(&ubi->volumes_lock);
  564. vol->name_len = re->new_name_len;
  565. memcpy(vol->name, re->new_name, re->new_name_len + 1);
  566. spin_unlock(&ubi->volumes_lock);
  567. }
  568. }
  569. if (!err && paranoid_check_volumes(ubi))
  570. ;
  571. return err;
  572. }
  573. /**
  574. * ubi_add_volume - add volume.
  575. * @ubi: UBI device description object
  576. * @vol: volume description object
  577. *
  578. * This function adds an existing volume and initializes all its data
  579. * structures. Returns zero in case of success and a negative error code in
  580. * case of failure.
  581. */
  582. int ubi_add_volume(struct ubi_device *ubi, struct ubi_volume *vol)
  583. {
  584. int err, vol_id = vol->vol_id;
  585. dev_t dev;
  586. dbg_gen("add volume %d", vol_id);
  587. /* Register character device for the volume */
  588. cdev_init(&vol->cdev, &ubi_vol_cdev_operations);
  589. vol->cdev.owner = THIS_MODULE;
  590. dev = MKDEV(MAJOR(ubi->cdev.dev), vol->vol_id + 1);
  591. err = cdev_add(&vol->cdev, dev, 1);
  592. if (err) {
  593. ubi_err("cannot add character device for volume %d, error %d",
  594. vol_id, err);
  595. return err;
  596. }
  597. err = ubi_create_gluebi(ubi, vol);
  598. if (err)
  599. goto out_cdev;
  600. vol->dev.release = vol_release;
  601. vol->dev.parent = &ubi->dev;
  602. vol->dev.devt = dev;
  603. vol->dev.class = ubi_class;
  604. dev_set_name(&vol->dev, "%s_%d", ubi->ubi_name, vol->vol_id);
  605. err = device_register(&vol->dev);
  606. if (err)
  607. goto out_gluebi;
  608. err = volume_sysfs_init(ubi, vol);
  609. if (err) {
  610. cdev_del(&vol->cdev);
  611. err = ubi_destroy_gluebi(vol);
  612. volume_sysfs_close(vol);
  613. return err;
  614. }
  615. if (paranoid_check_volumes(ubi))
  616. dbg_err("check failed while adding volume %d", vol_id);
  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. dump_stack();
  774. spin_unlock(&ubi->volumes_lock);
  775. return -EINVAL;
  776. }
  777. /**
  778. * paranoid_check_volumes - check information about all volumes.
  779. * @ubi: UBI device description object
  780. *
  781. * Returns zero if volumes are all right and a a negative error code if not.
  782. */
  783. static int paranoid_check_volumes(struct ubi_device *ubi)
  784. {
  785. int i, err = 0;
  786. for (i = 0; i < ubi->vtbl_slots; i++) {
  787. err = paranoid_check_volume(ubi, i);
  788. if (err)
  789. break;
  790. }
  791. return err;
  792. }
  793. #endif