Whamcloud - gitweb
LU-15652 build: On Debian detect -common kernel headers
[fs/lustre-release.git] / lustre / utils / mount_utils.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
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.
9  *
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).
15  *
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
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2012, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  */
31
32 #if HAVE_CONFIG_H
33 #  include "config.h"
34 #endif /* HAVE_CONFIG_H */
35
36 #include <inttypes.h>
37 #include <limits.h>
38 #include <mntent.h>
39 #include <stdio.h>
40 #include <errno.h>
41 #include <string.h>
42 #include <unistd.h>
43 #include <config.h>
44 #include <linux/lustre/lustre_ver.h>
45 #include <sys/mount.h>
46 #include <sys/stat.h>
47 #include <sys/utsname.h>
48 #include <linux/loop.h>
49 #include <sys/types.h>
50 #include <dirent.h>
51 #include <dlfcn.h>
52 #include <linux/lustre/lustre_cfg.h>
53 #include <dirent.h>
54 #include <sys/types.h>
55 #include <sys/xattr.h>
56
57 #ifdef HAVE_GSS
58 #ifdef HAVE_LIBKEYUTILS
59 #include <keyutils.h>
60 #endif
61 #include <lustre/utils/gss/sk_utils.h>
62 #endif
63
64 #include "mount_utils.h"
65
66 #define vprint(fmt, arg...) if (verbose > 0) printf(fmt, ##arg)
67 #define verrprint(fmt, arg...) if (verbose >= 0) fprintf(stderr, fmt, ##arg)
68
69 #ifdef HAVE_SERVER_SUPPORT
70 static struct module_backfs_ops *backfs_ops[LDD_MT_LAST];
71 #endif
72
73 void fatal(void)
74 {
75         verbose = 0;
76         fprintf(stderr, "\n%s FATAL: ", progname);
77 }
78
79 int run_command(char *cmd, int cmdsz)
80 {
81         char log[] = "/tmp/run_command_logXXXXXX";
82         int fd = -1, rc;
83
84         if ((cmdsz - strlen(cmd)) < 6) {
85                 fatal();
86                 fprintf(stderr, "Command buffer overflow: %.*s...\n",
87                         cmdsz, cmd);
88                 return ENOMEM;
89         }
90
91         if (verbose > 1) {
92                 printf("cmd: %s\n", cmd);
93         } else {
94                 if ((fd = mkstemp(log)) >= 0) {
95                         close(fd);
96                         strcat(cmd, " >");
97                         strcat(cmd, log);
98                 }
99         }
100         strcat(cmd, " 2>&1");
101
102         /* Can't use popen because we need the rv of the command */
103         rc = system(cmd);
104         if (rc && (fd >= 0)) {
105                 char buf[128];
106                 FILE *fp;
107
108                 fp = fopen(log, "r");
109                 if (fp) {
110                         while (fgets(buf, sizeof(buf), fp))
111                                 printf("   %s", buf);
112                         fclose(fp);
113                 }
114         }
115         if (fd >= 0)
116                 remove(log);
117         return rc;
118 }
119
120 #ifdef HAVE_SERVER_SUPPORT
121 int add_param(char *buf, char *key, char *val)
122 {
123         int end = sizeof(((struct lustre_disk_data *)0)->ldd_params);
124         int start = strlen(buf);
125         int keylen = 0;
126
127         if (key)
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);
132                 return 1;
133         }
134
135         sprintf(buf + start, " %s%s", key ? key : "", val);
136         return 0;
137 }
138
139 int get_param(char *buf, char *key, char **val)
140 {
141         int i, key_len = strlen(key);
142         char *ptr;
143
144         ptr = strstr(buf, key);
145         if (ptr) {
146                 *val = strdup(ptr + key_len);
147                 if (!(*val))
148                         return ENOMEM;
149
150                 for (i = 0; i < strlen(*val); i++)
151                         if (((*val)[i] == ' ') || ((*val)[i] == '\0'))
152                                 break;
153
154                 (*val)[i] = '\0';
155                 return 0;
156         }
157
158         return ENOENT;
159 }
160
161 int append_param(char *buf, char *key, char *val, char sep)
162 {
163         int key_len, i, offset, old_val_len;
164         char *ptr = NULL, str[1024];
165
166         if (key)
167                 ptr = strstr(buf, key);
168
169         /* key doesn't exist yet, so just add it */
170         if (!ptr)
171                 return add_param(buf, key, val);
172
173         key_len = strlen(key);
174
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'))
178                         break;
179                 str[i] = ptr[i + key_len];
180         }
181         if (i == sizeof(str))
182                 return E2BIG;
183         old_val_len = i;
184
185         offset = old_val_len + key_len;
186
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];
190
191         ptr[i] = '\0';
192
193         snprintf(str + old_val_len, sizeof(str) - old_val_len,
194                  "%c%s", sep, val);
195
196         return add_param(buf, key, str);
197 }
198 #endif
199
200 char *strscat(char *dst, char *src, int buflen)
201 {
202         dst[buflen - 1] = 0;
203         if (strlen(dst) + strlen(src) >= buflen) {
204                 fprintf(stderr,
205                         "string buffer overflow (max %d): '%s' + '%s'\n",
206                         buflen, dst, src);
207                 exit(EOVERFLOW);
208         }
209         return strcat(dst, src);
210 }
211
212 char *strscpy(char *dst, char *src, int buflen)
213 {
214         dst[0] = 0;
215         return strscat(dst, src, buflen);
216 }
217
218 int check_mtab_entry(char *spec1, char *spec2, char *mtpt, char *type)
219 {
220         FILE *fp;
221         struct mntent *mnt;
222
223         fp = setmntent(MOUNTED, "r");
224         if (!fp)
225                 return 0;
226
227         while ((mnt = getmntent(fp)) != NULL) {
228                 if ((strcmp(mnt->mnt_fsname, spec1) == 0 ||
229                      strcmp(mnt->mnt_fsname, spec2) == 0) &&
230                     (!mtpt || strcmp(mnt->mnt_dir, mtpt) == 0) &&
231                     (!type || strcmp(mnt->mnt_type, type) == 0)) {
232                         endmntent(fp);
233                         return EEXIST;
234                 }
235         }
236         endmntent(fp);
237
238         return 0;
239 }
240
241 #include <sys/vfs.h>
242 #include <linux/magic.h>
243
244 static int mtab_is_proc(const char *mtab)
245 {
246         struct statfs s;
247
248         if (statfs(mtab, &s) < 0)
249                 return 0;
250
251         return (s.f_type == PROC_SUPER_MAGIC);
252 }
253
254 #ifdef HAVE_LIBMOUNT
255
256 # include <libmount/libmount.h>
257
258 /*
259  * The libmount is part of util-linux since 2.18.
260  * We use it to update utab to avoid umount would
261  * blocked in some rare case.
262  */
263 int update_utab_entry(struct mount_opts *mop)
264 {
265         struct libmnt_fs *fs = mnt_new_fs();
266         struct libmnt_update *upd;
267         int rc;
268
269         mnt_fs_set_source(fs, mop->mo_source);
270         mnt_fs_set_target(fs, mop->mo_target);
271         mnt_fs_set_fstype(fs, "lustre");
272         mnt_fs_set_attributes(fs, "lustre");
273
274         upd = mnt_new_update();
275         if (!upd)
276                 return -ENOMEM;
277
278         rc = mnt_update_set_fs(upd, mop->mo_nomtab ? MS_REMOUNT : 0, NULL, fs);
279         if (rc == 1) /* update is unnecessary */
280                 rc = 0;
281         if (rc) {
282                 fprintf(stderr,
283                         "error: failed to save utab entry: rc = %d\n", rc);
284         } else {
285                 rc = mnt_update_table(upd, NULL);
286         }
287
288         mnt_free_update(upd);
289         mnt_free_fs(fs);
290
291         return rc;
292 }
293 #else
294 int update_utab_entry(struct mount_opts *mop)
295 {
296         return 0;
297 }
298 #endif /* HAVE_LIBMOUNT */
299
300 int update_mtab_entry(char *spec, char *mtpt, char *type, char *opts,
301                       int flags, int freq, int pass)
302 {
303         FILE *fp;
304         struct mntent mnt;
305         int rc = 0;
306
307         /* Don't update mtab if it is linked to any file in /proc direcotry.*/
308         if (mtab_is_proc(MOUNTED))
309                 return 0;
310
311         mnt.mnt_fsname = spec;
312         mnt.mnt_dir = mtpt;
313         mnt.mnt_type = type;
314         mnt.mnt_opts = opts ? opts : "";
315         mnt.mnt_freq = freq;
316         mnt.mnt_passno = pass;
317
318         fp = setmntent(MOUNTED, "a+");
319         if (!fp) {
320                 fprintf(stderr, "%s: setmntent(%s): %s\n",
321                         progname, MOUNTED, strerror(errno));
322                 rc = 16;
323         } else {
324                 if ((addmntent(fp, &mnt)) == 1) {
325                         fprintf(stderr, "%s: addmntent: %s\n",
326                                 progname, strerror(errno));
327                         rc = 16;
328                 }
329                 endmntent(fp);
330         }
331
332         return rc;
333 }
334
335 /* Search for opt in mntlist, returning true if found.
336  */
337 static int in_mntlist(char *opt, char *mntlist)
338 {
339         char *ml, *mlp, *item, *ctx = NULL;
340
341         if (!(ml = strdup(mntlist))) {
342                 fprintf(stderr, "%s: out of memory\n", progname);
343                 exit(1);
344         }
345         mlp = ml;
346         while ((item = strtok_r(mlp, ",", &ctx))) {
347                 if (!strcmp(opt, item))
348                         break;
349                 mlp = NULL;
350         }
351         free(ml);
352         return (item != NULL);
353 }
354
355 /* Issue a message on stderr for every item in wanted_mountopts that is not
356  * present in mountopts.  The justwarn boolean toggles between error and
357  * warning message.  Return an error count.
358  */
359 int check_mountfsoptions(char *mountopts, char *wanted_mountopts)
360 {
361         char *ml, *mlp, *item, *ctx = NULL;
362         int errors = 0;
363
364         if (!(ml = strdup(wanted_mountopts))) {
365                 fprintf(stderr, "%s: out of memory\n", progname);
366                 exit(1);
367         }
368         mlp = ml;
369         while ((item = strtok_r(mlp, ",", &ctx))) {
370                 if (!in_mntlist(item, mountopts)) {
371                         fprintf(stderr, "%s: Error: mandatory mount option"
372                                 " '%s' is missing\n", progname, item);
373                         errors++;
374                 }
375                 mlp = NULL;
376         }
377         free(ml);
378         return errors;
379 }
380
381 /* Trim embedded white space, leading and trailing commas from string s.
382  */
383 void trim_mountfsoptions(char *s)
384 {
385         char *p;
386
387         for (p = s; *p; ) {
388                 if (isspace(*p)) {
389                         memmove(p, p + 1, strlen(p + 1) + 1);
390                         continue;
391                 }
392                 p++;
393         }
394
395         while (s[0] == ',')
396                 memmove(&s[0], &s[1], strlen(&s[1]) + 1);
397
398         p = s + strlen(s) - 1;
399         while (p >= s && *p == ',')
400                 *p-- = '\0';
401 }
402
403 #ifdef HAVE_SERVER_SUPPORT
404 /* Setup a file in the first unused loop_device */
405 int loop_setup(struct mkfs_opts *mop)
406 {
407         char loop_base[20];
408         char l_device[64];
409         int i, ret = 0;
410
411         /* Figure out the loop device names */
412         if (!access("/dev/loop0", F_OK | R_OK) ||
413             !access("/dev/loop-control", F_OK | R_OK)) {
414                 strcpy(loop_base, "/dev/loop\0");
415         } else if (!access("/dev/loop/0", F_OK | R_OK)) {
416                 strcpy(loop_base, "/dev/loop/\0");
417         } else {
418                 fprintf(stderr, "%s: can't access loop devices\n", progname);
419                 return EACCES;
420         }
421
422         /* Find unused loop device */
423         for (i = 0; i < MAX_LOOP_DEVICES; i++) {
424                 char cmd[PATH_MAX];
425                 int cmdsz = sizeof(cmd);
426
427                 ret = open("/dev/loop-control", O_RDWR);
428                 if (ret < 0) {
429                         fprintf(stderr, "%s: can't access loop control\n",
430                                 progname);
431                         return EACCES;
432                 }
433                 /* find or allocate a free loop device to use */
434                 i = ioctl(ret, LOOP_CTL_GET_FREE);
435                 close(ret);
436                 if (i < 0) {
437                         fprintf(stderr, "%s: access loop control error\n",
438                                 progname);
439                         return EACCES;
440                 }
441                 sprintf(l_device, "%s%d", loop_base, i);
442
443                 snprintf(cmd, cmdsz, "losetup %s > /dev/null 2>&1", l_device);
444                 ret = system(cmd);
445
446                 /* losetup gets 1 (ret=256) for non-set-up device */
447                 if (ret) {
448                         /* Set up a loopback device to our file */
449                         snprintf(cmd, cmdsz, "losetup %s %s", l_device,
450                                  mop->mo_device);
451                         ret = run_command(cmd, cmdsz);
452                         if (ret == 256)
453                                 /*
454                                  * someone else picked up this loop device
455                                  * behind our back
456                                  */
457                                 continue;
458                         if (ret) {
459                                 fprintf(stderr, "%s: error %d on losetup: %s\n",
460                                         progname, ret,
461                                         ret >= 0 ? strerror(ret) : "");
462                                 return ret;
463                         }
464                         strscpy(mop->mo_loopdev, l_device,
465                                 sizeof(mop->mo_loopdev));
466                         return ret;
467                 }
468         }
469
470         fprintf(stderr, "%s: out of loop devices!\n", progname);
471         return EMFILE;
472 }
473
474 int loop_cleanup(struct mkfs_opts *mop)
475 {
476         char cmd[150];
477         int ret = 0;
478
479         if ((mop->mo_flags & MO_IS_LOOP) && *mop->mo_loopdev) {
480                 int tries;
481
482                 sprintf(cmd, "losetup -d %s", mop->mo_loopdev);
483                 for (tries = 0; tries < 3; tries++) {
484                         ret = run_command(cmd, sizeof(cmd));
485                         if (ret == 0)
486                                 break;
487                         sleep(1);
488                 }
489         }
490
491         if (ret != 0)
492                 fprintf(stderr, "cannot cleanup %s: rc = %d\n",
493                         mop->mo_loopdev, ret);
494         return ret;
495 }
496
497 int loop_format(struct mkfs_opts *mop)
498 {
499         int fd;
500
501         if (mop->mo_device_kb == 0) {
502                 fatal();
503                 fprintf(stderr,
504                         "loop device requires a --device-size= param\n");
505                 return EINVAL;
506         }
507
508         fd = creat(mop->mo_device, 0600);
509         if (fd < 0) {
510                 fatal();
511                 fprintf(stderr, "%s: Unable to create backing store: %s\n",
512                         progname, strerror(errno));
513                 return errno;
514         }
515
516         if (ftruncate(fd, mop->mo_device_kb * 1024) != 0) {
517                 close(fd);
518                 fatal();
519                 fprintf(stderr, "%s: Unable to truncate backing store: %s\n",
520                         progname, strerror(errno));
521                 return errno;
522         }
523
524         close(fd);
525         return 0;
526 }
527
528 #ifdef PLUGIN_DIR
529 #define DLSYM(prefix, sym, func)                                        \
530         do {                                                            \
531                 char _fname[PATH_MAX];                                  \
532                 snprintf(_fname, sizeof(_fname), "%s_%s", prefix, #func); \
533                 sym->func = (typeof(sym->func))dlsym(sym->dl_handle, _fname); \
534         } while (0)
535 #endif /* PLUGIN_DIR */
536
537 /**
538  * Load plugin for a given mount_type from ${pkglibdir}/mount_osd_FSTYPE.so and
539  * return struct of function pointers (will be freed in unloack_backfs_module).
540  *
541  * \param[in] mount_type        Mount type to load module for.
542  * \retval Value of backfs_ops struct
543  * \retval NULL if no module exists
544  */
545 struct module_backfs_ops *load_backfs_module(enum ldd_mount_type mount_type)
546 {
547         struct module_backfs_ops *ops;
548 #ifdef PLUGIN_DIR
549         char *error, filename[PATH_MAX], fsname[512], *name;
550         void *handle;
551
552         /*
553          * This deals with duplicate ldd_mount_types resolving to same OSD layer
554          * plugin (e.g. ext3/ldiskfs/ldiskfs2 all being ldiskfs)
555          */
556         strncpy(fsname, mt_type(mount_type), sizeof(fsname));
557         name = fsname + sizeof("osd-") - 1;
558
559         /* change osd- to osd_ */
560         fsname[sizeof("osd-") - 2] = '_';
561
562         snprintf(filename, sizeof(filename), PLUGIN_DIR"/mount_%s.so", fsname);
563
564         handle = dlopen(filename, RTLD_LAZY);
565
566         /*
567          * Check for $LUSTRE environment variable from test-framework.
568          * This allows using locally built modules to be used.
569          */
570         if (!handle) {
571                 char *dirname;
572
573                 dirname = getenv("LUSTRE");
574                 if (dirname) {
575                         snprintf(filename, sizeof(filename),
576                                  "%s/utils/mount_%s.so",
577                                  dirname, fsname);
578                         handle = dlopen(filename, RTLD_LAZY);
579                 }
580         }
581
582         /* Do not clutter up console with missing types */
583         if (!handle)
584                 return NULL;
585
586         ops = malloc(sizeof(*ops));
587         if (!ops) {
588                 dlclose(handle);
589                 return NULL;
590         }
591
592         ops->dl_handle = handle;
593         dlerror(); /* Clear any existing error */
594
595         DLSYM(name, ops, init);
596         DLSYM(name, ops, fini);
597         DLSYM(name, ops, read_ldd);
598         DLSYM(name, ops, write_ldd);
599         DLSYM(name, ops, erase_ldd);
600         DLSYM(name, ops, print_ldd_params);
601         DLSYM(name, ops, is_lustre);
602         DLSYM(name, ops, make_lustre);
603         DLSYM(name, ops, prepare_lustre);
604         DLSYM(name, ops, tune_lustre);
605         DLSYM(name, ops, label_lustre);
606         DLSYM(name, ops, rename_fsname);
607         DLSYM(name, ops, enable_quota);
608
609         error = dlerror();
610         if (error) {
611                 fatal();
612                 fprintf(stderr, "%s\n", error);
613                 dlclose(handle);
614                 free(ops);
615                 return NULL;
616         }
617
618         /* optional methods */
619         DLSYM(name, ops, fix_mountopts);
620 #else
621         switch (mount_type) {
622 #ifdef HAVE_LDISKFS_OSD
623         case LDD_MT_LDISKFS:
624                 ops = &ldiskfs_ops;
625                 break;
626 #endif /* HAVE_LDISKFS_OSD */
627 #ifdef HAVE_ZFS_OSD
628         case LDD_MT_ZFS:
629                 ops = &zfs_ops;
630                 break;
631 #endif /* HAVE_ZFS_OSD */
632         default:
633                 ops = NULL;
634                 break;
635         }
636 #endif
637         return ops;
638 }
639
640 /**
641  * Unload plugin and free backfs_ops structure. Must be called the same number
642  * of times as load_backfs_module is.
643  */
644 void unload_backfs_module(struct module_backfs_ops *ops)
645 {
646 #ifdef PLUGIN_DIR
647         if (!ops)
648                 return;
649
650         dlclose(ops->dl_handle);
651         free(ops);
652 #endif
653 }
654
655 /* Return true if backfs_ops has operations for the given mount_type. */
656 int backfs_mount_type_okay(enum ldd_mount_type mount_type)
657 {
658         if (mount_type >= LDD_MT_LAST || mount_type < 0) {
659                 fatal();
660                 fprintf(stderr, "fs type out of range %d\n", mount_type);
661                 return 0;
662         }
663         if (!backfs_ops[mount_type]) {
664                 fatal();
665                 fprintf(stderr, "unhandled/unloaded fs type %d '%s'\n",
666                         mount_type, mt_str(mount_type));
667                 return 0;
668         }
669         return 1;
670 }
671
672 /* Write the server config files */
673 int osd_write_ldd(struct mkfs_opts *mop)
674 {
675         struct lustre_disk_data *ldd = &mop->mo_ldd;
676         int ret;
677
678         if (backfs_mount_type_okay(ldd->ldd_mount_type))
679                 ret = backfs_ops[ldd->ldd_mount_type]->write_ldd(mop);
680         else
681                 ret = EINVAL;
682
683         return ret;
684 }
685
686 /* Read the server config files */
687 int osd_read_ldd(char *dev, struct lustre_disk_data *ldd)
688 {
689         int ret;
690
691         if (backfs_mount_type_okay(ldd->ldd_mount_type))
692                 ret = backfs_ops[ldd->ldd_mount_type]->read_ldd(dev, ldd);
693         else
694                 ret = EINVAL;
695
696         return ret;
697 }
698
699 /* Erase param from the server config files */
700 int osd_erase_ldd(struct mkfs_opts *mop, char *param)
701 {
702         struct lustre_disk_data *ldd = &mop->mo_ldd;
703         int ret;
704
705         if (backfs_mount_type_okay(ldd->ldd_mount_type))
706                 ret = backfs_ops[ldd->ldd_mount_type]->erase_ldd(mop, param);
707         else
708                 ret = EINVAL;
709
710         return ret;
711 }
712
713 /* Print ldd_params */
714 void osd_print_ldd_params(struct mkfs_opts *mop)
715 {
716         struct lustre_disk_data *ldd = &mop->mo_ldd;
717
718         if (backfs_mount_type_okay(ldd->ldd_mount_type))
719                 backfs_ops[ldd->ldd_mount_type]->print_ldd_params(mop);
720 }
721
722 /* Was this device formatted for Lustre */
723 int osd_is_lustre(char *dev, unsigned int *mount_type)
724 {
725         int i;
726
727         vprint("checking for existing Lustre data: ");
728
729         for (i = 0; i < LDD_MT_LAST; ++i) {
730                 if (backfs_ops[i] &&
731                     backfs_ops[i]->is_lustre(dev, mount_type)) {
732                         vprint("found\n");
733                         return 1;
734                 }
735         }
736
737         vprint("not found\n");
738         return 0;
739 }
740
741 /* Build fs according to type */
742 int osd_make_lustre(struct mkfs_opts *mop)
743 {
744         struct lustre_disk_data *ldd = &mop->mo_ldd;
745         int ret;
746
747         if (backfs_mount_type_okay(ldd->ldd_mount_type))
748                 ret = backfs_ops[ldd->ldd_mount_type]->make_lustre(mop);
749         else
750                 ret = EINVAL;
751
752         return ret;
753 }
754
755 int osd_prepare_lustre(struct mkfs_opts *mop,
756                        char *wanted_mountopts, size_t len)
757 {
758         struct lustre_disk_data *ldd = &mop->mo_ldd;
759         int ret;
760
761         if (backfs_mount_type_okay(ldd->ldd_mount_type))
762                 ret = backfs_ops[ldd->ldd_mount_type]->prepare_lustre(mop,
763                                                         wanted_mountopts, len);
764         else
765                 ret = EINVAL;
766
767         return ret;
768 }
769
770 int osd_fix_mountopts(struct mkfs_opts *mop, char *mountopts, size_t len)
771 {
772         struct lustre_disk_data *ldd = &mop->mo_ldd;
773
774         if (!backfs_mount_type_okay(ldd->ldd_mount_type))
775                 return EINVAL;
776
777         if (!backfs_ops[ldd->ldd_mount_type]->fix_mountopts)
778                 return 0;
779
780         return backfs_ops[ldd->ldd_mount_type]->fix_mountopts(mop, mountopts,
781                                                               len);
782 }
783
784 int osd_tune_lustre(char *dev, struct mount_opts *mop)
785 {
786         struct lustre_disk_data *ldd = &mop->mo_ldd;
787         int ret;
788
789         if (backfs_mount_type_okay(ldd->ldd_mount_type))
790                 ret = backfs_ops[ldd->ldd_mount_type]->tune_lustre(dev, mop);
791         else
792                 ret = EINVAL;
793
794         return ret;
795 }
796
797 int osd_label_lustre(struct mount_opts *mop)
798 {
799         struct lustre_disk_data *ldd = &mop->mo_ldd;
800         int ret;
801
802         if (backfs_mount_type_okay(ldd->ldd_mount_type))
803                 ret = backfs_ops[ldd->ldd_mount_type]->label_lustre(mop);
804         else
805                 ret = EINVAL;
806
807         return ret;
808 }
809
810 /* Rename filesystem fsname */
811 int osd_rename_fsname(struct mkfs_opts *mop, const char *oldname)
812 {
813         struct lustre_disk_data *ldd = &mop->mo_ldd;
814         int ret;
815
816         if (backfs_mount_type_okay(ldd->ldd_mount_type))
817                 ret = backfs_ops[ldd->ldd_mount_type]->rename_fsname(mop,
818                                                                      oldname);
819         else
820                 ret = EINVAL;
821
822         return ret;
823 }
824
825 /* Enable quota accounting */
826 int osd_enable_quota(struct mkfs_opts *mop)
827 {
828         struct lustre_disk_data *ldd = &mop->mo_ldd;
829         int ret;
830
831         if (backfs_mount_type_okay(ldd->ldd_mount_type))
832                 ret = backfs_ops[ldd->ldd_mount_type]->enable_quota(mop);
833         else
834                 ret = EINVAL;
835
836         return ret;
837 }
838
839 int osd_init(void)
840 {
841         int i, rc, ret = EINVAL;
842
843         for (i = 0; i < LDD_MT_LAST; ++i) {
844                 rc = 0;
845                 backfs_ops[i] = load_backfs_module(i);
846                 if (backfs_ops[i]) {
847                         rc = backfs_ops[i]->init();
848                         if (rc != 0) {
849                                 backfs_ops[i]->fini();
850                                 unload_backfs_module(backfs_ops[i]);
851                                 backfs_ops[i] = NULL;
852                         } else {
853                                 ret = 0;
854                         }
855                 }
856         }
857
858         return ret;
859 }
860
861 void osd_fini(void)
862 {
863         int i;
864
865         for (i = 0; i < LDD_MT_LAST; ++i) {
866                 if (backfs_ops[i]) {
867                         backfs_ops[i]->fini();
868                         unload_backfs_module(backfs_ops[i]);
869                         backfs_ops[i] = NULL;
870                 }
871         }
872 }
873
874 __u64 get_device_size(char *device)
875 {
876         int ret, fd;
877         __u64 size = 0;
878
879         fd = open(device, O_RDONLY);
880         if (fd < 0) {
881                 fprintf(stderr, "%s: cannot open %s: %s\n",
882                         progname, device, strerror(errno));
883                 return 0;
884         }
885
886 #ifdef BLKGETSIZE64
887         /* size in bytes. bz5831 */
888         ret = ioctl(fd, BLKGETSIZE64, (void *)&size);
889 #else
890         {
891                 __u32 lsize = 0;
892                 /* size in blocks */
893                 ret = ioctl(fd, BLKGETSIZE, (void *)&lsize);
894                 size = (__u64)lsize * 512;
895         }
896 #endif
897         close(fd);
898         if (ret < 0) {
899                 fprintf(stderr, "%s: size ioctl failed: %s\n",
900                         progname, strerror(errno));
901                 return 0;
902         }
903
904         vprint("device size = %juMB\n", (uintmax_t)(size >> 20));
905         /* return value in KB */
906         return size >> 10;
907 }
908 #endif
909
910 int file_create(char *path, __u64 size)
911 {
912         __u64 size_max;
913         int ret;
914         int fd;
915
916         /*
917          * Since "size" is in KB, the file offset it represents could overflow
918          * off_t.
919          */
920         size_max = (off_t)1 << (_FILE_OFFSET_BITS - 1 - 10);
921         if (size >= size_max) {
922                 fprintf(stderr,
923                         "%s: %ju KB: Backing store size must be smaller than %ju KB\n",
924                         progname, (uintmax_t)size, (uintmax_t)size_max);
925                 return EFBIG;
926         }
927
928         ret = access(path, F_OK);
929         if (ret == 0) {
930                 ret = unlink(path);
931                 if (ret != 0)
932                         return errno;
933         }
934
935         fd = creat(path, 0600);
936         if (fd < 0) {
937                 fatal();
938                 fprintf(stderr, "%s: Unable to create backing store: %s\n",
939                         progname, strerror(errno));
940                 return errno;
941         }
942
943         ret = ftruncate(fd, size * 1024);
944         close(fd);
945         if (ret != 0) {
946                 fatal();
947                 fprintf(stderr, "%s: Unable to truncate backing store: %s\n",
948                         progname, strerror(errno));
949                 return errno;
950         }
951
952         return 0;
953 }
954
955 #ifdef HAVE_SERVER_SUPPORT
956 struct lustre_cfg_entry {
957         struct list_head lce_list;
958         char             lce_name[0];
959 };
960
961 static struct lustre_cfg_entry *lustre_cfg_entry_init(const char *name)
962 {
963         struct lustre_cfg_entry *lce;
964         int len = strlen(name) + 1;
965
966         lce = malloc(sizeof(*lce) + len);
967         if (lce) {
968                 INIT_LIST_HEAD(&lce->lce_list);
969                 memcpy(lce->lce_name, name, len);
970         }
971
972         return lce;
973 }
974
975 static void lustre_cfg_entry_fini(struct lustre_cfg_entry *lce)
976 {
977         free(lce);
978 }
979
980 int lustre_rename_fsname(struct mkfs_opts *mop, const char *mntpt,
981                          const char *oldname)
982 {
983         struct lustre_disk_data *ldd = &mop->mo_ldd;
984         struct lr_server_data lsd;
985         char filepnm[132];
986         char cfg_dir[128];
987         DIR *dir = NULL;
988         struct dirent64 *dirent;
989         struct lustre_cfg_entry *lce;
990         struct list_head cfg_list;
991         int old_namelen = strlen(oldname);
992         int new_namelen = strlen(ldd->ldd_fsname);
993         int ret;
994         int fd;
995
996         INIT_LIST_HEAD(&cfg_list);
997
998         snprintf(filepnm, sizeof(filepnm), "%s/%s", mntpt, LAST_RCVD);
999         fd = open(filepnm, O_RDWR);
1000         if (fd < 0) {
1001                 if (errno == ENOENT)
1002                         goto config;
1003
1004                 if (errno != 0)
1005                         ret = errno;
1006                 else
1007                         ret = fd;
1008                 fprintf(stderr, "Unable to open %s: %s\n",
1009                         filepnm, strerror(ret));
1010                 return ret;
1011         }
1012
1013         ret = read(fd, &lsd, sizeof(lsd));
1014         if (ret != sizeof(lsd)) {
1015                 if (errno != 0)
1016                         ret = errno;
1017                 fprintf(stderr, "Unable to read %s: %s\n",
1018                         filepnm, strerror(ret));
1019                 close(fd);
1020                 return ret;
1021         }
1022
1023         ret = lseek(fd, 0, SEEK_SET);
1024         if (ret) {
1025                 if (errno != 0)
1026                         ret = errno;
1027                 fprintf(stderr, "Unable to lseek %s: %s\n",
1028                         filepnm, strerror(ret));
1029                 close(fd);
1030                 return ret;
1031         }
1032
1033         /* replace fsname in lr_server_data::lsd_uuid. */
1034         if (old_namelen > new_namelen)
1035                 memmove(lsd.lsd_uuid + new_namelen,
1036                         lsd.lsd_uuid + old_namelen,
1037                         sizeof(lsd.lsd_uuid) - old_namelen);
1038         else if (old_namelen < new_namelen)
1039                 memmove(lsd.lsd_uuid + new_namelen,
1040                         lsd.lsd_uuid + old_namelen,
1041                         sizeof(lsd.lsd_uuid) - new_namelen);
1042         memcpy(lsd.lsd_uuid, ldd->ldd_fsname, new_namelen);
1043         ret = write(fd, &lsd, sizeof(lsd));
1044         if (ret != sizeof(lsd)) {
1045                 if (errno != 0)
1046                         ret = errno;
1047                 fprintf(stderr, "Unable to write %s: %s\n",
1048                         filepnm, strerror(ret));
1049                 close(fd);
1050                 return ret;
1051         }
1052
1053         close(fd);
1054
1055 config:
1056         snprintf(cfg_dir, sizeof(cfg_dir), "%s/%s", mntpt, MOUNT_CONFIGS_DIR);
1057         dir = opendir(cfg_dir);
1058         if (!dir) {
1059                 if (errno != 0)
1060                         ret = errno;
1061                 else
1062                         ret = EINVAL;
1063                 fprintf(stderr, "Unable to opendir %s: %s\n",
1064                         cfg_dir, strerror(ret));
1065                 return ret;
1066         }
1067
1068         while ((dirent = readdir64(dir)) != NULL) {
1069                 char *ptr;
1070
1071                 if (strlen(dirent->d_name) <= old_namelen)
1072                         continue;
1073
1074                 ptr = strrchr(dirent->d_name, '-');
1075                 if (!ptr || (ptr - dirent->d_name) != old_namelen)
1076                         continue;
1077
1078                 if (strncmp(dirent->d_name, oldname, old_namelen) != 0)
1079                         continue;
1080
1081                 lce = lustre_cfg_entry_init(dirent->d_name);
1082                 if (!lce) {
1083                         if (errno != 0)
1084                                 ret = errno;
1085                         else
1086                                 ret = EINVAL;
1087
1088                         fprintf(stderr, "Fail to init item for %s: %s\n",
1089                                 dirent->d_name, strerror(ret));
1090                         goto out;
1091                 }
1092
1093                 list_add(&lce->lce_list, &cfg_list);
1094         }
1095
1096         closedir(dir);
1097         dir = NULL;
1098         ret = 0;
1099
1100         while (!list_empty(&cfg_list) && ret == 0) {
1101                 lce = list_entry(cfg_list.next, struct lustre_cfg_entry,
1102                                  lce_list);
1103                 list_del(&lce->lce_list);
1104                 snprintf(filepnm, sizeof(filepnm), "%s/%s", cfg_dir,
1105                          lce->lce_name);
1106                 if (IS_MGS(ldd))
1107                         /*
1108                          * Store the new fsname in the XATTR_TARGET_RENAME EA.
1109                          * When the MGS start, it will scan config logs, and
1110                          * for the ones which have the XATTR_TARGET_RENAME EA,
1111                          * it will replace old fsname with the new fsname in
1112                          * the config log by some shared kernel level config
1113                          * logs {fork,erase} functionalities automatically.
1114                          */
1115                         ret = setxattr(filepnm, XATTR_TARGET_RENAME,
1116                                        ldd->ldd_fsname,
1117                                        strlen(ldd->ldd_fsname), 0);
1118                 else
1119                         ret = unlink(filepnm);
1120
1121                 if (ret) {
1122                         if (errno != 0)
1123                                 ret = errno;
1124
1125                         fprintf(stderr, "Fail to %s %s: %s\n",
1126                                 IS_MGS(ldd) ? "setxattr" : "unlink",
1127                                 filepnm, strerror(ret));
1128                 }
1129
1130                 lustre_cfg_entry_fini(lce);
1131         }
1132
1133 out:
1134         if (dir)
1135                 closedir(dir);
1136
1137         while (!list_empty(&cfg_list)) {
1138                 lce = list_entry(cfg_list.next, struct lustre_cfg_entry,
1139                                  lce_list);
1140                 list_del(&lce->lce_list);
1141                 lustre_cfg_entry_fini(lce);
1142         }
1143
1144         return ret;
1145 }
1146 #endif /* HAVE_SERVER_SUPPORT */
1147
1148 #ifdef HAVE_GSS
1149 #ifdef HAVE_OPENSSL_SSK
1150 int load_shared_keys(struct mount_opts *mop)
1151 {
1152         DIR *dir;
1153         struct dirent *dentry;
1154         struct stat sbuf;
1155         char fullpath[PATH_MAX];
1156         char *path = mop->mo_skpath;
1157         int rc;
1158
1159         /* init logging */
1160         sk_init_logging(NULL, 1, 1);
1161
1162         rc = stat(path, &sbuf);
1163         if (rc < 0) {
1164                 fprintf(stderr, "stat() failed for key %s: %s\n", path,
1165                         strerror(errno));
1166                 return -errno;
1167         }
1168
1169         /* Load individual keys or a directory of them */
1170         if (S_ISREG(sbuf.st_mode)) {
1171                 return sk_load_keyfile(path);
1172         } else if (!S_ISDIR(sbuf.st_mode)) {
1173                 fprintf(stderr, "Invalid shared key path: %s\n", path);
1174                 return -ENOKEY;
1175         }
1176
1177         dir = opendir(path);
1178         if (!dir) {
1179                 fprintf(stderr, "Unable to open shared key directory: %s\n",
1180                         path);
1181                 return -ENOENT;
1182         }
1183
1184         /*
1185          * Loop through the files in the directory attempting to load them.
1186          * Any issue with loading the keyfile is treated as an error although
1187          * the loop continues until all files have been attempted.  This will
1188          * allow all errors be reported at once rather then requiring
1189          * incremental corrections to fix each one and try again.
1190          */
1191         while ((dentry = readdir(dir)) != NULL) {
1192                 if (strcmp(".", dentry->d_name) == 0 ||
1193                     strcmp("..", dentry->d_name) == 0)
1194                         continue;
1195
1196                 rc = snprintf(fullpath, PATH_MAX, "%s/%s", path,
1197                               dentry->d_name);
1198                 if (rc >= PATH_MAX) {
1199                         fprintf(stderr, "Path too long for %s/%s\n",
1200                                 path, dentry->d_name);
1201                         rc = -ENAMETOOLONG;
1202                         continue;
1203                 }
1204
1205                 rc = stat(fullpath, &sbuf);
1206                 if (rc < 0) {
1207                         fprintf(stderr, "Unable to stat %s: %s\n", fullpath,
1208                                 strerror(errno));
1209                         rc = -errno;
1210                         continue;
1211                 }
1212
1213                 if (!S_ISREG(sbuf.st_mode))
1214                         continue;
1215
1216                 rc = sk_load_keyfile(fullpath);
1217                 if (rc)
1218                         fprintf(stderr, "Failed to load key %s\n", fullpath);
1219         }
1220         closedir(dir);
1221
1222         return rc;
1223 }
1224 #endif /* HAVE_OPENSSL_SSK */
1225 #endif /* HAVE_GSS */