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 <linux/lustre/lustre_ioctl.h>
48 #include <linux/lustre/lustre_barrier_user.h>
52 #define SNAPSHOT_CONF_DIR "/etc/lsnapshot"
53 #define LDEV_CONF "/etc/ldev.conf"
54 #define SNAPSHOT_LOG "/var/log/lsnapshot.log"
55 #define SNAPSHOT_MAGIC "0x14F711B9"
56 #define MAX_BUF_SIZE 4096
64 struct snapshot_target {
65 struct list_head st_list;
66 /* Target node name. */
68 /* Where the pool is */
70 /* The target pool name on the target node. */
72 /* The backend filesystem name against the target pool. */
75 unsigned int st_index;
83 struct snapshot_instance {
84 struct list_head si_mdts_list;
85 struct list_head si_osts_list;
86 struct snapshot_target *si_mgs;
87 struct snapshot_target *si_mdt0;
101 static const char snapshot_rsh_default[] = "ssh";
102 static char snapshot_path[MAX_BUF_SIZE];
104 static char *snapshot_role2name(char *name, enum snapshot_role role,
108 snprintf(name, 8, "MDT%04x", index);
109 else if (role & SR_MGS)
110 snprintf(name, 4, "MGS");
112 snprintf(name, 8, "OST%04x", index);
117 #define SNAPSHOT_ADD_LOG(si, format, ...) \
119 char buf[MAX_BUF_SIZE]; \
123 memset(buf, 0, sizeof(buf)); \
125 snprintf(buf, sizeof(buf) - 1, "%s", ctime(&tt)); \
126 ptr = strrchr(buf, '\n'); \
130 fprintf(si->si_log_fp, "%s (%d:%s:%d:%s:%s): "format, buf, \
131 getpid(), __func__, __LINE__, si->si_fsname, \
132 si->si_rsh, ## __VA_ARGS__); \
136 #define DFSNAME "%s/%s"
137 #define DSSNAME "%s/%s@%s"
138 #define DZFS "%s zfs"
139 #define DIMPORT "%s zpool import -d %s %s > /dev/null 2>&1"
141 #define PRSH(si, st) (si)->si_rsh, (st)->st_host
142 #define PFSNAME(st) (st)->st_pool, (st)->st_filesystem
143 #define PSSNAME(si, st) PFSNAME(st), (si)->si_ssname
144 #define PSS_NEW(si, st) PFSNAME(st), (si)->si_new_ssname
145 #define PZFS(st) snapshot_path
146 #define PIMPORT(st) snapshot_path, \
147 (st)->st_dir ? (st)->st_dir : "/dev -d /tmp", (st)->st_pool
149 char *snapshot_fgets(FILE *fp, char *buf, int buflen)
153 memset(buf, 0, buflen);
154 if (fgets(buf, buflen, fp) == NULL)
157 ptr = strchr(buf, '\n');
164 static int snapshot_exec(const char *cmd)
170 /* system() return value depends on both the system() general framework,
171 * such as whether fork()/exec() success or fail, and the real @cmd exec
172 * result. Especially, if the @cmd is remote command, we may cannot know
173 * the real failure. */
175 /* fork()/exec() error */
177 return errno != 0 ? -errno : -1;
180 rc = WEXITSTATUS(rc);
183 } else if (WIFSIGNALED(rc)) {
186 /* all other known or unknown cases. */
193 static int snapshot_load_conf_ldev(struct snapshot_instance *si, char *buf,
194 struct snapshot_target *st, char **role)
204 rc = sscanf(buf, "%ms %ms %ms %ms",
205 &st->st_host, &ignore, &label, &device);
214 * [md|zfs:][pool_dir/]<pool>/<filesystem> */
215 ptr = strchr(device, ':');
218 if (strncmp(device, "zfs:", strlen("zfs:")) != 0) {
226 ptr1 = strrchr(ptr, '/');
227 /* "ptr1 - ptr + 1 == strlen(ptr)" means '/' is at the tail. */
228 if (!ptr1 || ptr1 == ptr || ptr1 - ptr + 1 == strlen(ptr)) {
234 st->st_filesystem = malloc(len);
235 if (!st->st_filesystem) {
241 strncpy(st->st_filesystem, ptr1 + 1, len - 1);
242 st->st_filesystem[len - 1] = '\0';
245 ptr1 = strrchr(ptr, '/');
247 st->st_dir = strdup(ptr);
255 st->st_pool = strdup(ptr);
262 * fsname-<role><index> or <role><index> */
263 ptr = strrchr(label, '-');
265 if (strncmp(si->si_fsname, label, ptr - label) != 0) {
266 /* This line is NOT for current filesystem .*/
276 if (strlen(ptr) < 3 || strlen(ptr) > 7) {
287 strncpy(*role, ptr, 3);
291 while (isxdigit(ptr[len])) {
292 if (isdigit(ptr[len]))
294 st->st_index * 16 + ptr[len] - '0';
295 else if (isupper(ptr[len]))
297 st->st_index * 16 + ptr[len] - 'A' + 10;
300 st->st_index * 16 + ptr[len] - 'a' + 10;
305 if (strncasecmp(*role, "MGS", 3) != 0)
313 if (!isxdigit(ptr[len]) && ptr[len] != '\0') {
328 * For old snasphot tools, the configration is in /etc/lsnapshot/${fsname}.conf,
330 * <host> <pool_dir> <pool> <local_fsname> <role(,s)> <index>
334 * host-mdt1 /tmp myfs-mdt1 mdt1 MGS,MDT 0
335 * host-mdt2 /tmp myfs-mdt2 mdt2 MDT 1
336 * host-ost1 /tmp myfs-ost1 ost1 OST 0
337 * host-ost2 /tmp myfs-ost2 ost2 OST 1
340 * For new snasphot tools, the configration is in /etc/ldev.conf, which is not
341 * only for snapshot, but also for other purpose. The format is:
342 * <host> foreign/- <label> <device> [journal-path]/- [raidtab]
344 * The format of <label> is:
345 * fsname-<role><index> or <role><index>
347 * The format of <device> is:
348 * [md|zfs:][pool_dir/]<pool>/<filesystem>
350 * Snapshot only uses the fields <host>, <label> and <device>.
354 * host-mdt1 - myfs-MDT0000 zfs:/tmp/myfs-mdt1/mdt1
357 * \retval 0 for success
358 * \retval +ve the line# with which the current line is conflict
359 * \retval -EAGAIN skip current line
360 * \retval -ve other failures
362 static int snapshot_load_conf_one(struct snapshot_instance *si,
363 char *buf, int line_num, bool is_ldev)
365 struct snapshot_target *st;
370 path = getenv("PATH");
374 memset(snapshot_path, 0, sizeof(snapshot_path));
375 snprintf(snapshot_path, sizeof(snapshot_path) - 1, "PATH='%s'", path);
377 /* filter out space */
378 while (isspace(*buf))
381 /* skip empty line */
385 /* skip comment line */
389 st = malloc(sizeof(*st));
393 memset(st, 0, sizeof(*st));
394 INIT_LIST_HEAD(&st->st_list);
397 rc = snapshot_load_conf_ldev(si, buf, st, &role);
399 rc = sscanf(buf, "%ms %ms %ms %ms %ms %d",
400 &st->st_host, &st->st_dir, &st->st_pool,
401 &st->st_filesystem, &role, &st->st_index);
410 if (strncasecmp(role, "MGS", 3) == 0) {
411 st->st_role = SR_MGS;
412 if (role[3] == ',') {
414 if (strncasecmp(&role[4], "MDT", 3) != 0) {
419 st->st_role |= SR_MDT;
421 } else if (strncasecmp(role, "MDT", 3) == 0) {
422 st->st_role = SR_MDT;
423 if (role[3] == ',') {
425 if (strncasecmp(&role[4], "MGS", 3) != 0) {
430 st->st_role |= SR_MGS;
432 } else if (strncasecmp(role, "OST", 3) == 0) {
433 st->st_role = SR_OST;
439 st->st_line = line_num;
440 if (st->st_role & SR_MDT) {
441 /* MGS is the first, MDT0 is just after the MGS
442 * if they are not combined together. */
443 if (st->st_role & SR_MGS) {
445 rc = si->si_mgs->st_line;
450 list_add(&st->st_list, &si->si_mdts_list);
453 if (st->st_index == 0) {
455 rc = si->si_mdt0->st_line;
460 if (list_empty(&st->st_list)) {
461 if (list_empty(&si->si_mdts_list) ||
463 list_add(&st->st_list,
466 list_add(&st->st_list,
467 &si->si_mgs->st_list);
469 } else if (list_empty(&st->st_list)) {
470 list_add_tail(&st->st_list, &si->si_mdts_list);
472 } else if (st->st_role & SR_MGS) {
474 rc = si->si_mgs->st_line;
479 list_add(&st->st_list, &si->si_mdts_list);
481 list_add_tail(&st->st_list, &si->si_osts_list);
495 if (st->st_filesystem)
496 free(st->st_filesystem);
503 static int snapshot_load_conf(struct snapshot_instance *si, int lock_mode)
506 char buf[MAX_BUF_SIZE];
513 memset(conf_name, 0, sizeof(conf_name));
514 strncpy(conf_name, LDEV_CONF, sizeof(conf_name) - 1);
515 fd = open(conf_name, O_RDONLY);
517 if (errno != ENOENT) {
519 "Can't open the snapshot config file %s: %s\n",
520 conf_name, strerror(errno));
525 snprintf(conf_name, sizeof(conf_name) - 1, "%s/%s.conf",
526 SNAPSHOT_CONF_DIR, si->si_fsname);
527 fd = open(conf_name, O_RDONLY);
530 "Can't open the snapshot config file %s: %s\n",
531 conf_name, strerror(errno));
539 rc = flock(fd, lock_mode | LOCK_NB);
542 "Can't lock the snapshot config file %s (%d): %s\n",
543 conf_name, lock_mode, strerror(errno));
548 fp = fdopen(fd, "r");
551 "Can't fdopen the snapshot config file %s: %s\n",
552 conf_name, strerror(errno));
557 while (snapshot_fgets(fp, buf, MAX_BUF_SIZE) != NULL) {
558 rc = snapshot_load_conf_one(si, buf, line_num, is_ldev);
561 "Invalid snapshot config file %s at the line "
562 "%d '%s'\n", conf_name, line_num, buf);
563 } else if (rc == -EAGAIN) {
567 "The config role has been specified repeatedly "
568 "at the lines %d/%d in %s\n",
569 rc, line_num, conf_name);
581 "Miss MDT0 in the config file %s\n",
587 /* By default, the MGS is on the MDT0 if it is not specified. */
589 si->si_mgs = si->si_mdt0;
590 si->si_mgs->st_role |= SR_MGS;
593 if (list_empty(&si->si_osts_list)) {
595 "Miss OST(s) in the config file %s\n",
614 static void snapshot_unload_conf(struct snapshot_instance *si)
616 struct snapshot_target *st;
618 while (!list_empty(&si->si_mdts_list)) {
619 st = list_entry(si->si_mdts_list.next,
620 struct snapshot_target, st_list);
621 list_del(&st->st_list);
625 free(st->st_filesystem);
629 while (!list_empty(&si->si_osts_list)) {
630 st = list_entry(si->si_osts_list.next,
631 struct snapshot_target, st_list);
632 list_del(&st->st_list);
636 free(st->st_filesystem);
643 if (si->si_conf_fd >= 0) {
644 flock(si->si_conf_fd, LOCK_UN);
645 close(si->si_conf_fd);
650 static int snapshot_handle_string_option(char **dst, const char *option,
651 const char *opt_name)
653 if (*dst && *dst != snapshot_rsh_default) {
655 "%s option has been specified repeatedly.\n", opt_name);
659 *dst = strdup(option);
665 static void snapshot_fini(struct snapshot_instance *si)
667 snapshot_unload_conf(si);
670 fclose(si->si_log_fp);
672 if (si->si_rsh && si->si_rsh != snapshot_rsh_default)
678 if (si->si_new_ssname)
679 free(si->si_new_ssname);
681 free(si->si_comment);
686 static struct snapshot_instance *
687 snapshot_init(int argc, char * const argv[], const struct option *longopts,
688 const char *optstring, void (*usage)(void),
689 int lock_mode, int *err)
691 struct snapshot_instance *si;
696 si = malloc(sizeof(*si));
699 "No enough memory to initialize snapshot instance.\n");
704 memset(si, 0, sizeof(*si));
705 INIT_LIST_HEAD(&si->si_mdts_list);
706 INIT_LIST_HEAD(&si->si_osts_list);
707 si->si_rsh = (char *)snapshot_rsh_default;
709 si->si_timeout = BARRIER_TIMEOUT_DEFAULT;
710 si->si_barrier = true;
711 si->si_detail = false;
712 si->si_force = false;
714 while ((opt = getopt_long(argc, argv, optstring,
715 longopts, &idx)) != EOF) {
718 if (!optarg || strcmp(optarg, "on") == 0) {
719 si->si_barrier = true;
720 } else if (strcmp(optarg, "off") == 0) {
721 si->si_barrier = false;
729 *err = snapshot_handle_string_option(&si->si_comment,
735 si->si_detail = true;
741 *err = snapshot_handle_string_option(&si->si_fsname,
747 *err = snapshot_handle_string_option(&si->si_ssname,
753 *err = snapshot_handle_string_option(&si->si_new_ssname,
760 *err = snapshot_handle_string_option(&si->si_rsh,
767 si->si_timeout = atoi(optarg);
781 if (!si->si_fsname) {
782 fprintf(stderr, "The fsname must be specified\n");
788 if (strlen(si->si_fsname) > 8) {
789 fprintf(stderr, "Invalid fsname %s\n", si->si_fsname);
794 si->si_log_fp = fopen(SNAPSHOT_LOG, "a");
795 if (!si->si_log_fp) {
798 "Can't open the snapshot log file %s: %s\n",
799 SNAPSHOT_LOG, strerror(errno));
803 *err = snapshot_load_conf(si, lock_mode);
806 if (*err != 0 && si) {
814 static int __snapshot_wait(struct snapshot_instance *si,
815 struct list_head *head, int *err)
817 struct snapshot_target *st;
821 list_for_each_entry(st, head, st_list) {
825 rc = waitpid(st->st_pid, &st->st_status, 0);
827 SNAPSHOT_ADD_LOG(si, "Can't wait child (%d) operation "
828 "on the target <%s:%x:%d>: %s\n",
829 st->st_pid, st->st_host, st->st_role,
830 st->st_index, strerror(errno));
836 /* continue to wait for next */
840 if (WIFEXITED(st->st_status)) {
841 rc = WEXITSTATUS(st->st_status);
846 st->st_ignored = true;
852 } else if (WIFSIGNALED(st->st_status)) {
853 SNAPSHOT_ADD_LOG(si, "The child (%d) operation on the "
854 "target <%s:%x:%d> was killed by "
856 st->st_pid, st->st_host, st->st_role,
857 st->st_index, WTERMSIG(st->st_status));
862 SNAPSHOT_ADD_LOG(si, "The child (%d) operation on the "
863 "target <%s:%x:%d> failed for "
865 st->st_pid, st->st_host, st->st_role,
878 static int snapshot_wait(struct snapshot_instance *si, int *err)
882 count = __snapshot_wait(si, &si->si_mdts_list, err);
883 count += __snapshot_wait(si, &si->si_osts_list, err);
888 static char *snapshot_first_skip_blank(char *buf)
892 ptr = strchr(buf, ' ');
894 ptr = strchr(buf, '\t');
899 while (*ptr == ' ' || *ptr == '\t')
908 static int mdt0_is_lustre_snapshot(struct snapshot_instance *si)
910 struct snapshot_target *st = si->si_mdt0;
911 char buf[MAX_BUF_SIZE * 3];
915 memset(buf, 0, sizeof(buf));
916 snprintf(buf, sizeof(buf) - 1,
917 DRSH" '"DIMPORT"; "DZFS
918 " get -H -o value lustre:magic "DSSNAME"'",
919 PRSH(si, st), PIMPORT(st), PZFS(st), PSSNAME(si, st));
920 fp = popen(buf, "r");
922 SNAPSHOT_ADD_LOG(si, "Popen fail to check snapshot "
923 "on mdt0: %s\n", strerror(errno));
927 if (snapshot_fgets(fp, buf, strlen(SNAPSHOT_MAGIC) + 1) == NULL) {
929 } else if (strcmp(buf, SNAPSHOT_MAGIC) == 0) {
933 "The target %s is not Lustre snapshot "
934 "or it does not exists\n", si->si_ssname);
942 static int target_is_mounted(struct snapshot_instance *si,
943 struct snapshot_target *st, const char *ssname)
945 char buf[MAX_BUF_SIZE];
946 char fullname[MAX_BUF_SIZE];
951 memset(buf, 0, sizeof(buf));
952 snprintf(buf, sizeof(buf) - 1,
955 fp = popen(buf, "r");
957 SNAPSHOT_ADD_LOG(si, "Popen fail to check target mount: %s\n",
962 memset(fullname, 0, sizeof(fullname));
964 snprintf(fullname, sizeof(fullname) - 1,
966 PFSNAME(st), ssname);
968 snprintf(fullname, sizeof(fullname) - 1,
972 while (snapshot_fgets(fp, buf, sizeof(buf)) != NULL) {
973 ptr = strstr(buf, fullname);
977 ptr += strlen(fullname) + 1; /* mount point */
978 if (ptr >= buf + strlen(buf))
981 ptr = strstr(ptr, "type lustre");
992 static int snapshot_get_fsname(struct snapshot_instance *si,
993 char *fsname, int fslen)
995 struct snapshot_target *st = si->si_mdt0;
996 char buf[MAX_BUF_SIZE * 3];
1000 memset(buf, 0, sizeof(buf));
1001 snprintf(buf, sizeof(buf) - 1,
1002 DRSH" '"DIMPORT"; "DZFS
1003 " get -H -o value lustre:fsname "DSSNAME"'",
1004 PRSH(si, st), PIMPORT(st), PZFS(st), PSSNAME(si, st));
1005 fp = popen(buf, "r");
1007 SNAPSHOT_ADD_LOG(si, "Popen fail to get fsname: %s\n",
1012 if (snapshot_fgets(fp, fsname, fslen) == NULL)
1019 static int snapshot_get_mgsnode(struct snapshot_instance *si,
1020 char *node, int size)
1022 char buf[MAX_BUF_SIZE * 2];
1023 struct snapshot_target *st;
1027 st = list_entry(si->si_osts_list.next, struct snapshot_target,
1029 memset(buf, 0, sizeof(buf));
1030 snprintf(buf, sizeof(buf) - 1,
1031 DRSH" '"DZFS" get -H -o value lustre:mgsnode "DFSNAME"'",
1032 PRSH(si, st), PZFS(st), PFSNAME(st));
1033 fp = popen(buf, "r");
1035 SNAPSHOT_ADD_LOG(si, "Popen fail to get mgsnode: %s\n",
1040 if (snapshot_fgets(fp, node, size) == NULL)
1047 static int snapshot_exists_check(struct snapshot_instance *si,
1048 struct snapshot_target *st)
1050 char buf[MAX_BUF_SIZE * 2];
1054 memset(buf, 0, sizeof(buf));
1055 snprintf(buf, sizeof(buf) - 1,
1056 DRSH" '"DZFS" list "DSSNAME" 2>/dev/null'",
1057 PRSH(si, st), PZFS(st), PSSNAME(si, st));
1058 fp = popen(buf, "r");
1060 SNAPSHOT_ADD_LOG(si, "Popen fail to create check: %s\n",
1065 if (snapshot_fgets(fp, buf, sizeof(buf)) != NULL)
1072 static int snapshot_general_check(struct snapshot_instance *si)
1074 return mdt0_is_lustre_snapshot(si);
1077 static void snapshot_create_usage(void)
1080 "Create snapshot for the given filesystem.\n"
1082 "snapshot_create [-b | --barrier [on | off]] "
1083 "[-c | --comment comment] "
1084 "<-F | --fsname fsname> "
1085 "[-h | --help] <-n | --name ssname> "
1086 "[-r | --rsh remote_shell]"
1087 "[-t | --timeout timeout]\n"
1089 "-b: set write barrier before creating snapshot, "
1090 "the default value is 'on'.\n"
1091 "-c: describe what the snapshot is for, and so on.\n"
1092 "-F: the filesystem name.\n"
1093 "-h: for help information.\n"
1094 "-n: the snapshot's name.\n"
1095 "-r: the remote shell used for communication with remote "
1096 "target, the default value is 'ssh'.\n"
1097 "-t: the life cycle (seconds) for write barrier, "
1098 "the default value is %d seconds.\n",
1099 BARRIER_TIMEOUT_DEFAULT);
1102 static int snapshot_create_check(struct snapshot_instance *si)
1106 rc = snapshot_exists_check(si, si->si_mdt0);
1108 fprintf(stderr, "The snapshot %s exists\n", si->si_ssname);
1113 static int snapshot_inherit_prop(struct snapshot_instance *si,
1114 struct snapshot_target *st,
1115 char *cmd, int size)
1117 char buf[MAX_BUF_SIZE * 3];
1122 memset(buf, 0, sizeof(buf));
1123 snprintf(buf, sizeof(buf) - 1,
1124 DRSH" \""DIMPORT"; "DZFS
1125 " get all "DFSNAME" | grep lustre: | grep local$ | "
1126 "awk '{ \\$1=\\\"\\\"; \\$NF=\\\"\\\"; print \\$0 }' | "
1127 "sed -e 's/^ //'\"",
1128 PRSH(si, st), PIMPORT(st), PZFS(st), PFSNAME(st));
1129 fp = popen(buf, "r");
1131 SNAPSHOT_ADD_LOG(si, "Popen fail to list one: %s\n",
1136 while (snapshot_fgets(fp, buf, MAX_BUF_SIZE) != NULL) {
1140 if (strncmp(buf, "lustre:fsname",
1141 strlen("lustre:fsname")) == 0)
1144 if (strncmp(buf, "lustre:magic",
1145 strlen("lustre:magic")) == 0)
1148 if (strncmp(buf, "lustre:ctime",
1149 strlen("lustre:ctime")) == 0)
1152 if (strncmp(buf, "lustre:mtime",
1153 strlen("lustre:mtime")) == 0)
1156 if (strncmp(buf, "lustre:comment",
1157 strlen("lustre:comment")) == 0)
1160 if (strncmp(buf, "lustre:svname",
1161 strlen("lustre:svname")) == 0)
1164 if (strncmp(buf, "lustre:mgsnode",
1165 strlen("lustre:mgsnode")) == 0)
1168 ptr = strchr(buf, ' ');
1170 ptr = strchr(buf, '\t');
1177 while (*ptr == ' ' || *ptr == '\t')
1183 end = strchr(ptr, ' ');
1185 end = strchr(buf, '\t');
1189 rc = snprintf(cmd + len, size - len - 1,
1190 "-o %s=\"%s\" ", buf, ptr);
1201 static int __snapshot_create(struct snapshot_instance *si,
1202 struct list_head *head, const char *fsname,
1203 const char *mgsnode, __u64 xtime)
1205 struct snapshot_target *st;
1209 list_for_each_entry(st, head, st_list) {
1216 SNAPSHOT_ADD_LOG(si, "Can't fork for create snapshot "
1217 "(%s@%s <%s>) on the target "
1219 fsname, si->si_ssname,
1220 si->si_comment, st->st_host,
1221 st->st_role, st->st_index,
1228 char cmd[MAX_BUF_SIZE];
1231 memset(cmd, 0, sizeof(cmd));
1232 len = snprintf(cmd, sizeof(cmd) - 1,
1233 DRSH" '"DZFS" snapshot "
1234 "-o lustre:fsname=%s "
1235 "-o lustre:magic=%s "
1236 "-o lustre:ctime=%llu "
1237 "-o lustre:mtime=%llu ",
1238 PRSH(si, st), PZFS(st), fsname,
1239 SNAPSHOT_MAGIC, xtime, xtime);
1243 if (si->si_comment) {
1244 rc = snprintf(cmd + len, sizeof(cmd) - len - 1,
1245 "-o lustre:comment=\"%s\" ",
1253 /* Make the inherited properties as local ones,
1254 * then even if others changed (or removed) the
1255 * property of the parent dataset, the snapshot
1256 * will not be affected. */
1257 rc = snapshot_inherit_prop(si, st, cmd + len,
1258 MAX_BUF_SIZE - len - 1);
1260 SNAPSHOT_ADD_LOG(si, "Can't filter property on "
1261 "target (%s:%x:%d): rc = %d\n",
1262 st->st_host, st->st_role,
1269 if (st->st_role & SR_OST)
1270 rc = snprintf(cmd + len, sizeof(cmd) - len - 1,
1271 "-o lustre:svname=%s-OST%04x "
1272 "-o lustre:mgsnode=%s "DSSNAME"'",
1273 fsname, st->st_index, mgsnode,
1275 else if (!(st->st_role & SR_MGS) ||
1276 /* MGS is on MDT0 */
1277 si->si_mdt0 == si->si_mgs)
1278 rc = snprintf(cmd + len, sizeof(cmd) - len - 1,
1279 "-o lustre:svname=%s-MDT%04x "
1280 "-o lustre:mgsnode=%s "DSSNAME"'",
1281 fsname, st->st_index, mgsnode,
1285 rc = snprintf(cmd + len, sizeof(cmd) - len - 1,
1286 DSSNAME"'", PSSNAME(si, st));
1290 rc = snapshot_exec(cmd);
1292 SNAPSHOT_ADD_LOG(si, "Can't execute \"%s\" on "
1293 "target (%s:%x:%d): rc = %d\n",
1294 cmd, st->st_host, st->st_role,
1298 } /* end of child */
1300 /* parent continue to run more snapshot commands in parallel. */
1307 static int __snapshot_destroy(struct snapshot_instance *si,
1308 struct list_head *head);
1310 static int snapshot_create(struct snapshot_instance *si)
1313 char buf[MAX_BUF_SIZE];
1320 rc = snapshot_create_check(si);
1324 rc = gettimeofday(&tv, NULL);
1328 srandom(tv.tv_usec);
1329 snprintf(new_fsname, sizeof(new_fsname), "%08x", (__u32)random());
1331 rc = snapshot_get_mgsnode(si, buf, sizeof(buf));
1335 __argv[1] = si->si_fsname;
1336 /* 1. Get barrier */
1337 if (si->si_barrier) {
1340 memset(tbuf, 0, sizeof(tbuf));
1341 snprintf(tbuf, 7, "%u", si->si_timeout);
1342 __argv[0] = "barrier_freeze";
1344 rc = jt_barrier_freeze(3, __argv);
1346 SNAPSHOT_ADD_LOG(si, "Can't set barrier within %u "
1347 "seconds on %s: rc = %d\n",
1348 si->si_timeout, si->si_fsname, rc);
1354 /* 2. Fork config llog on MGS */
1355 __argv[0] = "fork_lcfg";
1356 __argv[2] = new_fsname;
1357 rc = jt_lcfg_fork(3, __argv);
1359 SNAPSHOT_ADD_LOG(si, "Can't fork config log for create "
1360 "snapshot %s from %s to %s: rc = %d\n",
1361 si->si_ssname, si->si_fsname, new_fsname, rc);
1365 /* 3.1 Create snapshot on every MDT */
1366 rc = __snapshot_create(si, &si->si_mdts_list, new_fsname, buf,
1369 /* 3.2 Create snapshot on every OST */
1370 rc = __snapshot_create(si, &si->si_osts_list, new_fsname, buf,
1373 /* 4. Wait for all children, even though part of them maybe failed */
1374 snapshot_wait(si, &rc1);
1377 /* 5. Put barrier */
1378 if (si->si_barrier) {
1380 struct barrier_ctl bc;
1382 rc = __jt_barrier_stat(__argv[1], &bc);
1384 SNAPSHOT_ADD_LOG(si, "Can't get barrier status "
1387 } else if (bc.bc_status != BS_FROZEN ||
1388 bc.bc_timeout <= 0) {
1389 SNAPSHOT_ADD_LOG(si, "The barrier expired "
1390 "on %s\n", si->si_fsname);
1395 __argv[0] = "barrier_thaw";
1396 rc2 = jt_barrier_thaw(2, __argv);
1398 SNAPSHOT_ADD_LOG(si, "Can't release barrier on %s: "
1399 "rc = %d\n", si->si_fsname, rc2);
1404 si->si_force = true;
1405 __snapshot_destroy(si, &si->si_osts_list);
1406 __snapshot_destroy(si, &si->si_mdts_list);
1407 snapshot_wait(si, &rc2);
1409 __argv[0] = "erase_lcfg";
1410 __argv[1] = new_fsname;
1412 jt_lcfg_erase(3, __argv);
1415 return rc ? rc : (rc1 ? rc1 : rc2);
1418 int jt_snapshot_create(int argc, char **argv)
1420 struct snapshot_instance *si;
1421 struct option long_opts[] = {
1422 { .val = 'b', .name = "barrier", .has_arg = optional_argument },
1423 { .val = 'c', .name = "comment", .has_arg = required_argument },
1424 { .val = 'F', .name = "fsname", .has_arg = required_argument },
1425 { .val = 'h', .name = "help", .has_arg = no_argument },
1426 { .val = 'n', .name = "name", .has_arg = required_argument },
1427 { .val = 'r', .name = "rsh", .has_arg = required_argument },
1428 { .val = 't', .name = "timeout", .has_arg = required_argument },
1432 si = snapshot_init(argc, argv, long_opts, "b::c:F:hn:r:t:",
1433 snapshot_create_usage, LOCK_EX, &rc);
1437 if (!si->si_ssname) {
1439 "Miss the snapshot name to be created\n");
1440 snapshot_create_usage();
1445 rc = snapshot_create(si);
1448 "Can't create the snapshot %s\n", si->si_ssname);
1449 SNAPSHOT_ADD_LOG(si, "Can't create snapshot %s with "
1450 "comment <%s> barrier <%s>, timeout "
1452 si->si_ssname, si->si_comment,
1453 si->si_barrier ? "enable" : "disable",
1454 si->si_barrier ? si->si_timeout : -1, rc);
1456 SNAPSHOT_ADD_LOG(si, "Create snapshot %s successfully "
1457 "with comment <%s>, barrier <%s>, "
1459 si->si_ssname, si->si_comment,
1460 si->si_barrier ? "enable" : "disable",
1461 si->si_barrier ? si->si_timeout : -1);
1468 static void snapshot_destroy_usage(void)
1471 "Destroy the specified snapshot.\n"
1473 "snapshot_destroy [-f | --force] "
1474 "<-F | --fsname fsname> [-h | --help] "
1475 "<-n | --name ssname> "
1476 "[-r | --rsh remote_shell]\n"
1478 "-f: destroy the snapshot by force.\n"
1479 "-F: the filesystem name.\n"
1480 "-h: for help information.\n"
1481 "-n: the snapshot's name.\n"
1482 "-r: the remote shell used for communication with remote "
1483 "target, the default value is 'ssh'.\n");
1486 static int snapshot_destroy_check(struct snapshot_instance *si)
1488 struct list_head *head = &si->si_osts_list;
1489 struct snapshot_target *st;
1494 list_for_each_entry(st, head, st_list) {
1501 SNAPSHOT_ADD_LOG(si, "Can't fork for check snapshot "
1502 "%s on the target (%s:%x:%d): %s\n",
1503 si->si_ssname, st->st_host,
1504 st->st_role, st->st_index,
1511 rc = snapshot_exists_check(si, st);
1513 /* The snapshot piece does not exist */
1516 exit(rc == -EEXIST ? 0: rc);
1517 } /* end of child */
1519 /* parent continue to run more snapshot commands in parallel. */
1523 if (head == &si->si_osts_list) {
1524 head = &si->si_mdts_list;
1528 snapshot_wait(si, &rc);
1532 head = &si->si_osts_list;
1535 list_for_each_entry(st, head, st_list) {
1536 if (st->st_ignored && !si->si_force) {
1539 snapshot_role2name(name, st->st_role, st->st_index);
1541 "Miss snapshot piece on the %s. Use '-f' "
1542 "option if want to destroy it by force.\n",
1549 if (head == &si->si_osts_list) {
1550 head = &si->si_mdts_list;
1555 rc = snapshot_general_check(si);
1560 static int __snapshot_destroy(struct snapshot_instance *si,
1561 struct list_head *head)
1563 struct snapshot_target *st;
1567 list_for_each_entry(st, head, st_list) {
1576 SNAPSHOT_ADD_LOG(si, "Can't fork for destroy snapshot "
1577 "%s on the target (%s:%x:%d): %s\n",
1578 si->si_ssname, st->st_host,
1579 st->st_role, st->st_index,
1586 char cmd[MAX_BUF_SIZE * 2];
1588 memset(cmd, 0, sizeof(cmd));
1590 snprintf(cmd, sizeof(cmd) - 1,
1591 DRSH" 'umount -f "DSSNAME
1592 " > /dev/null 2>&1; "DZFS
1593 " destroy -f "DSSNAME"'",
1594 PRSH(si, st), PSSNAME(si, st),
1595 PZFS(st), PSSNAME(si, st));
1597 snprintf(cmd, sizeof(cmd) - 1,
1598 DRSH" '"DZFS" destroy "DSSNAME"'",
1599 PRSH(si, st), PZFS(st),
1601 rc = snapshot_exec(cmd);
1603 SNAPSHOT_ADD_LOG(si, "Can't execute \"%s\" on "
1604 "target (%s:%x:%d): rc = %d\n",
1605 cmd, st->st_host, st->st_role,
1609 } /* end of child */
1611 /* parent continue to run more snapshot commands in parallel. */
1618 static int snapshot_destroy(struct snapshot_instance *si)
1626 rc = snapshot_destroy_check(si);
1630 rc = snapshot_get_fsname(si, fsname, sizeof(fsname));
1634 /* 1.1 Destroy snapshot on every OST */
1635 rc = __snapshot_destroy(si, &si->si_osts_list);
1636 if (!si->si_force) {
1640 __snapshot_wait(si, &si->si_osts_list, &rc);
1645 /* 1.2 Destroy snapshot on every MDT */
1646 rc1 = __snapshot_destroy(si, &si->si_mdts_list);
1648 /* 2 Wait for all children, even though part of them maybe failed */
1649 snapshot_wait(si, &rc2);
1650 if (rc2 == -ENOENT && si->si_force)
1653 /* 3. Erase config llog from MGS */
1654 if ((!rc && !rc1 && !rc2) || si->si_force) {
1657 __argv[0] = "erase_lcfg";
1660 rc3 = jt_lcfg_erase(3, __argv);
1661 if (rc3 && errno == ENOENT)
1664 SNAPSHOT_ADD_LOG(si, "Can't erase config for destroy "
1665 "snapshot %s, fsname %s: rc = %d\n",
1666 si->si_ssname, fsname, rc3);
1669 return rc ? rc : (rc1 ? rc1 : (rc2 ? rc2 : rc3));
1672 int jt_snapshot_destroy(int argc, char **argv)
1674 struct snapshot_instance *si;
1675 struct option long_opts[] = {
1676 { .val = 'f', .name = "force", .has_arg = no_argument },
1677 { .val = 'F', .name = "fsname", .has_arg = required_argument },
1678 { .val = 'h', .name = "help", .has_arg = no_argument },
1679 { .val = 'n', .name = "name", .has_arg = required_argument },
1680 { .val = 'r', .name = "rsh", .has_arg = required_argument },
1684 si = snapshot_init(argc, argv, long_opts, "fF:hn:r:",
1685 snapshot_destroy_usage, LOCK_EX, &rc);
1689 if (!si->si_ssname) {
1691 "Miss the snapshot name to be destroyed\n");
1692 snapshot_destroy_usage();
1697 rc = snapshot_destroy(si);
1700 "Can't destroy the snapshot %s\n", si->si_ssname);
1701 SNAPSHOT_ADD_LOG(si, "Can't destroy snapshot %s with "
1702 "force <%s>: %d\n", si->si_ssname,
1703 si->si_force ? "enable" : "disable", rc);
1705 SNAPSHOT_ADD_LOG(si, "Destroy snapshot %s successfully "
1706 "with force <%s>\n", si->si_ssname,
1707 si->si_force ? "enable" : "disable");
1714 static void snapshot_modify_usage(void)
1717 "Change the specified snapshot's name and/or comment.\n"
1719 "snapshot_modify [-c | --comment comment] "
1720 "<-F | --fsname fsname> [-h | --help] "
1721 "<-n | --name ssname> [-N | --new new_ssname] "
1722 "[-r | --rsh remote_shell]\n"
1724 "-c: update the snapshot's comment.\n"
1725 "-F: the filesystem name.\n"
1726 "-h: for help information.\n"
1727 "-n: the snapshot's name.\n"
1728 "-N: rename the snapshot's name as the new_ssname.\n"
1729 "-r: the remote shell used for communication with remote "
1730 "target, the default value is 'ssh'.\n");
1733 static int snapshot_modify_check(struct snapshot_instance *si)
1737 if (si->si_new_ssname &&
1738 strcmp(si->si_ssname, si->si_new_ssname) == 0) {
1739 fprintf(stderr, "The new snapshot's name is the same as "
1740 "the old one %s %s.\n",
1741 si->si_ssname, si->si_new_ssname);
1745 if (!si->si_new_ssname && !si->si_comment) {
1746 fprintf(stderr, "Miss options, nothing to be changed.\n");
1750 rc = mdt0_is_lustre_snapshot(si);
1751 if (!rc && si->si_new_ssname) {
1752 rc = target_is_mounted(si, si->si_mdt0, si->si_ssname);
1755 "snapshot %s is mounted, can't be renamed.\n",
1764 static int __snapshot_modify(struct snapshot_instance *si,
1765 struct list_head *head, __u64 xtime)
1767 struct snapshot_target *st;
1771 list_for_each_entry(st, head, st_list) {
1778 SNAPSHOT_ADD_LOG(si, "Can't fork for modify snapshot "
1779 "(%s|%s, <%s>) on the target "
1781 si->si_ssname, si->si_new_ssname,
1782 si->si_comment, st->st_host,
1783 st->st_role, st->st_index,
1790 char cmd[MAX_BUF_SIZE * 5];
1792 memset(cmd, 0, sizeof(cmd));
1793 if (si->si_new_ssname && si->si_comment)
1794 snprintf(cmd, sizeof(cmd) - 1,
1795 DRSH" '"DIMPORT"; "DZFS" rename "
1796 DSSNAME" "DSSNAME" && "DZFS
1797 " set lustre:comment=\"%s\" "DSSNAME
1799 " set lustre:mtime=%llu "DSSNAME"'",
1800 PRSH(si, st), PIMPORT(st), PZFS(st),
1801 PSSNAME(si, st), PSS_NEW(si, st),
1802 PZFS(st), si->si_comment,
1803 PSS_NEW(si, st), PZFS(st), xtime,
1805 else if (si->si_new_ssname)
1806 snprintf(cmd, sizeof(cmd) - 1,
1807 DRSH" '"DIMPORT"; "DZFS
1808 " rename "DSSNAME" "DSSNAME" && "DZFS
1809 " set lustre:mtime=%llu "DSSNAME"'",
1810 PRSH(si, st), PIMPORT(st), PZFS(st),
1811 PSSNAME(si, st), PSS_NEW(si, st),
1812 PZFS(st), xtime, PSS_NEW(si, st));
1813 else if (si->si_comment)
1814 snprintf(cmd, sizeof(cmd) - 1,
1815 DRSH" '"DIMPORT"; "DZFS
1816 " set lustre:comment=\"%s\" "DSSNAME
1818 " set lustre:mtime=%llu "DSSNAME"'",
1819 PRSH(si, st), PIMPORT(st), PZFS(st),
1820 si->si_comment, PSSNAME(si, st),
1821 PZFS(st), xtime, PSSNAME(si, st));
1825 rc = snapshot_exec(cmd);
1827 SNAPSHOT_ADD_LOG(si, "Can't execute \"%s\" on "
1828 "target (%s:%x:%d): rc = %d\n",
1829 cmd, st->st_host, st->st_role,
1833 } /* end of child */
1835 /* parent continue to run more snapshot commands in parallel. */
1842 static int snapshot_modify(struct snapshot_instance *si)
1848 rc = snapshot_modify_check(si);
1854 /* Modify snapshot on every MDT */
1855 rc = __snapshot_modify(si, &si->si_mdts_list, (__u64)tt);
1857 /* Modify snapshot on every OST */
1858 rc = __snapshot_modify(si, &si->si_osts_list, (__u64)tt);
1860 /* Wait for all children, even though part of them maybe failed */
1861 snapshot_wait(si, &rc1);
1863 return rc ? rc : rc1;
1866 int jt_snapshot_modify(int argc, char **argv)
1868 struct snapshot_instance *si;
1869 struct option long_opts[] = {
1870 { .val = 'c', .name = "comment", .has_arg = required_argument },
1871 { .val = 'F', .name = "fsname", .has_arg = required_argument },
1872 { .val = 'h', .name = "help", .has_arg = no_argument },
1873 { .val = 'n', .name = "name", .has_arg = required_argument },
1874 { .val = 'N', .name = "new", .has_arg = required_argument },
1875 { .val = 'r', .name = "rsh", .has_arg = required_argument },
1879 si = snapshot_init(argc, argv, long_opts, "c:F:hn:N:r:",
1880 snapshot_modify_usage, LOCK_EX, &rc);
1884 if (!si->si_ssname) {
1886 "Miss the snapshot name to be modified\n");
1887 snapshot_modify_usage();
1892 rc = snapshot_modify(si);
1895 "Can't modify the snapshot %s\n", si->si_ssname);
1896 SNAPSHOT_ADD_LOG(si, "Can't modify snapshot %s with "
1897 "name <%s>, comment <%s>: %d\n",
1898 si->si_ssname, si->si_new_ssname,
1899 si->si_comment, rc);
1901 SNAPSHOT_ADD_LOG(si, "Modify snapshot %s successfully "
1902 "with name <%s>, comment <%s>\n",
1903 si->si_ssname, si->si_new_ssname,
1911 static void snapshot_list_usage(void)
1914 "List the specified snapshot or all snapshots.\n"
1916 "snapshot_list [-d | --detail] "
1917 "<-F | --fsname fsname> [-h | --help] "
1918 "[-n | --name ssname] [-r | --rsh remote_shell]\n"
1920 "-d: list every piece for the specified snapshot.\n"
1921 "-F: the filesystem name.\n"
1922 "-h: for help information.\n"
1923 "-n: the snapshot's name.\n"
1924 "-r: the remote shell used for communication with remote "
1925 "target, the default value is 'ssh'.\n");
1928 static int snapshot_list_one(struct snapshot_instance *si,
1929 struct snapshot_target *st)
1931 char buf[MAX_BUF_SIZE * 3];
1935 memset(buf, 0, sizeof(buf));
1936 snprintf(buf, sizeof(buf) - 1,
1937 DRSH" \""DIMPORT"; "DZFS
1938 " get all "DSSNAME" | grep lustre: | grep local$ | "
1939 "awk '{ \\$1=\\\"\\\"; \\$NF=\\\"\\\"; print \\$0 }' | "
1940 "sed -e 's/^ //'\"",
1941 PRSH(si, st), PIMPORT(st), PZFS(st), PSSNAME(si, st));
1942 fp = popen(buf, "r");
1944 SNAPSHOT_ADD_LOG(si, "Popen fail to list one: %s\n",
1949 if (si->si_detail) {
1952 snapshot_role2name(name, st->st_role, st->st_index);
1953 printf("\nsnapshot_role: %s\n", name);
1956 while (snapshot_fgets(fp, buf, MAX_BUF_SIZE) != NULL) {
1960 if (strncmp(buf, "lustre:fsname",
1961 strlen("lustre:fsname")) == 0) {
1962 ptr = snapshot_first_skip_blank(buf);
1964 printf("snapshot_fsname: %s\n", ptr);
1968 if (strncmp(buf, "lustre:comment",
1969 strlen("lustre:comment")) == 0) {
1970 ptr = snapshot_first_skip_blank(buf);
1972 printf("comment: %s\n", ptr);
1976 if (strncmp(buf, "lustre:ctime",
1977 strlen("lustre:ctime")) == 0) {
1978 ptr = snapshot_first_skip_blank(buf);
1980 sscanf(ptr, "%llu", &xtime);
1981 printf("create_time: %s",
1982 ctime((time_t *)&xtime));
1987 if (strncmp(buf, "lustre:mtime",
1988 strlen("lustre:mtime")) == 0) {
1989 ptr = snapshot_first_skip_blank(buf);
1991 sscanf(ptr, "%llu", &xtime);
1992 printf("modify_time: %s",
1993 ctime((time_t *)&xtime));
2000 rc = target_is_mounted(si, st, si->si_ssname);
2002 printf("status: unknown\n");
2004 printf("status: not mount\n");
2006 printf("status: mounted\n");
2011 static int __snapshot_list(struct snapshot_instance *si,
2012 struct list_head *head)
2014 struct snapshot_target *st;
2017 list_for_each_entry(st, head, st_list) {
2020 rc1 = snapshot_list_one(si, st);
2021 if (rc1 < 0 || rc >= 0)
2028 static int snapshot_list(struct snapshot_instance *si)
2032 rc = snapshot_general_check(si);
2036 printf("\nfilesystem_name: %s\nsnapshot_name: %s\n",
2037 si->si_fsname, si->si_ssname);
2039 if (!si->si_detail) {
2040 rc = snapshot_list_one(si, si->si_mdt0);
2044 rc = __snapshot_list(si, &si->si_mdts_list);
2045 rc1 = __snapshot_list(si, &si->si_osts_list);
2050 return rc < 0 ? rc : 0;
2053 static int snapshot_list_all(struct snapshot_instance *si)
2055 struct snapshot_target *st = si->si_mdt0;
2056 struct list_sub_item {
2057 struct list_head lsi_list;
2061 struct list_head list_sub_items;
2062 struct list_sub_item *lsi;
2063 char buf[MAX_BUF_SIZE * 2];
2067 INIT_LIST_HEAD(&list_sub_items);
2068 memset(buf, 0, sizeof(buf));
2069 snprintf(buf, sizeof(buf) - 1,
2070 DRSH" \""DZFS" get -H -r lustre:magic "DFSNAME
2071 " | grep %s | awk '{ print \\$1 }' | cut -d@ -f2\"",
2072 PRSH(si, st), PZFS(st), PFSNAME(st), SNAPSHOT_MAGIC);
2073 fp = popen(buf, "r");
2075 SNAPSHOT_ADD_LOG(si, "Popen fail to list ssnames: %s\n",
2080 while (snapshot_fgets(fp, buf, MAX_BUF_SIZE) != NULL) {
2081 int len = strlen(buf);
2083 lsi = malloc(len + 1 + sizeof(struct list_sub_item));
2085 SNAPSHOT_ADD_LOG(si, "NOT enough memory\n");
2090 memcpy(lsi->lsi_ssname, buf, len + 1);
2091 list_add(&lsi->lsi_list, &list_sub_items);
2095 while (!list_empty(&list_sub_items)) {
2096 lsi = list_entry(list_sub_items.next,
2097 struct list_sub_item, lsi_list);
2098 list_del(&lsi->lsi_list);
2100 si->si_ssname = lsi->lsi_ssname;
2101 rc = snapshot_list(si);
2102 si->si_ssname = NULL;
2111 int jt_snapshot_list(int argc, char **argv)
2113 struct snapshot_instance *si;
2114 struct option long_opts[] = {
2115 { .val = 'd', .name = "detail", .has_arg = no_argument },
2116 { .val = 'F', .name = "fsname", .has_arg = required_argument },
2117 { .val = 'h', .name = "help", .has_arg = no_argument },
2118 { .val = 'n', .name = "name", .has_arg = required_argument },
2119 { .val = 'r', .name = "rsh", .has_arg = required_argument },
2123 si = snapshot_init(argc, argv, long_opts, "dF:hn:r:",
2124 snapshot_list_usage, LOCK_SH, &rc);
2129 rc = snapshot_list(si);
2131 rc = snapshot_list_all(si);
2135 "Can't list the snapshot %s\n", si->si_ssname);
2136 SNAPSHOT_ADD_LOG(si, "Can't list snapshot %s with detail "
2137 "<%s>: %d\n", si->si_ssname,
2138 si->si_detail ? "yes" : "no", rc);
2145 static void snapshot_mount_usage(void)
2148 "Mount the specified snapshot.\n"
2150 "snapshot_mount <-F | --fsname fsname> [-h | --help] "
2151 "<-n | --name ssname> "
2152 "[-r | --rsh remote_shell]\n"
2154 "-F: the filesystem name.\n"
2155 "-h: for help information.\n"
2156 "-n: the snapshot's name.\n"
2157 "-r: the remote shell used for communication with remote "
2158 "target, the default value is 'ssh'.\n");
2161 static int snapshot_mount_check(struct snapshot_instance *si, char *fsname,
2162 int fslen, bool *mgs_running)
2166 rc = mdt0_is_lustre_snapshot(si);
2170 rc = snapshot_get_fsname(si, fsname, fslen);
2174 rc = target_is_mounted(si, si->si_mgs, NULL);
2176 *mgs_running = true;
2183 static int snapshot_mount_target(struct snapshot_instance *si,
2184 struct snapshot_target *st, const char *optstr)
2186 char cmd[MAX_BUF_SIZE * 2];
2190 rc = target_is_mounted(si, st, si->si_ssname);
2197 memset(cmd, 0, sizeof(cmd));
2198 memset(name, 0, sizeof(name));
2199 snapshot_role2name(name, st->st_role, st->st_index);
2200 snprintf(cmd, sizeof(cmd) - 1,
2201 DRSH" '"DIMPORT"; mkdir -p /mnt/%s_%s && mount -t lustre "
2202 "-o rdonly_dev%s "DSSNAME" /mnt/%s_%s'",
2203 PRSH(si, st), PIMPORT(st), si->si_ssname, name,
2204 st != si->si_mdt0 ? "" : optstr, PSSNAME(si, st),
2205 si->si_ssname, name);
2206 rc = snapshot_exec(cmd);
2208 SNAPSHOT_ADD_LOG(si, "Can't execute \"%s\" on the target "
2209 "(%s:%x:%d): rc = %d\n", cmd, st->st_host,
2210 st->st_role, st->st_index, rc);
2215 static int __snapshot_mount(struct snapshot_instance *si,
2216 struct list_head *head)
2218 struct snapshot_target *st;
2222 list_for_each_entry(st, head, st_list) {
2223 if (st == si->si_mgs || st == si->si_mdt0)
2232 SNAPSHOT_ADD_LOG(si, "Can't fork for mount snapshot "
2233 "%s on target (%s:%x:%d): %s\n",
2234 si->si_ssname, st->st_host,
2235 st->st_role, st->st_index,
2242 rc = snapshot_mount_target(si, st, "");
2246 /* parent continue to run more snapshot commands in parallel. */
2253 static int __snapshot_umount(struct snapshot_instance *si,
2254 struct list_head *head);
2256 static int snapshot_mount(struct snapshot_instance *si)
2258 struct snapshot_target *st;
2264 bool mdt0_mounted = false;
2265 bool mgs_running = false;
2267 rc = snapshot_mount_check(si, fsname, sizeof(fsname), &mgs_running);
2270 "Can't mount the snapshot %s: %s\n",
2271 si->si_ssname, strerror(-rc));
2275 /* 1. MGS is not mounted yet, mount the MGS firstly */
2276 si->si_mgs->st_ignored = 0;
2277 si->si_mgs->st_pid = 0;
2279 rc = snapshot_mount_target(si, si->si_mgs, "");
2281 si->si_mgs->st_ignored = 1;
2287 "Can't mount the snapshot %s: %s\n",
2288 si->si_ssname, strerror(-rc));
2292 if (si->si_mgs == si->si_mdt0)
2293 mdt0_mounted = true;
2296 /* 2. Mount MDT0 if it is not combined with the MGS. */
2297 if (!mdt0_mounted) {
2298 si->si_mdt0->st_ignored = 0;
2299 si->si_mdt0->st_pid = 0;
2300 rc = snapshot_mount_target(si, si->si_mdt0, ",nomgs");
2305 /* 3.1 Mount other MDTs in parallel */
2306 rc = __snapshot_mount(si, &si->si_mdts_list);
2308 /* 3.2 Mount OSTs in parallel */
2309 rc = __snapshot_mount(si, &si->si_osts_list);
2311 /* Wait for all children, even though part of them maybe failed */
2312 failed = snapshot_wait(si, &rc1);
2314 list_for_each_entry(st, &si->si_mdts_list, st_list) {
2315 if (!st->st_ignored)
2319 list_for_each_entry(st, &si->si_osts_list, st_list) {
2320 if (!st->st_ignored)
2328 __snapshot_umount(si, &si->si_mdts_list);
2329 __snapshot_umount(si, &si->si_osts_list);
2330 snapshot_wait(si, &rc2);
2334 "Can't mount the snapshot %s: %s\n",
2335 si->si_ssname, strerror(-rc));
2338 "%d of %d pieces of the snapshot %s "
2339 "can't be mounted: %s\n",
2340 failed, needed, si->si_ssname, strerror(-rc1));
2342 return rc ? rc : rc1;
2347 "The snapshot %s has already been mounted by other\n",
2352 fprintf(stdout, "mounted the snapshot %s with fsname %s\n",
2353 si->si_ssname, fsname);
2358 int jt_snapshot_mount(int argc, char **argv)
2360 struct snapshot_instance *si;
2361 struct option long_opts[] = {
2362 { .val = 'F', .name = "fsname", .has_arg = required_argument },
2363 { .val = 'h', .name = "help", .has_arg = no_argument },
2364 { .val = 'n', .name = "name", .has_arg = required_argument },
2365 { .val = 'r', .name = "rsh", .has_arg = required_argument },
2369 si = snapshot_init(argc, argv, long_opts, "F:hn:r:",
2370 snapshot_mount_usage, LOCK_EX, &rc);
2374 if (!si->si_ssname) {
2376 "Miss the snapshot name to be mounted\n");
2377 snapshot_mount_usage();
2382 rc = snapshot_mount(si);
2384 SNAPSHOT_ADD_LOG(si, "Can't mount snapshot %s: %d\n",
2387 SNAPSHOT_ADD_LOG(si, "The snapshot %s is mounted\n",
2395 static void snapshot_umount_usage(void)
2398 "Umount the specified snapshot.\n"
2400 "snapshot_umount <-F | --fsname fsname> [-h | --help] "
2401 "<-n | --name ssname> "
2402 "[-r | --rsh remote_shell]\n"
2404 "-F: the filesystem name.\n"
2405 "-h: for help information.\n"
2406 "-n: the snapshot's name.\n"
2407 "-r: the remote shell used for communication with remote "
2408 "target, the default value is 'ssh'.\n");
2411 static int __snapshot_umount(struct snapshot_instance *si,
2412 struct list_head *head)
2414 struct snapshot_target *st;
2418 list_for_each_entry(st, head, st_list) {
2425 SNAPSHOT_ADD_LOG(si, "Can't fork for umount snapshot "
2426 "%s on target (%s:%x:%d): %s\n",
2427 si->si_ssname, st->st_host,
2428 st->st_role, st->st_index,
2435 char cmd[MAX_BUF_SIZE];
2437 rc = target_is_mounted(si, st, si->si_ssname);
2444 memset(cmd, 0, sizeof(cmd));
2445 snprintf(cmd, sizeof(cmd) - 1,
2446 DRSH" 'umount "DSSNAME"'",
2447 PRSH(si, st), PSSNAME(si, st));
2448 rc = snapshot_exec(cmd);
2453 /* parent continue to run more snapshot commands in parallel. */
2460 static int snapshot_umount(struct snapshot_instance *si)
2462 struct snapshot_target *st;
2469 rc = snapshot_general_check(si);
2472 "Can't umount the snapshot %s: %s\n",
2473 si->si_ssname, strerror(-rc));
2477 rc = __snapshot_umount(si, &si->si_mdts_list);
2478 rc1 = __snapshot_umount(si, &si->si_osts_list);
2479 failed = snapshot_wait(si, &rc2);
2481 list_for_each_entry(st, &si->si_mdts_list, st_list) {
2482 if (!st->st_ignored)
2486 list_for_each_entry(st, &si->si_osts_list, st_list) {
2487 if (!st->st_ignored)
2493 "The snapshot %s has not been mounted\n",
2500 "%d of %d pieces of the snapshot %s "
2501 "can't be umounted: %s\n",
2502 failed, needed, si->si_ssname, strerror(-rc2));
2504 return rc ? rc : (rc1 ? rc1 : rc2);
2507 int jt_snapshot_umount(int argc, char **argv)
2509 struct snapshot_instance *si;
2510 struct option long_opts[] = {
2511 { .val = 'F', .name = "fsname", .has_arg = required_argument },
2512 { .val = 'h', .name = "help", .has_arg = no_argument },
2513 { .val = 'n', .name = "name", .has_arg = required_argument },
2514 { .val = 'r', .name = "rsh", .has_arg = required_argument },
2518 si = snapshot_init(argc, argv, long_opts, "F:hn:r:",
2519 snapshot_umount_usage, LOCK_EX, &rc);
2523 if (!si->si_ssname) {
2525 "Miss the snapshot name to be umounted\n");
2526 snapshot_umount_usage();
2531 rc = snapshot_umount(si);
2533 SNAPSHOT_ADD_LOG(si, "Can't umount snapshot %s: %d\n",
2536 SNAPSHOT_ADD_LOG(si, "the snapshot %s have been umounted\n",