1 /* $FreeBSD: src/lib/libstand/ufs.c,v 1.5.6.1 2000/05/04 13:47:53 ps Exp $ */
2 /* $NetBSD: ufs.c,v 1.20 1998/03/01 07:15:39 ross Exp $ */
6 * The Regents of the University of California. All rights reserved.
8 * This code is derived from software contributed to Berkeley by
9 * The Mach Operating System project at Carnegie-Mellon University.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 * Copyright (c) 1990, 1991 Carnegie Mellon University
41 * All Rights Reserved.
45 * Permission to use, copy, modify and distribute this software and its
46 * documentation is hereby granted, provided that both the copyright
47 * notice and this permission notice appear in all copies of the
48 * software, derivative works or modified versions, and any portions
49 * thereof, and that both notices appear in supporting documentation.
51 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
52 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
53 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
55 * Carnegie Mellon requests users of this software to return to
57 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
58 * School of Computer Science
59 * Carnegie Mellon University
60 * Pittsburgh PA 15213-3890
62 * any improvements or extensions that they make and grant Carnegie the
63 * rights to redistribute these changes.
67 * Stand-alone file reading package.
70 #include <sys/param.h>
72 #include <ufs/ufs/dinode.h>
73 #include <ufs/ufs/dir.h>
74 #include <ufs/ffs/fs.h>
79 #define COMPAT_UFS /* DUX has old format file systems */
82 static int ufs_open(const char *path, struct open_file *f);
83 static int ufs_close(struct open_file *f);
84 static int ufs_read(struct open_file *f, void *buf, size_t size, size_t *resid);
85 static off_t ufs_seek(struct open_file *f, off_t offset, int where);
86 static int ufs_stat(struct open_file *f, struct stat *sb);
87 static int ufs_readdir(struct open_file *f, struct dirent *d);
89 struct fs_ops ufs_fsops = {
104 off_t f_seekp; /* seek pointer */
105 struct fs *f_fs; /* pointer to super-block */
106 struct dinode f_di; /* copy of on-disk inode */
107 int f_nindir[NIADDR];
108 /* number of blocks mapped by
109 indirect block at level i */
110 char *f_blk[NIADDR]; /* buffer for indirect block at
112 size_t f_blksize[NIADDR];
114 daddr_t f_blkno[NIADDR];/* disk address of block in buffer */
115 char *f_buf; /* buffer for data block */
116 size_t f_buf_size; /* size of data block */
117 daddr_t f_buf_blkno; /* block number of data block */
120 static int read_inode(ino_t, struct open_file *);
121 static int block_map(struct open_file *, daddr_t, daddr_t *);
122 static int buf_read_file(struct open_file *, char **, size_t *);
123 static int search_directory(char *, struct open_file *, ino_t *);
125 static void ffs_oldfscompat(struct fs *);
129 * Read a new inode into a file structure.
132 read_inode(inumber, f)
136 register struct file *fp = (struct file *)f->f_fsdata;
137 register struct fs *fs = fp->f_fs;
146 * Read inode and save it.
148 buf = malloc(fs->fs_bsize);
150 rc = (f->f_dev->dv_strategy)(f->f_devdata, F_READ,
151 fsbtodb(fs, ino_to_fsba(fs, inumber)), fs->fs_bsize,
155 if (rsize != fs->fs_bsize) {
161 register struct dinode *dp;
163 dp = (struct dinode *)buf;
164 fp->f_di = dp[ino_to_fsbo(fs, inumber)];
168 * Clear out the old buffers
173 for (level = 0; level < NIADDR; level++)
174 fp->f_blkno[level] = -1;
175 fp->f_buf_blkno = -1;
183 * Given an offset in a file, find the disk block number that
184 * contains that block.
187 block_map(f, file_block, disk_block_p)
190 daddr_t *disk_block_p; /* out */
192 register struct file *fp = (struct file *)f->f_fsdata;
193 register struct fs *fs = fp->f_fs;
196 daddr_t ind_block_num;
201 * Index structure of an inode:
203 * di_db[0..NDADDR-1] hold block numbers for blocks
206 * di_ib[0] index block 0 is the single indirect block
207 * holds block numbers for blocks
208 * NDADDR .. NDADDR + NINDIR(fs)-1
210 * di_ib[1] index block 1 is the double indirect block
211 * holds block numbers for INDEX blocks for blocks
212 * NDADDR + NINDIR(fs) ..
213 * NDADDR + NINDIR(fs) + NINDIR(fs)**2 - 1
215 * di_ib[2] index block 2 is the triple indirect block
216 * holds block numbers for double-indirect
218 * NDADDR + NINDIR(fs) + NINDIR(fs)**2 ..
219 * NDADDR + NINDIR(fs) + NINDIR(fs)**2
220 * + NINDIR(fs)**3 - 1
223 if (file_block < NDADDR) {
225 *disk_block_p = fp->f_di.di_db[file_block];
229 file_block -= NDADDR;
233 * nindir[1] = NINDIR**2
234 * nindir[2] = NINDIR**3
237 for (level = 0; level < NIADDR; level++) {
238 if (file_block < fp->f_nindir[level])
240 file_block -= fp->f_nindir[level];
242 if (level == NIADDR) {
243 /* Block number too high */
247 ind_block_num = fp->f_di.di_ib[level];
249 for (; level >= 0; level--) {
250 if (ind_block_num == 0) {
251 *disk_block_p = 0; /* missing */
255 if (fp->f_blkno[level] != ind_block_num) {
256 if (fp->f_blk[level] == (char *)0)
258 malloc(fs->fs_bsize);
260 rc = (f->f_dev->dv_strategy)(f->f_devdata, F_READ,
261 fsbtodb(fp->f_fs, ind_block_num),
264 &fp->f_blksize[level]);
267 if (fp->f_blksize[level] != fs->fs_bsize)
269 fp->f_blkno[level] = ind_block_num;
272 ind_p = (daddr_t *)fp->f_blk[level];
275 idx = file_block / fp->f_nindir[level - 1];
276 file_block %= fp->f_nindir[level - 1];
280 ind_block_num = ind_p[idx];
283 *disk_block_p = ind_block_num;
289 * Read a portion of a file into an internal buffer. Return
290 * the location in the buffer and the amount in the buffer.
293 buf_read_file(f, buf_p, size_p)
295 char **buf_p; /* out */
296 size_t *size_p; /* out */
298 register struct file *fp = (struct file *)f->f_fsdata;
299 register struct fs *fs = fp->f_fs;
301 register daddr_t file_block;
306 off = blkoff(fs, fp->f_seekp);
307 file_block = lblkno(fs, fp->f_seekp);
308 block_size = dblksize(fs, &fp->f_di, file_block);
310 if (file_block != fp->f_buf_blkno) {
311 rc = block_map(f, file_block, &disk_block);
315 if (fp->f_buf == (char *)0)
316 fp->f_buf = malloc(fs->fs_bsize);
318 if (disk_block == 0) {
319 bzero(fp->f_buf, block_size);
320 fp->f_buf_size = block_size;
323 rc = (f->f_dev->dv_strategy)(f->f_devdata, F_READ,
324 fsbtodb(fs, disk_block),
325 block_size, fp->f_buf, &fp->f_buf_size);
330 fp->f_buf_blkno = file_block;
334 * Return address of byte in buffer corresponding to
335 * offset, and size of remainder of buffer after that
338 *buf_p = fp->f_buf + off;
339 *size_p = block_size - off;
342 * But truncate buffer at end of file.
344 if (*size_p > fp->f_di.di_size - fp->f_seekp)
345 *size_p = fp->f_di.di_size - fp->f_seekp;
351 * Search a directory for a name and return its
355 search_directory(name, f, inumber_p)
358 ino_t *inumber_p; /* out */
360 register struct file *fp = (struct file *)f->f_fsdata;
361 register struct direct *dp;
368 length = strlen(name);
371 while (fp->f_seekp < fp->f_di.di_size) {
372 rc = buf_read_file(f, &buf, &buf_size);
376 dp = (struct direct *)buf;
377 edp = (struct direct *)(buf + buf_size);
379 if (dp->d_ino == (ino_t)0)
381 #if BYTE_ORDER == LITTLE_ENDIAN
382 if (fp->f_fs->fs_maxsymlinklen <= 0)
386 namlen = dp->d_namlen;
387 if (namlen == length &&
388 !strcmp(name, dp->d_name)) {
390 *inumber_p = dp->d_ino;
394 dp = (struct direct *)((char *)dp + dp->d_reclen);
396 fp->f_seekp += buf_size;
409 register char *cp, *ncp;
411 ino_t inumber, parent_inumber;
417 char namebuf[MAXPATHLEN+1];
421 /* allocate file system specific data structure */
422 fp = malloc(sizeof(struct file));
423 bzero(fp, sizeof(struct file));
424 f->f_fsdata = (void *)fp;
426 /* allocate space and read super block */
430 rc = (f->f_dev->dv_strategy)(f->f_devdata, F_READ,
431 SBLOCK, SBSIZE, (char *)fs, &buf_size);
435 if (buf_size != SBSIZE || fs->fs_magic != FS_MAGIC ||
436 fs->fs_bsize > MAXBSIZE || fs->fs_bsize < sizeof(struct fs)) {
445 * Calculate indirect block levels.
452 for (level = 0; level < NIADDR; level++) {
454 fp->f_nindir[level] = mult;
459 if ((rc = read_inode(inumber, f)) != 0)
462 cp = path = strdup(upath);
470 * Remove extra separators
478 * Check that current node is a directory.
480 if ((fp->f_di.di_mode & IFMT) != IFDIR) {
486 * Get next component of path name.
489 register int len = 0;
492 while ((c = *cp) != '\0' && c != '/') {
493 if (++len > MAXNAMLEN) {
503 * Look up component in current directory.
504 * Save directory inumber in case we find a
507 parent_inumber = inumber;
508 rc = search_directory(ncp, f, &inumber);
514 * Open next component.
516 if ((rc = read_inode(inumber, f)) != 0)
520 * Check for symbolic link.
522 if ((fp->f_di.di_mode & IFMT) == IFLNK) {
523 int link_len = fp->f_di.di_size;
528 if (link_len + len > MAXPATHLEN ||
529 ++nlinks > MAXSYMLINKS) {
534 bcopy(cp, &namebuf[link_len], len + 1);
536 if (link_len < fs->fs_maxsymlinklen) {
537 bcopy(fp->f_di.di_shortlink, namebuf,
538 (unsigned) link_len);
541 * Read file for symbolic link
545 register struct fs *fs = fp->f_fs;
548 buf = malloc(fs->fs_bsize);
549 rc = block_map(f, (daddr_t)0, &disk_block);
554 rc = (f->f_dev->dv_strategy)(f->f_devdata,
555 F_READ, fsbtodb(fs, disk_block),
556 fs->fs_bsize, buf, &buf_size);
560 bcopy((char *)buf, namebuf, (unsigned)link_len);
564 * If relative pathname, restart at parent directory.
565 * If absolute pathname, restart at root.
569 inumber = parent_inumber;
571 inumber = (ino_t)ROOTINO;
573 if ((rc = read_inode(inumber, f)) != 0)
579 * Found terminal component.
600 register struct file *fp = (struct file *)f->f_fsdata;
603 f->f_fsdata = (void *)0;
604 if (fp == (struct file *)0)
607 for (level = 0; level < NIADDR; level++) {
608 if (fp->f_blk[level])
609 free(fp->f_blk[level]);
619 * Copy a portion of a file into kernel memory.
620 * Cross block boundaries when necessary.
623 ufs_read(f, start, size, resid)
627 size_t *resid; /* out */
629 register struct file *fp = (struct file *)f->f_fsdata;
630 register size_t csize;
634 register char *addr = start;
637 if (fp->f_seekp >= fp->f_di.di_size)
640 rc = buf_read_file(f, &buf, &buf_size);
645 if (csize > buf_size)
648 bcopy(buf, addr, csize);
650 fp->f_seekp += csize;
660 ufs_seek(f, offset, where)
665 register struct file *fp = (struct file *)f->f_fsdata;
669 fp->f_seekp = offset;
672 fp->f_seekp += offset;
675 fp->f_seekp = fp->f_di.di_size - offset;
680 return (fp->f_seekp);
688 register struct file *fp = (struct file *)f->f_fsdata;
690 /* only important stuff */
691 sb->st_mode = fp->f_di.di_mode;
692 sb->st_uid = fp->f_di.di_uid;
693 sb->st_gid = fp->f_di.di_gid;
694 sb->st_size = fp->f_di.di_size;
699 ufs_readdir(struct open_file *f, struct dirent *d)
701 struct file *fp = (struct file *)f->f_fsdata;
708 * assume that a directory entry will not be split across blocks
711 if (fp->f_seekp >= fp->f_di.di_size)
713 error = buf_read_file(f, &buf, &buf_size);
716 dp = (struct direct *)buf;
717 fp->f_seekp += dp->d_reclen;
718 if (dp->d_ino == (ino_t)0)
720 d->d_type = dp->d_type;
721 strcpy(d->d_name, dp->d_name);
727 * Sanity checks for old file systems.
729 * XXX - goes away some day.
737 fs->fs_npsect = max(fs->fs_npsect, fs->fs_nsect); /* XXX */
738 fs->fs_interleave = max(fs->fs_interleave, 1); /* XXX */
739 if (fs->fs_postblformat == FS_42POSTBLFMT) /* XXX */
740 fs->fs_nrpos = 8; /* XXX */
741 if (fs->fs_inodefmt < FS_44INODEFMT) { /* XXX */
742 quad_t sizepb = fs->fs_bsize; /* XXX */
744 fs->fs_maxfilesize = fs->fs_bsize * NDADDR - 1; /* XXX */
745 for (i = 0; i < NIADDR; i++) { /* XXX */
746 sizepb *= NINDIR(fs); /* XXX */
747 fs->fs_maxfilesize += sizepb; /* XXX */
749 fs->fs_qbmask = ~fs->fs_bmask; /* XXX */
750 fs->fs_qfmask = ~fs->fs_fmask; /* XXX */