summaryrefslogtreecommitdiffstats
path: root/libglusterfs/src/syscall.c
blob: 2d79d695b09a5273a0d9a76b73ddda3893021790 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
/*
  Copyright (c) 2008-2012 Red Hat, Inc. <http://www.redhat.com>
  This file is part of GlusterFS.

  This file is licensed to you under your choice of the GNU Lesser
  General Public License, version 3 or any later version (LGPLv3 or
  later), or the GNU General Public License, version 2 (GPLv2), in all
  cases as published by the Free Software Foundation.
*/

#include "syscall.h"
#include "compat.h"
#include "mem-pool.h"
#include "libglusterfs-messages.h"

#include <sys/types.h>
#include <utime.h>
#include <sys/time.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdarg.h>

#define FS_ERROR_LOG(result)                                                   \
        do {                                                                   \
                gf_msg_callingfn ("FS", GF_LOG_CRITICAL, EIO,                  \
                                  LG_MSG_SYSCALL_RETURNS_WRONG,                \
                                  "returned %zd for the syscall",              \
                                  (ssize_t)result);                            \
        } while (0)


/*
 * Input to these macros is generally a function call, so capture the result
 * i.e. (_ret) in another variable and use that instead of using _ret again
 */
#define FS_RET_CHECK(_ret, err)                                        \
({                                                                     \
        typeof(_ret) _result = (_ret);                                 \
        if (_result < -1) {                                            \
                FS_ERROR_LOG (_result);                                \
                _result = -1;                                          \
                err = EIO;                                             \
        }                                                              \
        _result;                                                       \
 })

#define FS_RET_CHECK0(_ret, err)                                       \
({                                                                     \
        typeof(_ret) _result0 = (_ret);                                \
        if (_result0 < -1 || _result0 > 0) {                           \
                FS_ERROR_LOG (_result0);                               \
                _result0 = -1;                                         \
                err = EIO;                                             \
        }                                                              \
        _result0;                                                      \
})

#define FS_RET_CHECK_ERRNO(_ret, err)                                  \
({                                                                     \
        typeof(_ret) _result1 = (_ret);                                \
        if (_result1 < 0) {                                            \
                FS_ERROR_LOG (_result1);                               \
                _result1 = -1;                                         \
                err = EIO;                                             \
        } else if (_result1 > 0) {                                     \
                err = _result1;                                        \
                _result1 = -1;                                         \
        }                                                              \
        _result1;                                                      \
})

int
sys_lstat (const char *path, struct stat *buf)
{
        return FS_RET_CHECK0(lstat (path, buf), errno);
}


int
sys_stat (const char *path, struct stat *buf)
{
        return FS_RET_CHECK0(stat (path, buf), errno);
}


int
sys_fstat (int fd, struct stat *buf)
{
        return FS_RET_CHECK0(fstat (fd, buf), errno);
}


int
sys_fstatat(int dirfd, const char *pathname, struct stat *buf, int flags)
{
#ifdef GF_DARWIN_HOST_OS
        if (fchdir(dirfd) < 0)
                return -1;
        if(flags & AT_SYMLINK_NOFOLLOW)
                return FS_RET_CHECK0(lstat(pathname, buf), errno);
        else
                return FS_RET_CHECK0(stat(pathname, buf), errno);
#else
        return FS_RET_CHECK0(fstatat (dirfd, pathname, buf, flags), errno);
#endif
}


