raid.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /* Copyright (C) 2000 MySQL AB
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation; either version 2 of the License, or
  5. (at your option) any later version.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program; if not, write to the Free Software
  12. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
  13. /* Parser needs these defines always, even if USE_RAID is not defined */
  14. #define RAID_TYPE_0 1 /* Striping */
  15. #define RAID_TYPE_x 2 /* Some new modes */
  16. #define RAID_TYPE_y 3
  17. #define RAID_DEFAULT_CHUNKS 4
  18. #define RAID_DEFAULT_CHUNKSIZE 256*1024 /* 256kB */
  19. C_MODE_START
  20. #define my_raid_type(raid_type) raid_type_string[(int)(raid_type)]
  21. extern const char *raid_type_string[];
  22. C_MODE_END
  23. #ifdef DONT_USE_RAID
  24. #undef USE_RAID
  25. #endif
  26. #if defined(USE_RAID)
  27. #include "my_dir.h"
  28. /* Trap all occurences of my_...() in source and use our wrapper around this function */
  29. #ifdef MAP_TO_USE_RAID
  30. #define my_read(A,B,C,D) my_raid_read(A,B,C,D)
  31. #define my_write(A,B,C,D) my_raid_write(A,B,C,D)
  32. #define my_pwrite(A,B,C,D,E) my_raid_pwrite(A,B,C,D,E)
  33. #define my_pread(A,B,C,D,E) my_raid_pread(A,B,C,D,E)
  34. #define my_chsize(A,B,C,D) my_raid_chsize(A,B,C,D)
  35. #define my_close(A,B) my_raid_close(A,B)
  36. #define my_tell(A,B) my_raid_tell(A,B)
  37. #define my_seek(A,B,C,D) my_raid_seek(A,B,C,D)
  38. #define my_lock(A,B,C,D,E) my_raid_lock(A,B,C,D,E)
  39. #define my_fstat(A,B,C) my_raid_fstat(A,B,C)
  40. #endif /* MAP_TO_USE_RAID */
  41. #ifdef __cplusplus
  42. extern "C" {
  43. #endif
  44. void init_raid(void);
  45. void end_raid(void);
  46. bool is_raid(File fd);
  47. File my_raid_create(const char *FileName, int CreateFlags, int access_flags,
  48. uint raid_type, uint raid_chunks, ulong raid_chunksize,
  49. myf MyFlags);
  50. File my_raid_open(const char *FileName, int Flags,
  51. uint raid_type, uint raid_chunks, ulong raid_chunksize,
  52. myf MyFlags);
  53. int my_raid_rename(const char *from, const char *to, uint raid_chunks,
  54. myf MyFlags);
  55. int my_raid_delete(const char *from, uint raid_chunks, myf MyFlags);
  56. int my_raid_redel(const char *old_name, const char *new_name,
  57. uint raid_chunks, myf MyFlags);
  58. my_off_t my_raid_seek(File fd, my_off_t pos, int whence, myf MyFlags);
  59. my_off_t my_raid_tell(File fd, myf MyFlags);
  60. uint my_raid_write(File,const byte *Buffer, uint Count, myf MyFlags);
  61. uint my_raid_read(File Filedes, byte *Buffer, uint Count, myf MyFlags);
  62. uint my_raid_pread(File Filedes, byte *Buffer, uint Count, my_off_t offset,
  63. myf MyFlags);
  64. uint my_raid_pwrite(int Filedes, const byte *Buffer, uint Count,
  65. my_off_t offset, myf MyFlags);
  66. int my_raid_lock(File,int locktype, my_off_t start, my_off_t length,
  67. myf MyFlags);
  68. int my_raid_chsize(File fd, my_off_t newlength, int filler, myf MyFlags);
  69. int my_raid_close(File, myf MyFlags);
  70. int my_raid_fstat(int Filedes, struct stat *buf, myf MyFlags);
  71. #ifdef __cplusplus
  72. }
  73. #ifdef USE_PRAGMA_INTERFACE
  74. #pragma interface /* gcc class implementation */
  75. #endif
  76. class RaidName {
  77. public:
  78. RaidName(const char *FileName);
  79. ~RaidName();
  80. bool IsRaid();
  81. int Rename(const char * from, const char * to, myf MyFlags);
  82. private:
  83. uint _raid_type; /* RAID_TYPE_0 or RAID_TYPE_1 or RAID_TYPE_5 */
  84. uint _raid_chunks; /* 1..n */
  85. ulong _raid_chunksize; /* 1..n in bytes */
  86. };
  87. class RaidFd {
  88. public:
  89. RaidFd(uint raid_type, uint raid_chunks , ulong raid_chunksize);
  90. ~RaidFd();
  91. File Create(const char *FileName, int CreateFlags, int access_flags,
  92. myf MyFlags);
  93. File Open(const char *FileName, int Flags, myf MyFlags);
  94. my_off_t Seek(my_off_t pos,int whence,myf MyFlags);
  95. my_off_t Tell(myf MyFlags);
  96. int Write(const byte *Buffer, uint Count, myf MyFlags);
  97. int Read(const byte *Buffer, uint Count, myf MyFlags);
  98. int Lock(int locktype, my_off_t start, my_off_t length, myf MyFlags);
  99. int Chsize(File fd, my_off_t newlength, int filler, myf MyFlags);
  100. int Fstat(int fd, MY_STAT *stat_area, myf MyFlags );
  101. int Close(myf MyFlags);
  102. static bool IsRaid(File fd);
  103. static DYNAMIC_ARRAY _raid_map; /* Map of RaidFD* */
  104. private:
  105. uint _raid_type; /* RAID_TYPE_0 or RAID_TYPE_1 or RAID_TYPE_5 */
  106. uint _raid_chunks; /* 1..n */
  107. ulong _raid_chunksize; /* 1..n in bytes */
  108. ulong _total_block; /* We are operating with block no x (can be 0..many). */
  109. uint _this_block; /* can be 0.._raid_chunks */
  110. uint _remaining_bytes; /* Maximum bytes that can be written in this block */
  111. my_off_t _position;
  112. my_off_t _size; /* Cached file size for faster seek(SEEK_END) */
  113. File _fd;
  114. File *_fd_vector; /* Array of File */
  115. off_t *_seek_vector; /* Array of cached seek positions */
  116. inline void Calculate()
  117. {
  118. DBUG_ENTER("RaidFd::_Calculate");
  119. DBUG_PRINT("info",("_position: %lu _raid_chunksize: %d, _size: %lu",
  120. (ulong) _position, _raid_chunksize, (ulong) _size));
  121. _total_block = (ulong) (_position / _raid_chunksize);
  122. _this_block = _total_block % _raid_chunks; /* can be 0.._raid_chunks */
  123. _remaining_bytes = (uint) (_raid_chunksize -
  124. (_position - _total_block * _raid_chunksize));
  125. DBUG_PRINT("info",
  126. ("_total_block: %d this_block: %d _remaining_bytes:%d",
  127. _total_block, _this_block, _remaining_bytes));
  128. DBUG_VOID_RETURN;
  129. }
  130. };
  131. #endif /* __cplusplus */
  132. #endif /* USE_RAID */