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