github.com/dennwc/btrfs@v0.0.0-20221026161108-3097362dc072/btrfs_tree_h.go (about)

     1  package btrfs
     2  
     3  /*
     4   * This header contains the structure definitions and constants used
     5   * by file system objects that can be retrieved using
     6   * the _BTRFS_IOC_SEARCH_TREE ioctl.  That means basically anything that
     7   * is needed to describe a leaf node's key or item contents.
     8   */
     9  
    10  /* holds pointers to all of the tree roots */
    11  
    12  /* stores information about which extents are in use, and reference counts */
    13  
    14  /*
    15   * chunk tree stores translations from logical -> physical block numbering
    16   * the super block points to the chunk tree
    17   */
    18  
    19  /*
    20   * stores information about which areas of a given device are in use.
    21   * one per device.  The tree of tree roots points to the device tree
    22   */
    23  
    24  /* one per subvolume, storing files and directories */
    25  
    26  /* directory objectid inside the root tree */
    27  
    28  /* holds checksums of all the data extents */
    29  
    30  /* holds quota configuration and tracking */
    31  
    32  /* for storing items that use the _BTRFS_UUID_KEY* types */
    33  
    34  /* tracks free space in block groups. */
    35  
    36  /* device stats in the device tree */
    37  
    38  /* for storing balance parameters in the root tree */
    39  
    40  /* orhpan objectid for tracking unlinked/truncated files */
    41  
    42  /* does write ahead logging to speed up fsyncs */
    43  
    44  /* for space balancing */
    45  
    46  /*
    47   * extent checksums all have this objectid
    48   * this allows them to share the logging tree
    49   * for fsyncs
    50   */
    51  
    52  /* For storing free space cache */
    53  
    54  /*
    55   * The inode number assigned to the special inode for storing
    56   * free ino cache
    57   */
    58  
    59  /* dummy objectid represents multiple objectids */
    60  
    61  /*
    62   * All files have objectids in this range.
    63   */
    64  
    65  /*
    66   * the device items go into the chunk tree.  The key is in the form
    67   * [ 1 _BTRFS_DEV_ITEM_KEY device_id ]
    68   */
    69  
    70  /*
    71   * inode items have the data typically returned from stat and store other
    72   * info about object characteristics.  There is one for every file and dir in
    73   * the FS
    74   */
    75  
    76  /* reserve 2-15 close to the inode for later flexibility */
    77  
    78  /*
    79   * dir items are the name -> inode pointers in a directory.  There is one
    80   * for every name in a directory.
    81   */
    82  
    83  /*
    84   * extent data is for file data
    85   */
    86  
    87  /*
    88   * extent csums are stored in a separate tree and hold csums for
    89   * an entire extent on disk.
    90   */
    91  
    92  /*
    93   * root items point to tree roots.  They are typically in the root
    94   * tree used by the super block to find all the other trees
    95   */
    96  
    97  /*
    98   * root backrefs tie subvols and snapshots to the directory entries that
    99   * reference them
   100   */
   101  
   102  /*
   103   * root refs make a fast index for listing all of the snapshots and
   104   * subvolumes referenced by a given root.  They point directly to the
   105   * directory item in the root that references the subvol
   106   */
   107  
   108  /*
   109   * extent items are in the extent map tree.  These record which blocks
   110   * are used, and how many references there are to each block
   111   */
   112  
   113  /*
   114   * The same as the _BTRFS_EXTENT_ITEM_KEY, except it's metadata we already know
   115   * the length, so we save the level in key->offset instead of the length.
   116   */
   117  
   118  /*
   119   * block groups give us hints into the extent allocation trees.  Which
   120   * blocks are free etc etc
   121   */
   122  
   123  /*
   124   * Every block group is represented in the free space tree by a free space info
   125   * item, which stores some accounting information. It is keyed on
   126   * (block_group_start, FREE_SPACE_INFO, block_group_length).
   127   */
   128  
   129  /*
   130   * A free space extent tracks an extent of space that is free in a block group.
   131   * It is keyed on (start, FREE_SPACE_EXTENT, length).
   132   */
   133  
   134  /*
   135   * When a block group becomes very fragmented, we convert it to use bitmaps
   136   * instead of extents. A free space bitmap is keyed on
   137   * (start, FREE_SPACE_BITMAP, length); the corresponding item is a bitmap with
   138   * (length / sectorsize) bits.
   139   */
   140  
   141  /*
   142   * Records the overall state of the qgroups.
   143   * There's only one instance of this key present,
   144   * (0, _BTRFS_QGROUP_STATUS_KEY, 0)
   145   */
   146  
   147  /*
   148   * Records the currently used space of the qgroup.
   149   * One key per qgroup, (0, _BTRFS_QGROUP_INFO_KEY, qgroupid).
   150   */
   151  
   152  /*
   153   * Contains the user configured limits for the qgroup.
   154   * One key per qgroup, (0, _BTRFS_QGROUP_LIMIT_KEY, qgroupid).
   155   */
   156  
   157  /*
   158   * Records the child-parent relationship of qgroups. For
   159   * each relation, 2 keys are present:
   160   * (childid, _BTRFS_QGROUP_RELATION_KEY, parentid)
   161   * (parentid, _BTRFS_QGROUP_RELATION_KEY, childid)
   162   */
   163  
   164  /*
   165   * Obsolete name, see _BTRFS_TEMPORARY_ITEM_KEY.
   166   */
   167  
   168  /*
   169   * The key type for tree items that are stored persistently, but do not need to
   170   * exist for extended period of time. The items can exist in any tree.
   171   *
   172   * [subtype, _BTRFS_TEMPORARY_ITEM_KEY, data]
   173   *
   174   * Existing items:
   175   *
   176   * - balance status item
   177   *   (_BTRFS_BALANCE_OBJECTID, _BTRFS_TEMPORARY_ITEM_KEY, 0)
   178   */
   179  
   180  /*
   181   * Obsolete name, see _BTRFS_PERSISTENT_ITEM_KEY
   182   */
   183  
   184  /*
   185   * The key type for tree items that are stored persistently and usually exist
   186   * for a long period, eg. filesystem lifetime. The item kinds can be status
   187   * information, stats or preference values. The item can exist in any tree.
   188   *
   189   * [subtype, _BTRFS_PERSISTENT_ITEM_KEY, data]
   190   *
   191   * Existing items:
   192   *
   193   * - device statistics, store IO stats in the device tree, one key for all
   194   *   stats
   195   *   (_BTRFS_DEV_STATS_OBJECTID, _BTRFS_DEV_STATS_KEY, 0)
   196   */
   197  
   198  /*
   199   * Persistantly stores the device replace state in the device tree.
   200   * The key is built like this: (0, _BTRFS_DEV_REPLACE_KEY, 0).
   201   */
   202  
   203  /*
   204   * Stores items that allow to quickly map UUIDs to something else.
   205   * These items are part of the filesystem UUID tree.
   206   * The key is built like this:
   207   * (UUID_upper_64_bits, _BTRFS_UUID_KEY*, UUID_lower_64_bits).
   208   */
   209  
   210  /* for UUIDs assigned to * received subvols */
   211  
   212  /*
   213   * string items are for debugging.  They just store a short string of
   214   * data in the FS
   215   */
   216  
   217  /* 32 bytes in various csum fields */
   218  
   219  /* csum types */
   220  
   221  /*
   222   * flags definitions for directory entry item type
   223   *
   224   * Used by:
   225   * struct btrfs_dir_item.type
   226   */
   227  
   228  /*
   229   * The key defines the order in the tree, and so it also defines (optimal)
   230   * block layout.
   231   *
   232   * objectid corresponds to the inode number.
   233   *
   234   * type tells us things about the object, and is a kind of stream selector.
   235   * so for a given inode, keys with type of 1 might refer to the inode data,
   236   * type of 2 may point to file data in the btree and type == 3 may point to
   237   * extents.
   238   *
   239   * offset is the starting byte offset for this key in the stream.
   240   *
   241   * btrfs_disk_key is in disk byte order.  struct btrfs_key is always
   242   * in cpu native order.  Otherwise they are identical and their sizes
   243   * should be the same (ie both packed)
   244   */
   245  type btrfs_disk_key struct {
   246  	objectid uint64
   247  	type_    uint8
   248  	offset   uint64
   249  }
   250  
   251  type btrfs_key struct {
   252  	objectid uint64
   253  	type_    uint8
   254  	offset   uint64
   255  }
   256  
   257  type btrfs_dev_item struct {
   258  	devid        uint64
   259  	total_bytes  uint64
   260  	bytes_used   uint64
   261  	io_align     uint32
   262  	io_width     uint32
   263  	sector_size  uint32
   264  	type_        uint64
   265  	generation   uint64
   266  	start_offset uint64
   267  	dev_group    uint32
   268  	seek_speed   uint8
   269  	bandwidth    uint8
   270  	uuid         UUID
   271  	fsid         FSID
   272  }
   273  
   274  type btrfs_stripe struct {
   275  	devid    uint64
   276  	offset   uint64
   277  	dev_uuid UUID
   278  }
   279  
   280  type btrfs_chunk struct {
   281  	length      uint64
   282  	owner       uint64
   283  	stripe_len  uint64
   284  	type_       uint64
   285  	io_align    uint32
   286  	io_width    uint32
   287  	sector_size uint32
   288  	num_stripes uint16
   289  	sub_stripes uint16
   290  	stripe      struct {
   291  		devid    uint64
   292  		offset   uint64
   293  		dev_uuid UUID
   294  	}
   295  }
   296  
   297  /* additional stripes go here */
   298  type btrfs_free_space_entry struct {
   299  	offset uint64
   300  	bytes  uint64
   301  	type_  uint8
   302  }
   303  
   304  type btrfs_free_space_header struct {
   305  	location struct {
   306  		objectid uint64
   307  		type_    uint8
   308  		offset   uint64
   309  	}
   310  	generation  uint64
   311  	num_entries uint64
   312  	num_bitmaps uint64
   313  }
   314  
   315  /* Super block flags */
   316  /* Errors detected */
   317  
   318  /*
   319   * items in the extent btree are used to record the objectid of the
   320   * owner of the block and the number of references
   321   */
   322  type btrfs_extent_item struct {
   323  	refs       uint64
   324  	generation uint64
   325  	flags      uint64
   326  }
   327  
   328  type btrfs_extent_item_v0 struct {
   329  	refs uint32
   330  }
   331  
   332  /* following flags only apply to tree blocks */
   333  
   334  /* use full backrefs for extent pointers in the block */
   335  
   336  /*
   337   * this flag is only used internally by scrub and may be changed at any time
   338   * it is only declared here to avoid collisions
   339   */
   340  type btrfs_tree_block_info struct {
   341  	key struct {
   342  		objectid uint64
   343  		type_    uint8
   344  		offset   uint64
   345  	}
   346  	level uint8
   347  }
   348  
   349  type btrfs_extent_data_ref struct {
   350  	root     uint64
   351  	objectid uint64
   352  	offset   uint64
   353  	count    uint32
   354  }
   355  
   356  type btrfs_shared_data_ref struct {
   357  	count uint32
   358  }
   359  
   360  type btrfs_extent_inline_ref struct {
   361  	type_  uint8
   362  	offset uint64
   363  }
   364  
   365  /* old style backrefs item */
   366  type btrfs_extent_ref_v0 struct {
   367  	root       uint64
   368  	generation uint64
   369  	objectid   uint64
   370  	count      uint32
   371  }
   372  
   373  /* dev extents record free space on individual devices.  The owner
   374   * field points back to the chunk allocation mapping tree that allocated
   375   * the extent.  The chunk tree uuid field is a way to double check the owner
   376   */
   377  type btrfs_dev_extent struct {
   378  	chunk_tree      uint64
   379  	chunk_objectid  uint64
   380  	chunk_offset    uint64
   381  	length          uint64
   382  	chunk_tree_uuid UUID
   383  }
   384  
   385  type btrfs_inode_ref struct {
   386  	index    uint64
   387  	name_len uint16
   388  }
   389  
   390  /* name goes here */
   391  type btrfs_inode_extref struct {
   392  	parent_objectid uint64
   393  	index           uint64
   394  	name_len        uint16
   395  	//name            [0]uint8
   396  }
   397  
   398  /* name goes here */
   399  type btrfs_timespec struct {
   400  	sec  uint64
   401  	nsec uint32
   402  }
   403  
   404  type btrfs_inode_item struct {
   405  	generation  uint64
   406  	transid     uint64
   407  	size        uint64
   408  	nbytes      uint64
   409  	block_group uint64
   410  	nlink       uint32
   411  	uid         uint32
   412  	gid         uint32
   413  	mode        uint32
   414  	rdev        uint64
   415  	flags       uint64
   416  	sequence    uint64
   417  	reserved    [4]uint64
   418  	atime       struct {
   419  		sec  uint64
   420  		nsec uint32
   421  	}
   422  	ctime struct {
   423  		sec  uint64
   424  		nsec uint32
   425  	}
   426  	mtime struct {
   427  		sec  uint64
   428  		nsec uint32
   429  	}
   430  	otime struct {
   431  		sec  uint64
   432  		nsec uint32
   433  	}
   434  }
   435  
   436  type btrfs_dir_log_item struct {
   437  	end uint64
   438  }
   439  
   440  type btrfs_dir_item struct {
   441  	location struct {
   442  		objectid uint64
   443  		type_    uint8
   444  		offset   uint64
   445  	}
   446  	transid  uint64
   447  	data_len uint16
   448  	name_len uint16
   449  	type_    uint8
   450  }
   451  
   452  /*
   453   * Internal in-memory flag that a subvolume has been marked for deletion but
   454   * still visible as a directory
   455   */
   456  type btrfs_root_item struct {
   457  	inode struct {
   458  		generation  uint64
   459  		transid     uint64
   460  		size        uint64
   461  		nbytes      uint64
   462  		block_group uint64
   463  		nlink       uint32
   464  		uid         uint32
   465  		gid         uint32
   466  		mode        uint32
   467  		rdev        uint64
   468  		flags       uint64
   469  		sequence    uint64
   470  		reserved    [4]uint64
   471  		atime       struct {
   472  			sec  uint64
   473  			nsec uint32
   474  		}
   475  		ctime struct {
   476  			sec  uint64
   477  			nsec uint32
   478  		}
   479  		mtime struct {
   480  			sec  uint64
   481  			nsec uint32
   482  		}
   483  		otime struct {
   484  			sec  uint64
   485  			nsec uint32
   486  		}
   487  	}
   488  	generation    uint64
   489  	root_dirid    uint64
   490  	bytenr        uint64
   491  	byte_limit    uint64
   492  	bytes_used    uint64
   493  	last_snapshot uint64
   494  	flags         uint64
   495  	refs          uint32
   496  	drop_progress struct {
   497  		objectid uint64
   498  		type_    uint8
   499  		offset   uint64
   500  	}
   501  	drop_level    uint8
   502  	level         uint8
   503  	generation_v2 uint64
   504  	uuid          UUID
   505  	parent_uuid   UUID
   506  	received_uuid UUID
   507  	ctransid      uint64
   508  	otransid      uint64
   509  	stransid      uint64
   510  	rtransid      uint64
   511  	ctime         struct {
   512  		sec  uint64
   513  		nsec uint32
   514  	}
   515  	otime struct {
   516  		sec  uint64
   517  		nsec uint32
   518  	}
   519  	stime struct {
   520  		sec  uint64
   521  		nsec uint32
   522  	}
   523  	rtime struct {
   524  		sec  uint64
   525  		nsec uint32
   526  	}
   527  	reserved [8]uint64
   528  }
   529  
   530  /*
   531   * this is used for both forward and backward root refs
   532   */
   533  type btrfs_root_ref struct {
   534  	dirid    uint64
   535  	sequence uint64
   536  	name_len uint16
   537  }
   538  
   539  type btrfs_disk_balance_args struct {
   540  	profiles    uint64
   541  	usage       uint64
   542  	usage_min   uint32
   543  	usage_max   uint32
   544  	devid       uint64
   545  	pstart      uint64
   546  	pend        uint64
   547  	vstart      uint64
   548  	vend        uint64
   549  	target      uint64
   550  	flags       uint64
   551  	limit       uint64
   552  	limit_min   uint32
   553  	limit_max   uint32
   554  	stripes_min uint32
   555  	stripes_max uint32
   556  	unused      [6]uint64
   557  }
   558  
   559  /*
   560   * store balance parameters to disk so that balance can be properly
   561   * resumed after crash or unmount
   562   */
   563  type btrfs_balance_item struct {
   564  	flags uint64
   565  	data  struct {
   566  		profiles    uint64
   567  		usage       uint64
   568  		usage_min   uint32
   569  		usage_max   uint32
   570  		devid       uint64
   571  		pstart      uint64
   572  		pend        uint64
   573  		vstart      uint64
   574  		vend        uint64
   575  		target      uint64
   576  		flags       uint64
   577  		limit       uint64
   578  		limit_min   uint32
   579  		limit_max   uint32
   580  		stripes_min uint32
   581  		stripes_max uint32
   582  		unused      [6]uint64
   583  	}
   584  	meta struct {
   585  		profiles    uint64
   586  		usage       uint64
   587  		usage_min   uint32
   588  		usage_max   uint32
   589  		devid       uint64
   590  		pstart      uint64
   591  		pend        uint64
   592  		vstart      uint64
   593  		vend        uint64
   594  		target      uint64
   595  		flags       uint64
   596  		limit       uint64
   597  		limit_min   uint32
   598  		limit_max   uint32
   599  		stripes_min uint32
   600  		stripes_max uint32
   601  		unused      [6]uint64
   602  	}
   603  	sys struct {
   604  		profiles    uint64
   605  		usage       uint64
   606  		usage_min   uint32
   607  		usage_max   uint32
   608  		devid       uint64
   609  		pstart      uint64
   610  		pend        uint64
   611  		vstart      uint64
   612  		vend        uint64
   613  		target      uint64
   614  		flags       uint64
   615  		limit       uint64
   616  		limit_min   uint32
   617  		limit_max   uint32
   618  		stripes_min uint32
   619  		stripes_max uint32
   620  		unused      [6]uint64
   621  	}
   622  	unused [4]uint64
   623  }
   624  
   625  type btrfs_file_extent_item struct {
   626  	generation     uint64
   627  	ram_bytes      uint64
   628  	compression    uint8
   629  	encryption     uint8
   630  	other_encoding uint16
   631  	type_          uint8
   632  	disk_bytenr    uint64
   633  	disk_num_bytes uint64
   634  	offset         uint64
   635  	num_bytes      uint64
   636  }
   637  
   638  type btrfs_csum_item struct {
   639  	csum uint8
   640  }
   641  
   642  type btrfs_dev_stats_item struct {
   643  	values [_BTRFS_DEV_STAT_VALUES_MAX]uint64
   644  }
   645  
   646  type btrfs_dev_replace_item struct {
   647  	src_devid                     uint64
   648  	cursor_left                   uint64
   649  	cursor_right                  uint64
   650  	cont_reading_from_srcdev_mode uint64
   651  	replace_state                 uint64
   652  	time_started                  uint64
   653  	time_stopped                  uint64
   654  	num_write_errors              uint64
   655  	num_uncorrectable_read_errors uint64
   656  }
   657  
   658  /* different types of block groups (and chunks) */
   659  const (
   660  	_BTRFS_RAID_RAID10 = iota
   661  	_BTRFS_RAID_RAID1
   662  	_BTRFS_RAID_DUP
   663  	_BTRFS_RAID_RAID0
   664  	_BTRFS_RAID_SINGLE
   665  	_BTRFS_RAID_RAID5
   666  	_BTRFS_RAID_RAID6
   667  	_BTRFS_NR_RAID_TYPES
   668  )
   669  
   670  /*
   671   * We need a bit for restriper to be able to tell when chunks of type
   672   * SINGLE are available.  This "extended" profile format is used in
   673   * fs_info->avail_*_alloc_bits (in-memory) and balance item fields
   674   * (on-disk).  The corresponding on-disk bit in chunk.type is reserved
   675   * to avoid remappings between two formats in future.
   676   */
   677  
   678  /*
   679   * A fake block group type that is used to communicate global block reserve
   680   * size to userspace via the SPACE_INFO ioctl.
   681   */
   682  func chunk_to_extended(flags uint64) uint64 {
   683  	if flags&uint64(_BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0 {
   684  		flags |= uint64(availAllocBitSingle)
   685  	}
   686  
   687  	return flags
   688  }
   689  
   690  func extended_to_chunk(flags uint64) uint64 {
   691  	return flags &^ uint64(availAllocBitSingle)
   692  }
   693  
   694  type btrfs_block_group_item struct {
   695  	used           uint64
   696  	chunk_objectid uint64
   697  	flags          uint64
   698  }
   699  
   700  type btrfs_free_space_info struct {
   701  	extent_count uint32
   702  	flags        uint32
   703  }
   704  
   705  func btrfs_qgroup_level(qgroupid uint64) uint64 {
   706  	return qgroupid >> uint32(qgroupLevelShift)
   707  }
   708  
   709  /*
   710   * is subvolume quota turned on?
   711   */
   712  
   713  /*
   714   * RESCAN is set during the initialization phase
   715   */
   716  
   717  /*
   718   * Some qgroup entries are known to be out of date,
   719   * either because the configuration has changed in a way that
   720   * makes a rescan necessary, or because the fs has been mounted
   721   * with a non-qgroup-aware version.
   722   * Turning qouta off and on again makes it inconsistent, too.
   723   */
   724  type btrfs_qgroup_status_item struct {
   725  	version    uint64
   726  	generation uint64
   727  	flags      uint64
   728  	rescan     uint64
   729  }
   730  
   731  type btrfs_qgroup_info_item struct {
   732  	generation uint64
   733  	rfer       uint64
   734  	rfer_cmpr  uint64
   735  	excl       uint64
   736  	excl_cmpr  uint64
   737  }
   738  
   739  type btrfs_qgroup_limit_item struct {
   740  	flags    uint64
   741  	max_rfer uint64
   742  	max_excl uint64
   743  	rsv_rfer uint64
   744  	rsv_excl uint64
   745  }