Blame view

fs/btrfs/btrfs_inode.h 9.97 KB
6cbd55707   Chris Mason   Btrfs: add GPLv2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  /*
   * Copyright (C) 2007 Oracle.  All rights reserved.
   *
   * This program is free software; you can redistribute it and/or
   * modify it under the terms of the GNU General Public
   * License v2 as published by the Free Software Foundation.
   *
   * This program is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   * General Public License for more details.
   *
   * You should have received a copy of the GNU General Public
   * License along with this program; if not, write to the
   * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   * Boston, MA 021110-1307, USA.
   */
2c90e5d65   Chris Mason   Btrfs: still corr...
18
19
  #ifndef __BTRFS_I__
  #define __BTRFS_I__
778ba82b1   Filipe David Borba Manana   Btrfs: improve in...
20
  #include <linux/hash.h>
a52d9a803   Chris Mason   Btrfs: Extent bas...
21
  #include "extent_map.h"
d1310b2e0   Chris Mason   Btrfs: Split the ...
22
  #include "extent_io.h"
e6dcd2dc9   Chris Mason   Btrfs: New data=o...
23
  #include "ordered-data.h"
16cdcec73   Miao Xie   btrfs: implement ...
24
  #include "delayed-inode.h"
a52d9a803   Chris Mason   Btrfs: Extent bas...
25

72ac3c0d7   Josef Bacik   Btrfs: convert th...
26
27
28
29
30
31
32
33
34
35
36
37
  /*
   * ordered_data_close is set by truncate when a file that used
   * to have good data has been truncated to zero.  When it is set
   * the btrfs file release call will add this inode to the
   * ordered operations list so that we make sure to flush out any
   * new data the application may have written before commit.
   */
  #define BTRFS_INODE_ORDERED_DATA_CLOSE		0
  #define BTRFS_INODE_ORPHAN_META_RESERVED	1
  #define BTRFS_INODE_DUMMY			2
  #define BTRFS_INODE_IN_DEFRAG			3
  #define BTRFS_INODE_DELALLOC_META_RESERVED	4
8a35d95ff   Josef Bacik   Btrfs: fix how we...
38
  #define BTRFS_INODE_HAS_ORPHAN_ITEM		5
7ddf5a42d   Josef Bacik   Btrfs: call filem...
39
  #define BTRFS_INODE_HAS_ASYNC_EXTENT		6
5dc562c54   Josef Bacik   Btrfs: turbo char...
40
  #define BTRFS_INODE_NEEDS_FULL_SYNC		7
e99761514   Josef Bacik   Btrfs: only log t...
41
  #define BTRFS_INODE_COPY_EVERYTHING		8
df0af1a57   Miao Xie   Btrfs: use the in...
42
  #define BTRFS_INODE_IN_DELALLOC_LIST		9
2e60a51e6   Miao Xie   Btrfs: serialize ...
43
  #define BTRFS_INODE_READDIO_NEED_LOCK		10
63541927c   Filipe David Borba Manana   Btrfs: add suppor...
44
  #define BTRFS_INODE_HAS_PROPS		        11
72ac3c0d7   Josef Bacik   Btrfs: convert th...
45

f1ace244c   Aneesh   btrfs: Code cleanup
46
  /* in memory btrfs inode */
