Whamcloud - gitweb
effa6c7650910533514b41c317fc39eba36072fd
[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 Whamcloud, Inc.
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 <stdio.h>
41 #include <errno.h>
42 #include <string.h>
43 #include <config.h>
44 #include <lustre_disk.h>
45 #include <lustre_ver.h>
46 #include <sys/stat.h>
47 #include <sys/utsname.h>
48 #include "mount_utils.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 int update_mtab_entry(char *spec, char *mtpt, char *type, char *opts,
184                 int flags, int freq, int pass)
185 {
186         FILE *fp;
187         struct mntent mnt;
188         int rc = 0;
189
190         mnt.mnt_fsname = spec;
191         mnt.mnt_dir = mtpt;
192         mnt.mnt_type = type;
193         mnt.mnt_opts = opts ? opts : "";
194         mnt.mnt_freq = freq;
195         mnt.mnt_passno = pass;
196
197         fp = setmntent(MOUNTED, "a+");
198         if (fp == NULL) {
199                 fprintf(stderr, "%s: setmntent(%s): %s:",
200                         progname, MOUNTED, strerror (errno));
201                 rc = 16;
202         } else {
203                 if ((addmntent(fp, &mnt)) == 1) {
204                         fprintf(stderr, "%s: addmntent: %s:",
205                                 progname, strerror (errno));
206                         rc = 16;
207                 }
208                 endmntent(fp);
209         }
210
211         return rc;
212 }
213
214 /* Search for opt in mntlist, returning true if found.
215  */
216 static int in_mntlist(char *opt, char *mntlist)
217 {
218         char *ml, *mlp, *item, *ctx = NULL;
219
220         if (!(ml = strdup(mntlist))) {
221                 fprintf(stderr, "%s: out of memory\n", progname);
222                 exit(1);
223         }
224         mlp = ml;
225         while ((item = strtok_r(mlp, ",", &ctx))) {
226                 if (!strcmp(opt, item))
227                         break;
228                 mlp = NULL;
229         }
230         free(ml);
231         return (item != NULL);
232 }
233
234 /* Issue a message on stderr for every item in wanted_mountopts that is not
235  * present in mountopts.  The justwarn boolean toggles between error and
236  * warning message.  Return an error count.
237  */
238 int check_mountfsoptions(char *mountopts, char *wanted_mountopts,
239                          int justwarn)
240 {
241         char *ml, *mlp, *item, *ctx = NULL;
242         int errors = 0;
243
244         if (!(ml = strdup(wanted_mountopts))) {
245                 fprintf(stderr, "%s: out of memory\n", progname);
246                 exit(1);
247         }
248         mlp = ml;
249         while ((item = strtok_r(mlp, ",", &ctx))) {
250                 if (!in_mntlist(item, mountopts)) {
251                         fprintf(stderr, "%s: %s mount option `%s' is missing\n",
252                                 progname, justwarn ? "Warning: default"
253                                 : "Error: mandatory", item);
254                         errors++;
255                 }
256                 mlp = NULL;
257         }
258         free(ml);
259         return errors;
260 }
261
262 /* Trim embedded white space, leading and trailing commas from string s.
263  */
264 void trim_mountfsoptions(char *s)
265 {
266         char *p;
267
268         for (p = s; *p; ) {
269                 if (isspace(*p)) {
270                         memmove(p, p + 1, strlen(p + 1) + 1);
271                         continue;
272                 }
273                 p++;
274         }
275
276         while (s[0] == ',')
277                 memmove(&s[0], &s[1], strlen(&s[1]) + 1);
278
279         p = s + strlen(s) - 1;
280         while (p >= s && *p == ',')
281                 *p-- = '\0';
282 }
283
284 /* Setup a file in the first unused loop_device */
285 int loop_setup(struct mkfs_opts *mop)
286 {
287         char loop_base[20];
288         char l_device[64];
289         int i, ret = 0;
290
291         /* Figure out the loop device names */
292         if (!access("/dev/loop0", F_OK | R_OK)) {
293                 strcpy(loop_base, "/dev/loop\0");
294         } else if (!access("/dev/loop/0", F_OK | R_OK)) {
295                 strcpy(loop_base, "/dev/loop/\0");
296         } else {
297                 fprintf(stderr, "%s: can't access loop devices\n", progname);
298                 return EACCES;
299         }
300
301         /* Find unused loop device */
302         for (i = 0; i < MAX_LOOP_DEVICES; i++) {
303                 char cmd[PATH_MAX];
304                 int cmdsz = sizeof(cmd);
305
306                 sprintf(l_device, "%s%d", loop_base, i);
307                 if (access(l_device, F_OK | R_OK))
308                         break;
309                 snprintf(cmd, cmdsz, "losetup %s > /dev/null 2>&1", l_device);
310                 ret = system(cmd);
311
312                 /* losetup gets 1 (ret=256) for non-set-up device */
313                 if (ret) {
314                         /* Set up a loopback device to our file */
315                         snprintf(cmd, cmdsz, "losetup %s %s", l_device,
316                                  mop->mo_device);
317                         ret = run_command(cmd, cmdsz);
318                         if (ret == 256)
319                                 /* someone else picked up this loop device
320                                  * behind our back */
321                                 continue;
322                         if (ret) {
323                                 fprintf(stderr, "%s: error %d on losetup: %s\n",
324                                         progname, ret, strerror(ret));
325                                 return ret;
326                         }
327                         strscpy(mop->mo_loopdev, l_device,
328                                 sizeof(mop->mo_loopdev));
329                         return ret;
330                 }
331         }
332
333         fprintf(stderr, "%s: out of loop devices!\n", progname);
334         return EMFILE;
335 }
336
337 int loop_cleanup(struct mkfs_opts *mop)
338 {
339         char cmd[150];
340         int ret = 1;
341         if ((mop->mo_flags & MO_IS_LOOP) && *mop->mo_loopdev) {
342                 sprintf(cmd, "losetup -d %s", mop->mo_loopdev);
343                 ret = run_command(cmd, sizeof(cmd));
344         }
345         return ret;
346 }
347
348 int loop_format(struct mkfs_opts *mop)
349 {
350         int fd;
351
352         if (mop->mo_device_sz == 0) {
353                 fatal();
354                 fprintf(stderr, "loop device requires a --device-size= "
355                         "param\n");
356                 return EINVAL;
357         }
358
359         fd = creat(mop->mo_device, S_IRUSR|S_IWUSR);
360         if (fd < 0) {
361                 fatal();
362                 fprintf(stderr, "%s: Unable to create backing store: %s\n",
363                         progname, strerror(errno));
364                 return errno;
365         }
366
367         if (ftruncate(fd, mop->mo_device_sz * 1024) != 0) {
368                 close(fd);
369                 fatal();
370                 fprintf(stderr, "%s: Unable to truncate backing store: %s\n",
371                         progname, strerror(errno));
372                 return errno;
373         }
374
375         close(fd);
376         return 0;
377 }
378
379 /* Write the server config files */
380 int osd_write_ldd(struct mkfs_opts *mop)
381 {
382         struct lustre_disk_data *ldd = &mop->mo_ldd;
383         int ret;
384
385         switch (ldd->ldd_mount_type) {
386 #ifdef HAVE_LDISKFS_OSD
387         case LDD_MT_LDISKFS:
388         case LDD_MT_LDISKFS2:
389                 ret = ldiskfs_write_ldd(mop);
390                 break;
391 #endif /* HAVE_LDISKFS_OSD */
392 #ifdef HAVE_ZFS_OSD
393         case LDD_MT_ZFS:
394                 ret = zfs_write_ldd(mop);
395                 break;
396 #endif /* HAVE_ZFS_OSD */
397         default:
398                 fatal();
399                 fprintf(stderr, "unknown fs type %d '%s'\n",
400                         ldd->ldd_mount_type, MT_STR(ldd));
401                 ret = EINVAL;
402                 break;
403         }
404
405         return ret;
406 }
407
408 /* Read the server config files */
409 int osd_read_ldd(char *dev, struct lustre_disk_data *ldd)
410 {
411         int ret;
412
413         switch (ldd->ldd_mount_type) {
414 #ifdef HAVE_LDISKFS_OSD
415         case LDD_MT_LDISKFS:
416         case LDD_MT_LDISKFS2:
417                 ret = ldiskfs_read_ldd(dev, ldd);
418                 break;
419 #endif /* HAVE_LDISKFS_OSD */
420 #ifdef HAVE_ZFS_OSD
421         case LDD_MT_ZFS:
422                 ret = zfs_read_ldd(dev, ldd);
423                 break;
424 #endif /* HAVE_ZFS_OSD */
425         default:
426                 fatal();
427                 fprintf(stderr, "unknown fs type %d '%s'\n",
428                         ldd->ldd_mount_type, MT_STR(ldd));
429                 ret = EINVAL;
430                 break;
431         }
432
433         return ret;
434 }
435
436 /* Was this device formatted for Lustre */
437 int osd_is_lustre(char *dev, unsigned *mount_type)
438 {
439         vprint("checking for existing Lustre data: ");
440
441 #ifdef HAVE_LDISKFS_OSD
442         if (ldiskfs_is_lustre(dev, mount_type)) {
443                 vprint("found\n");
444                 return 1;
445         }
446 #endif /* HAVE_LDISKFS_OSD */
447 #ifdef HAVE_ZFS_OSD
448         if (zfs_is_lustre(dev, mount_type)) {
449                 vprint("found\n");
450                 return 1;
451         }
452 #endif /* HAVE_ZFS_OSD */
453
454         vprint("not found\n");
455         return 0;
456 }
457
458 /* Build fs according to type */
459 int osd_make_lustre(struct mkfs_opts *mop)
460 {
461         struct lustre_disk_data *ldd = &mop->mo_ldd;
462         int ret;
463
464         switch (ldd->ldd_mount_type) {
465 #ifdef HAVE_LDISKFS_OSD
466         case LDD_MT_LDISKFS:
467         case LDD_MT_LDISKFS2:
468                 ret = ldiskfs_make_lustre(mop);
469                 break;
470 #endif /* HAVE_LDISKFS_OSD */
471 #ifdef HAVE_ZFS_OSD
472         case LDD_MT_ZFS:
473                 ret = zfs_make_lustre(mop);
474                 break;
475 #endif /* HAVE_ZFS_OSD */
476         default:
477                 fatal();
478                 fprintf(stderr, "unknown fs type %d '%s'\n",
479                         ldd->ldd_mount_type, MT_STR(ldd));
480                 ret = EINVAL;
481                 break;
482         }
483
484         return ret;
485 }
486
487 int osd_prepare_lustre(struct mkfs_opts *mop,
488                 char *default_mountopts, int default_len,
489                 char *always_mountopts, int always_len)
490 {
491         struct lustre_disk_data *ldd = &mop->mo_ldd;
492         int ret;
493
494         switch (ldd->ldd_mount_type) {
495 #ifdef HAVE_LDISKFS_OSD
496         case LDD_MT_LDISKFS:
497         case LDD_MT_LDISKFS2:
498                 ret = ldiskfs_prepare_lustre(mop,
499                                              default_mountopts, default_len,
500                                              always_mountopts, always_len);
501                 break;
502 #endif /* HAVE_LDISKFS_OSD */
503 #ifdef HAVE_ZFS_OSD
504         case LDD_MT_ZFS:
505                 ret = zfs_prepare_lustre(mop,
506                                          default_mountopts, default_len,
507                                          always_mountopts, always_len);
508                 break;
509 #endif /* HAVE_ZFS_OSD */
510         default:
511                 fatal();
512                 fprintf(stderr, "unknown fs type %d '%s'\n",
513                         ldd->ldd_mount_type, MT_STR(ldd));
514                 ret = EINVAL;
515                 break;
516         }
517
518         return ret;
519 }
520
521 int osd_tune_lustre(char *dev, struct mount_opts *mop)
522 {
523         struct lustre_disk_data *ldd = &mop->mo_ldd;
524         int ret;
525
526         switch (ldd->ldd_mount_type) {
527 #ifdef HAVE_LDISKFS_OSD
528         case LDD_MT_LDISKFS:
529         case LDD_MT_LDISKFS2:
530                 ret = ldiskfs_tune_lustre(dev, mop);
531                 break;
532 #endif /* HAVE_LDISKFS_OSD */
533 #ifdef HAVE_ZFS_OSD
534         case LDD_MT_ZFS:
535                 ret = zfs_tune_lustre(dev, mop);
536                 break;
537 #endif /* HAVE_ZFS_OSD */
538         default:
539                 fatal();
540                 fprintf(stderr, "unknown fs type %d '%s'\n",
541                                 ldd->ldd_mount_type, MT_STR(ldd));
542                 ret = EINVAL;
543                 break;
544         }
545
546         return ret;
547 }
548
549 int osd_label_lustre(struct mount_opts *mop)
550 {
551         struct lustre_disk_data *ldd = &mop->mo_ldd;
552         int ret;
553
554         switch (ldd->ldd_mount_type) {
555 #ifdef HAVE_LDISKFS_OSD
556         case LDD_MT_LDISKFS:
557         case LDD_MT_LDISKFS2:
558                 ret = ldiskfs_label_lustre(mop);
559                 break;
560 #endif /* HAVE_LDISKFS_OSD */
561 #ifdef HAVE_ZFS_OSD
562         case LDD_MT_ZFS:
563                 ret = zfs_label_lustre(mop);
564                 break;
565 #endif /* HAVE_ZFS_OSD */
566         default:
567                 fatal();
568                 fprintf(stderr, "unknown fs type %d '%s'\n",
569                         ldd->ldd_mount_type, MT_STR(ldd));
570                 ret = EINVAL;
571                 break;
572         }
573
574         return ret;
575 }
576
577 int osd_init(void)
578 {
579         int ret = 0;
580
581 #ifdef HAVE_LDISKFS_OSD
582         ret = ldiskfs_init();
583         if (ret)
584                 return ret;
585 #endif /* HAVE_LDISKFS_OSD */
586 #ifdef HAVE_ZFS_OSD
587         ret = zfs_init();
588         /* we want to be able to set up a ldiskfs-based filesystem w/o
589          * the ZFS modules installed, see ORI-425 */
590         if (ret)
591                 ret = 0;
592 #endif /* HAVE_ZFS_OSD */
593
594         return ret;
595 }
596
597 void osd_fini(void)
598 {
599 #ifdef HAVE_LDISKFS_OSD
600         ldiskfs_fini();
601 #endif /* HAVE_LDISKFS_OSD */
602 #ifdef HAVE_ZFS_OSD
603         zfs_fini();
604 #endif /* HAVE_ZFS_OSD */
605 }
606
607 __u64 get_device_size(char* device)
608 {
609         int ret, fd;
610         __u64 size = 0;
611
612         fd = open(device, O_RDONLY);
613         if (fd < 0) {
614                 fprintf(stderr, "%s: cannot open %s: %s\n",
615                         progname, device, strerror(errno));
616                 return 0;
617         }
618
619 #ifdef BLKGETSIZE64
620         /* size in bytes. bz5831 */
621         ret = ioctl(fd, BLKGETSIZE64, (void*)&size);
622 #else
623         {
624                 __u32 lsize = 0;
625                 /* size in blocks */
626                 ret = ioctl(fd, BLKGETSIZE, (void*)&lsize);
627                 size = (__u64)lsize * 512;
628         }
629 #endif
630         close(fd);
631         if (ret < 0) {
632                 fprintf(stderr, "%s: size ioctl failed: %s\n",
633                         progname, strerror(errno));
634                 return 0;
635         }
636
637         vprint("device size = "LPU64"MB\n", size >> 20);
638         /* return value in KB */
639         return size >> 10;
640 }
641
642 int file_create(char *path, int size)
643 {
644         int ret;
645         int fd;
646
647         ret = access(path, F_OK);
648         if (ret == 0) {
649                 ret = unlink(path);
650                 if (ret != 0)
651                         return errno;
652         }
653
654         fd = creat(path, S_IRUSR|S_IWUSR);
655         if (fd < 0) {
656                 fatal();
657                 fprintf(stderr, "%s: Unable to create backing store: %s\n",
658                         progname, strerror(errno));
659                 return errno;
660         }
661
662         ret = ftruncate(fd, size * 1024);
663         close(fd);
664         if (ret != 0) {
665                 fatal();
666                 fprintf(stderr, "%s: Unable to truncate backing store: %s\n",
667                         progname, strerror(errno));
668                 return errno;
669         }
670
671         return 0;
672 }