vmt.c 21 KB

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