int
sys_openat(int dirfd, const char *pathname, int flags, int mode)
{
        int fd;

#ifdef GF_DARWIN_HOST_OS
        if (fchdir(dirfd) < 0)
                return -1;
        fd = open (pathname, flags, mode);
        /* TODO: Shouldn't we restore the old current directory */
#else /* GF_DARWIN_HOST_OS */
        fd = openat (dirfd, pathname, flags, mode);
#ifdef __FreeBSD__
        /* On FreeBSD S_ISVTX flag is ignored for an open() with O_CREAT set.
         * We need to force the flag using fchmod(). */
        if ((fd >= 0) &&
            ((flags & O_CREAT) != 0) && ((mode & S_ISVTX) != 0)) {
                sys_fchmod(fd, mode);
                /* TODO: It's unlikely that fchmod could fail here. However,
                         if it fails we cannot always restore the old state
                         (if the file existed, we cannot recover it). We would
                         need many more system calls to correctly handle all
                         possible cases and it doesn't worth it. For now we
                         simply ignore the error. */
        }
#endif /* __FreeBSD__ */
#endif /* !GF_DARWIN_HOST_OS */

        return FS_RET_CHECK(fd, errno);
}


int
sys_open(const char *pathname, int flags, int mode)
{
        return FS_RET_CHECK(sys_openat(AT_FDCWD, pathname, flags, mode), errno);
}


DIR *
sys_opendir (const char *name)
{
        return opendir (name);
}

int sys_mkdirat(int dirfd, const char *pathname, mode_t mode)
{
#ifdef GF_DARWIN_HOST_OS
        if(fchdir(dirfd) < 0)
                return -1;
        return FS_RET_CHECK0(mkdir(pathname, mode), errno);
#else
        return FS_RET_CHECK0(mkdirat (dirfd, pathname, mode), errno);
#endif
}

struct dirent *
sys_readdir (DIR *dir, struct dirent *de)
{
#if !defined(__GLIBC__)
        /*
         * World+Dog says glibc's readdir(3) is MT-SAFE as long as
         * two threads are not accessing the same DIR; there's a
         * potential buffer overflow in glibc's readdir_r(3); and
         * glibc's readdir_r(3) is deprecated after version 2.22
         * with presumed eventual removal.
         * Given all that, World+Dog says everyone should just use
         * readdir(3). But it's unknown, unclear whether the same
         * is also true for *BSD, MacOS, and, etc.
        */
        struct dirent *entry = NULL;

        (void) readdir_r (dir, de, &entry);
        return entry;
#else
        return readdir (dir);
#endif
}


ssize_t
sys_readlink (const char *path, char *buf, size_t bufsiz)
{
        return FS_RET_CHECK(readlink (path, buf, bufsiz), errno);
}


int
sys_closedir (DIR *dir)
{
        return FS_RET_CHECK0(closedir (dir), errno);
}


int
sys_mknod (const char *pathname, mode_t mode, dev_t dev)
{
        return FS_RET_CHECK0(mknod (pathname, mode, dev), errno);
}


int
sys_mkdir (const char *pathname, mode_t mode)
{
        return FS_RET_CHECK0(mkdir (pathname, mode), errno);
}


int
sys_unlink (const char *pathname)
{
#ifdef GF_SOLARIS_HOST_OS
        return FS_RET_CHECK0(solaris_unlink (pathname), errno);
#endif
        return FS_RET_CHECK0(unlink (pathname), errno);
}


int
sys_rmdir (const char *pathname)
{
        return FS_RET_CHECK0(rmdir (pathname), errno);
}


int
sys_symlink (const char *oldpath, const char *newpath)
{
        return FS_RET_CHECK0(symlink (oldpath, newpath), errno);
}


int
sys_rename (const char *oldpath, const char *newpath)
{
#ifdef GF_SOLARIS_HOST_OS
        return FS_RET_CHECK0(solaris_rename (oldpath, newpath), errno);
#endif
        return FS_RET_CHECK0(rename (oldpath, newpath), errno);
}


int
sys_link (const char *oldpath, const char *newpath)
{
#ifdef HAVE_LINKAT
	/*
	 * On most systems (Linux being the notable exception), link(2)
	 * first resolves symlinks. If the target is a directory or
	 * is nonexistent, it will fail. linkat(2) operates on the
	 * symlink instead of its target when the AT_SYMLINK_FOLLOW
	 * flag is not supplied.
	 */
        return FS_RET_CHECK0(linkat (AT_FDCWD, oldpath, AT_FDCWD, newpath, 0),
                             errno);
#else
        return FS_RET_CHECK0(link (oldpath, newpath), errno);
#endif
}


