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