Whamcloud - gitweb
LU-3289 gss: Add option for loading keys during mount
[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.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2012, 2014, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  */
36
37 #if HAVE_CONFIG_H
38 #  include "config.h"
39 #endif /* HAVE_CONFIG_H */
40
41 #include <inttypes.h>
42 #include <limits.h>
43 #include <mntent.h>
44 #include <stdio.h>
45 #include <errno.h>
46 #include <string.h>
47 #include <unistd.h>
48 #include <config.h>
49 #include <lustre_disk.h>
50 #include <lustre_ver.h>
51 #include <sys/mount.h>
52 #include <sys/stat.h>
53 #include <sys/utsname.h>
54 #include <linux/loop.h>
55 #include <sys/types.h>
56 #include <dirent.h>
57 #include <dlfcn.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 #define PROC_DIR        "/proc/"
239 static int mtab_is_proc(const char *mtab)
240 {
241         char path[16];
242
243         if (readlink(mtab, path, sizeof(path)) < 0)
244                 return 0;
245
246         if (strncmp(path, PROC_DIR, strlen(PROC_DIR)))
247                 return 0;
248
249         return 1;
250 }
251
252 #ifdef HAVE_LIBMOUNT
253
254 # include <libmount/libmount.h>
255
256 /*
257  * The libmount is part of util-linux since 2.18.
258  * We use it to update utab to avoid umount would
259  * blocked in some rare case.
260  */
261 int update_utab_entry(struct mount_opts *mop)
262 {
263         struct libmnt_fs *fs = mnt_new_fs();
264         struct libmnt_update *upd;
265         int rc;
266
267         mnt_fs_set_source(fs, mop->mo_source);
268         mnt_fs_set_target(fs, mop->mo_target);
269         mnt_fs_set_fstype(fs, "lustre");
270         mnt_fs_set_attributes(fs, "lustre");
271
272         upd = mnt_new_update();
273         if (!upd)
274                 return -ENOMEM;
275
276         rc = mnt_update_set_fs(upd, mop->mo_nomtab ? MS_REMOUNT : 0, NULL, fs);
277         if (rc == 1) /* update is unnecessary */
278                 rc = 0;
279         if (rc) {
280                 fprintf(stderr,
281                         "error: failed to save utab entry: rc = %d\n", rc);
282         } else {
283                 rc = mnt_update_table(upd, NULL);
284         }
285
286         mnt_free_update(upd);
287         mnt_free_fs(fs);
288
289         return rc;
290 }
291 #else
292 int update_utab_entry(struct mount_opts *mop)
293 {
294         return 0;
295 }
296 #endif /* HAVE_LIBMOUNT */
297
298 int update_mtab_entry(char *spec, char *mtpt, char *type, char *opts,
299                 int flags, int freq, int pass)
300 {
301         FILE *fp;
302         struct mntent mnt;
303         int rc = 0;
304
305         /* Don't update mtab if it is linked to any file in /proc direcotry.*/
306         if (mtab_is_proc(MOUNTED))
307                 return 0;
308
309         mnt.mnt_fsname = spec;
310         mnt.mnt_dir = mtpt;
311         mnt.mnt_type = type;
312         mnt.mnt_opts = opts ? opts : "";
313         mnt.mnt_freq = freq;
314         mnt.mnt_passno = pass;
315
316         fp = setmntent(MOUNTED, "a+");
317         if (fp == NULL) {
318                 fprintf(stderr, "%s: setmntent(%s): %s:",
319                         progname, MOUNTED, strerror (errno));
320                 rc = 16;
321         } else {
322                 if ((addmntent(fp, &mnt)) == 1) {
323                         fprintf(stderr, "%s: addmntent: %s:",
324                                 progname, strerror (errno));
325                         rc = 16;
326                 }
327                 endmntent(fp);
328         }
329
330         return rc;
331 }
332
333 /* Search for opt in mntlist, returning true if found.
334  */
335 static int in_mntlist(char *opt, char *mntlist)
336 {
337         char *ml, *mlp, *item, *ctx = NULL;
338
339         if (!(ml = strdup(mntlist))) {
340                 fprintf(stderr, "%s: out of memory\n", progname);
341                 exit(1);
342         }
343         mlp = ml;
344         while ((item = strtok_r(mlp, ",", &ctx))) {
345                 if (!strcmp(opt, item))
346                         break;
347                 mlp = NULL;
348         }
349         free(ml);
350         return (item != NULL);
351 }
352
353 /* Issue a message on stderr for every item in wanted_mountopts that is not
354  * present in mountopts.  The justwarn boolean toggles between error and
355  * warning message.  Return an error count.
356  */
357 int check_mountfsoptions(char *mountopts, char *wanted_mountopts)
358 {
359         char *ml, *mlp, *item, *ctx = NULL;
360         int errors = 0;
361
362         if (!(ml = strdup(wanted_mountopts))) {
363                 fprintf(stderr, "%s: out of memory\n", progname);
364                 exit(1);
365         }
366         mlp = ml;
367         while ((item = strtok_r(mlp, ",", &ctx))) {
368                 if (!in_mntlist(item, mountopts)) {
369                         fprintf(stderr, "%s: Error: mandatory mount option"
370                                 " '%s' is missing\n", progname, item);
371                         errors++;
372                 }
373                 mlp = NULL;
374         }
375         free(ml);
376         return errors;
377 }
378
379 /* Trim embedded white space, leading and trailing commas from string s.
380  */
381 void trim_mountfsoptions(char *s)
382 {
383         char *p;
384
385         for (p = s; *p; ) {
386                 if (isspace(*p)) {
387                         memmove(p, p + 1, strlen(p + 1) + 1);
388                         continue;
389                 }
390                 p++;
391         }
392
393         while (s[0] == ',')
394                 memmove(&s[0], &s[1], strlen(&s[1]) + 1);
395
396         p = s + strlen(s) - 1;
397         while (p >= s && *p == ',')
398                 *p-- = '\0';
399 }
400
401 /* Setup a file in the first unused loop_device */
402 int loop_setup(struct mkfs_opts *mop)
403 {
404         char loop_base[20];
405         char l_device[64];
406         int i, ret = 0;
407
408         /* Figure out the loop device names */
409         if (!access("/dev/loop0", F_OK | R_OK) ||
410             !access("/dev/loop-control", F_OK | R_OK)) {
411                 strcpy(loop_base, "/dev/loop\0");
412         } else if (!access("/dev/loop/0", F_OK | R_OK)) {
413                 strcpy(loop_base, "/dev/loop/\0");
414         } else {
415                 fprintf(stderr, "%s: can't access loop devices\n", progname);
416                 return EACCES;
417         }
418
419         /* Find unused loop device */
420         for (i = 0; i < MAX_LOOP_DEVICES; i++) {
421                 char cmd[PATH_MAX];
422                 int cmdsz = sizeof(cmd);
423
424 #ifdef HAVE_LOOP_CTL_GET_FREE
425                 ret = open("/dev/loop-control", O_RDWR);
426                 if (ret < 0) {
427                         fprintf(stderr, "%s: can't access loop control\n", progname);
428                         return EACCES;
429                 }
430                 /* find or allocate a free loop device to use */
431                 i = ioctl(ret, LOOP_CTL_GET_FREE);
432                 close(ret);
433                 if (i < 0) {
434                         fprintf(stderr, "%s: access loop control error\n", progname);
435                         return EACCES;
436                 }
437                 sprintf(l_device, "%s%d", loop_base, i);
438 #else
439                 sprintf(l_device, "%s%d", loop_base, i);
440                 if (access(l_device, F_OK | R_OK))
441                         break;
442 #endif
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                                 /* someone else picked up this loop device
454                                  * behind our back */
455                                 continue;
456                         if (ret) {
457                                 fprintf(stderr, "%s: error %d on losetup: %s\n",
458                                         progname, ret,
459                                         ret >= 0 ? strerror(ret) : "");
460                                 return ret;
461                         }
462                         strscpy(mop->mo_loopdev, l_device,
463                                 sizeof(mop->mo_loopdev));
464                         return ret;
465                 }
466         }
467
468         fprintf(stderr, "%s: out of loop devices!\n", progname);
469         return EMFILE;
470 }
471
472 int loop_cleanup(struct mkfs_opts *mop)
473 {
474         char cmd[150];
475         int ret = 0;
476
477         if ((mop->mo_flags & MO_IS_LOOP) && *mop->mo_loopdev) {
478                 int tries;
479
480                 sprintf(cmd, "losetup -d %s", mop->mo_loopdev);
481                 for (tries = 0; tries < 3; tries++) {
482                         ret = run_command(cmd, sizeof(cmd));
483                         if (ret == 0)
484                                 break;
485                         sleep(1);
486                 }
487         }
488
489         if (ret != 0)
490                 fprintf(stderr, "cannot cleanup %s: rc = %d\n",
491                         mop->mo_loopdev, ret);
492         return ret;
493 }
494
495 int loop_format(struct mkfs_opts *mop)
496 {
497         int fd;
498
499         if (mop->mo_device_kb == 0) {
500                 fatal();
501                 fprintf(stderr, "loop device requires a --device-size= "
502                         "param\n");
503                 return EINVAL;
504         }
505
506         fd = creat(mop->mo_device, S_IRUSR|S_IWUSR);
507         if (fd < 0) {
508                 fatal();
509                 fprintf(stderr, "%s: Unable to create backing store: %s\n",
510                         progname, strerror(errno));
511                 return errno;
512         }
513
514         if (ftruncate(fd, mop->mo_device_kb * 1024) != 0) {
515                 close(fd);
516                 fatal();
517                 fprintf(stderr, "%s: Unable to truncate backing store: %s\n",
518                         progname, strerror(errno));
519                 return errno;
520         }
521
522         close(fd);
523         return 0;
524 }
525
526 #define DLSYM(prefix, sym, func)                                        \
527         do {                                                            \
528                 char _fname[64];                                        \
529                 snprintf(_fname, sizeof(_fname), "%s_%s", prefix, #func); \
530                 sym->func = (typeof(sym->func))dlsym(sym->dl_handle, _fname); \
531         } while (0)
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         void *handle;
544         char *error, filename[512], fsname[512], *name;
545         struct module_backfs_ops *ops;
546
547         /* This deals with duplicate ldd_mount_types resolving to same OSD layer
548          * plugin (e.g. ext3/ldiskfs/ldiskfs2 all being ldiskfs) */
549         strncpy(fsname, mt_type(mount_type), sizeof(fsname));
550         name = fsname + sizeof("osd-") - 1;
551
552         /* change osd- to osd_ */
553         fsname[sizeof("osd-") - 2] = '_';
554
555         snprintf(filename, sizeof(filename), PLUGIN_DIR"/mount_%s.so", fsname);
556
557         handle = dlopen(filename, RTLD_LAZY);
558
559         /* Check for $LUSTRE environment variable from test-framework.
560          * This allows using locally built modules to be used.
561          */
562         if (handle == NULL) {
563                 char *dirname;
564                 dirname = getenv("LUSTRE");
565                 if (dirname) {
566                         snprintf(filename, sizeof(filename),
567                                  "%s/utils/.libs/mount_%s.so",
568                                  dirname, fsname);
569                         handle = dlopen(filename, RTLD_LAZY);
570                 }
571         }
572
573         /* Do not clutter up console with missing types */
574         if (handle == NULL)
575                 return NULL;
576
577         ops = malloc(sizeof(*ops));
578         if (ops == NULL) {
579                 dlclose(handle);
580                 return NULL;
581         }
582
583         ops->dl_handle = handle;
584         dlerror(); /* Clear any existing error */
585
586         DLSYM(name, ops, init);
587         DLSYM(name, ops, fini);
588         DLSYM(name, ops, read_ldd);
589         DLSYM(name, ops, write_ldd);
590         DLSYM(name, ops, is_lustre);
591         DLSYM(name, ops, make_lustre);
592         DLSYM(name, ops, prepare_lustre);
593         DLSYM(name, ops, tune_lustre);
594         DLSYM(name, ops, label_lustre);
595         DLSYM(name, ops, enable_quota);
596
597         error = dlerror();
598         if (error != NULL) {
599                 fatal();
600                 fprintf(stderr, "%s\n", error);
601                 dlclose(handle);
602                 free(ops);
603                 return NULL;
604         }
605
606         /* optional methods */
607         DLSYM(name, ops, fix_mountopts);
608
609         return ops;
610 }
611
612 /**
613  * Unload plugin and free backfs_ops structure. Must be called the same number
614  * of times as load_backfs_module is.
615  */
616 void unload_backfs_module(struct module_backfs_ops *ops)
617 {
618         if (ops == NULL)
619                 return;
620
621         dlclose(ops->dl_handle);
622         free(ops);
623 }
624
625 /* Return true if backfs_ops has operations for the given mount_type. */
626 int backfs_mount_type_okay(enum ldd_mount_type mount_type)
627 {
628         if (unlikely(mount_type >= LDD_MT_LAST || mount_type < 0)) {
629                 fatal();
630                 fprintf(stderr, "fs type out of range %d\n", mount_type);
631                 return 0;
632         }
633         if (backfs_ops[mount_type] == NULL) {
634                 fatal();
635                 fprintf(stderr, "unhandled/unloaded fs type %d '%s'\n",
636                         mount_type, mt_str(mount_type));
637                 return 0;
638         }
639         return 1;
640 }
641
642 /* Write the server config files */
643 int osd_write_ldd(struct mkfs_opts *mop)
644 {
645         struct lustre_disk_data *ldd = &mop->mo_ldd;
646         int ret;
647
648         if (backfs_mount_type_okay(ldd->ldd_mount_type))
649                 ret = backfs_ops[ldd->ldd_mount_type]->write_ldd(mop);
650         else
651                 ret = EINVAL;
652
653         return ret;
654 }
655
656 /* Read the server config files */
657 int osd_read_ldd(char *dev, struct lustre_disk_data *ldd)
658 {
659         int ret;
660
661         if (backfs_mount_type_okay(ldd->ldd_mount_type))
662                 ret = backfs_ops[ldd->ldd_mount_type]->read_ldd(dev, ldd);
663         else
664                 ret = EINVAL;
665
666         return ret;
667 }
668
669 /* Was this device formatted for Lustre */
670 int osd_is_lustre(char *dev, unsigned *mount_type)
671 {
672         int i;
673
674         vprint("checking for existing Lustre data: ");
675
676         for (i = 0; i < LDD_MT_LAST; ++i) {
677                 if (backfs_ops[i] != NULL &&
678                     backfs_ops[i]->is_lustre(dev, mount_type)) {
679                         vprint("found\n");
680                         return 1;
681                 }
682         }
683
684         vprint("not found\n");
685         return 0;
686 }
687
688 /* Build fs according to type */
689 int osd_make_lustre(struct mkfs_opts *mop)
690 {
691         struct lustre_disk_data *ldd = &mop->mo_ldd;
692         int ret;
693
694         if (backfs_mount_type_okay(ldd->ldd_mount_type))
695                 ret = backfs_ops[ldd->ldd_mount_type]->make_lustre(mop);
696         else
697                 ret = EINVAL;
698
699         return ret;
700 }
701
702 int osd_prepare_lustre(struct mkfs_opts *mop,
703                        char *wanted_mountopts, size_t len)
704 {
705         struct lustre_disk_data *ldd = &mop->mo_ldd;
706         int ret;
707
708         if (backfs_mount_type_okay(ldd->ldd_mount_type))
709                 ret = backfs_ops[ldd->ldd_mount_type]->prepare_lustre(mop,
710                                                         wanted_mountopts, len);
711         else
712                 ret = EINVAL;
713
714         return ret;
715 }
716
717 int osd_fix_mountopts(struct mkfs_opts *mop, char *mountopts, size_t len)
718 {
719         struct lustre_disk_data *ldd = &mop->mo_ldd;
720
721         if (!backfs_mount_type_okay(ldd->ldd_mount_type))
722                 return EINVAL;
723
724         if (backfs_ops[ldd->ldd_mount_type]->fix_mountopts == NULL)
725                 return 0;
726
727         return backfs_ops[ldd->ldd_mount_type]->fix_mountopts(mop, mountopts,
728                                                               len);
729 }
730
731 int osd_tune_lustre(char *dev, struct mount_opts *mop)
732 {
733         struct lustre_disk_data *ldd = &mop->mo_ldd;
734         int ret;
735
736         if (backfs_mount_type_okay(ldd->ldd_mount_type))
737                 ret = backfs_ops[ldd->ldd_mount_type]->tune_lustre(dev, mop);
738         else
739                 ret = EINVAL;
740
741         return ret;
742 }
743
744 int osd_label_lustre(struct mount_opts *mop)
745 {
746         struct lustre_disk_data *ldd = &mop->mo_ldd;
747         int ret;
748
749         if (backfs_mount_type_okay(ldd->ldd_mount_type))
750                 ret = backfs_ops[ldd->ldd_mount_type]->label_lustre(mop);
751         else
752                 ret = EINVAL;
753
754         return ret;
755 }
756
757 /* Enable quota accounting */
758 int osd_enable_quota(struct mkfs_opts *mop)
759 {
760         struct lustre_disk_data *ldd = &mop->mo_ldd;
761         int ret;
762
763         if (backfs_mount_type_okay(ldd->ldd_mount_type))
764                 ret = backfs_ops[ldd->ldd_mount_type]->enable_quota(mop);
765         else
766                 ret = EINVAL;
767
768         return ret;
769 }
770
771 int osd_init(void)
772 {
773         int i, rc, ret = EINVAL;
774
775         for (i = 0; i < LDD_MT_LAST; ++i) {
776                 rc = 0;
777                 backfs_ops[i] = load_backfs_module(i);
778                 if (backfs_ops[i] != NULL)
779                         rc = backfs_ops[i]->init();
780                 if (rc != 0) {
781                         backfs_ops[i]->fini();
782                         unload_backfs_module(backfs_ops[i]);
783                         backfs_ops[i] = NULL;
784                 } else
785                         ret = 0;
786         }
787
788         return ret;
789 }
790
791 void osd_fini(void)
792 {
793         int i;
794
795         for (i = 0; i < LDD_MT_LAST; ++i) {
796                 if (backfs_ops[i] != NULL) {
797                         backfs_ops[i]->fini();
798                         unload_backfs_module(backfs_ops[i]);
799                         backfs_ops[i] = NULL;
800                 }
801         }
802 }
803
804 __u64 get_device_size(char* device)
805 {
806         int ret, fd;
807         __u64 size = 0;
808
809         fd = open(device, O_RDONLY);
810         if (fd < 0) {
811                 fprintf(stderr, "%s: cannot open %s: %s\n",
812                         progname, device, strerror(errno));
813                 return 0;
814         }
815
816 #ifdef BLKGETSIZE64
817         /* size in bytes. bz5831 */
818         ret = ioctl(fd, BLKGETSIZE64, (void*)&size);
819 #else
820         {
821                 __u32 lsize = 0;
822                 /* size in blocks */
823                 ret = ioctl(fd, BLKGETSIZE, (void*)&lsize);
824                 size = (__u64)lsize * 512;
825         }
826 #endif
827         close(fd);
828         if (ret < 0) {
829                 fprintf(stderr, "%s: size ioctl failed: %s\n",
830                         progname, strerror(errno));
831                 return 0;
832         }
833
834         vprint("device size = %juMB\n", (uintmax_t)(size >> 20));
835         /* return value in KB */
836         return size >> 10;
837 }
838
839 int file_create(char *path, __u64 size)
840 {
841         __u64 size_max;
842         int ret;
843         int fd;
844
845         /*
846          * Since "size" is in KB, the file offset it represents could overflow
847          * off_t.
848          */
849         size_max = (off_t)1 << (_FILE_OFFSET_BITS - 1 - 10);
850         if (size >= size_max) {
851                 fprintf(stderr, "%s: %ju KB: Backing store size must be "
852                         "smaller than %ju KB\n", progname, (uintmax_t) size,
853                         (uintmax_t)size_max);
854                 return EFBIG;
855         }
856
857         ret = access(path, F_OK);
858         if (ret == 0) {
859                 ret = unlink(path);
860                 if (ret != 0)
861                         return errno;
862         }
863
864         fd = creat(path, S_IRUSR|S_IWUSR);
865         if (fd < 0) {
866                 fatal();
867                 fprintf(stderr, "%s: Unable to create backing store: %s\n",
868                         progname, strerror(errno));
869                 return errno;
870         }
871
872         ret = ftruncate(fd, size * 1024);
873         close(fd);
874         if (ret != 0) {
875                 fatal();
876                 fprintf(stderr, "%s: Unable to truncate backing store: %s\n",
877                         progname, strerror(errno));
878                 return errno;
879         }
880
881         return 0;
882 }
883
884 #ifdef HAVE_GSS
885 int load_shared_keys(struct mount_opts *mop)
886 {
887         DIR *dir;
888         struct dirent *dentry;
889         struct stat sbuf;
890         char fullpath[PATH_MAX];
891         char *path = mop->mo_skpath;
892         int type = 0;
893         int rc;
894
895         if (IS_SERVER(&mop->mo_ldd)) {
896                 if (IS_MGS(&mop->mo_ldd))
897                         type |= SK_TYPE_MGS;
898                 if (IS_MDT(&mop->mo_ldd) || IS_OST(&mop->mo_ldd))
899                         type |= SK_TYPE_SERVER | SK_TYPE_CLIENT;
900         } else {
901                 type |= SK_TYPE_CLIENT;
902         }
903
904         /* init logging */
905         sk_init_logging(NULL, 1, 1);
906
907         rc = stat(path, &sbuf);
908         if (rc < 0) {
909                 fprintf(stderr, "stat() failed for key %s: %s\n", path,
910                         strerror(errno));
911                 return -errno;
912         }
913
914         /* Load individual keys or a directory of them */
915         if (S_ISREG(sbuf.st_mode)) {
916                 return sk_load_keyfile(path, type);
917         } else if (!S_ISDIR(sbuf.st_mode)) {
918                 fprintf(stderr, "Invalid shared key path: %s\n", path);
919                 return -ENOKEY;
920         }
921
922         dir = opendir(path);
923         if (dir == NULL) {
924                 fprintf(stderr, "Unable to open shared key directory: %s\n",
925                         path);
926                 return -ENOENT;
927         }
928
929         /* Loop through the files in the directory attempting to load them.
930          * Any issue with loading the keyfile is treated as an error although
931          * the loop continues until all files have been attempted.  This will
932          * allow all errors be reported at once rather then requiring
933          * incremental corrections to fix each one and try again. */
934         while ((dentry = readdir(dir)) != NULL) {
935                 if (strcmp(".", dentry->d_name) == 0 ||
936                     strcmp("..", dentry->d_name) == 0)
937                         continue;
938
939                 rc = snprintf(fullpath, PATH_MAX, "%s/%s", path,
940                               dentry->d_name);
941                 if (rc >= PATH_MAX) {
942                         fprintf(stderr, "Path too long for %s/%s\n",
943                                 path, dentry->d_name);
944                         rc = -ENAMETOOLONG;
945                         continue;
946                 }
947
948                 rc = stat(fullpath, &sbuf);
949                 if (rc < 0) {
950                         fprintf(stderr, "Unable to stat %s: %s\n", fullpath,
951                                 strerror(errno));
952                         rc = -errno;
953                         continue;
954                 }
955
956                 if (!S_ISREG(sbuf.st_mode))
957                         continue;
958
959                 rc = sk_load_keyfile(fullpath, type);
960                 if (rc) {
961                         fprintf(stderr, "Failed to load key %s\n", fullpath);
962                 }
963         }
964         closedir(dir);
965
966         return rc;
967 }
968 #endif