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, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
34 #endif /* HAVE_CONFIG_H */
44 #include <linux/lnet/nidstr.h>
45 #include <linux/lustre/lustre_ver.h>
46 #include <sys/mount.h>
48 #include <sys/utsname.h>
49 #include <linux/loop.h>
50 #include <sys/types.h>
53 #include <linux/lustre/lustre_cfg.h>
55 #include <sys/types.h>
56 #include <sys/xattr.h>
57 #include <libmount/libmount.h>
58 #include <libcfs/util/string.h>
61 #ifdef HAVE_LIBKEYUTILS
64 #include <lustre/utils/gss/sk_utils.h>
67 #include "mount_utils.h"
69 #define vprint(fmt, arg...) if (verbose > 0) printf(fmt, ##arg)
70 #define verrprint(fmt, arg...) if (verbose >= 0) fprintf(stderr, fmt, ##arg)
72 #ifdef HAVE_SERVER_SUPPORT
73 static struct module_backfs_ops *backfs_ops[LDD_MT_LAST];
79 fprintf(stderr, "\n%s FATAL: ", progname);
82 int run_command(char *cmd, int cmdsz)
84 char log[] = "/tmp/run_command_logXXXXXX";
87 if ((cmdsz - strlen(cmd)) < 6) {
89 fprintf(stderr, "Command buffer overflow: %.*s...\n",
95 printf("cmd: %s\n", cmd);
97 if ((fd = mkstemp(log)) >= 0) {
103 strcat(cmd, " 2>&1");
105 /* Can't use popen because we need the rv of the command */
107 if (rc && (fd >= 0)) {
111 fp = fopen(log, "r");
113 while (fgets(buf, sizeof(buf), fp))
123 #ifdef HAVE_SERVER_SUPPORT
124 int add_param(char *buf, char *key, char *val)
126 int end = sizeof(((struct lustre_disk_data *)0)->ldd_params);
127 int start = strlen(buf);
131 keylen = strlen(key);
132 if (start + 1 + keylen + strlen(val) >= end) {
133 fprintf(stderr, "%s: params are too long-\n%s %s%s\n",
134 progname, buf, key ? key : "", val);
138 sprintf(buf + start, " %s%s", key ? key : "", val);
142 int get_param(char *buf, char *key, char **val)
144 int i, key_len = strlen(key);
147 ptr = strstr(buf, key);
149 *val = strdup(ptr + key_len);
153 for (i = 0; i < strlen(*val); i++)
154 if (((*val)[i] == ' ') || ((*val)[i] == '\0'))
164 int append_param(char *buf, char *key, char *val, char sep)
166 int key_len, i, offset, old_val_len;
167 char *ptr = NULL, str[1024];
170 ptr = strstr(buf, key);
172 /* key doesn't exist yet, so just add it */
174 return add_param(buf, key, val);
176 key_len = strlen(key);
178 /* Copy previous values to str */
179 for (i = 0; i < sizeof(str); ++i) {
180 if ((ptr[i + key_len] == ' ') || (ptr[i + key_len] == '\0'))
182 str[i] = ptr[i + key_len];
184 if (i == sizeof(str))
188 offset = old_val_len + key_len;
190 /* Move rest of buf to overwrite previous key and value */
191 for (i = 0; ptr[i + offset] != '\0'; ++i)
192 ptr[i] = ptr[i + offset];
196 snprintf(str + old_val_len, sizeof(str) - old_val_len,
199 return add_param(buf, key, str);
203 char *strscat(char *dst, char *src, int buflen)
206 if (strlen(dst) + strlen(src) >= buflen) {
208 "string buffer overflow (max %d): '%s' + '%s'\n",
212 return strcat(dst, src);
215 char *strscpy(char *dst, char *src, int buflen)
218 return strscat(dst, src, buflen);
221 int check_mtab_entry(char *spec1, char *spec2, char *mtpt, char *type)
226 fp = setmntent(MOUNTED, "r");
230 while ((mnt = getmntent(fp)) != NULL) {
231 if ((strcmp(mnt->mnt_fsname, spec1) == 0 ||
232 strcmp(mnt->mnt_fsname, spec2) == 0) &&
233 (!mtpt || strcmp(mnt->mnt_dir, mtpt) == 0) &&
234 (!type || strcmp(mnt->mnt_type, type) == 0)) {
245 #include <linux/magic.h>
247 static int mtab_is_proc(const char *mtab)
251 if (statfs(mtab, &s) < 0)
254 return (s.f_type == PROC_SUPER_MAGIC);
258 * The libmount is part of util-linux since 2.18.
259 * We use it to update utab to avoid umount would
260 * blocked in some rare case.
262 int update_utab_entry(struct mount_opts *mop)
264 struct libmnt_fs *fs = mnt_new_fs();
265 struct libmnt_update *upd;
268 mnt_fs_set_source(fs, mop->mo_source);
269 mnt_fs_set_target(fs, mop->mo_target);
270 mnt_fs_set_fstype(fs, "lustre");
271 mnt_fs_set_attributes(fs, "lustre");
273 upd = mnt_new_update();
277 rc = mnt_update_set_fs(upd, mop->mo_nomtab ? MS_REMOUNT : 0, NULL, fs);
278 if (rc == 1) /* update is unnecessary */
282 "error: failed to save utab entry: rc = %d\n", rc);
284 rc = mnt_update_table(upd, NULL);
287 mnt_free_update(upd);
293 int update_mtab_entry(char *spec, char *mtpt, char *type, char *opts,
294 int flags, int freq, int pass)
300 /* Don't update mtab if it is linked to any file in /proc direcotry.*/
301 if (mtab_is_proc(MOUNTED))
304 mnt.mnt_fsname = spec;
307 mnt.mnt_opts = opts ? opts : "";
309 mnt.mnt_passno = pass;
311 fp = setmntent(MOUNTED, "a+");
313 fprintf(stderr, "%s: setmntent(%s): %s\n",
314 progname, MOUNTED, strerror(errno));
317 if ((addmntent(fp, &mnt)) == 1) {
318 fprintf(stderr, "%s: addmntent: %s\n",
319 progname, strerror(errno));
328 /* Search for opt in mntlist, returning true if found.
330 static int in_mntlist(char *opt, char *mntlist)
332 char *ml, *mlp, *item, *ctx = NULL;
334 if (!(ml = strdup(mntlist))) {
335 fprintf(stderr, "%s: out of memory\n", progname);
339 while ((item = strtok_r(mlp, ",", &ctx))) {
340 if (!strcmp(opt, item))
345 return (item != NULL);
348 /* Issue a message on stderr for every item in wanted_mountopts that is not
349 * present in mountopts. The justwarn boolean toggles between error and
350 * warning message. Return an error count.
352 int check_mountfsoptions(char *mountopts, char *wanted_mountopts)
354 char *ml, *mlp, *item, *ctx = NULL;
357 if (!(ml = strdup(wanted_mountopts))) {
358 fprintf(stderr, "%s: out of memory\n", progname);
362 while ((item = strtok_r(mlp, ",", &ctx))) {
363 if (!in_mntlist(item, mountopts)) {
364 fprintf(stderr, "%s: Error: mandatory mount option"
365 " '%s' is missing\n", progname, item);
374 /* Trim embedded white space, leading and trailing commas from string s.
376 void trim_mountfsoptions(char *s)
382 memmove(p, p + 1, strlen(p + 1) + 1);
389 memmove(&s[0], &s[1], strlen(&s[1]) + 1);
391 p = s + strlen(s) - 1;
392 while (p >= s && *p == ',')
396 #ifdef HAVE_SERVER_SUPPORT
397 /* Setup a file in the first unused loop_device */
398 int loop_setup(struct mkfs_opts *mop)
404 /* Figure out the loop device names */
405 if (!access("/dev/loop0", F_OK | R_OK) ||
406 !access("/dev/loop-control", F_OK | R_OK)) {
407 strcpy(loop_base, "/dev/loop\0");
408 } else if (!access("/dev/loop/0", F_OK | R_OK)) {
409 strcpy(loop_base, "/dev/loop/\0");
411 fprintf(stderr, "%s: can't access loop devices\n", progname);
415 /* Find unused loop device */
416 for (i = 0; i < MAX_LOOP_DEVICES; i++) {
418 int cmdsz = sizeof(cmd);
420 ret = open("/dev/loop-control", O_RDWR);
422 fprintf(stderr, "%s: can't access loop control\n",
426 /* find or allocate a free loop device to use */
427 i = ioctl(ret, LOOP_CTL_GET_FREE);
430 fprintf(stderr, "%s: access loop control error\n",
434 sprintf(l_device, "%s%d", loop_base, i);
436 snprintf(cmd, cmdsz, "losetup %s > /dev/null 2>&1", l_device);
439 /* losetup gets 1 (ret=256) for non-set-up device */
441 /* Set up a loopback device to our file */
442 snprintf(cmd, cmdsz, "losetup %s %s", l_device,
444 ret = run_command(cmd, cmdsz);
447 * someone else picked up this loop device
452 fprintf(stderr, "%s: error %d on losetup: %s\n",
454 ret >= 0 ? strerror(ret) : "");
457 strscpy(mop->mo_loopdev, l_device,
458 sizeof(mop->mo_loopdev));
463 fprintf(stderr, "%s: out of loop devices!\n", progname);
467 int loop_cleanup(struct mkfs_opts *mop)
472 if ((mop->mo_flags & MO_IS_LOOP) && *mop->mo_loopdev) {
475 sprintf(cmd, "losetup -d %s", mop->mo_loopdev);
476 for (tries = 0; tries < 3; tries++) {
477 ret = run_command(cmd, sizeof(cmd));
485 fprintf(stderr, "cannot cleanup %s: rc = %d\n",
486 mop->mo_loopdev, ret);
490 int loop_format(struct mkfs_opts *mop)
494 if (mop->mo_device_kb == 0) {
497 "loop device requires a --device-size= param\n");
501 fd = creat(mop->mo_device, 0600);
504 fprintf(stderr, "%s: Unable to create backing store: %s\n",
505 progname, strerror(errno));
509 if (ftruncate(fd, mop->mo_device_kb * 1024) != 0) {
512 fprintf(stderr, "%s: Unable to truncate backing store: %s\n",
513 progname, strerror(errno));
522 #define DLSYM(prefix, sym, func) \
524 char _fname[PATH_MAX]; \
525 snprintf(_fname, sizeof(_fname), "%s_%s", prefix, #func); \
526 sym->func = (typeof(sym->func))dlsym(sym->dl_handle, _fname); \
528 #endif /* PLUGIN_DIR */
531 * Load plugin for a given mount_type from ${pkglibdir}/mount_osd_FSTYPE.so and
532 * return struct of function pointers (will be freed in unloack_backfs_module).
534 * \param[in] mount_type Mount type to load module for.
535 * \retval Value of backfs_ops struct
536 * \retval NULL if no module exists
538 struct module_backfs_ops *load_backfs_module(enum ldd_mount_type mount_type)
540 struct module_backfs_ops *ops;
542 char *error, filename[PATH_MAX], fsname[512], *name;
546 * This deals with duplicate ldd_mount_types resolving to same OSD layer
547 * plugin (e.g. ext3/ldiskfs/ldiskfs2 all being ldiskfs)
549 strncpy(fsname, mt_type(mount_type), sizeof(fsname));
550 name = fsname + sizeof("osd-") - 1;
552 /* change osd- to osd_ */
553 fsname[sizeof("osd-") - 2] = '_';
555 snprintf(filename, sizeof(filename), PLUGIN_DIR"/mount_%s.so", fsname);
557 handle = dlopen(filename, RTLD_LAZY);
560 * Check for $LUSTRE environment variable from test-framework.
561 * This allows using locally built modules to be used.
566 dirname = getenv("LUSTRE");
568 snprintf(filename, sizeof(filename),
569 "%s/utils/mount_%s.so",
571 handle = dlopen(filename, RTLD_LAZY);
575 /* Do not clutter up console with missing types */
579 ops = malloc(sizeof(*ops));
585 ops->dl_handle = handle;
586 dlerror(); /* Clear any existing error */
588 DLSYM(name, ops, init);
589 DLSYM(name, ops, fini);
590 DLSYM(name, ops, read_ldd);
591 DLSYM(name, ops, write_ldd);
592 DLSYM(name, ops, erase_ldd);
593 DLSYM(name, ops, print_ldd_params);
594 DLSYM(name, ops, is_lustre);
595 DLSYM(name, ops, make_lustre);
596 DLSYM(name, ops, prepare_lustre);
597 DLSYM(name, ops, tune_lustre);
598 DLSYM(name, ops, label_lustre);
599 DLSYM(name, ops, rename_fsname);
600 DLSYM(name, ops, enable_quota);
605 fprintf(stderr, "%s\n", error);
611 /* optional methods */
612 DLSYM(name, ops, fix_mountopts);
614 switch (mount_type) {
615 #ifdef HAVE_LDISKFS_OSD
619 #endif /* HAVE_LDISKFS_OSD */
624 #endif /* HAVE_ZFS_OSD */
634 * Unload plugin and free backfs_ops structure. Must be called the same number
635 * of times as load_backfs_module is.
637 static void unload_backfs_module(struct module_backfs_ops *ops)
643 dlclose(ops->dl_handle);
648 /* Return true if backfs_ops has operations for the given mount_type. */
649 static int backfs_mount_type_okay(enum ldd_mount_type mount_type)
651 if (mount_type >= LDD_MT_LAST || mount_type < 0) {
653 fprintf(stderr, "fs type out of range %d\n", mount_type);
656 if (!backfs_ops[mount_type]) {
658 fprintf(stderr, "unhandled/unloaded fs type %d '%s'\n",
659 mount_type, mt_str(mount_type));
665 /* Write the server config files */
666 int osd_write_ldd(struct mkfs_opts *mop)
668 struct lustre_disk_data *ldd = &mop->mo_ldd;
671 if (backfs_mount_type_okay(ldd->ldd_mount_type))
672 ret = backfs_ops[ldd->ldd_mount_type]->write_ldd(mop);
679 /* Read the server config files */
680 int osd_read_ldd(char *dev, struct lustre_disk_data *ldd)
684 if (backfs_mount_type_okay(ldd->ldd_mount_type))
685 ret = backfs_ops[ldd->ldd_mount_type]->read_ldd(dev, ldd);
692 /* Erase param from the server config files */
693 int osd_erase_ldd(struct mkfs_opts *mop, char *param)
695 struct lustre_disk_data *ldd = &mop->mo_ldd;
698 if (backfs_mount_type_okay(ldd->ldd_mount_type))
699 ret = backfs_ops[ldd->ldd_mount_type]->erase_ldd(mop, param);
706 /* Print ldd_params */
707 void osd_print_ldd_params(struct mkfs_opts *mop)
709 struct lustre_disk_data *ldd = &mop->mo_ldd;
711 if (backfs_mount_type_okay(ldd->ldd_mount_type))
712 backfs_ops[ldd->ldd_mount_type]->print_ldd_params(mop);
715 /* Was this device formatted for Lustre */
716 int osd_is_lustre(char *dev, unsigned int *mount_type)
720 vprint("checking for existing Lustre data: ");
722 for (i = 0; i < LDD_MT_LAST; ++i) {
724 backfs_ops[i]->is_lustre(dev, mount_type)) {
730 vprint("not found\n");
734 /* Build fs according to type */
735 int osd_make_lustre(struct mkfs_opts *mop)
737 struct lustre_disk_data *ldd = &mop->mo_ldd;
740 if (backfs_mount_type_okay(ldd->ldd_mount_type))
741 ret = backfs_ops[ldd->ldd_mount_type]->make_lustre(mop);
748 int osd_prepare_lustre(struct mkfs_opts *mop,
749 char *wanted_mountopts, size_t len)
751 struct lustre_disk_data *ldd = &mop->mo_ldd;
754 if (backfs_mount_type_okay(ldd->ldd_mount_type))
755 ret = backfs_ops[ldd->ldd_mount_type]->prepare_lustre(mop,
756 wanted_mountopts, len);
763 int osd_fix_mountopts(struct mkfs_opts *mop, char *mountopts, size_t len)
765 struct lustre_disk_data *ldd = &mop->mo_ldd;
767 if (!backfs_mount_type_okay(ldd->ldd_mount_type))
770 if (!backfs_ops[ldd->ldd_mount_type]->fix_mountopts)
773 return backfs_ops[ldd->ldd_mount_type]->fix_mountopts(mop, mountopts,
777 int osd_tune_lustre(char *dev, struct mount_opts *mop)
779 struct lustre_disk_data *ldd = &mop->mo_ldd;
782 if (backfs_mount_type_okay(ldd->ldd_mount_type))
783 ret = backfs_ops[ldd->ldd_mount_type]->tune_lustre(dev, mop);
790 int osd_label_lustre(struct mount_opts *mop)
792 struct lustre_disk_data *ldd = &mop->mo_ldd;
795 if (backfs_mount_type_okay(ldd->ldd_mount_type))
796 ret = backfs_ops[ldd->ldd_mount_type]->label_lustre(mop);
803 /* Rename filesystem fsname */
804 int osd_rename_fsname(struct mkfs_opts *mop, const char *oldname)
806 struct lustre_disk_data *ldd = &mop->mo_ldd;
809 if (backfs_mount_type_okay(ldd->ldd_mount_type))
810 ret = backfs_ops[ldd->ldd_mount_type]->rename_fsname(mop,
818 /* Enable quota accounting */
819 int osd_enable_quota(struct mkfs_opts *mop)
821 struct lustre_disk_data *ldd = &mop->mo_ldd;
824 if (backfs_mount_type_okay(ldd->ldd_mount_type))
825 ret = backfs_ops[ldd->ldd_mount_type]->enable_quota(mop);
834 int i, rc, ret = EINVAL;
836 for (i = 0; i < LDD_MT_LAST; ++i) {
838 backfs_ops[i] = load_backfs_module(i);
840 rc = backfs_ops[i]->init();
842 backfs_ops[i]->fini();
843 unload_backfs_module(backfs_ops[i]);
844 backfs_ops[i] = NULL;
858 for (i = 0; i < LDD_MT_LAST; ++i) {
860 backfs_ops[i]->fini();
861 unload_backfs_module(backfs_ops[i]);
862 backfs_ops[i] = NULL;
867 __u64 get_device_size(char *device)
872 fd = open(device, O_RDONLY);
874 fprintf(stderr, "%s: cannot open %s: %s\n",
875 progname, device, strerror(errno));
880 /* size in bytes. bz5831 */
881 ret = ioctl(fd, BLKGETSIZE64, (void *)&size);
886 ret = ioctl(fd, BLKGETSIZE, (void *)&lsize);
887 size = (__u64)lsize * 512;
892 fprintf(stderr, "%s: size ioctl failed: %s\n",
893 progname, strerror(errno));
897 vprint("device size = %juMB\n", (uintmax_t)(size >> 20));
898 /* return value in KB */
903 int file_create(char *path, __u64 size)
910 * Since "size" is in KB, the file offset it represents could overflow
913 size_max = (off_t)1 << (_FILE_OFFSET_BITS - 1 - 10);
914 if (size >= size_max) {
916 "%s: %ju KB: Backing store size must be smaller than %ju KB\n",
917 progname, (uintmax_t)size, (uintmax_t)size_max);
921 ret = access(path, F_OK);
928 fd = creat(path, 0600);
931 fprintf(stderr, "%s: Unable to create backing store: %s\n",
932 progname, strerror(errno));
936 ret = ftruncate(fd, size * 1024);
940 fprintf(stderr, "%s: Unable to truncate backing store: %s\n",
941 progname, strerror(errno));
948 /* Get rid of symbolic hostnames for tcp, since kernel can't do lookups */
949 #define MAXNIDSTR 1024
951 char *convert_hostnames(char *s1, bool mount)
953 char *converted, *s2 = 0, *c, *end, sep;
954 int left = MAXNIDSTR;
957 converted = malloc(left);
959 fprintf(stderr, "%s: cannot allocate %u bytes for NID: %s\n",
960 progname, left, strerror(ENOMEM));
964 /* end is different between mount and mkfs case */
966 end = strchr(s1, '/');
968 fprintf(stderr, "%s: Invalid mount string: %s\n",
970 goto out_bad_mnt_str;
974 end = s1 + strlen(s1);
978 while ((left > 0) && (s1 < end)) {
981 /* Needed to skip : in IPv6 / GUID strings. Lustre uses
982 * ':' as a seperator as well which makes this complicated.
984 s2 = strchr(s1, '@');
985 if (!s2 || s2 > end) {
986 fprintf(stderr, "%s: Invalid NID string '%s'\n",
991 s2 = strpbrk(s2, ",:");
994 } else if (s2 > end) {
995 fprintf(stderr, "%s: Invalid NID string '%s'\n",
1002 rc = libcfs_strnid(&nid, s1);
1004 fprintf(stderr, "%s: Unsupported NID '%s': rc = %s.\n",
1005 progname, s1, strerror(rc));
1008 *s2 = sep; /* back to original string */
1010 if (LNET_NID_IS_ANY(&nid)) {
1011 fprintf(stderr, "%s: Cannot resolve hostname '%s'.\n",
1017 c += scnprintf(c, left, "%s%c", libcfs_nidstr(&nid),
1020 c += scnprintf(c, left, "%s", libcfs_nidstr(&nid));
1021 left = converted + MAXNIDSTR - c;
1026 snprintf(c, left, "%s", s1);
1030 fprintf(stderr, "%s: Can't parse NID '%s'\n", progname, s1);
1036 #ifdef HAVE_SERVER_SUPPORT
1037 struct lustre_cfg_entry {
1038 struct list_head lce_list;
1042 static struct lustre_cfg_entry *lustre_cfg_entry_init(const char *name)
1044 struct lustre_cfg_entry *lce;
1045 int len = strlen(name) + 1;
1047 lce = malloc(sizeof(*lce) + len);
1049 INIT_LIST_HEAD(&lce->lce_list);
1050 memcpy(lce->lce_name, name, len);
1056 static void lustre_cfg_entry_fini(struct lustre_cfg_entry *lce)
1061 int lustre_rename_fsname(struct mkfs_opts *mop, const char *mntpt,
1062 const char *oldname)
1064 struct lustre_disk_data *ldd = &mop->mo_ldd;
1065 struct lr_server_data lsd;
1069 struct dirent64 *dirent;
1070 struct lustre_cfg_entry *lce;
1071 struct list_head cfg_list;
1072 int old_namelen = strlen(oldname);
1073 int new_namelen = strlen(ldd->ldd_fsname);
1077 INIT_LIST_HEAD(&cfg_list);
1079 snprintf(filepnm, sizeof(filepnm), "%s/%s", mntpt, LAST_RCVD);
1080 fd = open(filepnm, O_RDWR);
1082 if (errno == ENOENT)
1086 fprintf(stderr, "Unable to open %s: %s\n",
1087 filepnm, strerror(ret));
1091 ret = read(fd, &lsd, sizeof(lsd));
1092 if (ret != sizeof(lsd)) {
1098 fprintf(stderr, "Unable to read %s: %s\n",
1099 filepnm, strerror(ret));
1104 ret = lseek(fd, 0, SEEK_SET);
1107 fprintf(stderr, "Unable to lseek %s: %s\n",
1108 filepnm, strerror(ret));
1113 /* replace fsname in lr_server_data::lsd_uuid. */
1114 if (old_namelen > new_namelen)
1115 memmove(lsd.lsd_uuid + new_namelen,
1116 lsd.lsd_uuid + old_namelen,
1117 sizeof(lsd.lsd_uuid) - old_namelen);
1118 else if (old_namelen < new_namelen)
1119 memmove(lsd.lsd_uuid + new_namelen,
1120 lsd.lsd_uuid + old_namelen,
1121 sizeof(lsd.lsd_uuid) - new_namelen);
1122 memcpy(lsd.lsd_uuid, ldd->ldd_fsname, new_namelen);
1123 ret = write(fd, &lsd, sizeof(lsd));
1124 if (ret != sizeof(lsd)) {
1130 fprintf(stderr, "Unable to write %s: %s\n",
1131 filepnm, strerror(ret));
1139 snprintf(cfg_dir, sizeof(cfg_dir), "%s/%s", mntpt, MOUNT_CONFIGS_DIR);
1140 dir = opendir(cfg_dir);
1143 fprintf(stderr, "Unable to opendir %s: %s\n",
1144 cfg_dir, strerror(ret));
1148 while ((dirent = readdir64(dir)) != NULL) {
1151 if (strlen(dirent->d_name) <= old_namelen)
1154 ptr = strrchr(dirent->d_name, '-');
1155 if (!ptr || (ptr - dirent->d_name) != old_namelen)
1158 if (strncmp(dirent->d_name, oldname, old_namelen) != 0)
1161 lce = lustre_cfg_entry_init(dirent->d_name);
1168 fprintf(stderr, "Fail to init item for %s: %s\n",
1169 dirent->d_name, strerror(ret));
1173 list_add(&lce->lce_list, &cfg_list);
1180 while (!list_empty(&cfg_list) && ret == 0) {
1181 lce = list_first_entry(&cfg_list, struct lustre_cfg_entry,
1183 list_del(&lce->lce_list);
1184 snprintf(filepnm, sizeof(filepnm), "%s/%s", cfg_dir,
1188 * Store the new fsname in the XATTR_TARGET_RENAME EA.
1189 * When the MGS start, it will scan config logs, and
1190 * for the ones which have the XATTR_TARGET_RENAME EA,
1191 * it will replace old fsname with the new fsname in
1192 * the config log by some shared kernel level config
1193 * logs {fork,erase} functionalities automatically.
1195 ret = setxattr(filepnm, XATTR_TARGET_RENAME,
1197 strlen(ldd->ldd_fsname), 0);
1199 ret = unlink(filepnm);
1204 fprintf(stderr, "Fail to %s %s: %s\n",
1205 IS_MGS(ldd) ? "setxattr" : "unlink",
1206 filepnm, strerror(ret));
1209 lustre_cfg_entry_fini(lce);
1216 while (!list_empty(&cfg_list)) {
1217 lce = list_first_entry(&cfg_list, struct lustre_cfg_entry,
1219 list_del(&lce->lce_list);
1220 lustre_cfg_entry_fini(lce);
1225 #endif /* HAVE_SERVER_SUPPORT */
1228 #ifdef HAVE_OPENSSL_SSK
1229 int load_shared_keys(struct mount_opts *mop)
1232 struct dirent *dentry;
1234 char fullpath[PATH_MAX];
1235 char *path = mop->mo_skpath;
1239 sk_init_logging(NULL, 1, 1);
1241 rc = stat(path, &sbuf);
1243 fprintf(stderr, "stat() failed for key %s: %s\n", path,
1248 /* Load individual keys or a directory of them */
1249 if (S_ISREG(sbuf.st_mode)) {
1250 return sk_load_keyfile(path);
1251 } else if (!S_ISDIR(sbuf.st_mode)) {
1252 fprintf(stderr, "Invalid shared key path: %s\n", path);
1256 dir = opendir(path);
1258 fprintf(stderr, "Unable to open shared key directory: %s\n",
1264 * Loop through the files in the directory attempting to load them.
1265 * Any issue with loading the keyfile is treated as an error although
1266 * the loop continues until all files have been attempted. This will
1267 * allow all errors be reported at once rather then requiring
1268 * incremental corrections to fix each one and try again.
1270 while ((dentry = readdir(dir)) != NULL) {
1271 if (strcmp(".", dentry->d_name) == 0 ||
1272 strcmp("..", dentry->d_name) == 0)
1275 rc = snprintf(fullpath, PATH_MAX, "%s/%s", path,
1277 if (rc >= PATH_MAX) {
1278 fprintf(stderr, "Path too long for %s/%s\n",
1279 path, dentry->d_name);
1284 rc = stat(fullpath, &sbuf);
1286 fprintf(stderr, "Unable to stat %s: %s\n", fullpath,
1292 if (!S_ISREG(sbuf.st_mode))
1295 rc = sk_load_keyfile(fullpath);
1297 fprintf(stderr, "Failed to load key %s\n", fullpath);
1303 #endif /* HAVE_OPENSSL_SSK */
1304 #endif /* HAVE_GSS */