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"
47 #include <lustre_disk.h>
48 #include <lustre_ver.h>
49 #include <sys/mount.h>
51 #include <sys/utsname.h>
52 #include <linux/loop.h>
55 extern char *progname;
58 #define vprint(fmt, arg...) if (verbose > 0) printf(fmt, ##arg)
59 #define verrprint(fmt, arg...) if (verbose >= 0) fprintf(stderr, fmt, ##arg)
61 static struct module_backfs_ops *backfs_ops[LDD_MT_LAST];
66 fprintf(stderr, "\n%s FATAL: ", progname);
69 int run_command(char *cmd, int cmdsz)
71 char log[] = "/tmp/run_command_logXXXXXX";
74 if ((cmdsz - strlen(cmd)) < 6) {
76 fprintf(stderr, "Command buffer overflow: %.*s...\n",
82 printf("cmd: %s\n", cmd);
84 if ((fd = mkstemp(log)) >= 0) {
92 /* Can't use popen because we need the rv of the command */
94 if (rc && (fd >= 0)) {
99 while (fgets(buf, sizeof(buf), fp) != NULL) {
110 int add_param(char *buf, char *key, char *val)
112 int end = sizeof(((struct lustre_disk_data *)0)->ldd_params);
113 int start = strlen(buf);
117 keylen = strlen(key);
118 if (start + 1 + keylen + strlen(val) >= end) {
119 fprintf(stderr, "%s: params are too long-\n%s %s%s\n",
120 progname, buf, key ? key : "", val);
124 sprintf(buf + start, " %s%s", key ? key : "", val);
128 int get_param(char *buf, char *key, char **val)
130 int i, key_len = strlen(key);
133 ptr = strstr(buf, key);
135 *val = strdup(ptr + key_len);
139 for (i = 0; i < strlen(*val); i++)
140 if (((*val)[i] == ' ') || ((*val)[i] == '\0'))
150 int append_param(char *buf, char *key, char *val, char sep)
152 int key_len, i, offset, old_val_len;
153 char *ptr = NULL, str[1024];
156 ptr = strstr(buf, key);
158 /* key doesn't exist yet, so just add it */
160 return add_param(buf, key, val);
162 key_len = strlen(key);
164 /* Copy previous values to str */
165 for (i = 0; i < sizeof(str); ++i) {
166 if ((ptr[i+key_len] == ' ') || (ptr[i+key_len] == '\0'))
168 str[i] = ptr[i+key_len];
170 if (i == sizeof(str))
174 offset = old_val_len+key_len;
176 /* Move rest of buf to overwrite previous key and value */
177 for (i = 0; ptr[i+offset] != '\0'; ++i)
178 ptr[i] = ptr[i+offset];
182 snprintf(str+old_val_len, sizeof(str)-old_val_len, "%c%s", sep, val);
184 return add_param(buf, key, str);
187 char *strscat(char *dst, char *src, int buflen)
190 if (strlen(dst) + strlen(src) >= buflen) {
191 fprintf(stderr, "string buffer overflow (max %d): '%s' + '%s'"
192 "\n", buflen, dst, src);
195 return strcat(dst, src);
198 char *strscpy(char *dst, char *src, int buflen)
201 return strscat(dst, src, buflen);
204 int check_mtab_entry(char *spec1, char *spec2, char *mtpt, char *type)
209 fp = setmntent(MOUNTED, "r");
213 while ((mnt = getmntent(fp)) != NULL) {
214 if ((strcmp(mnt->mnt_fsname, spec1) == 0 ||
215 strcmp(mnt->mnt_fsname, spec2) == 0) &&
216 (mtpt == NULL || strcmp(mnt->mnt_dir, mtpt) == 0) &&
217 (type == NULL || strcmp(mnt->mnt_type, type) == 0)) {
227 #define PROC_DIR "/proc/"
228 static int mtab_is_proc(const char *mtab)
232 if (readlink(mtab, path, sizeof(path)) < 0)
235 if (strncmp(path, PROC_DIR, strlen(PROC_DIR)))
241 int update_mtab_entry(char *spec, char *mtpt, char *type, char *opts,
242 int flags, int freq, int pass)
248 /* Don't update mtab if it is linked to any file in /proc direcotry.*/
249 if (mtab_is_proc(MOUNTED))
252 mnt.mnt_fsname = spec;
255 mnt.mnt_opts = opts ? opts : "";
257 mnt.mnt_passno = pass;
259 fp = setmntent(MOUNTED, "a+");
261 fprintf(stderr, "%s: setmntent(%s): %s:",
262 progname, MOUNTED, strerror (errno));
265 if ((addmntent(fp, &mnt)) == 1) {
266 fprintf(stderr, "%s: addmntent: %s:",
267 progname, strerror (errno));
276 /* Search for opt in mntlist, returning true if found.
278 static int in_mntlist(char *opt, char *mntlist)
280 char *ml, *mlp, *item, *ctx = NULL;
282 if (!(ml = strdup(mntlist))) {
283 fprintf(stderr, "%s: out of memory\n", progname);
287 while ((item = strtok_r(mlp, ",", &ctx))) {
288 if (!strcmp(opt, item))
293 return (item != NULL);
296 /* Issue a message on stderr for every item in wanted_mountopts that is not
297 * present in mountopts. The justwarn boolean toggles between error and
298 * warning message. Return an error count.
300 int check_mountfsoptions(char *mountopts, char *wanted_mountopts,
303 char *ml, *mlp, *item, *ctx = NULL;
306 if (!(ml = strdup(wanted_mountopts))) {
307 fprintf(stderr, "%s: out of memory\n", progname);
311 while ((item = strtok_r(mlp, ",", &ctx))) {
312 if (!in_mntlist(item, mountopts)) {
313 fprintf(stderr, "%s: %s mount option `%s' is missing\n",
314 progname, justwarn ? "Warning: default"
315 : "Error: mandatory", item);
324 /* Trim embedded white space, leading and trailing commas from string s.
326 void trim_mountfsoptions(char *s)
332 memmove(p, p + 1, strlen(p + 1) + 1);
339 memmove(&s[0], &s[1], strlen(&s[1]) + 1);
341 p = s + strlen(s) - 1;
342 while (p >= s && *p == ',')
346 /* Setup a file in the first unused loop_device */
347 int loop_setup(struct mkfs_opts *mop)
353 /* Figure out the loop device names */
354 if (!access("/dev/loop0", F_OK | R_OK) ||
355 !access("/dev/loop-control", F_OK | R_OK)) {
356 strcpy(loop_base, "/dev/loop\0");
357 } else if (!access("/dev/loop/0", F_OK | R_OK)) {
358 strcpy(loop_base, "/dev/loop/\0");
360 fprintf(stderr, "%s: can't access loop devices\n", progname);
364 /* Find unused loop device */
365 for (i = 0; i < MAX_LOOP_DEVICES; i++) {
367 int cmdsz = sizeof(cmd);
369 #ifdef LOOP_CTL_GET_FREE
370 ret = open("/dev/loop-control", O_RDWR);
372 fprintf(stderr, "%s: can't access loop control\n", progname);
375 /* find or allocate a free loop device to use */
376 i = ioctl(ret, LOOP_CTL_GET_FREE);
378 fprintf(stderr, "%s: access loop control error\n", progname);
381 sprintf(l_device, "%s%d", loop_base, i);
383 sprintf(l_device, "%s%d", loop_base, i);
384 if (access(l_device, F_OK | R_OK))
387 snprintf(cmd, cmdsz, "losetup %s > /dev/null 2>&1", l_device);
390 /* losetup gets 1 (ret=256) for non-set-up device */
392 /* Set up a loopback device to our file */
393 snprintf(cmd, cmdsz, "losetup %s %s", l_device,
395 ret = run_command(cmd, cmdsz);
397 /* someone else picked up this loop device
401 fprintf(stderr, "%s: error %d on losetup: %s\n",
403 ret >= 0 ? strerror(ret) : "");
406 strscpy(mop->mo_loopdev, l_device,
407 sizeof(mop->mo_loopdev));
412 fprintf(stderr, "%s: out of loop devices!\n", progname);
416 int loop_cleanup(struct mkfs_opts *mop)
421 if ((mop->mo_flags & MO_IS_LOOP) && *mop->mo_loopdev) {
424 sprintf(cmd, "losetup -d %s", mop->mo_loopdev);
425 for (tries = 0; tries < 3; tries++) {
426 ret = run_command(cmd, sizeof(cmd));
434 fprintf(stderr, "cannot cleanup %s: rc = %d\n",
435 mop->mo_loopdev, ret);
439 int loop_format(struct mkfs_opts *mop)
443 if (mop->mo_device_kb == 0) {
445 fprintf(stderr, "loop device requires a --device-size= "
450 fd = creat(mop->mo_device, S_IRUSR|S_IWUSR);
453 fprintf(stderr, "%s: Unable to create backing store: %s\n",
454 progname, strerror(errno));
458 if (ftruncate(fd, mop->mo_device_kb * 1024) != 0) {
461 fprintf(stderr, "%s: Unable to truncate backing store: %s\n",
462 progname, strerror(errno));
470 #define DLSYM(prefix, sym, func) \
473 snprintf(_fname, sizeof(_fname), "%s_%s", prefix, #func); \
474 sym->func = (typeof(sym->func))dlsym(sym->dl_handle, _fname); \
478 * Load plugin for a given mount_type from ${pkglibdir}/mount_osd_FSTYPE.so and
479 * return struct of function pointers (will be freed in unloack_backfs_module).
481 * \param[in] mount_type Mount type to load module for.
482 * \retval Value of backfs_ops struct
483 * \retval NULL if no module exists
485 struct module_backfs_ops *load_backfs_module(enum ldd_mount_type mount_type)
488 char *error, filename[512], fsname[512], *name;
489 struct module_backfs_ops *ops;
491 /* This deals with duplicate ldd_mount_types resolving to same OSD layer
492 * plugin (e.g. ext3/ldiskfs/ldiskfs2 all being ldiskfs) */
493 strlcpy(fsname, mt_type(mount_type), sizeof(fsname));
494 name = fsname + sizeof("osd-") - 1;
496 /* change osd- to osd_ */
497 fsname[sizeof("osd-") - 2] = '_';
499 snprintf(filename, sizeof(filename), PLUGIN_DIR"/mount_%s.so", fsname);
501 handle = dlopen(filename, RTLD_LAZY);
503 /* Check for $LUSTRE environment variable from test-framework.
504 * This allows using locally built modules to be used.
506 if (handle == NULL) {
508 dirname = getenv("LUSTRE");
510 snprintf(filename, sizeof(filename),
511 "%s/utils/.libs/mount_%s.so",
513 handle = dlopen(filename, RTLD_LAZY);
517 /* Do not clutter up console with missing types */
521 ops = malloc(sizeof(*ops));
527 ops->dl_handle = handle;
528 dlerror(); /* Clear any existing error */
530 DLSYM(name, ops, init);
531 DLSYM(name, ops, fini);
532 DLSYM(name, ops, read_ldd);
533 DLSYM(name, ops, write_ldd);
534 DLSYM(name, ops, is_lustre);
535 DLSYM(name, ops, make_lustre);
536 DLSYM(name, ops, prepare_lustre);
537 DLSYM(name, ops, tune_lustre);
538 DLSYM(name, ops, label_lustre);
539 DLSYM(name, ops, enable_quota);
544 fprintf(stderr, "%s\n", error);
553 * Unload plugin and free backfs_ops structure. Must be called the same number
554 * of times as load_backfs_module is.
556 void unload_backfs_module(struct module_backfs_ops *ops)
561 dlclose(ops->dl_handle);
565 /* Return true if backfs_ops has operations for the given mount_type. */
566 int backfs_mount_type_okay(enum ldd_mount_type mount_type)
568 if (unlikely(mount_type >= LDD_MT_LAST || mount_type < 0)) {
570 fprintf(stderr, "fs type out of range %d\n", mount_type);
573 if (backfs_ops[mount_type] == NULL) {
575 fprintf(stderr, "unhandled fs type %d '%s'\n",
576 mount_type, mt_str(mount_type));
582 /* Write the server config files */
583 int osd_write_ldd(struct mkfs_opts *mop)
585 struct lustre_disk_data *ldd = &mop->mo_ldd;
588 if (backfs_mount_type_okay(ldd->ldd_mount_type))
589 ret = backfs_ops[ldd->ldd_mount_type]->write_ldd(mop);
597 /* Read the server config files */
598 int osd_read_ldd(char *dev, struct lustre_disk_data *ldd)
602 if (backfs_mount_type_okay(ldd->ldd_mount_type))
603 ret = backfs_ops[ldd->ldd_mount_type]->read_ldd(dev, ldd);
611 /* Was this device formatted for Lustre */
612 int osd_is_lustre(char *dev, unsigned *mount_type)
616 vprint("checking for existing Lustre data: ");
618 for (i = 0; i < LDD_MT_LAST; ++i) {
619 if (backfs_ops[i] != NULL &&
620 backfs_ops[i]->is_lustre(dev, mount_type)) {
626 vprint("not found\n");
630 /* Build fs according to type */
631 int osd_make_lustre(struct mkfs_opts *mop)
633 struct lustre_disk_data *ldd = &mop->mo_ldd;
636 if (backfs_mount_type_okay(ldd->ldd_mount_type))
637 ret = backfs_ops[ldd->ldd_mount_type]->make_lustre(mop);
645 int osd_prepare_lustre(struct mkfs_opts *mop,
646 char *default_mountopts, int default_len,
647 char *always_mountopts, int always_len)
649 struct lustre_disk_data *ldd = &mop->mo_ldd;
652 if (backfs_mount_type_okay(ldd->ldd_mount_type))
653 ret = backfs_ops[ldd->ldd_mount_type]->prepare_lustre(mop,
654 default_mountopts, default_len,
655 always_mountopts, always_len);
663 int osd_tune_lustre(char *dev, struct mount_opts *mop)
665 struct lustre_disk_data *ldd = &mop->mo_ldd;
668 if (backfs_mount_type_okay(ldd->ldd_mount_type))
669 ret = backfs_ops[ldd->ldd_mount_type]->tune_lustre(dev, mop);
677 int osd_label_lustre(struct mount_opts *mop)
679 struct lustre_disk_data *ldd = &mop->mo_ldd;
682 if (backfs_mount_type_okay(ldd->ldd_mount_type))
683 ret = backfs_ops[ldd->ldd_mount_type]->label_lustre(mop);
691 /* Enable quota accounting */
692 int osd_enable_quota(struct mkfs_opts *mop)
694 struct lustre_disk_data *ldd = &mop->mo_ldd;
697 if (backfs_mount_type_okay(ldd->ldd_mount_type))
698 ret = backfs_ops[ldd->ldd_mount_type]->enable_quota(mop);
710 for (i = 0; i < LDD_MT_LAST; ++i) {
711 backfs_ops[i] = load_backfs_module(i);
712 if (backfs_ops[i] != NULL)
713 ret = backfs_ops[i]->init();
725 for (i = 0; i < LDD_MT_LAST; ++i) {
726 if (backfs_ops[i] != NULL) {
727 backfs_ops[i]->fini();
728 unload_backfs_module(backfs_ops[i]);
729 backfs_ops[i] = NULL;
734 __u64 get_device_size(char* device)
739 fd = open(device, O_RDONLY);
741 fprintf(stderr, "%s: cannot open %s: %s\n",
742 progname, device, strerror(errno));
747 /* size in bytes. bz5831 */
748 ret = ioctl(fd, BLKGETSIZE64, (void*)&size);
753 ret = ioctl(fd, BLKGETSIZE, (void*)&lsize);
754 size = (__u64)lsize * 512;
759 fprintf(stderr, "%s: size ioctl failed: %s\n",
760 progname, strerror(errno));
764 vprint("device size = "LPU64"MB\n", size >> 20);
765 /* return value in KB */
769 int file_create(char *path, __u64 size)
776 * Since "size" is in KB, the file offset it represents could overflow
779 size_max = (off_t)1 << (_FILE_OFFSET_BITS - 1 - 10);
780 if (size >= size_max) {
781 fprintf(stderr, "%s: "LPU64" KB: Backing store size must be "
782 "smaller than "LPU64" KB\n", progname, size, size_max);
786 ret = access(path, F_OK);
793 fd = creat(path, S_IRUSR|S_IWUSR);
796 fprintf(stderr, "%s: Unable to create backing store: %s\n",
797 progname, strerror(errno));
801 ret = ftruncate(fd, size * 1024);
805 fprintf(stderr, "%s: Unable to truncate backing store: %s\n",
806 progname, strerror(errno));