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