* Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
* Use is subject to license terms.
*
- * Copyright (c) 2012, 2016, Intel Corporation.
+ * Copyright (c) 2012, 2017, Intel Corporation.
*/
/*
* This file is part of Lustre, http://www.lustre.org/
- * Lustre is a trademark of Sun Microsystems, Inc.
*/
#if HAVE_CONFIG_H
#include "mount_utils.h"
-extern char *progname;
-extern int verbose;
-
#define vprint(fmt, arg...) if (verbose > 0) printf(fmt, ##arg)
#define verrprint(fmt, arg...) if (verbose >= 0) fprintf(stderr, fmt, ##arg)
void fatal(void)
{
- verbose = 0;
- fprintf(stderr, "\n%s FATAL: ", progname);
+ verbose = 0;
+ fprintf(stderr, "\n%s FATAL: ", progname);
}
int run_command(char *cmd, int cmdsz)
{
- char log[] = "/tmp/run_command_logXXXXXX";
- int fd = -1, rc;
-
- if ((cmdsz - strlen(cmd)) < 6) {
- fatal();
- fprintf(stderr, "Command buffer overflow: %.*s...\n",
- cmdsz, cmd);
- return ENOMEM;
- }
-
- if (verbose > 1) {
- printf("cmd: %s\n", cmd);
- } else {
- if ((fd = mkstemp(log)) >= 0) {
- close(fd);
- strcat(cmd, " >");
- strcat(cmd, log);
- }
- }
- strcat(cmd, " 2>&1");
-
- /* Can't use popen because we need the rv of the command */
- rc = system(cmd);
- if (rc && (fd >= 0)) {
- char buf[128];
- FILE *fp;
- fp = fopen(log, "r");
- if (fp) {
- while (fgets(buf, sizeof(buf), fp) != NULL) {
- printf(" %s", buf);
- }
- fclose(fp);
- }
- }
- if (fd >= 0)
- remove(log);
- return rc;
+ char log[] = "/tmp/run_command_logXXXXXX";
+ int fd = -1, rc;
+
+ if ((cmdsz - strlen(cmd)) < 6) {
+ fatal();
+ fprintf(stderr, "Command buffer overflow: %.*s...\n",
+ cmdsz, cmd);
+ return ENOMEM;
+ }
+
+ if (verbose > 1) {
+ printf("cmd: %s\n", cmd);
+ } else {
+ if ((fd = mkstemp(log)) >= 0) {
+ close(fd);
+ strcat(cmd, " >");
+ strcat(cmd, log);
+ }
+ }
+ strcat(cmd, " 2>&1");
+
+ /* Can't use popen because we need the rv of the command */
+ rc = system(cmd);
+ if (rc && (fd >= 0)) {
+ char buf[128];
+ FILE *fp;
+
+ fp = fopen(log, "r");
+ if (fp) {
+ while (fgets(buf, sizeof(buf), fp))
+ printf(" %s", buf);
+ fclose(fp);
+ }
+ }
+ if (fd >= 0)
+ remove(log);
+ return rc;
}
#ifdef HAVE_SERVER_SUPPORT
ptr = strstr(buf, key);
if (ptr) {
*val = strdup(ptr + key_len);
- if (*val == NULL)
+ if (!(*val))
return ENOMEM;
for (i = 0; i < strlen(*val); i++)
ptr = strstr(buf, key);
/* key doesn't exist yet, so just add it */
- if (ptr == NULL)
+ if (!ptr)
return add_param(buf, key, val);
key_len = strlen(key);
/* Copy previous values to str */
for (i = 0; i < sizeof(str); ++i) {
- if ((ptr[i+key_len] == ' ') || (ptr[i+key_len] == '\0'))
+ if ((ptr[i + key_len] == ' ') || (ptr[i + key_len] == '\0'))
break;
- str[i] = ptr[i+key_len];
+ str[i] = ptr[i + key_len];
}
if (i == sizeof(str))
return E2BIG;
old_val_len = i;
- offset = old_val_len+key_len;
+ offset = old_val_len + key_len;
/* Move rest of buf to overwrite previous key and value */
- for (i = 0; ptr[i+offset] != '\0'; ++i)
- ptr[i] = ptr[i+offset];
+ for (i = 0; ptr[i + offset] != '\0'; ++i)
+ ptr[i] = ptr[i + offset];
ptr[i] = '\0';
- snprintf(str+old_val_len, sizeof(str)-old_val_len, "%c%s", sep, val);
+ snprintf(str + old_val_len, sizeof(str) - old_val_len,
+ "%c%s", sep, val);
return add_param(buf, key, str);
}
{
dst[buflen - 1] = 0;
if (strlen(dst) + strlen(src) >= buflen) {
- fprintf(stderr, "string buffer overflow (max %d): '%s' + '%s'"
- "\n", buflen, dst, src);
+ fprintf(stderr,
+ "string buffer overflow (max %d): '%s' + '%s'\n",
+ buflen, dst, src);
exit(EOVERFLOW);
}
return strcat(dst, src);
struct mntent *mnt;
fp = setmntent(MOUNTED, "r");
- if (fp == NULL)
+ if (!fp)
return 0;
while ((mnt = getmntent(fp)) != NULL) {
if ((strcmp(mnt->mnt_fsname, spec1) == 0 ||
strcmp(mnt->mnt_fsname, spec2) == 0) &&
- (mtpt == NULL || strcmp(mnt->mnt_dir, mtpt) == 0) &&
- (type == NULL || strcmp(mnt->mnt_type, type) == 0)) {
+ (!mtpt || strcmp(mnt->mnt_dir, mtpt) == 0) &&
+ (!type || strcmp(mnt->mnt_type, type) == 0)) {
endmntent(fp);
- return(EEXIST);
+ return EEXIST;
}
}
endmntent(fp);
static int mtab_is_proc(const char *mtab)
{
struct statfs s;
+
if (statfs(mtab, &s) < 0)
return 0;
#endif /* HAVE_LIBMOUNT */
int update_mtab_entry(char *spec, char *mtpt, char *type, char *opts,
- int flags, int freq, int pass)
+ int flags, int freq, int pass)
{
FILE *fp;
struct mntent mnt;
mnt.mnt_passno = pass;
fp = setmntent(MOUNTED, "a+");
- if (fp == NULL) {
+ if (!fp) {
fprintf(stderr, "%s: setmntent(%s): %s\n",
- progname, MOUNTED, strerror (errno));
+ progname, MOUNTED, strerror(errno));
rc = 16;
} else {
if ((addmntent(fp, &mnt)) == 1) {
fprintf(stderr, "%s: addmntent: %s\n",
- progname, strerror (errno));
+ progname, strerror(errno));
rc = 16;
}
endmntent(fp);
char cmd[PATH_MAX];
int cmdsz = sizeof(cmd);
-#ifdef HAVE_LOOP_CTL_GET_FREE
ret = open("/dev/loop-control", O_RDWR);
if (ret < 0) {
- fprintf(stderr, "%s: can't access loop control\n", progname);
+ fprintf(stderr, "%s: can't access loop control\n",
+ progname);
return EACCES;
}
/* find or allocate a free loop device to use */
i = ioctl(ret, LOOP_CTL_GET_FREE);
close(ret);
if (i < 0) {
- fprintf(stderr, "%s: access loop control error\n", progname);
+ fprintf(stderr, "%s: access loop control error\n",
+ progname);
return EACCES;
}
sprintf(l_device, "%s%d", loop_base, i);
-#else
- sprintf(l_device, "%s%d", loop_base, i);
- if (access(l_device, F_OK | R_OK))
- break;
-#endif
+
snprintf(cmd, cmdsz, "losetup %s > /dev/null 2>&1", l_device);
ret = system(cmd);
mop->mo_device);
ret = run_command(cmd, cmdsz);
if (ret == 256)
- /* someone else picked up this loop device
- * behind our back */
+ /*
+ * someone else picked up this loop device
+ * behind our back
+ */
continue;
if (ret) {
fprintf(stderr, "%s: error %d on losetup: %s\n",
if (mop->mo_device_kb == 0) {
fatal();
- fprintf(stderr, "loop device requires a --device-size= "
- "param\n");
+ fprintf(stderr,
+ "loop device requires a --device-size= param\n");
return EINVAL;
}
- fd = creat(mop->mo_device, S_IRUSR|S_IWUSR);
+ fd = creat(mop->mo_device, 0600);
if (fd < 0) {
fatal();
fprintf(stderr, "%s: Unable to create backing store: %s\n",
#ifdef PLUGIN_DIR
#define DLSYM(prefix, sym, func) \
do { \
- char _fname[64]; \
+ char _fname[PATH_MAX]; \
snprintf(_fname, sizeof(_fname), "%s_%s", prefix, #func); \
sym->func = (typeof(sym->func))dlsym(sym->dl_handle, _fname); \
} while (0)
{
struct module_backfs_ops *ops;
#ifdef PLUGIN_DIR
- char *error, filename[512], fsname[512], *name;
+ char *error, filename[PATH_MAX], fsname[512], *name;
void *handle;
- /* This deals with duplicate ldd_mount_types resolving to same OSD layer
- * plugin (e.g. ext3/ldiskfs/ldiskfs2 all being ldiskfs) */
+ /*
+ * This deals with duplicate ldd_mount_types resolving to same OSD layer
+ * plugin (e.g. ext3/ldiskfs/ldiskfs2 all being ldiskfs)
+ */
strncpy(fsname, mt_type(mount_type), sizeof(fsname));
name = fsname + sizeof("osd-") - 1;
handle = dlopen(filename, RTLD_LAZY);
- /* Check for $LUSTRE environment variable from test-framework.
+ /*
+ * Check for $LUSTRE environment variable from test-framework.
* This allows using locally built modules to be used.
*/
- if (handle == NULL) {
+ if (!handle) {
char *dirname;
+
dirname = getenv("LUSTRE");
if (dirname) {
snprintf(filename, sizeof(filename),
}
/* Do not clutter up console with missing types */
- if (handle == NULL)
+ if (!handle)
return NULL;
ops = malloc(sizeof(*ops));
- if (ops == NULL) {
+ if (!ops) {
dlclose(handle);
return NULL;
}
DLSYM(name, ops, enable_quota);
error = dlerror();
- if (error != NULL) {
+ if (error) {
fatal();
fprintf(stderr, "%s\n", error);
dlclose(handle);
void unload_backfs_module(struct module_backfs_ops *ops)
{
#ifdef PLUGIN_DIR
- if (ops == NULL)
+ if (!ops)
return;
dlclose(ops->dl_handle);
fprintf(stderr, "fs type out of range %d\n", mount_type);
return 0;
}
- if (backfs_ops[mount_type] == NULL) {
+ if (!backfs_ops[mount_type]) {
fatal();
fprintf(stderr, "unhandled/unloaded fs type %d '%s'\n",
mount_type, mt_str(mount_type));
}
/* Was this device formatted for Lustre */
-int osd_is_lustre(char *dev, unsigned *mount_type)
+int osd_is_lustre(char *dev, unsigned int *mount_type)
{
int i;
vprint("checking for existing Lustre data: ");
for (i = 0; i < LDD_MT_LAST; ++i) {
- if (backfs_ops[i] != NULL &&
+ if (backfs_ops[i] &&
backfs_ops[i]->is_lustre(dev, mount_type)) {
vprint("found\n");
return 1;
if (!backfs_mount_type_okay(ldd->ldd_mount_type))
return EINVAL;
- if (backfs_ops[ldd->ldd_mount_type]->fix_mountopts == NULL)
+ if (!backfs_ops[ldd->ldd_mount_type]->fix_mountopts)
return 0;
return backfs_ops[ldd->ldd_mount_type]->fix_mountopts(mop, mountopts,
for (i = 0; i < LDD_MT_LAST; ++i) {
rc = 0;
backfs_ops[i] = load_backfs_module(i);
- if (backfs_ops[i] != NULL) {
+ if (backfs_ops[i]) {
rc = backfs_ops[i]->init();
if (rc != 0) {
backfs_ops[i]->fini();
unload_backfs_module(backfs_ops[i]);
backfs_ops[i] = NULL;
- } else
+ } else {
ret = 0;
+ }
}
}
int i;
for (i = 0; i < LDD_MT_LAST; ++i) {
- if (backfs_ops[i] != NULL) {
+ if (backfs_ops[i]) {
backfs_ops[i]->fini();
unload_backfs_module(backfs_ops[i]);
backfs_ops[i] = NULL;
}
}
-__u64 get_device_size(char* device)
+__u64 get_device_size(char *device)
{
int ret, fd;
__u64 size = 0;
#ifdef BLKGETSIZE64
/* size in bytes. bz5831 */
- ret = ioctl(fd, BLKGETSIZE64, (void*)&size);
+ ret = ioctl(fd, BLKGETSIZE64, (void *)&size);
#else
{
__u32 lsize = 0;
/* size in blocks */
- ret = ioctl(fd, BLKGETSIZE, (void*)&lsize);
+ ret = ioctl(fd, BLKGETSIZE, (void *)&lsize);
size = (__u64)lsize * 512;
}
#endif
*/
size_max = (off_t)1 << (_FILE_OFFSET_BITS - 1 - 10);
if (size >= size_max) {
- fprintf(stderr, "%s: %ju KB: Backing store size must be "
- "smaller than %ju KB\n", progname, (uintmax_t) size,
- (uintmax_t)size_max);
+ fprintf(stderr,
+ "%s: %ju KB: Backing store size must be smaller than %ju KB\n",
+ progname, (uintmax_t)size, (uintmax_t)size_max);
return EFBIG;
}
return errno;
}
- fd = creat(path, S_IRUSR|S_IWUSR);
+ fd = creat(path, 0600);
if (fd < 0) {
fatal();
fprintf(stderr, "%s: Unable to create backing store: %s\n",
{
struct lustre_disk_data *ldd = &mop->mo_ldd;
struct lr_server_data lsd;
- char filepnm[128];
+ char filepnm[132];
char cfg_dir[128];
DIR *dir = NULL;
struct dirent64 *dirent;
snprintf(filepnm, sizeof(filepnm), "%s/%s", cfg_dir,
lce->lce_name);
if (IS_MGS(ldd))
- /* Store the new fsname in the XATTR_TARGET_RENAME EA.
+ /*
+ * Store the new fsname in the XATTR_TARGET_RENAME EA.
* When the MGS start, it will scan config logs, and
* for the ones which have the XATTR_TARGET_RENAME EA,
* it will replace old fsname with the new fsname in
* the config log by some shared kernel level config
- * logs {fork,erase} functionalities automatically. */
+ * logs {fork,erase} functionalities automatically.
+ */
ret = setxattr(filepnm, XATTR_TARGET_RENAME,
ldd->ldd_fsname,
strlen(ldd->ldd_fsname), 0);
}
dir = opendir(path);
- if (dir == NULL) {
+ if (!dir) {
fprintf(stderr, "Unable to open shared key directory: %s\n",
path);
return -ENOENT;
}
- /* Loop through the files in the directory attempting to load them.
+ /*
+ * Loop through the files in the directory attempting to load them.
* Any issue with loading the keyfile is treated as an error although
* the loop continues until all files have been attempted. This will
* allow all errors be reported at once rather then requiring
- * incremental corrections to fix each one and try again. */
+ * incremental corrections to fix each one and try again.
+ */
while ((dentry = readdir(dir)) != NULL) {
if (strcmp(".", dentry->d_name) == 0 ||
strcmp("..", dentry->d_name) == 0)
continue;
rc = sk_load_keyfile(fullpath);
- if (rc) {
+ if (rc)
fprintf(stderr, "Failed to load key %s\n", fullpath);
- }
}
closedir(dir);