vmt.c 24 KB

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