Whamcloud - gitweb
LU-9430 utils: fix logic errors and putchar in sk_name2hmac()
[fs/lustre-release.git] / lustre / utils / lsnapshot.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
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.
9
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.
15
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
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2016, Intel Corporation.
24  *
25  * lustre/utils/lsnapshot.c
26  *
27  * Author: Fan, Yong <fan.yong@intel.com>
28  */
29
30 #include <stdio.h>
31 #include <string.h>
32 #include <getopt.h>
33 #include <unistd.h>
34 #include <stdlib.h>
35 #include <errno.h>
36 #include <string.h>
37 #include <sys/types.h>
38 #include <sys/wait.h>
39 #include <sys/time.h>
40 #include <sys/file.h>
41 #include <time.h>
42 #include <limits.h>
43 #include <ctype.h>
44
45 #include <libcfs/util/list.h>
46 #include <libcfs/util/ioctl.h>
47 #include <linux/lustre_ioctl.h>
48 #include <lustre/lustre_barrier_user.h>
49
50 #include "obdctl.h"
51
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
57
58 enum snapshot_role {
59         SR_MGS  = 0x0001,
60         SR_MDT  = 0x0002,
61         SR_OST  = 0x0004,
62 };
63
64 struct snapshot_target {
65         struct list_head         st_list;
66         /* Target node name. */
67         char                    *st_host;
68         /* Where the pool is */
69         char                    *st_dir;
70         /* The target pool name on the target node. */
71         char                    *st_pool;
72         /* The backend filesystem name against the target pool. */
73         char                    *st_filesystem;
74         int                      st_role;
75         unsigned int             st_index;
76         unsigned int             st_gen;
77         int                      st_line;
78         int                      st_status;
79         pid_t                    st_pid;
80         bool                     st_ignored;
81 };
82
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;
88         FILE                    *si_log_fp;
89         char                    *si_rsh;
90         char                    *si_fsname;
91         char                    *si_ssname;
92         char                    *si_new_ssname;
93         char                    *si_comment;
94         int                      si_conf_fd;
95         int                      si_timeout;
96         bool                     si_barrier;
97         bool                     si_detail;
98         bool                     si_force;
99 };
100
101 static const char snapshot_rsh_default[] = "ssh";
102
103 static char *snapshot_role2name(char *name, enum snapshot_role role,
104                                 __u32 index)
105 {
106         if (role & SR_MDT)
107                 snprintf(name, 8, "MDT%04x", index);
108         else if (role & SR_MGS)
109                 snprintf(name, 4, "MGS");
110         else
111                 snprintf(name, 8, "OST%04x", index);
112
113         return name;
114 }
115
116 #define SNAPSHOT_ADD_LOG(si, format, ...)                               \
117 do {                                                                    \
118         char buf[MAX_BUF_SIZE];                                         \
119         char *ptr;                                                      \
120         time_t tt;                                                      \
121                                                                         \
122         memset(buf, 0, sizeof(buf));                                    \
123         time(&tt);                                                      \
124         snprintf(buf, sizeof(buf) - 1, "%s", ctime(&tt));               \
125         ptr = strrchr(buf, '\n');                                       \
126         if (ptr)                                                        \
127                 *ptr = '\0';                                            \
128                                                                         \
129         fprintf(si->si_log_fp, "%s (%d:%s:%d:%s:%s): "format, buf,      \
130                 getpid(), __func__, __LINE__, si->si_fsname,            \
131                 si->si_rsh, ## __VA_ARGS__);                            \
132 } while (0)
133
134 char *snapshot_fgets(FILE *fp, char *buf, int buflen)
135 {
136         char *ptr;
137
138         memset(buf, 0, buflen);
139         if (fgets(buf, buflen, fp) == NULL)
140                 return NULL;
141
142         ptr = strchr(buf, '\n');
143         if (ptr)
144                 *ptr = '\0';
145
146         return buf;
147 }
148
149 static int snapshot_exec(const char *cmd)
150 {
151         int rc;
152
153         errno = 0;
154
155         /* system() return value depends on both the system() general framework,
156          * such as whether fork()/exec() success or fail, and the real @cmd exec
157          * result. Especially, if the @cmd is remote command, we may cannot know
158          * the real failure. */
159         rc = system(cmd);
160         /* fork()/exec() error */
161         if (rc == -1)
162                 return errno != 0 ? -errno : -1;
163
164         if (WIFEXITED(rc)) {
165                 rc = WEXITSTATUS(rc);
166                 if (rc > 0)
167                         rc = -rc;
168         } else if (WIFSIGNALED(rc)) {
169                 rc = -EINTR;
170         } else {
171                 /* all other known or unknown cases. */
172                 rc = -EFAULT;
173         }
174
175         return rc;
176 }
177
178 static int snapshot_load_conf_ldev(struct snapshot_instance *si, char *buf,
179                                    struct snapshot_target *st, char **role)
180 {
181         char *label = NULL;
182         char *device = NULL;
183         char *ignore = NULL;
184         char *ptr;
185         char *ptr1;
186         int len;
187         int rc;
188
189         rc = sscanf(buf, "%ms %ms %ms %ms",
190                     &st->st_host, &ignore, &label, &device);
191         if (rc < 4) {
192                 rc = -EINVAL;
193                 goto out;
194         }
195
196         free(ignore);
197
198         /* Format of device:
199          * [md|zfs:][pool_dir/]<pool>/<filesystem> */
200         ptr = strchr(device, ':');
201         if (ptr) {
202                 ptr++;
203                 if (strncmp(device, "zfs:", strlen("zfs:")) != 0) {
204                         rc = -EINVAL;
205                         goto out;
206                 }
207         } else {
208                         ptr = device;
209         }
210
211         ptr1 = strrchr(ptr, '/');
212         /* "ptr1 - ptr + 1 == strlen(ptr)" means '/' is at the tail. */
213         if (!ptr1 || ptr1 == ptr || ptr1 - ptr + 1 == strlen(ptr)) {
214                 rc = -EINVAL;
215                 goto out;
216         }
217
218         len = strlen(ptr1);
219         st->st_filesystem = malloc(len);
220         if (!st->st_filesystem) {
221                 rc = -ENOMEM;
222                 goto out;
223         }
224
225         *ptr1 = '\0';
226         strncpy(st->st_filesystem, ptr1 + 1, len - 1);
227         st->st_filesystem[len - 1] = '\0';
228
229         if (*ptr == '/') {
230                 ptr1 = strrchr(ptr, '/');
231                 *ptr1 = '\0';
232                 len = strlen(ptr);
233                 st->st_dir = malloc(len + 1);
234                 if (!st->st_dir) {
235                         rc = -ENOMEM;
236                         goto out;
237                 }
238
239                 strncpy(st->st_dir, ptr, len);
240                 st->st_dir[len] = '\0';
241                 ptr = ptr1 + 1;
242         }
243
244         len = strlen(ptr);
245         st->st_pool = malloc(len + 1);
246         if (!st->st_pool) {
247                 rc = -ENOMEM;
248                 goto out;
249         }
250
251         strncpy(st->st_pool, ptr, len);
252         st->st_pool[len] = '\0';
253
254         /* Format of label:
255          * fsname-<role><index> or <role><index> */
256         ptr = strchr(label, '-');
257         if (ptr) {
258                 if (strncmp(si->si_fsname, label, ptr - label) != 0) {
259                         /* This line is NOT for current filesystem .*/
260                         rc = -EAGAIN;
261                         goto out;
262                 }
263
264                 ptr++;
265         } else {
266                 ptr = label;
267         }
268
269         if (strlen(ptr) < 3 || strlen(ptr) > 7) {
270                 rc = -EINVAL;
271                 goto out;
272         }
273
274         *role = malloc(4);
275         if (!*role) {
276                 rc = -ENOMEM;
277                 goto out;
278         }
279
280         strncpy(*role, ptr, 3);
281         (*role)[3] = 0;
282         ptr += 3;
283         len = 0;
284         while (isxdigit(ptr[len])) {
285                 if (isdigit(ptr[len]))
286                         st->st_index =
287                                 st->st_index * 16 + ptr[len] - '0';
288                 else if (isupper(ptr[len]))
289                         st->st_index =
290                                 st->st_index * 16 + ptr[len] - 'A' + 10;
291                 else
292                         st->st_index =
293                                 st->st_index * 16 + ptr[len] - 'a' + 10;
294                 len++;
295         }
296
297         if (len == 0) {
298                 if (strncasecmp(*role, "MGS", 3) != 0)
299                         rc = -EINVAL;
300                 else
301                         rc = 0;
302
303                 goto out;
304         }
305
306         if (!isxdigit(ptr[len]) && ptr[len] != '\0') {
307                 rc = -EINVAL;
308                 goto out;
309         }
310
311 out:
312         if (label)
313                 free(label);
314         if (device)
315                 free(device);
316
317         return rc;
318 }
319
320 /**
321  * For old snasphot tools, the configration is in /etc/lsnapshot/${fsname}.conf,
322  * the format is:
323  * <host> <pool_dir> <pool> <local_fsname> <role(,s)> <index>
324  *
325  * For example:
326  *
327  * host-mdt1 /tmp myfs-mdt1 mdt1 MGS,MDT 0
328  * host-mdt2 /tmp myfs-mdt2 mdt2 MDT 1
329  * host-ost1 /tmp myfs-ost1 ost1 OST 0
330  * host-ost2 /tmp myfs-ost2 ost2 OST 1
331  *
332  *
333  * For new snasphot tools, the configration is in /etc/ldev.conf, which is not
334  * only for snapshot, but also for other purpose. The format is:
335  * <host> foreign/- <label> <device> [journal-path]/- [raidtab]
336  *
337  * The format of <label> is:
338  * fsname-<role><index> or <role><index>
339  *
340  * The format of <device> is:
341  * [md|zfs:][pool_dir/]<pool>/<filesystem>
342  *
343  * Snapshot only uses the fields <host>, <label> and <device>.
344  *
345  * For example:
346  *
347  * host-mdt1 - myfs-MDT0000 zfs:/tmp/myfs-mdt1/mdt1
348  *
349  *
350  * \retval       0      for success
351  * \retval      +ve     the line# with which the current line is conflict
352  * \retval      -EAGAIN skip current line
353  * \retval      -ve     other failures
354  */
355 static int snapshot_load_conf_one(struct snapshot_instance *si,
356                                   char *buf, int line_num, bool is_ldev)
357 {
358         struct snapshot_target *st;
359         char *role = NULL;
360         int rc = 0;
361
362         /* filter out space */
363         while (isspace(*buf))
364                 buf++;
365
366         /* skip empty line */
367         if (*buf == '\0')
368                 return 0;
369
370         /* skip comment line */
371         if (*buf == '#')
372                 return 0;
373
374         st = malloc(sizeof(*st));
375         if (!st)
376                 return -ENOMEM;
377
378         memset(st, 0, sizeof(*st));
379         INIT_LIST_HEAD(&st->st_list);
380
381         if (is_ldev) {
382                 rc = snapshot_load_conf_ldev(si, buf, st, &role);
383         } else {
384                 rc = sscanf(buf, "%ms %ms %ms %ms %ms %d",
385                             &st->st_host, &st->st_dir, &st->st_pool,
386                             &st->st_filesystem, &role, &st->st_index);
387                 if (rc < 6)
388                         rc = -EINVAL;
389         }
390
391         if (rc < 0)
392                 goto out;
393         rc = 0;
394
395         if (strncasecmp(role, "MGS", 3) == 0) {
396                 st->st_role = SR_MGS;
397                 if (role[3] == ',') {
398                         /* MGS,MDT */
399                         if (strncasecmp(&role[4], "MDT", 3) != 0) {
400                                 rc = -EINVAL;
401                                 goto out;
402                         }
403
404                         st->st_role |= SR_MDT;
405                 }
406         } else if (strncasecmp(role, "MDT", 3) == 0) {
407                 st->st_role = SR_MDT;
408                 if (role[3] == ',') {
409                         /* MDT,MGS */
410                         if (strncasecmp(&role[4], "MGS", 3) != 0) {
411                                 rc = -EINVAL;
412                                 goto out;
413                         }
414
415                         st->st_role |= SR_MGS;
416                 }
417         } else if (strncasecmp(role, "OST", 3) == 0) {
418                 st->st_role = SR_OST;
419         } else {
420                 rc = -EINVAL;
421                 goto out;
422         }
423
424         st->st_line = line_num;
425         if (st->st_role & SR_MDT) {
426                 /* MGS is the first, MDT0 is just after the MGS
427                  * if they are not combined together. */
428                 if (st->st_role & SR_MGS) {
429                         if (si->si_mgs) {
430                                 rc = si->si_mgs->st_line;
431                                 goto out;
432                         }
433
434                         si->si_mgs = st;
435                         list_add(&st->st_list, &si->si_mdts_list);
436                 }
437
438                 if (st->st_index == 0) {
439                         if (si->si_mdt0) {
440                                 rc = si->si_mdt0->st_line;
441                                 goto out;
442                         }
443
444                         si->si_mdt0 = st;
445                         if (list_empty(&st->st_list)) {
446                                 if (list_empty(&si->si_mdts_list) ||
447                                     !si->si_mgs)
448                                         list_add(&st->st_list,
449                                                  &si->si_mdts_list);
450                                 else
451                                         list_add(&st->st_list,
452                                                  &si->si_mgs->st_list);
453                         }
454                 } else if (list_empty(&st->st_list)) {
455                         list_add_tail(&st->st_list, &si->si_mdts_list);
456                 }
457         } else if (st->st_role & SR_MGS) {
458                 if (si->si_mgs) {
459                         rc = si->si_mgs->st_line;
460                         goto out;
461                 }
462
463                 si->si_mgs = st;
464                 list_add(&st->st_list, &si->si_mdts_list);
465         } else {
466                 list_add_tail(&st->st_list, &si->si_osts_list);
467         }
468
469 out:
470         if (role)
471                 free(role);
472
473         if (rc) {
474                 if (st->st_host)
475                         free(st->st_host);
476                 if (st->st_dir)
477                         free(st->st_dir);
478                 if (st->st_pool)
479                         free(st->st_pool);
480                 if (st->st_filesystem)
481                         free(st->st_filesystem);
482                 free(st);
483         }
484
485         return rc;
486 }
487
488 static int snapshot_load_conf(struct snapshot_instance *si, int lock_mode)
489 {
490         FILE *fp;
491         char buf[MAX_BUF_SIZE];
492         char conf_name[32];
493         int line_num = 1;
494         int fd = -1;
495         int rc = 0;
496         bool is_ldev = true;
497
498         memset(conf_name, 0, sizeof(conf_name));
499         strncpy(conf_name, LDEV_CONF, sizeof(conf_name) - 1);
500         fd = open(conf_name, O_RDONLY);
501         if (fd < 0) {
502                 if (errno != ENOENT) {
503                         fprintf(stderr,
504                                 "Can't open the snapshot config file %s: %s\n",
505                                 conf_name, strerror(errno));
506
507                         return fd;
508                 }
509
510                 snprintf(conf_name, sizeof(conf_name) - 1, "%s/%s.conf",
511                          SNAPSHOT_CONF_DIR, si->si_fsname);
512                 fd = open(conf_name, O_RDONLY);
513                 if (fd < 0) {
514                         fprintf(stderr,
515                                 "Can't open the snapshot config file %s: %s\n",
516                                 conf_name, strerror(errno));
517
518                         return fd;
519                 }
520
521                 is_ldev = false;
522         }
523
524         rc = flock(fd, lock_mode | LOCK_NB);
525         if (rc < 0) {
526                 fprintf(stderr,
527                         "Can't lock the snapshot config file %s (%d): %s\n",
528                         conf_name, lock_mode, strerror(errno));
529                 close(fd);
530                 return rc;
531         }
532
533         fp = fdopen(fd, "r");
534         if (!fp) {
535                 fprintf(stderr,
536                         "Can't fdopen the snapshot config file %s: %s\n",
537                         conf_name, strerror(errno));
538                 rc = -1;
539                 goto out;
540         }
541
542         while (snapshot_fgets(fp, buf, MAX_BUF_SIZE) != NULL) {
543                 rc = snapshot_load_conf_one(si, buf, line_num, is_ldev);
544                 if (rc == -EINVAL) {
545                         fprintf(stderr,
546                                 "Invalid snapshot config file %s at the line "
547                                 "%d '%s'\n", conf_name, line_num, buf);
548                 } else if (rc == -EAGAIN) {
549                         rc = 0;
550                 } else if (rc > 0) {
551                         fprintf(stderr,
552                                 "The config role has been specified repeatedly "
553                                 "at the lines %d/%d in %s\n",
554                                 rc, line_num, conf_name);
555                         rc = -EINVAL;
556                 }
557
558                 if (rc)
559                         goto out;
560
561                 line_num++;
562         }
563
564         if (!si->si_mdt0) {
565                 fprintf(stderr,
566                         "Miss MDT0 in the config file %s\n",
567                         conf_name);
568                 rc = -1;
569                 goto out;
570         }
571
572         /* By default, the MGS is on the MDT0 if it is not specified. */
573         if (!si->si_mgs) {
574                 si->si_mgs = si->si_mdt0;
575                 si->si_mgs->st_role |= SR_MGS;
576         }
577
578         if (list_empty(&si->si_osts_list)) {
579                 fprintf(stderr,
580                         "Miss OST(s) in the config file %s\n",
581                         conf_name);
582                 rc = -1;
583                 goto out;
584         }
585
586 out:
587         if (fd >= 0) {
588                 if (rc < 0) {
589                         flock(fd, LOCK_UN);
590                         close(fd);
591                 } else {
592                         si->si_conf_fd = fd;
593                 }
594         }
595
596         return rc;
597 }
598
599 static void snapshot_unload_conf(struct snapshot_instance *si)
600 {
601         struct snapshot_target *st;
602
603         while (!list_empty(&si->si_mdts_list)) {
604                 st = list_entry(si->si_mdts_list.next,
605                                 struct snapshot_target, st_list);
606                 list_del(&st->st_list);
607                 free(st->st_host);
608                 free(st->st_dir);
609                 free(st->st_pool);
610                 free(st->st_filesystem);
611                 free(st);
612         }
613
614         while (!list_empty(&si->si_osts_list)) {
615                 st = list_entry(si->si_osts_list.next,
616                                 struct snapshot_target, st_list);
617                 list_del(&st->st_list);
618                 free(st->st_host);
619                 free(st->st_dir);
620                 free(st->st_pool);
621                 free(st->st_filesystem);
622                 free(st);
623         }
624
625         si->si_mgs = NULL;
626         si->si_mdt0 = NULL;
627
628         if (si->si_conf_fd >= 0) {
629                 flock(si->si_conf_fd, LOCK_UN);
630                 close(si->si_conf_fd);
631                 si->si_conf_fd = -1;
632         }
633 }
634
635 static int snapshot_handle_string_option(char **dst, const char *option,
636                                          const char *opt_name)
637 {
638         int len;
639
640         if (*dst && *dst != snapshot_rsh_default) {
641                 fprintf(stderr, "specify the %s repeatedly.\n", opt_name);
642                 return -EINVAL;
643         }
644
645         len = strlen(option);
646         *dst = malloc(len + 1);
647         if (!*dst)
648                 return -ENOMEM;
649
650         strncpy(*dst, option, len);
651         (*dst)[len] = '\0';
652         return 0;
653 }
654
655 static void snapshot_fini(struct snapshot_instance *si)
656 {
657         snapshot_unload_conf(si);
658
659         if (si->si_log_fp)
660                 fclose(si->si_log_fp);
661
662         if (si->si_rsh && si->si_rsh != snapshot_rsh_default)
663                 free(si->si_rsh);
664         if (si->si_fsname)
665                 free(si->si_fsname);
666         if (si->si_ssname)
667                 free(si->si_ssname);
668         if (si->si_new_ssname)
669                 free(si->si_new_ssname);
670         if (si->si_comment)
671                 free(si->si_comment);
672
673         free(si);
674 }
675
676 static struct snapshot_instance *
677 snapshot_init(int argc, char * const argv[], const struct option *longopts,
678               const char *optstring, void (*usage)(void),
679               int lock_mode, int *err)
680 {
681         struct snapshot_instance *si;
682         int idx;
683         int opt;
684
685         *err = 0;
686         si = malloc(sizeof(*si));
687         if (!si) {
688                 fprintf(stderr,
689                         "No enough memory to initialize snapshot instance.\n");
690                 *err = -ENOMEM;
691                 return NULL;
692         }
693
694         memset(si, 0, sizeof(*si));
695         INIT_LIST_HEAD(&si->si_mdts_list);
696         INIT_LIST_HEAD(&si->si_osts_list);
697         si->si_rsh = (char *)snapshot_rsh_default;
698         si->si_conf_fd = -1;
699         si->si_timeout = BARRIER_TIMEOUT_DEFAULT;
700         si->si_barrier = true;
701         si->si_detail = false;
702         si->si_force = false;
703
704         while ((opt = getopt_long(argc, argv, optstring,
705                                   longopts, &idx)) != EOF) {
706                 switch (opt) {
707                 case 'b':
708                         if (!optarg || strcmp(optarg, "on") == 0) {
709                                 si->si_barrier = true;
710                         } else if (strcmp(optarg, "off") == 0) {
711                                 si->si_barrier = false;
712                         } else {
713                                 usage();
714                                 *err = -EINVAL;
715                                 goto out;
716                         }
717                         break;
718                 case 'c':
719                         *err = snapshot_handle_string_option(&si->si_comment,
720                                                              optarg, "comment");
721                         if (*err != 0)
722                                 goto out;
723                         break;
724                 case 'd':
725                         si->si_detail = true;
726                         break;
727                 case 'f':
728                         si->si_force = true;
729                         break;
730                 case 'F':
731                         *err = snapshot_handle_string_option(&si->si_fsname,
732                                                              optarg, "fsname");
733                         if (*err != 0)
734                                 goto out;
735                         break;
736                 case 'n':
737                         *err = snapshot_handle_string_option(&si->si_ssname,
738                                                              optarg, "ssname");
739                         if (*err != 0)
740                                 goto out;
741                         break;
742                 case 'N':
743                         *err = snapshot_handle_string_option(&si->si_new_ssname,
744                                                              optarg,
745                                                              "new ssname");
746                         if (*err != 0)
747                                 goto out;
748                         break;
749                 case 'r':
750                         *err = snapshot_handle_string_option(&si->si_rsh,
751                                                              optarg,
752                                                              "remote shell");
753                         if (*err != 0)
754                                 goto out;
755                         break;
756                 case 't':
757                         si->si_timeout = atoi(optarg);
758                         break;
759                 default:
760                         *err = -EINVAL;
761                         usage();
762                         goto out;
763                 case 'h':
764                         usage();
765                         snapshot_fini(si);
766                         *err = 0;
767                         return NULL;
768                 }
769         }
770
771         if (!si->si_fsname) {
772                 fprintf(stderr, "The fsname must be specified\n");
773                 usage();
774                 *err = -EINVAL;
775                 goto out;
776         }
777
778         if (strlen(si->si_fsname) > 8) {
779                 fprintf(stderr, "Invalid fsname %s\n", si->si_fsname);
780                 *err = -EINVAL;
781                 goto out;
782         }
783
784         si->si_log_fp = fopen(SNAPSHOT_LOG, "a");
785         if (!si->si_log_fp) {
786                 *err = -errno;
787                 fprintf(stderr,
788                         "Can't open the snapshot log file %s: %s\n",
789                         SNAPSHOT_LOG, strerror(errno));
790                 goto out;
791         }
792
793         *err = snapshot_load_conf(si, lock_mode);
794
795 out:
796         if (*err != 0 && si) {
797                 snapshot_fini(si);
798                 si = NULL;
799         }
800
801         return si;
802 }
803
804 static int __snapshot_wait(struct snapshot_instance *si,
805                            struct list_head *head, int *err)
806 {
807         struct snapshot_target *st;
808         int count = 0;
809         int rc = 0;
810
811         list_for_each_entry(st, head, st_list) {
812                 if (st->st_pid == 0)
813                         continue;
814
815                 rc = waitpid(st->st_pid, &st->st_status, 0);
816                 if (rc < 0) {
817                         SNAPSHOT_ADD_LOG(si, "Can't wait child (%d) operation "
818                                          "on the target <%s:%x:%d>: %s\n",
819                                          st->st_pid, st->st_host, st->st_role,
820                                          st->st_index, strerror(errno));
821                         count++;
822                         if (*err == 0)
823                                 *err = rc;
824
825                         st->st_pid = 0;
826                         /* continue to wait for next */
827                         continue;
828                 }
829
830                 if (WIFEXITED(st->st_status)) {
831                         rc = WEXITSTATUS(st->st_status);
832                         if (rc > 0)
833                                 rc -= 256;
834
835                         if (rc == -ESRCH) {
836                                 st->st_ignored = true;
837                         } else if (rc) {
838                                 count++;
839                                 if (*err == 0)
840                                         *err = rc;
841                         }
842                 } else if (WIFSIGNALED(st->st_status)) {
843                         SNAPSHOT_ADD_LOG(si, "The child (%d) operation on the "
844                                          "target <%s:%x:%d> was killed by "
845                                          "signal %d\n",
846                                          st->st_pid, st->st_host, st->st_role,
847                                          st->st_index, WTERMSIG(st->st_status));
848                         count++;
849                         if (*err == 0)
850                                 *err = -EINTR;
851                 } else {
852                         SNAPSHOT_ADD_LOG(si, "The child (%d) operation on the "
853                                          "target <%s:%x:%d> failed for "
854                                          "unknown reason\n",
855                                          st->st_pid, st->st_host, st->st_role,
856                                          st->st_index);
857                         count++;
858                         if (*err == 0)
859                                 *err = -EFAULT;
860                 }
861
862                 st->st_pid = 0;
863         }
864
865         return count;
866 }
867
868 static int snapshot_wait(struct snapshot_instance *si, int *err)
869 {
870         int count;
871
872         count = __snapshot_wait(si, &si->si_mdts_list, err);
873         count += __snapshot_wait(si, &si->si_osts_list, err);
874
875         return count;
876 }
877
878 static char *snapshot_first_skip_blank(char *buf)
879 {
880         char *ptr;
881
882         ptr = strchr(buf, ' ');
883         if (!ptr) {
884                 ptr = strchr(buf, '\t');
885                 if (!ptr)
886                         return NULL;
887         }
888
889         while (*ptr == ' ' || *ptr == '\t')
890                 ptr++;
891
892         if (*ptr == '\0')
893                 ptr = NULL;
894
895         return ptr;
896 }
897
898 static int mdt0_is_lustre_snapshot(struct snapshot_instance *si)
899 {
900         char buf[MAX_BUF_SIZE];
901         FILE *fp;
902         int rc;
903
904         memset(buf, 0, sizeof(buf));
905         snprintf(buf, sizeof(buf) - 1,
906                  "%s %s 'zpool import -d %s %s > /dev/null 2>&1; "
907                  "zfs get -H -o value lustre:magic %s/%s@%s'",
908                  si->si_rsh, si->si_mdt0->st_host,
909                  si->si_mdt0->st_dir ? si->si_mdt0->st_dir :
910                         "/dev -d /tmp",
911                  si->si_mdt0->st_pool, si->si_mdt0->st_pool,
912                  si->si_mdt0->st_filesystem, si->si_ssname);
913         fp = popen(buf, "r");
914         if (!fp) {
915                 SNAPSHOT_ADD_LOG(si, "Popen fail to check snapshot "
916                                  "on mdt0: %s\n", strerror(errno));
917                 return -errno;
918         }
919
920         if (snapshot_fgets(fp, buf, strlen(SNAPSHOT_MAGIC) + 1) == NULL) {
921                 rc = -EINVAL;
922         } else if (strcmp(buf, SNAPSHOT_MAGIC) == 0) {
923                 rc = 0;
924         } else {
925                 fprintf(stderr,
926                         "The target %s is not Lustre snapshot "
927                         "or it does not exists\n", si->si_ssname);
928                 rc = -EPERM;
929         }
930
931         pclose(fp);
932         return rc;
933 }
934
935 static int target_is_mounted(struct snapshot_instance *si,
936                              struct snapshot_target *st, const char *ssname)
937 {
938         char buf[MAX_BUF_SIZE];
939         char fullname[MAX_BUF_SIZE];
940         FILE *fp;
941         char *ptr;
942         int rc = 0;
943
944         memset(buf, 0, sizeof(buf));
945         snprintf(buf, sizeof(buf) - 1, "%s %s 'mount'",
946                  si->si_rsh, st->st_host);
947         fp = popen(buf, "r");
948         if (!fp) {
949                 SNAPSHOT_ADD_LOG(si, "Popen fail to check target mount: %s\n",
950                                  strerror(errno));
951                 return -errno;
952         }
953
954         memset(fullname, 0, sizeof(fullname));
955         if (ssname)
956                 snprintf(fullname, sizeof(fullname) - 1, "%s/%s@%s on ",
957                          st->st_pool, st->st_filesystem, ssname);
958         else
959                 snprintf(fullname, sizeof(fullname) - 1, "%s/%s on ",
960                          st->st_pool, st->st_filesystem);
961
962         while (snapshot_fgets(fp, buf, sizeof(buf)) != NULL) {
963                 ptr = strstr(buf, fullname);
964                 if (!ptr)
965                         continue;
966
967                 ptr += strlen(fullname) + 1; /* mount point */
968                 if (ptr >= buf + strlen(buf))
969                         continue;
970
971                 ptr = strstr(ptr, "type lustre");
972                 if (ptr) {
973                         rc = 1;
974                         break;
975                 }
976         }
977
978         pclose(fp);
979         return rc;
980 }
981
982 static int snapshot_get_fsname(struct snapshot_instance *si,
983                                char *fsname, int fslen)
984 {
985         char buf[MAX_BUF_SIZE];
986         FILE *fp;
987         int rc = 0;
988
989         memset(buf, 0, sizeof(buf));
990         snprintf(buf, sizeof(buf) - 1,
991                  "%s %s 'zpool import -d %s %s > /dev/null 2>&1; "
992                  "zfs get -H -o value lustre:fsname %s/%s@%s'",
993                  si->si_rsh, si->si_mdt0->st_host,
994                  si->si_mdt0->st_dir ? si->si_mdt0->st_dir :
995                         "/dev -d /tmp",
996                  si->si_mdt0->st_pool, si->si_mdt0->st_pool,
997                  si->si_mdt0->st_filesystem, si->si_ssname);
998         fp = popen(buf, "r");
999         if (!fp) {
1000                 SNAPSHOT_ADD_LOG(si, "Popen fail to get fsname: %s\n",
1001                                  strerror(errno));
1002                 return -errno;
1003         }
1004
1005         if (snapshot_fgets(fp, fsname, fslen) == NULL)
1006                 rc = -EINVAL;
1007
1008         pclose(fp);
1009         return rc;
1010 }
1011
1012 static int snapshot_get_mgsnode(struct snapshot_instance *si,
1013                                 char *node, int size)
1014 {
1015         char buf[MAX_BUF_SIZE];
1016         struct snapshot_target *st;
1017         FILE *fp;
1018         int rc = 0;
1019
1020         st = list_entry(si->si_osts_list.next, struct snapshot_target,
1021                         st_list);
1022         memset(buf, 0, sizeof(buf));
1023         snprintf(buf, sizeof(buf) - 1,
1024                  "%s %s 'zfs get -H -o value lustre:mgsnode %s/%s'",
1025                  si->si_rsh, st->st_host, st->st_pool, st->st_filesystem);
1026         fp = popen(buf, "r");
1027         if (!fp) {
1028                 SNAPSHOT_ADD_LOG(si, "Popen fail to get mgsnode: %s\n",
1029                                  strerror(errno));
1030                 return -errno;
1031         }
1032
1033         if (snapshot_fgets(fp, node, size) == NULL)
1034                 rc = -EINVAL;
1035
1036         pclose(fp);
1037         return rc;
1038 }
1039
1040 static int snapshot_exists_check(struct snapshot_instance *si,
1041                                  struct snapshot_target *st)
1042 {
1043         char buf[MAX_BUF_SIZE];
1044         FILE *fp;
1045         int rc = 0;
1046
1047         memset(buf, 0, sizeof(buf));
1048         snprintf(buf, sizeof(buf) - 1,
1049                  "%s %s 'zfs list %s/%s@%s 2>/dev/null'",
1050                  si->si_rsh, st->st_host, st->st_pool,
1051                  st->st_filesystem, si->si_ssname);
1052         fp = popen(buf, "r");
1053         if (!fp) {
1054                 SNAPSHOT_ADD_LOG(si, "Popen fail to create check: %s\n",
1055                                  strerror(errno));
1056                 return -errno;
1057         }
1058
1059         if (snapshot_fgets(fp, buf, sizeof(buf)) != NULL)
1060                 rc = -EEXIST;
1061
1062         pclose(fp);
1063         return rc;
1064 }
1065
1066 static int snapshot_general_check(struct snapshot_instance *si)
1067 {
1068         return mdt0_is_lustre_snapshot(si);
1069 }
1070
1071 static void snapshot_create_usage(void)
1072 {
1073         fprintf(stderr,
1074                 "Create snapshot for the given filesystem.\n"
1075                 "Usage:\n"
1076                 "snapshot_create [-b | --barrier [on | off]] "
1077                                 "[-c | --comment comment] "
1078                                 "<-F | --fsname fsname> "
1079                                 "[-h | --help] <-n | --name ssname> "
1080                                 "[-r | --rsh remote_shell]"
1081                                 "[-t | --timeout timeout]\n"
1082                 "Options:\n"
1083                 "-b: set write barrier before creating snapshot, "
1084                         "the default value is 'on'.\n"
1085                 "-c: describe what the snapshot is for, and so on.\n"
1086                 "-F: the filesystem name.\n"
1087                 "-h: for help information.\n"
1088                 "-n: the snapshot's name.\n"
1089                 "-r: the remote shell used for communication with remote "
1090                         "target, the default value is 'ssh'.\n"
1091                 "-t: the life cycle (seconds) for write barrier, "
1092                         "the default value is %d seconds.\n",
1093                 BARRIER_TIMEOUT_DEFAULT);
1094 }
1095
1096 static int snapshot_create_check(struct snapshot_instance *si)
1097 {
1098         int rc;
1099
1100         rc = snapshot_exists_check(si, si->si_mdt0);
1101         if (rc == -EEXIST)
1102                 fprintf(stderr, "The snapshot %s exists\n", si->si_ssname);
1103
1104         return rc;
1105 }
1106
1107 static int snapshot_inherit_prop(struct snapshot_instance *si,
1108                                  struct snapshot_target *st,
1109                                  char *cmd, int size)
1110 {
1111         char buf[MAX_BUF_SIZE];
1112         FILE *fp;
1113         int len = 0;
1114         int rc = 0;
1115
1116         memset(buf, 0, sizeof(buf));
1117         snprintf(buf, sizeof(buf) - 1,
1118                  "%s %s \"zpool import -d %s %s > /dev/null 2>&1; "
1119                  "zfs get all %s/%s | grep lustre: | grep local$ | "
1120                  "awk '{ \\$1=\\\"\\\"; \\$NF=\\\"\\\"; print \\$0 }' | "
1121                  "sed -e 's/^ //'\"",
1122                  si->si_rsh, st->st_host,
1123                  st->st_dir ? st->st_dir : "/dev -d /tmp",
1124                  st->st_pool, st->st_pool, st->st_filesystem);
1125         fp = popen(buf, "r");
1126         if (!fp) {
1127                 SNAPSHOT_ADD_LOG(si, "Popen fail to list one: %s\n",
1128                                  strerror(errno));
1129                 return -errno;
1130         }
1131
1132         while (snapshot_fgets(fp, buf, MAX_BUF_SIZE) != NULL) {
1133                 char *ptr;
1134                 char *end;
1135
1136                 if (strncmp(buf, "lustre:fsname",
1137                             strlen("lustre:fsname")) == 0)
1138                         continue;
1139
1140                 if (strncmp(buf, "lustre:magic",
1141                             strlen("lustre:magic")) == 0)
1142                         continue;
1143
1144                 if (strncmp(buf, "lustre:ctime",
1145                             strlen("lustre:ctime")) == 0)
1146                         continue;
1147
1148                 if (strncmp(buf, "lustre:mtime",
1149                             strlen("lustre:mtime")) == 0)
1150                         continue;
1151
1152                 if (strncmp(buf, "lustre:comment",
1153                             strlen("lustre:comment")) == 0)
1154                         continue;
1155
1156                 if (strncmp(buf, "lustre:svname",
1157                             strlen("lustre:svname")) == 0)
1158                         continue;
1159
1160                 if (strncmp(buf, "lustre:mgsnode",
1161                             strlen("lustre:mgsnode")) == 0)
1162                         continue;
1163
1164                 ptr = strchr(buf, ' ');
1165                 if (!ptr) {
1166                         ptr = strchr(buf, '\t');
1167                         if (!ptr)
1168                                 continue;
1169                 }
1170
1171                 *ptr = '\0';
1172                 ptr++;
1173                 while (*ptr == ' ' || *ptr == '\t')
1174                         ptr++;
1175
1176                 if (*ptr == '\0')
1177                         continue;
1178
1179                 end = strchr(ptr, ' ');
1180                 if (!end)
1181                         end = strchr(buf, '\t');
1182                 if (end)
1183                         *end = '\0';
1184
1185                 rc = snprintf(cmd + len, size - len - 1,
1186                               "-o %s=\"%s\" ", buf, ptr);
1187                 if (rc <= 0)
1188                         return -EOVERFLOW;
1189
1190                 len += rc;
1191         }
1192
1193         pclose(fp);
1194         return len;
1195 }
1196
1197 static int __snapshot_create(struct snapshot_instance *si,
1198                              struct list_head *head, const char *fsname,
1199                              const char *mgsnode, __u64 xtime)
1200 {
1201         struct snapshot_target *st;
1202         pid_t pid;
1203         int rc;
1204
1205         list_for_each_entry(st, head, st_list) {
1206                 st->st_status = 0;
1207                 st->st_ignored = 0;
1208                 st->st_pid = 0;
1209
1210                 pid = fork();
1211                 if (pid < 0) {
1212                         SNAPSHOT_ADD_LOG(si, "Can't fork for create snapshot "
1213                                          "(%s@%s <%s>) on the target "
1214                                          "(%s:%x:%d): %s\n",
1215                                          fsname, si->si_ssname,
1216                                          si->si_comment, st->st_host,
1217                                          st->st_role, st->st_index,
1218                                          strerror(errno));
1219                         return pid;
1220                 }
1221
1222                 /* child */
1223                 if (pid == 0) {
1224                         char cmd[MAX_BUF_SIZE];
1225                         int len;
1226
1227                         memset(cmd, 0, sizeof(cmd));
1228                         len = snprintf(cmd, sizeof(cmd) - 1,
1229                                        "%s %s 'zfs snapshot "
1230                                        "-o lustre:fsname=%s "
1231                                        "-o lustre:magic=%s "
1232                                        "-o lustre:ctime=%llu "
1233                                        "-o lustre:mtime=%llu ",
1234                                        si->si_rsh, st->st_host, fsname,
1235                                        SNAPSHOT_MAGIC, xtime, xtime);
1236                         if (len <= 0)
1237                                 exit(-EOVERFLOW);
1238
1239                         if (si->si_comment) {
1240                                 rc = snprintf(cmd + len, sizeof(cmd) - len - 1,
1241                                               "-o lustre:comment=\"%s\" ",
1242                                               si->si_comment);
1243                                 if (rc <= 0)
1244                                         exit(-EOVERFLOW);
1245
1246                                 len += rc;
1247                         }
1248
1249                         /* Make the inherited properties as local ones,
1250                          * then even if others changed (or removed) the
1251                          * property of the parent dataset, the snapshot
1252                          * will not be affected. */
1253                         rc = snapshot_inherit_prop(si, st, cmd + len,
1254                                                    MAX_BUF_SIZE - len - 1);
1255                         if (rc < 0) {
1256                                 SNAPSHOT_ADD_LOG(si, "Can't filter property on "
1257                                                  "target (%s:%x:%d): rc = %d\n",
1258                                                  st->st_host, st->st_role,
1259                                                  st->st_index, rc);
1260
1261                                 exit(rc);
1262                         }
1263
1264                         len += rc;
1265                         if (st->st_role & SR_OST)
1266                                 rc = snprintf(cmd + len, sizeof(cmd) - len - 1,
1267                                               "-o lustre:svname=%s-OST%04x "
1268                                               "-o lustre:mgsnode=%s %s/%s@%s'",
1269                                               fsname, st->st_index, mgsnode,
1270                                               st->st_pool, st->st_filesystem,
1271                                               si->si_ssname);
1272                         else if (!(st->st_role & SR_MGS) ||
1273                                 /* MGS is on MDT0 */
1274                                  si->si_mdt0 == si->si_mgs)
1275                                 rc = snprintf(cmd + len, sizeof(cmd) - len - 1,
1276                                               "-o lustre:svname=%s-MDT%04x "
1277                                               "-o lustre:mgsnode=%s %s/%s@%s'",
1278                                               fsname, st->st_index, mgsnode,
1279                                               st->st_pool, st->st_filesystem,
1280                                               si->si_ssname);
1281                         else
1282                                 /* separated MGS */
1283                                 rc = snprintf(cmd + len, sizeof(cmd) - len - 1,
1284                                               "%s/%s@%s'", st->st_pool,
1285                                               st->st_filesystem, si->si_ssname);
1286                         if (rc <= 0)
1287                                 exit(-EOVERFLOW);
1288
1289                         rc = snapshot_exec(cmd);
1290                         if (rc)
1291                                 SNAPSHOT_ADD_LOG(si, "Can't execute \"%s\" on "
1292                                                  "target (%s:%x:%d): rc = %d\n",
1293                                                  cmd, st->st_host, st->st_role,
1294                                                  st->st_index, rc);
1295
1296                         exit(rc);
1297                 } /* end of child */
1298
1299                 /* parent continue to run more snapshot commands in parallel. */
1300                 st->st_pid = pid;
1301         }
1302
1303         return 0;
1304 }
1305
1306 static int __snapshot_destroy(struct snapshot_instance *si,
1307                               struct list_head *head);
1308
1309 static int snapshot_create(struct snapshot_instance *si)
1310 {
1311         char *__argv[3];
1312         char buf[MAX_BUF_SIZE];
1313         struct timeval tv;
1314         char new_fsname[9];
1315         int rc = 0;
1316         int rc1 = 0;
1317         int rc2 = 0;
1318
1319         rc = snapshot_create_check(si);
1320         if (rc)
1321                 return rc;
1322
1323         rc = gettimeofday(&tv, NULL);
1324         if (rc)
1325                 return rc;
1326
1327         srandom(tv.tv_usec);
1328         snprintf(new_fsname, 8, "%08x", (__u32)random());
1329         new_fsname[8] = '\0';
1330
1331         rc = snapshot_get_mgsnode(si, buf, sizeof(buf));
1332         if (rc)
1333                 return rc;
1334
1335         __argv[1] = si->si_fsname;
1336         /* 1. Get barrier */
1337         if (si->si_barrier) {
1338                 char tbuf[8];
1339
1340                 memset(tbuf, 0, sizeof(tbuf));
1341                 snprintf(tbuf, 7, "%u", si->si_timeout);
1342                 __argv[0] = "barrier_freeze";
1343                 __argv[2] = tbuf;
1344                 rc = jt_barrier_freeze(3, __argv);
1345                 if (rc) {
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);
1349
1350                         return rc;
1351                 }
1352         }
1353
1354         /* 2. Fork config llog on MGS */
1355         __argv[0] = "fork_lcfg";
1356         __argv[2] = new_fsname;
1357         rc = jt_lcfg_fork(3, __argv);
1358         if (rc) {
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);
1362                 goto out;
1363         }
1364
1365         /* 3.1 Create snapshot on every MDT */
1366         rc = __snapshot_create(si, &si->si_mdts_list, new_fsname, buf,
1367                                tv.tv_sec);
1368         if (!rc)
1369                 /* 3.2 Create snapshot on every OST */
1370                 rc = __snapshot_create(si, &si->si_osts_list, new_fsname, buf,
1371                                        tv.tv_sec);
1372
1373         /* 4. Wait for all children, even though part of them maybe failed */
1374         snapshot_wait(si, &rc1);
1375
1376 out:
1377         /* 5. Put barrier */
1378         if (si->si_barrier) {
1379                 if (!rc && !rc1) {
1380                         struct barrier_ctl bc;
1381
1382                         __argv[0] = "barrier_stat";
1383                         rc = __jt_barrier_stat(2, __argv, &bc);
1384                         if (rc) {
1385                                 SNAPSHOT_ADD_LOG(si, "Can't get barrier status "
1386                                                  "on %s: rc = %d\n",
1387                                                  si->si_fsname, rc);
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);
1392                                 rc = -ETIMEDOUT;
1393                         }
1394                 }
1395
1396                 __argv[0] = "barrier_thaw";
1397                 rc2 = jt_barrier_thaw(2, __argv);
1398                 if (rc2)
1399                         SNAPSHOT_ADD_LOG(si, "Can't release barrier on %s: "
1400                                          "rc = %d\n", si->si_fsname, rc2);
1401         }
1402
1403         /* cleanup */
1404         if (rc || rc1) {
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);
1409
1410                 __argv[0] = "erase_lcfg";
1411                 __argv[1] = new_fsname;
1412                 __argv[2] = "-q";
1413                 jt_lcfg_erase(3, __argv);
1414         }
1415
1416         return rc ? rc : (rc1 ? rc1 : rc2);
1417 }
1418
1419 int jt_snapshot_create(int argc, char **argv)
1420 {
1421         struct snapshot_instance *si;
1422         struct option lopts_create[] = {
1423                 { "barrier",    optional_argument,      0,      'b' },
1424                 { "comment",    required_argument,      0,      'c' },
1425                 { "fsname",     required_argument,      0,      'F' },
1426                 { "help",       no_argument,            0,      'h' },
1427                 { "name",       required_argument,      0,      'n' },
1428                 { "rsh",        required_argument,      0,      'r' },
1429                 { "timeout",    required_argument,      0,      't' },
1430         };
1431         int rc = 0;
1432
1433         si = snapshot_init(argc, argv, lopts_create, "b::c:F:hn:r:t:",
1434                            snapshot_create_usage, LOCK_EX, &rc);
1435         if (!si)
1436                 return rc;
1437
1438         if (!si->si_ssname) {
1439                 fprintf(stderr,
1440                         "Miss the snapshot name to be created\n");
1441                 snapshot_create_usage();
1442                 snapshot_fini(si);
1443                 return -EINVAL;
1444         }
1445
1446         rc = snapshot_create(si);
1447         if (rc) {
1448                 fprintf(stderr,
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 "
1452                                  "<%d>: %d\n",
1453                                  si->si_ssname, si->si_comment,
1454                                  si->si_barrier ? "enable" : "disable",
1455                                  si->si_barrier ? si->si_timeout : -1, rc);
1456         } else {
1457                 SNAPSHOT_ADD_LOG(si, "Create snapshot %s successfully "
1458                                  "with comment <%s>, barrier <%s>, "
1459                                  "timeout <%d>\n",
1460                                  si->si_ssname, si->si_comment,
1461                                  si->si_barrier ? "enable" : "disable",
1462                                  si->si_barrier ? si->si_timeout : -1);
1463         }
1464
1465         snapshot_fini(si);
1466         return rc;
1467 }
1468
1469 static void snapshot_destroy_usage(void)
1470 {
1471         fprintf(stderr,
1472                 "Destroy the specified snapshot.\n"
1473                 "Usage:\n"
1474                 "snapshot_destroy [-f | --force] "
1475                                  "<-F | --fsname fsname> [-h | --help] "
1476                                  "<-n | --name ssname> "
1477                                  "[-r | --rsh remote_shell]\n"
1478                 "Options:\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");
1485 }
1486
1487 static int snapshot_destroy_check(struct snapshot_instance *si)
1488 {
1489         struct list_head *head = &si->si_osts_list;
1490         struct snapshot_target *st;
1491         pid_t pid;
1492         int rc = 0;
1493
1494 again1:
1495         list_for_each_entry(st, head, st_list) {
1496                 st->st_status = 0;
1497                 st->st_ignored = 0;
1498                 st->st_pid = 0;
1499
1500                 pid = fork();
1501                 if (pid < 0) {
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,
1506                                          strerror(errno));
1507                         return pid;
1508                 }
1509
1510                 /* child */
1511                 if (pid == 0) {
1512                         rc = snapshot_exists_check(si, st);
1513                         if (!rc)
1514                                 /* The snapshot piece does not exist */
1515                                 exit(-ESRCH);
1516
1517                         exit(rc == -EEXIST ? 0: rc);
1518                 } /* end of child */
1519
1520                 /* parent continue to run more snapshot commands in parallel. */
1521                 st->st_pid = pid;
1522         }
1523
1524         if (head == &si->si_osts_list) {
1525                 head = &si->si_mdts_list;
1526                 goto again1;
1527         }
1528
1529         snapshot_wait(si, &rc);
1530         if (rc)
1531                 return rc;
1532
1533         head = &si->si_osts_list;
1534
1535 again2:
1536         list_for_each_entry(st, head, st_list) {
1537                 if (st->st_ignored && !si->si_force) {
1538                         char name[8];
1539
1540                         snapshot_role2name(name, st->st_role, st->st_index);
1541                         fprintf(stderr,
1542                                 "Miss snapshot piece on the %s. Use '-f' "
1543                                 "option if want to destroy it by force.\n",
1544                                 name);
1545
1546                         return -ENOENT;
1547                 }
1548         }
1549
1550         if (head == &si->si_osts_list) {
1551                 head = &si->si_mdts_list;
1552                 goto again2;
1553         }
1554
1555         if (!si->si_force)
1556                 rc = snapshot_general_check(si);
1557
1558         return rc;
1559 }
1560
1561 static int __snapshot_destroy(struct snapshot_instance *si,
1562                               struct list_head *head)
1563 {
1564         struct snapshot_target *st;
1565         pid_t pid;
1566         int rc;
1567
1568         list_for_each_entry(st, head, st_list) {
1569                 if (st->st_ignored)
1570                         continue;
1571
1572                 st->st_status = 0;
1573                 st->st_pid = 0;
1574
1575                 pid = fork();
1576                 if (pid < 0) {
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,
1581                                          strerror(errno));
1582                         return pid;
1583                 }
1584
1585                 /* child */
1586                 if (pid == 0) {
1587                         char cmd[MAX_BUF_SIZE];
1588
1589                         memset(cmd, 0, sizeof(cmd));
1590                         if (si->si_force)
1591                                 snprintf(cmd, sizeof(cmd) - 1,
1592                                          "%s %s 'umount -f %s/%s@%s "
1593                                          "> /dev/null 2>&1; "
1594                                          "zfs destroy -f %s/%s@%s'",
1595                                          si->si_rsh, st->st_host, st->st_pool,
1596                                          st->st_filesystem, si->si_ssname,
1597                                          st->st_pool, st->st_filesystem,
1598                                          si->si_ssname);
1599                         else
1600                                 snprintf(cmd, sizeof(cmd) - 1,
1601                                          "%s %s 'zfs destroy %s/%s@%s'",
1602                                          si->si_rsh, st->st_host, st->st_pool,
1603                                          st->st_filesystem, si->si_ssname);
1604                         rc = snapshot_exec(cmd);
1605                         if (rc)
1606                                 SNAPSHOT_ADD_LOG(si, "Can't execute \"%s\" on "
1607                                                  "target (%s:%x:%d): rc = %d\n",
1608                                                  cmd, st->st_host, st->st_role,
1609                                                  st->st_index, rc);
1610
1611                         exit(rc);
1612                 } /* end of child */
1613
1614                 /* parent continue to run more snapshot commands in parallel. */
1615                 st->st_pid = pid;
1616         }
1617
1618         return 0;
1619 }
1620
1621 static int snapshot_destroy(struct snapshot_instance *si)
1622 {
1623         char fsname[9];
1624         int rc = 0;
1625         int rc1 = 0;
1626         int rc2 = 0;
1627         int rc3 = 0;
1628
1629         rc = snapshot_destroy_check(si);
1630         if (rc)
1631                 return rc;
1632
1633         rc = snapshot_get_fsname(si, fsname, sizeof(fsname));
1634         if (rc)
1635                 return rc;
1636
1637         /* 1.1 Destroy snapshot on every OST */
1638         rc = __snapshot_destroy(si, &si->si_osts_list);
1639         if (!si->si_force) {
1640                 if (rc)
1641                         return rc;
1642
1643                 __snapshot_wait(si, &si->si_osts_list, &rc);
1644                 if (rc)
1645                         return rc;
1646         }
1647
1648         /* 1.2 Destroy snapshot on every MDT */
1649         rc1 = __snapshot_destroy(si, &si->si_mdts_list);
1650
1651         /* 2 Wait for all children, even though part of them maybe failed */
1652         snapshot_wait(si, &rc2);
1653         if (rc2 == -ENOENT && si->si_force)
1654                 rc2 = 0;
1655
1656         /* 3. Erase config llog from MGS */
1657         if ((!rc && !rc1 && !rc2) || si->si_force) {
1658                 char *__argv[3];
1659
1660                 __argv[0] = "erase_lcfg";
1661                 __argv[1] = fsname;
1662                 __argv[2] = "-q";
1663                 rc3 = jt_lcfg_erase(3, __argv);
1664                 if (rc3 && errno == ENOENT)
1665                         rc3 = 0;
1666                 if (rc3)
1667                         SNAPSHOT_ADD_LOG(si, "Can't erase config for destroy "
1668                                          "snapshot %s, fsname %s: rc = %d\n",
1669                                          si->si_ssname, fsname, rc3);
1670         }
1671
1672         return rc ? rc : (rc1 ? rc1 : (rc2 ? rc2 : rc3));
1673 }
1674
1675 int jt_snapshot_destroy(int argc, char **argv)
1676 {
1677         struct snapshot_instance *si;
1678         struct option lopts_destroy[] = {
1679                 { "force",      no_argument,            0,      'f' },
1680                 { "fsname",     required_argument,      0,      'F' },
1681                 { "help",       no_argument,            0,      'h' },
1682                 { "name",       required_argument,      0,      'n' },
1683                 { "rsh",        required_argument,      0,      'r' },
1684         };
1685         int rc = 0;
1686
1687         si = snapshot_init(argc, argv, lopts_destroy, "fF:hn:r:",
1688                            snapshot_destroy_usage, LOCK_EX, &rc);
1689         if (!si)
1690                 return rc;
1691
1692         if (!si->si_ssname) {
1693                 fprintf(stderr,
1694                         "Miss the snapshot name to be destroyed\n");
1695                 snapshot_destroy_usage();
1696                 snapshot_fini(si);
1697                 return -EINVAL;
1698         }
1699
1700         rc = snapshot_destroy(si);
1701         if (rc) {
1702                 fprintf(stderr,
1703                         "Can't destroy the snapshot %s\n", si->si_ssname);
1704                 SNAPSHOT_ADD_LOG(si, "Can't destroy snapshot %s with "
1705                                  "force <%s>: %d\n", si->si_ssname,
1706                                  si->si_force ? "enable" : "disable", rc);
1707         } else {
1708                 SNAPSHOT_ADD_LOG(si, "Destroy snapshot %s successfully "
1709                                  "with force <%s>\n", si->si_ssname,
1710                                  si->si_force ? "enable" : "disable");
1711         }
1712
1713         snapshot_fini(si);
1714         return rc;
1715 }
1716
1717 static void snapshot_modify_usage(void)
1718 {
1719         fprintf(stderr,
1720                 "Change the specified snapshot's name and/or comment.\n"
1721                 "Usage:\n"
1722                 "snapshot_modify [-c | --comment comment] "
1723                                 "<-F | --fsname fsname> [-h | --help] "
1724                                 "<-n | --name ssname> [-N | --new new_ssname] "
1725                                 "[-r | --rsh remote_shell]\n"
1726                 "Options:\n"
1727                 "-c: update the snapshot's comment.\n"
1728                 "-F: the filesystem name.\n"
1729                 "-h: for help information.\n"
1730                 "-n: the snapshot's name.\n"
1731                 "-N: rename the snapshot's name as the new_ssname.\n"
1732                 "-r: the remote shell used for communication with remote "
1733                         "target, the default value is 'ssh'.\n");
1734 }
1735
1736 static int snapshot_modify_check(struct snapshot_instance *si)
1737 {
1738         int rc;
1739
1740         if (si->si_new_ssname &&
1741             strcmp(si->si_ssname, si->si_new_ssname) == 0) {
1742                 fprintf(stderr, "The new snapshot's name is the same as "
1743                         "the old one %s %s.\n",
1744                         si->si_ssname, si->si_new_ssname);
1745                 return -EPERM;
1746         }
1747
1748         if (!si->si_new_ssname && !si->si_comment) {
1749                 fprintf(stderr, "Miss options, nothing to be changed.\n");
1750                 return -EINVAL;
1751         }
1752
1753         rc = mdt0_is_lustre_snapshot(si);
1754         if (!rc && si->si_new_ssname) {
1755                 rc = target_is_mounted(si, si->si_mdt0, si->si_ssname);
1756                 if (rc > 0) {
1757                         fprintf(stderr,
1758                                 "snapshot %s is mounted, can't be renamed.\n",
1759                                 si->si_ssname);
1760                         rc = -EBUSY;
1761                 }
1762         }
1763
1764         return rc;
1765 }
1766
1767 static int __snapshot_modify(struct snapshot_instance *si,
1768                              struct list_head *head, __u64 xtime)
1769 {
1770         struct snapshot_target *st;
1771         pid_t pid;
1772         int rc;
1773
1774         list_for_each_entry(st, head, st_list) {
1775                 st->st_status = 0;
1776                 st->st_ignored = 0;
1777                 st->st_pid = 0;
1778
1779                 pid = fork();
1780                 if (pid < 0) {
1781                         SNAPSHOT_ADD_LOG(si, "Can't fork for modify snapshot "
1782                                          "(%s|%s, <%s>) on the target "
1783                                          "(%s:%x:%d): %s\n",
1784                                          si->si_ssname, si->si_new_ssname,
1785                                          si->si_comment, st->st_host,
1786                                          st->st_role, st->st_index,
1787                                          strerror(errno));
1788                         return pid;
1789                 }
1790
1791                 /* child */
1792                 if (pid == 0) {
1793                         char cmd[MAX_BUF_SIZE];
1794
1795                         memset(cmd, 0, sizeof(cmd));
1796                         if (si->si_new_ssname && si->si_comment)
1797                                 snprintf(cmd, sizeof(cmd) - 1,
1798                                          "%s %s 'zpool import -d %s %s > "
1799                                          "/dev/null 2>&1; "
1800                                          "zfs rename %s/%s@%s %s/%s@%s && "
1801                                          "zfs set lustre:comment=\"%s\" "
1802                                          "%s/%s@%s && "
1803                                          "zfs set lustre:mtime=%llu %s/%s@%s'",
1804                                          si->si_rsh, st->st_host,
1805                                          st->st_dir ? st->st_dir :
1806                                                 "/dev -d /tmp",
1807                                          st->st_pool, st->st_pool,
1808                                          st->st_filesystem, si->si_ssname,
1809                                          st->st_pool, st->st_filesystem,
1810                                          si->si_new_ssname, si->si_comment,
1811                                          st->st_pool, st->st_filesystem,
1812                                          si->si_new_ssname, xtime,
1813                                          st->st_pool, st->st_filesystem,
1814                                          si->si_new_ssname);
1815                         else if (si->si_new_ssname)
1816                                 snprintf(cmd, sizeof(cmd) - 1,
1817                                          "%s %s 'zpool import -d %s %s > "
1818                                          "/dev/null 2>&1; "
1819                                          "zfs rename %s/%s@%s %s/%s@%s && "
1820                                          "zfs set lustre:mtime=%llu %s/%s@%s'",
1821                                          si->si_rsh, st->st_host,
1822                                          st->st_dir ? st->st_dir :
1823                                                 "/dev -d /tmp",
1824                                          st->st_pool, st->st_pool,
1825                                          st->st_filesystem, si->si_ssname,
1826                                          st->st_pool, st->st_filesystem,
1827                                          si->si_new_ssname, xtime, st->st_pool,
1828                                          st->st_filesystem, si->si_new_ssname);
1829                         else if (si->si_comment)
1830                                 snprintf(cmd, sizeof(cmd) - 1,
1831                                          "%s %s 'zpool import -d %s %s > "
1832                                          "/dev/null 2>&1; zfs set "
1833                                          "lustre:comment=\"%s\" %s/%s@%s && "
1834                                          "zfs set lustre:mtime=%llu %s/%s@%s'",
1835                                          si->si_rsh, st->st_host,
1836                                          st->st_dir ? st->st_dir :
1837                                                 "/dev -d /tmp",
1838                                          st->st_pool, si->si_comment,
1839                                          st->st_pool, st->st_filesystem,
1840                                          si->si_ssname, xtime, st->st_pool,
1841                                          st->st_filesystem, si->si_ssname);
1842                         else
1843                                 exit(-EINVAL);
1844
1845                         rc = snapshot_exec(cmd);
1846                         if (rc)
1847                                 SNAPSHOT_ADD_LOG(si, "Can't execute \"%s\" on "
1848                                                  "target (%s:%x:%d): rc = %d\n",
1849                                                  cmd, st->st_host, st->st_role,
1850                                                  st->st_index, rc);
1851
1852                         exit(rc);
1853                 } /* end of child */
1854
1855                 /* parent continue to run more snapshot commands in parallel. */
1856                 st->st_pid = pid;
1857         }
1858
1859         return 0;
1860 }
1861
1862 static int snapshot_modify(struct snapshot_instance *si)
1863 {
1864         time_t tt;
1865         int rc = 0;
1866         int rc1 = 0;
1867
1868         rc = snapshot_modify_check(si);
1869         if (rc)
1870                 return rc;
1871
1872         time(&tt);
1873
1874         /* Modify snapshot on every MDT */
1875         rc = __snapshot_modify(si, &si->si_mdts_list, (__u64)tt);
1876         if (!rc)
1877                 /* Modify snapshot on every OST */
1878                 rc = __snapshot_modify(si, &si->si_osts_list, (__u64)tt);
1879
1880         /* Wait for all children, even though part of them maybe failed */
1881         snapshot_wait(si, &rc1);
1882
1883         return rc ? rc : rc1;
1884 }
1885
1886 int jt_snapshot_modify(int argc, char **argv)
1887 {
1888         struct snapshot_instance *si;
1889         struct option lopts_modify[] = {
1890                 { "comment",    required_argument,      0,      'c' },
1891                 { "fsname",     required_argument,      0,      'F' },
1892                 { "help",       no_argument,            0,      'h' },
1893                 { "name",       required_argument,      0,      'n' },
1894                 { "new",        required_argument,      0,      'N' },
1895                 { "rsh",        required_argument,      0,      'r' },
1896         };
1897         int rc = 0;
1898
1899         si = snapshot_init(argc, argv, lopts_modify, "c:F:hn:N:r:",
1900                            snapshot_modify_usage, LOCK_EX, &rc);
1901         if (!si)
1902                 return rc;
1903
1904         if (!si->si_ssname) {
1905                 fprintf(stderr,
1906                         "Miss the snapshot name to be modified\n");
1907                 snapshot_modify_usage();
1908                 snapshot_fini(si);
1909                 return -EINVAL;
1910         }
1911
1912         rc = snapshot_modify(si);
1913         if (rc) {
1914                 fprintf(stderr,
1915                         "Can't modify the snapshot %s\n", si->si_ssname);
1916                 SNAPSHOT_ADD_LOG(si, "Can't modify snapshot %s with "
1917                                  "name <%s>, comment <%s>: %d\n",
1918                                  si->si_ssname, si->si_new_ssname,
1919                                  si->si_comment, rc);
1920         } else {
1921                 SNAPSHOT_ADD_LOG(si, "Modify snapshot %s successfully "
1922                                  "with name <%s>, comment <%s>\n",
1923                                  si->si_ssname, si->si_new_ssname,
1924                                  si->si_comment);
1925         }
1926
1927         snapshot_fini(si);
1928         return rc;
1929 }
1930
1931 static void snapshot_list_usage(void)
1932 {
1933         fprintf(stderr,
1934                 "List the specified snapshot or all snapshots.\n"
1935                 "Usage:\n"
1936                 "snapshot_list [-d | --detail] "
1937                               "<-F | --fsname fsname> [-h | --help] "
1938                               "[-n | --name ssname] [-r | --rsh remote_shell]\n"
1939                 "Options:\n"
1940                 "-d: list every piece for the specified snapshot.\n"
1941                 "-F: the filesystem name.\n"
1942                 "-h: for help information.\n"
1943                 "-n: the snapshot's name.\n"
1944                 "-r: the remote shell used for communication with remote "
1945                         "target, the default value is 'ssh'.\n");
1946 }
1947
1948 static int snapshot_list_one(struct snapshot_instance *si,
1949                              struct snapshot_target *st)
1950 {
1951         char buf[MAX_BUF_SIZE];
1952         FILE *fp;
1953         int rc;
1954
1955         memset(buf, 0, sizeof(buf));
1956         snprintf(buf, sizeof(buf) - 1,
1957                  "%s %s \"zpool import -d %s %s > /dev/null 2>&1; "
1958                  "zfs get all %s/%s@%s | grep lustre: | grep local$ | "
1959                  "awk '{ \\$1=\\\"\\\"; \\$NF=\\\"\\\"; print \\$0 }' | "
1960                  "sed -e 's/^ //'\"",
1961                  si->si_rsh, st->st_host,
1962                  st->st_dir ? st->st_dir : "/dev -d /tmp",
1963                  st->st_pool, st->st_pool, st->st_filesystem, si->si_ssname);
1964         fp = popen(buf, "r");
1965         if (!fp) {
1966                 SNAPSHOT_ADD_LOG(si, "Popen fail to list one: %s\n",
1967                                  strerror(errno));
1968                 return -errno;
1969         }
1970
1971         if (si->si_detail) {
1972                 char name[8];
1973
1974                 snapshot_role2name(name, st->st_role, st->st_index);
1975                 printf("\nsnapshot_role: %s\n", name);
1976         }
1977
1978         while (snapshot_fgets(fp, buf, MAX_BUF_SIZE) != NULL) {
1979                 __u64 xtime;
1980                 char *ptr;
1981
1982                 if (strncmp(buf, "lustre:fsname",
1983                             strlen("lustre:fsname")) == 0) {
1984                         ptr = snapshot_first_skip_blank(buf);
1985                         if (ptr)
1986                                 printf("snapshot_fsname: %s\n", ptr);
1987                         continue;
1988                 }
1989
1990                 if (strncmp(buf, "lustre:comment",
1991                             strlen("lustre:comment")) == 0) {
1992                         ptr = snapshot_first_skip_blank(buf);
1993                         if (ptr)
1994                                 printf("comment: %s\n", ptr);
1995                         continue;
1996                 }
1997
1998                 if (strncmp(buf, "lustre:ctime",
1999                             strlen("lustre:ctime")) == 0) {
2000                         ptr = snapshot_first_skip_blank(buf);
2001                         if (ptr) {
2002                                 sscanf(ptr, "%llu", &xtime);
2003                                 printf("create_time: %s",
2004                                        ctime((time_t *)&xtime));
2005                         }
2006                         continue;
2007                 }
2008
2009                 if (strncmp(buf, "lustre:mtime",
2010                             strlen("lustre:mtime")) == 0) {
2011                         ptr = snapshot_first_skip_blank(buf);
2012                         if (ptr) {
2013                                 sscanf(ptr, "%llu", &xtime);
2014                                 printf("modify_time: %s",
2015                                        ctime((time_t *)&xtime));
2016                         }
2017                         continue;
2018                 }
2019         }
2020
2021         pclose(fp);
2022         rc = target_is_mounted(si, st, si->si_ssname);
2023         if (rc < 0)
2024                 printf("status: unknown\n");
2025         else if (!rc)
2026                 printf("status: not mount\n");
2027         else
2028                 printf("status: mounted\n");
2029
2030         return rc;
2031 }
2032
2033 static int __snapshot_list(struct snapshot_instance *si,
2034                            struct list_head *head)
2035 {
2036         struct snapshot_target *st;
2037         int rc = 0;
2038
2039         list_for_each_entry(st, head, st_list) {
2040                 int rc1;
2041
2042                 rc1 = snapshot_list_one(si, st);
2043                 if (rc1 < 0 || rc >= 0)
2044                         rc = rc1;
2045         }
2046
2047         return rc;
2048 }
2049
2050 static int snapshot_list(struct snapshot_instance *si)
2051 {
2052         int rc = 0;
2053
2054         rc = snapshot_general_check(si);
2055         if (rc)
2056                 return rc;
2057
2058         printf("\nfilesystem_name: %s\nsnapshot_name: %s\n",
2059                si->si_fsname, si->si_ssname);
2060
2061         if (!si->si_detail) {
2062                 rc = snapshot_list_one(si, si->si_mdt0);
2063         } else {
2064                 int rc1;
2065
2066                 rc = __snapshot_list(si, &si->si_mdts_list);
2067                 rc1 = __snapshot_list(si, &si->si_osts_list);
2068                 if (rc >= 0)
2069                         rc = rc1;
2070         }
2071
2072         return rc < 0 ? rc : 0;
2073 }
2074
2075 static int snapshot_list_all(struct snapshot_instance *si)
2076 {
2077         struct list_sub_item {
2078                 struct list_head lsi_list;
2079                 char lsi_ssname[0];
2080         };
2081
2082         struct list_head list_sub_items;
2083         struct list_sub_item *lsi;
2084         char buf[MAX_BUF_SIZE];
2085         FILE *fp;
2086         int rc = 0;
2087
2088         INIT_LIST_HEAD(&list_sub_items);
2089         memset(buf, 0, sizeof(buf));
2090         snprintf(buf, sizeof(buf) - 1,
2091                  "%s %s \"zfs get -H -r lustre:magic %s/%s | "
2092                  "grep %s | awk '{ print \\$1 }' | cut -d@ -f2\"",
2093                  si->si_rsh, si->si_mdt0->st_host, si->si_mdt0->st_pool,
2094                  si->si_mdt0->st_filesystem, SNAPSHOT_MAGIC);
2095         fp = popen(buf, "r");
2096         if (!fp) {
2097                 SNAPSHOT_ADD_LOG(si, "Popen fail to list ssnames: %s\n",
2098                                  strerror(errno));
2099                 return -errno;
2100         }
2101
2102         while (snapshot_fgets(fp, buf, MAX_BUF_SIZE) != NULL) {
2103                 int len = strlen(buf);
2104
2105                 lsi = malloc(len + 1 + sizeof(struct list_sub_item));
2106                 if (!lsi) {
2107                         SNAPSHOT_ADD_LOG(si, "NOT enough memory\n");
2108                         rc = -ENOMEM;
2109                         break;
2110                 }
2111
2112                 strncpy(lsi->lsi_ssname, buf, len);
2113                 lsi->lsi_ssname[len] = '\0';
2114                 list_add(&lsi->lsi_list, &list_sub_items);
2115         }
2116
2117         pclose(fp);
2118         while (!list_empty(&list_sub_items)) {
2119                 lsi = list_entry(list_sub_items.next,
2120                                  struct list_sub_item, lsi_list);
2121                 list_del(&lsi->lsi_list);
2122                 if (!rc) {
2123                         si->si_ssname = lsi->lsi_ssname;
2124                         rc = snapshot_list(si);
2125                         si->si_ssname = NULL;
2126                 }
2127
2128                 free(lsi);
2129         }
2130
2131         return rc;
2132 }
2133
2134 int jt_snapshot_list(int argc, char **argv)
2135 {
2136         struct snapshot_instance *si;
2137         struct option lopts_list[] = {
2138                 { "detail",     no_argument,            0,      'd' },
2139                 { "fsname",     required_argument,      0,      'F' },
2140                 { "help",       no_argument,            0,      'h' },
2141                 { "name",       required_argument,      0,      'n' },
2142                 { "rsh",        required_argument,      0,      'r' },
2143         };
2144         int rc = 0;
2145
2146         si = snapshot_init(argc, argv, lopts_list, "dF:hn:r:",
2147                            snapshot_list_usage, LOCK_SH, &rc);
2148         if (!si)
2149                 return rc;
2150
2151         if (si->si_ssname)
2152                 rc = snapshot_list(si);
2153         else
2154                 rc = snapshot_list_all(si);
2155
2156         if (rc) {
2157                 fprintf(stderr,
2158                         "Can't list the snapshot %s\n", si->si_ssname);
2159                 SNAPSHOT_ADD_LOG(si, "Can't list snapshot %s with detail "
2160                                  "<%s>: %d\n", si->si_ssname,
2161                                  si->si_detail ? "yes" : "no", rc);
2162         }
2163
2164         snapshot_fini(si);
2165         return rc;
2166 }
2167
2168 static void snapshot_mount_usage(void)
2169 {
2170         fprintf(stderr,
2171                 "Mount the specified snapshot.\n"
2172                 "Usage:\n"
2173                 "snapshot_mount <-F | --fsname fsname> [-h | --help] "
2174                                "<-n | --name ssname> "
2175                                "[-r | --rsh remote_shell]\n"
2176                 "Options:\n"
2177                 "-F: the filesystem name.\n"
2178                 "-h: for help information.\n"
2179                 "-n: the snapshot's name.\n"
2180                 "-r: the remote shell used for communication with remote "
2181                         "target, the default value is 'ssh'.\n");
2182 }
2183
2184 static int snapshot_mount_check(struct snapshot_instance *si, char *fsname,
2185                                 int fslen, bool *mgs_running)
2186 {
2187         int rc;
2188
2189         rc = mdt0_is_lustre_snapshot(si);
2190         if (rc)
2191                 return rc;
2192
2193         rc = snapshot_get_fsname(si, fsname, fslen);
2194         if (rc < 0)
2195                 return rc;
2196
2197         rc = target_is_mounted(si, si->si_mgs, NULL);
2198         if (rc > 0) {
2199                 *mgs_running = true;
2200                 rc = 0;
2201         }
2202
2203         return rc;
2204 }
2205
2206 static int snapshot_mount_target(struct snapshot_instance *si,
2207                                  struct snapshot_target *st, const char *optstr)
2208 {
2209         char cmd[MAX_BUF_SIZE];
2210         char name[8];
2211         int rc;
2212
2213         rc = target_is_mounted(si, st, si->si_ssname);
2214         if (rc < 0)
2215                 return rc;
2216
2217         if (rc > 0)
2218                 return -ESRCH;
2219
2220         memset(cmd, 0, sizeof(cmd));
2221         memset(name, 0, sizeof(name));
2222         snapshot_role2name(name, st->st_role, st->st_index);
2223         snprintf(cmd, sizeof(cmd) - 1,
2224                  "%s %s 'zpool import -d %s %s > /dev/null 2>&1; "
2225                  "mkdir -p /mnt/%s_%s && mount -t lustre "
2226                  "-o rdonly_dev%s %s/%s@%s /mnt/%s_%s'",
2227                  si->si_rsh, st->st_host,
2228                  st->st_dir ? st->st_dir : "/dev -d /tmp",
2229                  st->st_pool, si->si_ssname, name,
2230                  st != si->si_mdt0 ? "" : optstr,
2231                  st->st_pool, st->st_filesystem, si->si_ssname,
2232                  si->si_ssname, name);
2233         rc = snapshot_exec(cmd);
2234         if (rc)
2235                 SNAPSHOT_ADD_LOG(si, "Can't execute \"%s\" on the target "
2236                                  "(%s:%x:%d): rc = %d\n", cmd, st->st_host,
2237                                  st->st_role, st->st_index, rc);
2238
2239         return rc;
2240 }
2241
2242 static int __snapshot_mount(struct snapshot_instance *si,
2243                             struct list_head *head)
2244 {
2245         struct snapshot_target *st;
2246         pid_t pid;
2247         int rc;
2248
2249         list_for_each_entry(st, head, st_list) {
2250                 if (st == si->si_mgs || st == si->si_mdt0)
2251                         continue;
2252
2253                 st->st_status = 0;
2254                 st->st_ignored = 0;
2255                 st->st_pid = 0;
2256
2257                 pid = fork();
2258                 if (pid < 0) {
2259                         SNAPSHOT_ADD_LOG(si, "Can't fork for mount snapshot "
2260                                          "%s on target (%s:%x:%d): %s\n",
2261                                          si->si_ssname, st->st_host,
2262                                          st->st_role, st->st_index,
2263                                          strerror(errno));
2264                         return pid;
2265                 }
2266
2267                 /* child */
2268                 if (pid == 0) {
2269                         rc = snapshot_mount_target(si, st, "");
2270                         exit(rc);
2271                 }
2272
2273                 /* parent continue to run more snapshot commands in parallel. */
2274                 st->st_pid = pid;
2275         }
2276
2277         return 0;
2278 }
2279
2280 static int __snapshot_umount(struct snapshot_instance *si,
2281                              struct list_head *head);
2282
2283 static int snapshot_mount(struct snapshot_instance *si)
2284 {
2285         struct snapshot_target *st;
2286         int needed = 0;
2287         int failed = 0;
2288         int rc = 0;
2289         int rc1 = 0;
2290         char fsname[9];
2291         bool mdt0_mounted = false;
2292         bool mgs_running = false;
2293
2294         rc = snapshot_mount_check(si, fsname, sizeof(fsname), &mgs_running);
2295         if (rc < 0) {
2296                 fprintf(stderr,
2297                         "Can't mount the snapshot %s: %s\n",
2298                         si->si_ssname, strerror(-rc));
2299                 return rc;
2300         }
2301
2302         /* 1. MGS is not mounted yet, mount the MGS firstly */
2303         si->si_mgs->st_ignored = 0;
2304         si->si_mgs->st_pid = 0;
2305         if (!mgs_running) {
2306                 rc = snapshot_mount_target(si, si->si_mgs, "");
2307                 if (rc == -ESRCH) {
2308                         si->si_mgs->st_ignored = 1;
2309                         rc = 0;
2310                 }
2311
2312                 if (rc < 0) {
2313                         fprintf(stderr,
2314                                 "Can't mount the snapshot %s: %s\n",
2315                                 si->si_ssname, strerror(-rc));
2316                         return rc;
2317                 }
2318
2319                 if (si->si_mgs == si->si_mdt0)
2320                         mdt0_mounted = true;
2321         }
2322
2323         /* 2. Mount MDT0 if it is not combined with the MGS. */
2324         if (!mdt0_mounted) {
2325                 si->si_mdt0->st_ignored = 0;
2326                 si->si_mdt0->st_pid = 0;
2327                 rc = snapshot_mount_target(si, si->si_mdt0, ",nomgs");
2328                 if (rc)
2329                         goto cleanup;
2330         }
2331
2332         /* 3.1 Mount other MDTs in parallel */
2333         rc = __snapshot_mount(si, &si->si_mdts_list);
2334         if (!rc)
2335                 /* 3.2 Mount OSTs in parallel */
2336                 rc = __snapshot_mount(si, &si->si_osts_list);
2337
2338         /* Wait for all children, even though part of them maybe failed */
2339         failed = snapshot_wait(si, &rc1);
2340
2341         list_for_each_entry(st, &si->si_mdts_list, st_list) {
2342                 if (!st->st_ignored)
2343                         needed++;
2344         }
2345
2346         list_for_each_entry(st, &si->si_osts_list, st_list) {
2347                 if (!st->st_ignored)
2348                         needed++;
2349         }
2350
2351 cleanup:
2352         if (rc || rc1) {
2353                 int rc2 = 0;
2354
2355                 __snapshot_umount(si, &si->si_mdts_list);
2356                 __snapshot_umount(si, &si->si_osts_list);
2357                 snapshot_wait(si, &rc2);
2358
2359                 if (rc)
2360                         fprintf(stderr,
2361                                 "Can't mount the snapshot %s: %s\n",
2362                                 si->si_ssname, strerror(-rc));
2363                 else
2364                         fprintf(stderr,
2365                                 "%d of %d pieces of the snapshot %s "
2366                                 "can't be mounted: %s\n",
2367                                 failed, needed, si->si_ssname, strerror(-rc1));
2368
2369                 return rc ? rc : rc1;
2370         }
2371
2372         if (needed == 0) {
2373                 fprintf(stderr,
2374                         "The snapshot %s has already been mounted by other\n",
2375                         si->si_ssname);
2376                 return -EALREADY;
2377         }
2378
2379         fprintf(stdout, "mounted the snapshot %s with fsname %s\n",
2380                 si->si_ssname, fsname);
2381
2382         return 0;
2383 }
2384
2385 int jt_snapshot_mount(int argc, char **argv)
2386 {
2387         struct snapshot_instance *si;
2388         struct option lopts_mount[] = {
2389                 { "fsname",     required_argument,      0,      'F' },
2390                 { "help",       no_argument,            0,      'h' },
2391                 { "name",       required_argument,      0,      'n' },
2392                 { "rsh",        required_argument,      0,      'r' },
2393         };
2394         int rc = 0;
2395
2396         si = snapshot_init(argc, argv, lopts_mount, "F:hn:r:",
2397                            snapshot_mount_usage, LOCK_EX, &rc);
2398         if (!si)
2399                 return rc;
2400
2401         if (!si->si_ssname) {
2402                 fprintf(stderr,
2403                         "Miss the snapshot name to be mounted\n");
2404                 snapshot_mount_usage();
2405                 snapshot_fini(si);
2406                 return -EINVAL;
2407         }
2408
2409         rc = snapshot_mount(si);
2410         if (rc)
2411                 SNAPSHOT_ADD_LOG(si, "Can't mount snapshot %s: %d\n",
2412                                  si->si_ssname, rc);
2413         else
2414                 SNAPSHOT_ADD_LOG(si, "The snapshot %s is mounted\n",
2415                                  si->si_ssname);
2416
2417         snapshot_fini(si);
2418         return rc;
2419
2420 }
2421
2422 static void snapshot_umount_usage(void)
2423 {
2424         fprintf(stderr,
2425                 "Umount the specified snapshot.\n"
2426                 "Usage:\n"
2427                 "snapshot_umount <-F | --fsname fsname> [-h | --help] "
2428                                 "<-n | --name ssname> "
2429                                 "[-r | --rsh remote_shell]\n"
2430                 "Options:\n"
2431                 "-F: the filesystem name.\n"
2432                 "-h: for help information.\n"
2433                 "-n: the snapshot's name.\n"
2434                 "-r: the remote shell used for communication with remote "
2435                         "target, the default value is 'ssh'.\n");
2436 }
2437
2438 static int __snapshot_umount(struct snapshot_instance *si,
2439                              struct list_head *head)
2440 {
2441         struct snapshot_target *st;
2442         pid_t pid;
2443         int rc;
2444
2445         list_for_each_entry(st, head, st_list) {
2446                 st->st_status = 0;
2447                 st->st_ignored = 0;
2448                 st->st_pid = 0;
2449
2450                 pid = fork();
2451                 if (pid < 0) {
2452                         SNAPSHOT_ADD_LOG(si, "Can't fork for umount snapshot "
2453                                          "%s on target (%s:%x:%d): %s\n",
2454                                          si->si_ssname, st->st_host,
2455                                          st->st_role, st->st_index,
2456                                          strerror(errno));
2457                         return pid;
2458                 }
2459
2460                 /* child */
2461                 if (pid == 0) {
2462                         char cmd[MAX_BUF_SIZE];
2463
2464                         rc = target_is_mounted(si, st, si->si_ssname);
2465                         if (rc < 0)
2466                                 exit(rc);
2467
2468                         if (!rc)
2469                                 exit(-ESRCH);
2470
2471                         memset(cmd, 0, sizeof(cmd));
2472                         snprintf(cmd, sizeof(cmd) - 1,
2473                                  "%s %s 'umount %s/%s@%s'",
2474                                  si->si_rsh, st->st_host, st->st_pool,
2475                                  st->st_filesystem, si->si_ssname);
2476                         rc = snapshot_exec(cmd);
2477
2478                         exit(rc);
2479                 }
2480
2481                 /* parent continue to run more snapshot commands in parallel. */
2482                 st->st_pid = pid;
2483         }
2484
2485         return 0;
2486 }
2487
2488 static int snapshot_umount(struct snapshot_instance *si)
2489 {
2490         struct snapshot_target *st;
2491         int needed = 0;
2492         int failed;
2493         int rc = 0;
2494         int rc1 = 0;
2495         int rc2 = 0;
2496
2497         rc = snapshot_general_check(si);
2498         if (rc < 0) {
2499                 fprintf(stderr,
2500                         "Can't umount the snapshot %s: %s\n",
2501                         si->si_ssname, strerror(-rc));
2502                 return rc;
2503         }
2504
2505         rc = __snapshot_umount(si, &si->si_mdts_list);
2506         rc1 = __snapshot_umount(si, &si->si_osts_list);
2507         failed = snapshot_wait(si, &rc2);
2508
2509         list_for_each_entry(st, &si->si_mdts_list, st_list) {
2510                 if (!st->st_ignored)
2511                         needed++;
2512         }
2513
2514         list_for_each_entry(st, &si->si_osts_list, st_list) {
2515                 if (!st->st_ignored)
2516                         needed++;
2517         }
2518
2519         if (needed == 0) {
2520                 fprintf(stderr,
2521                         "The snapshot %s has not been mounted\n",
2522                         si->si_ssname);
2523                 return -EALREADY;
2524         }
2525
2526         if (failed != 0)
2527                 fprintf(stderr,
2528                         "%d of %d pieces of the snapshot %s "
2529                         "can't be umounted: %s\n",
2530                         failed, needed, si->si_ssname, strerror(-rc2));
2531
2532         return rc ? rc : (rc1 ? rc1 : rc2);
2533 }
2534
2535 int jt_snapshot_umount(int argc, char **argv)
2536 {
2537         struct snapshot_instance *si;
2538         struct option lopts_umount[] = {
2539                 { "fsname",     required_argument,      0,      'F' },
2540                 { "help",       no_argument,            0,      'h' },
2541                 { "name",       required_argument,      0,      'n' },
2542                 { "rsh",        required_argument,      0,      'r' },
2543         };
2544         int rc = 0;
2545
2546         si = snapshot_init(argc, argv, lopts_umount, "F:hn:r:",
2547                            snapshot_umount_usage, LOCK_EX, &rc);
2548         if (!si)
2549                 return rc;
2550
2551         if (!si->si_ssname) {
2552                 fprintf(stderr,
2553                         "Miss the snapshot name to be umounted\n");
2554                 snapshot_umount_usage();
2555                 snapshot_fini(si);
2556                 return -EINVAL;
2557         }
2558
2559         rc = snapshot_umount(si);
2560         if (rc < 0)
2561                 SNAPSHOT_ADD_LOG(si, "Can't umount snapshot %s: %d\n",
2562                                  si->si_ssname, rc);
2563         else
2564                 SNAPSHOT_ADD_LOG(si, "the snapshot %s have been umounted\n",
2565                                  si->si_ssname);
2566
2567         snapshot_fini(si);
2568         return rc;
2569 }