4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2012, 2014, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
35 #endif /* HAVE_CONFIG_H */
45 #include <lustre_disk.h>
46 #include <lustre_ver.h>
47 #include <sys/mount.h>
49 #include <sys/utsname.h>
50 #include <linux/loop.h>
51 #include <sys/types.h>
57 #include <lustre/utils/gss/sk_utils.h>
60 #include "mount_utils.h"
62 extern char *progname;
65 #define vprint(fmt, arg...) if (verbose > 0) printf(fmt, ##arg)
66 #define verrprint(fmt, arg...) if (verbose >= 0) fprintf(stderr, fmt, ##arg)
68 static struct module_backfs_ops *backfs_ops[LDD_MT_LAST];
73 fprintf(stderr, "\n%s FATAL: ", progname);
76 int run_command(char *cmd, int cmdsz)
78 char log[] = "/tmp/run_command_logXXXXXX";
81 if ((cmdsz - strlen(cmd)) < 6) {
83 fprintf(stderr, "Command buffer overflow: %.*s...\n",
89 printf("cmd: %s\n", cmd);
91 if ((fd = mkstemp(log)) >= 0) {
99 /* Can't use popen because we need the rv of the command */
101 if (rc && (fd >= 0)) {
104 fp = fopen(log, "r");
106 while (fgets(buf, sizeof(buf), fp) != NULL) {
117 int add_param(char *buf, char *key, char *val)
119 int end = sizeof(((struct lustre_disk_data *)0)->ldd_params);
120 int start = strlen(buf);
124 keylen = strlen(key);
125 if (start + 1 + keylen + strlen(val) >= end) {
126 fprintf(stderr, "%s: params are too long-\n%s %s%s\n",
127 progname, buf, key ? key : "", val);
131 sprintf(buf + start, " %s%s", key ? key : "", val);
135 int get_param(char *buf, char *key, char **val)
137 int i, key_len = strlen(key);
140 ptr = strstr(buf, key);
142 *val = strdup(ptr + key_len);
146 for (i = 0; i < strlen(*val); i++)
147 if (((*val)[i] == ' ') || ((*val)[i] == '\0'))
157 int append_param(char *buf, char *key, char *val, char sep)
159 int key_len, i, offset, old_val_len;
160 char *ptr = NULL, str[1024];
163 ptr = strstr(buf, key);
165 /* key doesn't exist yet, so just add it */
167 return add_param(buf, key, val);
169 key_len = strlen(key);
171 /* Copy previous values to str */
172 for (i = 0; i < sizeof(str); ++i) {
173 if ((ptr[i+key_len] == ' ') || (ptr[i+key_len] == '\0'))
175 str[i] = ptr[i+key_len];
177 if (i == sizeof(str))
181 offset = old_val_len+key_len;
183 /* Move rest of buf to overwrite previous key and value */
184 for (i = 0; ptr[i+offset] != '\0'; ++i)
185 ptr[i] = ptr[i+offset];
189 snprintf(str+old_val_len, sizeof(str)-old_val_len, "%c%s", sep, val);
191 return add_param(buf, key, str);
194 char *strscat(char *dst, char *src, int buflen)
197 if (strlen(dst) + strlen(src) >= buflen) {
198 fprintf(stderr, "string buffer overflow (max %d): '%s' + '%s'"
199 "\n", buflen, dst, src);
202 return strcat(dst, src);
205 char *strscpy(char *dst, char *src, int buflen)
208 return strscat(dst, src, buflen);
211 int check_mtab_entry(char *spec1, char *spec2, char *mtpt, char *type)
216 fp = setmntent(MOUNTED, "r");
220 while ((mnt = getmntent(fp)) != NULL) {
221 if ((strcmp(mnt->mnt_fsname, spec1) == 0 ||
222 strcmp(mnt->mnt_fsname, spec2) == 0) &&
223 (mtpt == NULL || strcmp(mnt->mnt_dir, mtpt) == 0) &&
224 (type == NULL || strcmp(mnt->mnt_type, type) == 0)) {
235 #include <linux/magic.h>
237 static int mtab_is_proc(const char *mtab)
240 if (statfs(mtab, &s) < 0)
243 return (s.f_type == PROC_SUPER_MAGIC);
248 # include <libmount/libmount.h>
251 * The libmount is part of util-linux since 2.18.
252 * We use it to update utab to avoid umount would
253 * blocked in some rare case.
255 int update_utab_entry(struct mount_opts *mop)
257 struct libmnt_fs *fs = mnt_new_fs();
258 struct libmnt_update *upd;
261 mnt_fs_set_source(fs, mop->mo_source);
262 mnt_fs_set_target(fs, mop->mo_target);
263 mnt_fs_set_fstype(fs, "lustre");
264 mnt_fs_set_attributes(fs, "lustre");
266 upd = mnt_new_update();
270 rc = mnt_update_set_fs(upd, mop->mo_nomtab ? MS_REMOUNT : 0, NULL, fs);
271 if (rc == 1) /* update is unnecessary */
275 "error: failed to save utab entry: rc = %d\n", rc);
277 rc = mnt_update_table(upd, NULL);
280 mnt_free_update(upd);
286 int update_utab_entry(struct mount_opts *mop)
290 #endif /* HAVE_LIBMOUNT */
292 int update_mtab_entry(char *spec, char *mtpt, char *type, char *opts,
293 int flags, int freq, int pass)
299 /* Don't update mtab if it is linked to any file in /proc direcotry.*/
300 if (mtab_is_proc(MOUNTED))
303 mnt.mnt_fsname = spec;
306 mnt.mnt_opts = opts ? opts : "";
308 mnt.mnt_passno = pass;
310 fp = setmntent(MOUNTED, "a+");
312 fprintf(stderr, "%s: setmntent(%s): %s\n",
313 progname, MOUNTED, strerror (errno));
316 if ((addmntent(fp, &mnt)) == 1) {
317 fprintf(stderr, "%s: addmntent: %s\n",
318 progname, strerror (errno));
327 /* Search for opt in mntlist, returning true if found.
329 static int in_mntlist(char *opt, char *mntlist)
331 char *ml, *mlp, *item, *ctx = NULL;
333 if (!(ml = strdup(mntlist))) {
334 fprintf(stderr, "%s: out of memory\n", progname);
338 while ((item = strtok_r(mlp, ",", &ctx))) {
339 if (!strcmp(opt, item))
344 return (item != NULL);
347 /* Issue a message on stderr for every item in wanted_mountopts that is not
348 * present in mountopts. The justwarn boolean toggles between error and
349 * warning message. Return an error count.
351 int check_mountfsoptions(char *mountopts, char *wanted_mountopts)
353 char *ml, *mlp, *item, *ctx = NULL;
356 if (!(ml = strdup(wanted_mountopts))) {
357 fprintf(stderr, "%s: out of memory\n", progname);
361 while ((item = strtok_r(mlp, ",", &ctx))) {
362 if (!in_mntlist(item, mountopts)) {
363 fprintf(stderr, "%s: Error: mandatory mount option"
364 " '%s' is missing\n", progname, item);
373 /* Trim embedded white space, leading and trailing commas from string s.
375 void trim_mountfsoptions(char *s)
381 memmove(p, p + 1, strlen(p + 1) + 1);
388 memmove(&s[0], &s[1], strlen(&s[1]) + 1);
390 p = s + strlen(s) - 1;
391 while (p >= s && *p == ',')
395 /* Setup a file in the first unused loop_device */
396 int loop_setup(struct mkfs_opts *mop)
402 /* Figure out the loop device names */
403 if (!access("/dev/loop0", F_OK | R_OK) ||
404 !access("/dev/loop-control", F_OK | R_OK)) {
405 strcpy(loop_base, "/dev/loop\0");
406 } else if (!access("/dev/loop/0", F_OK | R_OK)) {
407 strcpy(loop_base, "/dev/loop/\0");
409 fprintf(stderr, "%s: can't access loop devices\n", progname);
413 /* Find unused loop device */
414 for (i = 0; i < MAX_LOOP_DEVICES; i++) {
416 int cmdsz = sizeof(cmd);
418 #ifdef HAVE_LOOP_CTL_GET_FREE
419 ret = open("/dev/loop-control", O_RDWR);
421 fprintf(stderr, "%s: can't access loop control\n", progname);
424 /* find or allocate a free loop device to use */
425 i = ioctl(ret, LOOP_CTL_GET_FREE);
428 fprintf(stderr, "%s: access loop control error\n", progname);
431 sprintf(l_device, "%s%d", loop_base, i);
433 sprintf(l_device, "%s%d", loop_base, i);
434 if (access(l_device, F_OK | R_OK))
437 snprintf(cmd, cmdsz, "losetup %s > /dev/null 2>&1", l_device);
440 /* losetup gets 1 (ret=256) for non-set-up device */
442 /* Set up a loopback device to our file */
443 snprintf(cmd, cmdsz, "losetup %s %s", l_device,
445 ret = run_command(cmd, cmdsz);
447 /* someone else picked up this loop device
451 fprintf(stderr, "%s: error %d on losetup: %s\n",
453 ret >= 0 ? strerror(ret) : "");
456 strscpy(mop->mo_loopdev, l_device,
457 sizeof(mop->mo_loopdev));
462 fprintf(stderr, "%s: out of loop devices!\n", progname);
466 int loop_cleanup(struct mkfs_opts *mop)
471 if ((mop->mo_flags & MO_IS_LOOP) && *mop->mo_loopdev) {
474 sprintf(cmd, "losetup -d %s", mop->mo_loopdev);
475 for (tries = 0; tries < 3; tries++) {
476 ret = run_command(cmd, sizeof(cmd));
484 fprintf(stderr, "cannot cleanup %s: rc = %d\n",
485 mop->mo_loopdev, ret);
489 int loop_format(struct mkfs_opts *mop)
493 if (mop->mo_device_kb == 0) {
495 fprintf(stderr, "loop device requires a --device-size= "
500 fd = creat(mop->mo_device, S_IRUSR|S_IWUSR);
503 fprintf(stderr, "%s: Unable to create backing store: %s\n",
504 progname, strerror(errno));
508 if (ftruncate(fd, mop->mo_device_kb * 1024) != 0) {
511 fprintf(stderr, "%s: Unable to truncate backing store: %s\n",
512 progname, strerror(errno));
520 #define DLSYM(prefix, sym, func) \
523 snprintf(_fname, sizeof(_fname), "%s_%s", prefix, #func); \
524 sym->func = (typeof(sym->func))dlsym(sym->dl_handle, _fname); \
528 * Load plugin for a given mount_type from ${pkglibdir}/mount_osd_FSTYPE.so and
529 * return struct of function pointers (will be freed in unloack_backfs_module).
531 * \param[in] mount_type Mount type to load module for.
532 * \retval Value of backfs_ops struct
533 * \retval NULL if no module exists
535 struct module_backfs_ops *load_backfs_module(enum ldd_mount_type mount_type)
538 char *error, filename[512], fsname[512], *name;
539 struct module_backfs_ops *ops;
541 /* This deals with duplicate ldd_mount_types resolving to same OSD layer
542 * plugin (e.g. ext3/ldiskfs/ldiskfs2 all being ldiskfs) */
543 strncpy(fsname, mt_type(mount_type), sizeof(fsname));
544 name = fsname + sizeof("osd-") - 1;
546 /* change osd- to osd_ */
547 fsname[sizeof("osd-") - 2] = '_';
549 snprintf(filename, sizeof(filename), PLUGIN_DIR"/mount_%s.so", fsname);
551 handle = dlopen(filename, RTLD_LAZY);
553 /* Check for $LUSTRE environment variable from test-framework.
554 * This allows using locally built modules to be used.
556 if (handle == NULL) {
558 dirname = getenv("LUSTRE");
560 snprintf(filename, sizeof(filename),
561 "%s/utils/.libs/mount_%s.so",
563 handle = dlopen(filename, RTLD_LAZY);
567 /* Do not clutter up console with missing types */
571 ops = malloc(sizeof(*ops));
577 ops->dl_handle = handle;
578 dlerror(); /* Clear any existing error */
580 DLSYM(name, ops, init);
581 DLSYM(name, ops, fini);
582 DLSYM(name, ops, read_ldd);
583 DLSYM(name, ops, write_ldd);
584 DLSYM(name, ops, is_lustre);
585 DLSYM(name, ops, make_lustre);
586 DLSYM(name, ops, prepare_lustre);
587 DLSYM(name, ops, tune_lustre);
588 DLSYM(name, ops, label_lustre);
589 DLSYM(name, ops, enable_quota);
594 fprintf(stderr, "%s\n", error);
600 /* optional methods */
601 DLSYM(name, ops, fix_mountopts);
607 * Unload plugin and free backfs_ops structure. Must be called the same number
608 * of times as load_backfs_module is.
610 void unload_backfs_module(struct module_backfs_ops *ops)
615 dlclose(ops->dl_handle);
619 /* Return true if backfs_ops has operations for the given mount_type. */
620 int backfs_mount_type_okay(enum ldd_mount_type mount_type)
622 if (unlikely(mount_type >= LDD_MT_LAST || mount_type < 0)) {
624 fprintf(stderr, "fs type out of range %d\n", mount_type);
627 if (backfs_ops[mount_type] == NULL) {
629 fprintf(stderr, "unhandled/unloaded fs type %d '%s'\n",
630 mount_type, mt_str(mount_type));
636 /* Write the server config files */
637 int osd_write_ldd(struct mkfs_opts *mop)
639 struct lustre_disk_data *ldd = &mop->mo_ldd;
642 if (backfs_mount_type_okay(ldd->ldd_mount_type))
643 ret = backfs_ops[ldd->ldd_mount_type]->write_ldd(mop);
650 /* Read the server config files */
651 int osd_read_ldd(char *dev, struct lustre_disk_data *ldd)
655 if (backfs_mount_type_okay(ldd->ldd_mount_type))
656 ret = backfs_ops[ldd->ldd_mount_type]->read_ldd(dev, ldd);
663 /* Was this device formatted for Lustre */
664 int osd_is_lustre(char *dev, unsigned *mount_type)
668 vprint("checking for existing Lustre data: ");
670 for (i = 0; i < LDD_MT_LAST; ++i) {
671 if (backfs_ops[i] != NULL &&
672 backfs_ops[i]->is_lustre(dev, mount_type)) {
678 vprint("not found\n");
682 /* Build fs according to type */
683 int osd_make_lustre(struct mkfs_opts *mop)
685 struct lustre_disk_data *ldd = &mop->mo_ldd;
688 if (backfs_mount_type_okay(ldd->ldd_mount_type))
689 ret = backfs_ops[ldd->ldd_mount_type]->make_lustre(mop);
696 int osd_prepare_lustre(struct mkfs_opts *mop,
697 char *wanted_mountopts, size_t len)
699 struct lustre_disk_data *ldd = &mop->mo_ldd;
702 if (backfs_mount_type_okay(ldd->ldd_mount_type))
703 ret = backfs_ops[ldd->ldd_mount_type]->prepare_lustre(mop,
704 wanted_mountopts, len);
711 int osd_fix_mountopts(struct mkfs_opts *mop, char *mountopts, size_t len)
713 struct lustre_disk_data *ldd = &mop->mo_ldd;
715 if (!backfs_mount_type_okay(ldd->ldd_mount_type))
718 if (backfs_ops[ldd->ldd_mount_type]->fix_mountopts == NULL)
721 return backfs_ops[ldd->ldd_mount_type]->fix_mountopts(mop, mountopts,
725 int osd_tune_lustre(char *dev, struct mount_opts *mop)
727 struct lustre_disk_data *ldd = &mop->mo_ldd;
730 if (backfs_mount_type_okay(ldd->ldd_mount_type))
731 ret = backfs_ops[ldd->ldd_mount_type]->tune_lustre(dev, mop);
738 int osd_label_lustre(struct mount_opts *mop)
740 struct lustre_disk_data *ldd = &mop->mo_ldd;
743 if (backfs_mount_type_okay(ldd->ldd_mount_type))
744 ret = backfs_ops[ldd->ldd_mount_type]->label_lustre(mop);
751 /* Enable quota accounting */
752 int osd_enable_quota(struct mkfs_opts *mop)
754 struct lustre_disk_data *ldd = &mop->mo_ldd;
757 if (backfs_mount_type_okay(ldd->ldd_mount_type))
758 ret = backfs_ops[ldd->ldd_mount_type]->enable_quota(mop);
767 int i, rc, ret = EINVAL;
769 for (i = 0; i < LDD_MT_LAST; ++i) {
771 backfs_ops[i] = load_backfs_module(i);
772 if (backfs_ops[i] != NULL)
773 rc = backfs_ops[i]->init();
775 backfs_ops[i]->fini();
776 unload_backfs_module(backfs_ops[i]);
777 backfs_ops[i] = NULL;
789 for (i = 0; i < LDD_MT_LAST; ++i) {
790 if (backfs_ops[i] != NULL) {
791 backfs_ops[i]->fini();
792 unload_backfs_module(backfs_ops[i]);
793 backfs_ops[i] = NULL;
798 __u64 get_device_size(char* device)
803 fd = open(device, O_RDONLY);
805 fprintf(stderr, "%s: cannot open %s: %s\n",
806 progname, device, strerror(errno));
811 /* size in bytes. bz5831 */
812 ret = ioctl(fd, BLKGETSIZE64, (void*)&size);
817 ret = ioctl(fd, BLKGETSIZE, (void*)&lsize);
818 size = (__u64)lsize * 512;
823 fprintf(stderr, "%s: size ioctl failed: %s\n",
824 progname, strerror(errno));
828 vprint("device size = %juMB\n", (uintmax_t)(size >> 20));
829 /* return value in KB */
833 int file_create(char *path, __u64 size)
840 * Since "size" is in KB, the file offset it represents could overflow
843 size_max = (off_t)1 << (_FILE_OFFSET_BITS - 1 - 10);
844 if (size >= size_max) {
845 fprintf(stderr, "%s: %ju KB: Backing store size must be "
846 "smaller than %ju KB\n", progname, (uintmax_t) size,
847 (uintmax_t)size_max);
851 ret = access(path, F_OK);
858 fd = creat(path, S_IRUSR|S_IWUSR);
861 fprintf(stderr, "%s: Unable to create backing store: %s\n",
862 progname, strerror(errno));
866 ret = ftruncate(fd, size * 1024);
870 fprintf(stderr, "%s: Unable to truncate backing store: %s\n",
871 progname, strerror(errno));
879 #ifdef HAVE_OPENSSL_SSK
880 int load_shared_keys(struct mount_opts *mop)
883 struct dirent *dentry;
885 char fullpath[PATH_MAX];
886 char *path = mop->mo_skpath;
890 sk_init_logging(NULL, 1, 1);
892 rc = stat(path, &sbuf);
894 fprintf(stderr, "stat() failed for key %s: %s\n", path,
899 /* Load individual keys or a directory of them */
900 if (S_ISREG(sbuf.st_mode)) {
901 return sk_load_keyfile(path);
902 } else if (!S_ISDIR(sbuf.st_mode)) {
903 fprintf(stderr, "Invalid shared key path: %s\n", path);
909 fprintf(stderr, "Unable to open shared key directory: %s\n",
914 /* Loop through the files in the directory attempting to load them.
915 * Any issue with loading the keyfile is treated as an error although
916 * the loop continues until all files have been attempted. This will
917 * allow all errors be reported at once rather then requiring
918 * incremental corrections to fix each one and try again. */
919 while ((dentry = readdir(dir)) != NULL) {
920 if (strcmp(".", dentry->d_name) == 0 ||
921 strcmp("..", dentry->d_name) == 0)
924 rc = snprintf(fullpath, PATH_MAX, "%s/%s", path,
926 if (rc >= PATH_MAX) {
927 fprintf(stderr, "Path too long for %s/%s\n",
928 path, dentry->d_name);
933 rc = stat(fullpath, &sbuf);
935 fprintf(stderr, "Unable to stat %s: %s\n", fullpath,
941 if (!S_ISREG(sbuf.st_mode))
944 rc = sk_load_keyfile(fullpath);
946 fprintf(stderr, "Failed to load key %s\n", fullpath);
953 #endif /* HAVE_OPENSSL_SSK */
954 #endif /* HAVE_GSS */