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