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 */
41 #include "mount_utils.h"
48 #include <lustre_disk.h>
49 #include <lustre_ver.h>
50 #include <sys/mount.h>
52 #include <sys/utsname.h>
53 #include <linux/loop.h>
56 extern char *progname;
59 #define vprint(fmt, arg...) if (verbose > 0) printf(fmt, ##arg)
60 #define verrprint(fmt, arg...) if (verbose >= 0) fprintf(stderr, fmt, ##arg)
62 static struct module_backfs_ops *backfs_ops[LDD_MT_LAST];
67 fprintf(stderr, "\n%s FATAL: ", progname);
70 int run_command(char *cmd, int cmdsz)
72 char log[] = "/tmp/run_command_logXXXXXX";
75 if ((cmdsz - strlen(cmd)) < 6) {
77 fprintf(stderr, "Command buffer overflow: %.*s...\n",
83 printf("cmd: %s\n", cmd);
85 if ((fd = mkstemp(log)) >= 0) {
93 /* Can't use popen because we need the rv of the command */
95 if (rc && (fd >= 0)) {
100 while (fgets(buf, sizeof(buf), fp) != NULL) {
111 int add_param(char *buf, char *key, char *val)
113 int end = sizeof(((struct lustre_disk_data *)0)->ldd_params);
114 int start = strlen(buf);
118 keylen = strlen(key);
119 if (start + 1 + keylen + strlen(val) >= end) {
120 fprintf(stderr, "%s: params are too long-\n%s %s%s\n",
121 progname, buf, key ? key : "", val);
125 sprintf(buf + start, " %s%s", key ? key : "", val);
129 int get_param(char *buf, char *key, char **val)
131 int i, key_len = strlen(key);
134 ptr = strstr(buf, key);
136 *val = strdup(ptr + key_len);
140 for (i = 0; i < strlen(*val); i++)
141 if (((*val)[i] == ' ') || ((*val)[i] == '\0'))
151 int append_param(char *buf, char *key, char *val, char sep)
153 int key_len, i, offset, old_val_len;
154 char *ptr = NULL, str[1024];
157 ptr = strstr(buf, key);
159 /* key doesn't exist yet, so just add it */
161 return add_param(buf, key, val);
163 key_len = strlen(key);
165 /* Copy previous values to str */
166 for (i = 0; i < sizeof(str); ++i) {
167 if ((ptr[i+key_len] == ' ') || (ptr[i+key_len] == '\0'))
169 str[i] = ptr[i+key_len];
171 if (i == sizeof(str))
175 offset = old_val_len+key_len;
177 /* Move rest of buf to overwrite previous key and value */
178 for (i = 0; ptr[i+offset] != '\0'; ++i)
179 ptr[i] = ptr[i+offset];
183 snprintf(str+old_val_len, sizeof(str)-old_val_len, "%c%s", sep, val);
185 return add_param(buf, key, str);
188 char *strscat(char *dst, char *src, int buflen)
191 if (strlen(dst) + strlen(src) >= buflen) {
192 fprintf(stderr, "string buffer overflow (max %d): '%s' + '%s'"
193 "\n", buflen, dst, src);
196 return strcat(dst, src);
199 char *strscpy(char *dst, char *src, int buflen)
202 return strscat(dst, src, buflen);
205 int check_mtab_entry(char *spec1, char *spec2, char *mtpt, char *type)
210 fp = setmntent(MOUNTED, "r");
214 while ((mnt = getmntent(fp)) != NULL) {
215 if ((strcmp(mnt->mnt_fsname, spec1) == 0 ||
216 strcmp(mnt->mnt_fsname, spec2) == 0) &&
217 (mtpt == NULL || strcmp(mnt->mnt_dir, mtpt) == 0) &&
218 (type == NULL || strcmp(mnt->mnt_type, type) == 0)) {
228 #define PROC_DIR "/proc/"
229 static int mtab_is_proc(const char *mtab)
233 if (readlink(mtab, path, sizeof(path)) < 0)
236 if (strncmp(path, PROC_DIR, strlen(PROC_DIR)))
244 # include <libmount/libmount.h>
247 * The libmount is part of util-linux since 2.18.
248 * We use it to update utab to avoid umount would
249 * blocked in some rare case.
251 int update_utab_entry(struct mount_opts *mop)
253 struct libmnt_fs *fs = mnt_new_fs();
254 struct libmnt_update *upd;
257 mnt_fs_set_source(fs, mop->mo_source);
258 mnt_fs_set_target(fs, mop->mo_target);
259 mnt_fs_set_fstype(fs, "lustre");
260 mnt_fs_set_attributes(fs, "lustre");
262 upd = mnt_new_update();
266 rc = mnt_update_set_fs(upd, mop->mo_nomtab ? MS_REMOUNT : 0, NULL, fs);
267 if (rc == 1) /* update is unnecessary */
271 "error: failed to save utab entry: rc = %d\n", rc);
273 rc = mnt_update_table(upd, NULL);
276 mnt_free_update(upd);
282 int update_utab_entry(struct mount_opts *mop)
286 #endif /* HAVE_LIBMOUNT */
288 int update_mtab_entry(char *spec, char *mtpt, char *type, char *opts,
289 int flags, int freq, int pass)
295 /* Don't update mtab if it is linked to any file in /proc direcotry.*/
296 if (mtab_is_proc(MOUNTED))
299 mnt.mnt_fsname = spec;
302 mnt.mnt_opts = opts ? opts : "";
304 mnt.mnt_passno = pass;
306 fp = setmntent(MOUNTED, "a+");
308 fprintf(stderr, "%s: setmntent(%s): %s:",
309 progname, MOUNTED, strerror (errno));
312 if ((addmntent(fp, &mnt)) == 1) {
313 fprintf(stderr, "%s: addmntent: %s:",
314 progname, strerror (errno));
323 /* Search for opt in mntlist, returning true if found.
325 static int in_mntlist(char *opt, char *mntlist)
327 char *ml, *mlp, *item, *ctx = NULL;
329 if (!(ml = strdup(mntlist))) {
330 fprintf(stderr, "%s: out of memory\n", progname);
334 while ((item = strtok_r(mlp, ",", &ctx))) {
335 if (!strcmp(opt, item))
340 return (item != NULL);
343 /* Issue a message on stderr for every item in wanted_mountopts that is not
344 * present in mountopts. The justwarn boolean toggles between error and
345 * warning message. Return an error count.
347 int check_mountfsoptions(char *mountopts, char *wanted_mountopts)
349 char *ml, *mlp, *item, *ctx = NULL;
352 if (!(ml = strdup(wanted_mountopts))) {
353 fprintf(stderr, "%s: out of memory\n", progname);
357 while ((item = strtok_r(mlp, ",", &ctx))) {
358 if (!in_mntlist(item, mountopts)) {
359 fprintf(stderr, "%s: Error: mandatory mount option"
360 " '%s' is missing\n", progname, item);
369 /* Trim embedded white space, leading and trailing commas from string s.
371 void trim_mountfsoptions(char *s)
377 memmove(p, p + 1, strlen(p + 1) + 1);
384 memmove(&s[0], &s[1], strlen(&s[1]) + 1);
386 p = s + strlen(s) - 1;
387 while (p >= s && *p == ',')
391 /* Setup a file in the first unused loop_device */
392 int loop_setup(struct mkfs_opts *mop)
398 /* Figure out the loop device names */
399 if (!access("/dev/loop0", F_OK | R_OK) ||
400 !access("/dev/loop-control", F_OK | R_OK)) {
401 strcpy(loop_base, "/dev/loop\0");
402 } else if (!access("/dev/loop/0", F_OK | R_OK)) {
403 strcpy(loop_base, "/dev/loop/\0");
405 fprintf(stderr, "%s: can't access loop devices\n", progname);
409 /* Find unused loop device */
410 for (i = 0; i < MAX_LOOP_DEVICES; i++) {
412 int cmdsz = sizeof(cmd);
414 #ifdef HAVE_LOOP_CTL_GET_FREE
415 ret = open("/dev/loop-control", O_RDWR);
417 fprintf(stderr, "%s: can't access loop control\n", progname);
420 /* find or allocate a free loop device to use */
421 i = ioctl(ret, LOOP_CTL_GET_FREE);
423 fprintf(stderr, "%s: access loop control error\n", progname);
426 sprintf(l_device, "%s%d", loop_base, i);
428 sprintf(l_device, "%s%d", loop_base, i);
429 if (access(l_device, F_OK | R_OK))
432 snprintf(cmd, cmdsz, "losetup %s > /dev/null 2>&1", l_device);
435 /* losetup gets 1 (ret=256) for non-set-up device */
437 /* Set up a loopback device to our file */
438 snprintf(cmd, cmdsz, "losetup %s %s", l_device,
440 ret = run_command(cmd, cmdsz);
442 /* someone else picked up this loop device
446 fprintf(stderr, "%s: error %d on losetup: %s\n",
448 ret >= 0 ? strerror(ret) : "");
451 strscpy(mop->mo_loopdev, l_device,
452 sizeof(mop->mo_loopdev));
457 fprintf(stderr, "%s: out of loop devices!\n", progname);
461 int loop_cleanup(struct mkfs_opts *mop)
466 if ((mop->mo_flags & MO_IS_LOOP) && *mop->mo_loopdev) {
469 sprintf(cmd, "losetup -d %s", mop->mo_loopdev);
470 for (tries = 0; tries < 3; tries++) {
471 ret = run_command(cmd, sizeof(cmd));
479 fprintf(stderr, "cannot cleanup %s: rc = %d\n",
480 mop->mo_loopdev, ret);
484 int loop_format(struct mkfs_opts *mop)
488 if (mop->mo_device_kb == 0) {
490 fprintf(stderr, "loop device requires a --device-size= "
495 fd = creat(mop->mo_device, S_IRUSR|S_IWUSR);
498 fprintf(stderr, "%s: Unable to create backing store: %s\n",
499 progname, strerror(errno));
503 if (ftruncate(fd, mop->mo_device_kb * 1024) != 0) {
506 fprintf(stderr, "%s: Unable to truncate backing store: %s\n",
507 progname, strerror(errno));
515 #define DLSYM(prefix, sym, func) \
518 snprintf(_fname, sizeof(_fname), "%s_%s", prefix, #func); \
519 sym->func = (typeof(sym->func))dlsym(sym->dl_handle, _fname); \
523 * Load plugin for a given mount_type from ${pkglibdir}/mount_osd_FSTYPE.so and
524 * return struct of function pointers (will be freed in unloack_backfs_module).
526 * \param[in] mount_type Mount type to load module for.
527 * \retval Value of backfs_ops struct
528 * \retval NULL if no module exists
530 struct module_backfs_ops *load_backfs_module(enum ldd_mount_type mount_type)
533 char *error, filename[512], fsname[512], *name;
534 struct module_backfs_ops *ops;
536 /* This deals with duplicate ldd_mount_types resolving to same OSD layer
537 * plugin (e.g. ext3/ldiskfs/ldiskfs2 all being ldiskfs) */
538 strncpy(fsname, mt_type(mount_type), sizeof(fsname));
539 name = fsname + sizeof("osd-") - 1;
541 /* change osd- to osd_ */
542 fsname[sizeof("osd-") - 2] = '_';
544 snprintf(filename, sizeof(filename), PLUGIN_DIR"/mount_%s.so", fsname);
546 handle = dlopen(filename, RTLD_LAZY);
548 /* Check for $LUSTRE environment variable from test-framework.
549 * This allows using locally built modules to be used.
551 if (handle == NULL) {
553 dirname = getenv("LUSTRE");
555 snprintf(filename, sizeof(filename),
556 "%s/utils/.libs/mount_%s.so",
558 handle = dlopen(filename, RTLD_LAZY);
562 /* Do not clutter up console with missing types */
566 ops = malloc(sizeof(*ops));
572 ops->dl_handle = handle;
573 dlerror(); /* Clear any existing error */
575 DLSYM(name, ops, init);
576 DLSYM(name, ops, fini);
577 DLSYM(name, ops, read_ldd);
578 DLSYM(name, ops, write_ldd);
579 DLSYM(name, ops, is_lustre);
580 DLSYM(name, ops, make_lustre);
581 DLSYM(name, ops, prepare_lustre);
582 DLSYM(name, ops, tune_lustre);
583 DLSYM(name, ops, label_lustre);
584 DLSYM(name, ops, enable_quota);
589 fprintf(stderr, "%s\n", error);
595 /* optional methods */
596 DLSYM(name, ops, fix_mountopts);
602 * Unload plugin and free backfs_ops structure. Must be called the same number
603 * of times as load_backfs_module is.
605 void unload_backfs_module(struct module_backfs_ops *ops)
610 dlclose(ops->dl_handle);
614 /* Return true if backfs_ops has operations for the given mount_type. */
615 int backfs_mount_type_okay(enum ldd_mount_type mount_type)
617 if (unlikely(mount_type >= LDD_MT_LAST || mount_type < 0)) {
619 fprintf(stderr, "fs type out of range %d\n", mount_type);
622 if (backfs_ops[mount_type] == NULL) {
624 fprintf(stderr, "unhandled/unloaded fs type %d '%s'\n",
625 mount_type, mt_str(mount_type));
631 /* Write the server config files */
632 int osd_write_ldd(struct mkfs_opts *mop)
634 struct lustre_disk_data *ldd = &mop->mo_ldd;
637 if (backfs_mount_type_okay(ldd->ldd_mount_type))
638 ret = backfs_ops[ldd->ldd_mount_type]->write_ldd(mop);
646 /* Read the server config files */
647 int osd_read_ldd(char *dev, struct lustre_disk_data *ldd)
651 if (backfs_mount_type_okay(ldd->ldd_mount_type))
652 ret = backfs_ops[ldd->ldd_mount_type]->read_ldd(dev, ldd);
660 /* Was this device formatted for Lustre */
661 int osd_is_lustre(char *dev, unsigned *mount_type)
665 vprint("checking for existing Lustre data: ");
667 for (i = 0; i < LDD_MT_LAST; ++i) {
668 if (backfs_ops[i] != NULL &&
669 backfs_ops[i]->is_lustre(dev, mount_type)) {
675 vprint("not found\n");
679 /* Build fs according to type */
680 int osd_make_lustre(struct mkfs_opts *mop)
682 struct lustre_disk_data *ldd = &mop->mo_ldd;
685 if (backfs_mount_type_okay(ldd->ldd_mount_type))
686 ret = backfs_ops[ldd->ldd_mount_type]->make_lustre(mop);
694 int osd_prepare_lustre(struct mkfs_opts *mop,
695 char *wanted_mountopts, size_t len)
697 struct lustre_disk_data *ldd = &mop->mo_ldd;
700 if (backfs_mount_type_okay(ldd->ldd_mount_type))
701 ret = backfs_ops[ldd->ldd_mount_type]->prepare_lustre(mop,
702 wanted_mountopts, len);
710 int osd_fix_mountopts(struct mkfs_opts *mop, char *mountopts, size_t len)
712 struct lustre_disk_data *ldd = &mop->mo_ldd;
714 if (!backfs_mount_type_okay(ldd->ldd_mount_type))
717 if (backfs_ops[ldd->ldd_mount_type]->fix_mountopts == NULL)
720 return backfs_ops[ldd->ldd_mount_type]->fix_mountopts(mop, mountopts,
724 int osd_tune_lustre(char *dev, struct mount_opts *mop)
726 struct lustre_disk_data *ldd = &mop->mo_ldd;
729 if (backfs_mount_type_okay(ldd->ldd_mount_type))
730 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);
752 /* Enable quota accounting */
753 int osd_enable_quota(struct mkfs_opts *mop)
755 struct lustre_disk_data *ldd = &mop->mo_ldd;
758 if (backfs_mount_type_okay(ldd->ldd_mount_type))
759 ret = backfs_ops[ldd->ldd_mount_type]->enable_quota(mop);
769 int i, rc, ret = EINVAL;
771 for (i = 0; i < LDD_MT_LAST; ++i) {
773 backfs_ops[i] = load_backfs_module(i);
774 if (backfs_ops[i] != NULL)
775 rc = backfs_ops[i]->init();
777 backfs_ops[i]->fini();
778 unload_backfs_module(backfs_ops[i]);
779 backfs_ops[i] = NULL;
791 for (i = 0; i < LDD_MT_LAST; ++i) {
792 if (backfs_ops[i] != NULL) {
793 backfs_ops[i]->fini();
794 unload_backfs_module(backfs_ops[i]);
795 backfs_ops[i] = NULL;
800 __u64 get_device_size(char* device)
805 fd = open(device, O_RDONLY);
807 fprintf(stderr, "%s: cannot open %s: %s\n",
808 progname, device, strerror(errno));
813 /* size in bytes. bz5831 */
814 ret = ioctl(fd, BLKGETSIZE64, (void*)&size);
819 ret = ioctl(fd, BLKGETSIZE, (void*)&lsize);
820 size = (__u64)lsize * 512;
825 fprintf(stderr, "%s: size ioctl failed: %s\n",
826 progname, strerror(errno));
830 vprint("device size = %juMB\n", (uintmax_t)(size >> 20));
831 /* return value in KB */
835 int file_create(char *path, __u64 size)
842 * Since "size" is in KB, the file offset it represents could overflow
845 size_max = (off_t)1 << (_FILE_OFFSET_BITS - 1 - 10);
846 if (size >= size_max) {
847 fprintf(stderr, "%s: %ju KB: Backing store size must be "
848 "smaller than %ju KB\n", progname, (uintmax_t) size,
849 (uintmax_t)size_max);
853 ret = access(path, F_OK);
860 fd = creat(path, S_IRUSR|S_IWUSR);
863 fprintf(stderr, "%s: Unable to create backing store: %s\n",
864 progname, strerror(errno));
868 ret = ftruncate(fd, size * 1024);
872 fprintf(stderr, "%s: Unable to truncate backing store: %s\n",
873 progname, strerror(errno));