Home | History | Annotate | Line # | Download | only in libsa
ufs.c revision 1.4
      1 /*-
      2  * Copyright (c) 1993
      3  *	The Regents of the University of California.  All rights reserved.
      4  *
      5  * This code is derived from software contributed to Berkeley by
      6  * The Mach Operating System project at Carnegie-Mellon University.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *	This product includes software developed by the University of
     19  *	California, Berkeley and its contributors.
     20  * 4. Neither the name of the University nor the names of its contributors
     21  *    may be used to endorse or promote products derived from this software
     22  *    without specific prior written permission.
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  * SUCH DAMAGE.
     35  *
     36  *	from: @(#)ufs.c	8.1 (Berkeley) 6/11/93
     37  *
     38  *
     39  * Copyright (c) 1990, 1991 Carnegie Mellon University
     40  * All Rights Reserved.
     41  *
     42  * Author: David Golub
     43  *
     44  * Permission to use, copy, modify and distribute this software and its
     45  * documentation is hereby granted, provided that both the copyright
     46  * notice and this permission notice appear in all copies of the
     47  * software, derivative works or modified versions, and any portions
     48  * thereof, and that both notices appear in supporting documentation.
     49  *
     50  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
     51  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
     52  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
     53  *
     54  * Carnegie Mellon requests users of this software to return to
     55  *
     56  *  Software Distribution Coordinator  or  Software.Distribution (at) CS.CMU.EDU
     57  *  School of Computer Science
     58  *  Carnegie Mellon University
     59  *  Pittsburgh PA 15213-3890
     60  *
     61  * any improvements or extensions that they make and grant Carnegie the
     62  * rights to redistribute these changes.
     63  *
     64  *	$Id: ufs.c,v 1.4 1994/07/18 13:08:09 pk Exp $
     65  */
     66 
     67 /*
     68  *	Stand-alone file reading package.
     69  */
     70 
     71 #include <sys/param.h>
     72 #include <sys/time.h>
     73 #include <ufs/ffs/fs.h>
     74 #include <ufs/ufs/dinode.h>
     75 #include <ufs/ufs/dir.h>
     76 
     77 #include "stand.h"
     78 
     79 /*
     80  * In-core open file.
     81  */
     82 struct file {
     83 	off_t		f_seekp;	/* seek pointer */
     84 	struct fs	*f_fs;		/* pointer to super-block */
     85 	struct dinode	f_di;		/* copy of on-disk inode */
     86 	int		f_nindir[NIADDR];
     87 					/* number of blocks mapped by
     88 					   indirect block at level i */
     89 	char		*f_blk[NIADDR];	/* buffer for indirect block at
     90 					   level i */
     91 	u_long		f_blksize[NIADDR];
     92 					/* size of buffer */
     93 	daddr_t		f_blkno[NIADDR];/* disk address of block in buffer */
     94 	char		*f_buf;		/* buffer for data block */
     95 	u_int		f_buf_size;	/* size of data block */
     96 	daddr_t		f_buf_blkno;	/* block number of data block */
     97 };
     98 
     99 /*
    100  * Read a new inode into a file structure.
    101  */
    102 static int
    103 read_inode(inumber, f)
    104 	ino_t inumber;
    105 	struct open_file *f;
    106 {
    107 	register struct file *fp = (struct file *)f->f_fsdata;
    108 	register struct fs *fs = fp->f_fs;
    109 	char *buf;
    110 	u_int rsize;
    111 	int rc;
    112 
    113 	/*
    114 	 * Read inode and save it.
    115 	 */
    116 	buf = alloc(fs->fs_bsize);
    117 	rc = (f->f_dev->dv_strategy)(f->f_devdata, F_READ,
    118 		fsbtodb(fs, ino_to_fsba(fs, inumber)), fs->fs_bsize, buf, &rsize);
    119 	if (rc)
    120 		goto out;
    121 	if (rsize != fs->fs_bsize) {
    122 		rc = EIO;
    123 		goto out;
    124 	}
    125 
    126 	{
    127 		register struct dinode *dp;
    128 
    129 		dp = (struct dinode *)buf;
    130 		fp->f_di = dp[ino_to_fsbo(fs, inumber)];
    131 	}
    132 
    133 	/*
    134 	 * Clear out the old buffers
    135 	 */
    136 	{
    137 		register int level;
    138 
    139 		for (level = 0; level < NIADDR; level++)
    140 			fp->f_blkno[level] = -1;
    141 		fp->f_buf_blkno = -1;
    142 	}
    143 out:
    144 	free(buf, fs->fs_bsize);
    145 	return (0);
    146 }
    147 
    148 /*
    149  * Given an offset in a file, find the disk block number that
    150  * contains that block.
    151  */
    152 static int
    153 block_map(f, file_block, disk_block_p)
    154 	struct open_file *f;
    155 	daddr_t file_block;
    156 	daddr_t *disk_block_p;	/* out */
    157 {
    158 	register struct file *fp = (struct file *)f->f_fsdata;
    159 	register struct fs *fs = fp->f_fs;
    160 	int level;
    161 	int idx;
    162 	daddr_t ind_block_num;
    163 	daddr_t *ind_p;
    164 	int rc;
    165 
    166 	/*
    167 	 * Index structure of an inode:
    168 	 *
    169 	 * di_db[0..NDADDR-1]	hold block numbers for blocks
    170 	 *			0..NDADDR-1
    171 	 *
    172 	 * di_ib[0]		index block 0 is the single indirect block
    173 	 *			holds block numbers for blocks
    174 	 *			NDADDR .. NDADDR + NINDIR(fs)-1
    175 	 *
    176 	 * di_ib[1]		index block 1 is the double indirect block
    177 	 *			holds block numbers for INDEX blocks for blocks
    178 	 *			NDADDR + NINDIR(fs) ..
    179 	 *			NDADDR + NINDIR(fs) + NINDIR(fs)**2 - 1
    180 	 *
    181 	 * di_ib[2]		index block 2 is the triple indirect block
    182 	 *			holds block numbers for double-indirect
    183 	 *			blocks for blocks
    184 	 *			NDADDR + NINDIR(fs) + NINDIR(fs)**2 ..
    185 	 *			NDADDR + NINDIR(fs) + NINDIR(fs)**2
    186 	 *				+ NINDIR(fs)**3 - 1
    187 	 */
    188 
    189 	if (file_block < NDADDR) {
    190 		/* Direct block. */
    191 		*disk_block_p = fp->f_di.di_db[file_block];
    192 		return (0);
    193 	}
    194 
    195 	file_block -= NDADDR;
    196 
    197 	/*
    198 	 * nindir[0] = NINDIR
    199 	 * nindir[1] = NINDIR**2
    200 	 * nindir[2] = NINDIR**3
    201 	 *	etc
    202 	 */
    203 	for (level = 0; level < NIADDR; level++) {
    204 		if (file_block < fp->f_nindir[level])
    205 			break;
    206 		file_block -= fp->f_nindir[level];
    207 	}
    208 	if (level == NIADDR) {
    209 		/* Block number too high */
    210 		return (EFBIG);
    211 	}
    212 
    213 	ind_block_num = fp->f_di.di_ib[level];
    214 
    215 	for (; level >= 0; level--) {
    216 		if (ind_block_num == 0) {
    217 			*disk_block_p = 0;	/* missing */
    218 			return (0);
    219 		}
    220 
    221 		if (fp->f_blkno[level] != ind_block_num) {
    222 			if (fp->f_blk[level] == (char *)0)
    223 				fp->f_blk[level] =
    224 					alloc(fs->fs_bsize);
    225 			rc = (f->f_dev->dv_strategy)(f->f_devdata, F_READ,
    226 				fsbtodb(fp->f_fs, ind_block_num),
    227 				fs->fs_bsize,
    228 				fp->f_blk[level],
    229 				(u_int *)&fp->f_blksize[level]);
    230 			if (rc)
    231 				return (rc);
    232 			if (fp->f_blksize[level] != fs->fs_bsize)
    233 				return (EIO);
    234 			fp->f_blkno[level] = ind_block_num;
    235 		}
    236 
    237 		ind_p = (daddr_t *)fp->f_blk[level];
    238 
    239 		if (level > 0) {
    240 			idx = file_block / fp->f_nindir[level - 1];
    241 			file_block %= fp->f_nindir[level - 1];
    242 		} else
    243 			idx = file_block;
    244 
    245 		ind_block_num = ind_p[idx];
    246 	}
    247 
    248 	*disk_block_p = ind_block_num;
    249 
    250 	return (0);
    251 }
    252 
    253 /*
    254  * Read a portion of a file into an internal buffer.  Return
    255  * the location in the buffer and the amount in the buffer.
    256  */
    257 static int
    258 buf_read_file(f, buf_p, size_p)
    259 	struct open_file *f;
    260 	char **buf_p;		/* out */
    261 	u_int *size_p;		/* out */
    262 {
    263 	register struct file *fp = (struct file *)f->f_fsdata;
    264 	register struct fs *fs = fp->f_fs;
    265 	long off;
    266 	register daddr_t file_block;
    267 	daddr_t	disk_block;
    268 	long block_size;
    269 	int rc;
    270 
    271 	off = blkoff(fs, fp->f_seekp);
    272 	file_block = lblkno(fs, fp->f_seekp);
    273 	block_size = dblksize(fs, &fp->f_di, file_block);
    274 
    275 	if (file_block != fp->f_buf_blkno) {
    276 		rc = block_map(f, file_block, &disk_block);
    277 		if (rc)
    278 			return (rc);
    279 
    280 		if (fp->f_buf == (char *)0)
    281 			fp->f_buf = alloc(fs->fs_bsize);
    282 
    283 		if (disk_block == 0) {
    284 			bzero(fp->f_buf, block_size);
    285 			fp->f_buf_size = block_size;
    286 		} else {
    287 			rc = (f->f_dev->dv_strategy)(f->f_devdata, F_READ,
    288 				fsbtodb(fs, disk_block),
    289 				block_size, fp->f_buf, &fp->f_buf_size);
    290 			if (rc)
    291 				return (rc);
    292 		}
    293 
    294 		fp->f_buf_blkno = file_block;
    295 	}
    296 
    297 	/*
    298 	 * Return address of byte in buffer corresponding to
    299 	 * offset, and size of remainder of buffer after that
    300 	 * byte.
    301 	 */
    302 	*buf_p = fp->f_buf + off;
    303 	*size_p = block_size - off;
    304 
    305 	/*
    306 	 * But truncate buffer at end of file.
    307 	 */
    308 	if (*size_p > fp->f_di.di_size - fp->f_seekp)
    309 		*size_p = fp->f_di.di_size - fp->f_seekp;
    310 
    311 	return (0);
    312 }
    313 
    314 /*
    315  * Search a directory for a name and return its
    316  * i_number.
    317  */
    318 static int
    319 search_directory(name, f, inumber_p)
    320 	char *name;
    321 	struct open_file *f;
    322 	ino_t *inumber_p;		/* out */
    323 {
    324 	register struct file *fp = (struct file *)f->f_fsdata;
    325 	register struct direct *dp;
    326 	struct direct *edp;
    327 	char *buf;
    328 	u_int buf_size;
    329 	int namlen, length;
    330 	int rc;
    331 
    332 	length = strlen(name);
    333 
    334 	fp->f_seekp = 0;
    335 	while (fp->f_seekp < fp->f_di.di_size) {
    336 		rc = buf_read_file(f, &buf, &buf_size);
    337 		if (rc)
    338 			return (rc);
    339 
    340 		dp = (struct direct *)buf;
    341 		edp = (struct direct *)(buf + buf_size);
    342 		while (dp < edp) {
    343 			if (dp->d_ino == (ino_t)0)
    344 				goto next;
    345 #if 0
    346 #if BYTE_ORDER == LITTLE_ENDIAN
    347 			if (fp->f_fs->fs_maxsymlinklen <= 0)
    348 				namlen = dp->d_type;
    349 			else
    350 #endif
    351 #endif
    352 				namlen = dp->d_namlen;
    353 			if (namlen == length &&
    354 			    !strcmp(name, dp->d_name)) {
    355 				/* found entry */
    356 				*inumber_p = dp->d_ino;
    357 				return (0);
    358 			}
    359 		next:
    360 			dp = (struct direct *)((char *)dp + dp->d_reclen);
    361 		}
    362 		fp->f_seekp += buf_size;
    363 	}
    364 	return (ENOENT);
    365 }
    366 
    367 /*
    368  * Open a file.
    369  */
    370 int
    371 ufs_open(path, f)
    372 	char *path;
    373 	struct open_file *f;
    374 {
    375 	register char *cp, *ncp;
    376 	register int c;
    377 	ino_t inumber, parent_inumber;
    378 	int nlinks = 0;
    379 	struct file *fp;
    380 	struct fs *fs;
    381 	int rc;
    382 	u_int buf_size;
    383 #if 0
    384 	char namebuf[MAXPATHLEN+1];
    385 #endif
    386 
    387 	/* allocate file system specific data structure */
    388 	fp = alloc(sizeof(struct file));
    389 	bzero(fp, sizeof(struct file));
    390 	f->f_fsdata = (void *)fp;
    391 
    392 	/* allocate space and read super block */
    393 	fs = alloc(SBSIZE);
    394 	fp->f_fs = fs;
    395 	rc = (f->f_dev->dv_strategy)(f->f_devdata, F_READ,
    396 		SBLOCK, SBSIZE, (char *)fs, &buf_size);
    397 	if (rc)
    398 		goto out;
    399 
    400 	if (buf_size != SBSIZE || fs->fs_magic != FS_MAGIC ||
    401 	    fs->fs_bsize > MAXBSIZE || fs->fs_bsize < sizeof(struct fs)) {
    402 		rc = EINVAL;
    403 		goto out;
    404 	}
    405 	ffs_oldfscompat(fs);
    406 
    407 	/*
    408 	 * Calculate indirect block levels.
    409 	 */
    410 	{
    411 		register int mult;
    412 		register int level;
    413 
    414 		mult = 1;
    415 		for (level = 0; level < NIADDR; level++) {
    416 			mult *= NINDIR(fs);
    417 			fp->f_nindir[level] = mult;
    418 		}
    419 	}
    420 
    421 	inumber = ROOTINO;
    422 	if ((rc = read_inode(inumber, f)) != 0)
    423 		goto out;
    424 
    425 	cp = path;
    426 	while (*cp) {
    427 
    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 ((fp->f_di.di_mode & IFMT) != IFDIR) {
    440 			rc = ENOTDIR;
    441 			goto out;
    442 		}
    443 
    444 		/*
    445 		 * Get next component of path name.
    446 		 */
    447 		{
    448 			register int len = 0;
    449 
    450 			ncp = cp;
    451 			while ((c = *cp) != '\0' && c != '/') {
    452 				if (++len > MAXNAMLEN) {
    453 					rc = ENOENT;
    454 					goto out;
    455 				}
    456 				cp++;
    457 			}
    458 			*cp = '\0';
    459 		}
    460 
    461 		/*
    462 		 * Look up component in current directory.
    463 		 * Save directory inumber in case we find a
    464 		 * symbolic link.
    465 		 */
    466 		parent_inumber = inumber;
    467 		rc = search_directory(ncp, f, &inumber);
    468 		*cp = c;
    469 		if (rc)
    470 			goto out;
    471 
    472 		/*
    473 		 * Open next component.
    474 		 */
    475 		if ((rc = read_inode(inumber, f)) != 0)
    476 			goto out;
    477 
    478 #if 0
    479 		/*
    480 		 * Check for symbolic link.
    481 		 */
    482 		if ((fp->i_mode & IFMT) == IFLNK) {
    483 			int link_len = fp->f_di.di_size;
    484 			int len;
    485 
    486 			len = strlen(cp) + 1;
    487 
    488 			if (fp->f_di.di_size >= MAXPATHLEN - 1 ||
    489 			    ++nlinks > MAXSYMLINKS) {
    490 				rc = ENOENT;
    491 				goto out;
    492 			}
    493 
    494 			strcpy(&namebuf[link_len], cp);
    495 
    496 			if ((fp->i_flags & IC_FASTLINK) != 0) {
    497 				bcopy(fp->i_symlink, namebuf, (unsigned) link_len);
    498 			} else {
    499 				/*
    500 				 * Read file for symbolic link
    501 				 */
    502 				char *buf;
    503 				u_int buf_size;
    504 				daddr_t	disk_block;
    505 				register struct fs *fs = fp->f_fs;
    506 
    507 				(void) block_map(f, (daddr_t)0, &disk_block);
    508 				rc = device_read(&fp->f_dev,
    509 						 fsbtodb(fs, disk_block),
    510 						 blksize(fs, fp, 0),
    511 						 &buf, &buf_size);
    512 				if (rc)
    513 					goto out;
    514 
    515 				bcopy((char *)buf, namebuf, (unsigned)link_len);
    516 				free(buf, buf_size);
    517 			}
    518 
    519 			/*
    520 			 * If relative pathname, restart at parent directory.
    521 			 * If absolute pathname, restart at root.
    522 			 */
    523 			cp = namebuf;
    524 			if (*cp != '/')
    525 				inumber = parent_inumber;
    526 			else
    527 				inumber = (ino_t)ROOTINO;
    528 
    529 			if ((rc = read_inode(inumber, fp)) != 0)
    530 				goto out;
    531 		}
    532 #endif
    533 	}
    534 
    535 	/*
    536 	 * Found terminal component.
    537 	 */
    538 	rc = 0;
    539 out:
    540 	if (rc)
    541 		free(fp, sizeof(struct file));
    542 	return (rc);
    543 }
    544 
    545 int
    546 ufs_close(f)
    547 	struct open_file *f;
    548 {
    549 	register struct file *fp = (struct file *)f->f_fsdata;
    550 	int level;
    551 
    552 	f->f_fsdata = (void *)0;
    553 	if (fp == (struct file *)0)
    554 		return (0);
    555 
    556 	for (level = 0; level < NIADDR; level++) {
    557 		if (fp->f_blk[level])
    558 			free(fp->f_blk[level], fp->f_fs->fs_bsize);
    559 	}
    560 	if (fp->f_buf)
    561 		free(fp->f_buf, fp->f_fs->fs_bsize);
    562 	free(fp->f_fs, SBSIZE);
    563 	free(fp, sizeof(struct file));
    564 	return (0);
    565 }
    566 
    567 /*
    568  * Copy a portion of a file into kernel memory.
    569  * Cross block boundaries when necessary.
    570  */
    571 int
    572 ufs_read(f, start, size, resid)
    573 	struct open_file *f;
    574 	char *start;
    575 	u_int size;
    576 	u_int *resid;	/* out */
    577 {
    578 	register struct file *fp = (struct file *)f->f_fsdata;
    579 	register u_int csize;
    580 	char *buf;
    581 	u_int buf_size;
    582 	int rc = 0;
    583 
    584 	while (size != 0) {
    585 		if (fp->f_seekp >= fp->f_di.di_size)
    586 			break;
    587 
    588 		rc = buf_read_file(f, &buf, &buf_size);
    589 		if (rc)
    590 			break;
    591 
    592 		csize = size;
    593 		if (csize > buf_size)
    594 			csize = buf_size;
    595 
    596 		bcopy(buf, start, csize);
    597 
    598 		fp->f_seekp += csize;
    599 		start += csize;
    600 		size -= csize;
    601 	}
    602 	if (resid)
    603 		*resid = size;
    604 	return (rc);
    605 }
    606 
    607 /*
    608  * Not implemented.
    609  */
    610 int
    611 ufs_write(f, start, size, resid)
    612 	struct open_file *f;
    613 	char *start;
    614 	u_int size;
    615 	u_int *resid;	/* out */
    616 {
    617 
    618 	return (EROFS);
    619 }
    620 
    621 off_t
    622 ufs_seek(f, offset, where)
    623 	struct open_file *f;
    624 	off_t offset;
    625 	int where;
    626 {
    627 	register struct file *fp = (struct file *)f->f_fsdata;
    628 
    629 	switch (where) {
    630 	case SEEK_SET:
    631 		fp->f_seekp = offset;
    632 		break;
    633 	case SEEK_CUR:
    634 		fp->f_seekp += offset;
    635 		break;
    636 	case SEEK_END:
    637 		fp->f_seekp = fp->f_di.di_size - offset;
    638 		break;
    639 	default:
    640 		return (-1);
    641 	}
    642 	return (fp->f_seekp);
    643 }
    644 
    645 int
    646 ufs_stat(f, sb)
    647 	struct open_file *f;
    648 	struct stat *sb;
    649 {
    650 	register struct file *fp = (struct file *)f->f_fsdata;
    651 
    652 	/* only important stuff */
    653 	sb->st_mode = fp->f_di.di_mode;
    654 	sb->st_uid = fp->f_di.di_uid;
    655 	sb->st_gid = fp->f_di.di_gid;
    656 	sb->st_size = fp->f_di.di_size;
    657 	return (0);
    658 }
    659 
    660 /*
    661  * Sanity checks for old file systems.
    662  *
    663  * XXX - goes away some day.
    664  */
    665 ffs_oldfscompat(fs)
    666 	struct fs *fs;
    667 {
    668 	int i;
    669 
    670 	fs->fs_npsect = max(fs->fs_npsect, fs->fs_nsect);	/* XXX */
    671 	fs->fs_interleave = max(fs->fs_interleave, 1);		/* XXX */
    672 	if (fs->fs_postblformat == FS_42POSTBLFMT)		/* XXX */
    673 		fs->fs_nrpos = 8;				/* XXX */
    674 	if (fs->fs_inodefmt < FS_44INODEFMT) {			/* XXX */
    675 		quad_t sizepb = fs->fs_bsize;			/* XXX */
    676 								/* XXX */
    677 		fs->fs_maxfilesize = fs->fs_bsize * NDADDR - 1;	/* XXX */
    678 		for (i = 0; i < NIADDR; i++) {			/* XXX */
    679 			sizepb *= NINDIR(fs);			/* XXX */
    680 			fs->fs_maxfilesize += sizepb;		/* XXX */
    681 		}						/* XXX */
    682 		fs->fs_qbmask = ~fs->fs_bmask;			/* XXX */
    683 		fs->fs_qfmask = ~fs->fs_fmask;			/* XXX */
    684 	}							/* XXX */
    685 	return (0);
    686 }
    687