xfs-self-describing-metadata.txt 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. XFS Self Describing Metadata
  2. ----------------------------
  3. Introduction
  4. ------------
  5. The largest scalability problem facing XFS is not one of algorithmic
  6. scalability, but of verification of the filesystem structure. Scalabilty of the
  7. structures and indexes on disk and the algorithms for iterating them are
  8. adequate for supporting PB scale filesystems with billions of inodes, however it
  9. is this very scalability that causes the verification problem.
  10. Almost all metadata on XFS is dynamically allocated. The only fixed location
  11. metadata is the allocation group headers (SB, AGF, AGFL and AGI), while all
  12. other metadata structures need to be discovered by walking the filesystem
  13. structure in different ways. While this is already done by userspace tools for
  14. validating and repairing the structure, there are limits to what they can
  15. verify, and this in turn limits the supportable size of an XFS filesystem.
  16. For example, it is entirely possible to manually use xfs_db and a bit of
  17. scripting to analyse the structure of a 100TB filesystem when trying to
  18. determine the root cause of a corruption problem, but it is still mainly a
  19. manual task of verifying that things like single bit errors or misplaced writes
  20. weren't the ultimate cause of a corruption event. It may take a few hours to a
  21. few days to perform such forensic analysis, so for at this scale root cause
  22. analysis is entirely possible.
  23. However, if we scale the filesystem up to 1PB, we now have 10x as much metadata
  24. to analyse and so that analysis blows out towards weeks/months of forensic work.
  25. Most of the analysis work is slow and tedious, so as the amount of analysis goes
  26. up, the more likely that the cause will be lost in the noise. Hence the primary
  27. concern for supporting PB scale filesystems is minimising the time and effort
  28. required for basic forensic analysis of the filesystem structure.
  29. Self Describing Metadata
  30. ------------------------
  31. One of the problems with the current metadata format is that apart from the
  32. magic number in the metadata block, we have no other way of identifying what it
  33. is supposed to be. We can't even identify if it is the right place. Put simply,
  34. you can't look at a single metadata block in isolation and say "yes, it is
  35. supposed to be there and the contents are valid".
  36. Hence most of the time spent on forensic analysis is spent doing basic
  37. verification of metadata values, looking for values that are in range (and hence
  38. not detected by automated verification checks) but are not correct. Finding and
  39. understanding how things like cross linked block lists (e.g. sibling
  40. pointers in a btree end up with loops in them) are the key to understanding what
  41. went wrong, but it is impossible to tell what order the blocks were linked into
  42. each other or written to disk after the fact.
  43. Hence we need to record more information into the metadata to allow us to
  44. quickly determine if the metadata is intact and can be ignored for the purpose
  45. of analysis. We can't protect against every possible type of error, but we can
  46. ensure that common types of errors are easily detectable. Hence the concept of
  47. self describing metadata.
  48. The first, fundamental requirement of self describing metadata is that the
  49. metadata object contains some form of unique identifier in a well known
  50. location. This allows us to identify the expected contents of the block and
  51. hence parse and verify the metadata object. IF we can't independently identify
  52. the type of metadata in the object, then the metadata doesn't describe itself
  53. very well at all!
  54. Luckily, almost all XFS metadata has magic numbers embedded already - only the
  55. AGFL, remote symlinks and remote attribute blocks do not contain identifying
  56. magic numbers. Hence we can change the on-disk format of all these objects to
  57. add more identifying information and detect this simply by changing the magic
  58. numbers in the metadata objects. That is, if it has the current magic number,
  59. the metadata isn't self identifying. If it contains a new magic number, it is
  60. self identifying and we can do much more expansive automated verification of the
  61. metadata object at runtime, during forensic analysis or repair.
  62. As a primary concern, self describing metadata needs some form of overall
  63. integrity checking. We cannot trust the metadata if we cannot verify that it has
  64. not been changed as a result of external influences. Hence we need some form of
  65. integrity check, and this is done by adding CRC32c validation to the metadata
  66. block. If we can verify the block contains the metadata it was intended to
  67. contain, a large amount of the manual verification work can be skipped.
  68. CRC32c was selected as metadata cannot be more than 64k in length in XFS and
  69. hence a 32 bit CRC is more than sufficient to detect multi-bit errors in
  70. metadata blocks. CRC32c is also now hardware accelerated on common CPUs so it is
  71. fast. So while CRC32c is not the strongest of possible integrity checks that
  72. could be used, it is more than sufficient for our needs and has relatively
  73. little overhead. Adding support for larger integrity fields and/or algorithms
  74. does really provide any extra value over CRC32c, but it does add a lot of
  75. complexity and so there is no provision for changing the integrity checking
  76. mechanism.
  77. Self describing metadata needs to contain enough information so that the
  78. metadata block can be verified as being in the correct place without needing to
  79. look at any other metadata. This means it needs to contain location information.
  80. Just adding a block number to the metadata is not sufficient to protect against
  81. mis-directed writes - a write might be misdirected to the wrong LUN and so be
  82. written to the "correct block" of the wrong filesystem. Hence location
  83. information must contain a filesystem identifier as well as a block number.
  84. Another key information point in forensic analysis is knowing who the metadata
  85. block belongs to. We already know the type, the location, that it is valid
  86. and/or corrupted, and how long ago that it was last modified. Knowing the owner
  87. of the block is important as it allows us to find other related metadata to
  88. determine the scope of the corruption. For example, if we have a extent btree
  89. object, we don't know what inode it belongs to and hence have to walk the entire
  90. filesystem to find the owner of the block. Worse, the corruption could mean that
  91. no owner can be found (i.e. it's an orphan block), and so without an owner field
  92. in the metadata we have no idea of the scope of the corruption. If we have an
  93. owner field in the metadata object, we can immediately do top down validation to
  94. determine the scope of the problem.
  95. Different types of metadata have different owner identifiers. For example,
  96. directory, attribute and extent tree blocks are all owned by an inode, whilst
  97. freespace btree blocks are owned by an allocation group. Hence the size and
  98. contents of the owner field are determined by the type of metadata object we are
  99. looking at. The owner information can also identify misplaced writes (e.g.
  100. freespace btree block written to the wrong AG).
  101. Self describing metadata also needs to contain some indication of when it was
  102. written to the filesystem. One of the key information points when doing forensic
  103. analysis is how recently the block was modified. Correlation of set of corrupted
  104. metadata blocks based on modification times is important as it can indicate
  105. whether the corruptions are related, whether there's been multiple corruption
  106. events that lead to the eventual failure, and even whether there are corruptions
  107. present that the run-time verification is not detecting.
  108. For example, we can determine whether a metadata object is supposed to be free
  109. space or still allocated if it is still referenced by its owner by looking at
  110. when the free space btree block that contains the block was last written
  111. compared to when the metadata object itself was last written. If the free space
  112. block is more recent than the object and the object's owner, then there is a
  113. very good chance that the block should have been removed from the owner.
  114. To provide this "written timestamp", each metadata block gets the Log Sequence
  115. Number (LSN) of the most recent transaction it was modified on written into it.
  116. This number will always increase over the life of the filesystem, and the only
  117. thing that resets it is running xfs_repair on the filesystem. Further, by use of
  118. the LSN we can tell if the corrupted metadata all belonged to the same log
  119. checkpoint and hence have some idea of how much modification occurred between
  120. the first and last instance of corrupt metadata on disk and, further, how much
  121. modification occurred between the corruption being written and when it was
  122. detected.
  123. Runtime Validation
  124. ------------------
  125. Validation of self-describing metadata takes place at runtime in two places:
  126. - immediately after a successful read from disk
  127. - immediately prior to write IO submission
  128. The verification is completely stateless - it is done independently of the
  129. modification process, and seeks only to check that the metadata is what it says
  130. it is and that the metadata fields are within bounds and internally consistent.
  131. As such, we cannot catch all types of corruption that can occur within a block
  132. as there may be certain limitations that operational state enforces of the
  133. metadata, or there may be corruption of interblock relationships (e.g. corrupted
  134. sibling pointer lists). Hence we still need stateful checking in the main code
  135. body, but in general most of the per-field validation is handled by the
  136. verifiers.
  137. For read verification, the caller needs to specify the expected type of metadata
  138. that it should see, and the IO completion process verifies that the metadata
  139. object matches what was expected. If the verification process fails, then it
  140. marks the object being read as EFSCORRUPTED. The caller needs to catch this
  141. error (same as for IO errors), and if it needs to take special action due to a
  142. verification error it can do so by catching the EFSCORRUPTED error value. If we
  143. need more discrimination of error type at higher levels, we can define new
  144. error numbers for different errors as necessary.
  145. The first step in read verification is checking the magic number and determining
  146. whether CRC validating is necessary. If it is, the CRC32c is calculated and
  147. compared against the value stored in the object itself. Once this is validated,
  148. further checks are made against the location information, followed by extensive
  149. object specific metadata validation. If any of these checks fail, then the
  150. buffer is considered corrupt and the EFSCORRUPTED error is set appropriately.
  151. Write verification is the opposite of the read verification - first the object
  152. is extensively verified and if it is OK we then update the LSN from the last
  153. modification made to the object, After this, we calculate the CRC and insert it
  154. into the object. Once this is done the write IO is allowed to continue. If any
  155. error occurs during this process, the buffer is again marked with a EFSCORRUPTED
  156. error for the higher layers to catch.
  157. Structures
  158. ----------
  159. A typical on-disk structure needs to contain the following information:
  160. struct xfs_ondisk_hdr {
  161. __be32 magic; /* magic number */
  162. __be32 crc; /* CRC, not logged */
  163. uuid_t uuid; /* filesystem identifier */
  164. __be64 owner; /* parent object */
  165. __be64 blkno; /* location on disk */
  166. __be64 lsn; /* last modification in log, not logged */
  167. };
  168. Depending on the metadata, this information may be part of a header structure
  169. separate to the metadata contents, or may be distributed through an existing
  170. structure. The latter occurs with metadata that already contains some of this
  171. information, such as the superblock and AG headers.
  172. Other metadata may have different formats for the information, but the same
  173. level of information is generally provided. For example:
  174. - short btree blocks have a 32 bit owner (ag number) and a 32 bit block
  175. number for location. The two of these combined provide the same
  176. information as @owner and @blkno in eh above structure, but using 8
  177. bytes less space on disk.
  178. - directory/attribute node blocks have a 16 bit magic number, and the
  179. header that contains the magic number has other information in it as
  180. well. hence the additional metadata headers change the overall format
  181. of the metadata.
  182. A typical buffer read verifier is structured as follows:
  183. #define XFS_FOO_CRC_OFF offsetof(struct xfs_ondisk_hdr, crc)
  184. static void
  185. xfs_foo_read_verify(
  186. struct xfs_buf *bp)
  187. {
  188. struct xfs_mount *mp = bp->b_target->bt_mount;
  189. if ((xfs_sb_version_hascrc(&mp->m_sb) &&
  190. !xfs_verify_cksum(bp->b_addr, BBTOB(bp->b_length),
  191. XFS_FOO_CRC_OFF)) ||
  192. !xfs_foo_verify(bp)) {
  193. XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
  194. xfs_buf_ioerror(bp, EFSCORRUPTED);
  195. }
  196. }
  197. The code ensures that the CRC is only checked if the filesystem has CRCs enabled
  198. by checking the superblock of the feature bit, and then if the CRC verifies OK
  199. (or is not needed) it verifies the actual contents of the block.
  200. The verifier function will take a couple of different forms, depending on
  201. whether the magic number can be used to determine the format of the block. In
  202. the case it can't, the code is structured as follows:
  203. static bool
  204. xfs_foo_verify(
  205. struct xfs_buf *bp)
  206. {
  207. struct xfs_mount *mp = bp->b_target->bt_mount;
  208. struct xfs_ondisk_hdr *hdr = bp->b_addr;
  209. if (hdr->magic != cpu_to_be32(XFS_FOO_MAGIC))
  210. return false;
  211. if (!xfs_sb_version_hascrc(&mp->m_sb)) {
  212. if (!uuid_equal(&hdr->uuid, &mp->m_sb.sb_uuid))
  213. return false;
  214. if (bp->b_bn != be64_to_cpu(hdr->blkno))
  215. return false;
  216. if (hdr->owner == 0)
  217. return false;
  218. }
  219. /* object specific verification checks here */
  220. return true;
  221. }
  222. If there are different magic numbers for the different formats, the verifier
  223. will look like:
  224. static bool
  225. xfs_foo_verify(
  226. struct xfs_buf *bp)
  227. {
  228. struct xfs_mount *mp = bp->b_target->bt_mount;
  229. struct xfs_ondisk_hdr *hdr = bp->b_addr;
  230. if (hdr->magic == cpu_to_be32(XFS_FOO_CRC_MAGIC)) {
  231. if (!uuid_equal(&hdr->uuid, &mp->m_sb.sb_uuid))
  232. return false;
  233. if (bp->b_bn != be64_to_cpu(hdr->blkno))
  234. return false;
  235. if (hdr->owner == 0)
  236. return false;
  237. } else if (hdr->magic != cpu_to_be32(XFS_FOO_MAGIC))
  238. return false;
  239. /* object specific verification checks here */
  240. return true;
  241. }
  242. Write verifiers are very similar to the read verifiers, they just do things in
  243. the opposite order to the read verifiers. A typical write verifier:
  244. static void
  245. xfs_foo_write_verify(
  246. struct xfs_buf *bp)
  247. {
  248. struct xfs_mount *mp = bp->b_target->bt_mount;
  249. struct xfs_buf_log_item *bip = bp->b_fspriv;
  250. if (!xfs_foo_verify(bp)) {
  251. XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
  252. xfs_buf_ioerror(bp, EFSCORRUPTED);
  253. return;
  254. }
  255. if (!xfs_sb_version_hascrc(&mp->m_sb))
  256. return;
  257. if (bip) {
  258. struct xfs_ondisk_hdr *hdr = bp->b_addr;
  259. hdr->lsn = cpu_to_be64(bip->bli_item.li_lsn);
  260. }
  261. xfs_update_cksum(bp->b_addr, BBTOB(bp->b_length), XFS_FOO_CRC_OFF);
  262. }
  263. This will verify the internal structure of the metadata before we go any
  264. further, detecting corruptions that have occurred as the metadata has been
  265. modified in memory. If the metadata verifies OK, and CRCs are enabled, we then
  266. update the LSN field (when it was last modified) and calculate the CRC on the
  267. metadata. Once this is done, we can issue the IO.
  268. Inodes and Dquots
  269. -----------------
  270. Inodes and dquots are special snowflakes. They have per-object CRC and
  271. self-identifiers, but they are packed so that there are multiple objects per
  272. buffer. Hence we do not use per-buffer verifiers to do the work of per-object
  273. verification and CRC calculations. The per-buffer verifiers simply perform basic
  274. identification of the buffer - that they contain inodes or dquots, and that
  275. there are magic numbers in all the expected spots. All further CRC and
  276. verification checks are done when each inode is read from or written back to the
  277. buffer.
  278. The structure of the verifiers and the identifiers checks is very similar to the
  279. buffer code described above. The only difference is where they are called. For
  280. example, inode read verification is done in xfs_iread() when the inode is first
  281. read out of the buffer and the struct xfs_inode is instantiated. The inode is
  282. already extensively verified during writeback in xfs_iflush_int, so the only
  283. addition here is to add the LSN and CRC to the inode as it is copied back into
  284. the buffer.
  285. XXX: inode unlinked list modification doesn't recalculate the inode CRC! None of
  286. the unlinked list modifications check or update CRCs, neither during unlink nor
  287. log recovery. So, it's gone unnoticed until now. This won't matter immediately -
  288. repair will probably complain about it - but it needs to be fixed.