xref: /openbmc/linux/fs/btrfs/btrfs_inode.h (revision f248679e86fead40cc78e724c7181d6bec1a2046)
16cbd5570SChris Mason /*
26cbd5570SChris Mason  * Copyright (C) 2007 Oracle.  All rights reserved.
36cbd5570SChris Mason  *
46cbd5570SChris Mason  * This program is free software; you can redistribute it and/or
56cbd5570SChris Mason  * modify it under the terms of the GNU General Public
66cbd5570SChris Mason  * License v2 as published by the Free Software Foundation.
76cbd5570SChris Mason  *
86cbd5570SChris Mason  * This program is distributed in the hope that it will be useful,
96cbd5570SChris Mason  * but WITHOUT ANY WARRANTY; without even the implied warranty of
106cbd5570SChris Mason  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
116cbd5570SChris Mason  * General Public License for more details.
126cbd5570SChris Mason  *
136cbd5570SChris Mason  * You should have received a copy of the GNU General Public
146cbd5570SChris Mason  * License along with this program; if not, write to the
156cbd5570SChris Mason  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
166cbd5570SChris Mason  * Boston, MA 021110-1307, USA.
176cbd5570SChris Mason  */
186cbd5570SChris Mason 
192c90e5d6SChris Mason #ifndef __BTRFS_I__
202c90e5d6SChris Mason #define __BTRFS_I__
212c90e5d6SChris Mason 
22a52d9a80SChris Mason #include "extent_map.h"
23d1310b2eSChris Mason #include "extent_io.h"
24e6dcd2dcSChris Mason #include "ordered-data.h"
2516cdcec7SMiao Xie #include "delayed-inode.h"
26a52d9a80SChris Mason 
27f1ace244SAneesh /* in memory btrfs inode */
282c90e5d6SChris Mason struct btrfs_inode {
29d352ac68SChris Mason 	/* which subvolume this inode belongs to */
30d6e4a428SChris Mason 	struct btrfs_root *root;
31d352ac68SChris Mason 
32d352ac68SChris Mason 	/* key used to find this inode on disk.  This is used by the code
33d352ac68SChris Mason 	 * to read in roots of subvolumes
34d352ac68SChris Mason 	 */
35d6e4a428SChris Mason 	struct btrfs_key location;
36d352ac68SChris Mason 
379e0baf60SJosef Bacik 	/* Lock for counters */
389e0baf60SJosef Bacik 	spinlock_t lock;
399e0baf60SJosef Bacik 
40d352ac68SChris Mason 	/* the extent_tree has caches of all the extent mappings to disk */
41a52d9a80SChris Mason 	struct extent_map_tree extent_tree;
42d352ac68SChris Mason 
43d352ac68SChris Mason 	/* the io_tree does range state (DIRTY, LOCKED etc) */
44d1310b2eSChris Mason 	struct extent_io_tree io_tree;
45d352ac68SChris Mason 
46d352ac68SChris Mason 	/* special utility tree used to record which mirrors have already been
47d352ac68SChris Mason 	 * tried when checksums fail for a given block
48d352ac68SChris Mason 	 */
497e38326fSChris Mason 	struct extent_io_tree io_failure_tree;
50d352ac68SChris Mason 
51d352ac68SChris Mason 	/* held while logging the inode in tree-log.c */
52e02119d5SChris Mason 	struct mutex log_mutex;
53d352ac68SChris Mason 
54*f248679eSJosef Bacik 	/* held while doing delalloc reservations */
55*f248679eSJosef Bacik 	struct mutex delalloc_mutex;
56*f248679eSJosef Bacik 
57d352ac68SChris Mason 	/* used to order data wrt metadata */
58e6dcd2dcSChris Mason 	struct btrfs_ordered_inode_tree ordered_tree;
5915ee9bc7SJosef Bacik 
607b128766SJosef Bacik 	/* for keeping track of orphaned inodes */
617b128766SJosef Bacik 	struct list_head i_orphan;
627b128766SJosef Bacik 
63d352ac68SChris Mason 	/* list of all the delalloc inodes in the FS.  There are times we need
64d352ac68SChris Mason 	 * to write all the delalloc pages to disk, and this list is used
65d352ac68SChris Mason 	 * to walk them all.
66d352ac68SChris Mason 	 */
67ea8c2819SChris Mason 	struct list_head delalloc_inodes;
68ea8c2819SChris Mason 
695a3f23d5SChris Mason 	/*
705a3f23d5SChris Mason 	 * list for tracking inodes that must be sent to disk before a
715a3f23d5SChris Mason 	 * rename or truncate commit
725a3f23d5SChris Mason 	 */
735a3f23d5SChris Mason 	struct list_head ordered_operations;
745a3f23d5SChris Mason 
755d4f98a2SYan Zheng 	/* node for the red-black tree that links inodes in subvolume root */
765d4f98a2SYan Zheng 	struct rb_node rb_node;
775d4f98a2SYan Zheng 
786a63209fSJosef Bacik 	/* the space_info for where this inode's data allocations are done */
796a63209fSJosef Bacik 	struct btrfs_space_info *space_info;
806a63209fSJosef Bacik 
81d352ac68SChris Mason 	/* full 64 bit generation number, struct vfs_inode doesn't have a big
82d352ac68SChris Mason 	 * enough field for this.
83d352ac68SChris Mason 	 */
84e02119d5SChris Mason 	u64 generation;
85e02119d5SChris Mason 
86c3027eb5SChris Mason 	/* sequence number for NFS changes */
87c3027eb5SChris Mason 	u64 sequence;
88c3027eb5SChris Mason 
8915ee9bc7SJosef Bacik 	/*
9015ee9bc7SJosef Bacik 	 * transid of the trans_handle that last modified this inode
9115ee9bc7SJosef Bacik 	 */
9215ee9bc7SJosef Bacik 	u64 last_trans;
93257c62e1SChris Mason 
94257c62e1SChris Mason 	/*
95257c62e1SChris Mason 	 * log transid when this inode was last modified
96257c62e1SChris Mason 	 */
97257c62e1SChris Mason 	u64 last_sub_trans;
98257c62e1SChris Mason 
99e02119d5SChris Mason 	/*
100e02119d5SChris Mason 	 * transid that last logged this inode
101e02119d5SChris Mason 	 */
102e02119d5SChris Mason 	u64 logged_trans;
10349eb7e46SChris Mason 
104d352ac68SChris Mason 	/* total number of bytes pending delalloc, used by stat to calc the
105d352ac68SChris Mason 	 * real block usage of the file
106d352ac68SChris Mason 	 */
1079069218dSChris Mason 	u64 delalloc_bytes;
108d352ac68SChris Mason 
109d352ac68SChris Mason 	/*
110d352ac68SChris Mason 	 * the size of the file stored in the metadata on disk.  data=ordered
111d352ac68SChris Mason 	 * means the in-memory i_size might be larger than the size on disk
112d352ac68SChris Mason 	 * because not all the blocks are written yet.
113d352ac68SChris Mason 	 */
114dbe674a9SChris Mason 	u64 disk_i_size;
115d352ac68SChris Mason 
116aec7477bSJosef Bacik 	/*
117aec7477bSJosef Bacik 	 * if this is a directory then index_cnt is the counter for the index
118aec7477bSJosef Bacik 	 * number for new files that are created
119aec7477bSJosef Bacik 	 */
120aec7477bSJosef Bacik 	u64 index_cnt;
121d352ac68SChris Mason 
12212fcfd22SChris Mason 	/* the fsync log has some corner cases that mean we have to check
12312fcfd22SChris Mason 	 * directories to see if any unlinks have been done before
12412fcfd22SChris Mason 	 * the directory was logged.  See tree-log.c for all the
12512fcfd22SChris Mason 	 * details
12612fcfd22SChris Mason 	 */
12712fcfd22SChris Mason 	u64 last_unlink_trans;
12812fcfd22SChris Mason 
1297709cde3SJosef Bacik 	/*
1307709cde3SJosef Bacik 	 * Number of bytes outstanding that are going to need csums.  This is
1317709cde3SJosef Bacik 	 * used in ENOSPC accounting.
1327709cde3SJosef Bacik 	 */
1337709cde3SJosef Bacik 	u64 csum_bytes;
1347709cde3SJosef Bacik 
135f1bdcc0aSJosef Bacik 	/* flags field from the on disk inode */
136f1bdcc0aSJosef Bacik 	u32 flags;
137f1bdcc0aSJosef Bacik 
1385a3f23d5SChris Mason 	/*
13932c00affSJosef Bacik 	 * Counters to keep track of the number of extent item's we may use due
14032c00affSJosef Bacik 	 * to delalloc and such.  outstanding_extents is the number of extent
14132c00affSJosef Bacik 	 * items we think we'll end up using, and reserved_extents is the number
14232c00affSJosef Bacik 	 * of extent items we've reserved metadata for.
1439ed74f2dSJosef Bacik 	 */
1449e0baf60SJosef Bacik 	unsigned outstanding_extents;
1459e0baf60SJosef Bacik 	unsigned reserved_extents;
1469ed74f2dSJosef Bacik 
1479ed74f2dSJosef Bacik 	/*
1485a3f23d5SChris Mason 	 * ordered_data_close is set by truncate when a file that used
1495a3f23d5SChris Mason 	 * to have good data has been truncated to zero.  When it is set
1505a3f23d5SChris Mason 	 * the btrfs file release call will add this inode to the
1515a3f23d5SChris Mason 	 * ordered operations list so that we make sure to flush out any
1525a3f23d5SChris Mason 	 * new data the application may have written before commit.
1535a3f23d5SChris Mason 	 */
1545a3f23d5SChris Mason 	unsigned ordered_data_close:1;
155d68fc57bSYan, Zheng 	unsigned orphan_meta_reserved:1;
1564df27c4dSYan, Zheng 	unsigned dummy_inode:1;
1574cb5300bSChris Mason 	unsigned in_defrag:1;
1587fd2ae21SJosef Bacik 	unsigned delalloc_meta_reserved:1;
1595a3f23d5SChris Mason 
1601e701a32SChris Mason 	/*
1611e701a32SChris Mason 	 * always compress this one file
1621e701a32SChris Mason 	 */
163261507a0SLi Zefan 	unsigned force_compress:4;
1641e701a32SChris Mason 
16516cdcec7SMiao Xie 	struct btrfs_delayed_node *delayed_node;
16616cdcec7SMiao Xie 
167d352ac68SChris Mason 	struct inode vfs_inode;
1682c90e5d6SChris Mason };
169dbe674a9SChris Mason 
17016cdcec7SMiao Xie extern unsigned char btrfs_filetype_table[];
17116cdcec7SMiao Xie 
1722c90e5d6SChris Mason static inline struct btrfs_inode *BTRFS_I(struct inode *inode)
1732c90e5d6SChris Mason {
1742c90e5d6SChris Mason 	return container_of(inode, struct btrfs_inode, vfs_inode);
1752c90e5d6SChris Mason }
1762c90e5d6SChris Mason 
17733345d01SLi Zefan static inline u64 btrfs_ino(struct inode *inode)
17833345d01SLi Zefan {
17933345d01SLi Zefan 	u64 ino = BTRFS_I(inode)->location.objectid;
18033345d01SLi Zefan 
18114c7cca7SLiu Bo 	/*
18214c7cca7SLiu Bo 	 * !ino: btree_inode
18314c7cca7SLiu Bo 	 * type == BTRFS_ROOT_ITEM_KEY: subvol dir
18414c7cca7SLiu Bo 	 */
18514c7cca7SLiu Bo 	if (!ino || BTRFS_I(inode)->location.type == BTRFS_ROOT_ITEM_KEY)
18633345d01SLi Zefan 		ino = inode->i_ino;
18733345d01SLi Zefan 	return ino;
18833345d01SLi Zefan }
18933345d01SLi Zefan 
190dbe674a9SChris Mason static inline void btrfs_i_size_write(struct inode *inode, u64 size)
191dbe674a9SChris Mason {
192c2167754SYan, Zheng 	i_size_write(inode, size);
193dbe674a9SChris Mason 	BTRFS_I(inode)->disk_i_size = size;
194dbe674a9SChris Mason }
195dbe674a9SChris Mason 
1962cf8572dSChris Mason static inline bool btrfs_is_free_space_inode(struct btrfs_root *root,
1972cf8572dSChris Mason 				       struct inode *inode)
1982cf8572dSChris Mason {
1992cf8572dSChris Mason 	if (root == root->fs_info->tree_root ||
2002cf8572dSChris Mason 	    BTRFS_I(inode)->location.objectid == BTRFS_FREE_INO_OBJECTID)
2012cf8572dSChris Mason 		return true;
2022cf8572dSChris Mason 	return false;
2032cf8572dSChris Mason }
2042cf8572dSChris Mason 
2052c90e5d6SChris Mason #endif
206