int
sys_chmod (const char *path, mode_t mode)
{
        return FS_RET_CHECK0(chmod (path, mode), errno);
}


int
sys_fchmod (int fd, mode_t mode)
{
        return FS_RET_CHECK0(fchmod (fd, mode), errno);
}


int
sys_chown (const char *path, uid_t owner, gid_t group)
{
        return FS_RET_CHECK0(chown (path, owner, group), errno);
}


int
sys_fchown (int fd, uid_t owner, gid_t group)
{
        return FS_RET_CHECK0(fchown (fd, owner, group), errno);
}


int
sys_lchown (const char *path, uid_t owner, gid_t group)
{
        return FS_RET_CHECK0(lchown (path, owner, group), errno);
}


int
sys_truncate (const char *path, off_t length)
{
        return FS_RET_CHECK0(truncate (path, length), errno);
}


int
sys_ftruncate (int fd, off_t length)
{
        return FS_RET_CHECK0(ftruncate (fd, length), errno);
}


int
sys_utimes (const char *filename, const struct timeval times[2])
{
        return FS_RET_CHECK0(utimes (filename, times), errno);
}


#if defined(HAVE_UTIMENSAT)
int
sys_utimensat (int dirfd, const char *filename, const struct timespec times[2],
               int flags)
{
        return FS_RET_CHECK0(utimensat (dirfd, filename, times, flags), errno);
}
#endif


int
sys_futimes (int fd, const struct timeval times[2])
{
        return futimes (fd, times);
}


int
sys_creat (const char *pathname, mode_t mode)
{
        return FS_RET_CHECK(sys_open(pathname, O_CREAT | O_TRUNC | O_WRONLY,
                                     mode), errno);
}


ssize_t
sys_readv (int fd, const struct iovec *iov, int iovcnt)
{
        return FS_RET_CHECK(readv (fd, iov, iovcnt), errno);
}


ssize_t
sys_writev (int fd, const struct iovec *iov, int iovcnt)
{
        return FS_RET_CHECK(writev (fd, iov, iovcnt), errno);
}


ssize_t
sys_read (int fd, void *buf, size_t count)
{
        return FS_RET_CHECK(read (fd, buf, count), errno);
}


ssize_t
sys_write (int fd, const void *buf, size_t count)
{
        return FS_RET_CHECK(write (fd, buf, count), errno);
}


ssize_t
sys_preadv (int fd, const struct iovec *iov, int iovcnt, off_t offset)
{
        return FS_RET_CHECK(preadv (fd, iov, iovcnt, offset), errno);
}


ssize_t
sys_pwritev (int fd, const struct iovec *iov, int iovcnt, off_t offset)
{
        return FS_RET_CHECK(pwritev (fd, iov, iovcnt, offset), errno);
}


ssize_t
sys_pread (int fd, void *buf, size_t count, off_t offset)
{
        return FS_RET_CHECK(pread (fd, buf, count, offset), errno);
}


ssize_t
sys_pwrite (int fd, const void *buf, size_t count, off_t offset)
{
        return FS_RET_CHECK(pwrite (fd, buf, count, offset), errno);
}


off_t
sys_lseek (int fd, off_t offset, int whence)
{
        return FS_RET_CHECK(lseek (fd, offset, whence), errno);
}


int
sys_statvfs (const char *path, struct statvfs *buf)
{
        int ret;

        ret = statvfs (path, buf);
#ifdef __FreeBSD__
        /* FreeBSD doesn't return the expected value in buf->f_bsize. It
         * contains the optimal I/O size instead of the file system block
         * size. Gluster expects that this field contains the block size.
         */
        if (ret == 0) {
                buf->f_bsize = buf->f_frsize;
        }
#endif /* __FreeBSD__ */

        return FS_RET_CHECK0(ret, errno);
}


