Merge branch 'vendor/OPENSSL'
[dragonfly.git] / lib / libstand / ext2fs.c
1 /*-
2  * Copyright (c) 1999,2000 Jonathan Lemon <jlemon@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: src/lib/libstand/ext2fs.c,v 1.1.2.2 2001/03/05 06:26:07 kris Exp $
27  *      $DragonFly: src/lib/libstand/ext2fs.c,v 1.2 2003/06/17 04:26:51 dillon Exp $
28  */
29 /*-
30  * Copyright (c) 1993
31  *      The Regents of the University of California.  All rights reserved.
32  *
33  * This code is derived from software contributed to Berkeley by
34  * The Mach Operating System project at Carnegie-Mellon University.
35  *
36  * Redistribution and use in source and binary forms, with or without
37  * modification, are permitted provided that the following conditions
38  * are met:
39  * 1. Redistributions of source code must retain the above copyright
40  *    notice, this list of conditions and the following disclaimer.
41  * 2. Redistributions in binary form must reproduce the above copyright
42  *    notice, this list of conditions and the following disclaimer in the
43  *    documentation and/or other materials provided with the distribution.
44  * 3. Neither the name of the University nor the names of its contributors
45  *    may be used to endorse or promote products derived from this software
46  *    without specific prior written permission.
47  *
48  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58  * SUCH DAMAGE.
59  *  
60  *
61  * Copyright (c) 1990, 1991 Carnegie Mellon University
62  * All Rights Reserved.
63  *
64  * Author: David Golub
65  * 
66  * Permission to use, copy, modify and distribute this software and its
67  * documentation is hereby granted, provided that both the copyright
68  * notice and this permission notice appear in all copies of the
69  * software, derivative works or modified versions, and any portions
70  * thereof, and that both notices appear in supporting documentation.
71  * 
72  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
73  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
74  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
75  * 
76  * Carnegie Mellon requests users of this software to return to
77  * 
78  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
79  *  School of Computer Science
80  *  Carnegie Mellon University
81  *  Pittsburgh PA 15213-3890
82  * 
83  * any improvements or extensions that they make and grant Carnegie the
84  * rights to redistribute these changes.
85  */
86
87 #include <sys/param.h>
88 #include <sys/time.h>
89 #include "stand.h"
90 #include "string.h"
91
92 static int      ext2fs_open(const char *path, struct open_file *f);
93 static int      ext2fs_close(struct open_file *f);
94 static int      ext2fs_read(struct open_file *f, void *buf,
95                          size_t size, size_t *resid);
96 static off_t    ext2fs_seek(struct open_file *f, off_t offset, int where);
97 static int      ext2fs_stat(struct open_file *f, struct stat *sb);
98 static int      ext2fs_readdir(struct open_file *f, struct dirent *d);
99
100 static int dtmap[] = { DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR,
101                          DT_BLK, DT_FIFO, DT_SOCK, DT_LNK };
102 #define EXTFTODT(x)     (x) > sizeof(dtmap) / sizeof(dtmap[0]) ? \
103                         DT_UNKNOWN : dtmap[x]
104
105 struct fs_ops ext2fs_fsops = {
106         "ext2fs",
107         ext2fs_open,
108         ext2fs_close,
109         ext2fs_read,
110         null_write,
111         ext2fs_seek,
112         ext2fs_stat,
113         ext2fs_readdir
114 };
115
116 #define EXT2_SBSIZE     1024
117 #define EXT2_SBLOCK     (1024 / DEV_BSIZE)      /* block offset of superblock */
118 #define EXT2_MAGIC      0xef53
119 #define EXT2_ROOTINO    2
120
121 #define EXT2_REV0               0       /* original revision of ext2 */
122 #define EXT2_R0_ISIZE           128     /* inode size */
123 #define EXT2_R0_FIRSTINO        11      /* first inode */
124
125 #define EXT2_MINBSHIFT          10      /* mininum block shift */
126 #define EXT2_MINFSHIFT          10      /* mininum frag shift */
127
128 #define NDADDR          12              /* # of direct blocks */
129 #define NIADDR          3               /* # of indirect blocks */
130
131 /*
132  * file system block to disk address
133  */
134 #define fsb_to_db(fs, blk)      ((blk) << (fs)->fs_fsbtodb)
135
136 /*
137  * inode to block group offset
138  * inode to block group
139  * inode to disk address
140  * inode to block offset
141  */
142 #define ino_to_bgo(fs, ino)     (((ino) - 1) % (fs)->fs_ipg)
143 #define ino_to_bg(fs, ino)      (((ino) - 1) / (fs)->fs_ipg)
144 #define ino_to_db(fs, bg, ino) \
145         fsb_to_db(fs, ((bg)[ino_to_bg(fs, ino)].bg_inotbl + \
146             ino_to_bgo(fs, ino) / (fs)->fs_ipb))
147 #define ino_to_bo(fs, ino)      (ino_to_bgo(fs, ino) % (fs)->fs_ipb)
148
149 #define nindir(fs) \
150         ((fs)->fs_bsize / sizeof(u_int32_t))
151 #define lblkno(fs, loc)                         /* loc / bsize */ \
152         ((loc) >> (fs)->fs_bshift)
153 #define smalllblktosize(fs, blk)                /* blk * bsize */ \
154         ((blk) << (fs)->fs_bshift)
155 #define blkoff(fs, loc)                         /* loc % bsize */ \
156         ((loc) & (fs)->fs_bmask)
157 #define fragroundup(fs, size)                   /* roundup(size, fsize) */ \
158         (((size) + (fs)->fs_fmask) & ~(fs)->fs_fmask)
159 #define dblksize(fs, dip, lbn) \
160         (((lbn) >= NDADDR || (dip)->di_size >= smalllblktosize(fs, (lbn) + 1)) \
161             ? (fs)->fs_bsize \
162             : (fragroundup(fs, blkoff(fs, (dip)->di_size))))
163
164 /*
165  * superblock describing ext2fs
166  */
167 struct ext2fs_disk {
168         u_int32_t       fd_inodes;      /* # of inodes */
169         u_int32_t       fd_blocks;      /* # of blocks */
170         u_int32_t       fd_resblk;      /* # of reserved blocks */
171         u_int32_t       fd_freeblk;     /* # of free blocks */
172         u_int32_t       fd_freeino;     /* # of free inodes */
173         u_int32_t       fd_firstblk;    /* first data block */
174         u_int32_t       fd_bsize;       /* block size */
175         u_int32_t       fd_fsize;       /* frag size */
176         u_int32_t       fd_bpg;         /* blocks per group */
177         u_int32_t       fd_fpg;         /* frags per group */
178         u_int32_t       fd_ipg;         /* inodes per group */
179         u_int32_t       fd_mtime;       /* mount time */
180         u_int32_t       fd_wtime;       /* write time */
181         u_int16_t       fd_mount;       /* # of mounts */
182         int16_t         fd_maxmount;    /* max # of mounts */
183         u_int16_t       fd_magic;       /* magic number */
184         u_int16_t       fd_state;       /* state */
185         u_int16_t       fd_eflag;       /* error flags */
186         u_int16_t       fd_mnrrev;      /* minor revision */
187         u_int32_t       fd_lastchk;     /* last check */
188         u_int32_t       fd_chkintvl;    /* maximum check interval */
189         u_int32_t       fd_os;          /* os */
190         u_int32_t       fd_revision;    /* revision */
191         u_int16_t       fd_uid;         /* uid for reserved blocks */
192         u_int16_t       fd_gid;         /* gid for reserved blocks */
193
194         u_int32_t       fd_firstino;    /* first non-reserved inode */
195         u_int16_t       fd_isize;       /* inode size */
196         u_int16_t       fd_nblkgrp;     /* block group # of superblock */
197         u_int32_t       fd_fcompat;     /* compatible features */
198         u_int32_t       fd_fincompat;   /* incompatible features */
199         u_int32_t       fd_frocompat;   /* read-only compatibilties */
200         u_int8_t        fd_uuid[16];    /* volume uuid */
201         char            fd_volname[16]; /* volume name */
202         char            fd_fsmnt[64];   /* name last mounted on */
203         u_int32_t       fd_bitmap;      /* compression bitmap */
204
205         u_int8_t        fd_nblkpa;      /* # of blocks to preallocate */        
206         u_int8_t        fd_ndblkpa;     /* # of dir blocks to preallocate */
207 };
208
209 struct ext2fs_core {
210         int             fc_bsize;       /* block size */
211         int             fc_bshift;      /* block shift amount */
212         int             fc_bmask;       /* block mask */
213         int             fc_fsize;       /* frag size */
214         int             fc_fshift;      /* frag shift amount */
215         int             fc_fmask;       /* frag mask */
216         int             fc_isize;       /* inode size */
217         int             fc_imask;       /* inode mask */
218         int             fc_firstino;    /* first non-reserved inode */
219         int             fc_ipb;         /* inodes per block */
220         int             fc_fsbtodb;     /* fsb to ds shift */
221 };
222
223 struct ext2fs {
224         struct          ext2fs_disk fs_fd;
225         char            fs_pad[EXT2_SBSIZE - sizeof(struct ext2fs_disk)];
226         struct          ext2fs_core fs_fc;
227
228 #define fs_magic        fs_fd.fd_magic
229 #define fs_revision     fs_fd.fd_revision
230 #define fs_blocks       fs_fd.fd_blocks
231 #define fs_firstblk     fs_fd.fd_firstblk
232 #define fs_bpg          fs_fd.fd_bpg
233 #define fs_ipg          fs_fd.fd_ipg
234             
235 #define fs_bsize        fs_fc.fc_bsize
236 #define fs_bshift       fs_fc.fc_bshift
237 #define fs_bmask        fs_fc.fc_bmask
238 #define fs_fsize        fs_fc.fc_fsize
239 #define fs_fshift       fs_fc.fc_fshift
240 #define fs_fmask        fs_fc.fc_fmask
241 #define fs_isize        fs_fc.fc_isize
242 #define fs_imask        fs_fc.fc_imask
243 #define fs_firstino     fs_fc.fc_firstino
244 #define fs_ipb          fs_fc.fc_ipb
245 #define fs_fsbtodb      fs_fc.fc_fsbtodb
246 };
247
248 struct ext2blkgrp {
249         u_int32_t       bg_blkmap;      /* block bitmap */
250         u_int32_t       bg_inomap;      /* inode bitmap */
251         u_int32_t       bg_inotbl;      /* inode table */
252         u_int16_t       bg_nfblk;       /* # of free blocks */
253         u_int16_t       bg_nfino;       /* # of free inodes */
254         u_int16_t       bg_ndirs;       /* # of dirs */
255         char            bg_pad[14];
256 };
257
258 struct ext2dinode {
259         u_int16_t       di_mode;        /* mode */
260         u_int16_t       di_uid;         /* uid */
261         u_int32_t       di_size;        /* byte size */
262         u_int32_t       di_atime;       /* access time */
263         u_int32_t       di_ctime;       /* creation time */
264         u_int32_t       di_mtime;       /* modification time */
265         u_int32_t       di_dtime;       /* deletion time */
266         u_int16_t       di_gid;         /* gid */
267         u_int16_t       di_nlink;       /* link count */
268         u_int32_t       di_nblk;        /* block count */
269         u_int32_t       di_flags;       /* file flags */
270
271         u_int32_t       di_osdep1;      /* os dependent stuff */
272
273         u_int32_t       di_db[NDADDR];  /* direct blocks */
274         u_int32_t       di_ib[NIADDR];  /* indirect blocks */
275         u_int32_t       di_version;     /* version */
276         u_int32_t       di_facl;        /* file acl */
277         u_int32_t       di_dacl;        /* dir acl */
278         u_int32_t       di_faddr;       /* fragment addr */
279
280         u_int8_t        di_frag;        /* fragment number */
281         u_int8_t        di_fsize;       /* fragment size */
282
283         char            di_pad[10];
284
285 #define di_shortlink    di_db
286 };
287
288 #define EXT2_MAXNAMLEN       255
289
290 struct ext2dirent {
291         u_int32_t       d_ino;          /* inode */
292         u_int16_t       d_reclen;       /* directory entry length */
293         u_int8_t        d_namlen;       /* name length */
294         u_int8_t        d_type;         /* file type */
295         char            d_name[EXT2_MAXNAMLEN];
296 };
297
298 struct file {
299         off_t           f_seekp;                /* seek pointer */
300         struct          ext2fs *f_fs;           /* pointer to super-block */
301         struct          ext2blkgrp *f_bg;       /* pointer to blkgrp map */
302         struct          ext2dinode f_di;        /* copy of on-disk inode */
303         int             f_nindir[NIADDR];       /* number of blocks mapped by
304                                                    indirect block at level i */
305         char            *f_blk[NIADDR];         /* buffer for indirect block
306                                                    at level i */
307         size_t          f_blksize[NIADDR];      /* size of buffer */
308         daddr_t         f_blkno[NIADDR];        /* disk address of block in
309                                                    buffer */
310         char            *f_buf;                 /* buffer for data block */
311         size_t          f_buf_size;             /* size of data block */
312         daddr_t         f_buf_blkno;            /* block number of data block */
313 };
314
315 /* forward decls */
316 static int      read_inode(ino_t inumber, struct open_file *f);
317 static int      block_map(struct open_file *f, daddr_t file_block,
318                     daddr_t *disk_block_p);
319 static int      buf_read_file(struct open_file *f, char **buf_p,
320                     size_t *size_p);
321 static int      search_directory(char *name, struct open_file *f,
322                     ino_t *inumber_p);
323
324 /*
325  * Open a file.
326  */
327 static int
328 ext2fs_open(const char *upath, struct open_file *f)
329 {
330         struct file *fp;
331         struct ext2fs *fs;
332         size_t buf_size;
333         ino_t inumber, parent_inumber;
334         int i, len, groups, bg_per_blk, blkgrps, mult;
335         int nlinks = 0;
336         int error = 0;
337         char *cp, *ncp, *path = NULL, *buf = NULL;
338         char namebuf[MAXPATHLEN+1];
339         char c;
340
341         /* allocate file system specific data structure */
342         fp = malloc(sizeof(struct file));
343         if (fp == NULL)
344                 return (ENOMEM);
345         bzero(fp, sizeof(struct file));
346         f->f_fsdata = (void *)fp;
347
348         /* allocate space and read super block */
349         fs = (struct ext2fs *)malloc(sizeof(*fs));
350         fp->f_fs = fs;
351         twiddle();
352         error = (f->f_dev->dv_strategy)(f->f_devdata, F_READ,
353             EXT2_SBLOCK, EXT2_SBSIZE, (char *)fs, &buf_size);
354         if (error)
355                 goto out;
356
357         if (buf_size != EXT2_SBSIZE || fs->fs_magic != EXT2_MAGIC) {
358                 error = EINVAL;
359                 goto out;
360         }
361
362         /*
363          * compute in-core values for the superblock
364          */
365         fs->fs_bshift = EXT2_MINBSHIFT + fs->fs_fd.fd_bsize;
366         fs->fs_bsize = 1 << fs->fs_bshift;
367         fs->fs_bmask = fs->fs_bsize - 1;
368
369         fs->fs_fshift = EXT2_MINFSHIFT + fs->fs_fd.fd_fsize;
370         fs->fs_fsize = 1 << fs->fs_fshift;
371         fs->fs_fmask = fs->fs_fsize - 1;
372
373         if (fs->fs_revision == EXT2_REV0) {
374                 fs->fs_isize = EXT2_R0_ISIZE;
375                 fs->fs_firstino = EXT2_R0_FIRSTINO;
376         } else {
377                 fs->fs_isize = fs->fs_fd.fd_isize;
378                 fs->fs_firstino = fs->fs_fd.fd_firstino;
379         }
380         fs->fs_imask = fs->fs_isize - 1;
381         fs->fs_ipb = fs->fs_bsize / fs->fs_isize;
382         fs->fs_fsbtodb = (fs->fs_bsize / DEV_BSIZE) - 1;
383
384         /*
385          * we have to load in the "group descriptors" here
386          */
387         groups = howmany(fs->fs_blocks - fs->fs_firstblk, fs->fs_bpg);
388         bg_per_blk = fs->fs_bsize / sizeof(struct ext2blkgrp);
389         blkgrps = howmany(groups, bg_per_blk);
390         len = blkgrps * fs->fs_bsize;
391
392         fp->f_bg = malloc(len);
393         twiddle();
394         error = (f->f_dev->dv_strategy)(f->f_devdata, F_READ,
395             EXT2_SBLOCK + EXT2_SBSIZE / DEV_BSIZE, len,
396             (char *)fp->f_bg, &buf_size);
397         if (error)
398                 goto out;
399
400         /*
401          * XXX
402          * validation of values?  (blocksize, descriptors, etc?)
403          */
404
405         /*
406          * Calculate indirect block levels.
407          */
408         mult = 1;
409         for (i = 0; i < NIADDR; i++) {
410                 mult *= nindir(fs);
411                 fp->f_nindir[i] = mult;
412         }
413
414         inumber = EXT2_ROOTINO;
415         if ((error = read_inode(inumber, f)) != 0)
416                 goto out;
417
418         path = strdup(upath);
419         if (path == NULL) {
420                 error = ENOMEM;
421                 goto out;
422         }
423         cp = path;
424         while (*cp) {
425                 /*
426                  * Remove extra separators
427                  */
428                 while (*cp == '/')
429                         cp++;
430                 if (*cp == '\0')
431                         break;
432
433                 /*
434                  * Check that current node is a directory.
435                  */
436                 if (! S_ISDIR(fp->f_di.di_mode)) {
437                         error = ENOTDIR;
438                         goto out;
439                 }
440
441                 /*
442                  * Get next component of path name.
443                  */
444                 len = 0;
445
446                 ncp = cp;
447                 while ((c = *cp) != '\0' && c != '/') {
448                         if (++len > EXT2_MAXNAMLEN) {
449                                 error = ENOENT;
450                                 goto out;
451                         }
452                         cp++;
453                 }
454                 *cp = '\0';
455
456                 /*
457                  * Look up component in current directory.
458                  * Save directory inumber in case we find a
459                  * symbolic link.
460                  */
461                 parent_inumber = inumber;
462                 error = search_directory(ncp, f, &inumber);
463                 *cp = c;
464                 if (error)
465                         goto out;
466
467                 /*
468                  * Open next component.
469                  */
470                 if ((error = read_inode(inumber, f)) != 0)
471                         goto out;
472
473                 /*
474                  * Check for symbolic link.
475                  */
476                 if (S_ISLNK(fp->f_di.di_mode)) {
477                         int link_len = fp->f_di.di_size;
478                         int len;
479
480                         len = strlen(cp);
481                         if (link_len + len > MAXPATHLEN ||
482                             ++nlinks > MAXSYMLINKS) {
483                                 error = ENOENT;
484                                 goto out;
485                         }
486
487                         bcopy(cp, &namebuf[link_len], len + 1);
488                         if (fp->f_di.di_nblk == 0) {
489                                 bcopy(fp->f_di.di_shortlink,
490                                     namebuf, link_len);
491                         } else {
492                                 /*
493                                  * Read file for symbolic link
494                                  */
495                                 struct ext2fs *fs = fp->f_fs;
496                                 daddr_t disk_block;
497                                 size_t buf_size;
498
499                                 if (! buf)
500                                         buf = malloc(fs->fs_bsize);
501                                 error = block_map(f, (daddr_t)0, &disk_block);
502                                 if (error)
503                                         goto out;
504                                 
505                                 twiddle();
506                                 error = (f->f_dev->dv_strategy)(f->f_devdata,
507                                     F_READ, fsb_to_db(fs, disk_block),
508                                     fs->fs_bsize, buf, &buf_size);
509                                 if (error)
510                                         goto out;
511
512                                 bcopy((char *)buf, namebuf, link_len);
513                         }
514
515                         /*
516                          * If relative pathname, restart at parent directory.
517                          * If absolute pathname, restart at root.
518                          */
519                         cp = namebuf;
520                         if (*cp != '/')
521                                 inumber = parent_inumber;
522                         else
523                                 inumber = (ino_t)EXT2_ROOTINO;
524
525                         if ((error = read_inode(inumber, f)) != 0)
526                                 goto out;
527                 }
528         }
529
530         /*
531          * Found terminal component.
532          */
533         error = 0;
534 out:
535         if (buf)
536                 free(buf);
537         if (path)
538                 free(path);
539         if (error) {
540                 f->f_fsdata = NULL;
541                 if (fp->f_buf)
542                         free(fp->f_buf);
543                 free(fp->f_fs);
544                 free(fp);
545         }
546         return (error);
547 }
548
549 /*
550  * Read a new inode into a file structure.
551  */
552 static int
553 read_inode(ino_t inumber, struct open_file *f)
554 {
555         struct file *fp = (struct file *)f->f_fsdata;
556         struct ext2fs *fs = fp->f_fs;
557         struct ext2dinode *dp;
558         char *buf;
559         size_t rsize;
560         int level, error = 0;
561
562         /*
563          * Read inode and save it.
564          */
565         buf = malloc(fs->fs_bsize);
566         twiddle();
567         error = (f->f_dev->dv_strategy)(f->f_devdata, F_READ,
568             ino_to_db(fs, fp->f_bg, inumber), fs->fs_bsize, buf, &rsize);
569         if (error)
570                 goto out;
571         if (rsize != fs->fs_bsize) {
572                 error = EIO;
573                 goto out;
574         }
575
576         dp = (struct ext2dinode *)buf;
577         fp->f_di = dp[ino_to_bo(fs, inumber)];
578
579         /* clear out old buffers */
580         for (level = 0; level < NIADDR; level++)
581                 fp->f_blkno[level] = -1;
582         fp->f_buf_blkno = -1;
583
584 out:
585         free(buf);
586         return (error);  
587 }
588
589 /*
590  * Given an offset in a file, find the disk block number that
591  * contains that block.
592  */
593 static int
594 block_map(struct open_file *f, daddr_t file_block, daddr_t *disk_block_p)
595 {
596         struct file *fp = (struct file *)f->f_fsdata;
597         struct ext2fs *fs = fp->f_fs;
598         daddr_t ind_block_num;
599         daddr_t *ind_p;
600         int idx, level;
601         int error;
602
603         /*
604          * Index structure of an inode:
605          *
606          * di_db[0..NDADDR-1]   hold block numbers for blocks
607          *                      0..NDADDR-1
608          *
609          * di_ib[0]             index block 0 is the single indirect block
610          *                      holds block numbers for blocks
611          *                      NDADDR .. NDADDR + NINDIR(fs)-1
612          *
613          * di_ib[1]             index block 1 is the double indirect block
614          *                      holds block numbers for INDEX blocks for blocks
615          *                      NDADDR + NINDIR(fs) ..
616          *                      NDADDR + NINDIR(fs) + NINDIR(fs)**2 - 1
617          *
618          * di_ib[2]             index block 2 is the triple indirect block
619          *                      holds block numbers for double-indirect
620          *                      blocks for blocks
621          *                      NDADDR + NINDIR(fs) + NINDIR(fs)**2 ..
622          *                      NDADDR + NINDIR(fs) + NINDIR(fs)**2
623          *                              + NINDIR(fs)**3 - 1
624          */
625
626         if (file_block < NDADDR) {
627                 /* Direct block. */
628                 *disk_block_p = fp->f_di.di_db[file_block];
629                 return (0);
630         }
631
632         file_block -= NDADDR;
633
634         /*
635          * nindir[0] = NINDIR
636          * nindir[1] = NINDIR**2
637          * nindir[2] = NINDIR**3
638          *      etc
639          */
640         for (level = 0; level < NIADDR; level++) {
641                 if (file_block < fp->f_nindir[level])
642                         break;
643                 file_block -= fp->f_nindir[level];
644         }
645         if (level == NIADDR) {
646                 /* Block number too high */
647                 return (EFBIG);
648         }
649
650         ind_block_num = fp->f_di.di_ib[level];
651
652         for (; level >= 0; level--) {
653                 if (ind_block_num == 0) {
654                         *disk_block_p = 0;      /* missing */
655                         return (0);
656                 }
657
658                 if (fp->f_blkno[level] != ind_block_num) {
659                         if (fp->f_blk[level] == NULL)
660                                 fp->f_blk[level] =
661                                         malloc(fs->fs_bsize);
662                         twiddle();
663                         error = (f->f_dev->dv_strategy)(f->f_devdata, F_READ,
664                             fsb_to_db(fp->f_fs, ind_block_num), fs->fs_bsize,
665                             fp->f_blk[level], &fp->f_blksize[level]);
666                         if (error)
667                                 return (error);
668                         if (fp->f_blksize[level] != fs->fs_bsize)
669                                 return (EIO);
670                         fp->f_blkno[level] = ind_block_num;
671                 }
672
673                 ind_p = (daddr_t *)fp->f_blk[level];
674
675                 if (level > 0) {
676                         idx = file_block / fp->f_nindir[level - 1];
677                         file_block %= fp->f_nindir[level - 1];
678                 } else {
679                         idx = file_block;
680                 }
681                 ind_block_num = ind_p[idx];
682         }
683
684         *disk_block_p = ind_block_num;
685
686         return (0);
687 }
688
689 /*
690  * Read a portion of a file into an internal buffer.  Return
691  * the location in the buffer and the amount in the buffer.
692  */
693 static int
694 buf_read_file(struct open_file *f, char **buf_p, size_t *size_p)
695 {
696         struct file *fp = (struct file *)f->f_fsdata;
697         struct ext2fs *fs = fp->f_fs;
698         long off;
699         daddr_t file_block;
700         daddr_t disk_block;
701         size_t block_size;
702         int error = 0;
703
704         off = blkoff(fs, fp->f_seekp);
705         file_block = lblkno(fs, fp->f_seekp);
706         block_size = dblksize(fs, &fp->f_di, file_block);
707
708         if (file_block != fp->f_buf_blkno) {
709                 error = block_map(f, file_block, &disk_block);
710                 if (error)
711                         goto done;
712
713                 if (fp->f_buf == NULL)
714                         fp->f_buf = malloc(fs->fs_bsize);
715
716                 if (disk_block == 0) {
717                         bzero(fp->f_buf, block_size);
718                         fp->f_buf_size = block_size;
719                 } else {
720                         twiddle();
721                         error = (f->f_dev->dv_strategy)(f->f_devdata, F_READ,
722                             fsb_to_db(fs, disk_block), block_size,
723                             fp->f_buf, &fp->f_buf_size);
724                         if (error)
725                                 goto done;
726                 }
727                 fp->f_buf_blkno = file_block;
728         }
729
730         /*
731          * Return address of byte in buffer corresponding to
732          * offset, and size of remainder of buffer after that
733          * byte.
734          */
735         *buf_p = fp->f_buf + off;
736         *size_p = block_size - off;
737
738         /*
739          * But truncate buffer at end of file.
740          */
741         if (*size_p > fp->f_di.di_size - fp->f_seekp)
742                 *size_p = fp->f_di.di_size - fp->f_seekp;
743 done:
744         return (error);
745 }
746
747 /*
748  * Search a directory for a name and return its
749  * i_number.
750  */
751 static int
752 search_directory(char *name, struct open_file *f, ino_t *inumber_p)
753 {
754         struct file *fp = (struct file *)f->f_fsdata;
755         struct ext2dirent *dp, *edp;
756         char *buf;
757         size_t buf_size;
758         int namlen, length;
759         int error;
760
761         length = strlen(name);
762         fp->f_seekp = 0;
763         while (fp->f_seekp < fp->f_di.di_size) {
764                 error = buf_read_file(f, &buf, &buf_size);
765                 if (error)
766                         return (error);
767                 dp = (struct ext2dirent *)buf;
768                 edp = (struct ext2dirent *)(buf + buf_size);
769                 while (dp < edp) {
770                         if (dp->d_ino == (ino_t)0)
771                                 goto next;
772                         namlen = dp->d_namlen;
773                         if (namlen == length &&
774                             strncmp(name, dp->d_name, length) == 0) {
775                                 /* found entry */
776                                 *inumber_p = dp->d_ino;
777                                 return (0);
778                         }
779                 next:
780                         dp = (struct ext2dirent *)((char *)dp + dp->d_reclen);
781                 }
782                 fp->f_seekp += buf_size;
783         }
784         return (ENOENT);
785 }
786
787 static int
788 ext2fs_close(struct open_file *f)
789 {
790         struct file *fp = (struct file *)f->f_fsdata;
791         int level;
792
793         f->f_fsdata = NULL;
794         if (fp == NULL)
795                 return (0);
796
797         for (level = 0; level < NIADDR; level++) {
798                 if (fp->f_blk[level])
799                         free(fp->f_blk[level]);
800         }
801         if (fp->f_buf)
802                 free(fp->f_buf);
803         if (fp->f_bg)
804                 free(fp->f_bg);
805         free(fp->f_fs);
806         free(fp);
807         return (0);
808 }
809
810 static int
811 ext2fs_read(struct open_file *f, void *addr, size_t size, size_t *resid)
812 {
813         struct file *fp = (struct file *)f->f_fsdata;
814         size_t csize, buf_size;
815         char *buf;
816         int error = 0;
817
818         while (size != 0) {
819                 if (fp->f_seekp >= fp->f_di.di_size)
820                         break;
821
822                 error = buf_read_file(f, &buf, &buf_size);
823                 if (error)
824                         break;
825
826                 csize = size;
827                 if (csize > buf_size)
828                         csize = buf_size;
829
830                 bcopy(buf, addr, csize);
831
832                 fp->f_seekp += csize;
833                 addr += csize;
834                 size -= csize;
835         }
836         if (resid)
837                 *resid = size;
838         return (error);
839 }
840
841 static off_t
842 ext2fs_seek(struct open_file *f, off_t offset, int where)
843 {
844         struct file *fp = (struct file *)f->f_fsdata;
845
846         switch (where) {
847         case SEEK_SET:
848                 fp->f_seekp = offset;
849                 break;
850         case SEEK_CUR:
851                 fp->f_seekp += offset;
852                 break;
853         case SEEK_END:
854                 fp->f_seekp = fp->f_di.di_size - offset;
855                 break;
856         default:
857                 return (-1);
858         }
859         return (fp->f_seekp);
860 }
861
862 static int
863 ext2fs_stat(struct open_file *f, struct stat *sb)
864 {
865         struct file *fp = (struct file *)f->f_fsdata;
866
867         /* only important stuff */
868         sb->st_mode = fp->f_di.di_mode;
869         sb->st_uid = fp->f_di.di_uid;
870         sb->st_gid = fp->f_di.di_gid;
871         sb->st_size = fp->f_di.di_size;
872         return (0);
873 }
874
875 static int
876 ext2fs_readdir(struct open_file *f, struct dirent *d)
877 {
878         struct file *fp = (struct file *)f->f_fsdata;
879         struct ext2dirent *ed;
880         char *buf;
881         size_t buf_size;
882         int error;
883
884         /*
885          * assume that a directory entry will not be split across blocks
886          */
887 again:
888         if (fp->f_seekp >= fp->f_di.di_size)
889                 return (ENOENT);
890         error = buf_read_file(f, &buf, &buf_size);
891         if (error)
892                 return (error);
893         ed = (struct ext2dirent *)buf;
894         fp->f_seekp += ed->d_reclen;
895         if (ed->d_ino == (ino_t)0)
896                 goto again;
897         d->d_type = EXTFTODT(ed->d_type);
898         strncpy(d->d_name, ed->d_name, ed->d_namlen);
899         d->d_name[ed->d_namlen] = '\0';
900         return (0);
901 }