vmt.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  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] || ubi->volumes[vol->vol_id]->is_dead) {
  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. static struct fwnode_handle *find_volume_fwnode(struct ubi_volume *vol)
  114. {
  115. struct fwnode_handle *fw_vols, *fw_vol;
  116. const char *volname;
  117. u32 volid;
  118. fw_vols = device_get_named_child_node(vol->dev.parent->parent, "volumes");
  119. if (!fw_vols)
  120. return NULL;
  121. fwnode_for_each_child_node(fw_vols, fw_vol) {
  122. if (!fwnode_property_read_string(fw_vol, "volname", &volname) &&
  123. strncmp(volname, vol->name, vol->name_len))
  124. continue;
  125. if (!fwnode_property_read_u32(fw_vol, "volid", &volid) &&
  126. vol->vol_id != volid)
  127. continue;
  128. fwnode_handle_put(fw_vols);
  129. return fw_vol;
  130. }
  131. fwnode_handle_put(fw_vols);
  132. return NULL;
  133. }
  134. /**
  135. * ubi_create_volume - create volume.
  136. * @ubi: UBI device description object
  137. * @req: volume creation request
  138. *
  139. * This function creates volume described by @req. If @req->vol_id id
  140. * %UBI_VOL_NUM_AUTO, this function automatically assign ID to the new volume
  141. * and saves it in @req->vol_id. Returns zero in case of success and a negative
  142. * error code in case of failure. Note, the caller has to have the
  143. * @ubi->device_mutex locked.
  144. */
  145. int ubi_create_volume(struct ubi_device *ubi, struct ubi_mkvol_req *req)
  146. {
  147. int i, err, vol_id = req->vol_id;
  148. struct ubi_volume *vol;
  149. struct ubi_vtbl_record vtbl_rec;
  150. struct ubi_eba_table *eba_tbl = NULL;
  151. if (ubi->ro_mode)
  152. return -EROFS;
  153. vol = kzalloc(sizeof(struct ubi_volume), GFP_KERNEL);
  154. if (!vol)
  155. return -ENOMEM;
  156. device_initialize(&vol->dev);
  157. vol->dev.release = vol_release;
  158. vol->dev.parent = &ubi->dev;
  159. vol->dev.class = &ubi_class;
  160. vol->dev.groups = volume_dev_groups;
  161. if (req->flags & UBI_VOL_SKIP_CRC_CHECK_FLG)
  162. vol->skip_check = 1;
  163. spin_lock(&ubi->volumes_lock);
  164. if (vol_id == UBI_VOL_NUM_AUTO) {
  165. /* Find unused volume ID */
  166. dbg_gen("search for vacant volume ID");
  167. for (i = 0; i < ubi->vtbl_slots; i++)
  168. if (!ubi->volumes[i]) {
  169. vol_id = i;
  170. break;
  171. }
  172. if (vol_id == UBI_VOL_NUM_AUTO) {
  173. ubi_err(ubi, "out of volume IDs");
  174. err = -ENFILE;
  175. goto out_unlock;
  176. }
  177. req->vol_id = vol_id;
  178. }
  179. dbg_gen("create device %d, volume %d, %llu bytes, type %d, name %s",
  180. ubi->ubi_num, vol_id, (unsigned long long)req->bytes,
  181. (int)req->vol_type, req->name);
  182. /* Ensure that this volume does not exist */
  183. err = -EEXIST;
  184. if (ubi->volumes[vol_id]) {
  185. ubi_err(ubi, "volume %d already exists", vol_id);
  186. goto out_unlock;
  187. }
  188. /* Ensure that the name is unique */
  189. for (i = 0; i < ubi->vtbl_slots; i++)
  190. if (ubi->volumes[i] && !ubi->volumes[i]->is_dead &&
  191. ubi->volumes[i]->name_len == req->name_len &&
  192. !strcmp(ubi->volumes[i]->name, req->name)) {
  193. ubi_err(ubi, "volume \"%s\" exists (ID %d)",
  194. req->name, i);
  195. goto out_unlock;
  196. }
  197. /* Calculate how many eraseblocks are requested */
  198. vol->usable_leb_size = ubi->leb_size - ubi->leb_size % req->alignment;
  199. vol->reserved_pebs = div_u64(req->bytes + vol->usable_leb_size - 1,
  200. vol->usable_leb_size);
  201. /* Reserve physical eraseblocks */
  202. if (vol->reserved_pebs > ubi->avail_pebs) {
  203. ubi_err(ubi, "not enough PEBs, only %d available",
  204. ubi->avail_pebs);
  205. if (ubi->corr_peb_count)
  206. ubi_err(ubi, "%d PEBs are corrupted and not used",
  207. ubi->corr_peb_count);
  208. err = -ENOSPC;
  209. goto out_unlock;
  210. }
  211. ubi->avail_pebs -= vol->reserved_pebs;
  212. ubi->rsvd_pebs += vol->reserved_pebs;
  213. spin_unlock(&ubi->volumes_lock);
  214. vol->vol_id = vol_id;
  215. vol->alignment = req->alignment;
  216. vol->data_pad = ubi->leb_size % vol->alignment;
  217. vol->vol_type = req->vol_type;
  218. vol->name_len = req->name_len;
  219. memcpy(vol->name, req->name, vol->name_len);
  220. vol->ubi = ubi;
  221. device_set_node(&vol->dev, find_volume_fwnode(vol));
  222. /*
  223. * Finish all pending erases because there may be some LEBs belonging
  224. * to the same volume ID.
  225. */
  226. err = ubi_wl_flush(ubi, vol_id, UBI_ALL);
  227. if (err)
  228. goto out_acc;
  229. eba_tbl = ubi_eba_create_table(vol, vol->reserved_pebs);
  230. if (IS_ERR(eba_tbl)) {
  231. err = PTR_ERR(eba_tbl);
  232. goto out_acc;
  233. }
  234. ubi_eba_replace_table(vol, eba_tbl);
  235. if (vol->vol_type == UBI_DYNAMIC_VOLUME) {
  236. vol->used_ebs = vol->reserved_pebs;
  237. vol->last_eb_bytes = vol->usable_leb_size;
  238. vol->used_bytes =
  239. (long long)vol->used_ebs * vol->usable_leb_size;
  240. } else {
  241. vol->used_ebs = div_u64_rem(vol->used_bytes,
  242. vol->usable_leb_size,
  243. &vol->last_eb_bytes);
  244. if (vol->last_eb_bytes != 0)
  245. vol->used_ebs += 1;
  246. else
  247. vol->last_eb_bytes = vol->usable_leb_size;
  248. }
  249. /* Make volume "available" before it becomes accessible via sysfs */
  250. spin_lock(&ubi->volumes_lock);
  251. ubi->volumes[vol_id] = vol;
  252. ubi->vol_count += 1;
  253. spin_unlock(&ubi->volumes_lock);
  254. /* Register character device for the volume */
  255. cdev_init(&vol->cdev, &ubi_vol_cdev_operations);
  256. vol->cdev.owner = THIS_MODULE;
  257. vol->dev.devt = MKDEV(MAJOR(ubi->cdev.dev), vol_id + 1);
  258. dev_set_name(&vol->dev, "%s_%d", ubi->ubi_name, vol->vol_id);
  259. err = cdev_device_add(&vol->cdev, &vol->dev);
  260. if (err) {
  261. ubi_err(ubi, "cannot add device");
  262. goto out_mapping;
  263. }
  264. /* Fill volume table record */
  265. memset(&vtbl_rec, 0, sizeof(struct ubi_vtbl_record));
  266. vtbl_rec.reserved_pebs = cpu_to_be32(vol->reserved_pebs);
  267. vtbl_rec.alignment = cpu_to_be32(vol->alignment);
  268. vtbl_rec.data_pad = cpu_to_be32(vol->data_pad);
  269. vtbl_rec.name_len = cpu_to_be16(vol->name_len);
  270. if (vol->vol_type == UBI_DYNAMIC_VOLUME)
  271. vtbl_rec.vol_type = UBI_VID_DYNAMIC;
  272. else
  273. vtbl_rec.vol_type = UBI_VID_STATIC;
  274. if (vol->skip_check)
  275. vtbl_rec.flags |= UBI_VTBL_SKIP_CRC_CHECK_FLG;
  276. memcpy(vtbl_rec.name, vol->name, vol->name_len);
  277. err = ubi_change_vtbl_record(ubi, vol_id, &vtbl_rec);
  278. if (err)
  279. goto out_sysfs;
  280. ubi_volume_notify(ubi, vol, UBI_VOLUME_ADDED);
  281. self_check_volumes(ubi);
  282. return err;
  283. out_sysfs:
  284. /*
  285. * We have registered our device, we should not free the volume
  286. * description object in this function in case of an error - it is
  287. * freed by the release function.
  288. */
  289. cdev_device_del(&vol->cdev, &vol->dev);
  290. out_mapping:
  291. spin_lock(&ubi->volumes_lock);
  292. ubi->volumes[vol_id] = NULL;
  293. ubi->vol_count -= 1;
  294. spin_unlock(&ubi->volumes_lock);
  295. out_acc:
  296. spin_lock(&ubi->volumes_lock);
  297. ubi->rsvd_pebs -= vol->reserved_pebs;
  298. ubi->avail_pebs += vol->reserved_pebs;
  299. out_unlock:
  300. spin_unlock(&ubi->volumes_lock);
  301. put_device(&vol->dev);
  302. ubi_err(ubi, "cannot create volume %d, error %d", vol_id, err);
  303. return err;
  304. }
  305. /**
  306. * ubi_remove_volume - remove volume.
  307. * @desc: volume descriptor
  308. * @no_vtbl: do not change volume table if not zero
  309. *
  310. * This function removes volume described by @desc. The volume has to be opened
  311. * in "exclusive" mode. Returns zero in case of success and a negative error
  312. * code in case of failure. The caller has to have the @ubi->device_mutex
  313. * locked.
  314. */
  315. int ubi_remove_volume(struct ubi_volume_desc *desc, int no_vtbl)
  316. {
  317. struct ubi_volume *vol = desc->vol;
  318. struct ubi_device *ubi = vol->ubi;
  319. int i, err, vol_id = vol->vol_id, reserved_pebs = vol->reserved_pebs;
  320. dbg_gen("remove device %d, volume %d", ubi->ubi_num, vol_id);
  321. ubi_assert(desc->mode == UBI_EXCLUSIVE);
  322. ubi_assert(vol == ubi->volumes[vol_id]);
  323. if (ubi->ro_mode)
  324. return -EROFS;
  325. spin_lock(&ubi->volumes_lock);
  326. if (vol->ref_count > 1) {
  327. /*
  328. * The volume is busy, probably someone is reading one of its
  329. * sysfs files.
  330. */
  331. err = -EBUSY;
  332. goto out_unlock;
  333. }
  334. /*
  335. * Mark volume as dead at this point to prevent that anyone
  336. * can take a reference to the volume from now on.
  337. * This is necessary as we have to release the spinlock before
  338. * calling ubi_volume_notify.
  339. */
  340. vol->is_dead = true;
  341. spin_unlock(&ubi->volumes_lock);
  342. ubi_volume_notify(ubi, vol, UBI_VOLUME_SHUTDOWN);
  343. spin_lock(&ubi->volumes_lock);
  344. ubi->volumes[vol_id] = NULL;
  345. spin_unlock(&ubi->volumes_lock);
  346. if (!no_vtbl) {
  347. err = ubi_change_vtbl_record(ubi, vol_id, NULL);
  348. if (err)
  349. goto out_err;
  350. }
  351. for (i = 0; i < vol->reserved_pebs; i++) {
  352. err = ubi_eba_unmap_leb(ubi, vol, i);
  353. if (err)
  354. goto out_err;
  355. }
  356. cdev_device_del(&vol->cdev, &vol->dev);
  357. put_device(&vol->dev);
  358. spin_lock(&ubi->volumes_lock);
  359. ubi->rsvd_pebs -= reserved_pebs;
  360. ubi->avail_pebs += reserved_pebs;
  361. ubi_update_reserved(ubi);
  362. ubi->vol_count -= 1;
  363. spin_unlock(&ubi->volumes_lock);
  364. ubi_volume_notify(ubi, vol, UBI_VOLUME_REMOVED);
  365. if (!no_vtbl)
  366. self_check_volumes(ubi);
  367. return 0;
  368. out_err:
  369. ubi_err(ubi, "cannot remove volume %d, error %d", vol_id, err);
  370. spin_lock(&ubi->volumes_lock);
  371. ubi->volumes[vol_id] = vol;
  372. out_unlock:
  373. spin_unlock(&ubi->volumes_lock);
  374. return err;
  375. }
  376. /**
  377. * ubi_resize_volume - re-size volume.
  378. * @desc: volume descriptor
  379. * @reserved_pebs: new size in physical eraseblocks
  380. *
  381. * This function re-sizes the volume and returns zero in case of success, and a
  382. * negative error code in case of failure. The caller has to have the
  383. * @ubi->device_mutex locked.
  384. */
  385. int ubi_resize_volume(struct ubi_volume_desc *desc, int reserved_pebs)
  386. {
  387. int i, err, pebs;
  388. struct ubi_volume *vol = desc->vol;
  389. struct ubi_device *ubi = vol->ubi;
  390. struct ubi_vtbl_record vtbl_rec;
  391. struct ubi_eba_table *new_eba_tbl = NULL;
  392. struct ubi_eba_table *old_eba_tbl = NULL;
  393. int vol_id = vol->vol_id;
  394. if (ubi->ro_mode)
  395. return -EROFS;
  396. dbg_gen("re-size device %d, volume %d to from %d to %d PEBs",
  397. ubi->ubi_num, vol_id, vol->reserved_pebs, reserved_pebs);
  398. if (vol->vol_type == UBI_STATIC_VOLUME &&
  399. reserved_pebs < vol->used_ebs) {
  400. ubi_err(ubi, "too small size %d, %d LEBs contain data",
  401. reserved_pebs, vol->used_ebs);
  402. return -EINVAL;
  403. }
  404. /* If the size is the same, we have nothing to do */
  405. if (reserved_pebs == vol->reserved_pebs)
  406. return 0;
  407. new_eba_tbl = ubi_eba_create_table(vol, reserved_pebs);
  408. if (IS_ERR(new_eba_tbl))
  409. return PTR_ERR(new_eba_tbl);
  410. spin_lock(&ubi->volumes_lock);
  411. if (vol->ref_count > 1) {
  412. spin_unlock(&ubi->volumes_lock);
  413. err = -EBUSY;
  414. goto out_free;
  415. }
  416. spin_unlock(&ubi->volumes_lock);
  417. /* Reserve physical eraseblocks */
  418. pebs = reserved_pebs - vol->reserved_pebs;
  419. if (pebs > 0) {
  420. spin_lock(&ubi->volumes_lock);
  421. if (pebs > ubi->avail_pebs) {
  422. ubi_err(ubi, "not enough PEBs: requested %d, available %d",
  423. pebs, ubi->avail_pebs);
  424. if (ubi->corr_peb_count)
  425. ubi_err(ubi, "%d PEBs are corrupted and not used",
  426. ubi->corr_peb_count);
  427. spin_unlock(&ubi->volumes_lock);
  428. err = -ENOSPC;
  429. goto out_free;
  430. }
  431. ubi->avail_pebs -= pebs;
  432. ubi->rsvd_pebs += pebs;
  433. ubi_eba_copy_table(vol, new_eba_tbl, vol->reserved_pebs);
  434. old_eba_tbl = vol->eba_tbl;
  435. vol->eba_tbl = new_eba_tbl;
  436. vol->reserved_pebs = reserved_pebs;
  437. spin_unlock(&ubi->volumes_lock);
  438. }
  439. if (pebs < 0) {
  440. for (i = 0; i < -pebs; i++) {
  441. err = ubi_eba_unmap_leb(ubi, vol, reserved_pebs + i);
  442. if (err)
  443. goto out_free;
  444. }
  445. spin_lock(&ubi->volumes_lock);
  446. ubi->rsvd_pebs += pebs;
  447. ubi->avail_pebs -= pebs;
  448. ubi_update_reserved(ubi);
  449. ubi_eba_copy_table(vol, new_eba_tbl, reserved_pebs);
  450. old_eba_tbl = vol->eba_tbl;
  451. vol->eba_tbl = new_eba_tbl;
  452. vol->reserved_pebs = reserved_pebs;
  453. spin_unlock(&ubi->volumes_lock);
  454. }
  455. /*
  456. * When we shrink a volume we have to flush all pending (erase) work.
  457. * Otherwise it can happen that upon next attach UBI finds a LEB with
  458. * lnum > highest_lnum and refuses to attach.
  459. */
  460. if (pebs < 0) {
  461. err = ubi_wl_flush(ubi, vol_id, UBI_ALL);
  462. if (err)
  463. goto out_acc;
  464. }
  465. /* Change volume table record */
  466. vtbl_rec = ubi->vtbl[vol_id];
  467. vtbl_rec.reserved_pebs = cpu_to_be32(reserved_pebs);
  468. err = ubi_change_vtbl_record(ubi, vol_id, &vtbl_rec);
  469. if (err)
  470. goto out_acc;
  471. if (vol->vol_type == UBI_DYNAMIC_VOLUME) {
  472. vol->used_ebs = reserved_pebs;
  473. vol->last_eb_bytes = vol->usable_leb_size;
  474. vol->used_bytes =
  475. (long long)vol->used_ebs * vol->usable_leb_size;
  476. }
  477. /* destroy old table */
  478. ubi_eba_destroy_table(old_eba_tbl);
  479. ubi_volume_notify(ubi, vol, UBI_VOLUME_RESIZED);
  480. self_check_volumes(ubi);
  481. return err;
  482. out_acc:
  483. spin_lock(&ubi->volumes_lock);
  484. vol->reserved_pebs = reserved_pebs - pebs;
  485. ubi->rsvd_pebs -= pebs;
  486. ubi->avail_pebs += pebs;
  487. if (pebs > 0)
  488. ubi_eba_copy_table(vol, old_eba_tbl, vol->reserved_pebs);
  489. else
  490. ubi_eba_copy_table(vol, old_eba_tbl, reserved_pebs);
  491. vol->eba_tbl = old_eba_tbl;
  492. spin_unlock(&ubi->volumes_lock);
  493. out_free:
  494. ubi_eba_destroy_table(new_eba_tbl);
  495. return err;
  496. }
  497. /**
  498. * ubi_rename_volumes - re-name UBI volumes.
  499. * @ubi: UBI device description object
  500. * @rename_list: list of &struct ubi_rename_entry objects
  501. *
  502. * This function re-names or removes volumes specified in the re-name list.
  503. * Returns zero in case of success and a negative error code in case of
  504. * failure.
  505. */
  506. int ubi_rename_volumes(struct ubi_device *ubi, struct list_head *rename_list)
  507. {
  508. int err;
  509. struct ubi_rename_entry *re;
  510. err = ubi_vtbl_rename_volumes(ubi, rename_list);
  511. if (err)
  512. return err;
  513. list_for_each_entry(re, rename_list, list) {
  514. if (re->remove) {
  515. err = ubi_remove_volume(re->desc, 1);
  516. if (err)
  517. break;
  518. } else {
  519. struct ubi_volume *vol = re->desc->vol;
  520. spin_lock(&ubi->volumes_lock);
  521. vol->name_len = re->new_name_len;
  522. memcpy(vol->name, re->new_name, re->new_name_len + 1);
  523. spin_unlock(&ubi->volumes_lock);
  524. ubi_volume_notify(ubi, vol, UBI_VOLUME_RENAMED);
  525. }
  526. }
  527. if (!err)
  528. self_check_volumes(ubi);
  529. return err;
  530. }
  531. /**
  532. * ubi_add_volume - add volume.
  533. * @ubi: UBI device description object
  534. * @vol: volume description object
  535. *
  536. * This function adds an existing volume and initializes all its data
  537. * structures. Returns zero in case of success and a negative error code in
  538. * case of failure.
  539. */
  540. int ubi_add_volume(struct ubi_device *ubi, struct ubi_volume *vol)
  541. {
  542. int err, vol_id = vol->vol_id;
  543. dev_t dev;
  544. dbg_gen("add volume %d", vol_id);
  545. /* Register character device for the volume */
  546. cdev_init(&vol->cdev, &ubi_vol_cdev_operations);
  547. vol->cdev.owner = THIS_MODULE;
  548. dev = MKDEV(MAJOR(ubi->cdev.dev), vol->vol_id + 1);
  549. err = cdev_add(&vol->cdev, dev, 1);
  550. if (err) {
  551. ubi_err(ubi, "cannot add character device for volume %d, error %d",
  552. vol_id, err);
  553. vol_release(&vol->dev);
  554. return err;
  555. }
  556. vol->dev.release = vol_release;
  557. vol->dev.parent = &ubi->dev;
  558. vol->dev.devt = dev;
  559. vol->dev.class = &ubi_class;
  560. vol->dev.groups = volume_dev_groups;
  561. dev_set_name(&vol->dev, "%s_%d", ubi->ubi_name, vol->vol_id);
  562. device_set_node(&vol->dev, find_volume_fwnode(vol));
  563. err = device_register(&vol->dev);
  564. if (err) {
  565. cdev_del(&vol->cdev);
  566. put_device(&vol->dev);
  567. return err;
  568. }
  569. self_check_volumes(ubi);
  570. return err;
  571. }
  572. /**
  573. * ubi_free_volume - free volume.
  574. * @ubi: UBI device description object
  575. * @vol: volume description object
  576. *
  577. * This function frees all resources for volume @vol but does not remove it.
  578. * Used only when the UBI device is detached.
  579. */
  580. void ubi_free_volume(struct ubi_device *ubi, struct ubi_volume *vol)
  581. {
  582. dbg_gen("free volume %d", vol->vol_id);
  583. ubi->volumes[vol->vol_id] = NULL;
  584. cdev_del(&vol->cdev);
  585. device_unregister(&vol->dev);
  586. }
  587. /**
  588. * self_check_volume - check volume information.
  589. * @ubi: UBI device description object
  590. * @vol_id: volume ID
  591. *
  592. * Returns zero if volume is all right and a negative error code if not.
  593. */
  594. static int self_check_volume(struct ubi_device *ubi, int vol_id)
  595. {
  596. int idx = vol_id2idx(ubi, vol_id);
  597. int reserved_pebs, alignment, data_pad, vol_type, name_len, upd_marker;
  598. const struct ubi_volume *vol;
  599. long long n;
  600. const char *name;
  601. spin_lock(&ubi->volumes_lock);
  602. reserved_pebs = be32_to_cpu(ubi->vtbl[vol_id].reserved_pebs);
  603. vol = ubi->volumes[idx];
  604. if (!vol) {
  605. if (reserved_pebs) {
  606. ubi_err(ubi, "no volume info, but volume exists");
  607. goto fail;
  608. }
  609. spin_unlock(&ubi->volumes_lock);
  610. return 0;
  611. }
  612. if (vol->reserved_pebs < 0 || vol->alignment < 0 || vol->data_pad < 0 ||
  613. vol->name_len < 0) {
  614. ubi_err(ubi, "negative values");
  615. goto fail;
  616. }
  617. if (vol->alignment > ubi->leb_size || vol->alignment == 0) {
  618. ubi_err(ubi, "bad alignment");
  619. goto fail;
  620. }
  621. n = vol->alignment & (ubi->min_io_size - 1);
  622. if (vol->alignment != 1 && n) {
  623. ubi_err(ubi, "alignment is not multiple of min I/O unit");
  624. goto fail;
  625. }
  626. n = ubi->leb_size % vol->alignment;
  627. if (vol->data_pad != n) {
  628. ubi_err(ubi, "bad data_pad, has to be %lld", n);
  629. goto fail;
  630. }
  631. if (vol->vol_type != UBI_DYNAMIC_VOLUME &&
  632. vol->vol_type != UBI_STATIC_VOLUME) {
  633. ubi_err(ubi, "bad vol_type");
  634. goto fail;
  635. }
  636. if (vol->upd_marker && vol->corrupted) {
  637. ubi_err(ubi, "update marker and corrupted simultaneously");
  638. goto fail;
  639. }
  640. if (vol->reserved_pebs > ubi->good_peb_count) {
  641. ubi_err(ubi, "too large reserved_pebs");
  642. goto fail;
  643. }
  644. n = ubi->leb_size - vol->data_pad;
  645. if (vol->usable_leb_size != ubi->leb_size - vol->data_pad) {
  646. ubi_err(ubi, "bad usable_leb_size, has to be %lld", n);
  647. goto fail;
  648. }
  649. if (vol->name_len > UBI_VOL_NAME_MAX) {
  650. ubi_err(ubi, "too long volume name, max is %d",
  651. UBI_VOL_NAME_MAX);
  652. goto fail;
  653. }
  654. n = strnlen(vol->name, vol->name_len + 1);
  655. if (n != vol->name_len) {
  656. ubi_err(ubi, "bad name_len %lld", n);
  657. goto fail;
  658. }
  659. n = (long long)vol->used_ebs * vol->usable_leb_size;
  660. if (vol->vol_type == UBI_DYNAMIC_VOLUME) {
  661. if (vol->corrupted) {
  662. ubi_err(ubi, "corrupted dynamic volume");
  663. goto fail;
  664. }
  665. if (vol->used_ebs != vol->reserved_pebs) {
  666. ubi_err(ubi, "bad used_ebs");
  667. goto fail;
  668. }
  669. if (vol->last_eb_bytes != vol->usable_leb_size) {
  670. ubi_err(ubi, "bad last_eb_bytes");
  671. goto fail;
  672. }
  673. if (vol->used_bytes != n) {
  674. ubi_err(ubi, "bad used_bytes");
  675. goto fail;
  676. }
  677. if (vol->skip_check) {
  678. ubi_err(ubi, "bad skip_check");
  679. goto fail;
  680. }
  681. } else {
  682. if (vol->used_ebs < 0 || vol->used_ebs > vol->reserved_pebs) {
  683. ubi_err(ubi, "bad used_ebs");
  684. goto fail;
  685. }
  686. if (vol->last_eb_bytes < 0 ||
  687. vol->last_eb_bytes > vol->usable_leb_size) {
  688. ubi_err(ubi, "bad last_eb_bytes");
  689. goto fail;
  690. }
  691. if (vol->used_bytes < 0 || vol->used_bytes > n ||
  692. vol->used_bytes < n - vol->usable_leb_size) {
  693. ubi_err(ubi, "bad used_bytes");
  694. goto fail;
  695. }
  696. }
  697. alignment = be32_to_cpu(ubi->vtbl[vol_id].alignment);
  698. data_pad = be32_to_cpu(ubi->vtbl[vol_id].data_pad);
  699. name_len = be16_to_cpu(ubi->vtbl[vol_id].name_len);
  700. upd_marker = ubi->vtbl[vol_id].upd_marker;
  701. name = &ubi->vtbl[vol_id].name[0];
  702. if (ubi->vtbl[vol_id].vol_type == UBI_VID_DYNAMIC)
  703. vol_type = UBI_DYNAMIC_VOLUME;
  704. else
  705. vol_type = UBI_STATIC_VOLUME;
  706. if (alignment != vol->alignment || data_pad != vol->data_pad ||
  707. upd_marker != vol->upd_marker || vol_type != vol->vol_type ||
  708. name_len != vol->name_len || strncmp(name, vol->name, name_len)) {
  709. ubi_err(ubi, "volume info is different");
  710. goto fail;
  711. }
  712. spin_unlock(&ubi->volumes_lock);
  713. return 0;
  714. fail:
  715. ubi_err(ubi, "self-check failed for volume %d", vol_id);
  716. if (vol)
  717. ubi_dump_vol_info(vol);
  718. ubi_dump_vtbl_record(&ubi->vtbl[vol_id], vol_id);
  719. dump_stack();
  720. spin_unlock(&ubi->volumes_lock);
  721. return -EINVAL;
  722. }
  723. /**
  724. * self_check_volumes - check information about all volumes.
  725. * @ubi: UBI device description object
  726. *
  727. * Returns zero if volumes are all right and a negative error code if not.
  728. */
  729. static int self_check_volumes(struct ubi_device *ubi)
  730. {
  731. int i, err = 0;
  732. if (!ubi_dbg_chk_gen(ubi))
  733. return 0;
  734. for (i = 0; i < ubi->vtbl_slots; i++) {
  735. err = self_check_volume(ubi, i);
  736. if (err)
  737. break;
  738. }
  739. return err;
  740. }