PageRenderTime 73ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 1ms

/release/src/router/mdadm/Manage.c

https://gitlab.com/envieidoc/advancedtomato2
C | 436 lines | 324 code | 24 blank | 88 comment | 96 complexity | 9dfeb31628f8859795557a35487585ff MD5 | raw file
  1. /*
  2. * mdadm - manage Linux "md" devices aka RAID arrays.
  3. *
  4. * Copyright (C) 2001-2006 Neil Brown <neilb@suse.de>
  5. *
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. *
  21. * Author: Neil Brown
  22. * Email: <neilb@cse.unsw.edu.au>
  23. * Paper: Neil Brown
  24. * School of Computer Science and Engineering
  25. * The University of New South Wales
  26. * Sydney, 2052
  27. * Australia
  28. */
  29. #include "mdadm.h"
  30. #include "md_u.h"
  31. #include "md_p.h"
  32. #define REGISTER_DEV _IO (MD_MAJOR, 1)
  33. #define START_MD _IO (MD_MAJOR, 2)
  34. #define STOP_MD _IO (MD_MAJOR, 3)
  35. int Manage_ro(char *devname, int fd, int readonly)
  36. {
  37. /* switch to readonly or rw
  38. *
  39. * requires >= 0.90.0
  40. * first check that array is runing
  41. * use RESTART_ARRAY_RW or STOP_ARRAY_RO
  42. *
  43. */
  44. mdu_array_info_t array;
  45. if (md_get_version(fd) < 9000) {
  46. fprintf(stderr, Name ": need md driver version 0.90.0 or later\n");
  47. return 1;
  48. }
  49. if (ioctl(fd, GET_ARRAY_INFO, &array)) {
  50. fprintf(stderr, Name ": %s does not appear to be active.\n",
  51. devname);
  52. return 1;
  53. }
  54. if (readonly>0) {
  55. if (ioctl(fd, STOP_ARRAY_RO, NULL)) {
  56. fprintf(stderr, Name ": failed to set readonly for %s: %s\n",
  57. devname, strerror(errno));
  58. return 1;
  59. }
  60. } else if (readonly < 0) {
  61. if (ioctl(fd, RESTART_ARRAY_RW, NULL)) {
  62. fprintf(stderr, Name ": failed to set writable for %s: %s\n",
  63. devname, strerror(errno));
  64. return 1;
  65. }
  66. }
  67. return 0;
  68. }
  69. #ifndef MDASSEMBLE
  70. int Manage_runstop(char *devname, int fd, int runstop, int quiet)
  71. {
  72. /* Run or stop the array. array must already be configured
  73. * required >= 0.90.0
  74. */
  75. mdu_param_t param; /* unused */
  76. if (runstop == -1 && md_get_version(fd) < 9000) {
  77. if (ioctl(fd, STOP_MD, 0)) {
  78. if (!quiet) fprintf(stderr, Name ": stopping device %s failed: %s\n",
  79. devname, strerror(errno));
  80. return 1;
  81. }
  82. }
  83. if (md_get_version(fd) < 9000) {
  84. fprintf(stderr, Name ": need md driver version 0.90.0 or later\n");
  85. return 1;
  86. }
  87. /*
  88. if (ioctl(fd, GET_ARRAY_INFO, &array)) {
  89. fprintf(stderr, Name ": %s does not appear to be active.\n",
  90. devname);
  91. return 1;
  92. }
  93. */
  94. if (runstop>0) {
  95. if (ioctl(fd, RUN_ARRAY, &param)) {
  96. fprintf(stderr, Name ": failed to run array %s: %s\n",
  97. devname, strerror(errno));
  98. return 1;
  99. }
  100. if (quiet <= 0)
  101. fprintf(stderr, Name ": started %s\n", devname);
  102. } else if (runstop < 0){
  103. struct map_ent *map = NULL;
  104. struct stat stb;
  105. if (ioctl(fd, STOP_ARRAY, NULL)) {
  106. if (quiet==0)
  107. fprintf(stderr, Name ": fail to stop array %s: %s\n",
  108. devname, strerror(errno));
  109. return 1;
  110. }
  111. if (quiet <= 0)
  112. fprintf(stderr, Name ": stopped %s\n", devname);
  113. if (fstat(fd, &stb) == 0) {
  114. int devnum;
  115. if (major(stb.st_rdev) == MD_MAJOR)
  116. devnum = minor(stb.st_rdev);
  117. else
  118. devnum = -1-(minor(stb.st_rdev)>>6);
  119. map_delete(&map, devnum);
  120. map_write(map);
  121. map_free(map);
  122. }
  123. }
  124. return 0;
  125. }
  126. int Manage_resize(char *devname, int fd, long long size, int raid_disks)
  127. {
  128. mdu_array_info_t info;
  129. if (ioctl(fd, GET_ARRAY_INFO, &info) != 0) {
  130. fprintf(stderr, Name ": Cannot get array information for %s: %s\n",
  131. devname, strerror(errno));
  132. return 1;
  133. }
  134. if (size >= 0)
  135. info.size = size;
  136. if (raid_disks > 0)
  137. info.raid_disks = raid_disks;
  138. if (ioctl(fd, SET_ARRAY_INFO, &info) != 0) {
  139. fprintf(stderr, Name ": Cannot set device size/shape for %s: %s\n",
  140. devname, strerror(errno));
  141. return 1;
  142. }
  143. return 0;
  144. }
  145. int Manage_reconfig(char *devname, int fd, int layout)
  146. {
  147. mdu_array_info_t info;
  148. if (ioctl(fd, GET_ARRAY_INFO, &info) != 0) {
  149. fprintf(stderr, Name ": Cannot get array information for %s: %s\n",
  150. devname, strerror(errno));
  151. return 1;
  152. }
  153. info.layout = layout;
  154. printf("layout set to %d\n", info.layout);
  155. if (ioctl(fd, SET_ARRAY_INFO, &info) != 0) {
  156. fprintf(stderr, Name ": Cannot set layout for %s: %s\n",
  157. devname, strerror(errno));
  158. return 1;
  159. }
  160. return 0;
  161. }
  162. int Manage_subdevs(char *devname, int fd,
  163. mddev_dev_t devlist, int verbose)
  164. {
  165. /* do something to each dev.
  166. * devmode can be
  167. * 'a' - add the device
  168. * try HOT_ADD_DISK
  169. * If that fails EINVAL, try ADD_NEW_DISK
  170. * 'r' - remove the device HOT_REMOVE_DISK
  171. * 'f' - set the device faulty SET_DISK_FAULTY
  172. */
  173. mdu_array_info_t array;
  174. mdu_disk_info_t disc;
  175. mddev_dev_t dv;
  176. struct stat stb;
  177. int j;
  178. int tfd;
  179. struct supertype *st;
  180. void *dsuper = NULL;
  181. void *osuper = NULL; /* original super */
  182. int duuid[4];
  183. int ouuid[4];
  184. if (ioctl(fd, GET_ARRAY_INFO, &array)) {
  185. fprintf(stderr, Name ": cannot get array info for %s\n",
  186. devname);
  187. return 1;
  188. }
  189. for (dv = devlist ; dv; dv=dv->next) {
  190. unsigned long long ldsize;
  191. if (stat(dv->devname, &stb)) {
  192. fprintf(stderr, Name ": cannot find %s: %s\n",
  193. dv->devname, strerror(errno));
  194. return 1;
  195. }
  196. if ((stb.st_mode & S_IFMT) != S_IFBLK) {
  197. fprintf(stderr, Name ": %s is not a block device.\n",
  198. dv->devname);
  199. return 1;
  200. }
  201. switch(dv->disposition){
  202. default:
  203. fprintf(stderr, Name ": internal error - devmode[%s]=%d\n",
  204. dv->devname, dv->disposition);
  205. return 1;
  206. case 'a':
  207. /* add the device */
  208. st = super_by_version(array.major_version,
  209. array.minor_version);
  210. if (!st) {
  211. fprintf(stderr, Name ": unsupport array - version %d.%d\n",
  212. array.major_version, array.minor_version);
  213. return 1;
  214. }
  215. /* Make sure it isn't in use (in 2.6 or later) */
  216. tfd = open(dv->devname, O_RDONLY|O_EXCL);
  217. if (tfd < 0) {
  218. fprintf(stderr, Name ": Cannot open %s: %s\n",
  219. dv->devname, strerror(errno));
  220. return 1;
  221. }
  222. if (array.not_persistent==0)
  223. st->ss->load_super(st, tfd, &osuper, NULL);
  224. /* will use osuper later */
  225. if (!get_dev_size(tfd, dv->devname, &ldsize)) {
  226. close(tfd);
  227. return 1;
  228. }
  229. close(tfd);
  230. if (array.major_version == 0 &&
  231. md_get_version(fd)%100 < 2) {
  232. if (ioctl(fd, HOT_ADD_DISK,
  233. (unsigned long)stb.st_rdev)==0) {
  234. if (verbose >= 0)
  235. fprintf(stderr, Name ": hot added %s\n",
  236. dv->devname);
  237. continue;
  238. }
  239. fprintf(stderr, Name ": hot add failed for %s: %s\n",
  240. dv->devname, strerror(errno));
  241. return 1;
  242. }
  243. if (array.not_persistent == 0) {
  244. /* Make sure device is large enough */
  245. if (st->ss->avail_size(st, ldsize/512) <
  246. array.size) {
  247. fprintf(stderr, Name ": %s not large enough to join array\n",
  248. dv->devname);
  249. return 1;
  250. }
  251. /* need to find a sample superblock to copy, and
  252. * a spare slot to use
  253. */
  254. for (j=0; j<st->max_devs; j++) {
  255. char *dev;
  256. int dfd;
  257. disc.number = j;
  258. if (ioctl(fd, GET_DISK_INFO, &disc))
  259. continue;
  260. if (disc.major==0 && disc.minor==0)
  261. continue;
  262. if ((disc.state & 4)==0) continue; /* sync */
  263. /* Looks like a good device to try */
  264. dev = map_dev(disc.major, disc.minor, 1);
  265. if (!dev) continue;
  266. dfd = dev_open(dev, O_RDONLY);
  267. if (dfd < 0) continue;
  268. if (st->ss->load_super(st, dfd, &dsuper, NULL)) {
  269. close(dfd);
  270. continue;
  271. }
  272. remove_partitions(dfd);
  273. close(dfd);
  274. break;
  275. }
  276. if (!dsuper) {
  277. fprintf(stderr, Name ": cannot find valid superblock in this array - HELP\n");
  278. return 1;
  279. }
  280. /* Possibly this device was recently part of the array
  281. * and was temporarily removed, and is now being re-added.
  282. * If so, we can simply re-add it.
  283. */
  284. st->ss->uuid_from_super(duuid, dsuper);
  285. /* re-add doesn't work for version-1 superblocks
  286. * before 2.6.18 :-(
  287. */
  288. if (array.major_version == 1 &&
  289. get_linux_version() <= 2006018)
  290. ;
  291. else if (osuper) {
  292. st->ss->uuid_from_super(ouuid, osuper);
  293. if (memcmp(duuid, ouuid, sizeof(ouuid))==0) {
  294. /* look close enough for now. Kernel
  295. * will worry about where a bitmap
  296. * based reconstruct is possible
  297. */
  298. struct mdinfo mdi;
  299. st->ss->getinfo_super(&mdi, osuper);
  300. disc.major = major(stb.st_rdev);
  301. disc.minor = minor(stb.st_rdev);
  302. disc.number = mdi.disk.number;
  303. disc.raid_disk = mdi.disk.raid_disk;
  304. disc.state = mdi.disk.state;
  305. if (ioctl(fd, ADD_NEW_DISK, &disc) == 0) {
  306. if (verbose >= 0)
  307. fprintf(stderr, Name ": re-added %s\n", dv->devname);
  308. continue;
  309. }
  310. /* fall back on normal-add */
  311. }
  312. }
  313. } else {
  314. /* non-persistent. Must ensure that new drive
  315. * is at least array.size big.
  316. */
  317. if (ldsize/512 < array.size) {
  318. fprintf(stderr, Name ": %s not large enough to join array\n",
  319. dv->devname);
  320. return 1;
  321. }
  322. }
  323. /* in 2.6.17 and earlier, version-1 superblocks won't
  324. * use the number we write, but will choose a free number.
  325. * we must choose the same free number, which requires
  326. * starting at 'raid_disks' and counting up
  327. */
  328. for (j = array.raid_disks; j< st->max_devs; j++) {
  329. disc.number = j;
  330. if (ioctl(fd, GET_DISK_INFO, &disc))
  331. break;
  332. if (disc.major==0 && disc.minor==0)
  333. break;
  334. if (disc.state & 8) /* removed */
  335. break;
  336. }
  337. disc.major = major(stb.st_rdev);
  338. disc.minor = minor(stb.st_rdev);
  339. disc.number =j;
  340. disc.state = 0;
  341. if (array.not_persistent==0) {
  342. if (dv->writemostly)
  343. disc.state |= 1 << MD_DISK_WRITEMOSTLY;
  344. st->ss->add_to_super(dsuper, &disc);
  345. if (st->ss->write_init_super(st, dsuper, &disc, dv->devname))
  346. return 1;
  347. } else if (dv->re_add) {
  348. /* this had better be raid1.
  349. * As we are "--re-add"ing we must find a spare slot
  350. * to fill.
  351. */
  352. char *used = malloc(array.raid_disks);
  353. memset(used, 0, array.raid_disks);
  354. for (j=0; j< st->max_devs; j++) {
  355. mdu_disk_info_t disc2;
  356. disc2.number = j;
  357. if (ioctl(fd, GET_DISK_INFO, &disc2))
  358. continue;
  359. if (disc2.major==0 && disc2.minor==0)
  360. continue;
  361. if (disc2.state & 8) /* removed */
  362. continue;
  363. if (disc2.raid_disk < 0)
  364. continue;
  365. if (disc2.raid_disk > array.raid_disks)
  366. continue;
  367. used[disc2.raid_disk] = 1;
  368. }
  369. for (j=0 ; j<array.raid_disks; j++)
  370. if (!used[j]) {
  371. disc.raid_disk = j;
  372. disc.state |= (1<<MD_DISK_SYNC);
  373. break;
  374. }
  375. }
  376. if (dv->writemostly)
  377. disc.state |= (1 << MD_DISK_WRITEMOSTLY);
  378. if (ioctl(fd,ADD_NEW_DISK, &disc)) {
  379. fprintf(stderr, Name ": add new device failed for %s as %d: %s\n",
  380. dv->devname, j, strerror(errno));
  381. return 1;
  382. }
  383. if (verbose >= 0)
  384. fprintf(stderr, Name ": added %s\n", dv->devname);
  385. break;
  386. case 'r':
  387. /* hot remove */
  388. /* FIXME check that it is a current member */
  389. if (ioctl(fd, HOT_REMOVE_DISK, (unsigned long)stb.st_rdev)) {
  390. fprintf(stderr, Name ": hot remove failed for %s: %s\n",
  391. dv->devname, strerror(errno));
  392. return 1;
  393. }
  394. if (verbose >= 0)
  395. fprintf(stderr, Name ": hot removed %s\n", dv->devname);
  396. break;
  397. case 'f': /* set faulty */
  398. /* FIXME check current member */
  399. if (ioctl(fd, SET_DISK_FAULTY, (unsigned long) stb.st_rdev)) {
  400. fprintf(stderr, Name ": set device faulty failed for %s: %s\n",
  401. dv->devname, strerror(errno));
  402. return 1;
  403. }
  404. if (verbose >= 0)
  405. fprintf(stderr, Name ": set %s faulty in %s\n",
  406. dv->devname, devname);
  407. break;
  408. }
  409. }
  410. return 0;
  411. }
  412. #endif