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