vmt.c 22 KB

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