efs_subr.c revision 1.5.4.3 1 1.5.4.3 yamt /* $NetBSD: efs_subr.c,v 1.5.4.3 2007/10/27 11:35:01 yamt Exp $ */
2 1.5.4.2 yamt
3 1.5.4.2 yamt /*
4 1.5.4.2 yamt * Copyright (c) 2006 Stephen M. Rumble <rumble (at) ephemeral.org>
5 1.5.4.2 yamt *
6 1.5.4.2 yamt * Permission to use, copy, modify, and distribute this software for any
7 1.5.4.2 yamt * purpose with or without fee is hereby granted, provided that the above
8 1.5.4.2 yamt * copyright notice and this permission notice appear in all copies.
9 1.5.4.2 yamt *
10 1.5.4.2 yamt * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 1.5.4.2 yamt * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 1.5.4.2 yamt * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 1.5.4.2 yamt * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 1.5.4.2 yamt * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 1.5.4.2 yamt * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 1.5.4.2 yamt * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 1.5.4.2 yamt */
18 1.5.4.2 yamt
19 1.5.4.2 yamt #include <sys/cdefs.h>
20 1.5.4.3 yamt __KERNEL_RCSID(0, "$NetBSD: efs_subr.c,v 1.5.4.3 2007/10/27 11:35:01 yamt Exp $");
21 1.5.4.2 yamt
22 1.5.4.2 yamt #include <sys/param.h>
23 1.5.4.2 yamt #include <sys/kauth.h>
24 1.5.4.2 yamt #include <sys/lwp.h>
25 1.5.4.2 yamt #include <sys/proc.h>
26 1.5.4.2 yamt #include <sys/buf.h>
27 1.5.4.2 yamt #include <sys/mount.h>
28 1.5.4.2 yamt #include <sys/vnode.h>
29 1.5.4.2 yamt #include <sys/namei.h>
30 1.5.4.2 yamt #include <sys/stat.h>
31 1.5.4.2 yamt #include <sys/malloc.h>
32 1.5.4.2 yamt
33 1.5.4.2 yamt #include <miscfs/genfs/genfs_node.h>
34 1.5.4.2 yamt
35 1.5.4.2 yamt #include <fs/efs/efs.h>
36 1.5.4.2 yamt #include <fs/efs/efs_sb.h>
37 1.5.4.2 yamt #include <fs/efs/efs_dir.h>
38 1.5.4.2 yamt #include <fs/efs/efs_genfs.h>
39 1.5.4.2 yamt #include <fs/efs/efs_mount.h>
40 1.5.4.2 yamt #include <fs/efs/efs_extent.h>
41 1.5.4.2 yamt #include <fs/efs/efs_dinode.h>
42 1.5.4.2 yamt #include <fs/efs/efs_inode.h>
43 1.5.4.2 yamt #include <fs/efs/efs_subr.h>
44 1.5.4.2 yamt
45 1.5.4.2 yamt struct pool efs_inode_pool;
46 1.5.4.2 yamt
47 1.5.4.2 yamt /*
48 1.5.4.2 yamt * Calculate a checksum for the provided superblock in __host byte order__.
49 1.5.4.2 yamt *
50 1.5.4.2 yamt * At some point SGI changed the checksum algorithm slightly, which can be
51 1.5.4.2 yamt * enabled with the 'new' flag.
52 1.5.4.2 yamt *
53 1.5.4.2 yamt * Presumably this change occured on or before 24 Oct 1988 (around IRIX 3.1),
54 1.5.4.2 yamt * so we're pretty unlikely to ever actually see an old checksum. Further, it
55 1.5.4.2 yamt * means that EFS_NEWMAGIC filesystems (IRIX >= 3.3) must match the new
56 1.5.4.2 yamt * checksum whereas EFS_MAGIC filesystems could potentially use either
57 1.5.4.2 yamt * algorithm.
58 1.5.4.2 yamt *
59 1.5.4.2 yamt * See comp.sys.sgi <1991Aug9.050838.16876 (at) odin.corp.sgi.com>
60 1.5.4.2 yamt */
61 1.5.4.2 yamt int32_t
62 1.5.4.2 yamt efs_sb_checksum(struct efs_sb *esb, int new)
63 1.5.4.2 yamt {
64 1.5.4.2 yamt int i;
65 1.5.4.2 yamt int32_t cksum;
66 1.5.4.2 yamt uint16_t *sbarray = (uint16_t *)esb;
67 1.5.4.2 yamt
68 1.5.4.2 yamt KASSERT((EFS_SB_CHECKSUM_SIZE % 2) == 0);
69 1.5.4.2 yamt
70 1.5.4.2 yamt for (i = cksum = 0; i < (EFS_SB_CHECKSUM_SIZE / 2); i++) {
71 1.5.4.2 yamt cksum ^= be16toh(sbarray[i]);
72 1.5.4.2 yamt cksum = (cksum << 1) | (new && cksum < 0);
73 1.5.4.2 yamt }
74 1.5.4.2 yamt
75 1.5.4.2 yamt return (cksum);
76 1.5.4.2 yamt }
77 1.5.4.2 yamt
78 1.5.4.2 yamt /*
79 1.5.4.2 yamt * Determine if the superblock is valid.
80 1.5.4.2 yamt *
81 1.5.4.2 yamt * Returns 0 if valid, else invalid. If invalid, 'why' is set to an
82 1.5.4.2 yamt * explanation.
83 1.5.4.2 yamt */
84 1.5.4.2 yamt int
85 1.5.4.2 yamt efs_sb_validate(struct efs_sb *esb, const char **why)
86 1.5.4.2 yamt {
87 1.5.4.2 yamt uint32_t ocksum, ncksum;
88 1.5.4.2 yamt
89 1.5.4.2 yamt *why = NULL;
90 1.5.4.2 yamt
91 1.5.4.2 yamt if (be32toh(esb->sb_magic) != EFS_SB_MAGIC &&
92 1.5.4.2 yamt be32toh(esb->sb_magic) != EFS_SB_NEWMAGIC) {
93 1.5.4.2 yamt *why = "sb_magic invalid";
94 1.5.4.2 yamt return (1);
95 1.5.4.2 yamt }
96 1.5.4.2 yamt
97 1.5.4.2 yamt ocksum = htobe32(efs_sb_checksum(esb, 0));
98 1.5.4.2 yamt ncksum = htobe32(efs_sb_checksum(esb, 1));
99 1.5.4.2 yamt if (esb->sb_checksum != ocksum && esb->sb_checksum != ncksum) {
100 1.5.4.2 yamt *why = "sb_checksum invalid";
101 1.5.4.2 yamt return (1);
102 1.5.4.2 yamt }
103 1.5.4.2 yamt
104 1.5.4.2 yamt if (be32toh(esb->sb_size) > EFS_SIZE_MAX) {
105 1.5.4.2 yamt *why = "sb_size > EFS_SIZE_MAX";
106 1.5.4.2 yamt return (1);
107 1.5.4.2 yamt }
108 1.5.4.2 yamt
109 1.5.4.2 yamt if (be32toh(esb->sb_firstcg) <= EFS_BB_BITMAP) {
110 1.5.4.2 yamt *why = "sb_firstcg <= EFS_BB_BITMAP";
111 1.5.4.2 yamt return (1);
112 1.5.4.2 yamt }
113 1.5.4.2 yamt
114 1.5.4.2 yamt /* XXX - add better sb consistency checks here */
115 1.5.4.2 yamt if (esb->sb_cgfsize == 0 ||
116 1.5.4.2 yamt esb->sb_cgisize == 0 ||
117 1.5.4.2 yamt esb->sb_ncg == 0 ||
118 1.5.4.2 yamt esb->sb_bmsize == 0) {
119 1.5.4.2 yamt *why = "something bad happened";
120 1.5.4.2 yamt return (1);
121 1.5.4.2 yamt }
122 1.5.4.2 yamt
123 1.5.4.2 yamt return (0);
124 1.5.4.2 yamt }
125 1.5.4.2 yamt
126 1.5.4.2 yamt /*
127 1.5.4.2 yamt * Determine the basic block offset and inode index within that block, given
128 1.5.4.2 yamt * the inode 'ino' and filesystem parameters _in host byte order_. The inode
129 1.5.4.2 yamt * will live at byte address 'bboff' * EFS_BB_SIZE + 'index' * EFS_DINODE_SIZE.
130 1.5.4.2 yamt */
131 1.5.4.2 yamt void
132 1.5.4.2 yamt efs_locate_inode(ino_t ino, struct efs_sb *sbp, uint32_t *bboff, int *index)
133 1.5.4.2 yamt {
134 1.5.4.2 yamt uint32_t cgfsize, firstcg;
135 1.5.4.2 yamt uint16_t cgisize;
136 1.5.4.2 yamt
137 1.5.4.2 yamt cgisize = be16toh(sbp->sb_cgisize);
138 1.5.4.2 yamt cgfsize = be32toh(sbp->sb_cgfsize);
139 1.5.4.2 yamt firstcg = be32toh(sbp->sb_firstcg),
140 1.5.4.2 yamt
141 1.5.4.2 yamt *bboff = firstcg + ((ino / (cgisize * EFS_DINODES_PER_BB)) * cgfsize) +
142 1.5.4.2 yamt ((ino % (cgisize * EFS_DINODES_PER_BB)) / EFS_DINODES_PER_BB);
143 1.5.4.2 yamt *index = ino & (EFS_DINODES_PER_BB - 1);
144 1.5.4.2 yamt }
145 1.5.4.2 yamt
146 1.5.4.2 yamt /*
147 1.5.4.2 yamt * Read in an inode from disk.
148 1.5.4.2 yamt *
149 1.5.4.2 yamt * We actually take in four inodes at a time. Hopefully these will stick
150 1.5.4.2 yamt * around in the buffer cache and get used without going to disk.
151 1.5.4.2 yamt *
152 1.5.4.2 yamt * Returns 0 on success.
153 1.5.4.2 yamt */
154 1.5.4.2 yamt int
155 1.5.4.2 yamt efs_read_inode(struct efs_mount *emp, ino_t ino, struct lwp *l,
156 1.5.4.2 yamt struct efs_dinode *di)
157 1.5.4.2 yamt {
158 1.5.4.2 yamt struct efs_sb *sbp;
159 1.5.4.2 yamt struct buf *bp;
160 1.5.4.2 yamt int index, err;
161 1.5.4.2 yamt uint32_t bboff;
162 1.5.4.2 yamt
163 1.5.4.2 yamt sbp = &emp->em_sb;
164 1.5.4.2 yamt efs_locate_inode(ino, sbp, &bboff, &index);
165 1.5.4.2 yamt
166 1.5.4.2 yamt err = efs_bread(emp, bboff, l, &bp);
167 1.5.4.2 yamt if (err) {
168 1.5.4.3 yamt brelse(bp, 0);
169 1.5.4.2 yamt return (err);
170 1.5.4.2 yamt }
171 1.5.4.2 yamt memcpy(di, ((struct efs_dinode *)bp->b_data) + index, sizeof(*di));
172 1.5.4.3 yamt brelse(bp, 0);
173 1.5.4.2 yamt
174 1.5.4.2 yamt return (0);
175 1.5.4.2 yamt }
176 1.5.4.2 yamt
177 1.5.4.2 yamt /*
178 1.5.4.2 yamt * Perform a read from our device handling the potential DEV_BSIZE
179 1.5.4.2 yamt * messiness (although as of 19.2.2006, all ports appear to use 512) as
180 1.5.4.2 yamt * we as EFS block sizing.
181 1.5.4.2 yamt *
182 1.5.4.2 yamt * bboff: basic block offset
183 1.5.4.2 yamt *
184 1.5.4.2 yamt * Returns 0 on success.
185 1.5.4.2 yamt */
186 1.5.4.2 yamt int
187 1.5.4.2 yamt efs_bread(struct efs_mount *emp, uint32_t bboff, struct lwp *l, struct buf **bp)
188 1.5.4.2 yamt {
189 1.5.4.2 yamt KASSERT(bboff < EFS_SIZE_MAX);
190 1.5.4.2 yamt
191 1.5.4.2 yamt return (bread(emp->em_devvp, (daddr_t)bboff * (EFS_BB_SIZE / DEV_BSIZE),
192 1.5.4.2 yamt EFS_BB_SIZE, (l == NULL) ? NOCRED : l->l_cred, bp));
193 1.5.4.2 yamt }
194 1.5.4.2 yamt
195 1.5.4.2 yamt /*
196 1.5.4.2 yamt * Synchronise the in-core, host ordered and typed inode fields with their
197 1.5.4.2 yamt * corresponding on-disk, EFS ordered and typed copies.
198 1.5.4.2 yamt *
199 1.5.4.2 yamt * This is the inverse of efs_dinode_sync_inode(), and should be called when
200 1.5.4.2 yamt * an inode is loaded from disk.
201 1.5.4.2 yamt */
202 1.5.4.2 yamt void
203 1.5.4.2 yamt efs_sync_dinode_to_inode(struct efs_inode *ei)
204 1.5.4.2 yamt {
205 1.5.4.2 yamt
206 1.5.4.2 yamt ei->ei_mode = be16toh(ei->ei_di.di_mode); /*same as nbsd*/
207 1.5.4.2 yamt ei->ei_nlink = be16toh(ei->ei_di.di_nlink);
208 1.5.4.2 yamt ei->ei_uid = be16toh(ei->ei_di.di_uid);
209 1.5.4.2 yamt ei->ei_gid = be16toh(ei->ei_di.di_gid);
210 1.5.4.2 yamt ei->ei_size = be32toh(ei->ei_di.di_size);
211 1.5.4.2 yamt ei->ei_atime = be32toh(ei->ei_di.di_atime);
212 1.5.4.2 yamt ei->ei_mtime = be32toh(ei->ei_di.di_mtime);
213 1.5.4.2 yamt ei->ei_ctime = be32toh(ei->ei_di.di_ctime);
214 1.5.4.2 yamt ei->ei_gen = be32toh(ei->ei_di.di_gen);
215 1.5.4.2 yamt ei->ei_numextents = be16toh(ei->ei_di.di_numextents);
216 1.5.4.2 yamt ei->ei_version = ei->ei_di.di_version;
217 1.5.4.2 yamt }
218 1.5.4.2 yamt
219 1.5.4.2 yamt /*
220 1.5.4.2 yamt * Synchronise the on-disk, EFS ordered and typed inode fields with their
221 1.5.4.2 yamt * corresponding in-core, host ordered and typed copies.
222 1.5.4.2 yamt *
223 1.5.4.2 yamt * This is the inverse of efs_inode_sync_dinode(), and should be called before
224 1.5.4.2 yamt * an inode is flushed to disk.
225 1.5.4.2 yamt */
226 1.5.4.2 yamt void
227 1.5.4.2 yamt efs_sync_inode_to_dinode(struct efs_inode *ei)
228 1.5.4.2 yamt {
229 1.5.4.2 yamt
230 1.5.4.2 yamt panic("readonly -- no need to call me");
231 1.5.4.2 yamt }
232 1.5.4.2 yamt
233 1.5.4.2 yamt #ifdef DIAGNOSTIC
234 1.5.4.2 yamt /*
235 1.5.4.2 yamt * Ensure that the in-core inode's host cached fields match its on-disk copy.
236 1.5.4.2 yamt *
237 1.5.4.2 yamt * Returns 0 if they match.
238 1.5.4.2 yamt */
239 1.5.4.2 yamt static int
240 1.5.4.2 yamt efs_is_inode_synced(struct efs_inode *ei)
241 1.5.4.2 yamt {
242 1.5.4.2 yamt int s;
243 1.5.4.2 yamt
244 1.5.4.2 yamt s = 0;
245 1.5.4.2 yamt /* XXX -- see above remarks about assumption */
246 1.5.4.2 yamt s += (ei->ei_mode != be16toh(ei->ei_di.di_mode));
247 1.5.4.2 yamt s += (ei->ei_nlink != be16toh(ei->ei_di.di_nlink));
248 1.5.4.2 yamt s += (ei->ei_uid != be16toh(ei->ei_di.di_uid));
249 1.5.4.2 yamt s += (ei->ei_gid != be16toh(ei->ei_di.di_gid));
250 1.5.4.2 yamt s += (ei->ei_size != be32toh(ei->ei_di.di_size));
251 1.5.4.2 yamt s += (ei->ei_atime != be32toh(ei->ei_di.di_atime));
252 1.5.4.2 yamt s += (ei->ei_mtime != be32toh(ei->ei_di.di_mtime));
253 1.5.4.2 yamt s += (ei->ei_ctime != be32toh(ei->ei_di.di_ctime));
254 1.5.4.2 yamt s += (ei->ei_gen != be32toh(ei->ei_di.di_gen));
255 1.5.4.2 yamt s += (ei->ei_numextents != be16toh(ei->ei_di.di_numextents));
256 1.5.4.2 yamt s += (ei->ei_version != ei->ei_di.di_version);
257 1.5.4.2 yamt
258 1.5.4.2 yamt return (s);
259 1.5.4.2 yamt }
260 1.5.4.2 yamt #endif
261 1.5.4.2 yamt
262 1.5.4.2 yamt /*
263 1.5.4.2 yamt * Given an efs_dirblk structure and a componentname to search for, return the
264 1.5.4.2 yamt * corresponding inode if it is found.
265 1.5.4.2 yamt *
266 1.5.4.2 yamt * Returns 0 on success.
267 1.5.4.2 yamt */
268 1.5.4.2 yamt static int
269 1.5.4.2 yamt efs_dirblk_lookup(struct efs_dirblk *dir, struct componentname *cn,
270 1.5.4.2 yamt ino_t *inode)
271 1.5.4.2 yamt {
272 1.5.4.2 yamt struct efs_dirent *de;
273 1.5.4.2 yamt int i, slot, offset;
274 1.5.4.2 yamt
275 1.5.4.2 yamt KASSERT(cn->cn_namelen <= EFS_DIRENT_NAMELEN_MAX);
276 1.5.4.2 yamt
277 1.5.4.2 yamt slot = offset = 0;
278 1.5.4.2 yamt
279 1.5.4.2 yamt for (i = 0; i < dir->db_slots; i++) {
280 1.5.4.2 yamt offset = EFS_DIRENT_OFF_EXPND(dir->db_space[i]);
281 1.5.4.2 yamt
282 1.5.4.2 yamt if (offset == EFS_DIRBLK_SLOT_FREE)
283 1.5.4.2 yamt continue;
284 1.5.4.2 yamt
285 1.5.4.2 yamt de = (struct efs_dirent *)((char *)dir + offset);
286 1.5.4.2 yamt if (de->de_namelen == cn->cn_namelen &&
287 1.5.4.2 yamt (strncmp(cn->cn_nameptr, de->de_name, cn->cn_namelen) == 0)){
288 1.5.4.2 yamt slot = i;
289 1.5.4.2 yamt break;
290 1.5.4.2 yamt }
291 1.5.4.2 yamt }
292 1.5.4.2 yamt if (i == dir->db_slots)
293 1.5.4.2 yamt return (ENOENT);
294 1.5.4.2 yamt
295 1.5.4.2 yamt KASSERT(slot < offset && offset < EFS_DIRBLK_SPACE_SIZE);
296 1.5.4.2 yamt de = (struct efs_dirent *)((char *)dir + offset);
297 1.5.4.2 yamt *inode = be32toh(de->de_inumber);
298 1.5.4.2 yamt
299 1.5.4.2 yamt return (0);
300 1.5.4.2 yamt }
301 1.5.4.2 yamt
302 1.5.4.2 yamt /*
303 1.5.4.2 yamt * Given an extent descriptor that represents a directory, look up
304 1.5.4.2 yamt * componentname within its efs_dirblk's. If it is found, return the
305 1.5.4.2 yamt * corresponding inode in 'ino'.
306 1.5.4.2 yamt *
307 1.5.4.2 yamt * Returns 0 on success.
308 1.5.4.2 yamt */
309 1.5.4.2 yamt static int
310 1.5.4.2 yamt efs_extent_lookup(struct efs_mount *emp, struct efs_extent *ex,
311 1.5.4.2 yamt struct componentname *cn, ino_t *ino)
312 1.5.4.2 yamt {
313 1.5.4.2 yamt struct efs_dirblk *db;
314 1.5.4.2 yamt struct buf *bp;
315 1.5.4.2 yamt int i, err;
316 1.5.4.2 yamt
317 1.5.4.2 yamt /*
318 1.5.4.2 yamt * Read in each of the dirblks until we find our entry.
319 1.5.4.2 yamt * If we don't, return ENOENT.
320 1.5.4.2 yamt */
321 1.5.4.2 yamt for (i = 0; i < ex->ex_length; i++) {
322 1.5.4.2 yamt err = efs_bread(emp, ex->ex_bn + i, NULL, &bp);
323 1.5.4.2 yamt if (err) {
324 1.5.4.2 yamt printf("efs: warning: invalid extent descriptor\n");
325 1.5.4.3 yamt brelse(bp, 0);
326 1.5.4.2 yamt return (err);
327 1.5.4.2 yamt }
328 1.5.4.2 yamt
329 1.5.4.2 yamt db = (struct efs_dirblk *)bp->b_data;
330 1.5.4.2 yamt if (efs_dirblk_lookup(db, cn, ino) == 0) {
331 1.5.4.3 yamt brelse(bp, 0);
332 1.5.4.2 yamt return (0);
333 1.5.4.2 yamt }
334 1.5.4.3 yamt brelse(bp, 0);
335 1.5.4.2 yamt }
336 1.5.4.2 yamt
337 1.5.4.2 yamt return (ENOENT);
338 1.5.4.2 yamt }
339 1.5.4.2 yamt
340 1.5.4.2 yamt /*
341 1.5.4.2 yamt * Given the provided in-core inode, look up the pathname requested. If
342 1.5.4.2 yamt * we find it, 'ino' reflects its corresponding on-disk inode number.
343 1.5.4.2 yamt *
344 1.5.4.2 yamt * Returns 0 on success.
345 1.5.4.2 yamt */
346 1.5.4.2 yamt int
347 1.5.4.2 yamt efs_inode_lookup(struct efs_mount *emp, struct efs_inode *ei,
348 1.5.4.2 yamt struct componentname *cn, ino_t *ino)
349 1.5.4.2 yamt {
350 1.5.4.2 yamt struct efs_extent ex;
351 1.5.4.2 yamt struct efs_extent_iterator exi;
352 1.5.4.2 yamt int ret;
353 1.5.4.2 yamt
354 1.5.4.2 yamt KASSERT(VOP_ISLOCKED(ei->ei_vp));
355 1.5.4.2 yamt KASSERT(efs_is_inode_synced(ei) == 0);
356 1.5.4.2 yamt KASSERT((ei->ei_mode & S_IFMT) == S_IFDIR);
357 1.5.4.2 yamt
358 1.5.4.2 yamt efs_extent_iterator_init(&exi, ei, 0);
359 1.5.4.2 yamt while ((ret = efs_extent_iterator_next(&exi, &ex)) == 0) {
360 1.5.4.2 yamt if (efs_extent_lookup(emp, &ex, cn, ino) == 0) {
361 1.5.4.2 yamt return (0);
362 1.5.4.2 yamt }
363 1.5.4.2 yamt }
364 1.5.4.2 yamt
365 1.5.4.2 yamt return ((ret == -1) ? ENOENT : ret);
366 1.5.4.2 yamt }
367 1.5.4.2 yamt
368 1.5.4.2 yamt /*
369 1.5.4.2 yamt * Convert on-disk extent structure to in-core format.
370 1.5.4.2 yamt */
371 1.5.4.2 yamt void
372 1.5.4.2 yamt efs_dextent_to_extent(struct efs_dextent *dex, struct efs_extent *ex)
373 1.5.4.2 yamt {
374 1.5.4.2 yamt
375 1.5.4.2 yamt KASSERT(dex != NULL && ex != NULL);
376 1.5.4.2 yamt
377 1.5.4.2 yamt ex->ex_magic = dex->ex_bytes[0];
378 1.5.4.2 yamt ex->ex_bn = be32toh(dex->ex_words[0]) & 0x00ffffff;
379 1.5.4.2 yamt ex->ex_length = dex->ex_bytes[4];
380 1.5.4.2 yamt ex->ex_offset = be32toh(dex->ex_words[1]) & 0x00ffffff;
381 1.5.4.2 yamt }
382 1.5.4.2 yamt
383 1.5.4.2 yamt /*
384 1.5.4.2 yamt * Convert in-core extent format to on-disk structure.
385 1.5.4.2 yamt */
386 1.5.4.2 yamt void
387 1.5.4.2 yamt efs_extent_to_dextent(struct efs_extent *ex, struct efs_dextent *dex)
388 1.5.4.2 yamt {
389 1.5.4.2 yamt
390 1.5.4.2 yamt KASSERT(ex != NULL && dex != NULL);
391 1.5.4.2 yamt KASSERT(ex->ex_magic == EFS_EXTENT_MAGIC);
392 1.5.4.2 yamt KASSERT((ex->ex_bn & ~EFS_EXTENT_BN_MASK) == 0);
393 1.5.4.2 yamt KASSERT((ex->ex_offset & ~EFS_EXTENT_OFFSET_MASK) == 0);
394 1.5.4.2 yamt
395 1.5.4.2 yamt dex->ex_words[0] = htobe32(ex->ex_bn);
396 1.5.4.2 yamt dex->ex_bytes[0] = ex->ex_magic;
397 1.5.4.2 yamt dex->ex_words[1] = htobe32(ex->ex_offset);
398 1.5.4.2 yamt dex->ex_bytes[4] = ex->ex_length;
399 1.5.4.2 yamt }
400 1.5.4.2 yamt
401 1.5.4.2 yamt /*
402 1.5.4.2 yamt * Initialise an extent iterator.
403 1.5.4.2 yamt *
404 1.5.4.2 yamt * If start_hint is non-0, attempt to set up the iterator beginning with the
405 1.5.4.2 yamt * extent descriptor in which the start_hint'th byte exists. Callers must not
406 1.5.4.2 yamt * expect success (this is simply an optimisation), so we reserve the right
407 1.5.4.2 yamt * to start from the beginning.
408 1.5.4.2 yamt */
409 1.5.4.2 yamt void
410 1.5.4.2 yamt efs_extent_iterator_init(struct efs_extent_iterator *exi, struct efs_inode *eip,
411 1.5.4.2 yamt off_t start_hint)
412 1.5.4.2 yamt {
413 1.5.4.2 yamt struct efs_extent ex, ex2;
414 1.5.4.2 yamt struct buf *bp;
415 1.5.4.2 yamt struct efs_mount *emp = VFSTOEFS(eip->ei_vp->v_mount);
416 1.5.4.2 yamt off_t offset, length, next;
417 1.5.4.2 yamt int i, err, numextents, numinextents;
418 1.5.4.2 yamt int hi, lo, mid;
419 1.5.4.2 yamt int indir;
420 1.5.4.2 yamt
421 1.5.4.2 yamt exi->exi_eip = eip;
422 1.5.4.2 yamt exi->exi_next = 0;
423 1.5.4.2 yamt exi->exi_dnext = 0;
424 1.5.4.2 yamt exi->exi_innext = 0;
425 1.5.4.2 yamt
426 1.5.4.2 yamt if (start_hint == 0)
427 1.5.4.2 yamt return;
428 1.5.4.2 yamt
429 1.5.4.2 yamt /* force iterator to end if hint is too big */
430 1.5.4.2 yamt if (start_hint >= eip->ei_size) {
431 1.5.4.2 yamt exi->exi_next = eip->ei_numextents;
432 1.5.4.2 yamt return;
433 1.5.4.2 yamt }
434 1.5.4.2 yamt
435 1.5.4.2 yamt /*
436 1.5.4.2 yamt * Use start_hint to jump to the right extent descriptor. We'll
437 1.5.4.2 yamt * iterate over the 12 indirect extents because it's cheap, then
438 1.5.4.2 yamt * bring the appropriate vector into core and binary search it.
439 1.5.4.2 yamt */
440 1.5.4.2 yamt
441 1.5.4.2 yamt /*
442 1.5.4.2 yamt * Handle the small file case separately first...
443 1.5.4.2 yamt */
444 1.5.4.2 yamt if (eip->ei_numextents <= EFS_DIRECTEXTENTS) {
445 1.5.4.2 yamt for (i = 0; i < eip->ei_numextents; i++) {
446 1.5.4.2 yamt efs_dextent_to_extent(&eip->ei_di.di_extents[i], &ex);
447 1.5.4.2 yamt
448 1.5.4.2 yamt offset = ex.ex_offset * EFS_BB_SIZE;
449 1.5.4.2 yamt length = ex.ex_length * EFS_BB_SIZE;
450 1.5.4.2 yamt
451 1.5.4.2 yamt if (start_hint >= offset &&
452 1.5.4.2 yamt start_hint < (offset + length)) {
453 1.5.4.2 yamt exi->exi_next = exi->exi_dnext = i;
454 1.5.4.2 yamt return;
455 1.5.4.2 yamt }
456 1.5.4.2 yamt }
457 1.5.4.2 yamt
458 1.5.4.2 yamt /* shouldn't get here, no? */
459 1.5.4.2 yamt EFS_DPRINTF(("efs_extent_iterator_init: bad direct extents\n"));
460 1.5.4.2 yamt return;
461 1.5.4.2 yamt }
462 1.5.4.2 yamt
463 1.5.4.2 yamt /*
464 1.5.4.2 yamt * Now do the large files with indirect extents...
465 1.5.4.2 yamt *
466 1.5.4.2 yamt * The first indirect extent's ex_offset field contains the
467 1.5.4.2 yamt * number of indirect extents used.
468 1.5.4.2 yamt */
469 1.5.4.2 yamt efs_dextent_to_extent(&eip->ei_di.di_extents[0], &ex);
470 1.5.4.2 yamt
471 1.5.4.2 yamt numinextents = ex.ex_offset;
472 1.5.4.2 yamt if (numinextents < 1 || numinextents >= EFS_DIRECTEXTENTS) {
473 1.5.4.2 yamt EFS_DPRINTF(("efs_extent_iterator_init: bad ex.ex_offset\n"));
474 1.5.4.2 yamt return;
475 1.5.4.2 yamt }
476 1.5.4.2 yamt
477 1.5.4.2 yamt next = 0;
478 1.5.4.2 yamt indir = -1;
479 1.5.4.2 yamt numextents = 0;
480 1.5.4.2 yamt for (i = 0; i < numinextents; i++) {
481 1.5.4.2 yamt efs_dextent_to_extent(&eip->ei_di.di_extents[i], &ex);
482 1.5.4.2 yamt
483 1.5.4.2 yamt err = efs_bread(emp, ex.ex_bn, NULL, &bp);
484 1.5.4.2 yamt if (err) {
485 1.5.4.3 yamt brelse(bp, 0);
486 1.5.4.2 yamt return;
487 1.5.4.2 yamt }
488 1.5.4.2 yamt
489 1.5.4.2 yamt efs_dextent_to_extent((struct efs_dextent *)bp->b_data, &ex2);
490 1.5.4.3 yamt brelse(bp, 0);
491 1.5.4.2 yamt
492 1.5.4.2 yamt offset = ex2.ex_offset * EFS_BB_SIZE;
493 1.5.4.2 yamt
494 1.5.4.2 yamt if (offset > start_hint) {
495 1.5.4.2 yamt indir = MAX(0, i - 1);
496 1.5.4.2 yamt break;
497 1.5.4.2 yamt }
498 1.5.4.2 yamt
499 1.5.4.2 yamt /* number of extents prior to this indirect vector of extents */
500 1.5.4.2 yamt next += numextents;
501 1.5.4.2 yamt
502 1.5.4.2 yamt /* number of extents within this indirect vector of extents */
503 1.5.4.2 yamt numextents = ex.ex_length * EFS_EXTENTS_PER_BB;
504 1.5.4.2 yamt numextents = MIN(numextents, eip->ei_numextents - next);
505 1.5.4.2 yamt }
506 1.5.4.2 yamt
507 1.5.4.2 yamt /*
508 1.5.4.2 yamt * We hit the end, so assume it's in the last extent.
509 1.5.4.2 yamt */
510 1.5.4.2 yamt if (indir == -1)
511 1.5.4.2 yamt indir = numinextents - 1;
512 1.5.4.2 yamt
513 1.5.4.2 yamt /*
514 1.5.4.2 yamt * Binary search to find our desired direct extent.
515 1.5.4.2 yamt */
516 1.5.4.2 yamt lo = 0;
517 1.5.4.2 yamt mid = 0;
518 1.5.4.2 yamt hi = numextents - 1;
519 1.5.4.2 yamt efs_dextent_to_extent(&eip->ei_di.di_extents[indir], &ex);
520 1.5.4.2 yamt while (lo <= hi) {
521 1.5.4.2 yamt int bboff;
522 1.5.4.2 yamt int index;
523 1.5.4.2 yamt
524 1.5.4.2 yamt mid = (lo + hi) / 2;
525 1.5.4.2 yamt
526 1.5.4.2 yamt bboff = mid / EFS_EXTENTS_PER_BB;
527 1.5.4.2 yamt index = mid % EFS_EXTENTS_PER_BB;
528 1.5.4.2 yamt
529 1.5.4.2 yamt err = efs_bread(emp, ex.ex_bn + bboff, NULL, &bp);
530 1.5.4.2 yamt if (err) {
531 1.5.4.3 yamt brelse(bp, 0);
532 1.5.4.2 yamt EFS_DPRINTF(("efs_extent_iterator_init: bsrch read\n"));
533 1.5.4.2 yamt return;
534 1.5.4.2 yamt }
535 1.5.4.2 yamt
536 1.5.4.2 yamt efs_dextent_to_extent((struct efs_dextent *)bp->b_data + index,
537 1.5.4.2 yamt &ex2);
538 1.5.4.3 yamt brelse(bp, 0);
539 1.5.4.2 yamt
540 1.5.4.2 yamt offset = ex2.ex_offset * EFS_BB_SIZE;
541 1.5.4.2 yamt length = ex2.ex_length * EFS_BB_SIZE;
542 1.5.4.2 yamt
543 1.5.4.2 yamt if (start_hint >= offset && start_hint < (offset + length))
544 1.5.4.2 yamt break;
545 1.5.4.2 yamt
546 1.5.4.2 yamt if (start_hint < offset)
547 1.5.4.2 yamt hi = mid - 1;
548 1.5.4.2 yamt else
549 1.5.4.2 yamt lo = mid + 1;
550 1.5.4.2 yamt }
551 1.5.4.2 yamt
552 1.5.4.2 yamt /*
553 1.5.4.2 yamt * This is bad. Either the hint is bogus (which shouldn't
554 1.5.4.2 yamt * happen) or the extent list must be screwed up. We
555 1.5.4.2 yamt * have to abort.
556 1.5.4.2 yamt */
557 1.5.4.2 yamt if (lo > hi) {
558 1.5.4.2 yamt EFS_DPRINTF(("efs_extent_iterator_init: bsearch "
559 1.5.4.2 yamt "failed to find extent\n"));
560 1.5.4.2 yamt return;
561 1.5.4.2 yamt }
562 1.5.4.2 yamt
563 1.5.4.2 yamt exi->exi_next = next + mid;
564 1.5.4.2 yamt exi->exi_dnext = indir;
565 1.5.4.2 yamt exi->exi_innext = mid;
566 1.5.4.2 yamt }
567 1.5.4.2 yamt
568 1.5.4.2 yamt /*
569 1.5.4.2 yamt * Return the next EFS extent.
570 1.5.4.2 yamt *
571 1.5.4.2 yamt * Returns 0 if another extent was iterated, -1 if we've exhausted all
572 1.5.4.2 yamt * extents, or an error number. If 'exi' is non-NULL, the next extent is
573 1.5.4.2 yamt * written to it (should it exist).
574 1.5.4.2 yamt */
575 1.5.4.2 yamt int
576 1.5.4.2 yamt efs_extent_iterator_next(struct efs_extent_iterator *exi,
577 1.5.4.2 yamt struct efs_extent *exp)
578 1.5.4.2 yamt {
579 1.5.4.2 yamt struct efs_extent ex;
580 1.5.4.2 yamt struct efs_dextent *dexp;
581 1.5.4.2 yamt struct efs_inode *eip = exi->exi_eip;
582 1.5.4.2 yamt struct buf *bp;
583 1.5.4.2 yamt int err, bboff, index;
584 1.5.4.2 yamt
585 1.5.4.2 yamt if (exi->exi_next++ >= eip->ei_numextents)
586 1.5.4.2 yamt return (-1);
587 1.5.4.2 yamt
588 1.5.4.2 yamt /* direct or indirect extents? */
589 1.5.4.2 yamt if (eip->ei_numextents <= EFS_DIRECTEXTENTS) {
590 1.5.4.2 yamt if (exp != NULL) {
591 1.5.4.2 yamt dexp = &eip->ei_di.di_extents[exi->exi_dnext++];
592 1.5.4.2 yamt efs_dextent_to_extent(dexp, exp);
593 1.5.4.2 yamt }
594 1.5.4.2 yamt } else {
595 1.5.4.2 yamt efs_dextent_to_extent(
596 1.5.4.2 yamt &eip->ei_di.di_extents[exi->exi_dnext], &ex);
597 1.5.4.2 yamt
598 1.5.4.2 yamt bboff = exi->exi_innext / EFS_EXTENTS_PER_BB;
599 1.5.4.2 yamt index = exi->exi_innext % EFS_EXTENTS_PER_BB;
600 1.5.4.2 yamt
601 1.5.4.2 yamt err = efs_bread(VFSTOEFS(eip->ei_vp->v_mount),
602 1.5.4.2 yamt ex.ex_bn + bboff, NULL, &bp);
603 1.5.4.2 yamt if (err) {
604 1.5.4.2 yamt EFS_DPRINTF(("efs_extent_iterator_next: "
605 1.5.4.2 yamt "efs_bread failed: %d\n", err));
606 1.5.4.3 yamt brelse(bp, 0);
607 1.5.4.2 yamt return (err);
608 1.5.4.2 yamt }
609 1.5.4.2 yamt
610 1.5.4.2 yamt if (exp != NULL) {
611 1.5.4.2 yamt dexp = (struct efs_dextent *)bp->b_data + index;
612 1.5.4.2 yamt efs_dextent_to_extent(dexp, exp);
613 1.5.4.2 yamt }
614 1.5.4.3 yamt brelse(bp, 0);
615 1.5.4.2 yamt
616 1.5.4.2 yamt bboff = exi->exi_innext++ / EFS_EXTENTS_PER_BB;
617 1.5.4.2 yamt if (bboff >= ex.ex_length) {
618 1.5.4.2 yamt exi->exi_innext = 0;
619 1.5.4.2 yamt exi->exi_dnext++;
620 1.5.4.2 yamt }
621 1.5.4.2 yamt }
622 1.5.4.2 yamt
623 1.5.4.2 yamt return (0);
624 1.5.4.2 yamt }
625