posix_fadvise64.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * posix_fadvise64() for uClibc
  4. * http://www.opengroup.org/onlinepubs/009695399/functions/posix_fadvise.html
  5. *
  6. * Copyright (C) 2000-2006 Erik Andersen <andersen@uclibc.org>
  7. *
  8. * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
  9. */
  10. #include <features.h>
  11. #include <unistd.h>
  12. #include <errno.h>
  13. #include <endian.h>
  14. #include <stdint.h>
  15. #include <sys/types.h>
  16. #include <sys/syscall.h>
  17. #include <fcntl.h>
  18. #ifdef __UCLIBC_HAS_LFS__
  19. #ifdef __NR_fadvise64_64
  20. /* 64 bit implementation is cake ... or more like pie ... */
  21. #if __WORDSIZE == 64
  22. #define __NR_posix_fadvise64 __NR_fadvise64_64
  23. #if defined INTERNAL_SYSCALL && ! defined __TARGET_powerpc__
  24. int posix_fadvise64(int fd, __off64_t offset, __off64_t len, int advice)
  25. {
  26. if (len != (off_t) len)
  27. return EOVERFLOW;
  28. INTERNAL_SYSCALL_DECL (err);
  29. int ret = INTERNAL_SYSCALL (posix_fadvise64, err, 5, fd,
  30. __LONG_LONG_PAIR ((long) (offset >> 32),
  31. (long) offset),
  32. (off_t) len, advice);
  33. if (!INTERNAL_SYSCALL_ERROR_P (ret, err))
  34. return 0;
  35. return INTERNAL_SYSCALL_ERRNO (ret, err);
  36. }
  37. #else
  38. static __inline__ int syscall_posix_fadvise(int fd, off_t offset1, off_t offset2, off_t len, int advice);
  39. #define __NR_syscall_posix_fadvise64 __NR_posix_fadvise64
  40. _syscall4(int, syscall_posix_fadvise64, int, fd, __off64_t, offset,
  41. __off64_t, len, int, advice)
  42. int posix_fadvise64(int fd, __off64_t offset, __off64_t len, int advice)
  43. {
  44. int ret = syscall_posix_fadvise64(fd, offset, len, advice);
  45. if (ret == -1)
  46. return errno;
  47. return ret;
  48. }
  49. #endif
  50. /* 32 bit implementation is kind of a pita */
  51. #elif __WORDSIZE == 32
  52. #if defined INTERNAL_SYSCALL && ! defined __TARGET_powerpc__
  53. int posix_fadvise64(int fd, __off64_t offset, __off64_t len, int advice)
  54. {
  55. INTERNAL_SYSCALL_DECL (err);
  56. int ret = INTERNAL_SYSCALL (fadvise64_64, err, 6, fd,
  57. __LONG_LONG_PAIR(offset >> 32, offset & 0xffffffff),
  58. __LONG_LONG_PAIR(len >> 32, len & 0xffffffff),
  59. advice);
  60. if (!INTERNAL_SYSCALL_ERROR_P (ret, err))
  61. return 0;
  62. return INTERNAL_SYSCALL_ERRNO (ret, err);
  63. }
  64. #elif defined _syscall6 /* workaround until everyone has _syscall6() */
  65. #define __NR___syscall_fadvise64_64 __NR_fadvise64_64
  66. static __inline__ _syscall6(int, __syscall_fadvise64_64, int, fd,
  67. unsigned long, high_offset, unsigned long, low_offset,
  68. unsigned long, high_len, unsigned long, low_len,
  69. int, advice)
  70. int posix_fadvise64(int fd, __off64_t offset, __off64_t len, int advice)
  71. {
  72. int ret = __syscall_fadvise64_64(fd,
  73. __LONG_LONG_PAIR(offset >> 32, offset & 0xffffffff),
  74. __LONG_LONG_PAIR(len >> 32, len & 0xffffffff),
  75. advice);
  76. if (ret == -1)
  77. return errno;
  78. return ret;
  79. }
  80. #else
  81. #warning neither INTERNAL_SYSCALL nor _syscall6 has been defined for your machine :(
  82. #endif /* INTERNAL_SYSCALL */
  83. #else
  84. #error your machine is neither 32 bit or 64 bit ... it must be magical
  85. #endif
  86. #elif !defined __NR_fadvise64
  87. /* This is declared as a strong alias in posix_fadvise.c if __NR_fadvise64
  88. * is defined.
  89. */
  90. int posix_fadvise64(int fd, __off64_t offset, __off64_t len, int advice)
  91. {
  92. #warning This is not correct as far as SUSv3 is concerned.
  93. return ENOSYS;
  94. }
  95. #endif /* __NR_fadvise64_64 */
  96. #endif /* __UCLIBC_HAS_LFS__ */