int
sys_fstatvfs (int fd, struct statvfs *buf)
{
        int ret;

        ret = fstatvfs (fd, buf);
#ifdef __FreeBSD__
        /* FreeBSD doesn't return the expected value in buf->f_bsize. It
         * contains the optimal I/O size instead of the file system block
         * size. Gluster expects this field to contain the block size.
         */
        if (ret == 0) {
                buf->f_bsize = buf->f_frsize;
        }
#endif /* __FreeBSD__ */

        return FS_RET_CHECK0(ret, errno);
}


int
sys_close (int fd)
{
        int ret = -1;

        if (fd >= 0)
                ret = close (fd);

        return FS_RET_CHECK0(ret, errno);
}


int
sys_fsync (int fd)
{
        return FS_RET_CHECK0(fsync (fd), errno);
}


int
sys_fdatasync (int fd)
{
#ifdef GF_DARWIN_HOST_OS
        return FS_RET_CHECK0(fcntl (fd, F_FULLFSYNC), errno);
#elif __FreeBSD__
	return FS_RET_CHECK0(fsync (fd), errno);
#else
        return FS_RET_CHECK0(fdatasync (fd), errno);
#endif
}

void
gf_add_prefix(const char *ns, const char *key, char **newkey)
{
        /* if we don't have any namespace, append USER NS */
        if (strncmp(key, XATTR_USER_PREFIX,     XATTR_USER_PREFIX_LEN) &&
            strncmp(key, XATTR_TRUSTED_PREFIX,  XATTR_TRUSTED_PREFIX_LEN) &&
            strncmp(key, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN) &&
            strncmp(key, XATTR_SYSTEM_PREFIX,   XATTR_SYSTEM_PREFIX_LEN)) {
                int ns_length =  strlen(ns);
                *newkey = GF_MALLOC(ns_length + strlen(key) + 10,
                                    gf_common_mt_char);
                strcpy(*newkey, ns);
                strcat(*newkey, key);
        } else {
                *newkey = gf_strdup(key);
        }
}

void
gf_remove_prefix(const char *ns, const char *key, char **newkey)
{
        int ns_length =  strlen(ns);
        if (strncmp(key, ns, ns_length) == 0) {
                *newkey = GF_MALLOC(-ns_length + strlen(key) + 10,
                                    gf_common_mt_char);
                strcpy(*newkey, key + ns_length);
        } else {
                *newkey = gf_strdup(key);
        }
}

int
sys_lsetxattr (const char *path, const char *name, const void *value,
               size_t size, int flags)
{

#if defined(GF_LINUX_HOST_OS) || defined(__NetBSD__)
        return FS_RET_CHECK0(lsetxattr (path, name, value, size, flags), errno);
#endif

#ifdef GF_BSD_HOST_OS
        return FS_RET_CHECK0(extattr_set_link (path, EXTATTR_NAMESPACE_USER,
                             name, value, size), errno);
#endif

#ifdef GF_SOLARIS_HOST_OS
        return FS_RET_CHECK0(solaris_setxattr (path, name, value, size, flags),
                             errno);
#endif

#ifdef GF_DARWIN_HOST_OS
        /* OS X clients will carry other flags, which will be used on a
           OS X host, but masked out on others. GF assume NOFOLLOW on Linux,
           enforcing  */
        return FS_RET_CHECK0(setxattr (path, name, value, size, 0,
                             (flags & ~XATTR_NOSECURITY) | XATTR_NOFOLLOW),
                             errno);
#endif

}