2c90e5d65   Chris Mason   Btrfs: still corr...
47
  struct btrfs_inode {
d352ac681   Chris Mason   Btrfs: add and im...
48
  	/* which subvolume this inode belongs to */
d6e4a428e   Chris Mason   Btrfs: start of s...
49
  	struct btrfs_root *root;
d352ac681   Chris Mason   Btrfs: add and im...
50

d352ac681   Chris Mason   Btrfs: add and im...
51
52
53
  	/* key used to find this inode on disk.  This is used by the code
  	 * to read in roots of subvolumes
  	 */
d6e4a428e   Chris Mason   Btrfs: start of s...
54
  	struct btrfs_key location;
d352ac681   Chris Mason   Btrfs: add and im...
55

2f2ff0ee5   Filipe Manana   Btrfs: fix metada...
56
57
58
59
60
  	/*
  	 * Lock for counters and all fields used to determine if the inode is in
  	 * the log or not (last_trans, last_sub_trans, last_log_commit,
  	 * logged_trans).
  	 */
9e0baf60d   Josef Bacik   Btrfs: fix enospc...
61
  	spinlock_t lock;
d352ac681   Chris Mason   Btrfs: add and im...
62
  	/* the extent_tree has caches of all the extent mappings to disk */
a52d9a803   Chris Mason   Btrfs: Extent bas...
63
  	struct extent_map_tree extent_tree;
d352ac681   Chris Mason   Btrfs: add and im...
64
65
  
  	/* the io_tree does range state (DIRTY, LOCKED etc) */
d1310b2e0   Chris Mason   Btrfs: Split the ...
66
  	struct extent_io_tree io_tree;
d352ac681   Chris Mason   Btrfs: add and im...
67
68
69
70
  
  	/* special utility tree used to record which mirrors have already been
  	 * tried when checksums fail for a given block
  	 */
7e38326f5   Chris Mason   Btrfs: Handle che...
71
  	struct extent_io_tree io_failure_tree;
d352ac681   Chris Mason   Btrfs: add and im...
72

d352ac681   Chris Mason   Btrfs: add and im...
73
  	/* held while logging the inode in tree-log.c */
e02119d5a   Chris Mason   Btrfs: Add a writ...
74
  	struct mutex log_mutex;
d352ac681   Chris Mason   Btrfs: add and im...
75

f248679e8   Josef Bacik   Btrfs: add a dela...
76
77
  	/* held while doing delalloc reservations */
  	struct mutex delalloc_mutex;
d352ac681   Chris Mason   Btrfs: add and im...
78
  	/* used to order data wrt metadata */
e6dcd2dc9   Chris Mason   Btrfs: New data=o...
79
  	struct btrfs_ordered_inode_tree ordered_tree;
15ee9bc7e   Josef Bacik   Btrfs: delay comm...
80

d352ac681   Chris Mason   Btrfs: add and im...
81
82
83
84
  	/* list of all the delalloc inodes in the FS.  There are times we need
  	 * to write all the delalloc pages to disk, and this list is used
  	 * to walk them all.
  	 */
ea8c28194   Chris Mason   Btrfs: Maintain a...
85
  	struct list_head delalloc_inodes;
5d4f98a28   Yan Zheng   Btrfs: Mixed back...
86
87
  	/* node for the red-black tree that links inodes in subvolume root */
  	struct rb_node rb_node;
72ac3c0d7   Josef Bacik   Btrfs: convert th...
88
  	unsigned long runtime_flags;
9c931c5ab   Nathaniel Yazdani   btrfs: fix minor ...
89
  	/* Keep track of who's O_SYNC/fsyncing currently */
b812ce287   Josef Bacik   Btrfs: inline csu...
90
  	atomic_t sync_writers;
d352ac681   Chris Mason   Btrfs: add and im...
91
92
93
  	/* full 64 bit generation number, struct vfs_inode doesn't have a big
  	 * enough field for this.
  	 */
e02119d5a   Chris Mason   Btrfs: Add a writ...
94
  	u64 generation;
15ee9bc7e   Josef Bacik   Btrfs: delay comm...
95
96
97
98
  	/*
  	 * transid of the trans_handle that last modified this inode
  	 */
  	u64 last_trans;
257c62e1b   Chris Mason   Btrfs: avoid tree...
99
100
  
  	/*
bb14a59b6   Miao Xie   Btrfs: use signed...
101
  	 * transid that last logged this inode
257c62e1b   Chris Mason   Btrfs: avoid tree...
102
  	 */
bb14a59b6   Miao Xie   Btrfs: use signed...
103
  	u64 logged_trans;
257c62e1b   Chris Mason   Btrfs: avoid tree...
104

e02119d5a   Chris Mason   Btrfs: Add a writ...
105
  	/*
bb14a59b6   Miao Xie   Btrfs: use signed...
106
  	 * log transid when this inode was last modified
e02119d5a   Chris Mason   Btrfs: Add a writ...
107
  	 */
bb14a59b6   Miao Xie   Btrfs: use signed...
108
109
110
111
  	int last_sub_trans;
  
  	/* a local copy of root's last_log_commit */
  	int last_log_commit;
49eb7e46d   Chris Mason   Btrfs: Dir fsync ...
112

d352ac681   Chris Mason   Btrfs: add and im...
113
114
115
  	/* total number of bytes pending delalloc, used by stat to calc the
  	 * real block usage of the file
  	 */
9069218d4   Chris Mason   Btrfs: Fix i_bloc...
116
  	u64 delalloc_bytes;
d352ac681   Chris Mason   Btrfs: add and im...
117
118
  
  	/*
a7e3b975a   Filipe Manana   Btrfs: fix report...
119
120
121
122
123
124
125
  	 * Total number of bytes pending delalloc that fall within a file
  	 * range that is either a hole or beyond EOF (and no prealloc extent
  	 * exists in the range). This is always <= delalloc_bytes.
  	 */
  	u64 new_delalloc_bytes;
  
  	/*
47059d930   Wang Shilong   Btrfs: make defra...
126
127
128
129
130
131
  	 * total number of bytes pending defrag, used by stat to check whether
  	 * it needs COW.
  	 */
  	u64 defrag_bytes;
  
  	/*
d352ac681   Chris Mason   Btrfs: add and im...
132
133
134
135
  	 * the size of the file stored in the metadata on disk.  data=ordered
  	 * means the in-memory i_size might be larger than the size on disk
  	 * because not all the blocks are written yet.
  	 */
dbe674a99   Chris Mason   Btrfs: Update on ...
136
  	u64 disk_i_size;
d352ac681   Chris Mason   Btrfs: add and im...
137

aec7477b3   Josef Bacik   Btrfs: Implement ...
138
139
140
141
142
  	/*
  	 * if this is a directory then index_cnt is the counter for the index
  	 * number for new files that are created
  	 */
  	u64 index_cnt;
d352ac681   Chris Mason   Btrfs: add and im...
143

67de11769   Miao Xie   Btrfs: introduce ...
144
145
  	/* Cache the directory index number to speed the dir/file remove */
  	u64 dir_index;
12fcfd22f   Chris Mason   Btrfs: tree loggi...
146
147
148
149
150
151
  	/* the fsync log has some corner cases that mean we have to check
  	 * directories to see if any unlinks have been done before
  	 * the directory was logged.  See tree-log.c for all the
  	 * details
  	 */
  	u64 last_unlink_trans;
7709cde33   Josef Bacik   Btrfs: calculate ...
152
  	/*
20373d980   Filipe Manana   Btrfs: fix fsync ...
153
154
155
156
157
158
  	 * Track the transaction id of the last transaction used to create a
  	 * hard link for the inode. This is used by the log tree (fsync).
  	 */
  	u64 last_link_trans;
  
  	/*
7709cde33   Josef Bacik   Btrfs: calculate ...
159
160
161
162
  	 * Number of bytes outstanding that are going to need csums.  This is
  	 * used in ENOSPC accounting.
  	 */
  	u64 csum_bytes;
f1bdcc0a8   Josef Bacik   Btrfs: move stuff...
163
164
  	/* flags field from the on disk inode */
  	u32 flags;
5a3f23d51   Chris Mason   Btrfs: add extra ...
165
  	/*
32c00aff7   Josef Bacik   Btrfs: release de...
166
167
168
169
  	 * Counters to keep track of the number of extent item's we may use due
  	 * to delalloc and such.  outstanding_extents is the number of extent
  	 * items we think we'll end up using, and reserved_extents is the number
  	 * of extent items we've reserved metadata for.
9ed74f2db   Josef Bacik   Btrfs: proper -EN...
170
  	 */
9e0baf60d   Josef Bacik   Btrfs: fix enospc...
171
172
  	unsigned outstanding_extents;
  	unsigned reserved_extents;
9ed74f2db   Josef Bacik   Btrfs: proper -EN...
173
174
  
  	/*
b52aa8c93   David Sterba   btrfs: rename var...
175
  	 * Cached values of inode properties
1e701a329   Chris Mason   Btrfs: add new de...
176
  	 */
b52aa8c93   David Sterba   btrfs: rename var...
177
  	unsigned prop_compress;		/* per-file compression algorithm */
eec63c65d   David Sterba   btrfs: separate d...
178
179
180
181
182
  	/*
  	 * Force compression on the file using the defrag ioctl, could be
  	 * different from prop_compress and takes precedence if set
  	 */
  	unsigned defrag_compress;
1e701a329   Chris Mason   Btrfs: add new de...
183

16cdcec73   Miao Xie   btrfs: implement ...
184
  	struct btrfs_delayed_node *delayed_node;
9cc97d646   chandan r   Btrfs: Add code t...
185
186
  	/* File creation time. */
  	struct timespec i_otime;
8089fe62c   David Sterba   btrfs: put delaye...
187
188
189
  	/* Hook into fs_info->delayed_iputs */
  	struct list_head delayed_iput;
  	long delayed_iput_count;
5f9a8a51d   Filipe Manana   Btrfs: add semaph...
190
191
192
193
194
195
196
197
198
  	/*
  	 * To avoid races between lockless (i_mutex not held) direct IO writes
  	 * and concurrent fsync requests. Direct IO writes must acquire read
  	 * access on this semaphore for creating an extent map and its
  	 * corresponding ordered extent. The fast fsync path must acquire write
  	 * access on this semaphore before it collects ordered extents and
  	 * extent maps.
  	 */
  	struct rw_semaphore dio_sem;
d352ac681   Chris Mason   Btrfs: add and im...
199
  	struct inode vfs_inode;
2c90e5d65   Chris Mason   Btrfs: still corr...
200
  };
