xref: /openbmc/linux/fs/btrfs/btrfs_inode.h (revision 8089fe62c6603860f6796ca80519b92391292f21)
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
47656f30dbSFilipe Manana /*
48656f30dbSFilipe Manana  * The following 3 bits are meant only for the btree inode.
49656f30dbSFilipe Manana  * When any of them is set, it means an error happened while writing an
50656f30dbSFilipe Manana  * extent buffer belonging to:
51656f30dbSFilipe Manana  * 1) a non-log btree
52656f30dbSFilipe Manana  * 2) a log btree and first log sub-transaction
53656f30dbSFilipe Manana  * 3) a log btree and second log sub-transaction
54656f30dbSFilipe Manana  */
55656f30dbSFilipe Manana #define BTRFS_INODE_BTREE_ERR		        12
56656f30dbSFilipe Manana #define BTRFS_INODE_BTREE_LOG1_ERR		13
57656f30dbSFilipe 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 
692f2ff0eeSFilipe Manana 	/*
702f2ff0eeSFilipe Manana 	 * Lock for counters and all fields used to determine if the inode is in
712f2ff0eeSFilipe Manana 	 * the log or not (last_trans, last_sub_trans, last_log_commit,
722f2ff0eeSFilipe Manana 	 * logged_trans).
732f2ff0eeSFilipe Manana 	 */
749e0baf60SJosef Bacik 	spinlock_t lock;
759e0baf60SJosef Bacik 
76d352ac68SChris Mason 	/* the extent_tree has caches of all the extent mappings to disk */
77a52d9a80SChris Mason 	struct extent_map_tree extent_tree;
78d352ac68SChris Mason 
79d352ac68SChris Mason 	/* the io_tree does range state (DIRTY, LOCKED etc) */
80d1310b2eSChris Mason 	struct extent_io_tree io_tree;
81d352ac68SChris Mason 
82d352ac68SChris Mason 	/* special utility tree used to record which mirrors have already been
83d352ac68SChris Mason 	 * tried when checksums fail for a given block
84d352ac68SChris Mason 	 */
857e38326fSChris Mason 	struct extent_io_tree io_failure_tree;
86d352ac68SChris Mason 
87d352ac68SChris Mason 	/* held while logging the inode in tree-log.c */
88e02119d5SChris Mason 	struct mutex log_mutex;
89d352ac68SChris Mason 
90f248679eSJosef Bacik 	/* held while doing delalloc reservations */
91f248679eSJosef Bacik 	struct mutex delalloc_mutex;
92f248679eSJosef Bacik 
93d352ac68SChris Mason 	/* used to order data wrt metadata */
94e6dcd2dcSChris Mason 	struct btrfs_ordered_inode_tree ordered_tree;
9515ee9bc7SJosef Bacik 
96d352ac68SChris Mason 	/* list of all the delalloc inodes in the FS.  There are times we need
97d352ac68SChris Mason 	 * to write all the delalloc pages to disk, and this list is used
98d352ac68SChris Mason 	 * to walk them all.
99d352ac68SChris Mason 	 */
100ea8c2819SChris Mason 	struct list_head delalloc_inodes;
101ea8c2819SChris Mason 
1025d4f98a2SYan Zheng 	/* node for the red-black tree that links inodes in subvolume root */
1035d4f98a2SYan Zheng 	struct rb_node rb_node;
1045d4f98a2SYan Zheng 
10572ac3c0dSJosef Bacik 	unsigned long runtime_flags;
10672ac3c0dSJosef Bacik 
1079c931c5aSNathaniel Yazdani 	/* Keep track of who's O_SYNC/fsyncing currently */
108b812ce28SJosef Bacik 	atomic_t sync_writers;
109b812ce28SJosef Bacik 
110d352ac68SChris Mason 	/* full 64 bit generation number, struct vfs_inode doesn't have a big
111d352ac68SChris Mason 	 * enough field for this.
112d352ac68SChris Mason 	 */
113e02119d5SChris Mason 	u64 generation;
114e02119d5SChris Mason 
11515ee9bc7SJosef Bacik 	/*
11615ee9bc7SJosef Bacik 	 * transid of the trans_handle that last modified this inode
11715ee9bc7SJosef Bacik 	 */
11815ee9bc7SJosef Bacik 	u64 last_trans;
119257c62e1SChris Mason 
120257c62e1SChris Mason 	/*
121e02119d5SChris Mason 	 * transid that last logged this inode
122e02119d5SChris Mason 	 */
123e02119d5SChris Mason 	u64 logged_trans;
12449eb7e46SChris Mason 
125bb14a59bSMiao Xie 	/*
126bb14a59bSMiao Xie 	 * log transid when this inode was last modified
127bb14a59bSMiao Xie 	 */
128bb14a59bSMiao Xie 	int last_sub_trans;
129bb14a59bSMiao Xie 
130bb14a59bSMiao Xie 	/* a local copy of root's last_log_commit */
131bb14a59bSMiao Xie 	int last_log_commit;
132bb14a59bSMiao Xie 
133d352ac68SChris Mason 	/* total number of bytes pending delalloc, used by stat to calc the
134d352ac68SChris Mason 	 * real block usage of the file
135d352ac68SChris Mason 	 */
1369069218dSChris Mason 	u64 delalloc_bytes;
137d352ac68SChris Mason 
138d352ac68SChris Mason 	/*
13947059d93SWang Shilong 	 * total number of bytes pending defrag, used by stat to check whether
14047059d93SWang Shilong 	 * it needs COW.
14147059d93SWang Shilong 	 */
14247059d93SWang Shilong 	u64 defrag_bytes;
14347059d93SWang Shilong 
14447059d93SWang Shilong 	/*
145d352ac68SChris Mason 	 * the size of the file stored in the metadata on disk.  data=ordered
146d352ac68SChris Mason 	 * means the in-memory i_size might be larger than the size on disk
147d352ac68SChris Mason 	 * because not all the blocks are written yet.
148d352ac68SChris Mason 	 */
149dbe674a9SChris Mason 	u64 disk_i_size;
150d352ac68SChris Mason 
151aec7477bSJosef Bacik 	/*
152aec7477bSJosef Bacik 	 * if this is a directory then index_cnt is the counter for the index
153aec7477bSJosef Bacik 	 * number for new files that are created
154aec7477bSJosef Bacik 	 */
155aec7477bSJosef Bacik 	u64 index_cnt;
156d352ac68SChris Mason 
15767de1176SMiao Xie 	/* Cache the directory index number to speed the dir/file remove */
15867de1176SMiao Xie 	u64 dir_index;
15967de1176SMiao Xie 
16012fcfd22SChris Mason 	/* the fsync log has some corner cases that mean we have to check
16112fcfd22SChris Mason 	 * directories to see if any unlinks have been done before
16212fcfd22SChris Mason 	 * the directory was logged.  See tree-log.c for all the
16312fcfd22SChris Mason 	 * details
16412fcfd22SChris Mason 	 */
16512fcfd22SChris Mason 	u64 last_unlink_trans;
16612fcfd22SChris Mason 
1677709cde3SJosef Bacik 	/*
1687709cde3SJosef Bacik 	 * Number of bytes outstanding that are going to need csums.  This is
1697709cde3SJosef Bacik 	 * used in ENOSPC accounting.
1707709cde3SJosef Bacik 	 */
1717709cde3SJosef Bacik 	u64 csum_bytes;
1727709cde3SJosef Bacik 
173f1bdcc0aSJosef Bacik 	/* flags field from the on disk inode */
174f1bdcc0aSJosef Bacik 	u32 flags;
175f1bdcc0aSJosef Bacik 
1765a3f23d5SChris Mason 	/*
17732c00affSJosef Bacik 	 * Counters to keep track of the number of extent item's we may use due
17832c00affSJosef Bacik 	 * to delalloc and such.  outstanding_extents is the number of extent
17932c00affSJosef Bacik 	 * items we think we'll end up using, and reserved_extents is the number
18032c00affSJosef Bacik 	 * of extent items we've reserved metadata for.
1819ed74f2dSJosef Bacik 	 */
1829e0baf60SJosef Bacik 	unsigned outstanding_extents;
1839e0baf60SJosef Bacik 	unsigned reserved_extents;
1849ed74f2dSJosef Bacik 
1859ed74f2dSJosef Bacik 	/*
1861e701a32SChris Mason 	 * always compress this one file
1871e701a32SChris Mason 	 */
18872ac3c0dSJosef Bacik 	unsigned force_compress;
1891e701a32SChris Mason 
19016cdcec7SMiao Xie 	struct btrfs_delayed_node *delayed_node;
19116cdcec7SMiao Xie 
1929cc97d64Schandan r 	/* File creation time. */
1939cc97d64Schandan r 	struct timespec i_otime;
1949cc97d64Schandan r 
195*8089fe62SDavid Sterba 	/* Hook into fs_info->delayed_iputs */
196*8089fe62SDavid Sterba 	struct list_head delayed_iput;
197*8089fe62SDavid Sterba 	long delayed_iput_count;
198*8089fe62SDavid Sterba 
199d352ac68SChris Mason 	struct inode vfs_inode;
2002c90e5d6SChris Mason };
201dbe674a9SChris Mason 
20216cdcec7SMiao Xie extern unsigned char btrfs_filetype_table[];
20316cdcec7SMiao Xie 
2042c90e5d6SChris Mason static inline struct btrfs_inode *BTRFS_I(struct inode *inode)
2052c90e5d6SChris Mason {
2062c90e5d6SChris Mason 	return container_of(inode, struct btrfs_inode, vfs_inode);
2072c90e5d6SChris Mason }
2082c90e5d6SChris Mason 
209778ba82bSFilipe David Borba Manana static inline unsigned long btrfs_inode_hash(u64 objectid,
210778ba82bSFilipe David Borba Manana 					     const struct btrfs_root *root)
211778ba82bSFilipe David Borba Manana {
212778ba82bSFilipe David Borba Manana 	u64 h = objectid ^ (root->objectid * GOLDEN_RATIO_PRIME);
213778ba82bSFilipe David Borba Manana 
214778ba82bSFilipe David Borba Manana #if BITS_PER_LONG == 32
215778ba82bSFilipe David Borba Manana 	h = (h >> 32) ^ (h & 0xffffffff);
216778ba82bSFilipe David Borba Manana #endif
217778ba82bSFilipe David Borba Manana 
218778ba82bSFilipe David Borba Manana 	return (unsigned long)h;
219778ba82bSFilipe David Borba Manana }
220778ba82bSFilipe David Borba Manana 
221778ba82bSFilipe David Borba Manana static inline void btrfs_insert_inode_hash(struct inode *inode)
222778ba82bSFilipe David Borba Manana {
223778ba82bSFilipe David Borba Manana 	unsigned long h = btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root);
224778ba82bSFilipe David Borba Manana 
225778ba82bSFilipe David Borba Manana 	__insert_inode_hash(inode, h);
226778ba82bSFilipe David Borba Manana }
227778ba82bSFilipe David Borba Manana 
22833345d01SLi Zefan static inline u64 btrfs_ino(struct inode *inode)
22933345d01SLi Zefan {
23033345d01SLi Zefan 	u64 ino = BTRFS_I(inode)->location.objectid;
23133345d01SLi Zefan 
23214c7cca7SLiu Bo 	/*
23314c7cca7SLiu Bo 	 * !ino: btree_inode
23414c7cca7SLiu Bo 	 * type == BTRFS_ROOT_ITEM_KEY: subvol dir
23514c7cca7SLiu Bo 	 */
23614c7cca7SLiu Bo 	if (!ino || BTRFS_I(inode)->location.type == BTRFS_ROOT_ITEM_KEY)
23733345d01SLi Zefan 		ino = inode->i_ino;
23833345d01SLi Zefan 	return ino;
23933345d01SLi Zefan }
24033345d01SLi Zefan 
241dbe674a9SChris Mason static inline void btrfs_i_size_write(struct inode *inode, u64 size)
242dbe674a9SChris Mason {
243c2167754SYan, Zheng 	i_size_write(inode, size);
244dbe674a9SChris Mason 	BTRFS_I(inode)->disk_i_size = size;
245dbe674a9SChris Mason }
246dbe674a9SChris Mason 
24783eea1f1SLiu Bo static inline bool btrfs_is_free_space_inode(struct inode *inode)
2482cf8572dSChris Mason {
24983eea1f1SLiu Bo 	struct btrfs_root *root = BTRFS_I(inode)->root;
25083eea1f1SLiu Bo 
25151a8cf9dSLiu Bo 	if (root == root->fs_info->tree_root &&
25251a8cf9dSLiu Bo 	    btrfs_ino(inode) != BTRFS_BTREE_INODE_OBJECTID)
25351a8cf9dSLiu Bo 		return true;
25451a8cf9dSLiu Bo 	if (BTRFS_I(inode)->location.objectid == BTRFS_FREE_INO_OBJECTID)
2552cf8572dSChris Mason 		return true;
2562cf8572dSChris Mason 	return false;
2572cf8572dSChris Mason }
2582cf8572dSChris Mason 
25922ee6985SJosef Bacik static inline int btrfs_inode_in_log(struct inode *inode, u64 generation)
26022ee6985SJosef Bacik {
2612f2ff0eeSFilipe Manana 	int ret = 0;
2622f2ff0eeSFilipe Manana 
2632f2ff0eeSFilipe Manana 	spin_lock(&BTRFS_I(inode)->lock);
26422ee6985SJosef Bacik 	if (BTRFS_I(inode)->logged_trans == generation &&
265a5874ce6SJosef Bacik 	    BTRFS_I(inode)->last_sub_trans <=
266a5874ce6SJosef Bacik 	    BTRFS_I(inode)->last_log_commit &&
267a5874ce6SJosef Bacik 	    BTRFS_I(inode)->last_sub_trans <=
268125c4cf9SFilipe Manana 	    BTRFS_I(inode)->root->last_log_commit) {
269125c4cf9SFilipe Manana 		/*
270125c4cf9SFilipe Manana 		 * After a ranged fsync we might have left some extent maps
271125c4cf9SFilipe Manana 		 * (that fall outside the fsync's range). So return false
272125c4cf9SFilipe Manana 		 * here if the list isn't empty, to make sure btrfs_log_inode()
273125c4cf9SFilipe Manana 		 * will be called and process those extent maps.
274125c4cf9SFilipe Manana 		 */
275125c4cf9SFilipe Manana 		smp_mb();
276125c4cf9SFilipe Manana 		if (list_empty(&BTRFS_I(inode)->extent_tree.modified_extents))
2772f2ff0eeSFilipe Manana 			ret = 1;
278125c4cf9SFilipe Manana 	}
2792f2ff0eeSFilipe Manana 	spin_unlock(&BTRFS_I(inode)->lock);
2802f2ff0eeSFilipe Manana 	return ret;
28122ee6985SJosef Bacik }
28222ee6985SJosef Bacik 
283c1dc0896SMiao Xie #define BTRFS_DIO_ORIG_BIO_SUBMITTED	0x1
284c1dc0896SMiao Xie 
285facc8a22SMiao Xie struct btrfs_dio_private {
286facc8a22SMiao Xie 	struct inode *inode;
287c1dc0896SMiao Xie 	unsigned long flags;
288facc8a22SMiao Xie 	u64 logical_offset;
289facc8a22SMiao Xie 	u64 disk_bytenr;
290facc8a22SMiao Xie 	u64 bytes;
291facc8a22SMiao Xie 	void *private;
292facc8a22SMiao Xie 
293facc8a22SMiao Xie 	/* number of bios pending for this dio */
294facc8a22SMiao Xie 	atomic_t pending_bios;
295facc8a22SMiao Xie 
296facc8a22SMiao Xie 	/* IO errors */
297facc8a22SMiao Xie 	int errors;
298facc8a22SMiao Xie 
299facc8a22SMiao Xie 	/* orig_bio is our btrfs_io_bio */
300facc8a22SMiao Xie 	struct bio *orig_bio;
301facc8a22SMiao Xie 
302facc8a22SMiao Xie 	/* dio_bio came from fs/direct-io.c */
303facc8a22SMiao Xie 	struct bio *dio_bio;
304c1dc0896SMiao Xie 
305c1dc0896SMiao Xie 	/*
306c1dc0896SMiao Xie 	 * The original bio may be splited to several sub-bios, this is
307c1dc0896SMiao Xie 	 * done during endio of sub-bios
308c1dc0896SMiao Xie 	 */
3098b110e39SMiao Xie 	int (*subio_endio)(struct inode *, struct btrfs_io_bio *, int);
310facc8a22SMiao Xie };
311facc8a22SMiao Xie 
3122e60a51eSMiao Xie /*
3132e60a51eSMiao Xie  * Disable DIO read nolock optimization, so new dio readers will be forced
3142e60a51eSMiao Xie  * to grab i_mutex. It is used to avoid the endless truncate due to
3152e60a51eSMiao Xie  * nonlocked dio read.
3162e60a51eSMiao Xie  */
3172e60a51eSMiao Xie static inline void btrfs_inode_block_unlocked_dio(struct inode *inode)
3182e60a51eSMiao Xie {
3192e60a51eSMiao Xie 	set_bit(BTRFS_INODE_READDIO_NEED_LOCK, &BTRFS_I(inode)->runtime_flags);
3202e60a51eSMiao Xie 	smp_mb();
3212e60a51eSMiao Xie }
3222e60a51eSMiao Xie 
3232e60a51eSMiao Xie static inline void btrfs_inode_resume_unlocked_dio(struct inode *inode)
3242e60a51eSMiao Xie {
3254e857c58SPeter Zijlstra 	smp_mb__before_atomic();
3262e60a51eSMiao Xie 	clear_bit(BTRFS_INODE_READDIO_NEED_LOCK,
3272e60a51eSMiao Xie 		  &BTRFS_I(inode)->runtime_flags);
3282e60a51eSMiao Xie }
3292e60a51eSMiao Xie 
330fc4adbffSAlex Gartrell bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end);
331fc4adbffSAlex Gartrell 
3322c90e5d6SChris Mason #endif
333