ssize_t
sys_llistxattr (const char *path, char *list, size_t size)
{

#if defined(GF_LINUX_HOST_OS) || defined(__NetBSD__)
        return FS_RET_CHECK(llistxattr (path, list, size), errno);
#endif

#ifdef GF_BSD_HOST_OS
        ssize_t ret = FS_RET_CHECK(extattr_list_link (path,
                                                      EXTATTR_NAMESPACE_USER,
                                                      list, size), errno);
        gf_extattr_list_reshape (list, ret);
        return ret;
#endif

#ifdef GF_SOLARIS_HOST_OS
        return FS_RET_CHECK(solaris_listxattr (path, list, size), errno);
#endif

#ifdef GF_DARWIN_HOST_OS
        return FS_RET_CHECK(listxattr (path, list, size, XATTR_NOFOLLOW),
                            errno);
#endif
}

ssize_t
sys_lgetxattr (const char *path, const char *name, void *value, size_t size)
{

#if defined(GF_LINUX_HOST_OS) || defined(__NetBSD__)
        return FS_RET_CHECK(lgetxattr (path, name, value, size), errno);
#endif

#ifdef GF_BSD_HOST_OS
        return FS_RET_CHECK(extattr_get_link (path, EXTATTR_NAMESPACE_USER,
                                              name, value, size), errno);
#endif

#ifdef GF_SOLARIS_HOST_OS
        return FS_RET_CHECK(solaris_getxattr (path, name, value, size), errno);
#endif

#ifdef GF_DARWIN_HOST_OS
         return FS_RET_CHECK(getxattr (path, name, value, size, 0,
                                       XATTR_NOFOLLOW), errno);
#endif

}


ssize_t
sys_fgetxattr (int filedes, const char *name, void *value, size_t size)
{

#if defined(GF_LINUX_HOST_OS) || defined(__NetBSD__)
        return FS_RET_CHECK(fgetxattr (filedes, name, value, size), errno);
#endif

#ifdef GF_BSD_HOST_OS
        return FS_RET_CHECK(extattr_get_fd (filedes, EXTATTR_NAMESPACE_USER,
                            name, value, size), errno);
#endif

#ifdef GF_SOLARIS_HOST_OS
        return FS_RET_CHECK(solaris_fgetxattr (filedes, name, value, size),
                            errno);
#endif

#ifdef GF_DARWIN_HOST_OS
        return FS_RET_CHECK(fgetxattr (filedes, name, value, size, 0, 0),
                            errno);
#endif

}

int
sys_fremovexattr (int filedes, const char *name)
{

#if defined(GF_LINUX_HOST_OS) || defined(__NetBSD__)
        return FS_RET_CHECK0(fremovexattr (filedes, name), errno);
#endif

#ifdef GF_BSD_HOST_OS
        return FS_RET_CHECK0(extattr_delete_fd (filedes, EXTATTR_NAMESPACE_USER,
                                                name), errno);
#endif

#ifdef GF_SOLARIS_HOST_OS
        return FS_RET_CHECK0(solaris_fremovexattr (filedes, name), errno);
#endif

#ifdef GF_DARWIN_HOST_OS
        return FS_RET_CHECK0(fremovexattr (filedes, name, 0), errno);
#endif
}


int
sys_fsetxattr (int filedes, const char *name, const void *value,
               size_t size, int flags)
{

#if defined(GF_LINUX_HOST_OS) || defined(__NetBSD__)
        return FS_RET_CHECK0(fsetxattr (filedes, name, value, size, flags),
                             errno);
#endif

#ifdef GF_BSD_HOST_OS
        return FS_RET_CHECK0(extattr_set_fd (filedes, EXTATTR_NAMESPACE_USER,
                                             name, value, size), errno);
#endif

#ifdef GF_SOLARIS_HOST_OS
        return FS_RET_CHECK0(solaris_fsetxattr (filedes, name, value, size,
                                                flags), errno);
#endif

#ifdef GF_DARWIN_HOST_OS
        return FS_RET_CHECK0(fsetxattr (filedes, name, value, size, 0,
                                        flags & ~XATTR_NOSECURITY), errno);
#endif

}