dbe674a99   Chris Mason   Btrfs: Update on ...
201

16cdcec73   Miao Xie   btrfs: implement ...
202
  extern unsigned char btrfs_filetype_table[];
9a35b6372   Jeff Mahoney   btrfs: constify t...
203
  static inline struct btrfs_inode *BTRFS_I(const struct inode *inode)
2c90e5d65   Chris Mason   Btrfs: still corr...
204
205
206
  {
  	return container_of(inode, struct btrfs_inode, vfs_inode);
  }
778ba82b1   Filipe David Borba Manana   Btrfs: improve in...
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
  static inline unsigned long btrfs_inode_hash(u64 objectid,
  					     const struct btrfs_root *root)
  {
  	u64 h = objectid ^ (root->objectid * GOLDEN_RATIO_PRIME);
  
  #if BITS_PER_LONG == 32
  	h = (h >> 32) ^ (h & 0xffffffff);
  #endif
  
  	return (unsigned long)h;
  }
  
  static inline void btrfs_insert_inode_hash(struct inode *inode)
  {
  	unsigned long h = btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root);
  
  	__insert_inode_hash(inode, h);
  }
9a35b6372   Jeff Mahoney   btrfs: constify t...
225
  static inline u64 btrfs_ino(const struct btrfs_inode *inode)
