xref: /openbmc/linux/fs/btrfs/btrfs_inode.h (revision eec63c65dcbeb14b59c95159eb225b1fc2310806)
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 
582f2ff0eeSFilipe Manana 	/*
592f2ff0eeSFilipe Manana 	 * Lock for counters and all fields used to determine if the inode is in
602f2ff0eeSFilipe Manana 	 * the log or not (last_trans, last_sub_trans, last_log_commit,
612f2ff0eeSFilipe Manana 	 * logged_trans).
622f2ff0eeSFilipe Manana 	 */
639e0baf60SJosef Bacik 	spinlock_t lock;
649e0baf60SJosef Bacik 
65d352ac68SChris Mason 	/* the extent_tree has caches of all the extent mappings to disk */
66a52d9a80SChris Mason 	struct extent_map_tree extent_tree;
67d352ac68SChris Mason 
68d352ac68SChris Mason 	/* the io_tree does range state (DIRTY, LOCKED etc) */
69d1310b2eSChris Mason 	struct extent_io_tree io_tree;
70d352ac68SChris Mason 
71d352ac68SChris Mason 	/* special utility tree used to record which mirrors have already been
72d352ac68SChris Mason 	 * tried when checksums fail for a given block
73d352ac68SChris Mason 	 */
747e38326fSChris Mason 	struct extent_io_tree io_failure_tree;
75d352ac68SChris Mason 
76d352ac68SChris Mason 	/* held while logging the inode in tree-log.c */
77e02119d5SChris Mason 	struct mutex log_mutex;
78d352ac68SChris Mason 
79f248679eSJosef Bacik 	/* held while doing delalloc reservations */
80f248679eSJosef Bacik 	struct mutex delalloc_mutex;
81f248679eSJosef Bacik 
82d352ac68SChris Mason 	/* used to order data wrt metadata */
83e6dcd2dcSChris Mason 	struct btrfs_ordered_inode_tree ordered_tree;
8415ee9bc7SJosef Bacik 
85d352ac68SChris Mason 	/* list of all the delalloc inodes in the FS.  There are times we need
86d352ac68SChris Mason 	 * to write all the delalloc pages to disk, and this list is used
87d352ac68SChris Mason 	 * to walk them all.
88d352ac68SChris Mason 	 */
89ea8c2819SChris Mason 	struct list_head delalloc_inodes;
90ea8c2819SChris Mason 
915d4f98a2SYan Zheng 	/* node for the red-black tree that links inodes in subvolume root */
925d4f98a2SYan Zheng 	struct rb_node rb_node;
935d4f98a2SYan Zheng 
9472ac3c0dSJosef Bacik 	unsigned long runtime_flags;
9572ac3c0dSJosef Bacik 
969c931c5aSNathaniel Yazdani 	/* Keep track of who's O_SYNC/fsyncing currently */
97b812ce28SJosef Bacik 	atomic_t sync_writers;
98b812ce28SJosef Bacik 
99d352ac68SChris Mason 	/* full 64 bit generation number, struct vfs_inode doesn't have a big
100d352ac68SChris Mason 	 * enough field for this.
101d352ac68SChris Mason 	 */
102e02119d5SChris Mason 	u64 generation;
103e02119d5SChris Mason 
10415ee9bc7SJosef Bacik 	/*
10515ee9bc7SJosef Bacik 	 * transid of the trans_handle that last modified this inode
10615ee9bc7SJosef Bacik 	 */
10715ee9bc7SJosef Bacik 	u64 last_trans;
108257c62e1SChris Mason 
109257c62e1SChris Mason 	/*
110e02119d5SChris Mason 	 * transid that last logged this inode
111e02119d5SChris Mason 	 */
112e02119d5SChris Mason 	u64 logged_trans;
11349eb7e46SChris Mason 
114bb14a59bSMiao Xie 	/*
115bb14a59bSMiao Xie 	 * log transid when this inode was last modified
116bb14a59bSMiao Xie 	 */
117bb14a59bSMiao Xie 	int last_sub_trans;
118bb14a59bSMiao Xie 
119bb14a59bSMiao Xie 	/* a local copy of root's last_log_commit */
120bb14a59bSMiao Xie 	int last_log_commit;
121bb14a59bSMiao Xie 
122d352ac68SChris Mason 	/* total number of bytes pending delalloc, used by stat to calc the
123d352ac68SChris Mason 	 * real block usage of the file
124d352ac68SChris Mason 	 */
1259069218dSChris Mason 	u64 delalloc_bytes;
126d352ac68SChris Mason 
127d352ac68SChris Mason 	/*
128a7e3b975SFilipe Manana 	 * Total number of bytes pending delalloc that fall within a file
129a7e3b975SFilipe Manana 	 * range that is either a hole or beyond EOF (and no prealloc extent
130a7e3b975SFilipe Manana 	 * exists in the range). This is always <= delalloc_bytes.
131a7e3b975SFilipe Manana 	 */
132a7e3b975SFilipe Manana 	u64 new_delalloc_bytes;
133a7e3b975SFilipe Manana 
134a7e3b975SFilipe Manana 	/*
13547059d93SWang Shilong 	 * total number of bytes pending defrag, used by stat to check whether
13647059d93SWang Shilong 	 * it needs COW.
13747059d93SWang Shilong 	 */
13847059d93SWang Shilong 	u64 defrag_bytes;
13947059d93SWang Shilong 
14047059d93SWang Shilong 	/*
141d352ac68SChris Mason 	 * the size of the file stored in the metadata on disk.  data=ordered
142d352ac68SChris Mason 	 * means the in-memory i_size might be larger than the size on disk
143d352ac68SChris Mason 	 * because not all the blocks are written yet.
144d352ac68SChris Mason 	 */
145dbe674a9SChris Mason 	u64 disk_i_size;
146d352ac68SChris Mason 
147aec7477bSJosef Bacik 	/*
148aec7477bSJosef Bacik 	 * if this is a directory then index_cnt is the counter for the index
149aec7477bSJosef Bacik 	 * number for new files that are created
150aec7477bSJosef Bacik 	 */
151aec7477bSJosef Bacik 	u64 index_cnt;
152d352ac68SChris Mason 
15367de1176SMiao Xie 	/* Cache the directory index number to speed the dir/file remove */
15467de1176SMiao Xie 	u64 dir_index;
15567de1176SMiao Xie 
15612fcfd22SChris Mason 	/* the fsync log has some corner cases that mean we have to check
15712fcfd22SChris Mason 	 * directories to see if any unlinks have been done before
15812fcfd22SChris Mason 	 * the directory was logged.  See tree-log.c for all the
15912fcfd22SChris Mason 	 * details
16012fcfd22SChris Mason 	 */
16112fcfd22SChris Mason 	u64 last_unlink_trans;
16212fcfd22SChris Mason 
1637709cde3SJosef Bacik 	/*
1647709cde3SJosef Bacik 	 * Number of bytes outstanding that are going to need csums.  This is
1657709cde3SJosef Bacik 	 * used in ENOSPC accounting.
1667709cde3SJosef Bacik 	 */
1677709cde3SJosef Bacik 	u64 csum_bytes;
1687709cde3SJosef Bacik 
169f1bdcc0aSJosef Bacik 	/* flags field from the on disk inode */
170f1bdcc0aSJosef Bacik 	u32 flags;
171f1bdcc0aSJosef Bacik 
1725a3f23d5SChris Mason 	/*
17332c00affSJosef Bacik 	 * Counters to keep track of the number of extent item's we may use due
17432c00affSJosef Bacik 	 * to delalloc and such.  outstanding_extents is the number of extent
17532c00affSJosef Bacik 	 * items we think we'll end up using, and reserved_extents is the number
17632c00affSJosef Bacik 	 * of extent items we've reserved metadata for.
1779ed74f2dSJosef Bacik 	 */
1789e0baf60SJosef Bacik 	unsigned outstanding_extents;
1799e0baf60SJosef Bacik 	unsigned reserved_extents;
1809ed74f2dSJosef Bacik 
1819ed74f2dSJosef Bacik 	/*
182b52aa8c9SDavid Sterba 	 * Cached values of inode properties
1831e701a32SChris Mason 	 */
184b52aa8c9SDavid Sterba 	unsigned prop_compress;		/* per-file compression algorithm */
185*eec63c65SDavid Sterba 	/*
186*eec63c65SDavid Sterba 	 * Force compression on the file using the defrag ioctl, could be
187*eec63c65SDavid Sterba 	 * different from prop_compress and takes precedence if set
188*eec63c65SDavid Sterba 	 */
189*eec63c65SDavid Sterba 	unsigned defrag_compress;
1901e701a32SChris Mason 
19116cdcec7SMiao Xie 	struct btrfs_delayed_node *delayed_node;
19216cdcec7SMiao Xie 
1939cc97d64Schandan r 	/* File creation time. */
1949cc97d64Schandan r 	struct timespec i_otime;
1959cc97d64Schandan r 
1968089fe62SDavid Sterba 	/* Hook into fs_info->delayed_iputs */
1978089fe62SDavid Sterba 	struct list_head delayed_iput;
1988089fe62SDavid Sterba 	long delayed_iput_count;
1998089fe62SDavid Sterba 
2005f9a8a51SFilipe Manana 	/*
2015f9a8a51SFilipe Manana 	 * To avoid races between lockless (i_mutex not held) direct IO writes
2025f9a8a51SFilipe Manana 	 * and concurrent fsync requests. Direct IO writes must acquire read
2035f9a8a51SFilipe Manana 	 * access on this semaphore for creating an extent map and its
2045f9a8a51SFilipe Manana 	 * corresponding ordered extent. The fast fsync path must acquire write
2055f9a8a51SFilipe Manana 	 * access on this semaphore before it collects ordered extents and
2065f9a8a51SFilipe Manana 	 * extent maps.
2075f9a8a51SFilipe Manana 	 */
2085f9a8a51SFilipe Manana 	struct rw_semaphore dio_sem;
2095f9a8a51SFilipe Manana 
210d352ac68SChris Mason 	struct inode vfs_inode;
2112c90e5d6SChris Mason };
212dbe674a9SChris Mason 
21316cdcec7SMiao Xie extern unsigned char btrfs_filetype_table[];
21416cdcec7SMiao Xie 
2159a35b637SJeff Mahoney static inline struct btrfs_inode *BTRFS_I(const struct inode *inode)
2162c90e5d6SChris Mason {
2172c90e5d6SChris Mason 	return container_of(inode, struct btrfs_inode, vfs_inode);
2182c90e5d6SChris Mason }
2192c90e5d6SChris Mason 
220778ba82bSFilipe David Borba Manana static inline unsigned long btrfs_inode_hash(u64 objectid,
221778ba82bSFilipe David Borba Manana 					     const struct btrfs_root *root)
222778ba82bSFilipe David Borba Manana {
223778ba82bSFilipe David Borba Manana 	u64 h = objectid ^ (root->objectid * GOLDEN_RATIO_PRIME);
224778ba82bSFilipe David Borba Manana 
225778ba82bSFilipe David Borba Manana #if BITS_PER_LONG == 32
226778ba82bSFilipe David Borba Manana 	h = (h >> 32) ^ (h & 0xffffffff);
227778ba82bSFilipe David Borba Manana #endif
228778ba82bSFilipe David Borba Manana 
229778ba82bSFilipe David Borba Manana 	return (unsigned long)h;
230778ba82bSFilipe David Borba Manana }
231778ba82bSFilipe David Borba Manana 
232778ba82bSFilipe David Borba Manana static inline void btrfs_insert_inode_hash(struct inode *inode)
233778ba82bSFilipe David Borba Manana {
234778ba82bSFilipe David Borba Manana 	unsigned long h = btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root);
235778ba82bSFilipe David Borba Manana 
236778ba82bSFilipe David Borba Manana 	__insert_inode_hash(inode, h);
237778ba82bSFilipe David Borba Manana }
238778ba82bSFilipe David Borba Manana 
2399a35b637SJeff Mahoney static inline u64 btrfs_ino(const struct btrfs_inode *inode)
24033345d01SLi Zefan {
2414a0cc7caSNikolay Borisov 	u64 ino = inode->location.objectid;
24233345d01SLi Zefan 
24314c7cca7SLiu Bo 	/*
24414c7cca7SLiu Bo 	 * !ino: btree_inode
24514c7cca7SLiu Bo 	 * type == BTRFS_ROOT_ITEM_KEY: subvol dir
24614c7cca7SLiu Bo 	 */
2474a0cc7caSNikolay Borisov 	if (!ino || inode->location.type == BTRFS_ROOT_ITEM_KEY)
2484a0cc7caSNikolay Borisov 		ino = inode->vfs_inode.i_ino;
24933345d01SLi Zefan 	return ino;
25033345d01SLi Zefan }
25133345d01SLi Zefan 
2526ef06d27SNikolay Borisov static inline void btrfs_i_size_write(struct btrfs_inode *inode, u64 size)
253dbe674a9SChris Mason {
2546ef06d27SNikolay Borisov 	i_size_write(&inode->vfs_inode, size);
2556ef06d27SNikolay Borisov 	inode->disk_i_size = size;
256dbe674a9SChris Mason }
257dbe674a9SChris Mason 
25870ddc553SNikolay Borisov static inline bool btrfs_is_free_space_inode(struct btrfs_inode *inode)
2592cf8572dSChris Mason {
26070ddc553SNikolay Borisov 	struct btrfs_root *root = inode->root;
26183eea1f1SLiu Bo 
26251a8cf9dSLiu Bo 	if (root == root->fs_info->tree_root &&
26370ddc553SNikolay Borisov 	    btrfs_ino(inode) != BTRFS_BTREE_INODE_OBJECTID)
26451a8cf9dSLiu Bo 		return true;
26570ddc553SNikolay Borisov 	if (inode->location.objectid == BTRFS_FREE_INO_OBJECTID)
2662cf8572dSChris Mason 		return true;
2672cf8572dSChris Mason 	return false;
2682cf8572dSChris Mason }
2692cf8572dSChris Mason 
2700f8939b8SNikolay Borisov static inline int btrfs_inode_in_log(struct btrfs_inode *inode, u64 generation)
27122ee6985SJosef Bacik {
2722f2ff0eeSFilipe Manana 	int ret = 0;
2732f2ff0eeSFilipe Manana 
2740f8939b8SNikolay Borisov 	spin_lock(&inode->lock);
2750f8939b8SNikolay Borisov 	if (inode->logged_trans == generation &&
2760f8939b8SNikolay Borisov 	    inode->last_sub_trans <= inode->last_log_commit &&
2770f8939b8SNikolay Borisov 	    inode->last_sub_trans <= inode->root->last_log_commit) {
278125c4cf9SFilipe Manana 		/*
279125c4cf9SFilipe Manana 		 * After a ranged fsync we might have left some extent maps
280125c4cf9SFilipe Manana 		 * (that fall outside the fsync's range). So return false
281125c4cf9SFilipe Manana 		 * here if the list isn't empty, to make sure btrfs_log_inode()
282125c4cf9SFilipe Manana 		 * will be called and process those extent maps.
283125c4cf9SFilipe Manana 		 */
284125c4cf9SFilipe Manana 		smp_mb();
2850f8939b8SNikolay Borisov 		if (list_empty(&inode->extent_tree.modified_extents))
2862f2ff0eeSFilipe Manana 			ret = 1;
287125c4cf9SFilipe Manana 	}
2880f8939b8SNikolay Borisov 	spin_unlock(&inode->lock);
2892f2ff0eeSFilipe Manana 	return ret;
29022ee6985SJosef Bacik }
29122ee6985SJosef Bacik 
292c1dc0896SMiao Xie #define BTRFS_DIO_ORIG_BIO_SUBMITTED	0x1
293c1dc0896SMiao Xie 
294facc8a22SMiao Xie struct btrfs_dio_private {
295facc8a22SMiao Xie 	struct inode *inode;
296c1dc0896SMiao Xie 	unsigned long flags;
297facc8a22SMiao Xie 	u64 logical_offset;
298facc8a22SMiao Xie 	u64 disk_bytenr;
299facc8a22SMiao Xie 	u64 bytes;
300facc8a22SMiao Xie 	void *private;
301facc8a22SMiao Xie 
302facc8a22SMiao Xie 	/* number of bios pending for this dio */
303facc8a22SMiao Xie 	atomic_t pending_bios;
304facc8a22SMiao Xie 
305facc8a22SMiao Xie 	/* IO errors */
306facc8a22SMiao Xie 	int errors;
307facc8a22SMiao Xie 
308facc8a22SMiao Xie 	/* orig_bio is our btrfs_io_bio */
309facc8a22SMiao Xie 	struct bio *orig_bio;
310facc8a22SMiao Xie 
311facc8a22SMiao Xie 	/* dio_bio came from fs/direct-io.c */
312facc8a22SMiao Xie 	struct bio *dio_bio;
313c1dc0896SMiao Xie 
314c1dc0896SMiao Xie 	/*
31501327610SNicholas D Steeves 	 * The original bio may be split to several sub-bios, this is
316c1dc0896SMiao Xie 	 * done during endio of sub-bios
317c1dc0896SMiao Xie 	 */
3184e4cbee9SChristoph Hellwig 	blk_status_t (*subio_endio)(struct inode *, struct btrfs_io_bio *,
3194e4cbee9SChristoph Hellwig 			blk_status_t);
320facc8a22SMiao Xie };
321facc8a22SMiao Xie 
3222e60a51eSMiao Xie /*
3232e60a51eSMiao Xie  * Disable DIO read nolock optimization, so new dio readers will be forced
3242e60a51eSMiao Xie  * to grab i_mutex. It is used to avoid the endless truncate due to
3252e60a51eSMiao Xie  * nonlocked dio read.
3262e60a51eSMiao Xie  */
327abcefb1eSNikolay Borisov static inline void btrfs_inode_block_unlocked_dio(struct btrfs_inode *inode)
3282e60a51eSMiao Xie {
329abcefb1eSNikolay Borisov 	set_bit(BTRFS_INODE_READDIO_NEED_LOCK, &inode->runtime_flags);
3302e60a51eSMiao Xie 	smp_mb();
3312e60a51eSMiao Xie }
3322e60a51eSMiao Xie 
3330b581701SNikolay Borisov static inline void btrfs_inode_resume_unlocked_dio(struct btrfs_inode *inode)
3342e60a51eSMiao Xie {
3354e857c58SPeter Zijlstra 	smp_mb__before_atomic();
3360b581701SNikolay Borisov 	clear_bit(BTRFS_INODE_READDIO_NEED_LOCK, &inode->runtime_flags);
3372e60a51eSMiao Xie }
3382e60a51eSMiao Xie 
3390970a22eSNikolay Borisov static inline void btrfs_print_data_csum_error(struct btrfs_inode *inode,
3406f6b643eSQu Wenruo 		u64 logical_start, u32 csum, u32 csum_expected, int mirror_num)
3416f6b643eSQu Wenruo {
3420970a22eSNikolay Borisov 	struct btrfs_root *root = inode->root;
3436f6b643eSQu Wenruo 
3446f6b643eSQu Wenruo 	/* Output minus objectid, which is more meaningful */
3456f6b643eSQu Wenruo 	if (root->objectid >= BTRFS_LAST_FREE_OBJECTID)
3466f6b643eSQu Wenruo 		btrfs_warn_rl(root->fs_info,
3476f6b643eSQu Wenruo 	"csum failed root %lld ino %lld off %llu csum 0x%08x expected csum 0x%08x mirror %d",
3480970a22eSNikolay Borisov 			root->objectid, btrfs_ino(inode),
3496f6b643eSQu Wenruo 			logical_start, csum, csum_expected, mirror_num);
3506f6b643eSQu Wenruo 	else
3516f6b643eSQu Wenruo 		btrfs_warn_rl(root->fs_info,
3526f6b643eSQu Wenruo 	"csum failed root %llu ino %llu off %llu csum 0x%08x expected csum 0x%08x mirror %d",
3530970a22eSNikolay Borisov 			root->objectid, btrfs_ino(inode),
3546f6b643eSQu Wenruo 			logical_start, csum, csum_expected, mirror_num);
3556f6b643eSQu Wenruo }
3566f6b643eSQu Wenruo 
357fc4adbffSAlex Gartrell bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end);
358fc4adbffSAlex Gartrell 
3592c90e5d6SChris Mason #endif
360