vmt.c 22 KB

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