33345d015   Li Zefan   Btrfs: Always use...
226
  {
4a0cc7ca6   Nikolay Borisov   btrfs: Make btrfs...
227
  	u64 ino = inode->location.objectid;
33345d015   Li Zefan   Btrfs: Always use...
228

14c7cca78   Liu Bo   Btrfs: fix an oop...
229
230
231
232
  	/*
  	 * !ino: btree_inode
  	 * type == BTRFS_ROOT_ITEM_KEY: subvol dir
  	 */
4a0cc7ca6   Nikolay Borisov   btrfs: Make btrfs...
233
234
  	if (!ino || inode->location.type == BTRFS_ROOT_ITEM_KEY)
  		ino = inode->vfs_inode.i_ino;
33345d015   Li Zefan   Btrfs: Always use...
235
236
  	return ino;
  }
6ef06d279   Nikolay Borisov   btrfs: Make btrfs...
237
  static inline void btrfs_i_size_write(struct btrfs_inode *inode, u64 size)
dbe674a99   Chris Mason   Btrfs: Update on ...
238
  {
6ef06d279   Nikolay Borisov   btrfs: Make btrfs...
239
240
  	i_size_write(&inode->vfs_inode, size);
  	inode->disk_i_size = size;
dbe674a99   Chris Mason   Btrfs: Update on ...
241
  }
