Whamcloud - gitweb
85ed583ae6d04cf64e2c9d8e8468004224423755
[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_disk.h>
46 #include <lustre_ver.h>
47 #include <sys/mount.h>
48 #include <sys/stat.h>
49 #include <sys/utsname.h>
50 #include <linux/loop.h>
51 #include <sys/types.h>
52 #include <dirent.h>
53 #include <dlfcn.h>
54
55 #ifdef HAVE_GSS
56 #include <keyutils.h>
57 #include <lustre/utils/gss/sk_utils.h>
58 #endif
59
60 #include "mount_utils.h"
61
62 extern char *progname;
63 extern int verbose;
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 static struct module_backfs_ops *backfs_ops[LDD_MT_LAST];
69
70 void fatal(void)
71 {
72         verbose = 0;
73         fprintf(stderr, "\n%s FATAL: ", progname);
74 }
75
76 int run_command(char *cmd, int cmdsz)
77 {
78         char log[] = "/tmp/run_command_logXXXXXX";
79         int fd = -1, rc;
80
81         if ((cmdsz - strlen(cmd)) < 6) {
82                 fatal();
83                 fprintf(stderr, "Command buffer overflow: %.*s...\n",
84                         cmdsz, cmd);
85                 return ENOMEM;
86         }
87
88         if (verbose > 1) {
89                 printf("cmd: %s\n", cmd);
90         } else {
91                 if ((fd = mkstemp(log)) >= 0) {
92                         close(fd);
93                         strcat(cmd, " >");
94                         strcat(cmd, log);
95                 }
96         }
97         strcat(cmd, " 2>&1");
98
99         /* Can't use popen because we need the rv of the command */
100         rc = system(cmd);
101         if (rc && (fd >= 0)) {
102                 char buf[128];
103                 FILE *fp;
104                 fp = fopen(log, "r");
105                 if (fp) {
106                         while (fgets(buf, sizeof(buf), fp) != NULL) {
107                                 printf("   %s", buf);
108                         }
109                         fclose(fp);
110                 }
111         }
112         if (fd >= 0)
113                 remove(log);
114         return rc;
115 }
116
117 int add_param(char *buf, char *key, char *val)
118 {
119         int end = sizeof(((struct lustre_disk_data *)0)->ldd_params);
120         int start = strlen(buf);
121         int keylen = 0;
122
123         if (key)
124                 keylen = strlen(key);
125         if (start + 1 + keylen + strlen(val) >= end) {
126                 fprintf(stderr, "%s: params are too long-\n%s %s%s\n",
127                         progname, buf, key ? key : "", val);
128                 return 1;
129         }
130
131         sprintf(buf + start, " %s%s", key ? key : "", val);
132         return 0;
133 }
134
135 int get_param(char *buf, char *key, char **val)
136 {
137         int i, key_len = strlen(key);
138         char *ptr;
139
140         ptr = strstr(buf, key);
141         if (ptr) {
142                 *val = strdup(ptr + key_len);
143                 if (*val == NULL)
144                         return ENOMEM;
145
146                 for (i = 0; i < strlen(*val); i++)
147                         if (((*val)[i] == ' ') || ((*val)[i] == '\0'))
148                                 break;
149
150                 (*val)[i] = '\0';
151                 return 0;
152         }
153
154         return ENOENT;
155 }
156
157 int append_param(char *buf, char *key, char *val, char sep)
158 {
159         int key_len, i, offset, old_val_len;
160         char *ptr = NULL, str[1024];
161
162         if (key)
163                 ptr = strstr(buf, key);
164
165         /* key doesn't exist yet, so just add it */
166         if (ptr == NULL)
167                 return add_param(buf, key, val);
168
169         key_len = strlen(key);
170
171         /* Copy previous values to str */
172         for (i = 0; i < sizeof(str); ++i) {
173                 if ((ptr[i+key_len] == ' ') || (ptr[i+key_len] == '\0'))
174                         break;
175                 str[i] = ptr[i+key_len];
176         }
177         if (i == sizeof(str))
178                 return E2BIG;
179         old_val_len = i;
180
181         offset = old_val_len+key_len;
182
183         /* Move rest of buf to overwrite previous key and value */
184         for (i = 0; ptr[i+offset] != '\0'; ++i)
185                 ptr[i] = ptr[i+offset];
186
187         ptr[i] = '\0';
188
189         snprintf(str+old_val_len, sizeof(str)-old_val_len, "%c%s", sep, val);
190
191         return add_param(buf, key, str);
192 }
193
194 char *strscat(char *dst, char *src, int buflen)
195 {
196         dst[buflen - 1] = 0;
197         if (strlen(dst) + strlen(src) >= buflen) {
198                 fprintf(stderr, "string buffer overflow (max %d): '%s' + '%s'"
199                         "\n", buflen, dst, src);
200                 exit(EOVERFLOW);
201         }
202         return strcat(dst, src);
203 }
204
205 char *strscpy(char *dst, char *src, int buflen)
206 {
207         dst[0] = 0;
208         return strscat(dst, src, buflen);
209 }
210
211 int check_mtab_entry(char *spec1, char *spec2, char *mtpt, char *type)
212 {
213         FILE *fp;
214         struct mntent *mnt;
215
216         fp = setmntent(MOUNTED, "r");
217         if (fp == NULL)
218                 return 0;
219
220         while ((mnt = getmntent(fp)) != NULL) {
221                 if ((strcmp(mnt->mnt_fsname, spec1) == 0 ||
222                      strcmp(mnt->mnt_fsname, spec2) == 0) &&
223                     (mtpt == NULL || strcmp(mnt->mnt_dir, mtpt) == 0) &&
224                     (type == NULL || strcmp(mnt->mnt_type, type) == 0)) {
225                         endmntent(fp);
226                         return(EEXIST);
227                 }
228         }
229         endmntent(fp);
230
231         return 0;
232 }
233
234 #include <sys/vfs.h>
235 #include <linux/magic.h>
236
237 static int mtab_is_proc(const char *mtab)
238 {
239         struct statfs s;
240         if (statfs(mtab, &s) < 0)
241                 return 0;
242
243         return (s.f_type == PROC_SUPER_MAGIC);
244 }
245
246 #ifdef HAVE_LIBMOUNT
247
248 # include <libmount/libmount.h>
249
250 /*
251  * The libmount is part of util-linux since 2.18.
252  * We use it to update utab to avoid umount would
253  * blocked in some rare case.
254  */
255 int update_utab_entry(struct mount_opts *mop)
256 {
257         struct libmnt_fs *fs = mnt_new_fs();
258         struct libmnt_update *upd;
259         int rc;
260
261         mnt_fs_set_source(fs, mop->mo_source);
262         mnt_fs_set_target(fs, mop->mo_target);
263         mnt_fs_set_fstype(fs, "lustre");
264         mnt_fs_set_attributes(fs, "lustre");
265
266         upd = mnt_new_update();
267         if (!upd)
268                 return -ENOMEM;
269
270         rc = mnt_update_set_fs(upd, mop->mo_nomtab ? MS_REMOUNT : 0, NULL, fs);
271         if (rc == 1) /* update is unnecessary */
272                 rc = 0;
273         if (rc) {
274                 fprintf(stderr,
275                         "error: failed to save utab entry: rc = %d\n", rc);
276         } else {
277                 rc = mnt_update_table(upd, NULL);
278         }
279
280         mnt_free_update(upd);
281         mnt_free_fs(fs);
282
283         return rc;
284 }
285 #else
286 int update_utab_entry(struct mount_opts *mop)
287 {
288         return 0;
289 }
290 #endif /* HAVE_LIBMOUNT */
291
292 int update_mtab_entry(char *spec, char *mtpt, char *type, char *opts,
293                 int flags, int freq, int pass)
294 {
295         FILE *fp;
296         struct mntent mnt;
297         int rc = 0;
298
299         /* Don't update mtab if it is linked to any file in /proc direcotry.*/
300         if (mtab_is_proc(MOUNTED))
301                 return 0;
302
303         mnt.mnt_fsname = spec;
304         mnt.mnt_dir = mtpt;
305         mnt.mnt_type = type;
306         mnt.mnt_opts = opts ? opts : "";
307         mnt.mnt_freq = freq;
308         mnt.mnt_passno = pass;
309
310         fp = setmntent(MOUNTED, "a+");
311         if (fp == NULL) {
312                 fprintf(stderr, "%s: setmntent(%s): %s\n",
313                         progname, MOUNTED, strerror (errno));
314                 rc = 16;
315         } else {
316                 if ((addmntent(fp, &mnt)) == 1) {
317                         fprintf(stderr, "%s: addmntent: %s\n",
318                                 progname, strerror (errno));
319                         rc = 16;
320                 }
321                 endmntent(fp);
322         }
323
324         return rc;
325 }
326
327 /* Search for opt in mntlist, returning true if found.
328  */
329 static int in_mntlist(char *opt, char *mntlist)
330 {
331         char *ml, *mlp, *item, *ctx = NULL;
332
333         if (!(ml = strdup(mntlist))) {
334                 fprintf(stderr, "%s: out of memory\n", progname);
335                 exit(1);
336         }
337         mlp = ml;
338         while ((item = strtok_r(mlp, ",", &ctx))) {
339                 if (!strcmp(opt, item))
340                         break;
341                 mlp = NULL;
342         }
343         free(ml);
344         return (item != NULL);
345 }
346
347 /* Issue a message on stderr for every item in wanted_mountopts that is not
348  * present in mountopts.  The justwarn boolean toggles between error and
349  * warning message.  Return an error count.
350  */
351 int check_mountfsoptions(char *mountopts, char *wanted_mountopts)
352 {
353         char *ml, *mlp, *item, *ctx = NULL;
354         int errors = 0;
355
356         if (!(ml = strdup(wanted_mountopts))) {
357                 fprintf(stderr, "%s: out of memory\n", progname);
358                 exit(1);
359         }
360         mlp = ml;
361         while ((item = strtok_r(mlp, ",", &ctx))) {
362                 if (!in_mntlist(item, mountopts)) {
363                         fprintf(stderr, "%s: Error: mandatory mount option"
364                                 " '%s' is missing\n", progname, item);
365                         errors++;
366                 }
367                 mlp = NULL;
368         }
369         free(ml);
370         return errors;
371 }
372
373 /* Trim embedded white space, leading and trailing commas from string s.
374  */
375 void trim_mountfsoptions(char *s)
376 {
377         char *p;
378
379         for (p = s; *p; ) {
380                 if (isspace(*p)) {
381                         memmove(p, p + 1, strlen(p + 1) + 1);
382                         continue;
383                 }
384                 p++;
385         }
386
387         while (s[0] == ',')
388                 memmove(&s[0], &s[1], strlen(&s[1]) + 1);
389
390         p = s + strlen(s) - 1;
391         while (p >= s && *p == ',')
392                 *p-- = '\0';
393 }
394
395 /* Setup a file in the first unused loop_device */
396 int loop_setup(struct mkfs_opts *mop)
397 {
398         char loop_base[20];
399         char l_device[64];
400         int i, ret = 0;
401
402         /* Figure out the loop device names */
403         if (!access("/dev/loop0", F_OK | R_OK) ||
404             !access("/dev/loop-control", F_OK | R_OK)) {
405                 strcpy(loop_base, "/dev/loop\0");
406         } else if (!access("/dev/loop/0", F_OK | R_OK)) {
407                 strcpy(loop_base, "/dev/loop/\0");
408         } else {
409                 fprintf(stderr, "%s: can't access loop devices\n", progname);
410                 return EACCES;
411         }
412
413         /* Find unused loop device */
414         for (i = 0; i < MAX_LOOP_DEVICES; i++) {
415                 char cmd[PATH_MAX];
416                 int cmdsz = sizeof(cmd);
417
418 #ifdef HAVE_LOOP_CTL_GET_FREE
419                 ret = open("/dev/loop-control", O_RDWR);
420                 if (ret < 0) {
421                         fprintf(stderr, "%s: can't access loop control\n", progname);
422                         return EACCES;
423                 }
424                 /* find or allocate a free loop device to use */
425                 i = ioctl(ret, LOOP_CTL_GET_FREE);
426                 close(ret);
427                 if (i < 0) {
428                         fprintf(stderr, "%s: access loop control error\n", progname);
429                         return EACCES;
430                 }
431                 sprintf(l_device, "%s%d", loop_base, i);
432 #else
433                 sprintf(l_device, "%s%d", loop_base, i);
434                 if (access(l_device, F_OK | R_OK))
435                         break;
436 #endif
437                 snprintf(cmd, cmdsz, "losetup %s > /dev/null 2>&1", l_device);
438                 ret = system(cmd);
439
440                 /* losetup gets 1 (ret=256) for non-set-up device */
441                 if (ret) {
442                         /* Set up a loopback device to our file */
443                         snprintf(cmd, cmdsz, "losetup %s %s", l_device,
444                                  mop->mo_device);
445                         ret = run_command(cmd, cmdsz);
446                         if (ret == 256)
447                                 /* someone else picked up this loop device
448                                  * behind our back */
449                                 continue;
450                         if (ret) {
451                                 fprintf(stderr, "%s: error %d on losetup: %s\n",
452                                         progname, ret,
453                                         ret >= 0 ? strerror(ret) : "");
454                                 return ret;
455                         }
456                         strscpy(mop->mo_loopdev, l_device,
457                                 sizeof(mop->mo_loopdev));
458                         return ret;
459                 }
460         }
461
462         fprintf(stderr, "%s: out of loop devices!\n", progname);
463         return EMFILE;
464 }
465
466 int loop_cleanup(struct mkfs_opts *mop)
467 {
468         char cmd[150];
469         int ret = 0;
470
471         if ((mop->mo_flags & MO_IS_LOOP) && *mop->mo_loopdev) {
472                 int tries;
473
474                 sprintf(cmd, "losetup -d %s", mop->mo_loopdev);
475                 for (tries = 0; tries < 3; tries++) {
476                         ret = run_command(cmd, sizeof(cmd));
477                         if (ret == 0)
478                                 break;
479                         sleep(1);
480                 }
481         }
482
483         if (ret != 0)
484                 fprintf(stderr, "cannot cleanup %s: rc = %d\n",
485                         mop->mo_loopdev, ret);
486         return ret;
487 }
488
489 int loop_format(struct mkfs_opts *mop)
490 {
491         int fd;
492
493         if (mop->mo_device_kb == 0) {
494                 fatal();
495                 fprintf(stderr, "loop device requires a --device-size= "
496                         "param\n");
497                 return EINVAL;
498         }
499
500         fd = creat(mop->mo_device, S_IRUSR|S_IWUSR);
501         if (fd < 0) {
502                 fatal();
503                 fprintf(stderr, "%s: Unable to create backing store: %s\n",
504                         progname, strerror(errno));
505                 return errno;
506         }
507
508         if (ftruncate(fd, mop->mo_device_kb * 1024) != 0) {
509                 close(fd);
510                 fatal();
511                 fprintf(stderr, "%s: Unable to truncate backing store: %s\n",
512                         progname, strerror(errno));
513                 return errno;
514         }
515
516         close(fd);
517         return 0;
518 }
519
520 #define DLSYM(prefix, sym, func)                                        \
521         do {                                                            \
522                 char _fname[64];                                        \
523                 snprintf(_fname, sizeof(_fname), "%s_%s", prefix, #func); \
524                 sym->func = (typeof(sym->func))dlsym(sym->dl_handle, _fname); \
525         } while (0)
526
527 /**
528  * Load plugin for a given mount_type from ${pkglibdir}/mount_osd_FSTYPE.so and
529  * return struct of function pointers (will be freed in unloack_backfs_module).
530  *
531  * \param[in] mount_type        Mount type to load module for.
532  * \retval Value of backfs_ops struct
533  * \retval NULL if no module exists
534  */
535 struct module_backfs_ops *load_backfs_module(enum ldd_mount_type mount_type)
536 {
537         void *handle;
538         char *error, filename[512], fsname[512], *name;
539         struct module_backfs_ops *ops;
540
541         /* This deals with duplicate ldd_mount_types resolving to same OSD layer
542          * plugin (e.g. ext3/ldiskfs/ldiskfs2 all being ldiskfs) */
543         strncpy(fsname, mt_type(mount_type), sizeof(fsname));
544         name = fsname + sizeof("osd-") - 1;
545
546         /* change osd- to osd_ */
547         fsname[sizeof("osd-") - 2] = '_';
548
549         snprintf(filename, sizeof(filename), PLUGIN_DIR"/mount_%s.so", fsname);
550
551         handle = dlopen(filename, RTLD_LAZY);
552
553         /* Check for $LUSTRE environment variable from test-framework.
554          * This allows using locally built modules to be used.
555          */
556         if (handle == NULL) {
557                 char *dirname;
558                 dirname = getenv("LUSTRE");
559                 if (dirname) {
560                         snprintf(filename, sizeof(filename),
561                                  "%s/utils/.libs/mount_%s.so",
562                                  dirname, fsname);
563                         handle = dlopen(filename, RTLD_LAZY);
564                 }
565         }
566
567         /* Do not clutter up console with missing types */
568         if (handle == NULL)
569                 return NULL;
570
571         ops = malloc(sizeof(*ops));
572         if (ops == NULL) {
573                 dlclose(handle);
574                 return NULL;
575         }
576
577         ops->dl_handle = handle;
578         dlerror(); /* Clear any existing error */
579
580         DLSYM(name, ops, init);
581         DLSYM(name, ops, fini);
582         DLSYM(name, ops, read_ldd);
583         DLSYM(name, ops, write_ldd);
584         DLSYM(name, ops, is_lustre);
585         DLSYM(name, ops, make_lustre);
586         DLSYM(name, ops, prepare_lustre);
587         DLSYM(name, ops, tune_lustre);
588         DLSYM(name, ops, label_lustre);
589         DLSYM(name, ops, enable_quota);
590
591         error = dlerror();
592         if (error != NULL) {
593                 fatal();
594                 fprintf(stderr, "%s\n", error);
595                 dlclose(handle);
596                 free(ops);
597                 return NULL;
598         }
599
600         /* optional methods */
601         DLSYM(name, ops, fix_mountopts);
602
603         return ops;
604 }
605
606 /**
607  * Unload plugin and free backfs_ops structure. Must be called the same number
608  * of times as load_backfs_module is.
609  */
610 void unload_backfs_module(struct module_backfs_ops *ops)
611 {
612         if (ops == NULL)
613                 return;
614
615         dlclose(ops->dl_handle);
616         free(ops);
617 }
618
619 /* Return true if backfs_ops has operations for the given mount_type. */
620 int backfs_mount_type_okay(enum ldd_mount_type mount_type)
621 {
622         if (unlikely(mount_type >= LDD_MT_LAST || mount_type < 0)) {
623                 fatal();
624                 fprintf(stderr, "fs type out of range %d\n", mount_type);
625                 return 0;
626         }
627         if (backfs_ops[mount_type] == NULL) {
628                 fatal();
629                 fprintf(stderr, "unhandled/unloaded fs type %d '%s'\n",
630                         mount_type, mt_str(mount_type));
631                 return 0;
632         }
633         return 1;
634 }
635
636 /* Write the server config files */
637 int osd_write_ldd(struct mkfs_opts *mop)
638 {
639         struct lustre_disk_data *ldd = &mop->mo_ldd;
640         int ret;
641
642         if (backfs_mount_type_okay(ldd->ldd_mount_type))
643                 ret = backfs_ops[ldd->ldd_mount_type]->write_ldd(mop);
644         else
645                 ret = EINVAL;
646
647         return ret;
648 }
649
650 /* Read the server config files */
651 int osd_read_ldd(char *dev, struct lustre_disk_data *ldd)
652 {
653         int ret;
654
655         if (backfs_mount_type_okay(ldd->ldd_mount_type))
656                 ret = backfs_ops[ldd->ldd_mount_type]->read_ldd(dev, ldd);
657         else
658                 ret = EINVAL;
659
660         return ret;
661 }
662
663 /* Was this device formatted for Lustre */
664 int osd_is_lustre(char *dev, unsigned *mount_type)
665 {
666         int i;
667
668         vprint("checking for existing Lustre data: ");
669
670         for (i = 0; i < LDD_MT_LAST; ++i) {
671                 if (backfs_ops[i] != NULL &&
672                     backfs_ops[i]->is_lustre(dev, mount_type)) {
673                         vprint("found\n");
674                         return 1;
675                 }
676         }
677
678         vprint("not found\n");
679         return 0;
680 }
681
682 /* Build fs according to type */
683 int osd_make_lustre(struct mkfs_opts *mop)
684 {
685         struct lustre_disk_data *ldd = &mop->mo_ldd;
686         int ret;
687
688         if (backfs_mount_type_okay(ldd->ldd_mount_type))
689                 ret = backfs_ops[ldd->ldd_mount_type]->make_lustre(mop);
690         else
691                 ret = EINVAL;
692
693         return ret;
694 }
695
696 int osd_prepare_lustre(struct mkfs_opts *mop,
697                        char *wanted_mountopts, size_t len)
698 {
699         struct lustre_disk_data *ldd = &mop->mo_ldd;
700         int ret;
701
702         if (backfs_mount_type_okay(ldd->ldd_mount_type))
703                 ret = backfs_ops[ldd->ldd_mount_type]->prepare_lustre(mop,
704                                                         wanted_mountopts, len);
705         else
706                 ret = EINVAL;
707
708         return ret;
709 }
710
711 int osd_fix_mountopts(struct mkfs_opts *mop, char *mountopts, size_t len)
712 {
713         struct lustre_disk_data *ldd = &mop->mo_ldd;
714
715         if (!backfs_mount_type_okay(ldd->ldd_mount_type))
716                 return EINVAL;
717
718         if (backfs_ops[ldd->ldd_mount_type]->fix_mountopts == NULL)
719                 return 0;
720
721         return backfs_ops[ldd->ldd_mount_type]->fix_mountopts(mop, mountopts,
722                                                               len);
723 }
724
725 int osd_tune_lustre(char *dev, struct mount_opts *mop)
726 {
727         struct lustre_disk_data *ldd = &mop->mo_ldd;
728         int ret;
729
730         if (backfs_mount_type_okay(ldd->ldd_mount_type))
731                 ret = backfs_ops[ldd->ldd_mount_type]->tune_lustre(dev, mop);
732         else
733                 ret = EINVAL;
734
735         return ret;
736 }
737
738 int osd_label_lustre(struct mount_opts *mop)
739 {
740         struct lustre_disk_data *ldd = &mop->mo_ldd;
741         int ret;
742
743         if (backfs_mount_type_okay(ldd->ldd_mount_type))
744                 ret = backfs_ops[ldd->ldd_mount_type]->label_lustre(mop);
745         else
746                 ret = EINVAL;
747
748         return ret;
749 }
750
751 /* Enable quota accounting */
752 int osd_enable_quota(struct mkfs_opts *mop)
753 {
754         struct lustre_disk_data *ldd = &mop->mo_ldd;
755         int ret;
756
757         if (backfs_mount_type_okay(ldd->ldd_mount_type))
758                 ret = backfs_ops[ldd->ldd_mount_type]->enable_quota(mop);
759         else
760                 ret = EINVAL;
761
762         return ret;
763 }
764
765 int osd_init(void)
766 {
767         int i, rc, ret = EINVAL;
768
769         for (i = 0; i < LDD_MT_LAST; ++i) {
770                 rc = 0;
771                 backfs_ops[i] = load_backfs_module(i);
772                 if (backfs_ops[i] != NULL)
773                         rc = backfs_ops[i]->init();
774                 if (rc != 0) {
775                         backfs_ops[i]->fini();
776                         unload_backfs_module(backfs_ops[i]);
777                         backfs_ops[i] = NULL;
778                 } else
779                         ret = 0;
780         }
781
782         return ret;
783 }
784
785 void osd_fini(void)
786 {
787         int i;
788
789         for (i = 0; i < LDD_MT_LAST; ++i) {
790                 if (backfs_ops[i] != NULL) {
791                         backfs_ops[i]->fini();
792                         unload_backfs_module(backfs_ops[i]);
793                         backfs_ops[i] = NULL;
794                 }
795         }
796 }
797
798 __u64 get_device_size(char* device)
799 {
800         int ret, fd;
801         __u64 size = 0;
802
803         fd = open(device, O_RDONLY);
804         if (fd < 0) {
805                 fprintf(stderr, "%s: cannot open %s: %s\n",
806                         progname, device, strerror(errno));
807                 return 0;
808         }
809
810 #ifdef BLKGETSIZE64
811         /* size in bytes. bz5831 */
812         ret = ioctl(fd, BLKGETSIZE64, (void*)&size);
813 #else
814         {
815                 __u32 lsize = 0;
816                 /* size in blocks */
817                 ret = ioctl(fd, BLKGETSIZE, (void*)&lsize);
818                 size = (__u64)lsize * 512;
819         }
820 #endif
821         close(fd);
822         if (ret < 0) {
823                 fprintf(stderr, "%s: size ioctl failed: %s\n",
824                         progname, strerror(errno));
825                 return 0;
826         }
827
828         vprint("device size = %juMB\n", (uintmax_t)(size >> 20));
829         /* return value in KB */
830         return size >> 10;
831 }
832
833 int file_create(char *path, __u64 size)
834 {
835         __u64 size_max;
836         int ret;
837         int fd;
838
839         /*
840          * Since "size" is in KB, the file offset it represents could overflow
841          * off_t.
842          */
843         size_max = (off_t)1 << (_FILE_OFFSET_BITS - 1 - 10);
844         if (size >= size_max) {
845                 fprintf(stderr, "%s: %ju KB: Backing store size must be "
846                         "smaller than %ju KB\n", progname, (uintmax_t) size,
847                         (uintmax_t)size_max);
848                 return EFBIG;
849         }
850
851         ret = access(path, F_OK);
852         if (ret == 0) {
853                 ret = unlink(path);
854                 if (ret != 0)
855                         return errno;
856         }
857
858         fd = creat(path, S_IRUSR|S_IWUSR);
859         if (fd < 0) {
860                 fatal();
861                 fprintf(stderr, "%s: Unable to create backing store: %s\n",
862                         progname, strerror(errno));
863                 return errno;
864         }
865
866         ret = ftruncate(fd, size * 1024);
867         close(fd);
868         if (ret != 0) {
869                 fatal();
870                 fprintf(stderr, "%s: Unable to truncate backing store: %s\n",
871                         progname, strerror(errno));
872                 return errno;
873         }
874
875         return 0;
876 }
877
878 #ifdef HAVE_GSS
879 #ifdef HAVE_OPENSSL_SSK
880 int load_shared_keys(struct mount_opts *mop)
881 {
882         DIR *dir;
883         struct dirent *dentry;
884         struct stat sbuf;
885         char fullpath[PATH_MAX];
886         char *path = mop->mo_skpath;
887         int rc;
888
889         /* init logging */
890         sk_init_logging(NULL, 1, 1);
891
892         rc = stat(path, &sbuf);
893         if (rc < 0) {
894                 fprintf(stderr, "stat() failed for key %s: %s\n", path,
895                         strerror(errno));
896                 return -errno;
897         }
898
899         /* Load individual keys or a directory of them */
900         if (S_ISREG(sbuf.st_mode)) {
901                 return sk_load_keyfile(path);
902         } else if (!S_ISDIR(sbuf.st_mode)) {
903                 fprintf(stderr, "Invalid shared key path: %s\n", path);
904                 return -ENOKEY;
905         }
906
907         dir = opendir(path);
908         if (dir == NULL) {
909                 fprintf(stderr, "Unable to open shared key directory: %s\n",
910                         path);
911                 return -ENOENT;
912         }
913
914         /* Loop through the files in the directory attempting to load them.
915          * Any issue with loading the keyfile is treated as an error although
916          * the loop continues until all files have been attempted.  This will
917          * allow all errors be reported at once rather then requiring
918          * incremental corrections to fix each one and try again. */
919         while ((dentry = readdir(dir)) != NULL) {
920                 if (strcmp(".", dentry->d_name) == 0 ||
921                     strcmp("..", dentry->d_name) == 0)
922                         continue;
923
924                 rc = snprintf(fullpath, PATH_MAX, "%s/%s", path,
925                               dentry->d_name);
926                 if (rc >= PATH_MAX) {
927                         fprintf(stderr, "Path too long for %s/%s\n",
928                                 path, dentry->d_name);
929                         rc = -ENAMETOOLONG;
930                         continue;
931                 }
932
933                 rc = stat(fullpath, &sbuf);
934                 if (rc < 0) {
935                         fprintf(stderr, "Unable to stat %s: %s\n", fullpath,
936                                 strerror(errno));
937                         rc = -errno;
938                         continue;
939                 }
940
941                 if (!S_ISREG(sbuf.st_mode))
942                         continue;
943
944                 rc = sk_load_keyfile(fullpath);
945                 if (rc) {
946                         fprintf(stderr, "Failed to load key %s\n", fullpath);
947                 }
948         }
949         closedir(dir);
950
951         return rc;
952 }
953 #endif /* HAVE_OPENSSL_SSK */
954 #endif /* HAVE_GSS */