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