mkvol_basic.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. *
  18. * Author: Artem B. Bityutskiy
  19. *
  20. * Test test checks basic volume creation and deletion capabilities.
  21. */
  22. #include <stdio.h>
  23. #include <errno.h>
  24. #include <string.h>
  25. #include "libubi.h"
  26. #define PROGRAM_NAME "mkvol_basic"
  27. #include "common.h"
  28. #include "helpers.h"
  29. static libubi_t libubi;
  30. static struct ubi_dev_info dev_info;
  31. const char *node;
  32. /**
  33. * mkvol_alignment - create volumes with different alignments.
  34. *
  35. * Thus function returns %0 in case of success and %-1 in case of failure.
  36. */
  37. static int mkvol_alignment(void)
  38. {
  39. struct ubi_mkvol_request req;
  40. int i, vol_id, ebsz;
  41. const char *name = PROGRAM_NAME ":mkvol_alignment()";
  42. int alignments[] = ALIGNMENTS(dev_info.leb_size);
  43. for (i = 0; i < sizeof(alignments)/sizeof(int); i++) {
  44. req.vol_id = UBI_VOL_NUM_AUTO;
  45. /* Alignment should actually be multiple of min. I/O size */
  46. req.alignment = alignments[i];
  47. req.alignment -= req.alignment % dev_info.min_io_size;
  48. if (req.alignment == 0)
  49. req.alignment = dev_info.min_io_size;
  50. /* Bear in mind alignment reduces EB size */
  51. ebsz = dev_info.leb_size - dev_info.leb_size % req.alignment;
  52. req.bytes = (long long)dev_info.avail_lebs * ebsz;
  53. req.vol_type = UBI_DYNAMIC_VOLUME;
  54. req.name = name;
  55. req.flags = 0;
  56. if (ubi_mkvol(libubi, node, &req)) {
  57. failed("ubi_mkvol");
  58. errorm("alignment %d", req.alignment);
  59. return -1;
  60. }
  61. vol_id = req.vol_id;
  62. if (check_volume(vol_id, &req))
  63. goto remove;
  64. if (ubi_rmvol(libubi, node, vol_id)) {
  65. failed("ubi_rmvol");
  66. return -1;
  67. }
  68. }
  69. return 0;
  70. remove:
  71. ubi_rmvol(libubi, node, vol_id);
  72. return -1;
  73. }
  74. /**
  75. * mkvol_basic - simple test that checks basic volume creation capability.
  76. *
  77. * Thus function returns %0 in case of success and %-1 in case of failure.
  78. */
  79. static int mkvol_basic(void)
  80. {
  81. struct ubi_mkvol_request req;
  82. struct ubi_vol_info vol_info;
  83. int vol_id, ret;
  84. const char *name = PROGRAM_NAME ":mkvol_basic()";
  85. /* Create dynamic volume of maximum size */
  86. req.vol_id = UBI_VOL_NUM_AUTO;
  87. req.alignment = 1;
  88. req.bytes = dev_info.avail_bytes;
  89. req.vol_type = UBI_DYNAMIC_VOLUME;
  90. req.name = name;
  91. req.flags = 0;
  92. if (ubi_mkvol(libubi, node, &req)) {
  93. failed("ubi_mkvol");
  94. return -1;
  95. }
  96. vol_id = req.vol_id;
  97. if (check_volume(vol_id, &req))
  98. goto remove;
  99. if (ubi_rmvol(libubi, node, vol_id)) {
  100. failed("ubi_rmvol");
  101. return -1;
  102. }
  103. /* Create static volume of maximum size */
  104. req.vol_id = UBI_VOL_NUM_AUTO;
  105. req.alignment = 1;
  106. req.bytes = dev_info.avail_bytes;
  107. req.vol_type = UBI_STATIC_VOLUME;
  108. req.name = name;
  109. if (ubi_mkvol(libubi, node, &req)) {
  110. failed("ubi_mkvol");
  111. return -1;
  112. }
  113. vol_id = req.vol_id;
  114. if (check_volume(vol_id, &req))
  115. goto remove;
  116. if (ubi_rmvol(libubi, node, vol_id)) {
  117. failed("ubi_rmvol");
  118. return -1;
  119. }
  120. /* Make sure volume does not exist */
  121. ret = ubi_get_vol_info1(libubi, dev_info.dev_num, vol_id, &vol_info);
  122. if (ret == 0) {
  123. errorm("removed volume %d exists", vol_id);
  124. goto remove;
  125. }
  126. return 0;
  127. remove:
  128. ubi_rmvol(libubi, node, vol_id);
  129. return -1;
  130. }
  131. /**
  132. * mkvol_multiple - test multiple volumes creation
  133. *
  134. * Thus function returns %0 if the test passed and %-1 if not.
  135. */
  136. static int mkvol_multiple(void)
  137. {
  138. struct ubi_mkvol_request req;
  139. int i, ret, max = dev_info.max_vol_count;
  140. const char *name = PROGRAM_NAME ":mkvol_multiple()";
  141. /* Create maximum number of volumes */
  142. for (i = 0; i < max; i++) {
  143. char nm[strlen(name) + 50];
  144. req.vol_id = UBI_VOL_NUM_AUTO;
  145. req.alignment = 1;
  146. req.bytes = 1;
  147. req.vol_type = UBI_STATIC_VOLUME;
  148. req.flags = 0;
  149. sprintf(nm, "%s:%d", name, i);
  150. req.name = nm;
  151. if (ubi_mkvol(libubi, node, &req)) {
  152. if (errno == ENFILE || errno == ENOSPC) {
  153. max = i;
  154. break;
  155. }
  156. failed("ubi_mkvol");
  157. errorm("vol_id %d", i);
  158. goto remove;
  159. }
  160. if (check_volume(req.vol_id, &req)) {
  161. errorm("vol_id %d", i);
  162. goto remove;
  163. }
  164. }
  165. for (i = 0; i < max; i++) {
  166. struct ubi_vol_info vol_info;
  167. if (ubi_rmvol(libubi, node, i)) {
  168. failed("ubi_rmvol");
  169. return -1;
  170. }
  171. /* Make sure volume does not exist */
  172. ret = ubi_get_vol_info1(libubi, dev_info.dev_num, i, &vol_info);
  173. if (ret == 0) {
  174. errorm("removed volume %d exists", i);
  175. goto remove;
  176. }
  177. }
  178. return 0;
  179. remove:
  180. for (i = 0; i < dev_info.max_vol_count + 1; i++)
  181. ubi_rmvol(libubi, node, i);
  182. return -1;
  183. }
  184. int main(int argc, char * const argv[])
  185. {
  186. if (initial_check(argc, argv))
  187. return 1;
  188. node = argv[1];
  189. libubi = libubi_open();
  190. if (libubi == NULL) {
  191. failed("libubi_open");
  192. return 1;
  193. }
  194. if (ubi_get_dev_info(libubi, node, &dev_info)) {
  195. failed("ubi_get_dev_info");
  196. goto close;
  197. }
  198. if (mkvol_basic())
  199. goto close;
  200. if (mkvol_alignment())
  201. goto close;
  202. if (mkvol_multiple())
  203. goto close;
  204. libubi_close(libubi);
  205. return 0;
  206. close:
  207. libubi_close(libubi);
  208. return 1;
  209. }