blob: c4b8298068556458f4b9775635d28187049eb484 [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Chris Mason234b63a2007-03-13 10:46:10 -040019#ifndef __BTRFS__
20#define __BTRFS__
Chris Masoneb60cea2007-02-02 09:18:22 -050021
Chris Masone20d96d2007-03-22 12:13:20 -040022#include <linux/fs.h>
Chris Mason08607c12007-06-08 15:33:54 -040023#include <linux/workqueue.h>
Josef Bacik58176a92007-08-29 15:47:34 -040024#include <linux/completion.h>
Chris Mason8ef97622007-03-26 10:15:30 -040025#include "bit-radix.h"
Chris Mason5f39d392007-10-15 16:14:19 -040026#include "extent_map.h"
Chris Masone20d96d2007-03-22 12:13:20 -040027
Chris Masone089f052007-03-16 16:20:31 -040028struct btrfs_trans_handle;
Chris Mason79154b12007-03-22 15:59:16 -040029struct btrfs_transaction;
Chris Mason35b7e472007-05-02 15:53:43 -040030extern struct kmem_cache *btrfs_trans_handle_cachep;
31extern struct kmem_cache *btrfs_transaction_cachep;
32extern struct kmem_cache *btrfs_bit_radix_cachep;
Chris Mason2c90e5d2007-04-02 10:50:19 -040033extern struct kmem_cache *btrfs_path_cachep;
Chris Masone089f052007-03-16 16:20:31 -040034
Chris Mason5f39d392007-10-15 16:14:19 -040035#define BTRFS_MAGIC "_B2RfS_M"
Chris Masoneb60cea2007-02-02 09:18:22 -050036
Chris Mason6407bf62007-03-27 06:33:00 -040037#define BTRFS_ROOT_TREE_OBJECTID 1ULL
Chris Mason0cf6c622007-06-09 09:22:25 -040038#define BTRFS_EXTENT_TREE_OBJECTID 2ULL
39#define BTRFS_FS_TREE_OBJECTID 3ULL
40#define BTRFS_ROOT_TREE_DIR_OBJECTID 4ULL
41#define BTRFS_FIRST_FREE_OBJECTID 5ULL
Chris Mason3768f362007-03-13 16:47:54 -040042
Chris Masonfec577f2007-02-26 10:40:21 -050043/*
Chris Masone20d96d2007-03-22 12:13:20 -040044 * we can actually store much bigger names, but lets not confuse the rest
45 * of linux
46 */
47#define BTRFS_NAME_LEN 255
48
Chris Masonf254e522007-03-29 15:15:27 -040049/* 32 bytes in various csum fields */
50#define BTRFS_CSUM_SIZE 32
Chris Mason509659c2007-05-10 12:36:17 -040051/* four bytes for CRC32 */
52#define BTRFS_CRC32_SIZE 4
Chris Masone06afa82007-05-23 15:44:28 -040053#define BTRFS_EMPTY_DIR_SIZE 6
Chris Masonf254e522007-03-29 15:15:27 -040054
Chris Masonfabb5682007-06-07 22:13:21 -040055#define BTRFS_FT_UNKNOWN 0
56#define BTRFS_FT_REG_FILE 1
57#define BTRFS_FT_DIR 2
58#define BTRFS_FT_CHRDEV 3
59#define BTRFS_FT_BLKDEV 4
60#define BTRFS_FT_FIFO 5
61#define BTRFS_FT_SOCK 6
62#define BTRFS_FT_SYMLINK 7
63#define BTRFS_FT_MAX 8
64
Chris Masone20d96d2007-03-22 12:13:20 -040065/*
Chris Masonfec577f2007-02-26 10:40:21 -050066 * the key defines the order in the tree, and so it also defines (optimal)
67 * block layout. objectid corresonds to the inode number. The flags
68 * tells us things about the object, and is a kind of stream selector.
69 * so for a given inode, keys with flags of 1 might refer to the inode
70 * data, flags of 2 may point to file data in the btree and flags == 3
71 * may point to extents.
72 *
73 * offset is the starting byte offset for this key in the stream.
Chris Masone2fa7222007-03-12 16:22:34 -040074 *
75 * btrfs_disk_key is in disk byte order. struct btrfs_key is always
76 * in cpu native order. Otherwise they are identical and their sizes
77 * should be the same (ie both packed)
Chris Masonfec577f2007-02-26 10:40:21 -050078 */
Chris Masone2fa7222007-03-12 16:22:34 -040079struct btrfs_disk_key {
80 __le64 objectid;
Chris Mason5f39d392007-10-15 16:14:19 -040081 u8 type;
Chris Mason70b2bef2007-04-17 15:39:32 -040082 __le64 offset;
Chris Masone2fa7222007-03-12 16:22:34 -040083} __attribute__ ((__packed__));
84
85struct btrfs_key {
Chris Masoneb60cea2007-02-02 09:18:22 -050086 u64 objectid;
Chris Mason5f39d392007-10-15 16:14:19 -040087 u8 type;
Chris Mason70b2bef2007-04-17 15:39:32 -040088 u64 offset;
Chris Masoneb60cea2007-02-02 09:18:22 -050089} __attribute__ ((__packed__));
90
Chris Mason5f39d392007-10-15 16:14:19 -040091#define BTRFS_FSID_SIZE 16
Chris Masonfec577f2007-02-26 10:40:21 -050092/*
93 * every tree block (leaf or node) starts with this header.
94 */
Chris Masonbb492bb2007-03-12 12:29:44 -040095struct btrfs_header {
Chris Masonf254e522007-03-29 15:15:27 -040096 u8 csum[BTRFS_CSUM_SIZE];
Chris Mason5f39d392007-10-15 16:14:19 -040097 u8 fsid[BTRFS_FSID_SIZE]; /* FS specific uuid */
Chris Masonbb492bb2007-03-12 12:29:44 -040098 __le64 blocknr; /* which block this node is supposed to live in */
Chris Mason7f5c1512007-03-23 15:56:19 -040099 __le64 generation;
Chris Mason4d775672007-04-20 20:23:12 -0400100 __le64 owner;
Chris Mason5f39d392007-10-15 16:14:19 -0400101 __le32 nritems;
Chris Masonbb492bb2007-03-12 12:29:44 -0400102 __le16 flags;
Chris Mason9a6f11e2007-03-27 09:06:38 -0400103 u8 level;
Chris Masoneb60cea2007-02-02 09:18:22 -0500104} __attribute__ ((__packed__));
105
Chris Mason234b63a2007-03-13 10:46:10 -0400106#define BTRFS_MAX_LEVEL 8
Chris Mason5f39d392007-10-15 16:14:19 -0400107#define BTRFS_NODEPTRS_PER_BLOCK(r) (((r)->nodesize - \
Chris Mason123abc82007-03-14 14:14:43 -0400108 sizeof(struct btrfs_header)) / \
109 (sizeof(struct btrfs_disk_key) + sizeof(u64)))
110#define __BTRFS_LEAF_DATA_SIZE(bs) ((bs) - sizeof(struct btrfs_header))
Chris Mason5f39d392007-10-15 16:14:19 -0400111#define BTRFS_LEAF_DATA_SIZE(r) (__BTRFS_LEAF_DATA_SIZE(r->leafsize))
Chris Mason236454d2007-04-19 13:37:44 -0400112#define BTRFS_MAX_INLINE_DATA_SIZE(r) (BTRFS_LEAF_DATA_SIZE(r) - \
113 sizeof(struct btrfs_item) - \
114 sizeof(struct btrfs_file_extent_item))
Chris Masoneb60cea2007-02-02 09:18:22 -0500115
Chris Masonfec577f2007-02-26 10:40:21 -0500116/*
Chris Masonfec577f2007-02-26 10:40:21 -0500117 * the super block basically lists the main trees of the FS
118 * it currently lacks any block count etc etc
119 */
Chris Mason234b63a2007-03-13 10:46:10 -0400120struct btrfs_super_block {
Chris Masonf254e522007-03-29 15:15:27 -0400121 u8 csum[BTRFS_CSUM_SIZE];
Chris Mason87cbda52007-03-28 19:44:27 -0400122 /* the first 3 fields must match struct btrfs_header */
Chris Mason3768f362007-03-13 16:47:54 -0400123 u8 fsid[16]; /* FS specific uuid */
124 __le64 blocknr; /* this block number */
Chris Mason3768f362007-03-13 16:47:54 -0400125 __le64 magic;
Chris Mason3768f362007-03-13 16:47:54 -0400126 __le64 generation;
127 __le64 root;
128 __le64 total_blocks;
129 __le64 blocks_used;
Chris Mason2e635a22007-03-21 11:12:56 -0400130 __le64 root_dir_objectid;
Chris Mason5f39d392007-10-15 16:14:19 -0400131 __le32 sectorsize;
132 __le32 nodesize;
133 __le32 leafsize;
Chris Masoncfaa7292007-02-21 17:04:57 -0500134} __attribute__ ((__packed__));
135
Chris Masonfec577f2007-02-26 10:40:21 -0500136/*
Chris Mason62e27492007-03-15 12:56:47 -0400137 * A leaf is full of items. offset and size tell us where to find
Chris Masonfec577f2007-02-26 10:40:21 -0500138 * the item in the leaf (relative to the start of the data area)
139 */
Chris Mason0783fcf2007-03-12 20:12:07 -0400140struct btrfs_item {
Chris Masone2fa7222007-03-12 16:22:34 -0400141 struct btrfs_disk_key key;
Chris Mason123abc82007-03-14 14:14:43 -0400142 __le32 offset;
Chris Mason5f39d392007-10-15 16:14:19 -0400143 __le32 size;
Chris Masoneb60cea2007-02-02 09:18:22 -0500144} __attribute__ ((__packed__));
145
Chris Masonfec577f2007-02-26 10:40:21 -0500146/*
147 * leaves have an item area and a data area:
148 * [item0, item1....itemN] [free space] [dataN...data1, data0]
149 *
150 * The data is separate from the items to get the keys closer together
151 * during searches.
152 */
Chris Mason234b63a2007-03-13 10:46:10 -0400153struct btrfs_leaf {
Chris Masonbb492bb2007-03-12 12:29:44 -0400154 struct btrfs_header header;
Chris Mason123abc82007-03-14 14:14:43 -0400155 struct btrfs_item items[];
Chris Masoneb60cea2007-02-02 09:18:22 -0500156} __attribute__ ((__packed__));
157
Chris Masonfec577f2007-02-26 10:40:21 -0500158/*
159 * all non-leaf blocks are nodes, they hold only keys and pointers to
160 * other blocks
161 */
Chris Mason123abc82007-03-14 14:14:43 -0400162struct btrfs_key_ptr {
163 struct btrfs_disk_key key;
164 __le64 blockptr;
165} __attribute__ ((__packed__));
166
Chris Mason234b63a2007-03-13 10:46:10 -0400167struct btrfs_node {
Chris Masonbb492bb2007-03-12 12:29:44 -0400168 struct btrfs_header header;
Chris Mason123abc82007-03-14 14:14:43 -0400169 struct btrfs_key_ptr ptrs[];
Chris Masoneb60cea2007-02-02 09:18:22 -0500170} __attribute__ ((__packed__));
171
Chris Masonfec577f2007-02-26 10:40:21 -0500172/*
Chris Mason234b63a2007-03-13 10:46:10 -0400173 * btrfs_paths remember the path taken from the root down to the leaf.
174 * level 0 is always the leaf, and nodes[1...BTRFS_MAX_LEVEL] will point
Chris Masonfec577f2007-02-26 10:40:21 -0500175 * to any other levels that are present.
176 *
177 * The slots array records the index of the item or block pointer
178 * used while walking the tree.
179 */
Chris Mason234b63a2007-03-13 10:46:10 -0400180struct btrfs_path {
Chris Mason5f39d392007-10-15 16:14:19 -0400181 struct extent_buffer *nodes[BTRFS_MAX_LEVEL];
Chris Mason234b63a2007-03-13 10:46:10 -0400182 int slots[BTRFS_MAX_LEVEL];
Chris Mason3c69fae2007-08-07 15:52:22 -0400183 int reada;
Chris Mason6702ed42007-08-07 16:15:09 -0400184 int lowest_level;
Chris Masoneb60cea2007-02-02 09:18:22 -0500185};
Chris Mason5de08d72007-02-24 06:24:44 -0500186
Chris Mason62e27492007-03-15 12:56:47 -0400187/*
188 * items in the extent btree are used to record the objectid of the
189 * owner of the block and the number of references
190 */
191struct btrfs_extent_item {
192 __le32 refs;
Chris Mason4d775672007-04-20 20:23:12 -0400193 __le64 owner;
Chris Mason62e27492007-03-15 12:56:47 -0400194} __attribute__ ((__packed__));
195
Chris Mason1e1d2702007-03-15 19:03:33 -0400196struct btrfs_inode_timespec {
Chris Masonf254e522007-03-29 15:15:27 -0400197 __le64 sec;
Chris Mason1e1d2702007-03-15 19:03:33 -0400198 __le32 nsec;
199} __attribute__ ((__packed__));
200
201/*
202 * there is no padding here on purpose. If you want to extent the inode,
203 * make a new item type
204 */
205struct btrfs_inode_item {
206 __le64 generation;
207 __le64 size;
208 __le64 nblocks;
Chris Mason31f3c992007-04-30 15:25:45 -0400209 __le64 block_group;
Chris Mason1e1d2702007-03-15 19:03:33 -0400210 __le32 nlink;
211 __le32 uid;
212 __le32 gid;
213 __le32 mode;
214 __le32 rdev;
215 __le16 flags;
216 __le16 compat_flags;
217 struct btrfs_inode_timespec atime;
218 struct btrfs_inode_timespec ctime;
219 struct btrfs_inode_timespec mtime;
220 struct btrfs_inode_timespec otime;
221} __attribute__ ((__packed__));
222
Chris Mason62e27492007-03-15 12:56:47 -0400223struct btrfs_dir_item {
Chris Masond6e4a422007-04-06 15:37:36 -0400224 struct btrfs_disk_key location;
Chris Mason62e27492007-03-15 12:56:47 -0400225 __le16 flags;
Chris Masona8a2ee02007-03-16 08:46:49 -0400226 __le16 name_len;
Chris Mason62e27492007-03-15 12:56:47 -0400227 u8 type;
228} __attribute__ ((__packed__));
229
230struct btrfs_root_item {
Chris Masond6e4a422007-04-06 15:37:36 -0400231 struct btrfs_inode_item inode;
232 __le64 root_dirid;
Chris Mason62e27492007-03-15 12:56:47 -0400233 __le64 blocknr;
Chris Mason62e27492007-03-15 12:56:47 -0400234 __le64 block_limit;
235 __le64 blocks_used;
Chris Mason5eda7b52007-06-22 14:16:25 -0400236 __le32 flags;
Chris Mason62e27492007-03-15 12:56:47 -0400237 __le32 refs;
Chris Mason5eda7b52007-06-22 14:16:25 -0400238 struct btrfs_disk_key drop_progress;
239 u8 drop_level;
Chris Mason9f5fae22007-03-20 14:38:32 -0400240} __attribute__ ((__packed__));
241
Chris Mason236454d2007-04-19 13:37:44 -0400242#define BTRFS_FILE_EXTENT_REG 0
243#define BTRFS_FILE_EXTENT_INLINE 1
244
Chris Mason9f5fae22007-03-20 14:38:32 -0400245struct btrfs_file_extent_item {
Chris Mason71951f32007-03-27 09:16:29 -0400246 __le64 generation;
Chris Mason236454d2007-04-19 13:37:44 -0400247 u8 type;
Chris Mason9f5fae22007-03-20 14:38:32 -0400248 /*
249 * disk space consumed by the extent, checksum blocks are included
250 * in these numbers
251 */
252 __le64 disk_blocknr;
253 __le64 disk_num_blocks;
254 /*
Chris Masondee26a92007-03-26 16:00:06 -0400255 * the logical offset in file blocks (no csums)
Chris Mason9f5fae22007-03-20 14:38:32 -0400256 * this extent record is for. This allows a file extent to point
257 * into the middle of an existing extent on disk, sharing it
258 * between two snapshots (useful if some bytes in the middle of the
259 * extent have changed
260 */
261 __le64 offset;
262 /*
263 * the logical number of file blocks (no csums included)
264 */
265 __le64 num_blocks;
266} __attribute__ ((__packed__));
267
Chris Masonf254e522007-03-29 15:15:27 -0400268struct btrfs_csum_item {
Chris Mason509659c2007-05-10 12:36:17 -0400269 u8 csum;
Chris Masonf254e522007-03-29 15:15:27 -0400270} __attribute__ ((__packed__));
271
Chris Mason9078a3e2007-04-26 16:46:15 -0400272/* tag for the radix tree of block groups in ram */
273#define BTRFS_BLOCK_GROUP_DIRTY 0
Chris Mason31f3c992007-04-30 15:25:45 -0400274#define BTRFS_BLOCK_GROUP_AVAIL 1
Chris Mason9078a3e2007-04-26 16:46:15 -0400275#define BTRFS_BLOCK_GROUP_SIZE (256 * 1024 * 1024)
Chris Mason1e2677e2007-05-29 16:52:18 -0400276
277
278#define BTRFS_BLOCK_GROUP_DATA 1
Chris Mason9078a3e2007-04-26 16:46:15 -0400279struct btrfs_block_group_item {
280 __le64 used;
Chris Mason1e2677e2007-05-29 16:52:18 -0400281 u8 flags;
Chris Mason9078a3e2007-04-26 16:46:15 -0400282} __attribute__ ((__packed__));
283
284struct btrfs_block_group_cache {
285 struct btrfs_key key;
286 struct btrfs_block_group_item item;
Chris Mason3e1ad542007-05-07 20:03:49 -0400287 struct radix_tree_root *radix;
Chris Masoncd1bc462007-04-27 10:08:34 -0400288 u64 first_free;
289 u64 last_alloc;
Chris Masonbe744172007-05-06 10:15:01 -0400290 u64 pinned;
Chris Masone37c9e62007-05-09 20:13:14 -0400291 u64 last_prealloc;
Chris Masonbe744172007-05-06 10:15:01 -0400292 int data;
Chris Masone37c9e62007-05-09 20:13:14 -0400293 int cached;
Chris Mason9078a3e2007-04-26 16:46:15 -0400294};
295
Chris Mason9f5fae22007-03-20 14:38:32 -0400296struct btrfs_fs_info {
Chris Mason5f39d392007-10-15 16:14:19 -0400297 u8 fsid[BTRFS_FSID_SIZE];
Chris Mason9f5fae22007-03-20 14:38:32 -0400298 struct btrfs_root *extent_root;
299 struct btrfs_root *tree_root;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400300 struct radix_tree_root fs_roots_radix;
Chris Mason8ef97622007-03-26 10:15:30 -0400301 struct radix_tree_root pending_del_radix;
Chris Mason9f5fae22007-03-20 14:38:32 -0400302 struct radix_tree_root pinned_radix;
Chris Mason9078a3e2007-04-26 16:46:15 -0400303 struct radix_tree_root block_group_radix;
Chris Masonbe744172007-05-06 10:15:01 -0400304 struct radix_tree_root block_group_data_radix;
Chris Masone37c9e62007-05-09 20:13:14 -0400305 struct radix_tree_root extent_map_radix;
Chris Mason26b80032007-08-08 20:17:12 -0400306 struct radix_tree_root extent_ins_radix;
Chris Mason293ffd52007-03-20 15:57:25 -0400307 u64 generation;
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400308 u64 last_trans_committed;
Chris Mason79154b12007-03-22 15:59:16 -0400309 struct btrfs_transaction *running_transaction;
Chris Mason4b52dff2007-06-26 10:06:50 -0400310 struct btrfs_super_block super_copy;
Chris Mason5f39d392007-10-15 16:14:19 -0400311 struct extent_buffer *sb_buffer;
Chris Masone20d96d2007-03-22 12:13:20 -0400312 struct super_block *sb;
Chris Masond98237b2007-03-28 13:57:48 -0400313 struct inode *btree_inode;
Chris Mason79154b12007-03-22 15:59:16 -0400314 struct mutex trans_mutex;
Chris Masond561c022007-03-23 19:47:49 -0400315 struct mutex fs_mutex;
Chris Mason8fd17792007-04-19 21:01:03 -0400316 struct list_head trans_list;
Chris Masonfacda1e2007-06-08 18:11:48 -0400317 struct list_head dead_roots;
Chris Mason08607c12007-06-08 15:33:54 -0400318 struct delayed_work trans_work;
Josef Bacik58176a92007-08-29 15:47:34 -0400319 struct kobject super_kobj;
320 struct completion kobj_unregister;
Chris Masone66f7092007-04-20 13:16:02 -0400321 int do_barriers;
Chris Masonfacda1e2007-06-08 18:11:48 -0400322 int closing;
Chris Mason62e27492007-03-15 12:56:47 -0400323};
324
325/*
326 * in ram representation of the tree. extent_root is used for all allocations
Chris Masonf2458e12007-04-25 15:52:25 -0400327 * and for the extent tree extent_root root.
Chris Mason62e27492007-03-15 12:56:47 -0400328 */
329struct btrfs_root {
Chris Mason5f39d392007-10-15 16:14:19 -0400330 struct extent_buffer *node;
331 struct extent_buffer *commit_root;
Chris Mason62e27492007-03-15 12:56:47 -0400332 struct btrfs_root_item root_item;
333 struct btrfs_key root_key;
Chris Mason9f5fae22007-03-20 14:38:32 -0400334 struct btrfs_fs_info *fs_info;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400335 struct inode *inode;
Josef Bacik58176a92007-08-29 15:47:34 -0400336 struct kobject root_kobj;
337 struct completion kobj_unregister;
Chris Mason011410b2007-09-10 19:58:36 -0400338 struct rw_semaphore snap_sem;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400339 u64 objectid;
340 u64 last_trans;
Chris Mason5f39d392007-10-15 16:14:19 -0400341
342 /* data allocations are done in sectorsize units */
343 u32 sectorsize;
344
345 /* node allocations are done in nodesize units */
346 u32 nodesize;
347
348 /* leaf allocations are done in leafsize units */
349 u32 leafsize;
350
Chris Mason9f5fae22007-03-20 14:38:32 -0400351 u32 type;
Chris Mason1b05da22007-04-10 12:13:09 -0400352 u64 highest_inode;
353 u64 last_inode_alloc;
Chris Mason9f3a7422007-08-07 15:52:19 -0400354 int ref_cows;
Chris Mason6702ed42007-08-07 16:15:09 -0400355 struct btrfs_key defrag_progress;
356 int defrag_running;
357 int defrag_level;
Josef Bacik58176a92007-08-29 15:47:34 -0400358 char *name;
Chris Mason62e27492007-03-15 12:56:47 -0400359};
360
Chris Mason1e1d2702007-03-15 19:03:33 -0400361/*
362 * inode items have the data typically returned from stat and store other
363 * info about object characteristics. There is one for every file and dir in
364 * the FS
365 */
Chris Mason9078a3e2007-04-26 16:46:15 -0400366#define BTRFS_INODE_ITEM_KEY 1
367
368/* reserve 2-15 close to the inode for later flexibility */
Chris Mason1e1d2702007-03-15 19:03:33 -0400369
370/*
371 * dir items are the name -> inode pointers in a directory. There is one
372 * for every name in a directory.
373 */
Chris Mason9078a3e2007-04-26 16:46:15 -0400374#define BTRFS_DIR_ITEM_KEY 16
375#define BTRFS_DIR_INDEX_KEY 17
Chris Mason1e1d2702007-03-15 19:03:33 -0400376/*
Chris Mason9078a3e2007-04-26 16:46:15 -0400377 * extent data is for file data
Chris Mason1e1d2702007-03-15 19:03:33 -0400378 */
Chris Mason9078a3e2007-04-26 16:46:15 -0400379#define BTRFS_EXTENT_DATA_KEY 18
Chris Mason1e1d2702007-03-15 19:03:33 -0400380/*
Chris Masonf254e522007-03-29 15:15:27 -0400381 * csum items have the checksums for data in the extents
382 */
Chris Mason9078a3e2007-04-26 16:46:15 -0400383#define BTRFS_CSUM_ITEM_KEY 19
384
385/* reserve 20-31 for other file stuff */
Chris Masonf254e522007-03-29 15:15:27 -0400386
387/*
Chris Mason1e1d2702007-03-15 19:03:33 -0400388 * root items point to tree roots. There are typically in the root
389 * tree used by the super block to find all the other trees
390 */
Chris Mason9078a3e2007-04-26 16:46:15 -0400391#define BTRFS_ROOT_ITEM_KEY 32
Chris Mason1e1d2702007-03-15 19:03:33 -0400392/*
393 * extent items are in the extent map tree. These record which blocks
394 * are used, and how many references there are to each block
395 */
Chris Mason9078a3e2007-04-26 16:46:15 -0400396#define BTRFS_EXTENT_ITEM_KEY 33
397
398/*
399 * block groups give us hints into the extent allocation trees. Which
400 * blocks are free etc etc
401 */
402#define BTRFS_BLOCK_GROUP_ITEM_KEY 34
Chris Mason9f5fae22007-03-20 14:38:32 -0400403
404/*
Chris Mason1e1d2702007-03-15 19:03:33 -0400405 * string items are for debugging. They just store a short string of
406 * data in the FS
407 */
Chris Mason9078a3e2007-04-26 16:46:15 -0400408#define BTRFS_STRING_ITEM_KEY 253
409
Chris Mason5f39d392007-10-15 16:14:19 -0400410/* some macros to generate set/get funcs for the struct fields. This
411 * assumes there is a lefoo_to_cpu for every type, so lets make a simple
412 * one for u8:
413 */
414#define le8_to_cpu(v) (v)
415#define cpu_to_le8(v) (v)
416#define __le8 u8
Chris Mason9078a3e2007-04-26 16:46:15 -0400417
Chris Mason5f39d392007-10-15 16:14:19 -0400418#define read_eb_member(eb, ptr, type, member, result) ( \
419 read_extent_buffer(eb, (char *)(result), \
420 ((unsigned long)(ptr)) + \
421 offsetof(type, member), \
422 sizeof(((type *)0)->member)))
423
424#define write_eb_member(eb, ptr, type, member, result) ( \
425 write_extent_buffer(eb, (char *)(result), \
426 ((unsigned long)(ptr)) + \
427 offsetof(type, member), \
428 sizeof(((type *)0)->member)))
429
430#define BTRFS_SETGET_FUNCS(name, type, member, bits) \
431static inline u##bits btrfs_##name(struct extent_buffer *eb, \
432 type *s) \
433{ \
434 __le##bits res; \
435 read_eb_member(eb, s, type, member, &res); \
436 return le##bits##_to_cpu(res); \
437} \
438static inline void btrfs_set_##name(struct extent_buffer *eb, \
439 type *s, u##bits val) \
440{ \
441 val = cpu_to_le##bits(val); \
442 write_eb_member(eb, s, type, member, &val); \
Chris Mason9078a3e2007-04-26 16:46:15 -0400443}
444
Chris Mason5f39d392007-10-15 16:14:19 -0400445#define BTRFS_SETGET_HEADER_FUNCS(name, type, member, bits) \
446static inline u##bits btrfs_##name(struct extent_buffer *eb) \
447{ \
448 __le##bits res; \
449 read_eb_member(eb, NULL, type, member, &res); \
450 return le##bits##_to_cpu(res); \
451} \
452static inline void btrfs_set_##name(struct extent_buffer *eb, \
453 u##bits val) \
454{ \
455 val = cpu_to_le##bits(val); \
456 write_eb_member(eb, NULL, type, member, &val); \
Chris Mason9078a3e2007-04-26 16:46:15 -0400457}
Chris Mason1e1d2702007-03-15 19:03:33 -0400458
Chris Mason5f39d392007-10-15 16:14:19 -0400459#define BTRFS_SETGET_STACK_FUNCS(name, type, member, bits) \
460static inline u##bits btrfs_##name(type *s) \
461{ \
462 return le##bits##_to_cpu(s->member); \
463} \
464static inline void btrfs_set_##name(type *s, u##bits val) \
465{ \
466 s->member = cpu_to_le##bits(val); \
Chris Mason1e1d2702007-03-15 19:03:33 -0400467}
468
Chris Mason5f39d392007-10-15 16:14:19 -0400469/* struct btrfs_block_group_item */
470BTRFS_SETGET_STACK_FUNCS(block_group_used, struct btrfs_block_group_item,
471 used, 64);
472BTRFS_SETGET_FUNCS(disk_block_group_used, struct btrfs_block_group_item,
473 used, 64);
474
475/* struct btrfs_inode_item */
476BTRFS_SETGET_FUNCS(inode_generation, struct btrfs_inode_item, generation, 64);
477BTRFS_SETGET_FUNCS(inode_size, struct btrfs_inode_item, size, 64);
478BTRFS_SETGET_FUNCS(inode_nblocks, struct btrfs_inode_item, nblocks, 64);
479BTRFS_SETGET_FUNCS(inode_block_group, struct btrfs_inode_item, block_group, 64);
480BTRFS_SETGET_FUNCS(inode_nlink, struct btrfs_inode_item, nlink, 32);
481BTRFS_SETGET_FUNCS(inode_uid, struct btrfs_inode_item, uid, 32);
482BTRFS_SETGET_FUNCS(inode_gid, struct btrfs_inode_item, gid, 32);
483BTRFS_SETGET_FUNCS(inode_mode, struct btrfs_inode_item, mode, 32);
484BTRFS_SETGET_FUNCS(inode_rdev, struct btrfs_inode_item, rdev, 32);
485BTRFS_SETGET_FUNCS(inode_flags, struct btrfs_inode_item, flags, 16);
486BTRFS_SETGET_FUNCS(inode_compat_flags, struct btrfs_inode_item,
487 compat_flags, 16);
488
489static inline struct btrfs_inode_timespec *
490btrfs_inode_atime(struct btrfs_inode_item *inode_item)
Chris Mason1e1d2702007-03-15 19:03:33 -0400491{
Chris Mason5f39d392007-10-15 16:14:19 -0400492 unsigned long ptr = (unsigned long)inode_item;
493 ptr += offsetof(struct btrfs_inode_item, atime);
494 return (struct btrfs_inode_timespec *)ptr;
Chris Mason1e1d2702007-03-15 19:03:33 -0400495}
496
Chris Mason5f39d392007-10-15 16:14:19 -0400497static inline struct btrfs_inode_timespec *
498btrfs_inode_mtime(struct btrfs_inode_item *inode_item)
Chris Mason1e1d2702007-03-15 19:03:33 -0400499{
Chris Mason5f39d392007-10-15 16:14:19 -0400500 unsigned long ptr = (unsigned long)inode_item;
501 ptr += offsetof(struct btrfs_inode_item, mtime);
502 return (struct btrfs_inode_timespec *)ptr;
Chris Mason1e1d2702007-03-15 19:03:33 -0400503}
504
Chris Mason5f39d392007-10-15 16:14:19 -0400505static inline struct btrfs_inode_timespec *
506btrfs_inode_ctime(struct btrfs_inode_item *inode_item)
Chris Mason1e1d2702007-03-15 19:03:33 -0400507{
Chris Mason5f39d392007-10-15 16:14:19 -0400508 unsigned long ptr = (unsigned long)inode_item;
509 ptr += offsetof(struct btrfs_inode_item, ctime);
510 return (struct btrfs_inode_timespec *)ptr;
Chris Mason1e1d2702007-03-15 19:03:33 -0400511}
512
Chris Mason5f39d392007-10-15 16:14:19 -0400513static inline struct btrfs_inode_timespec *
514btrfs_inode_otime(struct btrfs_inode_item *inode_item)
Chris Mason1e1d2702007-03-15 19:03:33 -0400515{
Chris Mason5f39d392007-10-15 16:14:19 -0400516 unsigned long ptr = (unsigned long)inode_item;
517 ptr += offsetof(struct btrfs_inode_item, otime);
518 return (struct btrfs_inode_timespec *)ptr;
Chris Mason1e1d2702007-03-15 19:03:33 -0400519}
520
Chris Mason5f39d392007-10-15 16:14:19 -0400521BTRFS_SETGET_FUNCS(timespec_sec, struct btrfs_inode_timespec, sec, 64);
522BTRFS_SETGET_FUNCS(timespec_nsec, struct btrfs_inode_timespec, nsec, 32);
523
524/* struct btrfs_extent_item */
525BTRFS_SETGET_FUNCS(extent_refs, struct btrfs_extent_item, refs, 32);
526BTRFS_SETGET_FUNCS(extent_owner, struct btrfs_extent_item, owner, 32);
527
528BTRFS_SETGET_STACK_FUNCS(stack_extent_refs, struct btrfs_extent_item,
529 refs, 32);
530BTRFS_SETGET_STACK_FUNCS(stack_extent_owner, struct btrfs_extent_item,
531 owner, 32);
532
533/* struct btrfs_node */
534BTRFS_SETGET_FUNCS(key_blockptr, struct btrfs_key_ptr, blockptr, 64);
535
536static inline u64 btrfs_node_blockptr(struct extent_buffer *eb, int nr)
Chris Mason1e1d2702007-03-15 19:03:33 -0400537{
Chris Mason5f39d392007-10-15 16:14:19 -0400538 unsigned long ptr;
539 ptr = offsetof(struct btrfs_node, ptrs) +
540 sizeof(struct btrfs_key_ptr) * nr;
541 return btrfs_key_blockptr(eb, (struct btrfs_key_ptr *)ptr);
Chris Mason1e1d2702007-03-15 19:03:33 -0400542}
543
Chris Mason5f39d392007-10-15 16:14:19 -0400544static inline void btrfs_set_node_blockptr(struct extent_buffer *eb,
545 int nr, u64 val)
Chris Mason31f3c992007-04-30 15:25:45 -0400546{
Chris Mason5f39d392007-10-15 16:14:19 -0400547 unsigned long ptr;
548 ptr = offsetof(struct btrfs_node, ptrs) +
549 sizeof(struct btrfs_key_ptr) * nr;
550 btrfs_set_key_blockptr(eb, (struct btrfs_key_ptr *)ptr, val);
Chris Mason31f3c992007-04-30 15:25:45 -0400551}
552
Chris Mason5f39d392007-10-15 16:14:19 -0400553static unsigned long btrfs_node_key_ptr_offset(int nr)
Chris Mason31f3c992007-04-30 15:25:45 -0400554{
Chris Mason5f39d392007-10-15 16:14:19 -0400555 return offsetof(struct btrfs_node, ptrs) +
556 sizeof(struct btrfs_key_ptr) * nr;
Chris Mason31f3c992007-04-30 15:25:45 -0400557}
558
Chris Mason5f39d392007-10-15 16:14:19 -0400559static void btrfs_node_key(struct extent_buffer *eb,
560 struct btrfs_disk_key *disk_key, int nr)
Chris Mason1e1d2702007-03-15 19:03:33 -0400561{
Chris Mason5f39d392007-10-15 16:14:19 -0400562 unsigned long ptr;
563 ptr = btrfs_node_key_ptr_offset(nr);
564 read_eb_member(eb, (struct btrfs_key_ptr *)ptr,
565 struct btrfs_key_ptr, key, disk_key);
566}
567static inline void btrfs_set_node_key(struct extent_buffer *eb,
568 struct btrfs_disk_key *disk_key, int nr)
569{
570 unsigned long ptr;
571 ptr = btrfs_node_key_ptr_offset(nr);
572 write_eb_member(eb, (struct btrfs_key_ptr *)ptr,
573 struct btrfs_key_ptr, key, disk_key);
Chris Mason1e1d2702007-03-15 19:03:33 -0400574}
575
Chris Mason5f39d392007-10-15 16:14:19 -0400576/* struct btrfs_item */
577BTRFS_SETGET_FUNCS(item_offset, struct btrfs_item, offset, 32);
578BTRFS_SETGET_FUNCS(item_size, struct btrfs_item, size, 32);
579
580static inline unsigned long btrfs_item_nr_offset(int nr)
Chris Mason1e1d2702007-03-15 19:03:33 -0400581{
Chris Mason5f39d392007-10-15 16:14:19 -0400582 return offsetof(struct btrfs_leaf, items) +
583 sizeof(struct btrfs_item) * nr;
Chris Mason1e1d2702007-03-15 19:03:33 -0400584}
585
Chris Mason5f39d392007-10-15 16:14:19 -0400586static inline struct btrfs_item *btrfs_item_nr(struct extent_buffer *eb,
587 int nr)
Chris Mason1e1d2702007-03-15 19:03:33 -0400588{
Chris Mason5f39d392007-10-15 16:14:19 -0400589 return (struct btrfs_item *)btrfs_item_nr_offset(nr);
Chris Mason1e1d2702007-03-15 19:03:33 -0400590}
591
Chris Mason5f39d392007-10-15 16:14:19 -0400592static inline u32 btrfs_item_end(struct extent_buffer *eb,
593 struct btrfs_item *item)
Chris Mason1e1d2702007-03-15 19:03:33 -0400594{
Chris Mason5f39d392007-10-15 16:14:19 -0400595 return btrfs_item_offset(eb, item) + btrfs_item_size(eb, item);
Chris Mason1e1d2702007-03-15 19:03:33 -0400596}
597
Chris Mason5f39d392007-10-15 16:14:19 -0400598static inline u32 btrfs_item_end_nr(struct extent_buffer *eb, int nr)
Chris Mason1e1d2702007-03-15 19:03:33 -0400599{
Chris Mason5f39d392007-10-15 16:14:19 -0400600 return btrfs_item_end(eb, btrfs_item_nr(eb, nr));
Chris Mason1e1d2702007-03-15 19:03:33 -0400601}
602
Chris Mason5f39d392007-10-15 16:14:19 -0400603static inline u32 btrfs_item_offset_nr(struct extent_buffer *eb, int nr)
Chris Mason1e1d2702007-03-15 19:03:33 -0400604{
Chris Mason5f39d392007-10-15 16:14:19 -0400605 return btrfs_item_offset(eb, btrfs_item_nr(eb, nr));
Chris Mason1e1d2702007-03-15 19:03:33 -0400606}
607
Chris Mason5f39d392007-10-15 16:14:19 -0400608static inline u32 btrfs_item_size_nr(struct extent_buffer *eb, int nr)
Chris Mason1e1d2702007-03-15 19:03:33 -0400609{
Chris Mason5f39d392007-10-15 16:14:19 -0400610 return btrfs_item_size(eb, btrfs_item_nr(eb, nr));
Chris Mason1e1d2702007-03-15 19:03:33 -0400611}
612
Chris Mason5f39d392007-10-15 16:14:19 -0400613static inline void btrfs_item_key(struct extent_buffer *eb,
614 struct btrfs_disk_key *disk_key, int nr)
Chris Mason1e1d2702007-03-15 19:03:33 -0400615{
Chris Mason5f39d392007-10-15 16:14:19 -0400616 struct btrfs_item *item = btrfs_item_nr(eb, nr);
617 read_eb_member(eb, item, struct btrfs_item, key, disk_key);
Chris Mason1e1d2702007-03-15 19:03:33 -0400618}
619
Chris Mason5f39d392007-10-15 16:14:19 -0400620static inline void btrfs_set_item_key(struct extent_buffer *eb,
621 struct btrfs_disk_key *disk_key, int nr)
Chris Mason1e1d2702007-03-15 19:03:33 -0400622{
Chris Mason5f39d392007-10-15 16:14:19 -0400623 struct btrfs_item *item = btrfs_item_nr(eb, nr);
624 write_eb_member(eb, item, struct btrfs_item, key, disk_key);
Chris Mason1e1d2702007-03-15 19:03:33 -0400625}
626
Chris Mason5f39d392007-10-15 16:14:19 -0400627/* struct btrfs_dir_item */
628BTRFS_SETGET_FUNCS(dir_flags, struct btrfs_dir_item, flags, 16);
629BTRFS_SETGET_FUNCS(dir_type, struct btrfs_dir_item, type, 8);
630BTRFS_SETGET_FUNCS(dir_name_len, struct btrfs_dir_item, name_len, 16);
631
632static inline void btrfs_dir_item_key(struct extent_buffer *eb,
633 struct btrfs_dir_item *item,
634 struct btrfs_disk_key *key)
Chris Mason1e1d2702007-03-15 19:03:33 -0400635{
Chris Mason5f39d392007-10-15 16:14:19 -0400636 read_eb_member(eb, item, struct btrfs_dir_item, location, key);
Chris Mason1e1d2702007-03-15 19:03:33 -0400637}
638
Chris Mason5f39d392007-10-15 16:14:19 -0400639static inline void btrfs_set_dir_item_key(struct extent_buffer *eb,
640 struct btrfs_dir_item *item,
641 struct btrfs_disk_key *key)
Chris Mason1e1d2702007-03-15 19:03:33 -0400642{
Chris Mason5f39d392007-10-15 16:14:19 -0400643 write_eb_member(eb, item, struct btrfs_dir_item, location, key);
Chris Mason1e1d2702007-03-15 19:03:33 -0400644}
645
Chris Mason5f39d392007-10-15 16:14:19 -0400646/* struct btrfs_disk_key */
647BTRFS_SETGET_STACK_FUNCS(disk_key_objectid, struct btrfs_disk_key,
648 objectid, 64);
649BTRFS_SETGET_STACK_FUNCS(disk_key_offset, struct btrfs_disk_key, offset, 64);
650BTRFS_SETGET_STACK_FUNCS(disk_key_type, struct btrfs_disk_key, type, 8);
Chris Mason1d4f6402007-03-15 15:18:43 -0400651
Chris Masone2fa7222007-03-12 16:22:34 -0400652static inline void btrfs_disk_key_to_cpu(struct btrfs_key *cpu,
653 struct btrfs_disk_key *disk)
654{
655 cpu->offset = le64_to_cpu(disk->offset);
Chris Mason5f39d392007-10-15 16:14:19 -0400656 cpu->type = disk->type;
Chris Masone2fa7222007-03-12 16:22:34 -0400657 cpu->objectid = le64_to_cpu(disk->objectid);
658}
659
660static inline void btrfs_cpu_key_to_disk(struct btrfs_disk_key *disk,
661 struct btrfs_key *cpu)
662{
663 disk->offset = cpu_to_le64(cpu->offset);
Chris Mason5f39d392007-10-15 16:14:19 -0400664 disk->type = cpu->type;
Chris Masone2fa7222007-03-12 16:22:34 -0400665 disk->objectid = cpu_to_le64(cpu->objectid);
666}
667
Chris Mason5f39d392007-10-15 16:14:19 -0400668static inline void btrfs_node_key_to_cpu(struct extent_buffer *eb,
669 struct btrfs_key *key, int nr)
Chris Masone2fa7222007-03-12 16:22:34 -0400670{
Chris Mason5f39d392007-10-15 16:14:19 -0400671 struct btrfs_disk_key disk_key;
672 btrfs_node_key(eb, &disk_key, nr);
673 btrfs_disk_key_to_cpu(key, &disk_key);
Chris Masone2fa7222007-03-12 16:22:34 -0400674}
675
Chris Mason5f39d392007-10-15 16:14:19 -0400676static inline void btrfs_item_key_to_cpu(struct extent_buffer *eb,
677 struct btrfs_key *key, int nr)
Chris Masone2fa7222007-03-12 16:22:34 -0400678{
Chris Mason5f39d392007-10-15 16:14:19 -0400679 struct btrfs_disk_key disk_key;
680 btrfs_item_key(eb, &disk_key, nr);
681 btrfs_disk_key_to_cpu(key, &disk_key);
Chris Masone2fa7222007-03-12 16:22:34 -0400682}
683
Chris Mason5f39d392007-10-15 16:14:19 -0400684static inline void btrfs_dir_item_key_to_cpu(struct extent_buffer *eb,
685 struct btrfs_dir_item *item,
686 struct btrfs_key *key)
Chris Masone2fa7222007-03-12 16:22:34 -0400687{
Chris Mason5f39d392007-10-15 16:14:19 -0400688 struct btrfs_disk_key disk_key;
689 btrfs_dir_item_key(eb, item, &disk_key);
690 btrfs_disk_key_to_cpu(key, &disk_key);
Chris Masone2fa7222007-03-12 16:22:34 -0400691}
692
Chris Mason5f39d392007-10-15 16:14:19 -0400693
694static inline u8 btrfs_key_type(struct btrfs_key *key)
Chris Masone2fa7222007-03-12 16:22:34 -0400695{
Chris Mason5f39d392007-10-15 16:14:19 -0400696 return key->type;
Chris Masone2fa7222007-03-12 16:22:34 -0400697}
698
Chris Mason5f39d392007-10-15 16:14:19 -0400699static inline void btrfs_set_key_type(struct btrfs_key *key, u8 val)
Chris Masone2fa7222007-03-12 16:22:34 -0400700{
Chris Mason5f39d392007-10-15 16:14:19 -0400701 key->type = val;
Chris Masone2fa7222007-03-12 16:22:34 -0400702}
703
Chris Mason5f39d392007-10-15 16:14:19 -0400704/* struct btrfs_header */
705BTRFS_SETGET_HEADER_FUNCS(header_blocknr, struct btrfs_header, blocknr, 64);
706BTRFS_SETGET_HEADER_FUNCS(header_generation, struct btrfs_header,
707 generation, 64);
708BTRFS_SETGET_HEADER_FUNCS(header_owner, struct btrfs_header, owner, 64);
709BTRFS_SETGET_HEADER_FUNCS(header_nritems, struct btrfs_header, nritems, 32);
710BTRFS_SETGET_HEADER_FUNCS(header_flags, struct btrfs_header, flags, 16);
711BTRFS_SETGET_HEADER_FUNCS(header_level, struct btrfs_header, level, 8);
712
713static inline u8 *btrfs_header_fsid(struct extent_buffer *eb)
Chris Masone2fa7222007-03-12 16:22:34 -0400714{
Chris Mason5f39d392007-10-15 16:14:19 -0400715 unsigned long ptr = offsetof(struct btrfs_header, fsid);
716 return (u8 *)ptr;
Chris Masone2fa7222007-03-12 16:22:34 -0400717}
718
Chris Mason5f39d392007-10-15 16:14:19 -0400719static inline u8 *btrfs_super_fsid(struct extent_buffer *eb)
Chris Masona429e512007-04-18 16:15:28 -0400720{
Chris Mason5f39d392007-10-15 16:14:19 -0400721 unsigned long ptr = offsetof(struct btrfs_super_block, fsid);
722 return (u8 *)ptr;
Chris Masona429e512007-04-18 16:15:28 -0400723}
724
Chris Mason5f39d392007-10-15 16:14:19 -0400725static inline u8 *btrfs_header_csum(struct extent_buffer *eb)
Chris Masona429e512007-04-18 16:15:28 -0400726{
Chris Mason5f39d392007-10-15 16:14:19 -0400727 unsigned long ptr = offsetof(struct btrfs_header, csum);
728 return (u8 *)ptr;
Chris Masona429e512007-04-18 16:15:28 -0400729}
730
Chris Mason5f39d392007-10-15 16:14:19 -0400731static inline struct btrfs_node *btrfs_buffer_node(struct extent_buffer *eb)
Chris Masona429e512007-04-18 16:15:28 -0400732{
Chris Mason5f39d392007-10-15 16:14:19 -0400733 return NULL;
Chris Masona429e512007-04-18 16:15:28 -0400734}
735
Chris Mason5f39d392007-10-15 16:14:19 -0400736static inline struct btrfs_leaf *btrfs_buffer_leaf(struct extent_buffer *eb)
Chris Masona429e512007-04-18 16:15:28 -0400737{
Chris Mason5f39d392007-10-15 16:14:19 -0400738 return NULL;
Chris Masona429e512007-04-18 16:15:28 -0400739}
740
Chris Mason5f39d392007-10-15 16:14:19 -0400741static inline struct btrfs_header *btrfs_buffer_header(struct extent_buffer *eb)
Chris Mason7518a232007-03-12 12:01:18 -0400742{
Chris Mason5f39d392007-10-15 16:14:19 -0400743 return NULL;
Chris Mason7518a232007-03-12 12:01:18 -0400744}
745
Chris Mason5f39d392007-10-15 16:14:19 -0400746static inline int btrfs_is_leaf(struct extent_buffer *eb)
Chris Mason7518a232007-03-12 12:01:18 -0400747{
Chris Mason5f39d392007-10-15 16:14:19 -0400748 return (btrfs_header_level(eb) == 0);
Chris Mason7518a232007-03-12 12:01:18 -0400749}
750
Chris Mason5f39d392007-10-15 16:14:19 -0400751/* struct btrfs_root_item */
752BTRFS_SETGET_FUNCS(disk_root_refs, struct btrfs_root_item, refs, 32);
753BTRFS_SETGET_FUNCS(disk_root_blocknr, struct btrfs_root_item, blocknr, 64);
754
755BTRFS_SETGET_STACK_FUNCS(root_blocknr, struct btrfs_root_item, blocknr, 64);
756BTRFS_SETGET_STACK_FUNCS(root_dirid, struct btrfs_root_item, root_dirid, 64);
757BTRFS_SETGET_STACK_FUNCS(root_refs, struct btrfs_root_item, refs, 32);
758BTRFS_SETGET_STACK_FUNCS(root_flags, struct btrfs_root_item, flags, 32);
759BTRFS_SETGET_STACK_FUNCS(root_used, struct btrfs_root_item, blocks_used, 64);
760BTRFS_SETGET_STACK_FUNCS(root_limit, struct btrfs_root_item, block_limit, 64);
761
762/* struct btrfs_super_block */
763BTRFS_SETGET_STACK_FUNCS(super_blocknr, struct btrfs_super_block, blocknr, 64);
764BTRFS_SETGET_STACK_FUNCS(super_generation, struct btrfs_super_block,
765 generation, 64);
766BTRFS_SETGET_STACK_FUNCS(super_root, struct btrfs_super_block, root, 64);
767BTRFS_SETGET_STACK_FUNCS(super_total_blocks, struct btrfs_super_block,
768 total_blocks, 64);
769BTRFS_SETGET_STACK_FUNCS(super_blocks_used, struct btrfs_super_block,
770 blocks_used, 64);
771BTRFS_SETGET_STACK_FUNCS(super_sectorsize, struct btrfs_super_block,
772 sectorsize, 32);
773BTRFS_SETGET_STACK_FUNCS(super_nodesize, struct btrfs_super_block,
774 nodesize, 32);
775BTRFS_SETGET_STACK_FUNCS(super_leafsize, struct btrfs_super_block,
776 leafsize, 32);
777BTRFS_SETGET_STACK_FUNCS(super_root_dir, struct btrfs_super_block,
778 root_dir_objectid, 64);
779
780static inline unsigned long btrfs_leaf_data(struct extent_buffer *l)
Chris Mason7f5c1512007-03-23 15:56:19 -0400781{
Chris Mason5f39d392007-10-15 16:14:19 -0400782 return offsetof(struct btrfs_leaf, items);
Chris Mason7f5c1512007-03-23 15:56:19 -0400783}
784
Chris Mason5f39d392007-10-15 16:14:19 -0400785/* struct btrfs_file_extent_item */
786BTRFS_SETGET_FUNCS(file_extent_type, struct btrfs_file_extent_item, type, 8);
Chris Mason7f5c1512007-03-23 15:56:19 -0400787
Chris Mason5f39d392007-10-15 16:14:19 -0400788static inline unsigned long btrfs_file_extent_inline_start(struct
Chris Mason236454d2007-04-19 13:37:44 -0400789 btrfs_file_extent_item *e)
790{
Chris Mason5f39d392007-10-15 16:14:19 -0400791 unsigned long offset = (unsigned long)e;
792 offset += offsetof(struct btrfs_file_extent_item, disk_blocknr);
793 return offset;
Chris Mason236454d2007-04-19 13:37:44 -0400794}
795
796static inline u32 btrfs_file_extent_calc_inline_size(u32 datasize)
797{
Chris Mason5f39d392007-10-15 16:14:19 -0400798 return offsetof(struct btrfs_file_extent_item, disk_blocknr) + datasize;
Chris Mason236454d2007-04-19 13:37:44 -0400799}
800
Chris Mason5f39d392007-10-15 16:14:19 -0400801static inline u32 btrfs_file_extent_inline_len(struct extent_buffer *eb,
802 struct btrfs_item *e)
Chris Mason236454d2007-04-19 13:37:44 -0400803{
Chris Mason5f39d392007-10-15 16:14:19 -0400804 unsigned long offset;
805 offset = offsetof(struct btrfs_file_extent_item, disk_blocknr);
806 return btrfs_item_size(eb, e) - offset;
Chris Mason236454d2007-04-19 13:37:44 -0400807}
808
Chris Mason5f39d392007-10-15 16:14:19 -0400809BTRFS_SETGET_FUNCS(file_extent_disk_blocknr, struct btrfs_file_extent_item,
810 disk_blocknr, 64);
811BTRFS_SETGET_FUNCS(file_extent_generation, struct btrfs_file_extent_item,
812 generation, 64);
813BTRFS_SETGET_FUNCS(file_extent_disk_num_blocks, struct btrfs_file_extent_item,
814 disk_num_blocks, 64);
815BTRFS_SETGET_FUNCS(file_extent_offset, struct btrfs_file_extent_item,
816 offset, 64);
817BTRFS_SETGET_FUNCS(file_extent_num_blocks, struct btrfs_file_extent_item,
818 num_blocks, 64);
Chris Mason9f5fae22007-03-20 14:38:32 -0400819
Chris Masone20d96d2007-03-22 12:13:20 -0400820static inline struct btrfs_root *btrfs_sb(struct super_block *sb)
821{
822 return sb->s_fs_info;
823}
824
Josef Bacik58176a92007-08-29 15:47:34 -0400825static inline int btrfs_set_root_name(struct btrfs_root *root,
826 const char *name, int len)
827{
828 /* if we already have a name just free it */
829 if (root->name)
830 kfree(root->name);
831
832 root->name = kmalloc(len+1, GFP_KERNEL);
833 if (!root->name)
834 return -ENOMEM;
835
836 memcpy(root->name, name, len);
837 root->name[len] ='\0';
838
839 return 0;
840}
841
Chris Mason4beb1b82007-03-14 10:31:29 -0400842/* helper function to cast into the data area of the leaf. */
843#define btrfs_item_ptr(leaf, slot, type) \
Chris Mason123abc82007-03-14 14:14:43 -0400844 ((type *)(btrfs_leaf_data(leaf) + \
Chris Mason5f39d392007-10-15 16:14:19 -0400845 btrfs_item_offset_nr(leaf, slot)))
846
847#define btrfs_item_ptr_offset(leaf, slot) \
848 ((unsigned long)(btrfs_leaf_data(leaf) + \
849 btrfs_item_offset_nr(leaf, slot)))
Chris Mason4beb1b82007-03-14 10:31:29 -0400850
Chris Mason95e05282007-08-29 09:11:44 -0400851/* mount option defines and helpers */
852#define BTRFS_MOUNT_SUBVOL 0x000001
853#define btrfs_clear_opt(o, opt) o &= ~BTRFS_MOUNT_##opt
854#define btrfs_set_opt(o, opt) o |= BTRFS_MOUNT_##opt
855#define btrfs_test_opt(sb, opt) (BTRFS_SB(sb)->s_mount_opt & \
856 BTRFS_MOUNT_##opt)
Chris Masonb18c6682007-04-17 13:26:50 -0400857/* extent-tree.c */
Chris Masone9d0b132007-08-10 14:06:19 -0400858int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
859 struct btrfs_root *root);
Chris Masonccd467d2007-06-28 15:57:36 -0400860int btrfs_copy_pinned(struct btrfs_root *root, struct radix_tree_root *copy);
Chris Mason5276aed2007-06-11 21:33:38 -0400861struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
862 btrfs_fs_info *info,
863 u64 blocknr);
Chris Mason31f3c992007-04-30 15:25:45 -0400864struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
865 struct btrfs_block_group_cache
Chris Masonbe744172007-05-06 10:15:01 -0400866 *hint, u64 search_start,
Chris Masonde428b62007-05-18 13:28:27 -0400867 int data, int owner);
Chris Masonc5739bb2007-04-10 09:27:04 -0400868int btrfs_inc_root_ref(struct btrfs_trans_handle *trans,
869 struct btrfs_root *root);
Chris Mason5f39d392007-10-15 16:14:19 -0400870struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Chris Mason6702ed42007-08-07 16:15:09 -0400871 struct btrfs_root *root, u64 hint,
872 u64 empty_size);
Chris Mason4d775672007-04-20 20:23:12 -0400873int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
874 struct btrfs_root *root, u64 owner,
Chris Mason6702ed42007-08-07 16:15:09 -0400875 u64 num_blocks, u64 empty_size, u64 search_start,
Chris Masonbe08c1b2007-05-03 09:06:49 -0400876 u64 search_end, struct btrfs_key *ins, int data);
Chris Masone089f052007-03-16 16:20:31 -0400877int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400878 struct extent_buffer *buf);
Chris Masone089f052007-03-16 16:20:31 -0400879int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
880 *root, u64 blocknr, u64 num_blocks, int pin);
Chris Masonccd467d2007-06-28 15:57:36 -0400881int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
882 struct btrfs_root *root,
883 struct radix_tree_root *unpin_radix);
Chris Masonb18c6682007-04-17 13:26:50 -0400884int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
885 struct btrfs_root *root,
886 u64 blocknr, u64 num_blocks);
Chris Mason9078a3e2007-04-26 16:46:15 -0400887int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
888 struct btrfs_root *root);
889int btrfs_free_block_groups(struct btrfs_fs_info *info);
890int btrfs_read_block_groups(struct btrfs_root *root);
Chris Masondee26a92007-03-26 16:00:06 -0400891/* ctree.c */
Chris Mason5f39d392007-10-15 16:14:19 -0400892int btrfs_cow_block(struct btrfs_trans_handle *trans,
893 struct btrfs_root *root, struct extent_buffer *buf,
894 struct extent_buffer *parent, int parent_slot,
895 struct extent_buffer **cow_ret);
Chris Mason6567e832007-04-16 09:22:45 -0400896int btrfs_extend_item(struct btrfs_trans_handle *trans, struct btrfs_root
897 *root, struct btrfs_path *path, u32 data_size);
Chris Masonb18c6682007-04-17 13:26:50 -0400898int btrfs_truncate_item(struct btrfs_trans_handle *trans,
899 struct btrfs_root *root,
900 struct btrfs_path *path,
901 u32 new_size);
Chris Masone089f052007-03-16 16:20:31 -0400902int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
903 *root, struct btrfs_key *key, struct btrfs_path *p, int
904 ins_len, int cow);
Chris Mason6702ed42007-08-07 16:15:09 -0400905int btrfs_realloc_node(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400906 struct btrfs_root *root, struct extent_buffer *parent,
Chris Masone9d0b132007-08-10 14:06:19 -0400907 int cache_only, u64 *last_ret);
Chris Mason234b63a2007-03-13 10:46:10 -0400908void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400909struct btrfs_path *btrfs_alloc_path(void);
910void btrfs_free_path(struct btrfs_path *p);
Chris Mason234b63a2007-03-13 10:46:10 -0400911void btrfs_init_path(struct btrfs_path *p);
Chris Masone089f052007-03-16 16:20:31 -0400912int btrfs_del_item(struct btrfs_trans_handle *trans, struct btrfs_root *root,
913 struct btrfs_path *path);
914int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
915 *root, struct btrfs_key *key, void *data, u32 data_size);
916int btrfs_insert_empty_item(struct btrfs_trans_handle *trans, struct btrfs_root
917 *root, struct btrfs_path *path, struct btrfs_key
918 *cpu_key, u32 data_size);
Chris Mason234b63a2007-03-13 10:46:10 -0400919int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path);
Chris Mason5f39d392007-10-15 16:14:19 -0400920int btrfs_leaf_free_space(struct btrfs_root *root, struct extent_buffer *leaf);
Chris Masone089f052007-03-16 16:20:31 -0400921int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason9f3a7422007-08-07 15:52:19 -0400922 *root);
Chris Masondee26a92007-03-26 16:00:06 -0400923/* root-item.c */
Chris Masone089f052007-03-16 16:20:31 -0400924int btrfs_del_root(struct btrfs_trans_handle *trans, struct btrfs_root *root,
925 struct btrfs_key *key);
926int btrfs_insert_root(struct btrfs_trans_handle *trans, struct btrfs_root
927 *root, struct btrfs_key *key, struct btrfs_root_item
928 *item);
929int btrfs_update_root(struct btrfs_trans_handle *trans, struct btrfs_root
930 *root, struct btrfs_key *key, struct btrfs_root_item
931 *item);
932int btrfs_find_last_root(struct btrfs_root *root, u64 objectid, struct
933 btrfs_root_item *item, struct btrfs_key *key);
Chris Mason5ce14bb2007-09-11 11:15:39 -0400934int btrfs_find_dead_roots(struct btrfs_root *root, u64 objectid,
935 struct btrfs_root *latest_root);
Chris Masondee26a92007-03-26 16:00:06 -0400936/* dir-item.c */
Chris Masone089f052007-03-16 16:20:31 -0400937int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masond6e4a422007-04-06 15:37:36 -0400938 *root, const char *name, int name_len, u64 dir,
939 struct btrfs_key *location, u8 type);
Chris Mason7e381802007-04-19 15:36:27 -0400940struct btrfs_dir_item *btrfs_lookup_dir_item(struct btrfs_trans_handle *trans,
941 struct btrfs_root *root,
942 struct btrfs_path *path, u64 dir,
943 const char *name, int name_len,
944 int mod);
945struct btrfs_dir_item *
946btrfs_lookup_dir_index_item(struct btrfs_trans_handle *trans,
947 struct btrfs_root *root,
948 struct btrfs_path *path, u64 dir,
949 u64 objectid, const char *name, int name_len,
950 int mod);
951struct btrfs_dir_item *btrfs_match_dir_item_name(struct btrfs_root *root,
952 struct btrfs_path *path,
Chris Mason7f5c1512007-03-23 15:56:19 -0400953 const char *name, int name_len);
Chris Mason7e381802007-04-19 15:36:27 -0400954int btrfs_delete_one_dir_name(struct btrfs_trans_handle *trans,
955 struct btrfs_root *root,
956 struct btrfs_path *path,
957 struct btrfs_dir_item *di);
Chris Masondee26a92007-03-26 16:00:06 -0400958/* inode-map.c */
Chris Mason9f5fae22007-03-20 14:38:32 -0400959int btrfs_find_free_objectid(struct btrfs_trans_handle *trans,
960 struct btrfs_root *fs_root,
961 u64 dirid, u64 *objectid);
Chris Mason5be6f7f2007-04-05 13:35:25 -0400962int btrfs_find_highest_inode(struct btrfs_root *fs_root, u64 *objectid);
963
Chris Masondee26a92007-03-26 16:00:06 -0400964/* inode-item.c */
Chris Mason5f39d392007-10-15 16:14:19 -0400965int btrfs_insert_empty_inode(struct btrfs_trans_handle *trans,
966 struct btrfs_root *root,
967 struct btrfs_path *path, u64 objectid);
Chris Mason293ffd52007-03-20 15:57:25 -0400968int btrfs_lookup_inode(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masond6e4a422007-04-06 15:37:36 -0400969 *root, struct btrfs_path *path,
970 struct btrfs_key *location, int mod);
Chris Masondee26a92007-03-26 16:00:06 -0400971
972/* file-item.c */
Chris Masonb18c6682007-04-17 13:26:50 -0400973int btrfs_insert_file_extent(struct btrfs_trans_handle *trans,
Chris Masondee26a92007-03-26 16:00:06 -0400974 struct btrfs_root *root,
Chris Masonb18c6682007-04-17 13:26:50 -0400975 u64 objectid, u64 pos, u64 offset,
Chris Mason3a686372007-05-24 13:35:57 -0400976 u64 disk_num_blocks,
Chris Masonb18c6682007-04-17 13:26:50 -0400977 u64 num_blocks);
Chris Masondee26a92007-03-26 16:00:06 -0400978int btrfs_lookup_file_extent(struct btrfs_trans_handle *trans,
979 struct btrfs_root *root,
980 struct btrfs_path *path, u64 objectid,
Chris Mason9773a782007-03-27 11:26:26 -0400981 u64 blocknr, int mod);
Chris Masonf254e522007-03-29 15:15:27 -0400982int btrfs_csum_file_block(struct btrfs_trans_handle *trans,
983 struct btrfs_root *root,
984 u64 objectid, u64 offset,
985 char *data, size_t len);
Chris Masonb18c6682007-04-17 13:26:50 -0400986struct btrfs_csum_item *btrfs_lookup_csum(struct btrfs_trans_handle *trans,
987 struct btrfs_root *root,
988 struct btrfs_path *path,
989 u64 objectid, u64 offset,
990 int cow);
Chris Mason1de037a2007-05-29 15:17:08 -0400991int btrfs_csum_truncate(struct btrfs_trans_handle *trans,
992 struct btrfs_root *root, struct btrfs_path *path,
993 u64 isize);
Chris Mason39279cc2007-06-12 06:35:45 -0400994/* inode.c */
Chris Mason9ebefb182007-06-15 13:50:00 -0400995int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page);
996int btrfs_readpage(struct file *file, struct page *page);
Chris Mason39279cc2007-06-12 06:35:45 -0400997void btrfs_delete_inode(struct inode *inode);
998void btrfs_read_locked_inode(struct inode *inode);
999int btrfs_write_inode(struct inode *inode, int wait);
1000void btrfs_dirty_inode(struct inode *inode);
1001struct inode *btrfs_alloc_inode(struct super_block *sb);
1002void btrfs_destroy_inode(struct inode *inode);
1003int btrfs_init_cachep(void);
1004void btrfs_destroy_cachep(void);
Christoph Hellwig34287aa2007-09-14 10:22:47 -04001005long btrfs_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
Chris Mason39279cc2007-06-12 06:35:45 -04001006struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
1007 struct btrfs_root *root);
1008int btrfs_commit_write(struct file *file, struct page *page,
1009 unsigned from, unsigned to);
Chris Masona52d9a82007-08-27 16:49:44 -04001010struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
1011 size_t page_offset, u64 start, u64 end,
1012 int create);
1013int btrfs_update_inode(struct btrfs_trans_handle *trans,
1014 struct btrfs_root *root,
1015 struct inode *inode);
Chris Mason39279cc2007-06-12 06:35:45 -04001016/* file.c */
Chris Masona52d9a82007-08-27 16:49:44 -04001017int btrfs_drop_extent_cache(struct inode *inode, u64 start, u64 end);
Chris Mason39279cc2007-06-12 06:35:45 -04001018extern struct file_operations btrfs_file_operations;
1019int btrfs_drop_extents(struct btrfs_trans_handle *trans,
1020 struct btrfs_root *root, struct inode *inode,
1021 u64 start, u64 end, u64 *hint_block);
Chris Mason6702ed42007-08-07 16:15:09 -04001022/* tree-defrag.c */
1023int btrfs_defrag_leaves(struct btrfs_trans_handle *trans,
1024 struct btrfs_root *root, int cache_only);
Josef Bacik58176a92007-08-29 15:47:34 -04001025
1026/* sysfs.c */
1027int btrfs_init_sysfs(void);
1028void btrfs_exit_sysfs(void);
1029int btrfs_sysfs_add_super(struct btrfs_fs_info *fs);
1030int btrfs_sysfs_add_root(struct btrfs_root *root);
1031void btrfs_sysfs_del_root(struct btrfs_root *root);
1032void btrfs_sysfs_del_super(struct btrfs_fs_info *root);
1033
Chris Masoneb60cea2007-02-02 09:18:22 -05001034#endif