rsvol.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. * Tes UBI volume re-size.
  21. */
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <errno.h>
  25. #include <string.h>
  26. #include <unistd.h>
  27. #include <sys/types.h>
  28. #include <sys/stat.h>
  29. #include <fcntl.h>
  30. #include "libubi.h"
  31. #define PROGRAM_NAME "rsvol"
  32. #include "common.h"
  33. #include "helpers.h"
  34. static libubi_t libubi;
  35. static struct ubi_dev_info dev_info;
  36. const char *node;
  37. /**
  38. * test_basic - check volume re-size capability.
  39. *
  40. * @type volume type (%UBI_DYNAMIC_VOLUME or %UBI_STATIC_VOLUME)
  41. *
  42. * Thus function returns %0 in case of success and %-1 in case of failure.
  43. */
  44. static int test_basic(int type)
  45. {
  46. struct ubi_mkvol_request req;
  47. const char *name = PROGRAM_NAME ":test_basic()";
  48. req.vol_id = UBI_VOL_NUM_AUTO;
  49. req.alignment = 1;
  50. req.bytes = MIN_AVAIL_EBS * dev_info.leb_size;
  51. req.vol_type = type;
  52. req.name = name;
  53. req.flags = 0;
  54. if (ubi_mkvol(libubi, node, &req)) {
  55. failed("ubi_mkvol");
  56. return -1;
  57. }
  58. req.bytes = dev_info.leb_size;
  59. if (ubi_rsvol(libubi, node, req.vol_id, req.bytes)) {
  60. failed("ubi_rsvol");
  61. goto remove;
  62. }
  63. if (check_volume(req.vol_id, &req))
  64. goto remove;
  65. req.bytes = (MIN_AVAIL_EBS + 1) * dev_info.leb_size;
  66. if (ubi_rsvol(libubi, node, req.vol_id, req.bytes)) {
  67. failed("ubi_rsvol");
  68. goto remove;
  69. }
  70. if (check_volume(req.vol_id, &req))
  71. goto remove;
  72. req.bytes -= 1;
  73. if (ubi_rsvol(libubi, node, req.vol_id, req.bytes)) {
  74. failed("ubi_rsvol");
  75. goto remove;
  76. }
  77. if (check_volume(req.vol_id, &req))
  78. goto remove;
  79. if (ubi_rmvol(libubi, node, req.vol_id)) {
  80. failed("ubi_rmvol");
  81. return -1;
  82. }
  83. return 0;
  84. remove:
  85. ubi_rmvol(libubi, node, req.vol_id);
  86. return -1;
  87. }
  88. /*
  89. * Helper function for test_rsvol().
  90. */
  91. static int test_rsvol1(struct ubi_vol_info *vol_info)
  92. {
  93. long long bytes;
  94. struct ubi_vol_info vol_info1;
  95. char vol_node[strlen(UBI_VOLUME_PATTERN) + 100];
  96. unsigned char *buf;
  97. int fd, i, ret;
  98. int ret1 = -1;
  99. buf = malloc(vol_info->rsvd_bytes);
  100. if (!buf) {
  101. failed("malloc");
  102. goto out;
  103. }
  104. /* Make the volume smaller and check basic volume I/O */
  105. bytes = vol_info->rsvd_bytes - vol_info->leb_size;
  106. if (ubi_rsvol(libubi, node, vol_info->vol_id, bytes - 1)) {
  107. failed("ubi_rsvol");
  108. goto out;
  109. }
  110. if (ubi_get_vol_info1(libubi, vol_info->dev_num, vol_info->vol_id,
  111. &vol_info1)) {
  112. failed("ubi_get_vol_info");
  113. goto out;
  114. }
  115. if (vol_info1.rsvd_bytes != bytes) {
  116. errorm("rsvd_bytes %lld, must be %lld",
  117. vol_info1.rsvd_bytes, bytes);
  118. goto out;
  119. }
  120. if (vol_info1.rsvd_lebs != vol_info->rsvd_lebs - 1) {
  121. errorm("rsvd_lebs %d, must be %d",
  122. vol_info1.rsvd_lebs, vol_info->rsvd_lebs - 1);
  123. goto out;
  124. }
  125. /* Write data to the volume */
  126. sprintf(vol_node, UBI_VOLUME_PATTERN, dev_info.dev_num,
  127. vol_info->vol_id);
  128. fd = open(vol_node, O_RDWR);
  129. if (fd == -1) {
  130. failed("open");
  131. errorm("cannot open \"%s\"\n", vol_node);
  132. goto out;
  133. }
  134. bytes = vol_info->rsvd_bytes - vol_info->leb_size - 1;
  135. if (ubi_update_start(libubi, fd, bytes)) {
  136. failed("ubi_update_start");
  137. goto close;
  138. }
  139. for (i = 0; i < bytes; i++)
  140. buf[i] = (unsigned char)i;
  141. ret = write(fd, buf, bytes);
  142. if (ret != bytes) {
  143. failed("write");
  144. goto close;
  145. }
  146. close(fd);
  147. if (ubi_rsvol(libubi, node, vol_info->vol_id, bytes)) {
  148. failed("ubi_rsvol");
  149. goto out;
  150. }
  151. if (ubi_rsvol(libubi, node, vol_info->vol_id,
  152. (long long)vol_info->leb_size * dev_info.avail_lebs)) {
  153. failed("ubi_rsvol");
  154. goto out;
  155. }
  156. fd = open(vol_node, O_RDWR);
  157. if (fd == -1) {
  158. failed("open");
  159. errorm("cannot open \"%s\"\n", vol_node);
  160. goto out;
  161. }
  162. /* Read data back */
  163. if (lseek(fd, 0, SEEK_SET) != 0) {
  164. failed("seek");
  165. goto close;
  166. }
  167. memset(buf, 0, bytes);
  168. ret = read(fd, buf, bytes);
  169. if (ret != bytes) {
  170. failed("read");
  171. goto close;
  172. }
  173. for (i = 0; i < bytes; i++) {
  174. if (buf[i] != (unsigned char)i) {
  175. errorm("bad data");
  176. goto close;
  177. }
  178. }
  179. ret1 = 0;
  180. close:
  181. close(fd);
  182. out:
  183. free(buf);
  184. return ret1;
  185. }
  186. /**
  187. * test_rsvol - test UBI volume re-size.
  188. *
  189. * @type volume type (%UBI_DYNAMIC_VOLUME or %UBI_STATIC_VOLUME)
  190. *
  191. * Thus function returns %0 in case of success and %-1 in case of failure.
  192. */
  193. static int test_rsvol(int type)
  194. {
  195. const char *name = PROGRAM_NAME "test_rsvol:()";
  196. int alignments[] = ALIGNMENTS(dev_info.leb_size);
  197. char vol_node[strlen(UBI_VOLUME_PATTERN) + 100];
  198. struct ubi_mkvol_request req;
  199. int i;
  200. for (i = 0; i < sizeof(alignments)/sizeof(int); i++) {
  201. int leb_size;
  202. struct ubi_vol_info vol_info;
  203. req.vol_id = UBI_VOL_NUM_AUTO;
  204. req.vol_type = type;
  205. req.name = name;
  206. req.flags = 0;
  207. req.alignment = alignments[i];
  208. req.alignment -= req.alignment % dev_info.min_io_size;
  209. if (req.alignment == 0)
  210. req.alignment = dev_info.min_io_size;
  211. leb_size = dev_info.leb_size - dev_info.leb_size % req.alignment;
  212. req.bytes = MIN_AVAIL_EBS * leb_size;
  213. if (ubi_mkvol(libubi, node, &req)) {
  214. failed("ubi_mkvol");
  215. return -1;
  216. }
  217. sprintf(vol_node, UBI_VOLUME_PATTERN, dev_info.dev_num,
  218. req.vol_id);
  219. if (ubi_get_vol_info(libubi, vol_node, &vol_info)) {
  220. failed("ubi_get_vol_info");
  221. goto remove;
  222. }
  223. if (test_rsvol1(&vol_info)) {
  224. errorm("alignment = %d", req.alignment);
  225. goto remove;
  226. }
  227. if (ubi_rmvol(libubi, node, req.vol_id)) {
  228. failed("ubi_rmvol");
  229. return -1;
  230. }
  231. }
  232. return 0;
  233. remove:
  234. ubi_rmvol(libubi, node, req.vol_id);
  235. return -1;
  236. }
  237. int main(int argc, char * const argv[])
  238. {
  239. if (initial_check(argc, argv))
  240. return 1;
  241. node = argv[1];
  242. libubi = libubi_open();
  243. if (libubi == NULL) {
  244. failed("libubi_open");
  245. return 1;
  246. }
  247. if (ubi_get_dev_info(libubi, node, &dev_info)) {
  248. failed("ubi_get_dev_info");
  249. goto close;
  250. }
  251. if (test_basic(UBI_DYNAMIC_VOLUME))
  252. goto close;
  253. if (test_basic(UBI_STATIC_VOLUME))
  254. goto close;
  255. if (test_rsvol(UBI_DYNAMIC_VOLUME))
  256. goto close;
  257. if (test_rsvol(UBI_STATIC_VOLUME))
  258. goto close;
  259. libubi_close(libubi);
  260. return 0;
  261. close:
  262. libubi_close(libubi);
  263. return 1;
  264. }