Fix function getpfs for pseudo-filesystems specified as absolute path
[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.9 2008/08/17 10:35:18 mneumann 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                 if (strlen(dirpath) == 0) {
71                         free(dirpath);
72                         dirpath = strdup("/");
73                 }
74         } else {
75                 free(dirpath);
76                 dirpath = strdup(".");
77         }
78
79         if (lstat(path, &st) == 0 && S_ISLNK(st.st_mode)) {
80                 /*
81                  * Avoid foot-shooting.  Don't let the user access a PFS
82                  * softlink via a PFS.  PFS softlinks may only be accessed
83                  * via the master filesystem.
84                  */
85                 fd = open(dirpath, O_RDONLY);
86                 if (fd < 0)
87                         goto done;
88                 pfs->pfs_id = -1;
89                 ioctl(fd, HAMMERIOC_GET_PSEUDOFS, pfs);
90                 if (pfs->pfs_id != 0) {
91                         fprintf(stderr,
92                                 "You are attempting to access a PFS softlink "
93                                 "from a PFS.  It may not represent the PFS\n"
94                                 "on the main filesystem mount that you "
95                                 "expect!  You may only access PFS softlinks\n"
96                                 "via the main filesystem mount!\n");
97                         exit(1);
98                 }
99                 close(fd);
100
101                 /*
102                  * Extract the PFS from the link.  HAMMER will automatically
103                  * convert @@PFS%05d links so if actually see one in that
104                  * form the target PFS may not exist or may be corrupt.  But
105                  * we can extract the PFS id anyway.
106                  */
107                 n = readlink(path, buf, sizeof(buf) - 1);
108                 if (n < 0)
109                         n = 0;
110                 buf[n] = 0;
111                 if (sscanf(buf, "@@PFS%d", &pfs->pfs_id) == 1) {
112                         fd = open(dirpath, O_RDONLY);
113                         goto done;
114                 }
115                 if (sscanf(buf, "@@%llx:%d", &dummy_tid, &pfs->pfs_id) == 2) {
116                         fd = open(dirpath, O_RDONLY);
117                         goto done;
118                 }
119         }
120
121         /*
122          * Try to open the path and request the pfs_id that way.
123          */
124         fd = open(path, O_RDONLY);
125         if (fd >= 0) {
126                 pfs->pfs_id = -1;
127                 ioctl(fd, HAMMERIOC_GET_PSEUDOFS, pfs);
128                 if (pfs->pfs_id == -1) {
129                         close(fd);
130                         fd = -1;
131                 }
132         }
133
134         /*
135          * Cleanup
136          */
137 done:
138         if (fd < 0) {
139                 fprintf(stderr, "Cannot access PFS %s: %s\n",
140                         path, strerror(errno));
141                 exit(1);
142         }
143         if (ioctl(fd, HAMMERIOC_GET_PSEUDOFS, pfs) < 0) {
144                 fprintf(stderr, "Cannot access PFS %s: %s\n",
145                         path, strerror(errno));
146                 exit(1);
147         }
148         free(dirpath);
149         return(fd);
150 }
151
152 void
153 relpfs(int fd, struct hammer_ioc_pseudofs_rw *pfs)
154 {
155         close(fd);
156         if (pfs->ondisk) {
157                 free(pfs->ondisk);
158                 pfs->ondisk = NULL;
159         }
160 }
161
162 void
163 hammer_cmd_pseudofs_status(char **av, int ac)
164 {
165         struct hammer_ioc_pseudofs_rw pfs;
166         int i;
167         int fd;
168
169         for (i = 0; i < ac; ++i) {
170                 printf("%s\t", av[i]);
171                 fd = getpfs(&pfs, av[i]);
172                 if (fd < 0 || ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs) < 0) {
173                         printf("Not a HAMMER root\n");
174                 } else {
175                         printf("PFS #%d {\n", pfs.pfs_id);
176                         dump_pfsd(pfs.ondisk);
177                         printf("}\n");
178                 }
179                 if (fd >= 0)
180                         close(fd);
181                 if (pfs.ondisk)
182                         free(pfs.ondisk);
183         }
184 }
185
186 void
187 hammer_cmd_pseudofs_create(char **av, int ac, int is_slave)
188 {
189         struct hammer_ioc_pseudofs_rw pfs;
190         struct hammer_pseudofs_data pfsd;
191         struct stat st;
192         const char *path;
193         char *dirpath;
194         char *linkpath;
195         int pfs_id;
196         int fd;
197         int error;
198
199         if (ac == 0)
200                 pseudofs_usage(1);
201         path = av[0];
202         if (lstat(path, &st) == 0) {
203                 fprintf(stderr, "cannot create %s, file exists!\n", path);
204                 exit(1);
205         }
206
207         dirpath = strdup(path);
208         if (strrchr(dirpath, '/') != NULL) {
209                 *strrchr(dirpath, '/') = 0;
210         } else {
211                 free(dirpath);
212                 dirpath = strdup(".");
213         }
214         fd = open(dirpath, O_RDONLY);
215         if (fd < 0) {
216                 fprintf(stderr, "Cannot open directory %s\n", dirpath);
217                 exit(1);
218         }
219
220         error = 0;
221         for (pfs_id = 0; pfs_id < HAMMER_MAX_PFS; ++pfs_id) {
222                 bzero(&pfs, sizeof(pfs));
223                 bzero(&pfsd, sizeof(pfsd));
224                 pfs.pfs_id = pfs_id;
225                 pfs.ondisk = &pfsd;
226                 pfs.bytes = sizeof(pfsd);
227                 pfs.version = HAMMER_IOC_PSEUDOFS_VERSION;
228                 if (ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs) < 0) {
229                         error = errno;
230                         break;
231                 }
232         }
233         if (pfs_id == HAMMER_MAX_PFS) {
234                 fprintf(stderr, "Cannot create %s, all PFSs in use\n", path);
235                 exit(1);
236         }
237         if (error != ENOENT) {
238                 fprintf(stderr, "Cannot create %s, got %s during scan\n",
239                         path, strerror(error));
240                 exit(1);
241         }
242
243         /*
244          * Create the new PFS
245          */
246         printf("Creating PFS #%d\t", pfs_id);
247         bzero(&pfsd, sizeof(pfsd));
248         init_pfsd(&pfsd, is_slave);
249         pfs.pfs_id = pfs_id;
250         pfs.ondisk = &pfsd;
251         pfs.bytes = sizeof(pfsd);
252         pfs.version = HAMMER_IOC_PSEUDOFS_VERSION;
253
254         if (ioctl(fd, HAMMERIOC_SET_PSEUDOFS, &pfs) < 0) {
255                 printf("failed: %s\n", strerror(errno));
256         } else {
257                 /* special symlink, must be exactly 10 characters */
258                 asprintf(&linkpath, "@@PFS%05d", pfs_id);
259                 if (symlink(linkpath, path) < 0) {
260                         printf("failed: cannot create symlink: %s\n",
261                                 strerror(errno));
262                 } else {
263                         printf("succeeded!\n");
264                         hammer_cmd_pseudofs_update(av, ac);
265                 }
266         }
267         close(fd);
268 }
269
270 void
271 hammer_cmd_pseudofs_destroy(char **av, int ac)
272 {
273         struct hammer_ioc_pseudofs_rw pfs;
274         struct stat st;
275         int fd;
276         int i;
277
278         if (ac == 0)
279                 pseudofs_usage(1);
280         bzero(&pfs, sizeof(pfs));
281         fd = getpfs(&pfs, av[0]);
282
283         if (pfs.pfs_id == 0) {
284                 fprintf(stderr, "You cannot destroy PFS#0\n");
285                 exit(1);
286         }
287         printf("You have requested that PFS#%d (%s) be destroyed\n",
288                 pfs.pfs_id, pfs.ondisk->label);
289         printf("This will irrevocably destroy all data on this PFS!!!!!\n");
290         printf("Do you really want to do this? ");
291         fflush(stdout);
292         if (getyn() == 0) {
293                 fprintf(stderr, "No action taken on PFS#%d\n", pfs.pfs_id);
294                 exit(1);
295         }
296
297         if ((pfs.ondisk->mirror_flags & HAMMER_PFSD_SLAVE) == 0) {
298                 printf("This PFS is currently setup as a MASTER!\n");
299                 printf("Are you absolutely sure you want to destroy it? ");
300                 fflush(stdout);
301                 if (getyn() == 0) {
302                         fprintf(stderr, "No action taken on PFS#%d\n",
303                                 pfs.pfs_id);
304                         exit(1);
305                 }
306         }
307
308         printf("Destroying PFS #%d (%s) in ", pfs.pfs_id, pfs.ondisk->label);
309         for (i = 5; i; --i) {
310                 printf(" %d", i);
311                 fflush(stdout);
312                 sleep(1);
313         }
314         printf(".. starting destruction pass\n");
315         fflush(stdout);
316
317         /*
318          * Set the sync_beg_tid and sync_end_tid's to 1, once we start the
319          * RMR the PFS is basically destroyed even if someone ^C's it.
320          */
321         pfs.ondisk->mirror_flags |= HAMMER_PFSD_SLAVE;
322         pfs.ondisk->reserved01 = -1;
323         pfs.ondisk->sync_beg_tid = 1;
324         pfs.ondisk->sync_end_tid = 1;
325
326         if (ioctl(fd, HAMMERIOC_SET_PSEUDOFS, &pfs) < 0) {
327                 fprintf(stderr, "Unable to update the PFS configuration: %s\n",
328                         strerror(errno));
329                 exit(1);
330         }
331
332         /*
333          * Ok, do it.  Remove the softlink on success.
334          */
335         if (ioctl(fd, HAMMERIOC_RMR_PSEUDOFS, &pfs) == 0) {
336                 printf("pfs-destroy of PFS#%d succeeded!\n", pfs.pfs_id);
337                 if (lstat(av[0], &st) == 0 && S_ISLNK(st.st_mode)) {
338                         if (remove(av[0]) < 0) {
339                                 fprintf(stderr, "Unable to remove softlink: %s "
340                                         "(but the PFS has been destroyed)\n",
341                                         av[0]);
342                                 /* exit status 0 anyway */
343                         }
344                 }
345         } else {
346                 printf("pfs-destroy of PFS#%d failed: %s\n",
347                         pfs.pfs_id, strerror(errno));
348         }
349 }
350
351 void
352 hammer_cmd_pseudofs_upgrade(char **av, int ac)
353 {
354         struct hammer_ioc_pseudofs_rw pfs;
355         int fd;
356
357         if (ac == 0)
358                 pseudofs_usage(1);
359         bzero(&pfs, sizeof(pfs));
360         fd = getpfs(&pfs, av[0]);
361
362         if (pfs.pfs_id == 0) {
363                 fprintf(stderr, "You cannot upgrade PFS#0"
364                                 " (It should already be a master)\n");
365                 exit(1);
366         }
367         if (ioctl(fd, HAMMERIOC_UPG_PSEUDOFS, &pfs) == 0) {
368                 printf("pfs-upgrade of PFS#%d (%s) succeeded\n",
369                         pfs.pfs_id, pfs.ondisk->label);
370         } else {
371                 fprintf(stderr, "pfs-upgrade of PFS#%d (%s) failed: %s\n",
372                         pfs.pfs_id, pfs.ondisk->label, strerror(errno));
373         }
374 }
375
376 void
377 hammer_cmd_pseudofs_downgrade(char **av, int ac)
378 {
379         struct hammer_ioc_pseudofs_rw pfs;
380         int fd;
381
382         if (ac == 0)
383                 pseudofs_usage(1);
384         bzero(&pfs, sizeof(pfs));
385         fd = getpfs(&pfs, av[0]);
386
387         if (pfs.pfs_id == 0) {
388                 fprintf(stderr, "You cannot downgrade PFS#0\n");
389                 exit(1);
390         }
391
392         if (ioctl(fd, HAMMERIOC_DGD_PSEUDOFS, &pfs) == 0) {
393                 printf("pfs-downgrade of PFS#%d (%s) succeeded\n",
394                         pfs.pfs_id, pfs.ondisk->label);
395         } else {
396                 fprintf(stderr, "pfs-upgrade of PFS#%d (%s) failed: %s\n",
397                         pfs.pfs_id, pfs.ondisk->label, strerror(errno));
398         }
399 }
400
401 void
402 hammer_cmd_pseudofs_update(char **av, int ac)
403 {
404         struct hammer_ioc_pseudofs_rw pfs;
405         int fd;
406
407         if (ac == 0)
408                 pseudofs_usage(1);
409         bzero(&pfs, sizeof(pfs));
410         fd = getpfs(&pfs, av[0]);
411
412         printf("%s\n", av[0]);
413         fflush(stdout);
414
415         if (ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs) == 0) {
416                 parse_pfsd_options(av + 1, ac - 1, pfs.ondisk);
417                 if ((pfs.ondisk->mirror_flags & HAMMER_PFSD_SLAVE) &&
418                     pfs.pfs_id == 0) {
419                         printf("The real mount point cannot be made a PFS "
420                                "slave, only PFS sub-directories can be made "
421                                "slaves\n");
422                         exit(1);
423                 }
424                 pfs.bytes = sizeof(*pfs.ondisk);
425                 if (ioctl(fd, HAMMERIOC_SET_PSEUDOFS, &pfs) == 0) {
426                         if (ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs) == 0) {
427                                 dump_pfsd(pfs.ondisk);
428                         } else {
429                                 printf("Unable to retrieve pfs configuration after successful update: %s\n", strerror(errno));
430                                 exit(1);
431                         }
432                 } else {
433                         printf("Unable to adjust pfs configuration: %s\n", strerror(errno));
434                         exit(1);
435                 }
436         }
437 }
438
439 static void
440 init_pfsd(hammer_pseudofs_data_t pfsd, int is_slave)
441 {
442         uint32_t status;
443
444         pfsd->sync_beg_tid = 1;
445         pfsd->sync_end_tid = 1;
446         pfsd->sync_beg_ts = 0;
447         pfsd->sync_end_ts = 0;
448         uuid_create(&pfsd->shared_uuid, &status);
449         uuid_create(&pfsd->unique_uuid, &status);
450         if (is_slave)
451                 pfsd->mirror_flags |= HAMMER_PFSD_SLAVE;
452 }
453
454 static
455 void
456 dump_pfsd(hammer_pseudofs_data_t pfsd)
457 {
458         u_int32_t status;
459         char *str = NULL;
460
461         printf("    sync-beg-tid=0x%016llx\n", pfsd->sync_beg_tid);
462         printf("    sync-end-tid=0x%016llx\n", pfsd->sync_end_tid);
463         uuid_to_string(&pfsd->shared_uuid, &str, &status);
464         printf("    shared-uuid=%s\n", str);
465         uuid_to_string(&pfsd->unique_uuid, &str, &status);
466         printf("    unique-uuid=%s\n", str);
467         if (pfsd->mirror_flags & HAMMER_PFSD_SLAVE) {
468                 printf("    slave\n");
469         }
470         printf("    label=\"%s\"\n", pfsd->label);
471         if (pfsd->mirror_flags & HAMMER_PFSD_SLAVE)
472                 printf("    operating as a SLAVE\n");
473         else
474                 printf("    operating as a MASTER\n");
475 }
476
477 static void
478 parse_pfsd_options(char **av, int ac, hammer_pseudofs_data_t pfsd)
479 {
480         char *cmd;
481         char *ptr;
482         int len;
483         uint32_t status;
484
485         while (ac) {
486                 cmd = *av;
487                 if ((ptr = strchr(cmd, '=')) != NULL)
488                         *ptr++ = 0;
489
490                 /*
491                  * Basic assignment value test
492                  */
493                 if (ptr == NULL) {
494                         fprintf(stderr,
495                                 "option %s requires an assignment\n",
496                                 cmd);
497                         exit(1);
498                 }
499
500                 status = uuid_s_ok;
501                 if (strcmp(cmd, "sync-beg-tid") == 0) {
502                         pfsd->sync_beg_tid = strtoull(ptr, NULL, 16);
503                 } else if (strcmp(cmd, "sync-end-tid") == 0) {
504                         pfsd->sync_end_tid = strtoull(ptr, NULL, 16);
505                 } else if (strcmp(cmd, "shared-uuid") == 0) {
506                         uuid_from_string(ptr, &pfsd->shared_uuid, &status);
507                 } else if (strcmp(cmd, "unique-uuid") == 0) {
508                         uuid_from_string(ptr, &pfsd->unique_uuid, &status);
509                 } else if (strcmp(cmd, "label") == 0) {
510                         len = strlen(ptr);
511                         if (ptr[0] == '"' && ptr[len-1] == '"') {
512                                 ptr[len-1] = 0;
513                                 ++ptr;
514                         } else if (ptr[0] == '"') {
515                                 fprintf(stderr,
516                                         "option %s: malformed string\n",
517                                         cmd);
518                                 exit(1);
519                         }
520                         snprintf(pfsd->label, sizeof(pfsd->label), "%s", ptr);
521                 } else {
522                         fprintf(stderr, "invalid option: %s\n", cmd);
523                         exit(1);
524                 }
525                 if (status != uuid_s_ok) {
526                         fprintf(stderr, "option %s: error parsing uuid %s\n",
527                                 cmd, ptr);
528                         exit(1);
529                 }
530                 --ac;
531                 ++av;
532         }
533 }
534
535 static
536 void
537 pseudofs_usage(int code)
538 {
539         fprintf(stderr, 
540                 "hammer pfs-status <dirpath1>...<dirpathN>\n"
541                 "hammer pfs-master <dirpath> [options]\n"
542                 "hammer pfs-slave <dirpath> [options]\n"
543                 "hammer pfs-update <dirpath> [options]\n"
544                 "hammer pfs-upgrade <dirpath>\n"
545                 "hammer pfs-downgrade <dirpath>\n"
546                 "hammer pfs-destroy <dirpath>\n"
547                 "\n"
548                 "options:\n"
549                 "    sync-beg-tid=0x16llx\n"
550                 "    sync-end-tid=0x16llx\n"
551                 "    shared-uuid=0x16llx\n"
552                 "    unique-uuid=0x16llx\n"
553                 "    label=\"string\"\n"
554         );
555         exit(code);
556 }
557
558 static
559 int
560 getyn(void)
561 {
562         char buf[256];
563         int len;
564
565         if (fgets(buf, sizeof(buf), stdin) == NULL)
566                 return(0);
567         len = strlen(buf);
568         while (len && (buf[len-1] == '\n' || buf[len-1] == '\r'))
569                 --len;
570         buf[len] = 0;
571         if (strcmp(buf, "y") == 0 ||
572             strcmp(buf, "yes") == 0 ||
573             strcmp(buf, "Y") == 0 ||
574             strcmp(buf, "YES") == 0) {
575                 return(1);
576         }
577         return(0);
578 }
579