Home | History | Annotate | Line # | Download | only in libsa
ufs.c revision 1.5
      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.5 1994/07/27 07:37:35 cgd 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 BYTE_ORDER == LITTLE_ENDIAN
    346 			if (fp->f_fs->fs_maxsymlinklen <= 0)
    347 				namlen = dp->d_type;
    348 			else
    349 #endif
    350 				namlen = dp->d_namlen;
    351 			if (namlen == length &&
    352 			    !strcmp(name, dp->d_name)) {
    353 				/* found entry */
    354 				*inumber_p = dp->d_ino;
    355 				return (0);
    356 			}
    357 		next:
    358 			dp = (struct direct *)((char *)dp + dp->d_reclen);
    359 		}
    360 		fp->f_seekp += buf_size;
    361 	}
    362 	return (ENOENT);
    363 }
    364 
    365 /*
    366  * Open a file.
    367  */
    368 int
    369 ufs_open(path, f)
    370 	char *path;
    371 	struct open_file *f;
    372 {
    373 	register char *cp, *ncp;
    374 	register int c;
    375 	ino_t inumber, parent_inumber;
    376 	int nlinks = 0;
    377 	struct file *fp;
    378 	struct fs *fs;
    379 	int rc;
    380 	u_int buf_size;
    381 #if 0
    382 	char namebuf[MAXPATHLEN+1];
    383 #endif
    384 
    385 	/* allocate file system specific data structure */
    386 	fp = alloc(sizeof(struct file));
    387 	bzero(fp, sizeof(struct file));
    388 	f->f_fsdata = (void *)fp;
    389 
    390 	/* allocate space and read super block */
    391 	fs = alloc(SBSIZE);
    392 	fp->f_fs = fs;
    393 	rc = (f->f_dev->dv_strategy)(f->f_devdata, F_READ,
    394 		SBLOCK, SBSIZE, (char *)fs, &buf_size);
    395 	if (rc)
    396 		goto out;
    397 
    398 	if (buf_size != SBSIZE || fs->fs_magic != FS_MAGIC ||
    399 	    fs->fs_bsize > MAXBSIZE || fs->fs_bsize < sizeof(struct fs)) {
    400 		rc = EINVAL;
    401 		goto out;
    402 	}
    403 	ffs_oldfscompat(fs);
    404 
    405 	/*
    406 	 * Calculate indirect block levels.
    407 	 */
    408 	{
    409 		register int mult;
    410 		register int level;
    411 
    412 		mult = 1;
    413 		for (level = 0; level < NIADDR; level++) {
    414 			mult *= NINDIR(fs);
    415 			fp->f_nindir[level] = mult;
    416 		}
    417 	}
    418 
    419 	inumber = ROOTINO;
    420 	if ((rc = read_inode(inumber, f)) != 0)
    421 		goto out;
    422 
    423 	cp = path;
    424 	while (*cp) {
    425 
    426 		/*
    427 		 * Remove extra separators
    428 		 */
    429 		while (*cp == '/')
    430 			cp++;
    431 		if (*cp == '\0')
    432 			break;
    433 
    434 		/*
    435 		 * Check that current node is a directory.
    436 		 */
    437 		if ((fp->f_di.di_mode & IFMT) != IFDIR) {
    438 			rc = ENOTDIR;
    439 			goto out;
    440 		}
    441 
    442 		/*
    443 		 * Get next component of path name.
    444 		 */
    445 		{
    446 			register int len = 0;
    447 
    448 			ncp = cp;
    449 			while ((c = *cp) != '\0' && c != '/') {
    450 				if (++len > MAXNAMLEN) {
    451 					rc = ENOENT;
    452 					goto out;
    453 				}
    454 				cp++;
    455 			}
    456 			*cp = '\0';
    457 		}
    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 		rc = search_directory(ncp, f, &inumber);
    466 		*cp = c;
    467 		if (rc)
    468 			goto out;
    469 
    470 		/*
    471 		 * Open next component.
    472 		 */
    473 		if ((rc = read_inode(inumber, f)) != 0)
    474 			goto out;
    475 
    476 #if 0
    477 		/*
    478 		 * Check for symbolic link.
    479 		 */
    480 		if ((fp->i_mode & IFMT) == IFLNK) {
    481 			int link_len = fp->f_di.di_size;
    482 			int len;
    483 
    484 			len = strlen(cp) + 1;
    485 
    486 			if (fp->f_di.di_size >= MAXPATHLEN - 1 ||
    487 			    ++nlinks > MAXSYMLINKS) {
    488 				rc = ENOENT;
    489 				goto out;
    490 			}
    491 
    492 			strcpy(&namebuf[link_len], cp);
    493 
    494 			if ((fp->i_flags & IC_FASTLINK) != 0) {
    495 				bcopy(fp->i_symlink, namebuf, (unsigned) link_len);
    496 			} else {
    497 				/*
    498 				 * Read file for symbolic link
    499 				 */
    500 				char *buf;
    501 				u_int buf_size;
    502 				daddr_t	disk_block;
    503 				register struct fs *fs = fp->f_fs;
    504 
    505 				(void) block_map(f, (daddr_t)0, &disk_block);
    506 				rc = device_read(&fp->f_dev,
    507 						 fsbtodb(fs, disk_block),
    508 						 blksize(fs, fp, 0),
    509 						 &buf, &buf_size);
    510 				if (rc)
    511 					goto out;
    512 
    513 				bcopy((char *)buf, namebuf, (unsigned)link_len);
    514 				free(buf, buf_size);
    515 			}
    516 
    517 			/*
    518 			 * If relative pathname, restart at parent directory.
    519 			 * If absolute pathname, restart at root.
    520 			 */
    521 			cp = namebuf;
    522 			if (*cp != '/')
    523 				inumber = parent_inumber;
    524 			else
    525 				inumber = (ino_t)ROOTINO;
    526 
    527 			if ((rc = read_inode(inumber, fp)) != 0)
    528 				goto out;
    529 		}
    530 #endif
    531 	}
    532 
    533 	/*
    534 	 * Found terminal component.
    535 	 */
    536 	rc = 0;
    537 out:
    538 	if (rc)
    539 		free(fp, sizeof(struct file));
    540 	return (rc);
    541 }
    542 
    543 int
    544 ufs_close(f)
    545 	struct open_file *f;
    546 {
    547 	register struct file *fp = (struct file *)f->f_fsdata;
    548 	int level;
    549 
    550 	f->f_fsdata = (void *)0;
    551 	if (fp == (struct file *)0)
    552 		return (0);
    553 
    554 	for (level = 0; level < NIADDR; level++) {
    555 		if (fp->f_blk[level])
    556 			free(fp->f_blk[level], fp->f_fs->fs_bsize);
    557 	}
    558 	if (fp->f_buf)
    559 		free(fp->f_buf, fp->f_fs->fs_bsize);
    560 	free(fp->f_fs, SBSIZE);
    561 	free(fp, sizeof(struct file));
    562 	return (0);
    563 }
    564 
    565 /*
    566  * Copy a portion of a file into kernel memory.
    567  * Cross block boundaries when necessary.
    568  */
    569 int
    570 ufs_read(f, start, size, resid)
    571 	struct open_file *f;
    572 	char *start;
    573 	u_int size;
    574 	u_int *resid;	/* out */
    575 {
    576 	register struct file *fp = (struct file *)f->f_fsdata;
    577 	register u_int csize;
    578 	char *buf;
    579 	u_int buf_size;
    580 	int rc = 0;
    581 
    582 	while (size != 0) {
    583 		if (fp->f_seekp >= fp->f_di.di_size)
    584 			break;
    585 
    586 		rc = buf_read_file(f, &buf, &buf_size);
    587 		if (rc)
    588 			break;
    589 
    590 		csize = size;
    591 		if (csize > buf_size)
    592 			csize = buf_size;
    593 
    594 		bcopy(buf, start, csize);
    595 
    596 		fp->f_seekp += csize;
    597 		start += csize;
    598 		size -= csize;
    599 	}
    600 	if (resid)
    601 		*resid = size;
    602 	return (rc);
    603 }
    604 
    605 /*
    606  * Not implemented.
    607  */
    608 int
    609 ufs_write(f, start, size, resid)
    610 	struct open_file *f;
    611 	char *start;
    612 	u_int size;
    613 	u_int *resid;	/* out */
    614 {
    615 
    616 	return (EROFS);
    617 }
    618 
    619 off_t
    620 ufs_seek(f, offset, where)
    621 	struct open_file *f;
    622 	off_t offset;
    623 	int where;
    624 {
    625 	register struct file *fp = (struct file *)f->f_fsdata;
    626 
    627 	switch (where) {
    628 	case SEEK_SET:
    629 		fp->f_seekp = offset;
    630 		break;
    631 	case SEEK_CUR:
    632 		fp->f_seekp += offset;
    633 		break;
    634 	case SEEK_END:
    635 		fp->f_seekp = fp->f_di.di_size - offset;
    636 		break;
    637 	default:
    638 		return (-1);
    639 	}
    640 	return (fp->f_seekp);
    641 }
    642 
    643 int
    644 ufs_stat(f, sb)
    645 	struct open_file *f;
    646 	struct stat *sb;
    647 {
    648 	register struct file *fp = (struct file *)f->f_fsdata;
    649 
    650 	/* only important stuff */
    651 	sb->st_mode = fp->f_di.di_mode;
    652 	sb->st_uid = fp->f_di.di_uid;
    653 	sb->st_gid = fp->f_di.di_gid;
    654 	sb->st_size = fp->f_di.di_size;
    655 	return (0);
    656 }
    657 
    658 /*
    659  * Sanity checks for old file systems.
    660  *
    661  * XXX - goes away some day.
    662  */
    663 ffs_oldfscompat(fs)
    664 	struct fs *fs;
    665 {
    666 	int i;
    667 
    668 	fs->fs_npsect = max(fs->fs_npsect, fs->fs_nsect);	/* XXX */
    669 	fs->fs_interleave = max(fs->fs_interleave, 1);		/* XXX */
    670 	if (fs->fs_postblformat == FS_42POSTBLFMT)		/* XXX */
    671 		fs->fs_nrpos = 8;				/* XXX */
    672 	if (fs->fs_inodefmt < FS_44INODEFMT) {			/* XXX */
    673 		quad_t sizepb = fs->fs_bsize;			/* XXX */
    674 								/* XXX */
    675 		fs->fs_maxfilesize = fs->fs_bsize * NDADDR - 1;	/* XXX */
    676 		for (i = 0; i < NIADDR; i++) {			/* XXX */
    677 			sizepb *= NINDIR(fs);			/* XXX */
    678 			fs->fs_maxfilesize += sizepb;		/* XXX */
    679 		}						/* XXX */
    680 		fs->fs_qbmask = ~fs->fs_bmask;			/* XXX */
    681 		fs->fs_qfmask = ~fs->fs_fmask;			/* XXX */
    682 	}							/* XXX */
    683 	return (0);
    684 }
    685