ssize_t
sys_flistxattr (int filedes, char *list, size_t size)
{

#if defined(GF_LINUX_HOST_OS) || defined(__NetBSD__)
        return FS_RET_CHECK(flistxattr (filedes, list, size), errno);
#endif

#ifdef GF_BSD_HOST_OS
        ssize_t ret = FS_RET_CHECK (extattr_list_fd (filedes,
                                                     EXTATTR_NAMESPACE_USER,
                                                     list, size), errno);
        gf_extattr_list_reshape (list, ret);
        return ret;
#endif

#ifdef GF_SOLARIS_HOST_OS
        return FS_RET_CHECK(solaris_flistxattr (filedes, list, size), errno);
#endif

#ifdef GF_DARWIN_HOST_OS
        return FS_RET_CHECK(flistxattr (filedes, list, size, XATTR_NOFOLLOW),
                            errno);
#endif

}


int
sys_lremovexattr (const char *path, const char *name)
{

#if defined(GF_LINUX_HOST_OS) || defined(__NetBSD__)
        return FS_RET_CHECK0(lremovexattr (path, name), errno);
#endif

#ifdef GF_BSD_HOST_OS
        return FS_RET_CHECK0(extattr_delete_link (path, EXTATTR_NAMESPACE_USER,
                                                  name), errno);
#endif

#ifdef GF_SOLARIS_HOST_OS
        return FS_RET_CHECK0(solaris_removexattr (path, name), errno);
#endif

#ifdef GF_DARWIN_HOST_OS
        return FS_RET_CHECK0(removexattr (path, name, XATTR_NOFOLLOW), errno);
#endif

}


int
sys_access (const char *pathname, int mode)
{
        return FS_RET_CHECK0(access (pathname, mode), errno);
}


int
sys_fallocate(int fd, int mode, off_t offset, off_t len)
{
#ifdef HAVE_FALLOCATE
        return FS_RET_CHECK0(fallocate(fd, mode, offset, len), errno);
#endif

#ifdef HAVE_POSIX_FALLOCATE
        if (mode) {
                /* keep size not supported */
                errno = EOPNOTSUPP;
                return -1;
        }

        return FS_RET_CHECK_ERRNO(posix_fallocate(fd, offset, len), errno);
#endif

#if defined(F_ALLOCATECONTIG) && defined(GF_DARWIN_HOST_OS)
        /* C conversion from C++ implementation for OSX by Mozilla Foundation */
        if (mode) {
                /* keep size not supported */
                errno = EOPNOTSUPP;
                return -1;
        }
        /*
         *   The F_PREALLOCATE command operates on the following structure:
         *
         *    typedef struct fstore {
         *    u_int32_t fst_flags;      // IN: flags word
         *    int       fst_posmode;    // IN: indicates offset field
         *    off_t     fst_offset;     // IN: start of the region
         *    off_t     fst_length;     // IN: size of the region
         *    off_t     fst_bytesalloc; // OUT: number of bytes allocated
         *    } fstore_t;
         *
         * The flags (fst_flags) for the F_PREALLOCATE command are as follows:
         *    F_ALLOCATECONTIG   Allocate contiguous space.
         *    F_ALLOCATEALL      Allocate all requested space or no space at all.
         *
         * The position modes (fst_posmode) for the F_PREALLOCATE command
         * indicate how to use the offset field.  The modes are as follows:
         *    F_PEOFPOSMODE   Allocate from the physical end of file.
         *    F_VOLPOSMODE    Allocate from the volume offset.
         *
         */

        int ret;
        fstore_t store = {F_ALLOCATECONTIG, F_PEOFPOSMODE, offset, len, 0};
        ret = fcntl (fd, F_PREALLOCATE, &store);
        if (ret == -1) {
                store.fst_flags = F_ALLOCATEALL;
                ret = fcntl (fd, F_PREALLOCATE, &store);
        }
        if (ret == -1)
                return ret;
        return FS_RET_CHECK0(ftruncate (fd, offset + len), errno);
#endif
        errno = ENOSYS;
        return -1;
}