xref: /freebsd/sys/fs/ext2fs/ext2_extents.c (revision 43faedc1339a9624c7acedb7f3e5624e64da5b99)
1 /*-
2  * Copyright (c) 2010 Zheng Liu <lz@freebsd.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28 
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/types.h>
32 #include <sys/kernel.h>
33 #include <sys/malloc.h>
34 #include <sys/vnode.h>
35 #include <sys/bio.h>
36 #include <sys/buf.h>
37 #include <sys/conf.h>
38 
39 #include <fs/ext2fs/ext2_mount.h>
40 #include <fs/ext2fs/fs.h>
41 #include <fs/ext2fs/inode.h>
42 #include <fs/ext2fs/ext2fs.h>
43 #include <fs/ext2fs/ext2_extents.h>
44 #include <fs/ext2fs/ext2_extern.h>
45 
46 static void ext4_ext_binsearch_index(struct inode *ip, struct ext4_extent_path
47 		*path, daddr_t lbn)
48 {
49 	struct ext4_extent_header *ehp = path->ep_header;
50 	struct ext4_extent_index *l, *r, *m;
51 
52 	l = (struct ext4_extent_index *)(char *)(ehp + 1);
53 	r = (struct ext4_extent_index *)(char *)(ehp + 1) + ehp->eh_ecount - 1;
54 	while (l <= r) {
55 		m = l + (r - l) / 2;
56 		if (lbn < m->ei_blk)
57 			r = m - 1;
58 		else
59 			l = m + 1;
60 	}
61 
62 	path->ep_index = l - 1;
63 }
64 
65 static void
66 ext4_ext_binsearch(struct inode *ip, struct ext4_extent_path *path, daddr_t lbn)
67 {
68 	struct ext4_extent_header *ehp = path->ep_header;
69 	struct ext4_extent *first, *l, *r, *m;
70 
71 	if (ehp->eh_ecount == 0)
72 		return;
73 
74 	first = (struct ext4_extent *)(char *)(ehp + 1);
75 	l = first;
76 	r = first + ehp->eh_ecount - 1;
77 	while (l <= r) {
78 		m = l + (r - l) / 2;
79 		if (lbn < m->e_blk)
80 			r = m - 1;
81 		else
82 			l = m + 1;
83 	}
84 
85 	if (l == first) {
86 		path->ep_sparse_ext.e_blk = lbn;
87 		path->ep_sparse_ext.e_len = first->e_blk - lbn;
88 		path->ep_sparse_ext.e_start_hi = 0;
89 		path->ep_sparse_ext.e_start_lo = 0;
90 		path->ep_is_sparse = 1;
91 		return;
92 	}
93 	path->ep_ext = l - 1;
94 	if (path->ep_ext->e_blk + path->ep_ext->e_len <= lbn) {
95 		path->ep_sparse_ext.e_blk = lbn;
96 		if (l <= (first + ehp->eh_ecount - 1))
97 			path->ep_sparse_ext.e_len = l->e_blk - lbn;
98 		else	// XXX: where does it end?
99 			path->ep_sparse_ext.e_len = 1;
100 		path->ep_sparse_ext.e_start_hi = 0;
101 		path->ep_sparse_ext.e_start_lo = 0;
102 		path->ep_is_sparse = 1;
103 	}
104 }
105 
106 /*
107  * Find a block in ext4 extent cache.
108  */
109 int
110 ext4_ext_in_cache(struct inode *ip, daddr_t lbn, struct ext4_extent *ep)
111 {
112 	struct ext4_extent_cache *ecp;
113 	int ret = EXT4_EXT_CACHE_NO;
114 
115 	ecp = &ip->i_ext_cache;
116 
117 	/* cache is invalid */
118 	if (ecp->ec_type == EXT4_EXT_CACHE_NO)
119 		return (ret);
120 
121 	if (lbn >= ecp->ec_blk && lbn < ecp->ec_blk + ecp->ec_len) {
122 		ep->e_blk = ecp->ec_blk;
123 		ep->e_start_lo = ecp->ec_start & 0xffffffff;
124 		ep->e_start_hi = ecp->ec_start >> 32 & 0xffff;
125 		ep->e_len = ecp->ec_len;
126 		ret = ecp->ec_type;
127 	}
128 	return (ret);
129 }
130 
131 /*
132  * Put an ext4_extent structure in ext4 cache.
133  */
134 void
135 ext4_ext_put_cache(struct inode *ip, struct ext4_extent *ep, int type)
136 {
137 	struct ext4_extent_cache *ecp;
138 
139 	ecp = &ip->i_ext_cache;
140 	ecp->ec_type = type;
141 	ecp->ec_blk = ep->e_blk;
142 	ecp->ec_len = ep->e_len;
143 	ecp->ec_start = (daddr_t)ep->e_start_hi << 32 | ep->e_start_lo;
144 }
145 
146 /*
147  * Find an extent.
148  */
149 struct ext4_extent_path *
150 ext4_ext_find_extent(struct m_ext2fs *fs, struct inode *ip,
151 		     daddr_t lbn, struct ext4_extent_path *path)
152 {
153 	struct ext4_extent_header *ehp;
154 	uint16_t i;
155 	int error, size;
156 	daddr_t nblk;
157 
158 	ehp = (struct ext4_extent_header *)(char *)ip->i_db;
159 
160 	if (ehp->eh_magic != EXT4_EXT_MAGIC)
161 		return (NULL);
162 
163 	path->ep_header = ehp;
164 
165 	for (i = ehp->eh_depth; i != 0; --i) {
166 		ext4_ext_binsearch_index(ip, path, lbn);
167 		path->ep_depth = 0;
168 		path->ep_ext = NULL;
169 
170 		nblk = (daddr_t)path->ep_index->ei_leaf_hi << 32 |
171 		    path->ep_index->ei_leaf_lo;
172 		size = blksize(fs, ip, nblk);
173 		if (path->ep_bp != NULL) {
174 			brelse(path->ep_bp);
175 			path->ep_bp = NULL;
176 		}
177 		error = bread(ip->i_devvp, fsbtodb(fs, nblk), size, NOCRED,
178 			    &path->ep_bp);
179 		if (error) {
180 			brelse(path->ep_bp);
181 			path->ep_bp = NULL;
182 			return (NULL);
183 		}
184 		ehp = (struct ext4_extent_header *)path->ep_bp->b_data;
185 		path->ep_header = ehp;
186 	}
187 
188 	path->ep_depth = i;
189 	path->ep_ext = NULL;
190 	path->ep_index = NULL;
191 	path->ep_is_sparse = 0;
192 
193 	ext4_ext_binsearch(ip, path, lbn);
194 	return (path);
195 }
196