volrefcnt.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Copyright (c) Nokia Corporation, 2007
  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., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. *
  18. * Author: Artem B. Bityutskiy
  19. *
  20. * Test volume reference counting - create a volume, open a sysfs file
  21. * belonging to the volume, delete the volume but do not close the file, make
  22. * sure the file cannot be read, close the file, make sure the volume
  23. * disappeard, make sure its sysfs subtree disappeared.
  24. */
  25. #include <stdio.h>
  26. #include <errno.h>
  27. #include <string.h>
  28. #include <fcntl.h>
  29. #include <unistd.h>
  30. #include "libubi.h"
  31. #define PROGRAM_NAME "rmvol"
  32. #include "common.h"
  33. #include "helpers.h"
  34. #define SYSFS_FILE "/sys/class/ubi/ubi%d_%d/usable_eb_size"
  35. int main(int argc, char * const argv[])
  36. {
  37. int ret, fd;
  38. char fname[sizeof(SYSFS_FILE) + 20];
  39. const char *node;
  40. libubi_t libubi;
  41. struct ubi_dev_info dev_info;
  42. struct ubi_mkvol_request req;
  43. char tmp[100];
  44. if (initial_check(argc, argv))
  45. return 1;
  46. node = argv[1];
  47. libubi = libubi_open();
  48. if (libubi == NULL) {
  49. failed("libubi_open");
  50. return 1;
  51. }
  52. if (ubi_get_dev_info(libubi, node, &dev_info)) {
  53. failed("ubi_get_dev_info");
  54. goto out_libubi;
  55. }
  56. /* Create a small dynamic volume */
  57. req.vol_id = UBI_VOL_NUM_AUTO;
  58. req.alignment = dev_info.min_io_size;
  59. req.bytes = dev_info.leb_size;
  60. req.vol_type = UBI_DYNAMIC_VOLUME;
  61. req.name = "rmvol";
  62. req.flags = 0;
  63. if (ubi_mkvol(libubi, node, &req)) {
  64. failed("ubi_mkvol");
  65. goto out_libubi;
  66. }
  67. /* Open volume-related sysfs file */
  68. sprintf(fname, SYSFS_FILE, dev_info.dev_num, req.vol_id);
  69. fd = open(fname, O_RDONLY);
  70. if (fd == -1) {
  71. errorm("cannot open %s", fname);
  72. failed("open");
  73. goto out_rmvol;
  74. }
  75. /* Remove the volume, but do not close the file */
  76. if (ubi_rmvol(libubi, node, req.vol_id)) {
  77. failed("ubi_rmvol");
  78. perror("ubi_rmvol");
  79. goto out_close;
  80. }
  81. /* Try to read from the file, this should fail */
  82. ret = read(fd, tmp, 100);
  83. if (ret != -1) {
  84. errorm("read returned %d, expected -1", ret);
  85. failed("read");
  86. goto out_close;
  87. }
  88. /* Close the file and try to open it again, should fail */
  89. close(fd);
  90. fd = open(fname, O_RDONLY);
  91. if (fd != -1) {
  92. errorm("opened %s again, open returned %d, expected -1",
  93. fname, fd);
  94. failed("open");
  95. goto out_libubi;
  96. }
  97. libubi_close(libubi);
  98. return 0;
  99. out_rmvol:
  100. ubi_rmvol(libubi, node, req.vol_id);
  101. out_libubi:
  102. libubi_close(libubi);
  103. return 1;
  104. out_close:
  105. close(fd);
  106. libubi_close(libubi);
  107. return 1;
  108. }