xref: /openbmc/linux/fs/btrfs/btrfs_inode.h (revision 656f30dba7ab8179c9a2e04293b0c7b383fa9ce9)
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
47*656f30dbSFilipe Manana /*
48*656f30dbSFilipe Manana  * The following 3 bits are meant only for the btree inode.
49*656f30dbSFilipe Manana  * When any of them is set, it means an error happened while writing an
50*656f30dbSFilipe Manana  * extent buffer belonging to:
51*656f30dbSFilipe Manana  * 1) a non-log btree
52*656f30dbSFilipe Manana  * 2) a log btree and first log sub-transaction
53*656f30dbSFilipe Manana  * 3) a log btree and second log sub-transaction
54*656f30dbSFilipe Manana  */
55*656f30dbSFilipe Manana #define BTRFS_INODE_BTREE_ERR		        12
56*656f30dbSFilipe Manana #define BTRFS_INODE_BTREE_LOG1_ERR		13
57*656f30dbSFilipe Manana #define BTRFS_INODE_BTREE_LOG2_ERR		14
5872ac3c0dSJosef Bacik 
59f1ace244SAneesh /* in memory btrfs inode */
602c90e5d6SChris Mason struct btrfs_inode {
61d352ac68SChris Mason 	/* which subvolume this inode belongs to */
62d6e4a428SChris Mason 	struct btrfs_root *root;
63d352ac68SChris Mason 
64d352ac68SChris Mason 	/* key used to find this inode on disk.  This is used by the code
65d352ac68SChris Mason 	 * to read in roots of subvolumes
66d352ac68SChris Mason 	 */
67d6e4a428SChris Mason 	struct btrfs_key location;
68d352ac68SChris Mason 
699e0baf60SJosef Bacik 	/* Lock for counters */
709e0baf60SJosef Bacik 	spinlock_t lock;
719e0baf60SJosef Bacik 
72d352ac68SChris Mason 	/* the extent_tree has caches of all the extent mappings to disk */
73a52d9a80SChris Mason 	struct extent_map_tree extent_tree;
74d352ac68SChris Mason 
75d352ac68SChris Mason 	/* the io_tree does range state (DIRTY, LOCKED etc) */
76d1310b2eSChris Mason 	struct extent_io_tree io_tree;
77d352ac68SChris Mason 
78d352ac68SChris Mason 	/* special utility tree used to record which mirrors have already been
79d352ac68SChris Mason 	 * tried when checksums fail for a given block
80d352ac68SChris Mason 	 */
817e38326fSChris Mason 	struct extent_io_tree io_failure_tree;
82d352ac68SChris Mason 
83d352ac68SChris Mason 	/* held while logging the inode in tree-log.c */
84e02119d5SChris Mason 	struct mutex log_mutex;
85d352ac68SChris Mason 
86f248679eSJosef Bacik 	/* held while doing delalloc reservations */
87f248679eSJosef Bacik 	struct mutex delalloc_mutex;
88f248679eSJosef Bacik 
89d352ac68SChris Mason 	/* used to order data wrt metadata */
90e6dcd2dcSChris Mason 	struct btrfs_ordered_inode_tree ordered_tree;
9115ee9bc7SJosef Bacik 
92d352ac68SChris Mason 	/* list of all the delalloc inodes in the FS.  There are times we need
93d352ac68SChris Mason 	 * to write all the delalloc pages to disk, and this list is used
94d352ac68SChris Mason 	 * to walk them all.
95d352ac68SChris Mason 	 */
96ea8c2819SChris Mason 	struct list_head delalloc_inodes;
97ea8c2819SChris Mason 
985d4f98a2SYan Zheng 	/* node for the red-black tree that links inodes in subvolume root */
995d4f98a2SYan Zheng 	struct rb_node rb_node;
1005d4f98a2SYan Zheng 
10172ac3c0dSJosef Bacik 	unsigned long runtime_flags;
10272ac3c0dSJosef Bacik 
1039c931c5aSNathaniel Yazdani 	/* Keep track of who's O_SYNC/fsyncing currently */
104b812ce28SJosef Bacik 	atomic_t sync_writers;
105b812ce28SJosef Bacik 
106d352ac68SChris Mason 	/* full 64 bit generation number, struct vfs_inode doesn't have a big
107d352ac68SChris Mason 	 * enough field for this.
108d352ac68SChris Mason 	 */
109e02119d5SChris Mason 	u64 generation;
110e02119d5SChris Mason 
11115ee9bc7SJosef Bacik 	/*
11215ee9bc7SJosef Bacik 	 * transid of the trans_handle that last modified this inode
11315ee9bc7SJosef Bacik 	 */
11415ee9bc7SJosef Bacik 	u64 last_trans;
115257c62e1SChris Mason 
116257c62e1SChris Mason 	/*
117e02119d5SChris Mason 	 * transid that last logged this inode
118e02119d5SChris Mason 	 */
119e02119d5SChris Mason 	u64 logged_trans;
12049eb7e46SChris Mason 
121bb14a59bSMiao Xie 	/*
122bb14a59bSMiao Xie 	 * log transid when this inode was last modified
123bb14a59bSMiao Xie 	 */
124bb14a59bSMiao Xie 	int last_sub_trans;
125bb14a59bSMiao Xie 
126bb14a59bSMiao Xie 	/* a local copy of root's last_log_commit */
127bb14a59bSMiao Xie 	int last_log_commit;
128bb14a59bSMiao Xie 
129d352ac68SChris Mason 	/* total number of bytes pending delalloc, used by stat to calc the
130d352ac68SChris Mason 	 * real block usage of the file
131d352ac68SChris Mason 	 */
1329069218dSChris Mason 	u64 delalloc_bytes;
133d352ac68SChris Mason 
134d352ac68SChris Mason 	/*
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 	/*
1821e701a32SChris Mason 	 * always compress this one file
1831e701a32SChris Mason 	 */
18472ac3c0dSJosef Bacik 	unsigned force_compress;
1851e701a32SChris Mason 
18616cdcec7SMiao Xie 	struct btrfs_delayed_node *delayed_node;
18716cdcec7SMiao Xie 
188d352ac68SChris Mason 	struct inode vfs_inode;
1892c90e5d6SChris Mason };
190dbe674a9SChris Mason 
19116cdcec7SMiao Xie extern unsigned char btrfs_filetype_table[];
19216cdcec7SMiao Xie 
1932c90e5d6SChris Mason static inline struct btrfs_inode *BTRFS_I(struct inode *inode)
1942c90e5d6SChris Mason {
1952c90e5d6SChris Mason 	return container_of(inode, struct btrfs_inode, vfs_inode);
1962c90e5d6SChris Mason }
1972c90e5d6SChris Mason 
198778ba82bSFilipe David Borba Manana static inline unsigned long btrfs_inode_hash(u64 objectid,
199778ba82bSFilipe David Borba Manana 					     const struct btrfs_root *root)
200778ba82bSFilipe David Borba Manana {
201778ba82bSFilipe David Borba Manana 	u64 h = objectid ^ (root->objectid * GOLDEN_RATIO_PRIME);
202778ba82bSFilipe David Borba Manana 
203778ba82bSFilipe David Borba Manana #if BITS_PER_LONG == 32
204778ba82bSFilipe David Borba Manana 	h = (h >> 32) ^ (h & 0xffffffff);
205778ba82bSFilipe David Borba Manana #endif
206778ba82bSFilipe David Borba Manana 
207778ba82bSFilipe David Borba Manana 	return (unsigned long)h;
208778ba82bSFilipe David Borba Manana }
209778ba82bSFilipe David Borba Manana 
210778ba82bSFilipe David Borba Manana static inline void btrfs_insert_inode_hash(struct inode *inode)
211778ba82bSFilipe David Borba Manana {
212778ba82bSFilipe David Borba Manana 	unsigned long h = btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root);
213778ba82bSFilipe David Borba Manana 
214778ba82bSFilipe David Borba Manana 	__insert_inode_hash(inode, h);
215778ba82bSFilipe David Borba Manana }
216778ba82bSFilipe David Borba Manana 
21733345d01SLi Zefan static inline u64 btrfs_ino(struct inode *inode)
21833345d01SLi Zefan {
21933345d01SLi Zefan 	u64 ino = BTRFS_I(inode)->location.objectid;
22033345d01SLi Zefan 
22114c7cca7SLiu Bo 	/*
22214c7cca7SLiu Bo 	 * !ino: btree_inode
22314c7cca7SLiu Bo 	 * type == BTRFS_ROOT_ITEM_KEY: subvol dir
22414c7cca7SLiu Bo 	 */
22514c7cca7SLiu Bo 	if (!ino || BTRFS_I(inode)->location.type == BTRFS_ROOT_ITEM_KEY)
22633345d01SLi Zefan 		ino = inode->i_ino;
22733345d01SLi Zefan 	return ino;
22833345d01SLi Zefan }
22933345d01SLi Zefan 
230dbe674a9SChris Mason static inline void btrfs_i_size_write(struct inode *inode, u64 size)
231dbe674a9SChris Mason {
232c2167754SYan, Zheng 	i_size_write(inode, size);
233dbe674a9SChris Mason 	BTRFS_I(inode)->disk_i_size = size;
234dbe674a9SChris Mason }
235dbe674a9SChris Mason 
23683eea1f1SLiu Bo static inline bool btrfs_is_free_space_inode(struct inode *inode)
2372cf8572dSChris Mason {
23883eea1f1SLiu Bo 	struct btrfs_root *root = BTRFS_I(inode)->root;
23983eea1f1SLiu Bo 
24051a8cf9dSLiu Bo 	if (root == root->fs_info->tree_root &&
24151a8cf9dSLiu Bo 	    btrfs_ino(inode) != BTRFS_BTREE_INODE_OBJECTID)
24251a8cf9dSLiu Bo 		return true;
24351a8cf9dSLiu Bo 	if (BTRFS_I(inode)->location.objectid == BTRFS_FREE_INO_OBJECTID)
2442cf8572dSChris Mason 		return true;
2452cf8572dSChris Mason 	return false;
2462cf8572dSChris Mason }
2472cf8572dSChris Mason 
24822ee6985SJosef Bacik static inline int btrfs_inode_in_log(struct inode *inode, u64 generation)
24922ee6985SJosef Bacik {
25022ee6985SJosef Bacik 	if (BTRFS_I(inode)->logged_trans == generation &&
251a5874ce6SJosef Bacik 	    BTRFS_I(inode)->last_sub_trans <=
252a5874ce6SJosef Bacik 	    BTRFS_I(inode)->last_log_commit &&
253a5874ce6SJosef Bacik 	    BTRFS_I(inode)->last_sub_trans <=
254a5874ce6SJosef Bacik 	    BTRFS_I(inode)->root->last_log_commit)
25546d8bc34SLiu Bo 		return 1;
25646d8bc34SLiu Bo 	return 0;
25722ee6985SJosef Bacik }
25822ee6985SJosef Bacik 
259c1dc0896SMiao Xie #define BTRFS_DIO_ORIG_BIO_SUBMITTED	0x1
260c1dc0896SMiao Xie 
261facc8a22SMiao Xie struct btrfs_dio_private {
262facc8a22SMiao Xie 	struct inode *inode;
263c1dc0896SMiao Xie 	unsigned long flags;
264facc8a22SMiao Xie 	u64 logical_offset;
265facc8a22SMiao Xie 	u64 disk_bytenr;
266facc8a22SMiao Xie 	u64 bytes;
267facc8a22SMiao Xie 	void *private;
268facc8a22SMiao Xie 
269facc8a22SMiao Xie 	/* number of bios pending for this dio */
270facc8a22SMiao Xie 	atomic_t pending_bios;
271facc8a22SMiao Xie 
272facc8a22SMiao Xie 	/* IO errors */
273facc8a22SMiao Xie 	int errors;
274facc8a22SMiao Xie 
275facc8a22SMiao Xie 	/* orig_bio is our btrfs_io_bio */
276facc8a22SMiao Xie 	struct bio *orig_bio;
277facc8a22SMiao Xie 
278facc8a22SMiao Xie 	/* dio_bio came from fs/direct-io.c */
279facc8a22SMiao Xie 	struct bio *dio_bio;
280c1dc0896SMiao Xie 
281c1dc0896SMiao Xie 	/*
282c1dc0896SMiao Xie 	 * The original bio may be splited to several sub-bios, this is
283c1dc0896SMiao Xie 	 * done during endio of sub-bios
284c1dc0896SMiao Xie 	 */
2858b110e39SMiao Xie 	int (*subio_endio)(struct inode *, struct btrfs_io_bio *, int);
286facc8a22SMiao Xie };
287facc8a22SMiao Xie 
2882e60a51eSMiao Xie /*
2892e60a51eSMiao Xie  * Disable DIO read nolock optimization, so new dio readers will be forced
2902e60a51eSMiao Xie  * to grab i_mutex. It is used to avoid the endless truncate due to
2912e60a51eSMiao Xie  * nonlocked dio read.
2922e60a51eSMiao Xie  */
2932e60a51eSMiao Xie static inline void btrfs_inode_block_unlocked_dio(struct inode *inode)
2942e60a51eSMiao Xie {
2952e60a51eSMiao Xie 	set_bit(BTRFS_INODE_READDIO_NEED_LOCK, &BTRFS_I(inode)->runtime_flags);
2962e60a51eSMiao Xie 	smp_mb();
2972e60a51eSMiao Xie }
2982e60a51eSMiao Xie 
2992e60a51eSMiao Xie static inline void btrfs_inode_resume_unlocked_dio(struct inode *inode)
3002e60a51eSMiao Xie {
3014e857c58SPeter Zijlstra 	smp_mb__before_atomic();
3022e60a51eSMiao Xie 	clear_bit(BTRFS_INODE_READDIO_NEED_LOCK,
3032e60a51eSMiao Xie 		  &BTRFS_I(inode)->runtime_flags);
3042e60a51eSMiao Xie }
3052e60a51eSMiao Xie 
306fc4adbffSAlex Gartrell bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end);
307fc4adbffSAlex Gartrell 
3082c90e5d6SChris Mason #endif
309