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