vmt.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887
  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 <linux/slab.h>
  27. #include <linux/export.h>
  28. #include "ubi.h"
  29. #ifdef CONFIG_MTD_UBI_DEBUG
  30. static int paranoid_check_volumes(struct ubi_device *ubi);
  31. #else
  32. #define paranoid_check_volumes(ubi) 0
  33. #endif
  34. static ssize_t vol_attribute_show(struct device *dev,
  35. struct device_attribute *attr, char *buf);
  36. /* Device attributes corresponding to files in '/<sysfs>/class/ubi/ubiX_Y' */
  37. static struct device_attribute attr_vol_reserved_ebs =
  38. __ATTR(reserved_ebs, S_IRUGO, vol_attribute_show, NULL);
  39. static struct device_attribute attr_vol_type =
  40. __ATTR(type, S_IRUGO, vol_attribute_show, NULL);
  41. static struct device_attribute attr_vol_name =
  42. __ATTR(name, S_IRUGO, vol_attribute_show, NULL);
  43. static struct device_attribute attr_vol_corrupted =
  44. __ATTR(corrupted, S_IRUGO, vol_attribute_show, NULL);
  45. static struct device_attribute attr_vol_alignment =
  46. __ATTR(alignment, S_IRUGO, vol_attribute_show, NULL);
  47. static struct device_attribute attr_vol_usable_eb_size =
  48. __ATTR(usable_eb_size, S_IRUGO, vol_attribute_show, NULL);
  49. static struct device_attribute attr_vol_data_bytes =
  50. __ATTR(data_bytes, S_IRUGO, vol_attribute_show, NULL);
  51. static struct device_attribute attr_vol_upd_marker =
  52. __ATTR(upd_marker, S_IRUGO, vol_attribute_show, NULL);
  53. /*
  54. * "Show" method for files in '/<sysfs>/class/ubi/ubiX_Y/'.
  55. *
  56. * Consider a situation:
  57. * A. process 1 opens a sysfs file related to volume Y, say
  58. * /<sysfs>/class/ubi/ubiX_Y/reserved_ebs;
  59. * B. process 2 removes volume Y;
  60. * C. process 1 starts reading the /<sysfs>/class/ubi/ubiX_Y/reserved_ebs file;
  61. *
  62. * In this situation, this function will return %-ENODEV because it will find
  63. * out that the volume was removed from the @ubi->volumes array.
  64. */
  65. static ssize_t vol_attribute_show(struct device *dev,
  66. struct device_attribute *attr, char *buf)
  67. {
  68. int ret;
  69. struct ubi_volume *vol = container_of(dev, struct ubi_volume, dev);
  70. struct ubi_device *ubi;
  71. ubi = ubi_get_device(vol->ubi->ubi_num);
  72. if (!ubi)
  73. return -ENODEV;
  74. spin_lock(&ubi->volumes_lock);
  75. if (!ubi->volumes[vol->vol_id]) {
  76. spin_unlock(&ubi->volumes_lock);
  77. ubi_put_device(ubi);
  78. return -ENODEV;
  79. }
  80. /* Take a reference to prevent volume removal */
  81. vol->ref_count += 1;
  82. spin_unlock(&ubi->volumes_lock);
  83. if (attr == &attr_vol_reserved_ebs)
  84. ret = sprintf(buf, "%d\n", vol->reserved_pebs);
  85. else if (attr == &attr_vol_type) {
  86. const char *tp;
  87. if (vol->vol_type == UBI_DYNAMIC_VOLUME)
  88. tp = "dynamic";
  89. else
  90. tp = "static";
  91. ret = sprintf(buf, "%s\n", tp);
  92. } else if (attr == &attr_vol_name)
  93. ret = sprintf(buf, "%s\n", vol->name);
  94. else if (attr == &attr_vol_corrupted)
  95. ret = sprintf(buf, "%d\n", vol->corrupted);
  96. else if (attr == &attr_vol_alignment)
  97. ret = sprintf(buf, "%d\n", vol->alignment);
  98. else if (attr == &attr_vol_usable_eb_size)
  99. ret = sprintf(buf, "%d\n", vol->usable_leb_size);
  100. else if (attr == &attr_vol_data_bytes)
  101. ret = sprintf(buf, "%lld\n", vol->used_bytes);
  102. else if (attr == &attr_vol_upd_marker)
  103. ret = sprintf(buf, "%d\n", vol->upd_marker);
  104. else
  105. /* This must be a bug */
  106. ret = -EINVAL;
  107. /* We've done the operation, drop volume and UBI device references */
  108. spin_lock(&ubi->volumes_lock);
  109. vol->ref_count -= 1;
  110. ubi_assert(vol->ref_count >= 0);
  111. spin_unlock(&ubi->volumes_lock);
  112. ubi_put_device(ubi);
  113. return ret;
  114. }
  115. /* Release method for volume devices */
  116. static void vol_release(struct device *dev)
  117. {
  118. struct ubi_volume *vol = container_of(dev, struct ubi_volume, dev);
  119. kfree(vol->eba_tbl);
  120. kfree(vol);
  121. }
  122. /**
  123. * volume_sysfs_init - initialize sysfs for new volume.
  124. * @ubi: UBI device description object
  125. * @vol: volume description object
  126. *
  127. * This function returns zero in case of success and a negative error code in
  128. * case of failure.
  129. *
  130. * Note, this function does not free allocated resources in case of failure -
  131. * the caller does it. This is because this would cause release() here and the
  132. * caller would oops.
  133. */
  134. static int volume_sysfs_init(struct ubi_device *ubi, struct ubi_volume *vol)
  135. {
  136. int err;
  137. err = device_create_file(&vol->dev, &attr_vol_reserved_ebs);
  138. if (err)
  139. return err;
  140. err = device_create_file(&vol->dev, &attr_vol_type);
  141. if (err)
  142. return err;
  143. err = device_create_file(&vol->dev, &attr_vol_name);
  144. if (err)
  145. return err;
  146. err = device_create_file(&vol->dev, &attr_vol_corrupted);
  147. if (err)
  148. return err;
  149. err = device_create_file(&vol->dev, &attr_vol_alignment);
  150. if (err)
  151. return err;
  152. err = device_create_file(&vol->dev, &attr_vol_usable_eb_size);
  153. if (err)
  154. return err;
  155. err = device_create_file(&vol->dev, &attr_vol_data_bytes);
  156. if (err)
  157. return err;
  158. err = device_create_file(&vol->dev, &attr_vol_upd_marker);
  159. return err;
  160. }
  161. /**
  162. * volume_sysfs_close - close sysfs for a volume.
  163. * @vol: volume description object
  164. */
  165. static void volume_sysfs_close(struct ubi_volume *vol)
  166. {
  167. device_remove_file(&vol->dev, &attr_vol_upd_marker);
  168. device_remove_file(&vol->dev, &attr_vol_data_bytes);
  169. device_remove_file(&vol->dev, &attr_vol_usable_eb_size);
  170. device_remove_file(&vol->dev, &attr_vol_alignment);
  171. device_remove_file(&vol->dev, &attr_vol_corrupted);
  172. device_remove_file(&vol->dev, &attr_vol_name);
  173. device_remove_file(&vol->dev, &attr_vol_type);
  174. device_remove_file(&vol->dev, &attr_vol_reserved_ebs);
  175. device_unregister(&vol->dev);
  176. }
  177. /**
  178. * ubi_create_volume - create volume.
  179. * @ubi: UBI device description object
  180. * @req: volume creation request
  181. *
  182. * This function creates volume described by @req. If @req->vol_id id
  183. * %UBI_VOL_NUM_AUTO, this function automatically assign ID to the new volume
  184. * and saves it in @req->vol_id. Returns zero in case of success and a negative
  185. * error code in case of failure. Note, the caller has to have the
  186. * @ubi->device_mutex locked.
  187. */
  188. int ubi_create_volume(struct ubi_device *ubi, struct ubi_mkvol_req *req)
  189. {
  190. int i, err, vol_id = req->vol_id, do_free = 1;
  191. struct ubi_volume *vol;
  192. struct ubi_vtbl_record vtbl_rec;
  193. dev_t dev;
  194. if (ubi->ro_mode)
  195. return -EROFS;
  196. vol = kzalloc(sizeof(struct ubi_volume), GFP_KERNEL);
  197. if (!vol)
  198. return -ENOMEM;
  199. spin_lock(&ubi->volumes_lock);
  200. if (vol_id == UBI_VOL_NUM_AUTO) {
  201. /* Find unused volume ID */
  202. dbg_gen("search for vacant volume ID");
  203. for (i = 0; i < ubi->vtbl_slots; i++)
  204. if (!ubi->volumes[i]) {
  205. vol_id = i;
  206. break;
  207. }
  208. if (vol_id == UBI_VOL_NUM_AUTO) {
  209. dbg_err("out of volume IDs");
  210. err = -ENFILE;
  211. goto out_unlock;
  212. }
  213. req->vol_id = vol_id;
  214. }
  215. dbg_gen("create device %d, volume %d, %llu bytes, type %d, name %s",
  216. ubi->ubi_num, vol_id, (unsigned long long)req->bytes,
  217. (int)req->vol_type, req->name);
  218. /* Ensure that this volume does not exist */
  219. err = -EEXIST;
  220. if (ubi->volumes[vol_id]) {
  221. dbg_err("volume %d already exists", vol_id);
  222. goto out_unlock;
  223. }
  224. /* Ensure that the name is unique */
  225. for (i = 0; i < ubi->vtbl_slots; i++)
  226. if (ubi->volumes[i] &&
  227. ubi->volumes[i]->name_len == req->name_len &&
  228. !strcmp(ubi->volumes[i]->name, req->name)) {
  229. dbg_err("volume \"%s\" exists (ID %d)", req->name, i);
  230. goto out_unlock;
  231. }
  232. /* Calculate how many eraseblocks are requested */
  233. vol->usable_leb_size = ubi->leb_size - ubi->leb_size % req->alignment;
  234. vol->reserved_pebs += div_u64(req->bytes + vol->usable_leb_size - 1,
  235. vol->usable_leb_size);
  236. /* Reserve physical eraseblocks */
  237. if (vol->reserved_pebs > ubi->avail_pebs) {
  238. dbg_err("not enough PEBs, only %d available", ubi->avail_pebs);
  239. if (ubi->corr_peb_count)
  240. dbg_err("%d PEBs are corrupted and not used",
  241. ubi->corr_peb_count);
  242. err = -ENOSPC;
  243. goto out_unlock;
  244. }
  245. ubi->avail_pebs -= vol->reserved_pebs;
  246. ubi->rsvd_pebs += vol->reserved_pebs;
  247. spin_unlock(&ubi->volumes_lock);
  248. vol->vol_id = vol_id;
  249. vol->alignment = req->alignment;
  250. vol->data_pad = ubi->leb_size % vol->alignment;
  251. vol->vol_type = req->vol_type;
  252. vol->name_len = req->name_len;
  253. memcpy(vol->name, req->name, vol->name_len);
  254. vol->ubi = ubi;
  255. /*
  256. * Finish all pending erases because there may be some LEBs belonging
  257. * to the same volume ID.
  258. */
  259. err = ubi_wl_flush(ubi);
  260. if (err)
  261. goto out_acc;
  262. vol->eba_tbl = kmalloc(vol->reserved_pebs * sizeof(int), GFP_KERNEL);
  263. if (!vol->eba_tbl) {
  264. err = -ENOMEM;
  265. goto out_acc;
  266. }
  267. for (i = 0; i < vol->reserved_pebs; i++)
  268. vol->eba_tbl[i] = UBI_LEB_UNMAPPED;
  269. if (vol->vol_type == UBI_DYNAMIC_VOLUME) {
  270. vol->used_ebs = vol->reserved_pebs;
  271. vol->last_eb_bytes = vol->usable_leb_size;
  272. vol->used_bytes =
  273. (long long)vol->used_ebs * vol->usable_leb_size;
  274. } else {
  275. vol->used_ebs = div_u64_rem(vol->used_bytes,
  276. vol->usable_leb_size,
  277. &vol->last_eb_bytes);
  278. if (vol->last_eb_bytes != 0)
  279. vol->used_ebs += 1;
  280. else
  281. vol->last_eb_bytes = vol->usable_leb_size;
  282. }
  283. /* Register character device for the volume */
  284. cdev_init(&vol->cdev, &ubi_vol_cdev_operations);
  285. vol->cdev.owner = THIS_MODULE;
  286. dev = MKDEV(MAJOR(ubi->cdev.dev), vol_id + 1);
  287. err = cdev_add(&vol->cdev, dev, 1);
  288. if (err) {
  289. ubi_err("cannot add character device");
  290. goto out_mapping;
  291. }
  292. vol->dev.release = vol_release;
  293. vol->dev.parent = &ubi->dev;
  294. vol->dev.devt = dev;
  295. vol->dev.class = ubi_class;
  296. dev_set_name(&vol->dev, "%s_%d", ubi->ubi_name, vol->vol_id);
  297. err = device_register(&vol->dev);
  298. if (err) {
  299. ubi_err("cannot register device");
  300. goto out_cdev;
  301. }
  302. err = volume_sysfs_init(ubi, vol);
  303. if (err)
  304. goto out_sysfs;
  305. /* Fill volume table record */
  306. memset(&vtbl_rec, 0, sizeof(struct ubi_vtbl_record));
  307. vtbl_rec.reserved_pebs = cpu_to_be32(vol->reserved_pebs);
  308. vtbl_rec.alignment = cpu_to_be32(vol->alignment);
  309. vtbl_rec.data_pad = cpu_to_be32(vol->data_pad);
  310. vtbl_rec.name_len = cpu_to_be16(vol->name_len);
  311. if (vol->vol_type == UBI_DYNAMIC_VOLUME)
  312. vtbl_rec.vol_type = UBI_VID_DYNAMIC;
  313. else
  314. vtbl_rec.vol_type = UBI_VID_STATIC;
  315. memcpy(vtbl_rec.name, vol->name, vol->name_len);
  316. err = ubi_change_vtbl_record(ubi, vol_id, &vtbl_rec);
  317. if (err)
  318. goto out_sysfs;
  319. spin_lock(&ubi->volumes_lock);
  320. ubi->volumes[vol_id] = vol;
  321. ubi->vol_count += 1;
  322. spin_unlock(&ubi->volumes_lock);
  323. ubi_volume_notify(ubi, vol, UBI_VOLUME_ADDED);
  324. if (paranoid_check_volumes(ubi))
  325. dbg_err("check failed while creating volume %d", vol_id);
  326. return err;
  327. out_sysfs:
  328. /*
  329. * We have registered our device, we should not free the volume
  330. * description object in this function in case of an error - it is
  331. * freed by the release function.
  332. *
  333. * Get device reference to prevent the release function from being
  334. * called just after sysfs has been closed.
  335. */
  336. do_free = 0;
  337. get_device(&vol->dev);
  338. volume_sysfs_close(vol);
  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->device_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 device %d, volume %d", ubi->ubi_num, 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. if (!no_vtbl) {
  389. err = ubi_change_vtbl_record(ubi, vol_id, NULL);
  390. if (err)
  391. goto out_err;
  392. }
  393. for (i = 0; i < vol->reserved_pebs; i++) {
  394. err = ubi_eba_unmap_leb(ubi, vol, i);
  395. if (err)
  396. goto out_err;
  397. }
  398. cdev_del(&vol->cdev);
  399. volume_sysfs_close(vol);
  400. spin_lock(&ubi->volumes_lock);
  401. ubi->rsvd_pebs -= reserved_pebs;
  402. ubi->avail_pebs += reserved_pebs;
  403. i = ubi->beb_rsvd_level - ubi->beb_rsvd_pebs;
  404. if (i > 0) {
  405. i = ubi->avail_pebs >= i ? i : ubi->avail_pebs;
  406. ubi->avail_pebs -= i;
  407. ubi->rsvd_pebs += i;
  408. ubi->beb_rsvd_pebs += i;
  409. if (i > 0)
  410. ubi_msg("reserve more %d PEBs", i);
  411. }
  412. ubi->vol_count -= 1;
  413. spin_unlock(&ubi->volumes_lock);
  414. ubi_volume_notify(ubi, vol, UBI_VOLUME_REMOVED);
  415. if (!no_vtbl && paranoid_check_volumes(ubi))
  416. dbg_err("check failed while removing volume %d", vol_id);
  417. return err;
  418. out_err:
  419. ubi_err("cannot remove volume %d, error %d", vol_id, err);
  420. spin_lock(&ubi->volumes_lock);
  421. ubi->volumes[vol_id] = vol;
  422. out_unlock:
  423. spin_unlock(&ubi->volumes_lock);
  424. return err;
  425. }
  426. /**
  427. * ubi_resize_volume - re-size volume.
  428. * @desc: volume descriptor
  429. * @reserved_pebs: new size in physical eraseblocks
  430. *
  431. * This function re-sizes the volume and returns zero in case of success, and a
  432. * negative error code in case of failure. The caller has to have the
  433. * @ubi->device_mutex locked.
  434. */
  435. int ubi_resize_volume(struct ubi_volume_desc *desc, int reserved_pebs)
  436. {
  437. int i, err, pebs, *new_mapping;
  438. struct ubi_volume *vol = desc->vol;
  439. struct ubi_device *ubi = vol->ubi;
  440. struct ubi_vtbl_record vtbl_rec;
  441. int vol_id = vol->vol_id;
  442. if (ubi->ro_mode)
  443. return -EROFS;
  444. dbg_gen("re-size device %d, volume %d to from %d to %d PEBs",
  445. ubi->ubi_num, vol_id, vol->reserved_pebs, reserved_pebs);
  446. if (vol->vol_type == UBI_STATIC_VOLUME &&
  447. reserved_pebs < vol->used_ebs) {
  448. dbg_err("too small size %d, %d LEBs contain data",
  449. reserved_pebs, vol->used_ebs);
  450. return -EINVAL;
  451. }
  452. /* If the size is the same, we have nothing to do */
  453. if (reserved_pebs == vol->reserved_pebs)
  454. return 0;
  455. new_mapping = kmalloc(reserved_pebs * sizeof(int), GFP_KERNEL);
  456. if (!new_mapping)
  457. return -ENOMEM;
  458. for (i = 0; i < reserved_pebs; i++)
  459. new_mapping[i] = UBI_LEB_UNMAPPED;
  460. spin_lock(&ubi->volumes_lock);
  461. if (vol->ref_count > 1) {
  462. spin_unlock(&ubi->volumes_lock);
  463. err = -EBUSY;
  464. goto out_free;
  465. }
  466. spin_unlock(&ubi->volumes_lock);
  467. /* Reserve physical eraseblocks */
  468. pebs = reserved_pebs - vol->reserved_pebs;
  469. if (pebs > 0) {
  470. spin_lock(&ubi->volumes_lock);
  471. if (pebs > ubi->avail_pebs) {
  472. dbg_err("not enough PEBs: requested %d, available %d",
  473. pebs, ubi->avail_pebs);
  474. if (ubi->corr_peb_count)
  475. dbg_err("%d PEBs are corrupted and not used",
  476. ubi->corr_peb_count);
  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. ubi_volume_notify(ubi, vol, UBI_VOLUME_RESIZED);
  527. if (paranoid_check_volumes(ubi))
  528. dbg_err("check failed while re-sizing volume %d", vol_id);
  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. ubi_volume_notify(ubi, vol, UBI_VOLUME_RENAMED);
  569. }
  570. }
  571. if (!err && paranoid_check_volumes(ubi))
  572. ;
  573. return err;
  574. }
  575. /**
  576. * ubi_add_volume - add volume.
  577. * @ubi: UBI device description object
  578. * @vol: volume description object
  579. *
  580. * This function adds an existing volume and initializes all its data
  581. * structures. Returns zero in case of success and a negative error code in
  582. * case of failure.
  583. */
  584. int ubi_add_volume(struct ubi_device *ubi, struct ubi_volume *vol)
  585. {
  586. int err, vol_id = vol->vol_id;
  587. dev_t dev;
  588. dbg_gen("add volume %d", vol_id);
  589. /* Register character device for the volume */
  590. cdev_init(&vol->cdev, &ubi_vol_cdev_operations);
  591. vol->cdev.owner = THIS_MODULE;
  592. dev = MKDEV(MAJOR(ubi->cdev.dev), vol->vol_id + 1);
  593. err = cdev_add(&vol->cdev, dev, 1);
  594. if (err) {
  595. ubi_err("cannot add character device for volume %d, error %d",
  596. vol_id, err);
  597. return err;
  598. }
  599. vol->dev.release = vol_release;
  600. vol->dev.parent = &ubi->dev;
  601. vol->dev.devt = dev;
  602. vol->dev.class = ubi_class;
  603. dev_set_name(&vol->dev, "%s_%d", ubi->ubi_name, vol->vol_id);
  604. err = device_register(&vol->dev);
  605. if (err)
  606. goto out_cdev;
  607. err = volume_sysfs_init(ubi, vol);
  608. if (err) {
  609. cdev_del(&vol->cdev);
  610. volume_sysfs_close(vol);
  611. return err;
  612. }
  613. if (paranoid_check_volumes(ubi))
  614. dbg_err("check failed while adding volume %d", vol_id);
  615. return err;
  616. out_cdev:
  617. cdev_del(&vol->cdev);
  618. return err;
  619. }
  620. /**
  621. * ubi_free_volume - free volume.
  622. * @ubi: UBI device description object
  623. * @vol: volume description object
  624. *
  625. * This function frees all resources for volume @vol but does not remove it.
  626. * Used only when the UBI device is detached.
  627. */
  628. void ubi_free_volume(struct ubi_device *ubi, struct ubi_volume *vol)
  629. {
  630. dbg_gen("free volume %d", vol->vol_id);
  631. ubi->volumes[vol->vol_id] = NULL;
  632. cdev_del(&vol->cdev);
  633. volume_sysfs_close(vol);
  634. }
  635. #ifdef CONFIG_MTD_UBI_DEBUG
  636. /**
  637. * paranoid_check_volume - check volume information.
  638. * @ubi: UBI device description object
  639. * @vol_id: volume ID
  640. *
  641. * Returns zero if volume is all right and a a negative error code if not.
  642. */
  643. static int paranoid_check_volume(struct ubi_device *ubi, int vol_id)
  644. {
  645. int idx = vol_id2idx(ubi, vol_id);
  646. int reserved_pebs, alignment, data_pad, vol_type, name_len, upd_marker;
  647. const struct ubi_volume *vol;
  648. long long n;
  649. const char *name;
  650. spin_lock(&ubi->volumes_lock);
  651. reserved_pebs = be32_to_cpu(ubi->vtbl[vol_id].reserved_pebs);
  652. vol = ubi->volumes[idx];
  653. if (!vol) {
  654. if (reserved_pebs) {
  655. ubi_err("no volume info, but volume exists");
  656. goto fail;
  657. }
  658. spin_unlock(&ubi->volumes_lock);
  659. return 0;
  660. }
  661. if (vol->reserved_pebs < 0 || vol->alignment < 0 || vol->data_pad < 0 ||
  662. vol->name_len < 0) {
  663. ubi_err("negative values");
  664. goto fail;
  665. }
  666. if (vol->alignment > ubi->leb_size || vol->alignment == 0) {
  667. ubi_err("bad alignment");
  668. goto fail;
  669. }
  670. n = vol->alignment & (ubi->min_io_size - 1);
  671. if (vol->alignment != 1 && n) {
  672. ubi_err("alignment is not multiple of min I/O unit");
  673. goto fail;
  674. }
  675. n = ubi->leb_size % vol->alignment;
  676. if (vol->data_pad != n) {
  677. ubi_err("bad data_pad, has to be %lld", n);
  678. goto fail;
  679. }
  680. if (vol->vol_type != UBI_DYNAMIC_VOLUME &&
  681. vol->vol_type != UBI_STATIC_VOLUME) {
  682. ubi_err("bad vol_type");
  683. goto fail;
  684. }
  685. if (vol->upd_marker && vol->corrupted) {
  686. dbg_err("update marker and corrupted simultaneously");
  687. goto fail;
  688. }
  689. if (vol->reserved_pebs > ubi->good_peb_count) {
  690. ubi_err("too large reserved_pebs");
  691. goto fail;
  692. }
  693. n = ubi->leb_size - vol->data_pad;
  694. if (vol->usable_leb_size != ubi->leb_size - vol->data_pad) {
  695. ubi_err("bad usable_leb_size, has to be %lld", n);
  696. goto fail;
  697. }
  698. if (vol->name_len > UBI_VOL_NAME_MAX) {
  699. ubi_err("too long volume name, max is %d", UBI_VOL_NAME_MAX);
  700. goto fail;
  701. }
  702. n = strnlen(vol->name, vol->name_len + 1);
  703. if (n != vol->name_len) {
  704. ubi_err("bad name_len %lld", n);
  705. goto fail;
  706. }
  707. n = (long long)vol->used_ebs * vol->usable_leb_size;
  708. if (vol->vol_type == UBI_DYNAMIC_VOLUME) {
  709. if (vol->corrupted) {
  710. ubi_err("corrupted dynamic volume");
  711. goto fail;
  712. }
  713. if (vol->used_ebs != vol->reserved_pebs) {
  714. ubi_err("bad used_ebs");
  715. goto fail;
  716. }
  717. if (vol->last_eb_bytes != vol->usable_leb_size) {
  718. ubi_err("bad last_eb_bytes");
  719. goto fail;
  720. }
  721. if (vol->used_bytes != n) {
  722. ubi_err("bad used_bytes");
  723. goto fail;
  724. }
  725. } else {
  726. if (vol->used_ebs < 0 || vol->used_ebs > vol->reserved_pebs) {
  727. ubi_err("bad used_ebs");
  728. goto fail;
  729. }
  730. if (vol->last_eb_bytes < 0 ||
  731. vol->last_eb_bytes > vol->usable_leb_size) {
  732. ubi_err("bad last_eb_bytes");
  733. goto fail;
  734. }
  735. if (vol->used_bytes < 0 || vol->used_bytes > n ||
  736. vol->used_bytes < n - vol->usable_leb_size) {
  737. ubi_err("bad used_bytes");
  738. goto fail;
  739. }
  740. }
  741. alignment = be32_to_cpu(ubi->vtbl[vol_id].alignment);
  742. data_pad = be32_to_cpu(ubi->vtbl[vol_id].data_pad);
  743. name_len = be16_to_cpu(ubi->vtbl[vol_id].name_len);
  744. upd_marker = ubi->vtbl[vol_id].upd_marker;
  745. name = &ubi->vtbl[vol_id].name[0];
  746. if (ubi->vtbl[vol_id].vol_type == UBI_VID_DYNAMIC)
  747. vol_type = UBI_DYNAMIC_VOLUME;
  748. else
  749. vol_type = UBI_STATIC_VOLUME;
  750. if (alignment != vol->alignment || data_pad != vol->data_pad ||
  751. upd_marker != vol->upd_marker || vol_type != vol->vol_type ||
  752. name_len != vol->name_len || strncmp(name, vol->name, name_len)) {
  753. ubi_err("volume info is different");
  754. goto fail;
  755. }
  756. spin_unlock(&ubi->volumes_lock);
  757. return 0;
  758. fail:
  759. ubi_err("paranoid check failed for volume %d", vol_id);
  760. if (vol)
  761. ubi_dbg_dump_vol_info(vol);
  762. ubi_dbg_dump_vtbl_record(&ubi->vtbl[vol_id], vol_id);
  763. dump_stack();
  764. spin_unlock(&ubi->volumes_lock);
  765. return -EINVAL;
  766. }
  767. /**
  768. * paranoid_check_volumes - check information about all volumes.
  769. * @ubi: UBI device description object
  770. *
  771. * Returns zero if volumes are all right and a a negative error code if not.
  772. */
  773. static int paranoid_check_volumes(struct ubi_device *ubi)
  774. {
  775. int i, err = 0;
  776. if (!ubi->dbg->chk_gen)
  777. return 0;
  778. for (i = 0; i < ubi->vtbl_slots; i++) {
  779. err = paranoid_check_volume(ubi, i);
  780. if (err)
  781. break;
  782. }
  783. return err;
  784. }
  785. #endif