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