xref: /openbmc/linux/fs/btrfs/btrfs_inode.h (revision bb14a59b619d3a9993c3fa04bb10347db35ca550)
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 
22778ba82bSFilipe David Borba Manana #include <linux/hash.h>
23a52d9a80SChris Mason #include "extent_map.h"
24d1310b2eSChris Mason #include "extent_io.h"
25e6dcd2dcSChris Mason #include "ordered-data.h"
2616cdcec7SMiao Xie #include "delayed-inode.h"
27a52d9a80SChris Mason 
2872ac3c0dSJosef Bacik /*
2972ac3c0dSJosef Bacik  * ordered_data_close is set by truncate when a file that used
3072ac3c0dSJosef Bacik  * to have good data has been truncated to zero.  When it is set
3172ac3c0dSJosef Bacik  * the btrfs file release call will add this inode to the
3272ac3c0dSJosef Bacik  * ordered operations list so that we make sure to flush out any
3372ac3c0dSJosef Bacik  * new data the application may have written before commit.
3472ac3c0dSJosef Bacik  */
3572ac3c0dSJosef Bacik #define BTRFS_INODE_ORDERED_DATA_CLOSE		0
3672ac3c0dSJosef Bacik #define BTRFS_INODE_ORPHAN_META_RESERVED	1
3772ac3c0dSJosef Bacik #define BTRFS_INODE_DUMMY			2
3872ac3c0dSJosef Bacik #define BTRFS_INODE_IN_DEFRAG			3
3972ac3c0dSJosef Bacik #define BTRFS_INODE_DELALLOC_META_RESERVED	4
408a35d95fSJosef Bacik #define BTRFS_INODE_HAS_ORPHAN_ITEM		5
417ddf5a42SJosef Bacik #define BTRFS_INODE_HAS_ASYNC_EXTENT		6
425dc562c5SJosef Bacik #define BTRFS_INODE_NEEDS_FULL_SYNC		7
43e9976151SJosef Bacik #define BTRFS_INODE_COPY_EVERYTHING		8
44df0af1a5SMiao Xie #define BTRFS_INODE_IN_DELALLOC_LIST		9
452e60a51eSMiao Xie #define BTRFS_INODE_READDIO_NEED_LOCK		10
4663541927SFilipe David Borba Manana #define BTRFS_INODE_HAS_PROPS		        11
4772ac3c0dSJosef Bacik 
48f1ace244SAneesh /* in memory btrfs inode */
492c90e5d6SChris Mason struct btrfs_inode {
50d352ac68SChris Mason 	/* which subvolume this inode belongs to */
51d6e4a428SChris Mason 	struct btrfs_root *root;
52d352ac68SChris Mason 
53d352ac68SChris Mason 	/* key used to find this inode on disk.  This is used by the code
54d352ac68SChris Mason 	 * to read in roots of subvolumes
55d352ac68SChris Mason 	 */
56d6e4a428SChris Mason 	struct btrfs_key location;
57d352ac68SChris Mason 
589e0baf60SJosef Bacik 	/* Lock for counters */
599e0baf60SJosef Bacik 	spinlock_t lock;
609e0baf60SJosef Bacik 
61d352ac68SChris Mason 	/* the extent_tree has caches of all the extent mappings to disk */
62a52d9a80SChris Mason 	struct extent_map_tree extent_tree;
63d352ac68SChris Mason 
64d352ac68SChris Mason 	/* the io_tree does range state (DIRTY, LOCKED etc) */
65d1310b2eSChris Mason 	struct extent_io_tree io_tree;
66d352ac68SChris Mason 
67d352ac68SChris Mason 	/* special utility tree used to record which mirrors have already been
68d352ac68SChris Mason 	 * tried when checksums fail for a given block
69d352ac68SChris Mason 	 */
707e38326fSChris Mason 	struct extent_io_tree io_failure_tree;
71d352ac68SChris Mason 
72d352ac68SChris Mason 	/* held while logging the inode in tree-log.c */
73e02119d5SChris Mason 	struct mutex log_mutex;
74d352ac68SChris Mason 
75f248679eSJosef Bacik 	/* held while doing delalloc reservations */
76f248679eSJosef Bacik 	struct mutex delalloc_mutex;
77f248679eSJosef Bacik 
78d352ac68SChris Mason 	/* used to order data wrt metadata */
79e6dcd2dcSChris Mason 	struct btrfs_ordered_inode_tree ordered_tree;
8015ee9bc7SJosef Bacik 
81d352ac68SChris Mason 	/* list of all the delalloc inodes in the FS.  There are times we need
82d352ac68SChris Mason 	 * to write all the delalloc pages to disk, and this list is used
83d352ac68SChris Mason 	 * to walk them all.
84d352ac68SChris Mason 	 */
85ea8c2819SChris Mason 	struct list_head delalloc_inodes;
86ea8c2819SChris Mason 
875a3f23d5SChris Mason 	/*
885a3f23d5SChris Mason 	 * list for tracking inodes that must be sent to disk before a
895a3f23d5SChris Mason 	 * rename or truncate commit
905a3f23d5SChris Mason 	 */
915a3f23d5SChris Mason 	struct list_head ordered_operations;
925a3f23d5SChris Mason 
935d4f98a2SYan Zheng 	/* node for the red-black tree that links inodes in subvolume root */
945d4f98a2SYan Zheng 	struct rb_node rb_node;
955d4f98a2SYan Zheng 
9672ac3c0dSJosef Bacik 	unsigned long runtime_flags;
9772ac3c0dSJosef Bacik 
989c931c5aSNathaniel Yazdani 	/* Keep track of who's O_SYNC/fsyncing currently */
99b812ce28SJosef Bacik 	atomic_t sync_writers;
100b812ce28SJosef Bacik 
101d352ac68SChris Mason 	/* full 64 bit generation number, struct vfs_inode doesn't have a big
102d352ac68SChris Mason 	 * enough field for this.
103d352ac68SChris Mason 	 */
104e02119d5SChris Mason 	u64 generation;
105e02119d5SChris Mason 
10615ee9bc7SJosef Bacik 	/*
10715ee9bc7SJosef Bacik 	 * transid of the trans_handle that last modified this inode
10815ee9bc7SJosef Bacik 	 */
10915ee9bc7SJosef Bacik 	u64 last_trans;
110257c62e1SChris Mason 
111257c62e1SChris Mason 	/*
112e02119d5SChris Mason 	 * transid that last logged this inode
113e02119d5SChris Mason 	 */
114e02119d5SChris Mason 	u64 logged_trans;
11549eb7e46SChris Mason 
116*bb14a59bSMiao Xie 	/*
117*bb14a59bSMiao Xie 	 * log transid when this inode was last modified
118*bb14a59bSMiao Xie 	 */
119*bb14a59bSMiao Xie 	int last_sub_trans;
120*bb14a59bSMiao Xie 
121*bb14a59bSMiao Xie 	/* a local copy of root's last_log_commit */
122*bb14a59bSMiao Xie 	int last_log_commit;
123*bb14a59bSMiao Xie 
124d352ac68SChris Mason 	/* total number of bytes pending delalloc, used by stat to calc the
125d352ac68SChris Mason 	 * real block usage of the file
126d352ac68SChris Mason 	 */
1279069218dSChris Mason 	u64 delalloc_bytes;
128d352ac68SChris Mason 
129d352ac68SChris Mason 	/*
130d352ac68SChris Mason 	 * the size of the file stored in the metadata on disk.  data=ordered
131d352ac68SChris Mason 	 * means the in-memory i_size might be larger than the size on disk
132d352ac68SChris Mason 	 * because not all the blocks are written yet.
133d352ac68SChris Mason 	 */
134dbe674a9SChris Mason 	u64 disk_i_size;
135d352ac68SChris Mason 
136aec7477bSJosef Bacik 	/*
137aec7477bSJosef Bacik 	 * if this is a directory then index_cnt is the counter for the index
138aec7477bSJosef Bacik 	 * number for new files that are created
139aec7477bSJosef Bacik 	 */
140aec7477bSJosef Bacik 	u64 index_cnt;
141d352ac68SChris Mason 
14267de1176SMiao Xie 	/* Cache the directory index number to speed the dir/file remove */
14367de1176SMiao Xie 	u64 dir_index;
14467de1176SMiao Xie 
14512fcfd22SChris Mason 	/* the fsync log has some corner cases that mean we have to check
14612fcfd22SChris Mason 	 * directories to see if any unlinks have been done before
14712fcfd22SChris Mason 	 * the directory was logged.  See tree-log.c for all the
14812fcfd22SChris Mason 	 * details
14912fcfd22SChris Mason 	 */
15012fcfd22SChris Mason 	u64 last_unlink_trans;
15112fcfd22SChris Mason 
1527709cde3SJosef Bacik 	/*
1537709cde3SJosef Bacik 	 * Number of bytes outstanding that are going to need csums.  This is
1547709cde3SJosef Bacik 	 * used in ENOSPC accounting.
1557709cde3SJosef Bacik 	 */
1567709cde3SJosef Bacik 	u64 csum_bytes;
1577709cde3SJosef Bacik 
158f1bdcc0aSJosef Bacik 	/* flags field from the on disk inode */
159f1bdcc0aSJosef Bacik 	u32 flags;
160f1bdcc0aSJosef Bacik 
1615a3f23d5SChris Mason 	/*
16232c00affSJosef Bacik 	 * Counters to keep track of the number of extent item's we may use due
16332c00affSJosef Bacik 	 * to delalloc and such.  outstanding_extents is the number of extent
16432c00affSJosef Bacik 	 * items we think we'll end up using, and reserved_extents is the number
16532c00affSJosef Bacik 	 * of extent items we've reserved metadata for.
1669ed74f2dSJosef Bacik 	 */
1679e0baf60SJosef Bacik 	unsigned outstanding_extents;
1689e0baf60SJosef Bacik 	unsigned reserved_extents;
1699ed74f2dSJosef Bacik 
1709ed74f2dSJosef Bacik 	/*
1711e701a32SChris Mason 	 * always compress this one file
1721e701a32SChris Mason 	 */
17372ac3c0dSJosef Bacik 	unsigned force_compress;
1741e701a32SChris Mason 
17516cdcec7SMiao Xie 	struct btrfs_delayed_node *delayed_node;
17616cdcec7SMiao Xie 
177d352ac68SChris Mason 	struct inode vfs_inode;
1782c90e5d6SChris Mason };
179dbe674a9SChris Mason 
18016cdcec7SMiao Xie extern unsigned char btrfs_filetype_table[];
18116cdcec7SMiao Xie 
1822c90e5d6SChris Mason static inline struct btrfs_inode *BTRFS_I(struct inode *inode)
1832c90e5d6SChris Mason {
1842c90e5d6SChris Mason 	return container_of(inode, struct btrfs_inode, vfs_inode);
1852c90e5d6SChris Mason }
1862c90e5d6SChris Mason 
187778ba82bSFilipe David Borba Manana static inline unsigned long btrfs_inode_hash(u64 objectid,
188778ba82bSFilipe David Borba Manana 					     const struct btrfs_root *root)
189778ba82bSFilipe David Borba Manana {
190778ba82bSFilipe David Borba Manana 	u64 h = objectid ^ (root->objectid * GOLDEN_RATIO_PRIME);
191778ba82bSFilipe David Borba Manana 
192778ba82bSFilipe David Borba Manana #if BITS_PER_LONG == 32
193778ba82bSFilipe David Borba Manana 	h = (h >> 32) ^ (h & 0xffffffff);
194778ba82bSFilipe David Borba Manana #endif
195778ba82bSFilipe David Borba Manana 
196778ba82bSFilipe David Borba Manana 	return (unsigned long)h;
197778ba82bSFilipe David Borba Manana }
198778ba82bSFilipe David Borba Manana 
199778ba82bSFilipe David Borba Manana static inline void btrfs_insert_inode_hash(struct inode *inode)
200778ba82bSFilipe David Borba Manana {
201778ba82bSFilipe David Borba Manana 	unsigned long h = btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root);
202778ba82bSFilipe David Borba Manana 
203778ba82bSFilipe David Borba Manana 	__insert_inode_hash(inode, h);
204778ba82bSFilipe David Borba Manana }
205778ba82bSFilipe David Borba Manana 
20633345d01SLi Zefan static inline u64 btrfs_ino(struct inode *inode)
20733345d01SLi Zefan {
20833345d01SLi Zefan 	u64 ino = BTRFS_I(inode)->location.objectid;
20933345d01SLi Zefan 
21014c7cca7SLiu Bo 	/*
21114c7cca7SLiu Bo 	 * !ino: btree_inode
21214c7cca7SLiu Bo 	 * type == BTRFS_ROOT_ITEM_KEY: subvol dir
21314c7cca7SLiu Bo 	 */
21414c7cca7SLiu Bo 	if (!ino || BTRFS_I(inode)->location.type == BTRFS_ROOT_ITEM_KEY)
21533345d01SLi Zefan 		ino = inode->i_ino;
21633345d01SLi Zefan 	return ino;
21733345d01SLi Zefan }
21833345d01SLi Zefan 
219dbe674a9SChris Mason static inline void btrfs_i_size_write(struct inode *inode, u64 size)
220dbe674a9SChris Mason {
221c2167754SYan, Zheng 	i_size_write(inode, size);
222dbe674a9SChris Mason 	BTRFS_I(inode)->disk_i_size = size;
223dbe674a9SChris Mason }
224dbe674a9SChris Mason 
22583eea1f1SLiu Bo static inline bool btrfs_is_free_space_inode(struct inode *inode)
2262cf8572dSChris Mason {
22783eea1f1SLiu Bo 	struct btrfs_root *root = BTRFS_I(inode)->root;
22883eea1f1SLiu Bo 
22951a8cf9dSLiu Bo 	if (root == root->fs_info->tree_root &&
23051a8cf9dSLiu Bo 	    btrfs_ino(inode) != BTRFS_BTREE_INODE_OBJECTID)
23151a8cf9dSLiu Bo 		return true;
23251a8cf9dSLiu Bo 	if (BTRFS_I(inode)->location.objectid == BTRFS_FREE_INO_OBJECTID)
2332cf8572dSChris Mason 		return true;
2342cf8572dSChris Mason 	return false;
2352cf8572dSChris Mason }
2362cf8572dSChris Mason 
23722ee6985SJosef Bacik static inline int btrfs_inode_in_log(struct inode *inode, u64 generation)
23822ee6985SJosef Bacik {
23922ee6985SJosef Bacik 	if (BTRFS_I(inode)->logged_trans == generation &&
240a5874ce6SJosef Bacik 	    BTRFS_I(inode)->last_sub_trans <=
241a5874ce6SJosef Bacik 	    BTRFS_I(inode)->last_log_commit &&
242a5874ce6SJosef Bacik 	    BTRFS_I(inode)->last_sub_trans <=
243a5874ce6SJosef Bacik 	    BTRFS_I(inode)->root->last_log_commit)
24446d8bc34SLiu Bo 		return 1;
24546d8bc34SLiu Bo 	return 0;
24622ee6985SJosef Bacik }
24722ee6985SJosef Bacik 
248facc8a22SMiao Xie struct btrfs_dio_private {
249facc8a22SMiao Xie 	struct inode *inode;
250facc8a22SMiao Xie 	u64 logical_offset;
251facc8a22SMiao Xie 	u64 disk_bytenr;
252facc8a22SMiao Xie 	u64 bytes;
253facc8a22SMiao Xie 	void *private;
254facc8a22SMiao Xie 
255facc8a22SMiao Xie 	/* number of bios pending for this dio */
256facc8a22SMiao Xie 	atomic_t pending_bios;
257facc8a22SMiao Xie 
258facc8a22SMiao Xie 	/* IO errors */
259facc8a22SMiao Xie 	int errors;
260facc8a22SMiao Xie 
261facc8a22SMiao Xie 	/* orig_bio is our btrfs_io_bio */
262facc8a22SMiao Xie 	struct bio *orig_bio;
263facc8a22SMiao Xie 
264facc8a22SMiao Xie 	/* dio_bio came from fs/direct-io.c */
265facc8a22SMiao Xie 	struct bio *dio_bio;
266facc8a22SMiao Xie 	u8 csum[0];
267facc8a22SMiao Xie };
268facc8a22SMiao Xie 
2692e60a51eSMiao Xie /*
2702e60a51eSMiao Xie  * Disable DIO read nolock optimization, so new dio readers will be forced
2712e60a51eSMiao Xie  * to grab i_mutex. It is used to avoid the endless truncate due to
2722e60a51eSMiao Xie  * nonlocked dio read.
2732e60a51eSMiao Xie  */
2742e60a51eSMiao Xie static inline void btrfs_inode_block_unlocked_dio(struct inode *inode)
2752e60a51eSMiao Xie {
2762e60a51eSMiao Xie 	set_bit(BTRFS_INODE_READDIO_NEED_LOCK, &BTRFS_I(inode)->runtime_flags);
2772e60a51eSMiao Xie 	smp_mb();
2782e60a51eSMiao Xie }
2792e60a51eSMiao Xie 
2802e60a51eSMiao Xie static inline void btrfs_inode_resume_unlocked_dio(struct inode *inode)
2812e60a51eSMiao Xie {
2822e60a51eSMiao Xie 	smp_mb__before_clear_bit();
2832e60a51eSMiao Xie 	clear_bit(BTRFS_INODE_READDIO_NEED_LOCK,
2842e60a51eSMiao Xie 		  &BTRFS_I(inode)->runtime_flags);
2852e60a51eSMiao Xie }
2862e60a51eSMiao Xie 
2872c90e5d6SChris Mason #endif
288