logfile.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*
  2. * logfile.h - Defines for NTFS kernel journal ($LogFile) handling. Part of
  3. * the Linux-NTFS project.
  4. *
  5. * Copyright (c) 2000-2005 Anton Altaparmakov
  6. *
  7. * This program/include file is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as published
  9. * by the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program/include file is distributed in the hope that it will be
  13. * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
  14. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program (in the main directory of the Linux-NTFS
  19. * distribution in the file COPYING); if not, write to the Free Software
  20. * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. #ifndef _LINUX_NTFS_LOGFILE_H
  23. #define _LINUX_NTFS_LOGFILE_H
  24. #ifdef NTFS_RW
  25. #include <linux/fs.h>
  26. #include "types.h"
  27. #include "endian.h"
  28. #include "layout.h"
  29. /*
  30. * Journal ($LogFile) organization:
  31. *
  32. * Two restart areas present in the first two pages (restart pages, one restart
  33. * area in each page). When the volume is dismounted they should be identical,
  34. * except for the update sequence array which usually has a different update
  35. * sequence number.
  36. *
  37. * These are followed by log records organized in pages headed by a log record
  38. * header going up to log file size. Not all pages contain log records when a
  39. * volume is first formatted, but as the volume ages, all records will be used.
  40. * When the log file fills up, the records at the beginning are purged (by
  41. * modifying the oldest_lsn to a higher value presumably) and writing begins
  42. * at the beginning of the file. Effectively, the log file is viewed as a
  43. * circular entity.
  44. *
  45. * NOTE: Windows NT, 2000, and XP all use log file version 1.1 but they accept
  46. * versions <= 1.x, including 0.-1. (Yes, that is a minus one in there!) We
  47. * probably only want to support 1.1 as this seems to be the current version
  48. * and we don't know how that differs from the older versions. The only
  49. * exception is if the journal is clean as marked by the two restart pages
  50. * then it doesn't matter whether we are on an earlier version. We can just
  51. * reinitialize the logfile and start again with version 1.1.
  52. */
  53. /* Some $LogFile related constants. */
  54. #define MaxLogFileSize 0x100000000ULL
  55. #define DefaultLogPageSize 4096
  56. #define MinLogRecordPages 48
  57. /*
  58. * Log file restart page header (begins the restart area).
  59. */
  60. typedef struct {
  61. /*Ofs*/
  62. /* 0 NTFS_RECORD; -- Unfolded here as gcc doesn't like unnamed structs. */
  63. /* 0*/ NTFS_RECORD_TYPE magic; /* The magic is "RSTR". */
  64. /* 4*/ le16 usa_ofs; /* See NTFS_RECORD definition in layout.h.
  65. When creating, set this to be immediately
  66. after this header structure (without any
  67. alignment). */
  68. /* 6*/ le16 usa_count; /* See NTFS_RECORD definition in layout.h. */
  69. /* 8*/ leLSN chkdsk_lsn; /* The last log file sequence number found by
  70. chkdsk. Only used when the magic is changed
  71. to "CHKD". Otherwise this is zero. */
  72. /* 16*/ le32 system_page_size; /* Byte size of system pages when the log file
  73. was created, has to be >= 512 and a power of
  74. 2. Use this to calculate the required size
  75. of the usa (usa_count) and add it to usa_ofs.
  76. Then verify that the result is less than the
  77. value of the restart_area_offset. */
  78. /* 20*/ le32 log_page_size; /* Byte size of log file pages, has to be >=
  79. 512 and a power of 2. The default is 4096
  80. and is used when the system page size is
  81. between 4096 and 8192. Otherwise this is
  82. set to the system page size instead. */
  83. /* 24*/ le16 restart_area_offset;/* Byte offset from the start of this header to
  84. the RESTART_AREA. Value has to be aligned
  85. to 8-byte boundary. When creating, set this
  86. to be after the usa. */
  87. /* 26*/ sle16 minor_ver; /* Log file minor version. Only check if major
  88. version is 1. */
  89. /* 28*/ sle16 major_ver; /* Log file major version. We only support
  90. version 1.1. */
  91. /* sizeof() = 30 (0x1e) bytes */
  92. } __attribute__ ((__packed__)) RESTART_PAGE_HEADER;
  93. /*
  94. * Constant for the log client indices meaning that there are no client records
  95. * in this particular client array. Also inside the client records themselves,
  96. * this means that there are no client records preceding or following this one.
  97. */
  98. #define LOGFILE_NO_CLIENT cpu_to_le16(0xffff)
  99. #define LOGFILE_NO_CLIENT_CPU 0xffff
  100. /*
  101. * These are the so far known RESTART_AREA_* flags (16-bit) which contain
  102. * information about the log file in which they are present.
  103. */
  104. enum {
  105. RESTART_VOLUME_IS_CLEAN = cpu_to_le16(0x0002),
  106. RESTART_SPACE_FILLER = cpu_to_le16(0xffff), /* gcc: Force enum bit width to 16. */
  107. } __attribute__ ((__packed__));
  108. typedef le16 RESTART_AREA_FLAGS;
  109. /*
  110. * Log file restart area record. The offset of this record is found by adding
  111. * the offset of the RESTART_PAGE_HEADER to the restart_area_offset value found
  112. * in it. See notes at restart_area_offset above.
  113. */
  114. typedef struct {
  115. /*Ofs*/
  116. /* 0*/ leLSN current_lsn; /* The current, i.e. last LSN inside the log
  117. when the restart area was last written.
  118. This happens often but what is the interval?
  119. Is it just fixed time or is it every time a
  120. check point is written or somethine else?
  121. On create set to 0. */
  122. /* 8*/ le16 log_clients; /* Number of log client records in the array of
  123. log client records which follows this
  124. restart area. Must be 1. */
  125. /* 10*/ le16 client_free_list; /* The index of the first free log client record
  126. in the array of log client records.
  127. LOGFILE_NO_CLIENT means that there are no
  128. free log client records in the array.
  129. If != LOGFILE_NO_CLIENT, check that
  130. log_clients > client_free_list. On Win2k
  131. and presumably earlier, on a clean volume
  132. this is != LOGFILE_NO_CLIENT, and it should
  133. be 0, i.e. the first (and only) client
  134. record is free and thus the logfile is
  135. closed and hence clean. A dirty volume
  136. would have left the logfile open and hence
  137. this would be LOGFILE_NO_CLIENT. On WinXP
  138. and presumably later, the logfile is always
  139. open, even on clean shutdown so this should
  140. always be LOGFILE_NO_CLIENT. */
  141. /* 12*/ le16 client_in_use_list;/* The index of the first in-use log client
  142. record in the array of log client records.
  143. LOGFILE_NO_CLIENT means that there are no
  144. in-use log client records in the array. If
  145. != LOGFILE_NO_CLIENT check that log_clients
  146. > client_in_use_list. On Win2k and
  147. presumably earlier, on a clean volume this
  148. is LOGFILE_NO_CLIENT, i.e. there are no
  149. client records in use and thus the logfile
  150. is closed and hence clean. A dirty volume
  151. would have left the logfile open and hence
  152. this would be != LOGFILE_NO_CLIENT, and it
  153. should be 0, i.e. the first (and only)
  154. client record is in use. On WinXP and
  155. presumably later, the logfile is always
  156. open, even on clean shutdown so this should
  157. always be 0. */
  158. /* 14*/ RESTART_AREA_FLAGS flags;/* Flags modifying LFS behaviour. On Win2k
  159. and presumably earlier this is always 0. On
  160. WinXP and presumably later, if the logfile
  161. was shutdown cleanly, the second bit,
  162. RESTART_VOLUME_IS_CLEAN, is set. This bit
  163. is cleared when the volume is mounted by
  164. WinXP and set when the volume is dismounted,
  165. thus if the logfile is dirty, this bit is
  166. clear. Thus we don't need to check the
  167. Windows version to determine if the logfile
  168. is clean. Instead if the logfile is closed,
  169. we know it must be clean. If it is open and
  170. this bit is set, we also know it must be
  171. clean. If on the other hand the logfile is
  172. open and this bit is clear, we can be almost
  173. certain that the logfile is dirty. */
  174. /* 16*/ le32 seq_number_bits; /* How many bits to use for the sequence
  175. number. This is calculated as 67 - the
  176. number of bits required to store the logfile
  177. size in bytes and this can be used in with
  178. the specified file_size as a consistency
  179. check. */
  180. /* 20*/ le16 restart_area_length;/* Length of the restart area including the
  181. client array. Following checks required if
  182. version matches. Otherwise, skip them.
  183. restart_area_offset + restart_area_length
  184. has to be <= system_page_size. Also,
  185. restart_area_length has to be >=
  186. client_array_offset + (log_clients *
  187. sizeof(log client record)). */
  188. /* 22*/ le16 client_array_offset;/* Offset from the start of this record to
  189. the first log client record if versions are
  190. matched. When creating, set this to be
  191. after this restart area structure, aligned
  192. to 8-bytes boundary. If the versions do not
  193. match, this is ignored and the offset is
  194. assumed to be (sizeof(RESTART_AREA) + 7) &
  195. ~7, i.e. rounded up to first 8-byte
  196. boundary. Either way, client_array_offset
  197. has to be aligned to an 8-byte boundary.
  198. Also, restart_area_offset +
  199. client_array_offset has to be <= 510.
  200. Finally, client_array_offset + (log_clients
  201. * sizeof(log client record)) has to be <=
  202. system_page_size. On Win2k and presumably
  203. earlier, this is 0x30, i.e. immediately
  204. following this record. On WinXP and
  205. presumably later, this is 0x40, i.e. there
  206. are 16 extra bytes between this record and
  207. the client array. This probably means that
  208. the RESTART_AREA record is actually bigger
  209. in WinXP and later. */
  210. /* 24*/ sle64 file_size; /* Usable byte size of the log file. If the
  211. restart_area_offset + the offset of the
  212. file_size are > 510 then corruption has
  213. occurred. This is the very first check when
  214. starting with the restart_area as if it
  215. fails it means that some of the above values
  216. will be corrupted by the multi sector
  217. transfer protection. The file_size has to
  218. be rounded down to be a multiple of the
  219. log_page_size in the RESTART_PAGE_HEADER and
  220. then it has to be at least big enough to
  221. store the two restart pages and 48 (0x30)
  222. log record pages. */
  223. /* 32*/ le32 last_lsn_data_length;/* Length of data of last LSN, not including
  224. the log record header. On create set to
  225. 0. */
  226. /* 36*/ le16 log_record_header_length;/* Byte size of the log record header.
  227. If the version matches then check that the
  228. value of log_record_header_length is a
  229. multiple of 8, i.e.
  230. (log_record_header_length + 7) & ~7 ==
  231. log_record_header_length. When creating set
  232. it to sizeof(LOG_RECORD_HEADER), aligned to
  233. 8 bytes. */
  234. /* 38*/ le16 log_page_data_offset;/* Offset to the start of data in a log record
  235. page. Must be a multiple of 8. On create
  236. set it to immediately after the update
  237. sequence array of the log record page. */
  238. /* 40*/ le32 restart_log_open_count;/* A counter that gets incremented every
  239. time the logfile is restarted which happens
  240. at mount time when the logfile is opened.
  241. When creating set to a random value. Win2k
  242. sets it to the low 32 bits of the current
  243. system time in NTFS format (see time.h). */
  244. /* 44*/ le32 reserved; /* Reserved/alignment to 8-byte boundary. */
  245. /* sizeof() = 48 (0x30) bytes */
  246. } __attribute__ ((__packed__)) RESTART_AREA;
  247. /*
  248. * Log client record. The offset of this record is found by adding the offset
  249. * of the RESTART_AREA to the client_array_offset value found in it.
  250. */
  251. typedef struct {
  252. /*Ofs*/
  253. /* 0*/ leLSN oldest_lsn; /* Oldest LSN needed by this client. On create
  254. set to 0. */
  255. /* 8*/ leLSN client_restart_lsn;/* LSN at which this client needs to restart
  256. the volume, i.e. the current position within
  257. the log file. At present, if clean this
  258. should = current_lsn in restart area but it
  259. probably also = current_lsn when dirty most
  260. of the time. At create set to 0. */
  261. /* 16*/ le16 prev_client; /* The offset to the previous log client record
  262. in the array of log client records.
  263. LOGFILE_NO_CLIENT means there is no previous
  264. client record, i.e. this is the first one.
  265. This is always LOGFILE_NO_CLIENT. */
  266. /* 18*/ le16 next_client; /* The offset to the next log client record in
  267. the array of log client records.
  268. LOGFILE_NO_CLIENT means there are no next
  269. client records, i.e. this is the last one.
  270. This is always LOGFILE_NO_CLIENT. */
  271. /* 20*/ le16 seq_number; /* On Win2k and presumably earlier, this is set
  272. to zero every time the logfile is restarted
  273. and it is incremented when the logfile is
  274. closed at dismount time. Thus it is 0 when
  275. dirty and 1 when clean. On WinXP and
  276. presumably later, this is always 0. */
  277. /* 22*/ u8 reserved[6]; /* Reserved/alignment. */
  278. /* 28*/ le32 client_name_length;/* Length of client name in bytes. Should
  279. always be 8. */
  280. /* 32*/ ntfschar client_name[64];/* Name of the client in Unicode. Should
  281. always be "NTFS" with the remaining bytes
  282. set to 0. */
  283. /* sizeof() = 160 (0xa0) bytes */
  284. } __attribute__ ((__packed__)) LOG_CLIENT_RECORD;
  285. extern bool ntfs_check_logfile(struct inode *log_vi,
  286. RESTART_PAGE_HEADER **rp);
  287. extern bool ntfs_is_logfile_clean(struct inode *log_vi,
  288. const RESTART_PAGE_HEADER *rp);
  289. extern bool ntfs_empty_logfile(struct inode *log_vi);
  290. #endif /* NTFS_RW */
  291. #endif /* _LINUX_NTFS_LOGFILE_H */