70ddc553b   Nikolay Borisov   btrfs: make btrfs...
242
  static inline bool btrfs_is_free_space_inode(struct btrfs_inode *inode)
2cf8572da   Chris Mason   Btrfs: use the co...
243
  {
70ddc553b   Nikolay Borisov   btrfs: make btrfs...
244
  	struct btrfs_root *root = inode->root;
83eea1f1b   Liu Bo   Btrfs: kill root ...
245

51a8cf9d2   Liu Bo   Btrfs: fix btrfs_...
246
  	if (root == root->fs_info->tree_root &&
70ddc553b   Nikolay Borisov   btrfs: make btrfs...
247
  	    btrfs_ino(inode) != BTRFS_BTREE_INODE_OBJECTID)
51a8cf9d2   Liu Bo   Btrfs: fix btrfs_...
248
  		return true;
70ddc553b   Nikolay Borisov   btrfs: make btrfs...
249
  	if (inode->location.objectid == BTRFS_FREE_INO_OBJECTID)
2cf8572da   Chris Mason   Btrfs: use the co...
250
251
252
  		return true;
  	return false;
  }
0f8939b8a   Nikolay Borisov   btrfs: Make btrfs...
253
  static inline int btrfs_inode_in_log(struct btrfs_inode *inode, u64 generation)
22ee6985d   Josef Bacik   Btrfs: check to s...
254
  {
2f2ff0ee5   Filipe Manana   Btrfs: fix metada...
255
  	int ret = 0;
0f8939b8a   Nikolay Borisov   btrfs: Make btrfs...
256
257
258
259
  	spin_lock(&inode->lock);
  	if (inode->logged_trans == generation &&
  	    inode->last_sub_trans <= inode->last_log_commit &&
  	    inode->last_sub_trans <= inode->root->last_log_commit) {
125c4cf9f   Filipe Manana   Btrfs: set inode'...
260
261
262
263
264
265
266
  		/*
  		 * After a ranged fsync we might have left some extent maps
  		 * (that fall outside the fsync's range). So return false
  		 * here if the list isn't empty, to make sure btrfs_log_inode()
  		 * will be called and process those extent maps.
  		 */
  		smp_mb();
0f8939b8a   Nikolay Borisov   btrfs: Make btrfs...
267
  		if (list_empty(&inode->extent_tree.modified_extents))
2f2ff0ee5   Filipe Manana   Btrfs: fix metada...
268
  			ret = 1;
125c4cf9f   Filipe Manana   Btrfs: set inode'...
269
  	}
0f8939b8a   Nikolay Borisov   btrfs: Make btrfs...
270
  	spin_unlock(&inode->lock);
2f2ff0ee5   Filipe Manana   Btrfs: fix metada...
271
  	return ret;
22ee6985d   Josef Bacik   Btrfs: check to s...
272
  }
c1dc08967   Miao Xie   Btrfs: do file da...
273
  #define BTRFS_DIO_ORIG_BIO_SUBMITTED	0x1
