4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License version 2 for more details. A copy is
14 * included in the COPYING file that accompanied this code.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * Copyright (c) 2017, Intel Corporation.
25 * lustre/utils/lsnapshot.c
27 * Author: Fan, Yong <fan.yong@intel.com>
37 #include <sys/types.h>
45 #include <libcfs/util/list.h>
46 #include <libcfs/util/ioctl.h>
47 #include <libcfs/util/string.h>
48 #include <linux/lustre/lustre_ioctl.h>
49 #include <linux/lustre/lustre_barrier_user.h>
53 #define SNAPSHOT_CONF_DIR "/etc/lsnapshot"
54 #define LDEV_CONF "/etc/ldev.conf"
55 #define SNAPSHOT_LOG "/var/log/lsnapshot.log"
56 #define SNAPSHOT_MAGIC "0x14F711B9"
57 #define MAX_BUF_SIZE 4096
65 struct snapshot_target {
66 struct list_head st_list;
67 /* Target node name. */
69 /* Where the pool is */
71 /* The target pool name on the target node. */
73 /* The backend filesystem name against the target pool. */
76 unsigned int st_index;
84 struct snapshot_instance {
85 struct list_head si_mdts_list;
86 struct list_head si_osts_list;
87 struct snapshot_target *si_mgs;
88 struct snapshot_target *si_mdt0;
102 static const char snapshot_rsh_default[] = "ssh";
103 static char snapshot_path[MAX_BUF_SIZE];
105 static char *snapshot_role2name(char *name, enum snapshot_role role,
109 snprintf(name, 8, "MDT%04x", index);
110 else if (role & SR_MGS)
111 snprintf(name, 4, "MGS");
113 snprintf(name, 8, "OST%04x", index);
118 #define SNAPSHOT_ADD_LOG(si, format, ...) \
120 char buf[MAX_BUF_SIZE]; \
124 memset(buf, 0, sizeof(buf)); \
126 snprintf(buf, sizeof(buf) - 1, "%s", ctime(&tt)); \
127 ptr = strrchr(buf, '\n'); \
131 fprintf(si->si_log_fp, "%s (%d:%s:%d:%s:%s): "format, buf, \
132 getpid(), __func__, __LINE__, si->si_fsname, \
133 si->si_rsh, ## __VA_ARGS__); \
137 #define DFSNAME "%s/%s"
138 #define DSSNAME "%s/%s@%s"
139 #define DZFS "%s zfs"
140 #define DIMPORT "%s zpool import -d %s %s > /dev/null 2>&1"
142 #define PRSH(si, st) (si)->si_rsh, (st)->st_host
143 #define PFSNAME(st) (st)->st_pool, (st)->st_filesystem
144 #define PSSNAME(si, st) PFSNAME(st), (si)->si_ssname
145 #define PSS_NEW(si, st) PFSNAME(st), (si)->si_new_ssname
146 #define PZFS(st) snapshot_path
147 #define PIMPORT(st) snapshot_path, \
148 (st)->st_dir ? (st)->st_dir : "/dev -d /tmp", (st)->st_pool
150 char *snapshot_fgets(FILE *fp, char *buf, int buflen)
154 memset(buf, 0, buflen);
155 if (fgets(buf, buflen, fp) == NULL)
158 ptr = strchr(buf, '\n');
165 static int snapshot_exec(const char *cmd)
171 /* system() return value depends on both the system() general framework,
172 * such as whether fork()/exec() success or fail, and the real @cmd exec
173 * result. Especially, if the @cmd is remote command, we may cannot know
174 * the real failure. */
176 /* fork()/exec() error */
178 return errno != 0 ? -errno : -1;
181 rc = WEXITSTATUS(rc);
184 } else if (WIFSIGNALED(rc)) {
187 /* all other known or unknown cases. */
194 static int snapshot_load_conf_ldev(struct snapshot_instance *si, char *buf,
195 struct snapshot_target *st, char **role)
205 rc = sscanf(buf, "%ms %ms %ms %ms",
206 &st->st_host, &ignore, &label, &device);
215 * [md|zfs:][pool_dir/]<pool>/<filesystem> */
216 ptr = strchr(device, ':');
219 if (strncmp(device, "zfs:", strlen("zfs:")) != 0) {
227 ptr1 = strrchr(ptr, '/');
228 /* "ptr1 - ptr + 1 == strlen(ptr)" means '/' is at the tail. */
229 if (!ptr1 || ptr1 == ptr || ptr1 - ptr + 1 == strlen(ptr)) {
235 st->st_filesystem = malloc(len);
236 if (!st->st_filesystem) {
242 strncpy(st->st_filesystem, ptr1 + 1, len - 1);
243 st->st_filesystem[len - 1] = '\0';
246 ptr1 = strrchr(ptr, '/');
248 st->st_dir = strdup(ptr);
256 st->st_pool = strdup(ptr);
263 * fsname-<role><index> or <role><index> */
264 ptr = strrchr(label, '-');
266 if (strncmp(si->si_fsname, label, ptr - label) != 0) {
267 /* This line is NOT for current filesystem .*/
277 if (strlen(ptr) < 3 || strlen(ptr) > 7) {
288 strncpy(*role, ptr, 3);
292 while (isxdigit(ptr[len])) {
293 if (isdigit(ptr[len]))
295 st->st_index * 16 + ptr[len] - '0';
296 else if (isupper(ptr[len]))
298 st->st_index * 16 + ptr[len] - 'A' + 10;
301 st->st_index * 16 + ptr[len] - 'a' + 10;
306 if (strncasecmp(*role, "MGS", 3) != 0)
314 if (!isxdigit(ptr[len]) && ptr[len] != '\0') {
329 * For old snasphot tools, the configration is in /etc/lsnapshot/${fsname}.conf,
331 * <host> <pool_dir> <pool> <local_fsname> <role(,s)> <index>
335 * host-mdt1 /tmp myfs-mdt1 mdt1 MGS,MDT 0
336 * host-mdt2 /tmp myfs-mdt2 mdt2 MDT 1
337 * host-ost1 /tmp myfs-ost1 ost1 OST 0
338 * host-ost2 /tmp myfs-ost2 ost2 OST 1
341 * For new snasphot tools, the configration is in /etc/ldev.conf, which is not
342 * only for snapshot, but also for other purpose. The format is:
343 * <host> foreign/- <label> <device> [journal-path]/- [raidtab]
345 * The format of <label> is:
346 * fsname-<role><index> or <role><index>
348 * The format of <device> is:
349 * [md|zfs:][pool_dir/]<pool>/<filesystem>
351 * Snapshot only uses the fields <host>, <label> and <device>.
355 * host-mdt1 - myfs-MDT0000 zfs:/tmp/myfs-mdt1/mdt1
358 * \retval 0 for success
359 * \retval +ve the line# with which the current line is conflict
360 * \retval -EAGAIN skip current line
361 * \retval -ve other failures
363 static int snapshot_load_conf_one(struct snapshot_instance *si,
364 char *buf, int line_num, bool is_ldev)
366 struct snapshot_target *st;
371 path = getenv("PATH");
375 memset(snapshot_path, 0, sizeof(snapshot_path));
376 snprintf(snapshot_path, sizeof(snapshot_path) - 1, "PATH='%s'", path);
378 /* filter out space */
379 while (isspace(*buf))
382 /* skip empty line */
386 /* skip comment line */
390 st = malloc(sizeof(*st));
394 memset(st, 0, sizeof(*st));
395 INIT_LIST_HEAD(&st->st_list);
398 rc = snapshot_load_conf_ldev(si, buf, st, &role);
400 rc = sscanf(buf, "%ms %ms %ms %ms %ms %d",
401 &st->st_host, &st->st_dir, &st->st_pool,
402 &st->st_filesystem, &role, &st->st_index);
411 if (strncasecmp(role, "MGS", 3) == 0) {
412 st->st_role = SR_MGS;
413 if (role[3] == ',') {
415 if (strncasecmp(&role[4], "MDT", 3) != 0) {
420 st->st_role |= SR_MDT;
422 } else if (strncasecmp(role, "MDT", 3) == 0) {
423 st->st_role = SR_MDT;
424 if (role[3] == ',') {
426 if (strncasecmp(&role[4], "MGS", 3) != 0) {
431 st->st_role |= SR_MGS;
433 } else if (strncasecmp(role, "OST", 3) == 0) {
434 st->st_role = SR_OST;
440 st->st_line = line_num;
441 if (st->st_role & SR_MDT) {
442 /* MGS is the first, MDT0 is just after the MGS
443 * if they are not combined together. */
444 if (st->st_role & SR_MGS) {
446 rc = si->si_mgs->st_line;
451 list_add(&st->st_list, &si->si_mdts_list);
454 if (st->st_index == 0) {
456 rc = si->si_mdt0->st_line;
461 if (list_empty(&st->st_list)) {
462 if (list_empty(&si->si_mdts_list) ||
464 list_add(&st->st_list,
467 list_add(&st->st_list,
468 &si->si_mgs->st_list);
470 } else if (list_empty(&st->st_list)) {
471 list_add_tail(&st->st_list, &si->si_mdts_list);
473 } else if (st->st_role & SR_MGS) {
475 rc = si->si_mgs->st_line;
480 list_add(&st->st_list, &si->si_mdts_list);
482 list_add_tail(&st->st_list, &si->si_osts_list);
496 if (st->st_filesystem)
497 free(st->st_filesystem);
504 static int snapshot_load_conf(struct snapshot_instance *si, int lock_mode)
507 char buf[MAX_BUF_SIZE];
514 memset(conf_name, 0, sizeof(conf_name));
515 strncpy(conf_name, LDEV_CONF, sizeof(conf_name) - 1);
516 fd = open(conf_name, O_RDONLY);
518 if (errno != ENOENT) {
520 "Can't open the snapshot config file %s: %s\n",
521 conf_name, strerror(errno));
526 snprintf(conf_name, sizeof(conf_name) - 1, "%s/%s.conf",
527 SNAPSHOT_CONF_DIR, si->si_fsname);
528 fd = open(conf_name, O_RDONLY);
531 "Can't open the snapshot config file %s: %s\n",
532 conf_name, strerror(errno));
540 rc = flock(fd, lock_mode | LOCK_NB);
543 "Can't lock the snapshot config file %s (%d): %s\n",
544 conf_name, lock_mode, strerror(errno));
549 fp = fdopen(fd, "r");
552 "Can't fdopen the snapshot config file %s: %s\n",
553 conf_name, strerror(errno));
558 while (snapshot_fgets(fp, buf, MAX_BUF_SIZE) != NULL) {
559 rc = snapshot_load_conf_one(si, buf, line_num, is_ldev);
562 "Invalid snapshot config file %s at the line "
563 "%d '%s'\n", conf_name, line_num, buf);
564 } else if (rc == -EAGAIN) {
568 "The config role has been specified repeatedly "
569 "at the lines %d/%d in %s\n",
570 rc, line_num, conf_name);
582 "Miss MDT0 in the config file %s\n",
588 /* By default, the MGS is on the MDT0 if it is not specified. */
590 si->si_mgs = si->si_mdt0;
591 si->si_mgs->st_role |= SR_MGS;
594 if (list_empty(&si->si_osts_list)) {
596 "Miss OST(s) in the config file %s\n",
615 static void snapshot_unload_conf(struct snapshot_instance *si)
617 struct snapshot_target *st;
619 while (!list_empty(&si->si_mdts_list)) {
620 st = list_entry(si->si_mdts_list.next,
621 struct snapshot_target, st_list);
622 list_del(&st->st_list);
626 free(st->st_filesystem);
630 while (!list_empty(&si->si_osts_list)) {
631 st = list_entry(si->si_osts_list.next,
632 struct snapshot_target, st_list);
633 list_del(&st->st_list);
637 free(st->st_filesystem);
644 if (si->si_conf_fd >= 0) {
645 flock(si->si_conf_fd, LOCK_UN);
646 close(si->si_conf_fd);
651 static int snapshot_handle_string_option(char **dst, const char *option,
652 const char *opt_name)
654 if (*dst && *dst != snapshot_rsh_default) {
656 "%s option has been specified repeatedly.\n", opt_name);
660 *dst = strdup(option);
666 static void snapshot_fini(struct snapshot_instance *si)
668 snapshot_unload_conf(si);
671 fclose(si->si_log_fp);
673 if (si->si_rsh && si->si_rsh != snapshot_rsh_default)
679 if (si->si_new_ssname)
680 free(si->si_new_ssname);
682 free(si->si_comment);
687 static struct snapshot_instance *
688 snapshot_init(int argc, char * const argv[], const struct option *longopts,
689 const char *optstring, void (*usage)(void),
690 int lock_mode, int *err)
692 struct snapshot_instance *si;
697 si = malloc(sizeof(*si));
700 "No enough memory to initialize snapshot instance.\n");
705 memset(si, 0, sizeof(*si));
706 INIT_LIST_HEAD(&si->si_mdts_list);
707 INIT_LIST_HEAD(&si->si_osts_list);
708 si->si_rsh = (char *)snapshot_rsh_default;
710 si->si_timeout = BARRIER_TIMEOUT_DEFAULT;
711 si->si_barrier = true;
712 si->si_detail = false;
713 si->si_force = false;
715 while ((opt = getopt_long(argc, argv, optstring,
716 longopts, &idx)) != EOF) {
719 if (!optarg || strcmp(optarg, "on") == 0) {
720 si->si_barrier = true;
721 } else if (strcmp(optarg, "off") == 0) {
722 si->si_barrier = false;
730 *err = snapshot_handle_string_option(&si->si_comment,
736 si->si_detail = true;
742 *err = snapshot_handle_string_option(&si->si_fsname,
748 *err = snapshot_handle_string_option(&si->si_ssname,
754 *err = snapshot_handle_string_option(&si->si_new_ssname,
761 *err = snapshot_handle_string_option(&si->si_rsh,
768 si->si_timeout = atoi(optarg);
782 if (!si->si_fsname) {
783 fprintf(stderr, "The fsname must be specified\n");
789 if (strlen(si->si_fsname) > 8) {
790 fprintf(stderr, "Invalid fsname %s\n", si->si_fsname);
795 si->si_log_fp = fopen(SNAPSHOT_LOG, "a");
796 if (!si->si_log_fp) {
799 "Can't open the snapshot log file %s: %s\n",
800 SNAPSHOT_LOG, strerror(errno));
804 *err = snapshot_load_conf(si, lock_mode);
807 if (*err != 0 && si) {
815 static int __snapshot_wait(struct snapshot_instance *si,
816 struct list_head *head, int *err)
818 struct snapshot_target *st;
822 list_for_each_entry(st, head, st_list) {
826 rc = waitpid(st->st_pid, &st->st_status, 0);
828 SNAPSHOT_ADD_LOG(si, "Can't wait child (%d) operation "
829 "on the target <%s:%x:%d>: %s\n",
830 st->st_pid, st->st_host, st->st_role,
831 st->st_index, strerror(errno));
837 /* continue to wait for next */
841 if (WIFEXITED(st->st_status)) {
842 rc = WEXITSTATUS(st->st_status);
847 st->st_ignored = true;
853 } else if (WIFSIGNALED(st->st_status)) {
854 SNAPSHOT_ADD_LOG(si, "The child (%d) operation on the "
855 "target <%s:%x:%d> was killed by "
857 st->st_pid, st->st_host, st->st_role,
858 st->st_index, WTERMSIG(st->st_status));
863 SNAPSHOT_ADD_LOG(si, "The child (%d) operation on the "
864 "target <%s:%x:%d> failed for "
866 st->st_pid, st->st_host, st->st_role,
879 static int snapshot_wait(struct snapshot_instance *si, int *err)
883 count = __snapshot_wait(si, &si->si_mdts_list, err);
884 count += __snapshot_wait(si, &si->si_osts_list, err);
889 static char *snapshot_first_skip_blank(char *buf)
893 ptr = strchr(buf, ' ');
895 ptr = strchr(buf, '\t');
900 while (*ptr == ' ' || *ptr == '\t')
909 static int mdt0_is_lustre_snapshot(struct snapshot_instance *si)
911 struct snapshot_target *st = si->si_mdt0;
912 char buf[MAX_BUF_SIZE * 3];
916 memset(buf, 0, sizeof(buf));
917 snprintf(buf, sizeof(buf) - 1,
918 DRSH" '"DIMPORT"; "DZFS
919 " get -H -o value lustre:magic "DSSNAME"'",
920 PRSH(si, st), PIMPORT(st), PZFS(st), PSSNAME(si, st));
921 fp = popen(buf, "r");
923 SNAPSHOT_ADD_LOG(si, "Popen fail to check snapshot "
924 "on mdt0: %s\n", strerror(errno));
928 if (snapshot_fgets(fp, buf, strlen(SNAPSHOT_MAGIC) + 1) == NULL) {
930 } else if (strcmp(buf, SNAPSHOT_MAGIC) == 0) {
934 "The target %s is not Lustre snapshot "
935 "or it does not exists\n", si->si_ssname);
943 static int target_is_mounted(struct snapshot_instance *si,
944 struct snapshot_target *st, const char *ssname)
946 char buf[MAX_BUF_SIZE];
947 char fullname[MAX_BUF_SIZE];
952 memset(buf, 0, sizeof(buf));
953 snprintf(buf, sizeof(buf) - 1,
956 fp = popen(buf, "r");
958 SNAPSHOT_ADD_LOG(si, "Popen fail to check target mount: %s\n",
963 memset(fullname, 0, sizeof(fullname));
965 snprintf(fullname, sizeof(fullname) - 1,
967 PFSNAME(st), ssname);
969 snprintf(fullname, sizeof(fullname) - 1,
973 while (snapshot_fgets(fp, buf, sizeof(buf)) != NULL) {
974 ptr = strstr(buf, fullname);
978 ptr += strlen(fullname) + 1; /* mount point */
979 if (ptr >= buf + strlen(buf))
982 ptr = strstr(ptr, "type lustre");
993 static int snapshot_get_fsname(struct snapshot_instance *si,
994 char *fsname, int fslen)
996 struct snapshot_target *st = si->si_mdt0;
997 char buf[MAX_BUF_SIZE * 3];
1001 memset(buf, 0, sizeof(buf));
1002 snprintf(buf, sizeof(buf) - 1,
1003 DRSH" '"DIMPORT"; "DZFS
1004 " get -H -o value lustre:fsname "DSSNAME"'",
1005 PRSH(si, st), PIMPORT(st), PZFS(st), PSSNAME(si, st));
1006 fp = popen(buf, "r");
1008 SNAPSHOT_ADD_LOG(si, "Popen fail to get fsname: %s\n",
1013 if (snapshot_fgets(fp, fsname, fslen) == NULL)
1020 static int snapshot_get_mgsnode(struct snapshot_instance *si,
1021 char *node, int size)
1023 char buf[MAX_BUF_SIZE * 2];
1024 struct snapshot_target *st;
1028 st = list_entry(si->si_osts_list.next, struct snapshot_target,
1030 memset(buf, 0, sizeof(buf));
1031 snprintf(buf, sizeof(buf) - 1,
1032 DRSH" '"DZFS" get -H -o value lustre:mgsnode "DFSNAME"'",
1033 PRSH(si, st), PZFS(st), PFSNAME(st));
1034 fp = popen(buf, "r");
1036 SNAPSHOT_ADD_LOG(si, "Popen fail to get mgsnode: %s\n",
1041 if (snapshot_fgets(fp, node, size) == NULL)
1048 static int snapshot_exists_check(struct snapshot_instance *si,
1049 struct snapshot_target *st)
1051 char buf[MAX_BUF_SIZE * 2];
1055 memset(buf, 0, sizeof(buf));
1056 snprintf(buf, sizeof(buf) - 1,
1057 DRSH" '"DZFS" list "DSSNAME" 2>/dev/null'",
1058 PRSH(si, st), PZFS(st), PSSNAME(si, st));
1059 fp = popen(buf, "r");
1061 SNAPSHOT_ADD_LOG(si, "Popen fail to create check: %s\n",
1066 if (snapshot_fgets(fp, buf, sizeof(buf)) != NULL)
1073 static int snapshot_general_check(struct snapshot_instance *si)
1075 return mdt0_is_lustre_snapshot(si);
1078 static void snapshot_create_usage(void)
1081 "Create snapshot for the given filesystem.\n"
1083 "snapshot_create [-b | --barrier [on | off]] "
1084 "[-c | --comment comment] "
1085 "<-F | --fsname fsname> "
1086 "[-h | --help] <-n | --name ssname> "
1087 "[-r | --rsh remote_shell]"
1088 "[-t | --timeout timeout]\n"
1090 "-b: set write barrier before creating snapshot, "
1091 "the default value is 'on'.\n"
1092 "-c: describe what the snapshot is for, and so on.\n"
1093 "-F: the filesystem name.\n"
1094 "-h: for help information.\n"
1095 "-n: the snapshot's name.\n"
1096 "-r: the remote shell used for communication with remote "
1097 "target, the default value is 'ssh'.\n"
1098 "-t: the life cycle (seconds) for write barrier, "
1099 "the default value is %d seconds.\n",
1100 BARRIER_TIMEOUT_DEFAULT);
1103 static int snapshot_create_check(struct snapshot_instance *si)
1107 rc = snapshot_exists_check(si, si->si_mdt0);
1109 fprintf(stderr, "The snapshot %s exists\n", si->si_ssname);
1114 static int snapshot_inherit_prop(struct snapshot_instance *si,
1115 struct snapshot_target *st,
1116 char *cmd, int size)
1118 char buf[MAX_BUF_SIZE * 3];
1123 memset(buf, 0, sizeof(buf));
1124 snprintf(buf, sizeof(buf) - 1,
1125 DRSH" \""DIMPORT"; "DZFS
1126 " get all "DFSNAME" | grep lustre: | grep local$ | "
1127 "awk '{ \\$1=\\\"\\\"; \\$NF=\\\"\\\"; print \\$0 }' | "
1128 "sed -e 's/^ //'\"",
1129 PRSH(si, st), PIMPORT(st), PZFS(st), PFSNAME(st));
1130 fp = popen(buf, "r");
1132 SNAPSHOT_ADD_LOG(si, "Popen fail to list one: %s\n",
1137 while (snapshot_fgets(fp, buf, MAX_BUF_SIZE) != NULL) {
1141 if (strncmp(buf, "lustre:fsname",
1142 strlen("lustre:fsname")) == 0)
1145 if (strncmp(buf, "lustre:magic",
1146 strlen("lustre:magic")) == 0)
1149 if (strncmp(buf, "lustre:ctime",
1150 strlen("lustre:ctime")) == 0)
1153 if (strncmp(buf, "lustre:mtime",
1154 strlen("lustre:mtime")) == 0)
1157 if (strncmp(buf, "lustre:comment",
1158 strlen("lustre:comment")) == 0)
1161 if (strncmp(buf, "lustre:svname",
1162 strlen("lustre:svname")) == 0)
1165 if (strncmp(buf, "lustre:mgsnode",
1166 strlen("lustre:mgsnode")) == 0)
1169 ptr = strchr(buf, ' ');
1171 ptr = strchr(buf, '\t');
1178 while (*ptr == ' ' || *ptr == '\t')
1184 end = strchr(ptr, ' ');
1186 end = strchr(buf, '\t');
1190 rc = scnprintf(cmd + len, size - len - 1,
1191 "-o %s=\"%s\" ", buf, ptr);
1202 static int __snapshot_create(struct snapshot_instance *si,
1203 struct list_head *head, const char *fsname,
1204 const char *mgsnode, __u64 xtime)
1206 struct snapshot_target *st;
1210 list_for_each_entry(st, head, st_list) {
1217 SNAPSHOT_ADD_LOG(si, "Can't fork for create snapshot "
1218 "(%s@%s <%s>) on the target "
1220 fsname, si->si_ssname,
1221 si->si_comment, st->st_host,
1222 st->st_role, st->st_index,
1229 char cmd[MAX_BUF_SIZE];
1232 memset(cmd, 0, sizeof(cmd));
1233 len = scnprintf(cmd, sizeof(cmd) - 1,
1234 DRSH" '"DZFS" snapshot "
1235 "-o lustre:fsname=%s "
1236 "-o lustre:magic=%s "
1237 "-o lustre:ctime=%llu "
1238 "-o lustre:mtime=%llu ",
1239 PRSH(si, st), PZFS(st), fsname,
1240 SNAPSHOT_MAGIC, xtime, xtime);
1244 if (si->si_comment) {
1245 rc = scnprintf(cmd + len, sizeof(cmd) - len - 1,
1246 "-o lustre:comment=\"%s\" ",
1254 /* Make the inherited properties as local ones,
1255 * then even if others changed (or removed) the
1256 * property of the parent dataset, the snapshot
1257 * will not be affected. */
1258 rc = snapshot_inherit_prop(si, st, cmd + len,
1259 MAX_BUF_SIZE - len - 1);
1261 SNAPSHOT_ADD_LOG(si, "Can't filter property on "
1262 "target (%s:%x:%d): rc = %d\n",
1263 st->st_host, st->st_role,
1270 if (st->st_role & SR_OST)
1271 rc = snprintf(cmd + len, sizeof(cmd) - len - 1,
1272 "-o lustre:svname=%s-OST%04x "
1273 "-o lustre:mgsnode=%s "DSSNAME"'",
1274 fsname, st->st_index, mgsnode,
1276 else if (!(st->st_role & SR_MGS) ||
1277 /* MGS is on MDT0 */
1278 si->si_mdt0 == si->si_mgs)
1279 rc = snprintf(cmd + len, sizeof(cmd) - len - 1,
1280 "-o lustre:svname=%s-MDT%04x "
1281 "-o lustre:mgsnode=%s "DSSNAME"'",
1282 fsname, st->st_index, mgsnode,
1286 rc = snprintf(cmd + len, sizeof(cmd) - len - 1,
1287 DSSNAME"'", PSSNAME(si, st));
1291 rc = snapshot_exec(cmd);
1293 SNAPSHOT_ADD_LOG(si, "Can't execute \"%s\" on "
1294 "target (%s:%x:%d): rc = %d\n",
1295 cmd, st->st_host, st->st_role,
1299 } /* end of child */
1301 /* parent continue to run more snapshot commands in parallel. */
1308 static int __snapshot_destroy(struct snapshot_instance *si,
1309 struct list_head *head);
1311 static int snapshot_create(struct snapshot_instance *si)
1314 char buf[MAX_BUF_SIZE];
1321 rc = snapshot_create_check(si);
1325 rc = gettimeofday(&tv, NULL);
1329 srandom(tv.tv_usec);
1330 snprintf(new_fsname, sizeof(new_fsname), "%08x", (__u32)random());
1332 rc = snapshot_get_mgsnode(si, buf, sizeof(buf));
1336 __argv[1] = si->si_fsname;
1337 /* 1. Get barrier */
1338 if (si->si_barrier) {
1341 memset(tbuf, 0, sizeof(tbuf));
1342 snprintf(tbuf, 7, "%u", si->si_timeout);
1343 __argv[0] = "barrier_freeze";
1345 rc = jt_barrier_freeze(3, __argv);
1347 SNAPSHOT_ADD_LOG(si, "Can't set barrier within %u "
1348 "seconds on %s: rc = %d\n",
1349 si->si_timeout, si->si_fsname, rc);
1355 /* 2. Fork config llog on MGS */
1356 __argv[0] = "fork_lcfg";
1357 __argv[2] = new_fsname;
1358 rc = jt_lcfg_fork(3, __argv);
1360 SNAPSHOT_ADD_LOG(si, "Can't fork config log for create "
1361 "snapshot %s from %s to %s: rc = %d\n",
1362 si->si_ssname, si->si_fsname, new_fsname, rc);
1366 /* 3.1 Create snapshot on every MDT */
1367 rc = __snapshot_create(si, &si->si_mdts_list, new_fsname, buf,
1370 /* 3.2 Create snapshot on every OST */
1371 rc = __snapshot_create(si, &si->si_osts_list, new_fsname, buf,
1374 /* 4. Wait for all children, even though part of them maybe failed */
1375 snapshot_wait(si, &rc1);
1378 /* 5. Put barrier */
1379 if (si->si_barrier) {
1381 struct barrier_ctl bc;
1383 rc = __jt_barrier_stat(__argv[1], &bc);
1385 SNAPSHOT_ADD_LOG(si, "Can't get barrier status "
1388 } else if (bc.bc_status != BS_FROZEN ||
1389 bc.bc_timeout <= 0) {
1390 SNAPSHOT_ADD_LOG(si, "The barrier expired "
1391 "on %s\n", si->si_fsname);
1396 __argv[0] = "barrier_thaw";
1397 rc2 = jt_barrier_thaw(2, __argv);
1399 SNAPSHOT_ADD_LOG(si, "Can't release barrier on %s: "
1400 "rc = %d\n", si->si_fsname, rc2);
1405 si->si_force = true;
1406 __snapshot_destroy(si, &si->si_osts_list);
1407 __snapshot_destroy(si, &si->si_mdts_list);
1408 snapshot_wait(si, &rc2);
1410 __argv[0] = "erase_lcfg";
1411 __argv[1] = new_fsname;
1413 jt_lcfg_erase(3, __argv);
1416 return rc ? rc : (rc1 ? rc1 : rc2);
1419 int jt_snapshot_create(int argc, char **argv)
1421 struct snapshot_instance *si;
1422 struct option long_opts[] = {
1423 { .val = 'b', .name = "barrier", .has_arg = optional_argument },
1424 { .val = 'c', .name = "comment", .has_arg = required_argument },
1425 { .val = 'F', .name = "fsname", .has_arg = required_argument },
1426 { .val = 'h', .name = "help", .has_arg = no_argument },
1427 { .val = 'n', .name = "name", .has_arg = required_argument },
1428 { .val = 'r', .name = "rsh", .has_arg = required_argument },
1429 { .val = 't', .name = "timeout", .has_arg = required_argument },
1433 si = snapshot_init(argc, argv, long_opts, "b::c:F:hn:r:t:",
1434 snapshot_create_usage, LOCK_EX, &rc);
1438 if (!si->si_ssname) {
1440 "Miss the snapshot name to be created\n");
1441 snapshot_create_usage();
1446 rc = snapshot_create(si);
1449 "Can't create the snapshot %s\n", si->si_ssname);
1450 SNAPSHOT_ADD_LOG(si, "Can't create snapshot %s with "
1451 "comment <%s> barrier <%s>, timeout "
1453 si->si_ssname, si->si_comment,
1454 si->si_barrier ? "enable" : "disable",
1455 si->si_barrier ? si->si_timeout : -1, rc);
1457 SNAPSHOT_ADD_LOG(si, "Create snapshot %s successfully "
1458 "with comment <%s>, barrier <%s>, "
1460 si->si_ssname, si->si_comment,
1461 si->si_barrier ? "enable" : "disable",
1462 si->si_barrier ? si->si_timeout : -1);
1469 static void snapshot_destroy_usage(void)
1472 "Destroy the specified snapshot.\n"
1474 "snapshot_destroy [-f | --force] "
1475 "<-F | --fsname fsname> [-h | --help] "
1476 "<-n | --name ssname> "
1477 "[-r | --rsh remote_shell]\n"
1479 "-f: destroy the snapshot by force.\n"
1480 "-F: the filesystem name.\n"
1481 "-h: for help information.\n"
1482 "-n: the snapshot's name.\n"
1483 "-r: the remote shell used for communication with remote "
1484 "target, the default value is 'ssh'.\n");
1487 static int snapshot_destroy_check(struct snapshot_instance *si)
1489 struct list_head *head = &si->si_osts_list;
1490 struct snapshot_target *st;
1495 list_for_each_entry(st, head, st_list) {
1502 SNAPSHOT_ADD_LOG(si, "Can't fork for check snapshot "
1503 "%s on the target (%s:%x:%d): %s\n",
1504 si->si_ssname, st->st_host,
1505 st->st_role, st->st_index,
1512 rc = snapshot_exists_check(si, st);
1514 /* The snapshot piece does not exist */
1517 exit(rc == -EEXIST ? 0: rc);
1518 } /* end of child */
1520 /* parent continue to run more snapshot commands in parallel. */
1524 if (head == &si->si_osts_list) {
1525 head = &si->si_mdts_list;
1529 snapshot_wait(si, &rc);
1533 head = &si->si_osts_list;
1536 list_for_each_entry(st, head, st_list) {
1537 if (st->st_ignored && !si->si_force) {
1540 snapshot_role2name(name, st->st_role, st->st_index);
1542 "Miss snapshot piece on the %s. Use '-f' "
1543 "option if want to destroy it by force.\n",
1550 if (head == &si->si_osts_list) {
1551 head = &si->si_mdts_list;
1556 rc = snapshot_general_check(si);
1561 static int __snapshot_destroy(struct snapshot_instance *si,
1562 struct list_head *head)
1564 struct snapshot_target *st;
1568 list_for_each_entry(st, head, st_list) {
1577 SNAPSHOT_ADD_LOG(si, "Can't fork for destroy snapshot "
1578 "%s on the target (%s:%x:%d): %s\n",
1579 si->si_ssname, st->st_host,
1580 st->st_role, st->st_index,
1587 char cmd[MAX_BUF_SIZE * 2];
1589 memset(cmd, 0, sizeof(cmd));
1591 snprintf(cmd, sizeof(cmd) - 1,
1592 DRSH" 'umount -f "DSSNAME
1593 " > /dev/null 2>&1; "DZFS
1594 " destroy -f "DSSNAME"'",
1595 PRSH(si, st), PSSNAME(si, st),
1596 PZFS(st), PSSNAME(si, st));
1598 snprintf(cmd, sizeof(cmd) - 1,
1599 DRSH" '"DZFS" destroy "DSSNAME"'",
1600 PRSH(si, st), PZFS(st),
1602 rc = snapshot_exec(cmd);
1604 SNAPSHOT_ADD_LOG(si, "Can't execute \"%s\" on "
1605 "target (%s:%x:%d): rc = %d\n",
1606 cmd, st->st_host, st->st_role,
1610 } /* end of child */
1612 /* parent continue to run more snapshot commands in parallel. */
1619 static int snapshot_destroy(struct snapshot_instance *si)
1627 rc = snapshot_destroy_check(si);
1631 rc = snapshot_get_fsname(si, fsname, sizeof(fsname));
1635 /* 1.1 Destroy snapshot on every OST */
1636 rc = __snapshot_destroy(si, &si->si_osts_list);
1637 if (!si->si_force) {
1641 __snapshot_wait(si, &si->si_osts_list, &rc);
1646 /* 1.2 Destroy snapshot on every MDT */
1647 rc1 = __snapshot_destroy(si, &si->si_mdts_list);
1649 /* 2 Wait for all children, even though part of them maybe failed */
1650 snapshot_wait(si, &rc2);
1651 if (rc2 == -ENOENT && si->si_force)
1654 /* 3. Erase config llog from MGS */
1655 if ((!rc && !rc1 && !rc2) || si->si_force) {
1658 __argv[0] = "erase_lcfg";
1661 rc3 = jt_lcfg_erase(3, __argv);
1662 if (rc3 && errno == ENOENT)
1665 SNAPSHOT_ADD_LOG(si, "Can't erase config for destroy "
1666 "snapshot %s, fsname %s: rc = %d\n",
1667 si->si_ssname, fsname, rc3);
1670 return rc ? rc : (rc1 ? rc1 : (rc2 ? rc2 : rc3));
1673 int jt_snapshot_destroy(int argc, char **argv)
1675 struct snapshot_instance *si;
1676 struct option long_opts[] = {
1677 { .val = 'f', .name = "force", .has_arg = no_argument },
1678 { .val = 'F', .name = "fsname", .has_arg = required_argument },
1679 { .val = 'h', .name = "help", .has_arg = no_argument },
1680 { .val = 'n', .name = "name", .has_arg = required_argument },
1681 { .val = 'r', .name = "rsh", .has_arg = required_argument },
1685 si = snapshot_init(argc, argv, long_opts, "fF:hn:r:",
1686 snapshot_destroy_usage, LOCK_EX, &rc);
1690 if (!si->si_ssname) {
1692 "Miss the snapshot name to be destroyed\n");
1693 snapshot_destroy_usage();
1698 rc = snapshot_destroy(si);
1701 "Can't destroy the snapshot %s\n", si->si_ssname);
1702 SNAPSHOT_ADD_LOG(si, "Can't destroy snapshot %s with "
1703 "force <%s>: %d\n", si->si_ssname,
1704 si->si_force ? "enable" : "disable", rc);
1706 SNAPSHOT_ADD_LOG(si, "Destroy snapshot %s successfully "
1707 "with force <%s>\n", si->si_ssname,
1708 si->si_force ? "enable" : "disable");
1715 static void snapshot_modify_usage(void)
1718 "Change the specified snapshot's name and/or comment.\n"
1720 "snapshot_modify [-c | --comment comment] "
1721 "<-F | --fsname fsname> [-h | --help] "
1722 "<-n | --name ssname> [-N | --new new_ssname] "
1723 "[-r | --rsh remote_shell]\n"
1725 "-c: update the snapshot's comment.\n"
1726 "-F: the filesystem name.\n"
1727 "-h: for help information.\n"
1728 "-n: the snapshot's name.\n"
1729 "-N: rename the snapshot's name as the new_ssname.\n"
1730 "-r: the remote shell used for communication with remote "
1731 "target, the default value is 'ssh'.\n");
1734 static int snapshot_modify_check(struct snapshot_instance *si)
1738 if (si->si_new_ssname &&
1739 strcmp(si->si_ssname, si->si_new_ssname) == 0) {
1740 fprintf(stderr, "The new snapshot's name is the same as "
1741 "the old one %s %s.\n",
1742 si->si_ssname, si->si_new_ssname);
1746 if (!si->si_new_ssname && !si->si_comment) {
1747 fprintf(stderr, "Miss options, nothing to be changed.\n");
1751 rc = mdt0_is_lustre_snapshot(si);
1752 if (!rc && si->si_new_ssname) {
1753 rc = target_is_mounted(si, si->si_mdt0, si->si_ssname);
1756 "snapshot %s is mounted, can't be renamed.\n",
1765 static int __snapshot_modify(struct snapshot_instance *si,
1766 struct list_head *head, __u64 xtime)
1768 struct snapshot_target *st;
1772 list_for_each_entry(st, head, st_list) {
1779 SNAPSHOT_ADD_LOG(si, "Can't fork for modify snapshot "
1780 "(%s|%s, <%s>) on the target "
1782 si->si_ssname, si->si_new_ssname,
1783 si->si_comment, st->st_host,
1784 st->st_role, st->st_index,
1791 char cmd[MAX_BUF_SIZE * 5];
1793 memset(cmd, 0, sizeof(cmd));
1794 if (si->si_new_ssname && si->si_comment)
1795 snprintf(cmd, sizeof(cmd) - 1,
1796 DRSH" '"DIMPORT"; "DZFS" rename "
1797 DSSNAME" "DSSNAME" && "DZFS
1798 " set lustre:comment=\"%s\" "DSSNAME
1800 " set lustre:mtime=%llu "DSSNAME"'",
1801 PRSH(si, st), PIMPORT(st), PZFS(st),
1802 PSSNAME(si, st), PSS_NEW(si, st),
1803 PZFS(st), si->si_comment,
1804 PSS_NEW(si, st), PZFS(st), xtime,
1806 else if (si->si_new_ssname)
1807 snprintf(cmd, sizeof(cmd) - 1,
1808 DRSH" '"DIMPORT"; "DZFS
1809 " rename "DSSNAME" "DSSNAME" && "DZFS
1810 " set lustre:mtime=%llu "DSSNAME"'",
1811 PRSH(si, st), PIMPORT(st), PZFS(st),
1812 PSSNAME(si, st), PSS_NEW(si, st),
1813 PZFS(st), xtime, PSS_NEW(si, st));
1814 else if (si->si_comment)
1815 snprintf(cmd, sizeof(cmd) - 1,
1816 DRSH" '"DIMPORT"; "DZFS
1817 " set lustre:comment=\"%s\" "DSSNAME
1819 " set lustre:mtime=%llu "DSSNAME"'",
1820 PRSH(si, st), PIMPORT(st), PZFS(st),
1821 si->si_comment, PSSNAME(si, st),
1822 PZFS(st), xtime, PSSNAME(si, st));
1826 rc = snapshot_exec(cmd);
1828 SNAPSHOT_ADD_LOG(si, "Can't execute \"%s\" on "
1829 "target (%s:%x:%d): rc = %d\n",
1830 cmd, st->st_host, st->st_role,
1834 } /* end of child */
1836 /* parent continue to run more snapshot commands in parallel. */
1843 static int snapshot_modify(struct snapshot_instance *si)
1849 rc = snapshot_modify_check(si);
1855 /* Modify snapshot on every MDT */
1856 rc = __snapshot_modify(si, &si->si_mdts_list, (__u64)tt);
1858 /* Modify snapshot on every OST */
1859 rc = __snapshot_modify(si, &si->si_osts_list, (__u64)tt);
1861 /* Wait for all children, even though part of them maybe failed */
1862 snapshot_wait(si, &rc1);
1864 return rc ? rc : rc1;
1867 int jt_snapshot_modify(int argc, char **argv)
1869 struct snapshot_instance *si;
1870 struct option long_opts[] = {
1871 { .val = 'c', .name = "comment", .has_arg = required_argument },
1872 { .val = 'F', .name = "fsname", .has_arg = required_argument },
1873 { .val = 'h', .name = "help", .has_arg = no_argument },
1874 { .val = 'n', .name = "name", .has_arg = required_argument },
1875 { .val = 'N', .name = "new", .has_arg = required_argument },
1876 { .val = 'r', .name = "rsh", .has_arg = required_argument },
1880 si = snapshot_init(argc, argv, long_opts, "c:F:hn:N:r:",
1881 snapshot_modify_usage, LOCK_EX, &rc);
1885 if (!si->si_ssname) {
1887 "Miss the snapshot name to be modified\n");
1888 snapshot_modify_usage();
1893 rc = snapshot_modify(si);
1896 "Can't modify the snapshot %s\n", si->si_ssname);
1897 SNAPSHOT_ADD_LOG(si, "Can't modify snapshot %s with "
1898 "name <%s>, comment <%s>: %d\n",
1899 si->si_ssname, si->si_new_ssname,
1900 si->si_comment, rc);
1902 SNAPSHOT_ADD_LOG(si, "Modify snapshot %s successfully "
1903 "with name <%s>, comment <%s>\n",
1904 si->si_ssname, si->si_new_ssname,
1912 static void snapshot_list_usage(void)
1915 "List the specified snapshot or all snapshots.\n"
1917 "snapshot_list [-d | --detail] "
1918 "<-F | --fsname fsname> [-h | --help] "
1919 "[-n | --name ssname] [-r | --rsh remote_shell]\n"
1921 "-d: list every piece for the specified snapshot.\n"
1922 "-F: the filesystem name.\n"
1923 "-h: for help information.\n"
1924 "-n: the snapshot's name.\n"
1925 "-r: the remote shell used for communication with remote "
1926 "target, the default value is 'ssh'.\n");
1929 static int snapshot_list_one(struct snapshot_instance *si,
1930 struct snapshot_target *st)
1932 char buf[MAX_BUF_SIZE * 3];
1936 memset(buf, 0, sizeof(buf));
1937 snprintf(buf, sizeof(buf) - 1,
1938 DRSH" \""DIMPORT"; "DZFS
1939 " get all "DSSNAME" | grep lustre: | grep local$ | "
1940 "awk '{ \\$1=\\\"\\\"; \\$NF=\\\"\\\"; print \\$0 }' | "
1941 "sed -e 's/^ //'\"",
1942 PRSH(si, st), PIMPORT(st), PZFS(st), PSSNAME(si, st));
1943 fp = popen(buf, "r");
1945 SNAPSHOT_ADD_LOG(si, "Popen fail to list one: %s\n",
1950 if (si->si_detail) {
1953 snapshot_role2name(name, st->st_role, st->st_index);
1954 printf("\nsnapshot_role: %s\n", name);
1957 while (snapshot_fgets(fp, buf, MAX_BUF_SIZE) != NULL) {
1961 if (strncmp(buf, "lustre:fsname",
1962 strlen("lustre:fsname")) == 0) {
1963 ptr = snapshot_first_skip_blank(buf);
1965 printf("snapshot_fsname: %s\n", ptr);
1969 if (strncmp(buf, "lustre:comment",
1970 strlen("lustre:comment")) == 0) {
1971 ptr = snapshot_first_skip_blank(buf);
1973 printf("comment: %s\n", ptr);
1977 if (strncmp(buf, "lustre:ctime",
1978 strlen("lustre:ctime")) == 0) {
1979 ptr = snapshot_first_skip_blank(buf);
1981 sscanf(ptr, "%llu", &xtime);
1982 printf("create_time: %s",
1983 ctime((time_t *)&xtime));
1988 if (strncmp(buf, "lustre:mtime",
1989 strlen("lustre:mtime")) == 0) {
1990 ptr = snapshot_first_skip_blank(buf);
1992 sscanf(ptr, "%llu", &xtime);
1993 printf("modify_time: %s",
1994 ctime((time_t *)&xtime));
2001 rc = target_is_mounted(si, st, si->si_ssname);
2003 printf("status: unknown\n");
2005 printf("status: not mount\n");
2007 printf("status: mounted\n");
2012 static int __snapshot_list(struct snapshot_instance *si,
2013 struct list_head *head)
2015 struct snapshot_target *st;
2018 list_for_each_entry(st, head, st_list) {
2021 rc1 = snapshot_list_one(si, st);
2022 if (rc1 < 0 || rc >= 0)
2029 static int snapshot_list(struct snapshot_instance *si)
2033 rc = snapshot_general_check(si);
2037 printf("\nfilesystem_name: %s\nsnapshot_name: %s\n",
2038 si->si_fsname, si->si_ssname);
2040 if (!si->si_detail) {
2041 rc = snapshot_list_one(si, si->si_mdt0);
2045 rc = __snapshot_list(si, &si->si_mdts_list);
2046 rc1 = __snapshot_list(si, &si->si_osts_list);
2051 return rc < 0 ? rc : 0;
2054 static int snapshot_list_all(struct snapshot_instance *si)
2056 struct snapshot_target *st = si->si_mdt0;
2057 struct list_sub_item {
2058 struct list_head lsi_list;
2062 struct list_head list_sub_items;
2063 struct list_sub_item *lsi;
2064 char buf[MAX_BUF_SIZE * 2];
2068 INIT_LIST_HEAD(&list_sub_items);
2069 memset(buf, 0, sizeof(buf));
2070 snprintf(buf, sizeof(buf) - 1,
2071 DRSH" \""DZFS" get -H -r lustre:magic "DFSNAME
2072 " | grep %s | awk '{ print \\$1 }' | cut -d@ -f2\"",
2073 PRSH(si, st), PZFS(st), PFSNAME(st), SNAPSHOT_MAGIC);
2074 fp = popen(buf, "r");
2076 SNAPSHOT_ADD_LOG(si, "Popen fail to list ssnames: %s\n",
2081 while (snapshot_fgets(fp, buf, MAX_BUF_SIZE) != NULL) {
2082 int len = strlen(buf);
2084 lsi = malloc(len + 1 + sizeof(struct list_sub_item));
2086 SNAPSHOT_ADD_LOG(si, "NOT enough memory\n");
2091 memcpy(lsi->lsi_ssname, buf, len + 1);
2092 list_add(&lsi->lsi_list, &list_sub_items);
2096 while (!list_empty(&list_sub_items)) {
2097 lsi = list_entry(list_sub_items.next,
2098 struct list_sub_item, lsi_list);
2099 list_del(&lsi->lsi_list);
2101 si->si_ssname = lsi->lsi_ssname;
2102 rc = snapshot_list(si);
2103 si->si_ssname = NULL;
2112 int jt_snapshot_list(int argc, char **argv)
2114 struct snapshot_instance *si;
2115 struct option long_opts[] = {
2116 { .val = 'd', .name = "detail", .has_arg = no_argument },
2117 { .val = 'F', .name = "fsname", .has_arg = required_argument },
2118 { .val = 'h', .name = "help", .has_arg = no_argument },
2119 { .val = 'n', .name = "name", .has_arg = required_argument },
2120 { .val = 'r', .name = "rsh", .has_arg = required_argument },
2124 si = snapshot_init(argc, argv, long_opts, "dF:hn:r:",
2125 snapshot_list_usage, LOCK_SH, &rc);
2130 rc = snapshot_list(si);
2132 rc = snapshot_list_all(si);
2136 "Can't list the snapshot %s\n", si->si_ssname);
2137 SNAPSHOT_ADD_LOG(si, "Can't list snapshot %s with detail "
2138 "<%s>: %d\n", si->si_ssname,
2139 si->si_detail ? "yes" : "no", rc);
2146 static void snapshot_mount_usage(void)
2149 "Mount the specified snapshot.\n"
2151 "snapshot_mount <-F | --fsname fsname> [-h | --help] "
2152 "<-n | --name ssname> "
2153 "[-r | --rsh remote_shell]\n"
2155 "-F: the filesystem name.\n"
2156 "-h: for help information.\n"
2157 "-n: the snapshot's name.\n"
2158 "-r: the remote shell used for communication with remote "
2159 "target, the default value is 'ssh'.\n");
2162 static int snapshot_mount_check(struct snapshot_instance *si, char *fsname,
2163 int fslen, bool *mgs_running)
2167 rc = mdt0_is_lustre_snapshot(si);
2171 rc = snapshot_get_fsname(si, fsname, fslen);
2175 rc = target_is_mounted(si, si->si_mgs, NULL);
2177 *mgs_running = true;
2184 static int snapshot_mount_target(struct snapshot_instance *si,
2185 struct snapshot_target *st, const char *optstr)
2187 char cmd[MAX_BUF_SIZE * 2];
2191 rc = target_is_mounted(si, st, si->si_ssname);
2198 memset(cmd, 0, sizeof(cmd));
2199 memset(name, 0, sizeof(name));
2200 snapshot_role2name(name, st->st_role, st->st_index);
2201 snprintf(cmd, sizeof(cmd) - 1,
2202 DRSH" '"DIMPORT"; mkdir -p /mnt/%s_%s && mount -t lustre "
2203 "-o rdonly_dev%s "DSSNAME" /mnt/%s_%s'",
2204 PRSH(si, st), PIMPORT(st), si->si_ssname, name,
2205 st != si->si_mdt0 ? "" : optstr, PSSNAME(si, st),
2206 si->si_ssname, name);
2207 rc = snapshot_exec(cmd);
2209 SNAPSHOT_ADD_LOG(si, "Can't execute \"%s\" on the target "
2210 "(%s:%x:%d): rc = %d\n", cmd, st->st_host,
2211 st->st_role, st->st_index, rc);
2216 static int __snapshot_mount(struct snapshot_instance *si,
2217 struct list_head *head)
2219 struct snapshot_target *st;
2223 list_for_each_entry(st, head, st_list) {
2224 if (st == si->si_mgs || st == si->si_mdt0)
2233 SNAPSHOT_ADD_LOG(si, "Can't fork for mount snapshot "
2234 "%s on target (%s:%x:%d): %s\n",
2235 si->si_ssname, st->st_host,
2236 st->st_role, st->st_index,
2243 rc = snapshot_mount_target(si, st, "");
2247 /* parent continue to run more snapshot commands in parallel. */
2254 static int __snapshot_umount(struct snapshot_instance *si,
2255 struct list_head *head);
2257 static int snapshot_mount(struct snapshot_instance *si)
2259 struct snapshot_target *st;
2265 bool mdt0_mounted = false;
2266 bool mgs_running = false;
2268 rc = snapshot_mount_check(si, fsname, sizeof(fsname), &mgs_running);
2271 "Can't mount the snapshot %s: %s\n",
2272 si->si_ssname, strerror(-rc));
2276 /* 1. MGS is not mounted yet, mount the MGS firstly */
2277 si->si_mgs->st_ignored = 0;
2278 si->si_mgs->st_pid = 0;
2280 rc = snapshot_mount_target(si, si->si_mgs, "");
2282 si->si_mgs->st_ignored = 1;
2288 "Can't mount the snapshot %s: %s\n",
2289 si->si_ssname, strerror(-rc));
2293 if (si->si_mgs == si->si_mdt0)
2294 mdt0_mounted = true;
2297 /* 2. Mount MDT0 if it is not combined with the MGS. */
2298 if (!mdt0_mounted) {
2299 si->si_mdt0->st_ignored = 0;
2300 si->si_mdt0->st_pid = 0;
2301 rc = snapshot_mount_target(si, si->si_mdt0, ",nomgs");
2306 /* 3.1 Mount other MDTs in parallel */
2307 rc = __snapshot_mount(si, &si->si_mdts_list);
2309 /* 3.2 Mount OSTs in parallel */
2310 rc = __snapshot_mount(si, &si->si_osts_list);
2312 /* Wait for all children, even though part of them maybe failed */
2313 failed = snapshot_wait(si, &rc1);
2315 list_for_each_entry(st, &si->si_mdts_list, st_list) {
2316 if (!st->st_ignored)
2320 list_for_each_entry(st, &si->si_osts_list, st_list) {
2321 if (!st->st_ignored)
2329 __snapshot_umount(si, &si->si_mdts_list);
2330 __snapshot_umount(si, &si->si_osts_list);
2331 snapshot_wait(si, &rc2);
2335 "Can't mount the snapshot %s: %s\n",
2336 si->si_ssname, strerror(-rc));
2339 "%d of %d pieces of the snapshot %s "
2340 "can't be mounted: %s\n",
2341 failed, needed, si->si_ssname, strerror(-rc1));
2343 return rc ? rc : rc1;
2348 "The snapshot %s has already been mounted by other\n",
2353 fprintf(stdout, "mounted the snapshot %s with fsname %s\n",
2354 si->si_ssname, fsname);
2359 int jt_snapshot_mount(int argc, char **argv)
2361 struct snapshot_instance *si;
2362 struct option long_opts[] = {
2363 { .val = 'F', .name = "fsname", .has_arg = required_argument },
2364 { .val = 'h', .name = "help", .has_arg = no_argument },
2365 { .val = 'n', .name = "name", .has_arg = required_argument },
2366 { .val = 'r', .name = "rsh", .has_arg = required_argument },
2370 si = snapshot_init(argc, argv, long_opts, "F:hn:r:",
2371 snapshot_mount_usage, LOCK_EX, &rc);
2375 if (!si->si_ssname) {
2377 "Miss the snapshot name to be mounted\n");
2378 snapshot_mount_usage();
2383 rc = snapshot_mount(si);
2385 SNAPSHOT_ADD_LOG(si, "Can't mount snapshot %s: %d\n",
2388 SNAPSHOT_ADD_LOG(si, "The snapshot %s is mounted\n",
2396 static void snapshot_umount_usage(void)
2399 "Umount the specified snapshot.\n"
2401 "snapshot_umount <-F | --fsname fsname> [-h | --help] "
2402 "<-n | --name ssname> "
2403 "[-r | --rsh remote_shell]\n"
2405 "-F: the filesystem name.\n"
2406 "-h: for help information.\n"
2407 "-n: the snapshot's name.\n"
2408 "-r: the remote shell used for communication with remote "
2409 "target, the default value is 'ssh'.\n");
2412 static int __snapshot_umount(struct snapshot_instance *si,
2413 struct list_head *head)
2415 struct snapshot_target *st;
2419 list_for_each_entry(st, head, st_list) {
2426 SNAPSHOT_ADD_LOG(si, "Can't fork for umount snapshot "
2427 "%s on target (%s:%x:%d): %s\n",
2428 si->si_ssname, st->st_host,
2429 st->st_role, st->st_index,
2436 char cmd[MAX_BUF_SIZE];
2438 rc = target_is_mounted(si, st, si->si_ssname);
2445 memset(cmd, 0, sizeof(cmd));
2446 snprintf(cmd, sizeof(cmd) - 1,
2447 DRSH" 'umount "DSSNAME"'",
2448 PRSH(si, st), PSSNAME(si, st));
2449 rc = snapshot_exec(cmd);
2454 /* parent continue to run more snapshot commands in parallel. */
2461 static int snapshot_umount(struct snapshot_instance *si)
2463 struct snapshot_target *st;
2470 rc = snapshot_general_check(si);
2473 "Can't umount the snapshot %s: %s\n",
2474 si->si_ssname, strerror(-rc));
2478 rc = __snapshot_umount(si, &si->si_mdts_list);
2479 rc1 = __snapshot_umount(si, &si->si_osts_list);
2480 failed = snapshot_wait(si, &rc2);
2482 list_for_each_entry(st, &si->si_mdts_list, st_list) {
2483 if (!st->st_ignored)
2487 list_for_each_entry(st, &si->si_osts_list, st_list) {
2488 if (!st->st_ignored)
2494 "The snapshot %s has not been mounted\n",
2501 "%d of %d pieces of the snapshot %s "
2502 "can't be umounted: %s\n",
2503 failed, needed, si->si_ssname, strerror(-rc2));
2505 return rc ? rc : (rc1 ? rc1 : rc2);
2508 int jt_snapshot_umount(int argc, char **argv)
2510 struct snapshot_instance *si;
2511 struct option long_opts[] = {
2512 { .val = 'F', .name = "fsname", .has_arg = required_argument },
2513 { .val = 'h', .name = "help", .has_arg = no_argument },
2514 { .val = 'n', .name = "name", .has_arg = required_argument },
2515 { .val = 'r', .name = "rsh", .has_arg = required_argument },
2519 si = snapshot_init(argc, argv, long_opts, "F:hn:r:",
2520 snapshot_umount_usage, LOCK_EX, &rc);
2524 if (!si->si_ssname) {
2526 "Miss the snapshot name to be umounted\n");
2527 snapshot_umount_usage();
2532 rc = snapshot_umount(si);
2534 SNAPSHOT_ADD_LOG(si, "Can't umount snapshot %s: %d\n",
2537 SNAPSHOT_ADD_LOG(si, "the snapshot %s have been umounted\n",