xref: /openbmc/linux/fs/btrfs/btrfs_inode.h (revision 51a8cf9d2d97017d334f33f1b39067bd2f03bc49)
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 
2772ac3c0dSJosef Bacik /*
2872ac3c0dSJosef Bacik  * ordered_data_close is set by truncate when a file that used
2972ac3c0dSJosef Bacik  * to have good data has been truncated to zero.  When it is set
3072ac3c0dSJosef Bacik  * the btrfs file release call will add this inode to the
3172ac3c0dSJosef Bacik  * ordered operations list so that we make sure to flush out any
3272ac3c0dSJosef Bacik  * new data the application may have written before commit.
3372ac3c0dSJosef Bacik  */
3472ac3c0dSJosef Bacik #define BTRFS_INODE_ORDERED_DATA_CLOSE		0
3572ac3c0dSJosef Bacik #define BTRFS_INODE_ORPHAN_META_RESERVED	1
3672ac3c0dSJosef Bacik #define BTRFS_INODE_DUMMY			2
3772ac3c0dSJosef Bacik #define BTRFS_INODE_IN_DEFRAG			3
3872ac3c0dSJosef Bacik #define BTRFS_INODE_DELALLOC_META_RESERVED	4
398a35d95fSJosef Bacik #define BTRFS_INODE_HAS_ORPHAN_ITEM		5
407ddf5a42SJosef Bacik #define BTRFS_INODE_HAS_ASYNC_EXTENT		6
4172ac3c0dSJosef Bacik 
42f1ace244SAneesh /* in memory btrfs inode */
432c90e5d6SChris Mason struct btrfs_inode {
44d352ac68SChris Mason 	/* which subvolume this inode belongs to */
45d6e4a428SChris Mason 	struct btrfs_root *root;
46d352ac68SChris Mason 
47d352ac68SChris Mason 	/* key used to find this inode on disk.  This is used by the code
48d352ac68SChris Mason 	 * to read in roots of subvolumes
49d352ac68SChris Mason 	 */
50d6e4a428SChris Mason 	struct btrfs_key location;
51d352ac68SChris Mason 
529e0baf60SJosef Bacik 	/* Lock for counters */
539e0baf60SJosef Bacik 	spinlock_t lock;
549e0baf60SJosef Bacik 
55d352ac68SChris Mason 	/* the extent_tree has caches of all the extent mappings to disk */
56a52d9a80SChris Mason 	struct extent_map_tree extent_tree;
57d352ac68SChris Mason 
58d352ac68SChris Mason 	/* the io_tree does range state (DIRTY, LOCKED etc) */
59d1310b2eSChris Mason 	struct extent_io_tree io_tree;
60d352ac68SChris Mason 
61d352ac68SChris Mason 	/* special utility tree used to record which mirrors have already been
62d352ac68SChris Mason 	 * tried when checksums fail for a given block
63d352ac68SChris Mason 	 */
647e38326fSChris Mason 	struct extent_io_tree io_failure_tree;
65d352ac68SChris Mason 
66d352ac68SChris Mason 	/* held while logging the inode in tree-log.c */
67e02119d5SChris Mason 	struct mutex log_mutex;
68d352ac68SChris Mason 
69f248679eSJosef Bacik 	/* held while doing delalloc reservations */
70f248679eSJosef Bacik 	struct mutex delalloc_mutex;
71f248679eSJosef Bacik 
72d352ac68SChris Mason 	/* used to order data wrt metadata */
73e6dcd2dcSChris Mason 	struct btrfs_ordered_inode_tree ordered_tree;
7415ee9bc7SJosef Bacik 
75d352ac68SChris Mason 	/* list of all the delalloc inodes in the FS.  There are times we need
76d352ac68SChris Mason 	 * to write all the delalloc pages to disk, and this list is used
77d352ac68SChris Mason 	 * to walk them all.
78d352ac68SChris Mason 	 */
79ea8c2819SChris Mason 	struct list_head delalloc_inodes;
80ea8c2819SChris Mason 
815a3f23d5SChris Mason 	/*
825a3f23d5SChris Mason 	 * list for tracking inodes that must be sent to disk before a
835a3f23d5SChris Mason 	 * rename or truncate commit
845a3f23d5SChris Mason 	 */
855a3f23d5SChris Mason 	struct list_head ordered_operations;
865a3f23d5SChris Mason 
875d4f98a2SYan Zheng 	/* node for the red-black tree that links inodes in subvolume root */
885d4f98a2SYan Zheng 	struct rb_node rb_node;
895d4f98a2SYan Zheng 
906a63209fSJosef Bacik 	/* the space_info for where this inode's data allocations are done */
916a63209fSJosef Bacik 	struct btrfs_space_info *space_info;
926a63209fSJosef Bacik 
9372ac3c0dSJosef Bacik 	unsigned long runtime_flags;
9472ac3c0dSJosef Bacik 
95d352ac68SChris Mason 	/* full 64 bit generation number, struct vfs_inode doesn't have a big
96d352ac68SChris Mason 	 * enough field for this.
97d352ac68SChris Mason 	 */
98e02119d5SChris Mason 	u64 generation;
99e02119d5SChris Mason 
10015ee9bc7SJosef Bacik 	/*
10115ee9bc7SJosef Bacik 	 * transid of the trans_handle that last modified this inode
10215ee9bc7SJosef Bacik 	 */
10315ee9bc7SJosef Bacik 	u64 last_trans;
104257c62e1SChris Mason 
105257c62e1SChris Mason 	/*
106257c62e1SChris Mason 	 * log transid when this inode was last modified
107257c62e1SChris Mason 	 */
108257c62e1SChris Mason 	u64 last_sub_trans;
109257c62e1SChris Mason 
110e02119d5SChris Mason 	/*
111e02119d5SChris Mason 	 * transid that last logged this inode
112e02119d5SChris Mason 	 */
113e02119d5SChris Mason 	u64 logged_trans;
11449eb7e46SChris Mason 
115d352ac68SChris Mason 	/* total number of bytes pending delalloc, used by stat to calc the
116d352ac68SChris Mason 	 * real block usage of the file
117d352ac68SChris Mason 	 */
1189069218dSChris Mason 	u64 delalloc_bytes;
119d352ac68SChris Mason 
120d352ac68SChris Mason 	/*
121d352ac68SChris Mason 	 * the size of the file stored in the metadata on disk.  data=ordered
122d352ac68SChris Mason 	 * means the in-memory i_size might be larger than the size on disk
123d352ac68SChris Mason 	 * because not all the blocks are written yet.
124d352ac68SChris Mason 	 */
125dbe674a9SChris Mason 	u64 disk_i_size;
126d352ac68SChris Mason 
127aec7477bSJosef Bacik 	/*
128aec7477bSJosef Bacik 	 * if this is a directory then index_cnt is the counter for the index
129aec7477bSJosef Bacik 	 * number for new files that are created
130aec7477bSJosef Bacik 	 */
131aec7477bSJosef Bacik 	u64 index_cnt;
132d352ac68SChris Mason 
13312fcfd22SChris Mason 	/* the fsync log has some corner cases that mean we have to check
13412fcfd22SChris Mason 	 * directories to see if any unlinks have been done before
13512fcfd22SChris Mason 	 * the directory was logged.  See tree-log.c for all the
13612fcfd22SChris Mason 	 * details
13712fcfd22SChris Mason 	 */
13812fcfd22SChris Mason 	u64 last_unlink_trans;
13912fcfd22SChris Mason 
1407709cde3SJosef Bacik 	/*
1417709cde3SJosef Bacik 	 * Number of bytes outstanding that are going to need csums.  This is
1427709cde3SJosef Bacik 	 * used in ENOSPC accounting.
1437709cde3SJosef Bacik 	 */
1447709cde3SJosef Bacik 	u64 csum_bytes;
1457709cde3SJosef Bacik 
146f1bdcc0aSJosef Bacik 	/* flags field from the on disk inode */
147f1bdcc0aSJosef Bacik 	u32 flags;
148f1bdcc0aSJosef Bacik 
1495a3f23d5SChris Mason 	/*
15032c00affSJosef Bacik 	 * Counters to keep track of the number of extent item's we may use due
15132c00affSJosef Bacik 	 * to delalloc and such.  outstanding_extents is the number of extent
15232c00affSJosef Bacik 	 * items we think we'll end up using, and reserved_extents is the number
15332c00affSJosef Bacik 	 * of extent items we've reserved metadata for.
1549ed74f2dSJosef Bacik 	 */
1559e0baf60SJosef Bacik 	unsigned outstanding_extents;
1569e0baf60SJosef Bacik 	unsigned reserved_extents;
1579ed74f2dSJosef Bacik 
1589ed74f2dSJosef Bacik 	/*
1591e701a32SChris Mason 	 * always compress this one file
1601e701a32SChris Mason 	 */
16172ac3c0dSJosef Bacik 	unsigned force_compress;
1621e701a32SChris Mason 
16316cdcec7SMiao Xie 	struct btrfs_delayed_node *delayed_node;
16416cdcec7SMiao Xie 
165d352ac68SChris Mason 	struct inode vfs_inode;
1662c90e5d6SChris Mason };
167dbe674a9SChris Mason 
16816cdcec7SMiao Xie extern unsigned char btrfs_filetype_table[];
16916cdcec7SMiao Xie 
1702c90e5d6SChris Mason static inline struct btrfs_inode *BTRFS_I(struct inode *inode)
1712c90e5d6SChris Mason {
1722c90e5d6SChris Mason 	return container_of(inode, struct btrfs_inode, vfs_inode);
1732c90e5d6SChris Mason }
1742c90e5d6SChris Mason 
17533345d01SLi Zefan static inline u64 btrfs_ino(struct inode *inode)
17633345d01SLi Zefan {
17733345d01SLi Zefan 	u64 ino = BTRFS_I(inode)->location.objectid;
17833345d01SLi Zefan 
17914c7cca7SLiu Bo 	/*
18014c7cca7SLiu Bo 	 * !ino: btree_inode
18114c7cca7SLiu Bo 	 * type == BTRFS_ROOT_ITEM_KEY: subvol dir
18214c7cca7SLiu Bo 	 */
18314c7cca7SLiu Bo 	if (!ino || BTRFS_I(inode)->location.type == BTRFS_ROOT_ITEM_KEY)
18433345d01SLi Zefan 		ino = inode->i_ino;
18533345d01SLi Zefan 	return ino;
18633345d01SLi Zefan }
18733345d01SLi Zefan 
188dbe674a9SChris Mason static inline void btrfs_i_size_write(struct inode *inode, u64 size)
189dbe674a9SChris Mason {
190c2167754SYan, Zheng 	i_size_write(inode, size);
191dbe674a9SChris Mason 	BTRFS_I(inode)->disk_i_size = size;
192dbe674a9SChris Mason }
193dbe674a9SChris Mason 
1942cf8572dSChris Mason static inline bool btrfs_is_free_space_inode(struct btrfs_root *root,
1952cf8572dSChris Mason 				       struct inode *inode)
1962cf8572dSChris Mason {
197*51a8cf9dSLiu Bo 	if (root == root->fs_info->tree_root &&
198*51a8cf9dSLiu Bo 	    btrfs_ino(inode) != BTRFS_BTREE_INODE_OBJECTID)
199*51a8cf9dSLiu Bo 		return true;
200*51a8cf9dSLiu Bo 	if (BTRFS_I(inode)->location.objectid == BTRFS_FREE_INO_OBJECTID)
2012cf8572dSChris Mason 		return true;
2022cf8572dSChris Mason 	return false;
2032cf8572dSChris Mason }
2042cf8572dSChris Mason 
20522ee6985SJosef Bacik static inline int btrfs_inode_in_log(struct inode *inode, u64 generation)
20622ee6985SJosef Bacik {
20722ee6985SJosef Bacik 	struct btrfs_root *root = BTRFS_I(inode)->root;
20822ee6985SJosef Bacik 	int ret = 0;
20922ee6985SJosef Bacik 
21022ee6985SJosef Bacik 	mutex_lock(&root->log_mutex);
21122ee6985SJosef Bacik 	if (BTRFS_I(inode)->logged_trans == generation &&
21222ee6985SJosef Bacik 	    BTRFS_I(inode)->last_sub_trans <= root->last_log_commit)
21322ee6985SJosef Bacik 		ret = 1;
21422ee6985SJosef Bacik 	mutex_unlock(&root->log_mutex);
21522ee6985SJosef Bacik 	return ret;
21622ee6985SJosef Bacik }
21722ee6985SJosef Bacik 
2182c90e5d6SChris Mason #endif
219