facc8a224   Miao Xie   Btrfs: don't cach...
274
275
  struct btrfs_dio_private {
  	struct inode *inode;
c1dc08967   Miao Xie   Btrfs: do file da...
276
  	unsigned long flags;
facc8a224   Miao Xie   Btrfs: don't cach...
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
  	u64 logical_offset;
  	u64 disk_bytenr;
  	u64 bytes;
  	void *private;
  
  	/* number of bios pending for this dio */
  	atomic_t pending_bios;
  
  	/* IO errors */
  	int errors;
  
  	/* orig_bio is our btrfs_io_bio */
  	struct bio *orig_bio;
  
  	/* dio_bio came from fs/direct-io.c */
  	struct bio *dio_bio;
c1dc08967   Miao Xie   Btrfs: do file da...
293
294
  
  	/*
013276101   Nicholas D Steeves   btrfs: fix string...
295
  	 * The original bio may be split to several sub-bios, this is
c1dc08967   Miao Xie   Btrfs: do file da...
296
297
  	 * done during endio of sub-bios
  	 */
4e4cbee93   Christoph Hellwig   block: switch bio...
298
299
  	blk_status_t (*subio_endio)(struct inode *, struct btrfs_io_bio *,
  			blk_status_t);
facc8a224   Miao Xie   Btrfs: don't cach...
300
  };
2e60a51e6   Miao Xie   Btrfs: serialize ...
301
302
303
304
305
  /*
   * Disable DIO read nolock optimization, so new dio readers will be forced
   * to grab i_mutex. It is used to avoid the endless truncate due to
   * nonlocked dio read.
   */
abcefb1ee   Nikolay Borisov   btrfs: make btrfs...
306
  static inline void btrfs_inode_block_unlocked_dio(struct btrfs_inode *inode)
2e60a51e6   Miao Xie   Btrfs: serialize ...
307
  {
abcefb1ee   Nikolay Borisov   btrfs: make btrfs...
308
  	set_bit(BTRFS_INODE_READDIO_NEED_LOCK, &inode->runtime_flags);
2e60a51e6   Miao Xie   Btrfs: serialize ...
309
310
  	smp_mb();
  }
0b581701d   Nikolay Borisov   btrfs: make btrfs...
311
  static inline void btrfs_inode_resume_unlocked_dio(struct btrfs_inode *inode)
2e60a51e6   Miao Xie   Btrfs: serialize ...
312
  {
4e857c58e   Peter Zijlstra   arch: Mass conver...
313
  	smp_mb__before_atomic();
0b581701d   Nikolay Borisov   btrfs: make btrfs...
314
  	clear_bit(BTRFS_INODE_READDIO_NEED_LOCK, &inode->runtime_flags);
2e60a51e6   Miao Xie   Btrfs: serialize ...
315
  }
0970a22e5   Nikolay Borisov   btrfs: make btrfs...
316
  static inline void btrfs_print_data_csum_error(struct btrfs_inode *inode,
6f6b643e4   Qu Wenruo   btrfs: Better csu...
317
318
  		u64 logical_start, u32 csum, u32 csum_expected, int mirror_num)
  {
0970a22e5   Nikolay Borisov   btrfs: make btrfs...
319
  	struct btrfs_root *root = inode->root;
6f6b643e4   Qu Wenruo   btrfs: Better csu...
320
321
322
323
324
  
  	/* Output minus objectid, which is more meaningful */
  	if (root->objectid >= BTRFS_LAST_FREE_OBJECTID)
  		btrfs_warn_rl(root->fs_info,
  	"csum failed root %lld ino %lld off %llu csum 0x%08x expected csum 0x%08x mirror %d",
0970a22e5   Nikolay Borisov   btrfs: make btrfs...
325
  			root->objectid, btrfs_ino(inode),
6f6b643e4   Qu Wenruo   btrfs: Better csu...
326
327
328
329
  			logical_start, csum, csum_expected, mirror_num);
  	else
  		btrfs_warn_rl(root->fs_info,
  	"csum failed root %llu ino %llu off %llu csum 0x%08x expected csum 0x%08x mirror %d",
0970a22e5   Nikolay Borisov   btrfs: make btrfs...
330
  			root->objectid, btrfs_ino(inode),
6f6b643e4   Qu Wenruo   btrfs: Better csu...
331
332
  			logical_start, csum, csum_expected, mirror_num);
  }
fc4adbff8   Alex Gartrell   btrfs: Drop EXTEN...
333
  bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end);
2c90e5d65   Chris Mason   Btrfs: still corr...
334
  #endif