blob: 98f491d1022b419881d1146c61ca60465d46d6e9 [file] [log] [blame]
Chris Masondc17ff82008-01-08 15:46:30 -05001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
19#ifndef __BTRFS_ORDERED_DATA__
20#define __BTRFS_ORDERED_DATA__
21
Chris Masoneb84ae02008-07-17 13:53:27 -040022/* one of these per inode */
Chris Masondc17ff82008-01-08 15:46:30 -050023struct btrfs_ordered_inode_tree {
Chris Masone6dcd2d2008-07-17 12:53:50 -040024 struct mutex mutex;
Chris Masondc17ff82008-01-08 15:46:30 -050025 struct rb_root tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -040026 struct rb_node *last;
Chris Masondc17ff82008-01-08 15:46:30 -050027};
28
Chris Masoneb84ae02008-07-17 13:53:27 -040029/*
30 * these are used to collect checksums done just before bios submission.
31 * They are attached via a list into the ordered extent, and
32 * checksum items are inserted into the tree after all the blocks in
33 * the ordered extent are on disk
34 */
Chris Masone6dcd2d2008-07-17 12:53:50 -040035struct btrfs_sector_sum {
36 u64 offset;
37 u32 sum;
38};
39
40struct btrfs_ordered_sum {
41 u64 file_offset;
42 u64 len;
43 struct list_head list;
Chris Masoneb84ae02008-07-17 13:53:27 -040044 /* last field is a variable length array of btrfs_sector_sums */
Chris Masone6dcd2d2008-07-17 12:53:50 -040045 struct btrfs_sector_sum sums;
46};
47
Chris Masoneb84ae02008-07-17 13:53:27 -040048/*
49 * bits for the flags field:
50 *
51 * BTRFS_ORDERED_IO_DONE is set when all of the blocks are written.
52 * It is used to make sure metadata is inserted into the tree only once
53 * per extent.
54 *
55 * BTRFS_ORDERED_COMPLETE is set when the extent is removed from the
56 * rbtree, just before waking any waiters. It is used to indicate the
57 * IO is done and any metadata is inserted into the tree.
58 */
Chris Masone6dcd2d2008-07-17 12:53:50 -040059#define BTRFS_ORDERED_IO_DONE 0 /* set when all the pages are written */
Chris Masoneb84ae02008-07-17 13:53:27 -040060
Chris Masone6dcd2d2008-07-17 12:53:50 -040061#define BTRFS_ORDERED_COMPLETE 1 /* set when removed from the tree */
Chris Masone6dcd2d2008-07-17 12:53:50 -040062
63struct btrfs_ordered_extent {
Chris Masoneb84ae02008-07-17 13:53:27 -040064 /* logical offset in the file */
Chris Masone6dcd2d2008-07-17 12:53:50 -040065 u64 file_offset;
Chris Masoneb84ae02008-07-17 13:53:27 -040066
67 /* disk byte number */
Chris Masone6dcd2d2008-07-17 12:53:50 -040068 u64 start;
Chris Masoneb84ae02008-07-17 13:53:27 -040069
70 /* length of the extent in bytes */
Chris Masone6dcd2d2008-07-17 12:53:50 -040071 u64 len;
Chris Masoneb84ae02008-07-17 13:53:27 -040072
73 /* flags (described above) */
Chris Masone6dcd2d2008-07-17 12:53:50 -040074 unsigned long flags;
Chris Masoneb84ae02008-07-17 13:53:27 -040075
76 /* reference count */
Chris Masone6dcd2d2008-07-17 12:53:50 -040077 atomic_t refs;
Chris Masoneb84ae02008-07-17 13:53:27 -040078
79 /* list of checksums for insertion when the extent io is done */
Chris Masone6dcd2d2008-07-17 12:53:50 -040080 struct list_head list;
Chris Masoneb84ae02008-07-17 13:53:27 -040081
82 /* used to wait for the BTRFS_ORDERED_COMPLETE bit */
Chris Masone6dcd2d2008-07-17 12:53:50 -040083 wait_queue_head_t wait;
Chris Masoneb84ae02008-07-17 13:53:27 -040084
85 /* our friendly rbtree entry */
Chris Masone6dcd2d2008-07-17 12:53:50 -040086 struct rb_node rb_node;
87};
88
89
Chris Masoneb84ae02008-07-17 13:53:27 -040090/*
91 * calculates the total size you need to allocate for an ordered sum
92 * structure spanning 'bytes' in the file
93 */
Chris Masone6dcd2d2008-07-17 12:53:50 -040094static inline int btrfs_ordered_sum_size(struct btrfs_root *root, u64 bytes)
95{
96 unsigned long num_sectors = (bytes + root->sectorsize - 1) /
97 root->sectorsize;
98 return sizeof(struct btrfs_ordered_sum) +
99 num_sectors * sizeof(struct btrfs_sector_sum);
100}
101
Chris Masondc17ff82008-01-08 15:46:30 -0500102static inline void
103btrfs_ordered_inode_tree_init(struct btrfs_ordered_inode_tree *t)
104{
Chris Masone6dcd2d2008-07-17 12:53:50 -0400105 mutex_init(&t->mutex);
Chris Masondc17ff82008-01-08 15:46:30 -0500106 t->tree.rb_node = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400107 t->last = NULL;
Chris Masondc17ff82008-01-08 15:46:30 -0500108}
109
Chris Masone6dcd2d2008-07-17 12:53:50 -0400110int btrfs_put_ordered_extent(struct btrfs_ordered_extent *entry);
111int btrfs_remove_ordered_extent(struct inode *inode,
112 struct btrfs_ordered_extent *entry);
113int btrfs_dec_test_ordered_pending(struct inode *inode,
114 u64 file_offset, u64 io_size);
115int btrfs_add_ordered_extent(struct inode *inode, u64 file_offset,
116 u64 start, u64 len);
117int btrfs_add_ordered_sum(struct inode *inode, struct btrfs_ordered_sum *sum);
118struct btrfs_ordered_extent *btrfs_lookup_ordered_extent(struct inode *inode,
119 u64 file_offset);
Chris Masoneb84ae02008-07-17 13:53:27 -0400120void btrfs_start_ordered_extent(struct inode *inode,
121 struct btrfs_ordered_extent *entry, int wait);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400122void btrfs_wait_ordered_range(struct inode *inode, u64 start, u64 len);
123struct btrfs_ordered_extent *
124btrfs_lookup_first_ordered_extent(struct inode * inode, u64 file_offset);
Chris Masondbe674a2008-07-17 12:54:05 -0400125int btrfs_ordered_update_i_size(struct inode *inode,
126 struct btrfs_ordered_extent *ordered);
Chris Masonba1da2f2008-07-17 12:54:15 -0400127int btrfs_find_ordered_sum(struct inode *inode, u64 offset, u32 *sum);
Chris Masondc17ff82008-01-08 15:46:30 -0500128#endif