0001
0002
0003
0004
0005
0006 #ifndef BTRFS_INODE_H
0007 #define BTRFS_INODE_H
0008
0009 #include <linux/hash.h>
0010 #include <linux/refcount.h>
0011 #include "extent_map.h"
0012 #include "extent_io.h"
0013 #include "ordered-data.h"
0014 #include "delayed-inode.h"
0015
0016
0017
0018
0019
0020
0021 #define BTRFS_DIR_START_INDEX 2
0022
0023
0024
0025
0026
0027
0028
0029
0030 enum {
0031 BTRFS_INODE_FLUSH_ON_CLOSE,
0032 BTRFS_INODE_DUMMY,
0033 BTRFS_INODE_IN_DEFRAG,
0034 BTRFS_INODE_HAS_ASYNC_EXTENT,
0035
0036
0037
0038
0039
0040 BTRFS_INODE_NEEDS_FULL_SYNC,
0041 BTRFS_INODE_COPY_EVERYTHING,
0042 BTRFS_INODE_IN_DELALLOC_LIST,
0043 BTRFS_INODE_HAS_PROPS,
0044 BTRFS_INODE_SNAPSHOT_FLUSH,
0045
0046
0047
0048
0049
0050
0051 BTRFS_INODE_NO_XATTRS,
0052
0053
0054
0055
0056
0057
0058
0059
0060 BTRFS_INODE_NO_DELALLOC_FLUSH,
0061
0062
0063
0064
0065
0066
0067 BTRFS_INODE_VERITY_IN_PROGRESS,
0068 };
0069
0070
0071 struct btrfs_inode {
0072
0073 struct btrfs_root *root;
0074
0075
0076
0077
0078 struct btrfs_key location;
0079
0080
0081
0082
0083
0084
0085
0086 spinlock_t lock;
0087
0088
0089 struct extent_map_tree extent_tree;
0090
0091
0092 struct extent_io_tree io_tree;
0093
0094
0095
0096
0097 struct extent_io_tree io_failure_tree;
0098
0099
0100
0101
0102
0103 struct extent_io_tree file_extent_tree;
0104
0105
0106 struct mutex log_mutex;
0107
0108
0109 struct btrfs_ordered_inode_tree ordered_tree;
0110
0111
0112
0113
0114
0115 struct list_head delalloc_inodes;
0116
0117
0118 struct rb_node rb_node;
0119
0120 unsigned long runtime_flags;
0121
0122
0123 atomic_t sync_writers;
0124
0125
0126
0127
0128 u64 generation;
0129
0130
0131
0132
0133 u64 last_trans;
0134
0135
0136
0137
0138 u64 logged_trans;
0139
0140
0141
0142
0143 int last_sub_trans;
0144
0145
0146 int last_log_commit;
0147
0148
0149
0150
0151
0152 u64 delalloc_bytes;
0153
0154 union {
0155
0156
0157
0158
0159
0160
0161 u64 new_delalloc_bytes;
0162
0163
0164
0165
0166 u64 last_dir_index_offset;
0167 };
0168
0169
0170
0171
0172
0173 u64 defrag_bytes;
0174
0175
0176
0177
0178
0179
0180 u64 disk_i_size;
0181
0182
0183
0184
0185
0186
0187 u64 index_cnt;
0188
0189
0190 u64 dir_index;
0191
0192
0193
0194
0195
0196
0197 u64 last_unlink_trans;
0198
0199
0200
0201
0202
0203
0204
0205
0206
0207
0208 u64 last_reflink_trans;
0209
0210
0211
0212
0213
0214 u64 csum_bytes;
0215
0216
0217 u32 flags;
0218
0219 u32 ro_flags;
0220
0221
0222
0223
0224
0225
0226
0227 unsigned outstanding_extents;
0228
0229 struct btrfs_block_rsv block_rsv;
0230
0231
0232
0233
0234 unsigned prop_compress;
0235
0236
0237
0238
0239 unsigned defrag_compress;
0240
0241 struct btrfs_delayed_node *delayed_node;
0242
0243
0244 struct timespec64 i_otime;
0245
0246
0247 struct list_head delayed_iput;
0248
0249 struct rw_semaphore i_mmap_lock;
0250 struct inode vfs_inode;
0251 };
0252
0253 static inline u32 btrfs_inode_sectorsize(const struct btrfs_inode *inode)
0254 {
0255 return inode->root->fs_info->sectorsize;
0256 }
0257
0258 static inline struct btrfs_inode *BTRFS_I(const struct inode *inode)
0259 {
0260 return container_of(inode, struct btrfs_inode, vfs_inode);
0261 }
0262
0263 static inline unsigned long btrfs_inode_hash(u64 objectid,
0264 const struct btrfs_root *root)
0265 {
0266 u64 h = objectid ^ (root->root_key.objectid * GOLDEN_RATIO_PRIME);
0267
0268 #if BITS_PER_LONG == 32
0269 h = (h >> 32) ^ (h & 0xffffffff);
0270 #endif
0271
0272 return (unsigned long)h;
0273 }
0274
0275 static inline void btrfs_insert_inode_hash(struct inode *inode)
0276 {
0277 unsigned long h = btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root);
0278
0279 __insert_inode_hash(inode, h);
0280 }
0281
0282 #if BITS_PER_LONG == 32
0283
0284
0285
0286
0287
0288 static inline u64 btrfs_ino(const struct btrfs_inode *inode)
0289 {
0290 u64 ino = inode->location.objectid;
0291
0292
0293 if (inode->location.type == BTRFS_ROOT_ITEM_KEY)
0294 ino = inode->vfs_inode.i_ino;
0295 return ino;
0296 }
0297
0298 #else
0299
0300 static inline u64 btrfs_ino(const struct btrfs_inode *inode)
0301 {
0302 return inode->vfs_inode.i_ino;
0303 }
0304
0305 #endif
0306
0307 static inline void btrfs_i_size_write(struct btrfs_inode *inode, u64 size)
0308 {
0309 i_size_write(&inode->vfs_inode, size);
0310 inode->disk_i_size = size;
0311 }
0312
0313 static inline bool btrfs_is_free_space_inode(struct btrfs_inode *inode)
0314 {
0315 struct btrfs_root *root = inode->root;
0316
0317 if (root == root->fs_info->tree_root &&
0318 btrfs_ino(inode) != BTRFS_BTREE_INODE_OBJECTID)
0319 return true;
0320
0321 return false;
0322 }
0323
0324 static inline bool is_data_inode(struct inode *inode)
0325 {
0326 return btrfs_ino(BTRFS_I(inode)) != BTRFS_BTREE_INODE_OBJECTID;
0327 }
0328
0329 static inline void btrfs_mod_outstanding_extents(struct btrfs_inode *inode,
0330 int mod)
0331 {
0332 lockdep_assert_held(&inode->lock);
0333 inode->outstanding_extents += mod;
0334 if (btrfs_is_free_space_inode(inode))
0335 return;
0336 trace_btrfs_inode_mod_outstanding_extents(inode->root, btrfs_ino(inode),
0337 mod);
0338 }
0339
0340
0341
0342
0343
0344
0345
0346
0347
0348 static inline void btrfs_set_inode_last_sub_trans(struct btrfs_inode *inode)
0349 {
0350 spin_lock(&inode->lock);
0351 inode->last_sub_trans = inode->root->log_transid;
0352 spin_unlock(&inode->lock);
0353 }
0354
0355
0356
0357
0358
0359
0360 static inline void btrfs_set_inode_full_sync(struct btrfs_inode *inode)
0361 {
0362 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags);
0363
0364
0365
0366
0367
0368
0369
0370
0371
0372
0373
0374
0375
0376
0377
0378
0379 spin_lock(&inode->lock);
0380 if (inode->last_reflink_trans < inode->last_trans)
0381 inode->last_reflink_trans = inode->last_trans;
0382 spin_unlock(&inode->lock);
0383 }
0384
0385 static inline bool btrfs_inode_in_log(struct btrfs_inode *inode, u64 generation)
0386 {
0387 bool ret = false;
0388
0389 spin_lock(&inode->lock);
0390 if (inode->logged_trans == generation &&
0391 inode->last_sub_trans <= inode->last_log_commit &&
0392 inode->last_sub_trans <= inode->root->last_log_commit)
0393 ret = true;
0394 spin_unlock(&inode->lock);
0395 return ret;
0396 }
0397
0398
0399
0400
0401 static inline bool btrfs_inode_can_compress(const struct btrfs_inode *inode)
0402 {
0403 if (inode->flags & BTRFS_INODE_NODATACOW ||
0404 inode->flags & BTRFS_INODE_NODATASUM)
0405 return false;
0406 return true;
0407 }
0408
0409
0410
0411
0412
0413 static inline u64 btrfs_inode_combine_flags(u32 flags, u32 ro_flags)
0414 {
0415 return (flags | ((u64)ro_flags << 32));
0416 }
0417
0418 static inline void btrfs_inode_split_flags(u64 inode_item_flags,
0419 u32 *flags, u32 *ro_flags)
0420 {
0421 *flags = (u32)inode_item_flags;
0422 *ro_flags = (u32)(inode_item_flags >> 32);
0423 }
0424
0425
0426 #define CSUM_FMT "0x%*phN"
0427 #define CSUM_FMT_VALUE(size, bytes) size, bytes
0428
0429 static inline void btrfs_print_data_csum_error(struct btrfs_inode *inode,
0430 u64 logical_start, u8 *csum, u8 *csum_expected, int mirror_num)
0431 {
0432 struct btrfs_root *root = inode->root;
0433 const u32 csum_size = root->fs_info->csum_size;
0434
0435
0436 if (root->root_key.objectid >= BTRFS_LAST_FREE_OBJECTID)
0437 btrfs_warn_rl(root->fs_info,
0438 "csum failed root %lld ino %lld off %llu csum " CSUM_FMT " expected csum " CSUM_FMT " mirror %d",
0439 root->root_key.objectid, btrfs_ino(inode),
0440 logical_start,
0441 CSUM_FMT_VALUE(csum_size, csum),
0442 CSUM_FMT_VALUE(csum_size, csum_expected),
0443 mirror_num);
0444 else
0445 btrfs_warn_rl(root->fs_info,
0446 "csum failed root %llu ino %llu off %llu csum " CSUM_FMT " expected csum " CSUM_FMT " mirror %d",
0447 root->root_key.objectid, btrfs_ino(inode),
0448 logical_start,
0449 CSUM_FMT_VALUE(csum_size, csum),
0450 CSUM_FMT_VALUE(csum_size, csum_expected),
0451 mirror_num);
0452 }
0453
0454 #endif