vmt.c 24 KB

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