PageRenderTime 34ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/usr.sbin/mfiutil/mfi_volume.c

https://bitbucket.org/freebsd/freebsd-base
C | 500 lines | 409 code | 48 blank | 43 comment | 93 complexity | ff85b2359c4a48cf48d495f047f79433 MD5 | raw file
  1. /*-
  2. * SPDX-License-Identifier: BSD-3-Clause
  3. *
  4. * Copyright (c) 2008, 2009 Yahoo!, Inc.
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. The names of the authors may not be used to endorse or promote
  16. * products derived from this software without specific prior written
  17. * permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  23. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  25. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  26. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  27. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  28. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  29. * SUCH DAMAGE.
  30. *
  31. * $FreeBSD$
  32. */
  33. #include <sys/types.h>
  34. #include <sys/errno.h>
  35. #include <err.h>
  36. #include <fcntl.h>
  37. #include <libutil.h>
  38. #include <stdio.h>
  39. #include <stdlib.h>
  40. #include <string.h>
  41. #include <unistd.h>
  42. #include "mfiutil.h"
  43. MFI_TABLE(top, volume);
  44. const char *
  45. mfi_ldstate(enum mfi_ld_state state)
  46. {
  47. static char buf[16];
  48. switch (state) {
  49. case MFI_LD_STATE_OFFLINE:
  50. return ("OFFLINE");
  51. case MFI_LD_STATE_PARTIALLY_DEGRADED:
  52. return ("PARTIALLY DEGRADED");
  53. case MFI_LD_STATE_DEGRADED:
  54. return ("DEGRADED");
  55. case MFI_LD_STATE_OPTIMAL:
  56. return ("OPTIMAL");
  57. default:
  58. sprintf(buf, "LSTATE 0x%02x", state);
  59. return (buf);
  60. }
  61. }
  62. void
  63. mbox_store_ldref(uint8_t *mbox, union mfi_ld_ref *ref)
  64. {
  65. mbox[0] = ref->v.target_id;
  66. mbox[1] = ref->v.reserved;
  67. mbox[2] = ref->v.seq & 0xff;
  68. mbox[3] = ref->v.seq >> 8;
  69. }
  70. int
  71. mfi_ld_get_list(int fd, struct mfi_ld_list *list, uint8_t *statusp)
  72. {
  73. return (mfi_dcmd_command(fd, MFI_DCMD_LD_GET_LIST, list,
  74. sizeof(struct mfi_ld_list), NULL, 0, statusp));
  75. }
  76. int
  77. mfi_ld_get_info(int fd, uint8_t target_id, struct mfi_ld_info *info,
  78. uint8_t *statusp)
  79. {
  80. uint8_t mbox[1];
  81. mbox[0] = target_id;
  82. return (mfi_dcmd_command(fd, MFI_DCMD_LD_GET_INFO, info,
  83. sizeof(struct mfi_ld_info), mbox, 1, statusp));
  84. }
  85. static int
  86. mfi_ld_get_props(int fd, uint8_t target_id, struct mfi_ld_props *props)
  87. {
  88. uint8_t mbox[1];
  89. mbox[0] = target_id;
  90. return (mfi_dcmd_command(fd, MFI_DCMD_LD_GET_PROP, props,
  91. sizeof(struct mfi_ld_props), mbox, 1, NULL));
  92. }
  93. static int
  94. mfi_ld_set_props(int fd, struct mfi_ld_props *props)
  95. {
  96. uint8_t mbox[4];
  97. mbox_store_ldref(mbox, &props->ld);
  98. return (mfi_dcmd_command(fd, MFI_DCMD_LD_SET_PROP, props,
  99. sizeof(struct mfi_ld_props), mbox, 4, NULL));
  100. }
  101. static int
  102. update_cache_policy(int fd, struct mfi_ld_props *old, struct mfi_ld_props *new)
  103. {
  104. int error;
  105. uint8_t changes, policy;
  106. if (old->default_cache_policy == new->default_cache_policy &&
  107. old->disk_cache_policy == new->disk_cache_policy)
  108. return (0);
  109. policy = new->default_cache_policy;
  110. changes = policy ^ old->default_cache_policy;
  111. if (changes & MR_LD_CACHE_ALLOW_WRITE_CACHE)
  112. printf("%s caching of I/O writes\n",
  113. policy & MR_LD_CACHE_ALLOW_WRITE_CACHE ? "Enabling" :
  114. "Disabling");
  115. if (changes & MR_LD_CACHE_ALLOW_READ_CACHE)
  116. printf("%s caching of I/O reads\n",
  117. policy & MR_LD_CACHE_ALLOW_READ_CACHE ? "Enabling" :
  118. "Disabling");
  119. if (changes & MR_LD_CACHE_WRITE_BACK)
  120. printf("Setting write cache policy to %s\n",
  121. policy & MR_LD_CACHE_WRITE_BACK ? "write-back" :
  122. "write-through");
  123. if (changes & (MR_LD_CACHE_READ_AHEAD | MR_LD_CACHE_READ_ADAPTIVE))
  124. printf("Setting read ahead policy to %s\n",
  125. policy & MR_LD_CACHE_READ_AHEAD ?
  126. (policy & MR_LD_CACHE_READ_ADAPTIVE ?
  127. "adaptive" : "always") : "none");
  128. if (changes & MR_LD_CACHE_WRITE_CACHE_BAD_BBU)
  129. printf("%s write caching with bad BBU\n",
  130. policy & MR_LD_CACHE_WRITE_CACHE_BAD_BBU ? "Enabling" :
  131. "Disabling");
  132. if (old->disk_cache_policy != new->disk_cache_policy) {
  133. switch (new->disk_cache_policy) {
  134. case MR_PD_CACHE_ENABLE:
  135. printf("Enabling write-cache on physical drives\n");
  136. break;
  137. case MR_PD_CACHE_DISABLE:
  138. printf("Disabling write-cache on physical drives\n");
  139. break;
  140. case MR_PD_CACHE_UNCHANGED:
  141. printf("Using default write-cache setting on physical drives\n");
  142. break;
  143. }
  144. }
  145. if (mfi_ld_set_props(fd, new) < 0) {
  146. error = errno;
  147. warn("Failed to set volume properties");
  148. return (error);
  149. }
  150. return (0);
  151. }
  152. static void
  153. stage_cache_setting(struct mfi_ld_props *props, uint8_t new_policy,
  154. uint8_t mask)
  155. {
  156. props->default_cache_policy &= ~mask;
  157. props->default_cache_policy |= new_policy;
  158. }
  159. /*
  160. * Parse a single cache directive modifying the passed in policy.
  161. * Returns -1 on a parse error and the number of arguments consumed
  162. * on success.
  163. */
  164. static int
  165. process_cache_command(int ac, char **av, struct mfi_ld_props *props)
  166. {
  167. uint8_t policy;
  168. /* I/O cache settings. */
  169. if (strcmp(av[0], "all") == 0 || strcmp(av[0], "enable") == 0) {
  170. stage_cache_setting(props, MR_LD_CACHE_ALLOW_READ_CACHE |
  171. MR_LD_CACHE_ALLOW_WRITE_CACHE,
  172. MR_LD_CACHE_ALLOW_READ_CACHE |
  173. MR_LD_CACHE_ALLOW_WRITE_CACHE);
  174. return (1);
  175. }
  176. if (strcmp(av[0], "none") == 0 || strcmp(av[0], "disable") == 0) {
  177. stage_cache_setting(props, 0, MR_LD_CACHE_ALLOW_READ_CACHE |
  178. MR_LD_CACHE_ALLOW_WRITE_CACHE);
  179. return (1);
  180. }
  181. if (strcmp(av[0], "reads") == 0) {
  182. stage_cache_setting(props, MR_LD_CACHE_ALLOW_READ_CACHE,
  183. MR_LD_CACHE_ALLOW_READ_CACHE |
  184. MR_LD_CACHE_ALLOW_WRITE_CACHE);
  185. return (1);
  186. }
  187. if (strcmp(av[0], "writes") == 0) {
  188. stage_cache_setting(props, MR_LD_CACHE_ALLOW_WRITE_CACHE,
  189. MR_LD_CACHE_ALLOW_READ_CACHE |
  190. MR_LD_CACHE_ALLOW_WRITE_CACHE);
  191. return (1);
  192. }
  193. /* Write cache behavior. */
  194. if (strcmp(av[0], "write-back") == 0) {
  195. stage_cache_setting(props, MR_LD_CACHE_WRITE_BACK,
  196. MR_LD_CACHE_WRITE_BACK);
  197. return (1);
  198. }
  199. if (strcmp(av[0], "write-through") == 0) {
  200. stage_cache_setting(props, 0, MR_LD_CACHE_WRITE_BACK);
  201. return (1);
  202. }
  203. if (strcmp(av[0], "bad-bbu-write-cache") == 0) {
  204. if (ac < 2) {
  205. warnx("cache: bad BBU setting required");
  206. return (-1);
  207. }
  208. if (strcmp(av[1], "enable") == 0)
  209. policy = MR_LD_CACHE_WRITE_CACHE_BAD_BBU;
  210. else if (strcmp(av[1], "disable") == 0)
  211. policy = 0;
  212. else {
  213. warnx("cache: invalid bad BBU setting");
  214. return (-1);
  215. }
  216. stage_cache_setting(props, policy,
  217. MR_LD_CACHE_WRITE_CACHE_BAD_BBU);
  218. return (2);
  219. }
  220. /* Read cache behavior. */
  221. if (strcmp(av[0], "read-ahead") == 0) {
  222. if (ac < 2) {
  223. warnx("cache: read-ahead setting required");
  224. return (-1);
  225. }
  226. if (strcmp(av[1], "none") == 0)
  227. policy = 0;
  228. else if (strcmp(av[1], "always") == 0)
  229. policy = MR_LD_CACHE_READ_AHEAD;
  230. else if (strcmp(av[1], "adaptive") == 0)
  231. policy = MR_LD_CACHE_READ_AHEAD |
  232. MR_LD_CACHE_READ_ADAPTIVE;
  233. else {
  234. warnx("cache: invalid read-ahead setting");
  235. return (-1);
  236. }
  237. stage_cache_setting(props, policy, MR_LD_CACHE_READ_AHEAD |
  238. MR_LD_CACHE_READ_ADAPTIVE);
  239. return (2);
  240. }
  241. /* Drive write-cache behavior. */
  242. if (strcmp(av[0], "write-cache") == 0) {
  243. if (ac < 2) {
  244. warnx("cache: write-cache setting required");
  245. return (-1);
  246. }
  247. if (strcmp(av[1], "enable") == 0)
  248. props->disk_cache_policy = MR_PD_CACHE_ENABLE;
  249. else if (strcmp(av[1], "disable") == 0)
  250. props->disk_cache_policy = MR_PD_CACHE_DISABLE;
  251. else if (strcmp(av[1], "default") == 0)
  252. props->disk_cache_policy = MR_PD_CACHE_UNCHANGED;
  253. else {
  254. warnx("cache: invalid write-cache setting");
  255. return (-1);
  256. }
  257. return (2);
  258. }
  259. warnx("cache: Invalid command");
  260. return (-1);
  261. }
  262. static int
  263. volume_cache(int ac, char **av)
  264. {
  265. struct mfi_ld_props props, new;
  266. int error, fd, consumed;
  267. uint8_t target_id;
  268. if (ac < 2) {
  269. warnx("cache: volume required");
  270. return (EINVAL);
  271. }
  272. fd = mfi_open(mfi_unit, O_RDWR);
  273. if (fd < 0) {
  274. error = errno;
  275. warn("mfi_open");
  276. return (error);
  277. }
  278. if (mfi_lookup_volume(fd, av[1], &target_id) < 0) {
  279. error = errno;
  280. warn("Invalid volume: %s", av[1]);
  281. close(fd);
  282. return (error);
  283. }
  284. if (mfi_ld_get_props(fd, target_id, &props) < 0) {
  285. error = errno;
  286. warn("Failed to fetch volume properties");
  287. close(fd);
  288. return (error);
  289. }
  290. if (ac == 2) {
  291. printf("mfi%u volume %s cache settings:\n", mfi_unit,
  292. mfi_volume_name(fd, target_id));
  293. printf(" I/O caching: ");
  294. switch (props.default_cache_policy &
  295. (MR_LD_CACHE_ALLOW_WRITE_CACHE |
  296. MR_LD_CACHE_ALLOW_READ_CACHE)) {
  297. case 0:
  298. printf("disabled\n");
  299. break;
  300. case MR_LD_CACHE_ALLOW_WRITE_CACHE:
  301. printf("writes\n");
  302. break;
  303. case MR_LD_CACHE_ALLOW_READ_CACHE:
  304. printf("reads\n");
  305. break;
  306. case MR_LD_CACHE_ALLOW_WRITE_CACHE |
  307. MR_LD_CACHE_ALLOW_READ_CACHE:
  308. printf("writes and reads\n");
  309. break;
  310. }
  311. printf(" write caching: %s\n",
  312. props.default_cache_policy & MR_LD_CACHE_WRITE_BACK ?
  313. "write-back" : "write-through");
  314. printf("write cache with bad BBU: %s\n",
  315. props.default_cache_policy &
  316. MR_LD_CACHE_WRITE_CACHE_BAD_BBU ? "enabled" : "disabled");
  317. printf(" read ahead: %s\n",
  318. props.default_cache_policy & MR_LD_CACHE_READ_AHEAD ?
  319. (props.default_cache_policy & MR_LD_CACHE_READ_ADAPTIVE ?
  320. "adaptive" : "always") : "none");
  321. printf(" drive write cache: ");
  322. switch (props.disk_cache_policy) {
  323. case MR_PD_CACHE_UNCHANGED:
  324. printf("default\n");
  325. break;
  326. case MR_PD_CACHE_ENABLE:
  327. printf("enabled\n");
  328. break;
  329. case MR_PD_CACHE_DISABLE:
  330. printf("disabled\n");
  331. break;
  332. default:
  333. printf("??? %d\n", props.disk_cache_policy);
  334. break;
  335. }
  336. if (props.default_cache_policy != props.current_cache_policy)
  337. printf(
  338. "Cache disabled due to dead battery or ongoing battery relearn\n");
  339. error = 0;
  340. } else {
  341. new = props;
  342. av += 2;
  343. ac -= 2;
  344. while (ac > 0) {
  345. consumed = process_cache_command(ac, av, &new);
  346. if (consumed < 0) {
  347. close(fd);
  348. return (EINVAL);
  349. }
  350. av += consumed;
  351. ac -= consumed;
  352. }
  353. error = update_cache_policy(fd, &props, &new);
  354. }
  355. close(fd);
  356. return (error);
  357. }
  358. MFI_COMMAND(top, cache, volume_cache);
  359. static int
  360. volume_name(int ac, char **av)
  361. {
  362. struct mfi_ld_props props;
  363. int error, fd;
  364. uint8_t target_id;
  365. if (ac != 3) {
  366. warnx("name: volume and name required");
  367. return (EINVAL);
  368. }
  369. if (strlen(av[2]) >= sizeof(props.name)) {
  370. warnx("name: new name is too long");
  371. return (ENOSPC);
  372. }
  373. fd = mfi_open(mfi_unit, O_RDWR);
  374. if (fd < 0) {
  375. error = errno;
  376. warn("mfi_open");
  377. return (error);
  378. }
  379. if (mfi_lookup_volume(fd, av[1], &target_id) < 0) {
  380. error = errno;
  381. warn("Invalid volume: %s", av[1]);
  382. close(fd);
  383. return (error);
  384. }
  385. if (mfi_ld_get_props(fd, target_id, &props) < 0) {
  386. error = errno;
  387. warn("Failed to fetch volume properties");
  388. close(fd);
  389. return (error);
  390. }
  391. printf("mfi%u volume %s name changed from \"%s\" to \"%s\"\n", mfi_unit,
  392. mfi_volume_name(fd, target_id), props.name, av[2]);
  393. bzero(props.name, sizeof(props.name));
  394. strcpy(props.name, av[2]);
  395. if (mfi_ld_set_props(fd, &props) < 0) {
  396. error = errno;
  397. warn("Failed to set volume properties");
  398. close(fd);
  399. return (error);
  400. }
  401. close(fd);
  402. return (0);
  403. }
  404. MFI_COMMAND(top, name, volume_name);
  405. static int
  406. volume_progress(int ac, char **av)
  407. {
  408. struct mfi_ld_info info;
  409. int error, fd;
  410. uint8_t target_id;
  411. if (ac != 2) {
  412. warnx("volume progress: %s", ac > 2 ? "extra arguments" :
  413. "volume required");
  414. return (EINVAL);
  415. }
  416. fd = mfi_open(mfi_unit, O_RDONLY);
  417. if (fd < 0) {
  418. error = errno;
  419. warn("mfi_open");
  420. return (error);
  421. }
  422. if (mfi_lookup_volume(fd, av[1], &target_id) < 0) {
  423. error = errno;
  424. warn("Invalid volume: %s", av[1]);
  425. close(fd);
  426. return (error);
  427. }
  428. /* Get the info for this drive. */
  429. if (mfi_ld_get_info(fd, target_id, &info, NULL) < 0) {
  430. error = errno;
  431. warn("Failed to fetch info for volume %s",
  432. mfi_volume_name(fd, target_id));
  433. close(fd);
  434. return (error);
  435. }
  436. /* Display any of the active events. */
  437. if (info.progress.active & MFI_LD_PROGRESS_CC)
  438. mfi_display_progress("Consistency Check", &info.progress.cc);
  439. if (info.progress.active & MFI_LD_PROGRESS_BGI)
  440. mfi_display_progress("Background Init", &info.progress.bgi);
  441. if (info.progress.active & MFI_LD_PROGRESS_FGI)
  442. mfi_display_progress("Foreground Init", &info.progress.fgi);
  443. if (info.progress.active & MFI_LD_PROGRESS_RECON)
  444. mfi_display_progress("Reconstruction", &info.progress.recon);
  445. if ((info.progress.active & (MFI_LD_PROGRESS_CC | MFI_LD_PROGRESS_BGI |
  446. MFI_LD_PROGRESS_FGI | MFI_LD_PROGRESS_RECON)) == 0)
  447. printf("No activity in progress for volume %s.\n",
  448. mfi_volume_name(fd, target_id));
  449. close(fd);
  450. return (0);
  451. }
  452. MFI_COMMAND(volume, progress, volume_progress);