HAMMER Utillities: Sync with 61D
[games.git] / sbin / hammer / cmd_pseudofs.c
1 /*
2  * Copyright (c) 2008 The DragonFly Project.  All rights reserved.
3  * 
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@backplane.com>
6  * 
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  * 3. Neither the name of The DragonFly Project nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific, prior written permission.
20  * 
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  * 
34  * $DragonFly: src/sbin/hammer/cmd_pseudofs.c,v 1.6 2008/07/12 02:48:46 dillon Exp $
35  */
36
37 #include "hammer.h"
38
39 static void parse_pfsd_options(char **av, int ac, hammer_pseudofs_data_t pfsd);
40 static void init_pfsd(hammer_pseudofs_data_t pfsd, int is_slave);
41 static void dump_pfsd(hammer_pseudofs_data_t pfsd);
42 static void pseudofs_usage(int code);
43 static int getyn(void);
44
45 /*
46  * Calculate the pfs_id given a path to a directory or a @@PFS or @@%llx:%d
47  * softlink.
48  */
49 int
50 getpfs(struct hammer_ioc_pseudofs_rw *pfs, const char *path)
51 {
52         hammer_tid_t dummy_tid;
53         struct stat st;
54         char *dirpath;
55         char buf[64];
56         int fd;
57         int n;
58
59         bzero(pfs, sizeof(*pfs));
60         pfs->ondisk = malloc(sizeof(*pfs->ondisk));
61         bzero(pfs->ondisk, sizeof(*pfs->ondisk));
62         pfs->bytes = sizeof(*pfs->ondisk);
63
64         /*
65          * Calculate the directory containing the softlink
66          */
67         dirpath = strdup(path);
68         if (strrchr(dirpath, '/'))
69                 *strrchr(dirpath, '/') = 0;
70         else
71                 dirpath = strdup(".");
72
73         if (lstat(path, &st) == 0 && S_ISLNK(st.st_mode)) {
74                 /*
75                  * Avoid foot-shooting.  Don't let the user access a PFS
76                  * softlink via a PFS.  PFS softlinks may only be accessed
77                  * via the master filesystem.
78                  */
79                 fd = open(dirpath, O_RDONLY);
80                 if (fd < 0)
81                         goto done;
82                 pfs->pfs_id = -1;
83                 ioctl(fd, HAMMERIOC_GET_PSEUDOFS, pfs);
84                 if (pfs->pfs_id != 0) {
85                         fprintf(stderr,
86                                 "You are attempting to access a PFS softlink "
87                                 "from a PFS.  It may not represent the PFS\n"
88                                 "on the main filesystem mount that you "
89                                 "expect!  You may only access PFS softlinks\n"
90                                 "via the main filesystem mount!\n");
91                         exit(1);
92                 }
93                 close(fd);
94
95                 /*
96                  * Extract the PFS from the link.  HAMMER will automatically
97                  * convert @@PFS%05d links so if actually see one in that
98                  * form the target PFS may not exist or may be corrupt.  But
99                  * we can extract the PFS id anyway.
100                  */
101                 n = readlink(path, buf, sizeof(buf) - 1);
102                 if (n < 0)
103                         n = 0;
104                 buf[n] = 0;
105                 if (sscanf(buf, "@@PFS%d", &pfs->pfs_id) == 1) {
106                         fd = open(dirpath, O_RDONLY);
107                         goto done;
108                 }
109                 if (sscanf(buf, "@@%llx:%d", &dummy_tid, &pfs->pfs_id) == 2) {
110                         fd = open(dirpath, O_RDONLY);
111                         goto done;
112                 }
113         }
114
115         /*
116          * Try to open the path and request the pfs_id that way.
117          */
118         fd = open(path, O_RDONLY);
119         if (fd >= 0) {
120                 pfs->pfs_id = -1;
121                 ioctl(fd, HAMMERIOC_GET_PSEUDOFS, pfs);
122                 if (pfs->pfs_id == -1) {
123                         close(fd);
124                         fd = -1;
125                 }
126         }
127
128         /*
129          * Cleanup
130          */
131 done:
132         if (fd < 0) {
133                 fprintf(stderr, "Cannot access PFS %s: %s\n",
134                         path, strerror(errno));
135                 exit(1);
136         }
137         if (ioctl(fd, HAMMERIOC_GET_PSEUDOFS, pfs) < 0) {
138                 fprintf(stderr, "Cannot access PFS %s: %s\n",
139                         path, strerror(errno));
140                 exit(1);
141         }
142         free(dirpath);
143         return(fd);
144 }
145
146 void
147 hammer_cmd_pseudofs_status(char **av, int ac)
148 {
149         struct hammer_ioc_pseudofs_rw pfs;
150         int i;
151         int fd;
152
153         for (i = 0; i < ac; ++i) {
154                 printf("%s\t", av[i]);
155                 fd = getpfs(&pfs, av[i]);
156                 if (fd < 0 || ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs) < 0) {
157                         printf("Not a HAMMER root\n");
158                 } else {
159                         printf("PFS #%d {\n", pfs.pfs_id);
160                         dump_pfsd(pfs.ondisk);
161                         printf("}\n");
162                 }
163                 if (fd >= 0)
164                         close(fd);
165                 if (pfs.ondisk)
166                         free(pfs.ondisk);
167         }
168 }
169
170 void
171 hammer_cmd_pseudofs_create(char **av, int ac, int is_slave)
172 {
173         struct hammer_ioc_pseudofs_rw pfs;
174         struct hammer_pseudofs_data pfsd;
175         struct stat st;
176         const char *path;
177         char *dirpath;
178         char *linkpath;
179         int pfs_id;
180         int fd;
181         int error;
182
183         if (ac == 0)
184                 pseudofs_usage(1);
185         path = av[0];
186         if (lstat(path, &st) == 0) {
187                 fprintf(stderr, "cannot create %s, file exists!\n", path);
188                 exit(1);
189         }
190
191         dirpath = strdup(path);
192         if (strrchr(dirpath, '/') != NULL)
193                 *strrchr(dirpath, '/') = 0;
194         else
195                 dirpath = strdup(".");
196         fd = open(dirpath, O_RDONLY);
197         if (fd < 0) {
198                 fprintf(stderr, "Cannot open directory %s\n", dirpath);
199                 exit(1);
200         }
201
202         error = 0;
203         for (pfs_id = 0; pfs_id < HAMMER_MAX_PFS; ++pfs_id) {
204                 bzero(&pfs, sizeof(pfs));
205                 bzero(&pfsd, sizeof(pfsd));
206                 pfs.pfs_id = pfs_id;
207                 pfs.ondisk = &pfsd;
208                 pfs.bytes = sizeof(pfsd);
209                 pfs.version = HAMMER_IOC_PSEUDOFS_VERSION;
210                 if (ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs) < 0) {
211                         error = errno;
212                         break;
213                 }
214         }
215         if (pfs_id == HAMMER_MAX_PFS) {
216                 fprintf(stderr, "Cannot create %s, all PFSs in use\n", path);
217                 exit(1);
218         }
219         if (error != ENOENT) {
220                 fprintf(stderr, "Cannot create %s, got %s during scan\n",
221                         path, strerror(error));
222                 exit(1);
223         }
224
225         /*
226          * Create the new PFS
227          */
228         printf("Creating PFS #%d\t", pfs_id);
229         bzero(&pfsd, sizeof(pfsd));
230         init_pfsd(&pfsd, is_slave);
231         pfs.pfs_id = pfs_id;
232         pfs.ondisk = &pfsd;
233         pfs.bytes = sizeof(pfsd);
234         pfs.version = HAMMER_IOC_PSEUDOFS_VERSION;
235
236         if (ioctl(fd, HAMMERIOC_SET_PSEUDOFS, &pfs) < 0) {
237                 printf("failed: %s\n", strerror(errno));
238         } else {
239                 /* special symlink, must be exactly 10 characters */
240                 asprintf(&linkpath, "@@PFS%05d", pfs_id);
241                 if (symlink(linkpath, path) < 0) {
242                         printf("failed: cannot create symlink: %s\n",
243                                 strerror(errno));
244                 } else {
245                         printf("succeeded!\n");
246                         hammer_cmd_pseudofs_update(av, ac);
247                 }
248         }
249         close(fd);
250 }
251
252 void
253 hammer_cmd_pseudofs_destroy(char **av, int ac)
254 {
255         struct hammer_ioc_pseudofs_rw pfs;
256         struct stat st;
257         int fd;
258         int i;
259
260         if (ac == 0)
261                 pseudofs_usage(1);
262         bzero(&pfs, sizeof(pfs));
263         fd = getpfs(&pfs, av[0]);
264
265         if (pfs.pfs_id == 0) {
266                 fprintf(stderr, "You cannot destroy PFS#0\n");
267                 exit(1);
268         }
269         printf("You have requested that PFS#%d (%s) be destroyed\n",
270                 pfs.pfs_id, pfs.ondisk->label);
271         printf("This will irrevocably destroy all data on this PFS!!!!!\n");
272         printf("Do you really want to do this? ");
273         fflush(stdout);
274         if (getyn() == 0) {
275                 fprintf(stderr, "No action taken on PFS#%d\n", pfs.pfs_id);
276                 exit(1);
277         }
278
279         if ((pfs.ondisk->mirror_flags & HAMMER_PFSD_SLAVE) == 0) {
280                 printf("This PFS is currently setup as a MASTER!\n");
281                 printf("Are you absolutely sure you want to destroy it? ");
282                 fflush(stdout);
283                 if (getyn() == 0) {
284                         fprintf(stderr, "No action taken on PFS#%d\n",
285                                 pfs.pfs_id);
286                         exit(1);
287                 }
288         }
289
290         printf("Destroying PFS #%d (%s) in ", pfs.pfs_id, pfs.ondisk->label);
291         for (i = 5; i; --i) {
292                 printf(" %d", i);
293                 fflush(stdout);
294                 sleep(1);
295         }
296         printf(".. starting destruction pass\n");
297         fflush(stdout);
298
299         /*
300          * Set the sync_beg_tid and sync_end_tid's to 1, once we start the
301          * RMR the PFS is basically destroyed even if someone ^C's it.
302          */
303         pfs.ondisk->mirror_flags |= HAMMER_PFSD_SLAVE;
304         pfs.ondisk->master_id = -1;
305         pfs.ondisk->sync_beg_tid = 1;
306         pfs.ondisk->sync_end_tid = 1;
307
308         if (ioctl(fd, HAMMERIOC_SET_PSEUDOFS, &pfs) < 0) {
309                 fprintf(stderr, "Unable to update the PFS configuration: %s\n",
310                         strerror(errno));
311                 exit(1);
312         }
313
314         /*
315          * Ok, do it.  Remove the softlink on success.
316          */
317         if (ioctl(fd, HAMMERIOC_RMR_PSEUDOFS, &pfs) == 0) {
318                 printf("pfs-destroy of PFS#%d succeeded!\n", pfs.pfs_id);
319                 if (lstat(av[0], &st) == 0 && S_ISLNK(st.st_mode)) {
320                         if (remove(av[0]) < 0) {
321                                 fprintf(stderr, "Unable to remove softlink: %s "
322                                         "(but the PFS has been destroyed)\n",
323                                         av[0]);
324                                 /* exit status 0 anyway */
325                         }
326                 }
327         } else {
328                 printf("pfs-destroy of PFS#%d failed: %s\n",
329                         pfs.pfs_id, strerror(errno));
330         }
331 }
332
333 void
334 hammer_cmd_pseudofs_upgrade(char **av, int ac)
335 {
336         struct hammer_ioc_pseudofs_rw pfs;
337         int fd;
338
339         if (ac == 0)
340                 pseudofs_usage(1);
341         bzero(&pfs, sizeof(pfs));
342         fd = getpfs(&pfs, av[0]);
343
344         if (pfs.pfs_id == 0) {
345                 fprintf(stderr, "You cannot upgrade PFS#0"
346                                 " (It should already be a master)\n");
347                 exit(1);
348         }
349         if (pfs.ondisk->master_id == -1) {
350                 fprintf(stderr, "You must configure a master id before "
351                                 "upgrading a slave to a master.\n"
352                                 "Use pfs-update ... master=<id> first.\n");
353                 exit(1);
354         }
355
356         if (ioctl(fd, HAMMERIOC_UPG_PSEUDOFS, &pfs) == 0) {
357                 printf("pfs-upgrade of PFS#%d (%s) succeeded\n",
358                         pfs.pfs_id, pfs.ondisk->label);
359         } else {
360                 fprintf(stderr, "pfs-upgrade of PFS#%d (%s) failed: %s\n",
361                         pfs.pfs_id, pfs.ondisk->label, strerror(errno));
362         }
363 }
364
365 void
366 hammer_cmd_pseudofs_downgrade(char **av, int ac)
367 {
368         struct hammer_ioc_pseudofs_rw pfs;
369         int fd;
370
371         if (ac == 0)
372                 pseudofs_usage(1);
373         bzero(&pfs, sizeof(pfs));
374         fd = getpfs(&pfs, av[0]);
375
376         if (pfs.pfs_id == 0) {
377                 fprintf(stderr, "You cannot downgrade PFS#0\n");
378                 exit(1);
379         }
380
381         if (ioctl(fd, HAMMERIOC_DGD_PSEUDOFS, &pfs) == 0) {
382                 printf("pfs-downgrade of PFS#%d (%s) succeeded\n",
383                         pfs.pfs_id, pfs.ondisk->label);
384         } else {
385                 fprintf(stderr, "pfs-upgrade of PFS#%d (%s) failed: %s\n",
386                         pfs.pfs_id, pfs.ondisk->label, strerror(errno));
387         }
388 }
389
390 void
391 hammer_cmd_pseudofs_update(char **av, int ac)
392 {
393         struct hammer_ioc_pseudofs_rw pfs;
394         int fd;
395
396         if (ac == 0)
397                 pseudofs_usage(1);
398         bzero(&pfs, sizeof(pfs));
399         fd = getpfs(&pfs, av[0]);
400
401         printf("%s\n", av[0]);
402         fflush(stdout);
403
404         if (ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs) == 0) {
405                 parse_pfsd_options(av + 1, ac - 1, pfs.ondisk);
406                 if ((pfs.ondisk->mirror_flags & HAMMER_PFSD_SLAVE) &&
407                     pfs.pfs_id == 0) {
408                         printf("The real mount point cannot be made a PFS "
409                                "slave, only PFS sub-directories can be made "
410                                "slaves\n");
411                         exit(1);
412                 }
413                 pfs.bytes = sizeof(*pfs.ondisk);
414                 if (ioctl(fd, HAMMERIOC_SET_PSEUDOFS, &pfs) == 0) {
415                         if (ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs) == 0) {
416                                 dump_pfsd(pfs.ondisk);
417                         } else {
418                                 printf("Unable to retrieve pfs configuration after successful update: %s\n", strerror(errno));
419                                 exit(1);
420                         }
421                 } else {
422                         printf("Unable to adjust pfs configuration: %s\n", strerror(errno));
423                         exit(1);
424                 }
425         }
426 }
427
428 static void
429 init_pfsd(hammer_pseudofs_data_t pfsd, int is_slave)
430 {
431         uint32_t status;
432
433         pfsd->sync_beg_tid = 1;
434         pfsd->sync_end_tid = 1;
435         pfsd->sync_beg_ts = 0;
436         pfsd->sync_end_ts = 0;
437         uuid_create(&pfsd->shared_uuid, &status);
438         uuid_create(&pfsd->unique_uuid, &status);
439         if (is_slave) {
440                 pfsd->master_id = -1;
441                 pfsd->mirror_flags |= HAMMER_PFSD_SLAVE;
442         } else {
443                 pfsd->master_id = 0;
444         }
445 }
446
447 static
448 void
449 dump_pfsd(hammer_pseudofs_data_t pfsd)
450 {
451         u_int32_t status;
452         char *str = NULL;
453
454         printf("    sync-beg-tid=0x%016llx\n", pfsd->sync_beg_tid);
455         printf("    sync-end-tid=0x%016llx\n", pfsd->sync_end_tid);
456         uuid_to_string(&pfsd->shared_uuid, &str, &status);
457         printf("    shared-uuid=%s\n", str);
458         uuid_to_string(&pfsd->unique_uuid, &str, &status);
459         printf("    unique-uuid=%s\n", str);
460         if (pfsd->mirror_flags & HAMMER_PFSD_SLAVE) {
461                 printf("    slave\n");
462         }
463         if (pfsd->master_id < 0) {
464                 if (pfsd->mirror_flags & HAMMER_PFSD_SLAVE) {
465                         printf("    master=-1 "
466                                "(no upgrade id assigned for slave)\n");
467                 } else {
468                         printf("    no-mirror "
469                                "(mirror TID propagation disabled)\n");
470                 }
471         } else {
472                 printf("    master=%d", pfsd->master_id);
473                 if (pfsd->mirror_flags & HAMMER_PFSD_SLAVE)
474                         printf(" (only if upgraded, currently a slave)");
475                 printf("\n");
476         }
477         printf("    label=\"%s\"\n", pfsd->label);
478 }
479
480 static void
481 parse_pfsd_options(char **av, int ac, hammer_pseudofs_data_t pfsd)
482 {
483         char *cmd;
484         char *ptr;
485         int len;
486         uint32_t status;
487
488         while (ac) {
489                 cmd = *av;
490                 if ((ptr = strchr(cmd, '=')) != NULL)
491                         *ptr++ = 0;
492
493                 /*
494                  * Basic assignment value test
495                  */
496                 if (strcmp(cmd, "no-mirror") == 0 ||
497                     strcmp(cmd, "slave") == 0) {
498                         if (ptr) {
499                                 fprintf(stderr,
500                                         "option %s should not have "
501                                         "an assignment\n",
502                                         cmd);
503                                 exit(1);
504                         }
505                 } else {
506                         if (ptr == NULL) {
507                                 fprintf(stderr,
508                                         "option %s requires an assignment\n",
509                                         cmd);
510                                 exit(1);
511                         }
512                 }
513
514                 status = uuid_s_ok;
515                 if (strcmp(cmd, "sync-beg-tid") == 0) {
516                         pfsd->sync_beg_tid = strtoull(ptr, NULL, 16);
517                 } else if (strcmp(cmd, "sync-end-tid") == 0) {
518                         pfsd->sync_end_tid = strtoull(ptr, NULL, 16);
519                 } else if (strcmp(cmd, "shared-uuid") == 0) {
520                         uuid_from_string(ptr, &pfsd->shared_uuid, &status);
521                 } else if (strcmp(cmd, "unique-uuid") == 0) {
522                         uuid_from_string(ptr, &pfsd->unique_uuid, &status);
523                 } else if (strcmp(cmd, "master") == 0) {
524                         if (pfsd->mirror_flags & HAMMER_PFSD_SLAVE) {
525                                 printf("NOTE: This PFS is a slave, the master "
526                                        "id you are setting only applies\n"
527                                        "when you pfs-upgrade the PFS to "
528                                        "a master!\n");
529                         }
530                         pfsd->master_id = strtol(ptr, NULL, 0);
531                 } else if (strcmp(cmd, "slave") == 0) {
532                         if ((pfsd->mirror_flags & HAMMER_PFSD_SLAVE) == 0) {
533                                 fprintf(stderr,
534                                         "slave mode cannot be set on a master "
535                                         "PFS, use pfs-upgrade instead!\n");
536                                 exit(1);
537                         }
538                         pfsd->mirror_flags |= HAMMER_PFSD_SLAVE;
539                 } else if (strcmp(cmd, "no-mirror") == 0) {
540                         if (pfsd->mirror_flags & HAMMER_PFSD_SLAVE) {
541                                 fprintf(stderr,
542                                         "no-mirror mode cannot be set on a "
543                                         "slave PFS\n");
544                                 exit(1);
545                         }
546                         pfsd->master_id = -1;
547                         pfsd->mirror_flags &= ~HAMMER_PFSD_SLAVE;
548                 } else if (strcmp(cmd, "label") == 0) {
549                         len = strlen(ptr);
550                         if (ptr[0] == '"' && ptr[len-1] == '"') {
551                                 ptr[len-1] = 0;
552                                 ++ptr;
553                         } else if (ptr[0] == '"') {
554                                 fprintf(stderr,
555                                         "option %s: malformed string\n",
556                                         cmd);
557                                 exit(1);
558                         }
559                         snprintf(pfsd->label, sizeof(pfsd->label), "%s", ptr);
560                 } else {
561                         fprintf(stderr, "invalid option: %s\n", cmd);
562                         exit(1);
563                 }
564                 if (status != uuid_s_ok) {
565                         fprintf(stderr, "option %s: error parsing uuid %s\n",
566                                 cmd, ptr);
567                         exit(1);
568                 }
569                 --ac;
570                 ++av;
571         }
572 }
573
574 static
575 void
576 pseudofs_usage(int code)
577 {
578         fprintf(stderr, 
579                 "hammer pfs-status <dirpath1>...<dirpathN>\n"
580                 "hammer pfs-master <dirpath> [options]\n"
581                 "hammer pfs-slave <dirpath> [options]\n"
582                 "hammer pfs-update <dirpath> [options]\n"
583                 "hammer pfs-upgrade <dirpath>\n"
584                 "hammer pfs-downgrade <dirpath>\n"
585                 "hammer pfs-destroy <dirpath>\n"
586                 "\n"
587                 "options:\n"
588                 "    sync-beg-tid=0x16llx\n"
589                 "    sync-end-tid=0x16llx\n"
590                 "    shared-uuid=0x16llx\n"
591                 "    unique-uuid=0x16llx\n"
592                 "    master=0-15\n"
593                 "    slave\n"
594                 "    no-mirror\n"
595                 "    label=\"string\"\n"
596         );
597         exit(code);
598 }
599
600 static
601 int
602 getyn(void)
603 {
604         char buf[256];
605         int len;
606
607         if (fgets(buf, sizeof(buf), stdin) == NULL)
608                 return(0);
609         len = strlen(buf);
610         while (len && (buf[len-1] == '\n' || buf[len-1] == '\r'))
611                 --len;
612         buf[len] = 0;
613         if (strcmp(buf, "y") == 0 ||
614             strcmp(buf, "yes") == 0 ||
615             strcmp(buf, "Y") == 0 ||
616             strcmp(buf, "YES") == 0) {
617                 return(1);
618         }
619         return(0);
620 }
621