Whamcloud - gitweb
31cec4abca8fa6c956293edc5be0ccfe59994c6a
[fs/lustre-release.git] / lustre / utils / mount_utils.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, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  * Copyright (c) 2012, 2013, Intel Corporation.
30  */
31 /*
32  * This file is part of Lustre, http://www.lustre.org/
33  * Lustre is a trademark of Sun Microsystems, Inc.
34  */
35
36 #if HAVE_CONFIG_H
37 #  include "config.h"
38 #endif /* HAVE_CONFIG_H */
39
40 #include "mount_utils.h"
41 #include <stdio.h>
42 #include <errno.h>
43 #include <string.h>
44 #include <config.h>
45 #include <lustre_disk.h>
46 #include <lustre_ver.h>
47 #include <sys/stat.h>
48 #include <sys/utsname.h>
49
50 extern char *progname;
51 extern int verbose;
52
53 #define vprint(fmt, arg...) if (verbose > 0) printf(fmt, ##arg)
54 #define verrprint(fmt, arg...) if (verbose >= 0) fprintf(stderr, fmt, ##arg)
55
56 void fatal(void)
57 {
58         verbose = 0;
59         fprintf(stderr, "\n%s FATAL: ", progname);
60 }
61
62 int run_command(char *cmd, int cmdsz)
63 {
64         char log[] = "/tmp/run_command_logXXXXXX";
65         int fd = -1, rc;
66
67         if ((cmdsz - strlen(cmd)) < 6) {
68                 fatal();
69                 fprintf(stderr, "Command buffer overflow: %.*s...\n",
70                         cmdsz, cmd);
71                 return ENOMEM;
72         }
73
74         if (verbose > 1) {
75                 printf("cmd: %s\n", cmd);
76         } else {
77                 if ((fd = mkstemp(log)) >= 0) {
78                         close(fd);
79                         strcat(cmd, " >");
80                         strcat(cmd, log);
81                 }
82         }
83         strcat(cmd, " 2>&1");
84
85         /* Can't use popen because we need the rv of the command */
86         rc = system(cmd);
87         if (rc && (fd >= 0)) {
88                 char buf[128];
89                 FILE *fp;
90                 fp = fopen(log, "r");
91                 if (fp) {
92                         while (fgets(buf, sizeof(buf), fp) != NULL) {
93                                 printf("   %s", buf);
94                         }
95                         fclose(fp);
96                 }
97         }
98         if (fd >= 0)
99                 remove(log);
100         return rc;
101 }
102
103 int add_param(char *buf, char *key, char *val)
104 {
105         int end = sizeof(((struct lustre_disk_data *)0)->ldd_params);
106         int start = strlen(buf);
107         int keylen = 0;
108
109         if (key)
110                 keylen = strlen(key);
111         if (start + 1 + keylen + strlen(val) >= end) {
112                 fprintf(stderr, "%s: params are too long-\n%s %s%s\n",
113                         progname, buf, key ? key : "", val);
114                 return 1;
115         }
116
117         sprintf(buf + start, " %s%s", key ? key : "", val);
118         return 0;
119 }
120
121 int get_param(char *buf, char *key, char **val)
122 {
123         int i, key_len = strlen(key);
124         char *ptr;
125
126         ptr = strstr(buf, key);
127         if (ptr) {
128                 *val = strdup(ptr + key_len);
129                 if (*val == NULL)
130                         return ENOMEM;
131
132                 for (i = 0; i < strlen(*val); i++)
133                         if (((*val)[i] == ' ') || ((*val)[i] == '\0'))
134                                 break;
135
136                 (*val)[i] = '\0';
137                 return 0;
138         }
139
140         return ENOENT;
141 }
142
143 char *strscat(char *dst, char *src, int buflen)
144 {
145         dst[buflen - 1] = 0;
146         if (strlen(dst) + strlen(src) >= buflen) {
147                 fprintf(stderr, "string buffer overflow (max %d): '%s' + '%s'"
148                         "\n", buflen, dst, src);
149                 exit(EOVERFLOW);
150         }
151         return strcat(dst, src);
152 }
153
154 char *strscpy(char *dst, char *src, int buflen)
155 {
156         dst[0] = 0;
157         return strscat(dst, src, buflen);
158 }
159
160 int check_mtab_entry(char *spec1, char *spec2, char *mtpt, char *type)
161 {
162         FILE *fp;
163         struct mntent *mnt;
164
165         fp = setmntent(MOUNTED, "r");
166         if (fp == NULL)
167                 return 0;
168
169         while ((mnt = getmntent(fp)) != NULL) {
170                 if ((strcmp(mnt->mnt_fsname, spec1) == 0 ||
171                      strcmp(mnt->mnt_fsname, spec2) == 0) &&
172                     (mtpt == NULL || strcmp(mnt->mnt_dir, mtpt) == 0) &&
173                     (type == NULL || strcmp(mnt->mnt_type, type) == 0)) {
174                         endmntent(fp);
175                         return(EEXIST);
176                 }
177         }
178         endmntent(fp);
179
180         return 0;
181 }
182
183 #define PROC_DIR        "/proc/"
184 static int mtab_is_proc(const char *mtab)
185 {
186         char path[16];
187
188         if (readlink(mtab, path, sizeof(path)) < 0)
189                 return 0;
190
191         if (strncmp(path, PROC_DIR, strlen(PROC_DIR)))
192                 return 0;
193
194         return 1;
195 }
196
197 int update_mtab_entry(char *spec, char *mtpt, char *type, char *opts,
198                 int flags, int freq, int pass)
199 {
200         FILE *fp;
201         struct mntent mnt;
202         int rc = 0;
203
204         /* Don't update mtab if it is linked to any file in /proc direcotry.*/
205         if (mtab_is_proc(MOUNTED))
206                 return 0;
207
208         mnt.mnt_fsname = spec;
209         mnt.mnt_dir = mtpt;
210         mnt.mnt_type = type;
211         mnt.mnt_opts = opts ? opts : "";
212         mnt.mnt_freq = freq;
213         mnt.mnt_passno = pass;
214
215         fp = setmntent(MOUNTED, "a+");
216         if (fp == NULL) {
217                 fprintf(stderr, "%s: setmntent(%s): %s:",
218                         progname, MOUNTED, strerror (errno));
219                 rc = 16;
220         } else {
221                 if ((addmntent(fp, &mnt)) == 1) {
222                         fprintf(stderr, "%s: addmntent: %s:",
223                                 progname, strerror (errno));
224                         rc = 16;
225                 }
226                 endmntent(fp);
227         }
228
229         return rc;
230 }
231
232 /* Search for opt in mntlist, returning true if found.
233  */
234 static int in_mntlist(char *opt, char *mntlist)
235 {
236         char *ml, *mlp, *item, *ctx = NULL;
237
238         if (!(ml = strdup(mntlist))) {
239                 fprintf(stderr, "%s: out of memory\n", progname);
240                 exit(1);
241         }
242         mlp = ml;
243         while ((item = strtok_r(mlp, ",", &ctx))) {
244                 if (!strcmp(opt, item))
245                         break;
246                 mlp = NULL;
247         }
248         free(ml);
249         return (item != NULL);
250 }
251
252 /* Issue a message on stderr for every item in wanted_mountopts that is not
253  * present in mountopts.  The justwarn boolean toggles between error and
254  * warning message.  Return an error count.
255  */
256 int check_mountfsoptions(char *mountopts, char *wanted_mountopts,
257                          int justwarn)
258 {
259         char *ml, *mlp, *item, *ctx = NULL;
260         int errors = 0;
261
262         if (!(ml = strdup(wanted_mountopts))) {
263                 fprintf(stderr, "%s: out of memory\n", progname);
264                 exit(1);
265         }
266         mlp = ml;
267         while ((item = strtok_r(mlp, ",", &ctx))) {
268                 if (!in_mntlist(item, mountopts)) {
269                         fprintf(stderr, "%s: %s mount option `%s' is missing\n",
270                                 progname, justwarn ? "Warning: default"
271                                 : "Error: mandatory", item);
272                         errors++;
273                 }
274                 mlp = NULL;
275         }
276         free(ml);
277         return errors;
278 }
279
280 /* Trim embedded white space, leading and trailing commas from string s.
281  */
282 void trim_mountfsoptions(char *s)
283 {
284         char *p;
285
286         for (p = s; *p; ) {
287                 if (isspace(*p)) {
288                         memmove(p, p + 1, strlen(p + 1) + 1);
289                         continue;
290                 }
291                 p++;
292         }
293
294         while (s[0] == ',')
295                 memmove(&s[0], &s[1], strlen(&s[1]) + 1);
296
297         p = s + strlen(s) - 1;
298         while (p >= s && *p == ',')
299                 *p-- = '\0';
300 }
301
302 /* Setup a file in the first unused loop_device */
303 int loop_setup(struct mkfs_opts *mop)
304 {
305         char loop_base[20];
306         char l_device[64];
307         int i, ret = 0;
308
309         /* Figure out the loop device names */
310         if (!access("/dev/loop0", F_OK | R_OK)) {
311                 strcpy(loop_base, "/dev/loop\0");
312         } else if (!access("/dev/loop/0", F_OK | R_OK)) {
313                 strcpy(loop_base, "/dev/loop/\0");
314         } else {
315                 fprintf(stderr, "%s: can't access loop devices\n", progname);
316                 return EACCES;
317         }
318
319         /* Find unused loop device */
320         for (i = 0; i < MAX_LOOP_DEVICES; i++) {
321                 char cmd[PATH_MAX];
322                 int cmdsz = sizeof(cmd);
323
324                 sprintf(l_device, "%s%d", loop_base, i);
325                 if (access(l_device, F_OK | R_OK))
326                         break;
327                 snprintf(cmd, cmdsz, "losetup %s > /dev/null 2>&1", l_device);
328                 ret = system(cmd);
329
330                 /* losetup gets 1 (ret=256) for non-set-up device */
331                 if (ret) {
332                         /* Set up a loopback device to our file */
333                         snprintf(cmd, cmdsz, "losetup %s %s", l_device,
334                                  mop->mo_device);
335                         ret = run_command(cmd, cmdsz);
336                         if (ret == 256)
337                                 /* someone else picked up this loop device
338                                  * behind our back */
339                                 continue;
340                         if (ret) {
341                                 fprintf(stderr, "%s: error %d on losetup: %s\n",
342                                         progname, ret,
343                                         ret >= 0 ? strerror(ret) : "");
344                                 return ret;
345                         }
346                         strscpy(mop->mo_loopdev, l_device,
347                                 sizeof(mop->mo_loopdev));
348                         return ret;
349                 }
350         }
351
352         fprintf(stderr, "%s: out of loop devices!\n", progname);
353         return EMFILE;
354 }
355
356 int loop_cleanup(struct mkfs_opts *mop)
357 {
358         char cmd[150];
359         int ret = 0;
360
361         if ((mop->mo_flags & MO_IS_LOOP) && *mop->mo_loopdev) {
362                 int tries;
363
364                 sprintf(cmd, "losetup -d %s", mop->mo_loopdev);
365                 for (tries = 0; tries < 3; tries++) {
366                         ret = run_command(cmd, sizeof(cmd));
367                         if (ret == 0)
368                                 break;
369                         sleep(1);
370                 }
371         }
372
373         if (ret != 0)
374                 fprintf(stderr, "cannot cleanup %s: rc = %d\n",
375                         mop->mo_loopdev, ret);
376         return ret;
377 }
378
379 int loop_format(struct mkfs_opts *mop)
380 {
381         int fd;
382
383         if (mop->mo_device_kb == 0) {
384                 fatal();
385                 fprintf(stderr, "loop device requires a --device-size= "
386                         "param\n");
387                 return EINVAL;
388         }
389
390         fd = creat(mop->mo_device, S_IRUSR|S_IWUSR);
391         if (fd < 0) {
392                 fatal();
393                 fprintf(stderr, "%s: Unable to create backing store: %s\n",
394                         progname, strerror(errno));
395                 return errno;
396         }
397
398         if (ftruncate(fd, mop->mo_device_kb * 1024) != 0) {
399                 close(fd);
400                 fatal();
401                 fprintf(stderr, "%s: Unable to truncate backing store: %s\n",
402                         progname, strerror(errno));
403                 return errno;
404         }
405
406         close(fd);
407         return 0;
408 }
409
410 /* Write the server config files */
411 int osd_write_ldd(struct mkfs_opts *mop)
412 {
413         struct lustre_disk_data *ldd = &mop->mo_ldd;
414         int ret;
415
416         switch (ldd->ldd_mount_type) {
417 #ifdef HAVE_LDISKFS_OSD
418         case LDD_MT_LDISKFS:
419         case LDD_MT_LDISKFS2:
420                 ret = ldiskfs_write_ldd(mop);
421                 break;
422 #endif /* HAVE_LDISKFS_OSD */
423 #ifdef HAVE_ZFS_OSD
424         case LDD_MT_ZFS:
425                 ret = zfs_write_ldd(mop);
426                 break;
427 #endif /* HAVE_ZFS_OSD */
428         default:
429                 fatal();
430                 fprintf(stderr, "unknown fs type %d '%s'\n",
431                         ldd->ldd_mount_type, MT_STR(ldd));
432                 ret = EINVAL;
433                 break;
434         }
435
436         return ret;
437 }
438
439 /* Read the server config files */
440 int osd_read_ldd(char *dev, struct lustre_disk_data *ldd)
441 {
442         int ret;
443
444         switch (ldd->ldd_mount_type) {
445 #ifdef HAVE_LDISKFS_OSD
446         case LDD_MT_LDISKFS:
447         case LDD_MT_LDISKFS2:
448                 ret = ldiskfs_read_ldd(dev, ldd);
449                 break;
450 #endif /* HAVE_LDISKFS_OSD */
451 #ifdef HAVE_ZFS_OSD
452         case LDD_MT_ZFS:
453                 ret = zfs_read_ldd(dev, ldd);
454                 break;
455 #endif /* HAVE_ZFS_OSD */
456         default:
457                 fatal();
458                 fprintf(stderr, "unknown fs type %d '%s'\n",
459                         ldd->ldd_mount_type, MT_STR(ldd));
460                 ret = EINVAL;
461                 break;
462         }
463
464         return ret;
465 }
466
467 /* Was this device formatted for Lustre */
468 int osd_is_lustre(char *dev, unsigned *mount_type)
469 {
470         vprint("checking for existing Lustre data: ");
471
472 #ifdef HAVE_LDISKFS_OSD
473         if (ldiskfs_is_lustre(dev, mount_type)) {
474                 vprint("found\n");
475                 return 1;
476         }
477 #endif /* HAVE_LDISKFS_OSD */
478 #ifdef HAVE_ZFS_OSD
479         if (zfs_is_lustre(dev, mount_type)) {
480                 vprint("found\n");
481                 return 1;
482         }
483 #endif /* HAVE_ZFS_OSD */
484
485         vprint("not found\n");
486         return 0;
487 }
488
489 /* Build fs according to type */
490 int osd_make_lustre(struct mkfs_opts *mop)
491 {
492         struct lustre_disk_data *ldd = &mop->mo_ldd;
493         int ret;
494
495         switch (ldd->ldd_mount_type) {
496 #ifdef HAVE_LDISKFS_OSD
497         case LDD_MT_LDISKFS:
498         case LDD_MT_LDISKFS2:
499                 ret = ldiskfs_make_lustre(mop);
500                 break;
501 #endif /* HAVE_LDISKFS_OSD */
502 #ifdef HAVE_ZFS_OSD
503         case LDD_MT_ZFS:
504                 ret = zfs_make_lustre(mop);
505                 break;
506 #endif /* HAVE_ZFS_OSD */
507         default:
508                 fatal();
509                 fprintf(stderr, "unknown fs type %d '%s'\n",
510                         ldd->ldd_mount_type, MT_STR(ldd));
511                 ret = EINVAL;
512                 break;
513         }
514
515         return ret;
516 }
517
518 int osd_prepare_lustre(struct mkfs_opts *mop,
519                 char *default_mountopts, int default_len,
520                 char *always_mountopts, int always_len)
521 {
522         struct lustre_disk_data *ldd = &mop->mo_ldd;
523         int ret;
524
525         switch (ldd->ldd_mount_type) {
526 #ifdef HAVE_LDISKFS_OSD
527         case LDD_MT_LDISKFS:
528         case LDD_MT_LDISKFS2:
529                 ret = ldiskfs_prepare_lustre(mop,
530                                              default_mountopts, default_len,
531                                              always_mountopts, always_len);
532                 break;
533 #endif /* HAVE_LDISKFS_OSD */
534 #ifdef HAVE_ZFS_OSD
535         case LDD_MT_ZFS:
536                 ret = zfs_prepare_lustre(mop,
537                                          default_mountopts, default_len,
538                                          always_mountopts, always_len);
539                 break;
540 #endif /* HAVE_ZFS_OSD */
541         default:
542                 fatal();
543                 fprintf(stderr, "unknown fs type %d '%s'\n",
544                         ldd->ldd_mount_type, MT_STR(ldd));
545                 ret = EINVAL;
546                 break;
547         }
548
549         return ret;
550 }
551
552 int osd_tune_lustre(char *dev, struct mount_opts *mop)
553 {
554         struct lustre_disk_data *ldd = &mop->mo_ldd;
555         int ret;
556
557         switch (ldd->ldd_mount_type) {
558 #ifdef HAVE_LDISKFS_OSD
559         case LDD_MT_LDISKFS:
560         case LDD_MT_LDISKFS2:
561                 ret = ldiskfs_tune_lustre(dev, mop);
562                 break;
563 #endif /* HAVE_LDISKFS_OSD */
564 #ifdef HAVE_ZFS_OSD
565         case LDD_MT_ZFS:
566                 ret = zfs_tune_lustre(dev, mop);
567                 break;
568 #endif /* HAVE_ZFS_OSD */
569         default:
570                 fatal();
571                 fprintf(stderr, "unknown fs type %d '%s'\n",
572                                 ldd->ldd_mount_type, MT_STR(ldd));
573                 ret = EINVAL;
574                 break;
575         }
576
577         return ret;
578 }
579
580 int osd_label_lustre(struct mount_opts *mop)
581 {
582         struct lustre_disk_data *ldd = &mop->mo_ldd;
583         int ret;
584
585         switch (ldd->ldd_mount_type) {
586 #ifdef HAVE_LDISKFS_OSD
587         case LDD_MT_LDISKFS:
588         case LDD_MT_LDISKFS2:
589                 ret = ldiskfs_label_lustre(mop);
590                 break;
591 #endif /* HAVE_LDISKFS_OSD */
592 #ifdef HAVE_ZFS_OSD
593         case LDD_MT_ZFS:
594                 ret = zfs_label_lustre(mop);
595                 break;
596 #endif /* HAVE_ZFS_OSD */
597         default:
598                 fatal();
599                 fprintf(stderr, "unknown fs type %d '%s'\n",
600                         ldd->ldd_mount_type, MT_STR(ldd));
601                 ret = EINVAL;
602                 break;
603         }
604
605         return ret;
606 }
607
608 /* Enable quota accounting */
609 int osd_enable_quota(struct mkfs_opts *mop)
610 {
611         struct lustre_disk_data *ldd = &mop->mo_ldd;
612         int ret;
613
614         switch (ldd->ldd_mount_type) {
615 #ifdef HAVE_LDISKFS_OSD
616         case LDD_MT_EXT3:
617         case LDD_MT_LDISKFS:
618         case LDD_MT_LDISKFS2:
619                 ret = ldiskfs_enable_quota(mop);
620                 break;
621 #endif /* HAVE_LDISKFS_OSD */
622 #ifdef HAVE_ZFS_OSD
623         case LDD_MT_ZFS:
624                 fprintf(stderr, "this option is only valid for ldiskfs\n");
625                 ret = EINVAL;
626                 break;
627 #endif /* HAVE_ZFS_OSD */
628         default:
629                 fatal();
630                 fprintf(stderr, "unknown fs type %d '%s'\n",
631                         ldd->ldd_mount_type, MT_STR(ldd));
632                 ret = EINVAL;
633                 break;
634         }
635
636         return ret;
637 }
638
639 int osd_init(void)
640 {
641         int ret = 0;
642
643 #ifdef HAVE_LDISKFS_OSD
644         ret = ldiskfs_init();
645         if (ret)
646                 return ret;
647 #endif /* HAVE_LDISKFS_OSD */
648 #ifdef HAVE_ZFS_OSD
649         ret = zfs_init();
650         /* we want to be able to set up a ldiskfs-based filesystem w/o
651          * the ZFS modules installed, see ORI-425 */
652         if (ret)
653                 ret = 0;
654 #endif /* HAVE_ZFS_OSD */
655
656         return ret;
657 }
658
659 void osd_fini(void)
660 {
661 #ifdef HAVE_LDISKFS_OSD
662         ldiskfs_fini();
663 #endif /* HAVE_LDISKFS_OSD */
664 #ifdef HAVE_ZFS_OSD
665         zfs_fini();
666 #endif /* HAVE_ZFS_OSD */
667 }
668
669 __u64 get_device_size(char* device)
670 {
671         int ret, fd;
672         __u64 size = 0;
673
674         fd = open(device, O_RDONLY);
675         if (fd < 0) {
676                 fprintf(stderr, "%s: cannot open %s: %s\n",
677                         progname, device, strerror(errno));
678                 return 0;
679         }
680
681 #ifdef BLKGETSIZE64
682         /* size in bytes. bz5831 */
683         ret = ioctl(fd, BLKGETSIZE64, (void*)&size);
684 #else
685         {
686                 __u32 lsize = 0;
687                 /* size in blocks */
688                 ret = ioctl(fd, BLKGETSIZE, (void*)&lsize);
689                 size = (__u64)lsize * 512;
690         }
691 #endif
692         close(fd);
693         if (ret < 0) {
694                 fprintf(stderr, "%s: size ioctl failed: %s\n",
695                         progname, strerror(errno));
696                 return 0;
697         }
698
699         vprint("device size = "LPU64"MB\n", size >> 20);
700         /* return value in KB */
701         return size >> 10;
702 }
703
704 int file_create(char *path, __u64 size)
705 {
706         __u64 size_max;
707         int ret;
708         int fd;
709
710         /*
711          * Since "size" is in KB, the file offset it represents could overflow
712          * off_t.
713          */
714         size_max = (off_t)1 << (_FILE_OFFSET_BITS - 1 - 10);
715         if (size >= size_max) {
716                 fprintf(stderr, "%s: "LPU64" KB: Backing store size must be "
717                         "smaller than "LPU64" KB\n", progname, size, size_max);
718                 return EFBIG;
719         }
720
721         ret = access(path, F_OK);
722         if (ret == 0) {
723                 ret = unlink(path);
724                 if (ret != 0)
725                         return errno;
726         }
727
728         fd = creat(path, S_IRUSR|S_IWUSR);
729         if (fd < 0) {
730                 fatal();
731                 fprintf(stderr, "%s: Unable to create backing store: %s\n",
732                         progname, strerror(errno));
733                 return errno;
734         }
735
736         ret = ftruncate(fd, size * 1024);
737         close(fd);
738         if (ret != 0) {
739                 fatal();
740                 fprintf(stderr, "%s: Unable to truncate backing store: %s\n",
741                         progname, strerror(errno));
742                 return errno;
743         }
744
745         return 0;
746 }