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.sun.com/software/products/lustre/docs/GPLv2.pdf
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
27 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2012, 2014, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
39 #endif /* HAVE_CONFIG_H */
49 #include <lustre_disk.h>
50 #include <lustre_ver.h>
51 #include <sys/mount.h>
53 #include <sys/utsname.h>
54 #include <linux/loop.h>
55 #include <sys/types.h>
61 #include <lustre/utils/gss/sk_utils.h>
64 #include "mount_utils.h"
66 extern char *progname;
69 #define vprint(fmt, arg...) if (verbose > 0) printf(fmt, ##arg)
70 #define verrprint(fmt, arg...) if (verbose >= 0) fprintf(stderr, fmt, ##arg)
72 static struct module_backfs_ops *backfs_ops[LDD_MT_LAST];
77 fprintf(stderr, "\n%s FATAL: ", progname);
80 int run_command(char *cmd, int cmdsz)
82 char log[] = "/tmp/run_command_logXXXXXX";
85 if ((cmdsz - strlen(cmd)) < 6) {
87 fprintf(stderr, "Command buffer overflow: %.*s...\n",
93 printf("cmd: %s\n", cmd);
95 if ((fd = mkstemp(log)) >= 0) {
101 strcat(cmd, " 2>&1");
103 /* Can't use popen because we need the rv of the command */
105 if (rc && (fd >= 0)) {
108 fp = fopen(log, "r");
110 while (fgets(buf, sizeof(buf), fp) != NULL) {
121 int add_param(char *buf, char *key, char *val)
123 int end = sizeof(((struct lustre_disk_data *)0)->ldd_params);
124 int start = strlen(buf);
128 keylen = strlen(key);
129 if (start + 1 + keylen + strlen(val) >= end) {
130 fprintf(stderr, "%s: params are too long-\n%s %s%s\n",
131 progname, buf, key ? key : "", val);
135 sprintf(buf + start, " %s%s", key ? key : "", val);
139 int get_param(char *buf, char *key, char **val)
141 int i, key_len = strlen(key);
144 ptr = strstr(buf, key);
146 *val = strdup(ptr + key_len);
150 for (i = 0; i < strlen(*val); i++)
151 if (((*val)[i] == ' ') || ((*val)[i] == '\0'))
161 int append_param(char *buf, char *key, char *val, char sep)
163 int key_len, i, offset, old_val_len;
164 char *ptr = NULL, str[1024];
167 ptr = strstr(buf, key);
169 /* key doesn't exist yet, so just add it */
171 return add_param(buf, key, val);
173 key_len = strlen(key);
175 /* Copy previous values to str */
176 for (i = 0; i < sizeof(str); ++i) {
177 if ((ptr[i+key_len] == ' ') || (ptr[i+key_len] == '\0'))
179 str[i] = ptr[i+key_len];
181 if (i == sizeof(str))
185 offset = old_val_len+key_len;
187 /* Move rest of buf to overwrite previous key and value */
188 for (i = 0; ptr[i+offset] != '\0'; ++i)
189 ptr[i] = ptr[i+offset];
193 snprintf(str+old_val_len, sizeof(str)-old_val_len, "%c%s", sep, val);
195 return add_param(buf, key, str);
198 char *strscat(char *dst, char *src, int buflen)
201 if (strlen(dst) + strlen(src) >= buflen) {
202 fprintf(stderr, "string buffer overflow (max %d): '%s' + '%s'"
203 "\n", buflen, dst, src);
206 return strcat(dst, src);
209 char *strscpy(char *dst, char *src, int buflen)
212 return strscat(dst, src, buflen);
215 int check_mtab_entry(char *spec1, char *spec2, char *mtpt, char *type)
220 fp = setmntent(MOUNTED, "r");
224 while ((mnt = getmntent(fp)) != NULL) {
225 if ((strcmp(mnt->mnt_fsname, spec1) == 0 ||
226 strcmp(mnt->mnt_fsname, spec2) == 0) &&
227 (mtpt == NULL || strcmp(mnt->mnt_dir, mtpt) == 0) &&
228 (type == NULL || strcmp(mnt->mnt_type, type) == 0)) {
239 #include <linux/magic.h>
241 static int mtab_is_proc(const char *mtab)
244 if (statfs(mtab, &s) < 0)
247 return (s.f_type == PROC_SUPER_MAGIC);
252 # include <libmount/libmount.h>
255 * The libmount is part of util-linux since 2.18.
256 * We use it to update utab to avoid umount would
257 * blocked in some rare case.
259 int update_utab_entry(struct mount_opts *mop)
261 struct libmnt_fs *fs = mnt_new_fs();
262 struct libmnt_update *upd;
265 mnt_fs_set_source(fs, mop->mo_source);
266 mnt_fs_set_target(fs, mop->mo_target);
267 mnt_fs_set_fstype(fs, "lustre");
268 mnt_fs_set_attributes(fs, "lustre");
270 upd = mnt_new_update();
274 rc = mnt_update_set_fs(upd, mop->mo_nomtab ? MS_REMOUNT : 0, NULL, fs);
275 if (rc == 1) /* update is unnecessary */
279 "error: failed to save utab entry: rc = %d\n", rc);
281 rc = mnt_update_table(upd, NULL);
284 mnt_free_update(upd);
290 int update_utab_entry(struct mount_opts *mop)
294 #endif /* HAVE_LIBMOUNT */
296 int update_mtab_entry(char *spec, char *mtpt, char *type, char *opts,
297 int flags, int freq, int pass)
303 /* Don't update mtab if it is linked to any file in /proc direcotry.*/
304 if (mtab_is_proc(MOUNTED))
307 mnt.mnt_fsname = spec;
310 mnt.mnt_opts = opts ? opts : "";
312 mnt.mnt_passno = pass;
314 fp = setmntent(MOUNTED, "a+");
316 fprintf(stderr, "%s: setmntent(%s): %s\n",
317 progname, MOUNTED, strerror (errno));
320 if ((addmntent(fp, &mnt)) == 1) {
321 fprintf(stderr, "%s: addmntent: %s\n",
322 progname, strerror (errno));
331 /* Search for opt in mntlist, returning true if found.
333 static int in_mntlist(char *opt, char *mntlist)
335 char *ml, *mlp, *item, *ctx = NULL;
337 if (!(ml = strdup(mntlist))) {
338 fprintf(stderr, "%s: out of memory\n", progname);
342 while ((item = strtok_r(mlp, ",", &ctx))) {
343 if (!strcmp(opt, item))
348 return (item != NULL);
351 /* Issue a message on stderr for every item in wanted_mountopts that is not
352 * present in mountopts. The justwarn boolean toggles between error and
353 * warning message. Return an error count.
355 int check_mountfsoptions(char *mountopts, char *wanted_mountopts)
357 char *ml, *mlp, *item, *ctx = NULL;
360 if (!(ml = strdup(wanted_mountopts))) {
361 fprintf(stderr, "%s: out of memory\n", progname);
365 while ((item = strtok_r(mlp, ",", &ctx))) {
366 if (!in_mntlist(item, mountopts)) {
367 fprintf(stderr, "%s: Error: mandatory mount option"
368 " '%s' is missing\n", progname, item);
377 /* Trim embedded white space, leading and trailing commas from string s.
379 void trim_mountfsoptions(char *s)
385 memmove(p, p + 1, strlen(p + 1) + 1);
392 memmove(&s[0], &s[1], strlen(&s[1]) + 1);
394 p = s + strlen(s) - 1;
395 while (p >= s && *p == ',')
399 /* Setup a file in the first unused loop_device */
400 int loop_setup(struct mkfs_opts *mop)
406 /* Figure out the loop device names */
407 if (!access("/dev/loop0", F_OK | R_OK) ||
408 !access("/dev/loop-control", F_OK | R_OK)) {
409 strcpy(loop_base, "/dev/loop\0");
410 } else if (!access("/dev/loop/0", F_OK | R_OK)) {
411 strcpy(loop_base, "/dev/loop/\0");
413 fprintf(stderr, "%s: can't access loop devices\n", progname);
417 /* Find unused loop device */
418 for (i = 0; i < MAX_LOOP_DEVICES; i++) {
420 int cmdsz = sizeof(cmd);
422 #ifdef HAVE_LOOP_CTL_GET_FREE
423 ret = open("/dev/loop-control", O_RDWR);
425 fprintf(stderr, "%s: can't access loop control\n", progname);
428 /* find or allocate a free loop device to use */
429 i = ioctl(ret, LOOP_CTL_GET_FREE);
432 fprintf(stderr, "%s: access loop control error\n", progname);
435 sprintf(l_device, "%s%d", loop_base, i);
437 sprintf(l_device, "%s%d", loop_base, i);
438 if (access(l_device, F_OK | R_OK))
441 snprintf(cmd, cmdsz, "losetup %s > /dev/null 2>&1", l_device);
444 /* losetup gets 1 (ret=256) for non-set-up device */
446 /* Set up a loopback device to our file */
447 snprintf(cmd, cmdsz, "losetup %s %s", l_device,
449 ret = run_command(cmd, cmdsz);
451 /* someone else picked up this loop device
455 fprintf(stderr, "%s: error %d on losetup: %s\n",
457 ret >= 0 ? strerror(ret) : "");
460 strscpy(mop->mo_loopdev, l_device,
461 sizeof(mop->mo_loopdev));
466 fprintf(stderr, "%s: out of loop devices!\n", progname);
470 int loop_cleanup(struct mkfs_opts *mop)
475 if ((mop->mo_flags & MO_IS_LOOP) && *mop->mo_loopdev) {
478 sprintf(cmd, "losetup -d %s", mop->mo_loopdev);
479 for (tries = 0; tries < 3; tries++) {
480 ret = run_command(cmd, sizeof(cmd));
488 fprintf(stderr, "cannot cleanup %s: rc = %d\n",
489 mop->mo_loopdev, ret);
493 int loop_format(struct mkfs_opts *mop)
497 if (mop->mo_device_kb == 0) {
499 fprintf(stderr, "loop device requires a --device-size= "
504 fd = creat(mop->mo_device, S_IRUSR|S_IWUSR);
507 fprintf(stderr, "%s: Unable to create backing store: %s\n",
508 progname, strerror(errno));
512 if (ftruncate(fd, mop->mo_device_kb * 1024) != 0) {
515 fprintf(stderr, "%s: Unable to truncate backing store: %s\n",
516 progname, strerror(errno));
524 #define DLSYM(prefix, sym, func) \
527 snprintf(_fname, sizeof(_fname), "%s_%s", prefix, #func); \
528 sym->func = (typeof(sym->func))dlsym(sym->dl_handle, _fname); \
532 * Load plugin for a given mount_type from ${pkglibdir}/mount_osd_FSTYPE.so and
533 * return struct of function pointers (will be freed in unloack_backfs_module).
535 * \param[in] mount_type Mount type to load module for.
536 * \retval Value of backfs_ops struct
537 * \retval NULL if no module exists
539 struct module_backfs_ops *load_backfs_module(enum ldd_mount_type mount_type)
542 char *error, filename[512], fsname[512], *name;
543 struct module_backfs_ops *ops;
545 /* This deals with duplicate ldd_mount_types resolving to same OSD layer
546 * plugin (e.g. ext3/ldiskfs/ldiskfs2 all being ldiskfs) */
547 strncpy(fsname, mt_type(mount_type), sizeof(fsname));
548 name = fsname + sizeof("osd-") - 1;
550 /* change osd- to osd_ */
551 fsname[sizeof("osd-") - 2] = '_';
553 snprintf(filename, sizeof(filename), PLUGIN_DIR"/mount_%s.so", fsname);
555 handle = dlopen(filename, RTLD_LAZY);
557 /* Check for $LUSTRE environment variable from test-framework.
558 * This allows using locally built modules to be used.
560 if (handle == NULL) {
562 dirname = getenv("LUSTRE");
564 snprintf(filename, sizeof(filename),
565 "%s/utils/.libs/mount_%s.so",
567 handle = dlopen(filename, RTLD_LAZY);
571 /* Do not clutter up console with missing types */
575 ops = malloc(sizeof(*ops));
581 ops->dl_handle = handle;
582 dlerror(); /* Clear any existing error */
584 DLSYM(name, ops, init);
585 DLSYM(name, ops, fini);
586 DLSYM(name, ops, read_ldd);
587 DLSYM(name, ops, write_ldd);
588 DLSYM(name, ops, is_lustre);
589 DLSYM(name, ops, make_lustre);
590 DLSYM(name, ops, prepare_lustre);
591 DLSYM(name, ops, tune_lustre);
592 DLSYM(name, ops, label_lustre);
593 DLSYM(name, ops, enable_quota);
598 fprintf(stderr, "%s\n", error);
604 /* optional methods */
605 DLSYM(name, ops, fix_mountopts);
611 * Unload plugin and free backfs_ops structure. Must be called the same number
612 * of times as load_backfs_module is.
614 void unload_backfs_module(struct module_backfs_ops *ops)
619 dlclose(ops->dl_handle);
623 /* Return true if backfs_ops has operations for the given mount_type. */
624 int backfs_mount_type_okay(enum ldd_mount_type mount_type)
626 if (unlikely(mount_type >= LDD_MT_LAST || mount_type < 0)) {
628 fprintf(stderr, "fs type out of range %d\n", mount_type);
631 if (backfs_ops[mount_type] == NULL) {
633 fprintf(stderr, "unhandled/unloaded fs type %d '%s'\n",
634 mount_type, mt_str(mount_type));
640 /* Write the server config files */
641 int osd_write_ldd(struct mkfs_opts *mop)
643 struct lustre_disk_data *ldd = &mop->mo_ldd;
646 if (backfs_mount_type_okay(ldd->ldd_mount_type))
647 ret = backfs_ops[ldd->ldd_mount_type]->write_ldd(mop);
654 /* Read the server config files */
655 int osd_read_ldd(char *dev, struct lustre_disk_data *ldd)
659 if (backfs_mount_type_okay(ldd->ldd_mount_type))
660 ret = backfs_ops[ldd->ldd_mount_type]->read_ldd(dev, ldd);
667 /* Was this device formatted for Lustre */
668 int osd_is_lustre(char *dev, unsigned *mount_type)
672 vprint("checking for existing Lustre data: ");
674 for (i = 0; i < LDD_MT_LAST; ++i) {
675 if (backfs_ops[i] != NULL &&
676 backfs_ops[i]->is_lustre(dev, mount_type)) {
682 vprint("not found\n");
686 /* Build fs according to type */
687 int osd_make_lustre(struct mkfs_opts *mop)
689 struct lustre_disk_data *ldd = &mop->mo_ldd;
692 if (backfs_mount_type_okay(ldd->ldd_mount_type))
693 ret = backfs_ops[ldd->ldd_mount_type]->make_lustre(mop);
700 int osd_prepare_lustre(struct mkfs_opts *mop,
701 char *wanted_mountopts, size_t len)
703 struct lustre_disk_data *ldd = &mop->mo_ldd;
706 if (backfs_mount_type_okay(ldd->ldd_mount_type))
707 ret = backfs_ops[ldd->ldd_mount_type]->prepare_lustre(mop,
708 wanted_mountopts, len);
715 int osd_fix_mountopts(struct mkfs_opts *mop, char *mountopts, size_t len)
717 struct lustre_disk_data *ldd = &mop->mo_ldd;
719 if (!backfs_mount_type_okay(ldd->ldd_mount_type))
722 if (backfs_ops[ldd->ldd_mount_type]->fix_mountopts == NULL)
725 return backfs_ops[ldd->ldd_mount_type]->fix_mountopts(mop, mountopts,
729 int osd_tune_lustre(char *dev, struct mount_opts *mop)
731 struct lustre_disk_data *ldd = &mop->mo_ldd;
734 if (backfs_mount_type_okay(ldd->ldd_mount_type))
735 ret = backfs_ops[ldd->ldd_mount_type]->tune_lustre(dev, mop);
742 int osd_label_lustre(struct mount_opts *mop)
744 struct lustre_disk_data *ldd = &mop->mo_ldd;
747 if (backfs_mount_type_okay(ldd->ldd_mount_type))
748 ret = backfs_ops[ldd->ldd_mount_type]->label_lustre(mop);
755 /* Enable quota accounting */
756 int osd_enable_quota(struct mkfs_opts *mop)
758 struct lustre_disk_data *ldd = &mop->mo_ldd;
761 if (backfs_mount_type_okay(ldd->ldd_mount_type))
762 ret = backfs_ops[ldd->ldd_mount_type]->enable_quota(mop);
771 int i, rc, ret = EINVAL;
773 for (i = 0; i < LDD_MT_LAST; ++i) {
775 backfs_ops[i] = load_backfs_module(i);
776 if (backfs_ops[i] != NULL)
777 rc = backfs_ops[i]->init();
779 backfs_ops[i]->fini();
780 unload_backfs_module(backfs_ops[i]);
781 backfs_ops[i] = NULL;
793 for (i = 0; i < LDD_MT_LAST; ++i) {
794 if (backfs_ops[i] != NULL) {
795 backfs_ops[i]->fini();
796 unload_backfs_module(backfs_ops[i]);
797 backfs_ops[i] = NULL;
802 __u64 get_device_size(char* device)
807 fd = open(device, O_RDONLY);
809 fprintf(stderr, "%s: cannot open %s: %s\n",
810 progname, device, strerror(errno));
815 /* size in bytes. bz5831 */
816 ret = ioctl(fd, BLKGETSIZE64, (void*)&size);
821 ret = ioctl(fd, BLKGETSIZE, (void*)&lsize);
822 size = (__u64)lsize * 512;
827 fprintf(stderr, "%s: size ioctl failed: %s\n",
828 progname, strerror(errno));
832 vprint("device size = %juMB\n", (uintmax_t)(size >> 20));
833 /* return value in KB */
837 int file_create(char *path, __u64 size)
844 * Since "size" is in KB, the file offset it represents could overflow
847 size_max = (off_t)1 << (_FILE_OFFSET_BITS - 1 - 10);
848 if (size >= size_max) {
849 fprintf(stderr, "%s: %ju KB: Backing store size must be "
850 "smaller than %ju KB\n", progname, (uintmax_t) size,
851 (uintmax_t)size_max);
855 ret = access(path, F_OK);
862 fd = creat(path, S_IRUSR|S_IWUSR);
865 fprintf(stderr, "%s: Unable to create backing store: %s\n",
866 progname, strerror(errno));
870 ret = ftruncate(fd, size * 1024);
874 fprintf(stderr, "%s: Unable to truncate backing store: %s\n",
875 progname, strerror(errno));
883 int load_shared_keys(struct mount_opts *mop)
886 struct dirent *dentry;
888 char fullpath[PATH_MAX];
889 char *path = mop->mo_skpath;
893 if (IS_SERVER(&mop->mo_ldd)) {
894 if (IS_MGS(&mop->mo_ldd))
896 if (IS_MDT(&mop->mo_ldd) || IS_OST(&mop->mo_ldd))
897 type |= SK_TYPE_SERVER | SK_TYPE_CLIENT;
899 type |= SK_TYPE_CLIENT;
903 sk_init_logging(NULL, 1, 1);
905 rc = stat(path, &sbuf);
907 fprintf(stderr, "stat() failed for key %s: %s\n", path,
912 /* Load individual keys or a directory of them */
913 if (S_ISREG(sbuf.st_mode)) {
914 return sk_load_keyfile(path, type);
915 } else if (!S_ISDIR(sbuf.st_mode)) {
916 fprintf(stderr, "Invalid shared key path: %s\n", path);
922 fprintf(stderr, "Unable to open shared key directory: %s\n",
927 /* Loop through the files in the directory attempting to load them.
928 * Any issue with loading the keyfile is treated as an error although
929 * the loop continues until all files have been attempted. This will
930 * allow all errors be reported at once rather then requiring
931 * incremental corrections to fix each one and try again. */
932 while ((dentry = readdir(dir)) != NULL) {
933 if (strcmp(".", dentry->d_name) == 0 ||
934 strcmp("..", dentry->d_name) == 0)
937 rc = snprintf(fullpath, PATH_MAX, "%s/%s", path,
939 if (rc >= PATH_MAX) {
940 fprintf(stderr, "Path too long for %s/%s\n",
941 path, dentry->d_name);
946 rc = stat(fullpath, &sbuf);
948 fprintf(stderr, "Unable to stat %s: %s\n", fullpath,
954 if (!S_ISREG(sbuf.st_mode))
957 rc = sk_load_keyfile(fullpath, type);
959 fprintf(stderr, "Failed to load key %s\n", fullpath);