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