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