1 /* $FreeBSD: src/lib/libstand/ufs.c,v 1.5.6.1 2000/05/04 13:47:53 ps Exp $ */
2 /* $DragonFly: src/lib/libstand/ufs.c,v 1.9 2006/04/03 01:58:45 dillon Exp $ */
3 /* $NetBSD: ufs.c,v 1.20 1998/03/01 07:15:39 ross Exp $ */
7 * The Regents of the University of California. All rights reserved.
9 * This code is derived from software contributed to Berkeley by
10 * The Mach Operating System project at Carnegie-Mellon University.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. 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.
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
37 * Copyright (c) 1990, 1991 Carnegie Mellon University
38 * All Rights Reserved.
42 * Permission to use, copy, modify and distribute this software and its
43 * documentation is hereby granted, provided that both the copyright
44 * notice and this permission notice appear in all copies of the
45 * software, derivative works or modified versions, and any portions
46 * thereof, and that both notices appear in supporting documentation.
48 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
49 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
50 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
52 * Carnegie Mellon requests users of this software to return to
54 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
55 * School of Computer Science
56 * Carnegie Mellon University
57 * Pittsburgh PA 15213-3890
59 * any improvements or extensions that they make and grant Carnegie the
60 * rights to redistribute these changes.
64 * Stand-alone file reading package.
67 #include <sys/param.h>
72 #include <vfs/ufs/dinode.h>
73 #include <vfs/ufs/dir.h>
74 #include <vfs/ufs/fs.h>
77 static int ufs_open(const char *path, struct open_file *f);
78 static int ufs_close(struct open_file *f);
79 static int ufs_read(struct open_file *f, void *buf, size_t size, size_t *resid);
80 static off_t ufs_seek(struct open_file *f, off_t offset, int where);
81 static int ufs_stat(struct open_file *f, struct stat *sb);
82 static int ufs_readdir(struct open_file *f, struct dirent *d);
84 struct fs_ops ufs_fsops = {
99 off_t f_seekp; /* seek pointer */
100 struct fs *f_fs; /* pointer to super-block */
101 struct ufs1_dinode f_di; /* copy of on-disk inode */
102 int f_nindir[NIADDR];
103 /* number of blocks mapped by
104 indirect block at level i */
105 char *f_blk[NIADDR]; /* buffer for indirect block at
107 size_t f_blksize[NIADDR];
109 daddr_t f_blkno[NIADDR];/* disk address of block in buffer */
110 char *f_buf; /* buffer for data block */
111 size_t f_buf_size; /* size of data block */
112 daddr_t f_buf_blkno; /* block number of data block */
115 static int read_inode(ino_t, struct open_file *);
116 static int block_map(struct open_file *, daddr_t, daddr_t *);
117 static int buf_read_file(struct open_file *, char **, size_t *);
118 static int search_directory(char *, struct open_file *, ino_t *);
120 static void ffs_oldfscompat(struct fs *);
124 * Read a new inode into a file structure.
127 read_inode(ino_t inumber, struct open_file *f)
129 struct file *fp = (struct file *)f->f_fsdata;
130 struct fs *fs = fp->f_fs;
139 * Read inode and save it.
141 buf = malloc(fs->fs_bsize);
143 rc = (f->f_dev->dv_strategy)(f->f_devdata, F_READ,
144 fsbtodb(fs, ino_to_fsba(fs, inumber)), fs->fs_bsize,
148 if (rsize != fs->fs_bsize) {
154 struct ufs1_dinode *dp;
156 dp = (struct ufs1_dinode *)buf;
157 fp->f_di = dp[ino_to_fsbo(fs, inumber)];
161 * Clear out the old buffers
166 for (level = 0; level < NIADDR; level++)
167 fp->f_blkno[level] = -1;
168 fp->f_buf_blkno = -1;
176 * Given an offset in a file, find the disk block number that
177 * contains that block.
183 block_map(struct open_file *f, daddr_t file_block, daddr_t *disk_block_p)
185 struct file *fp = (struct file *)f->f_fsdata;
186 struct fs *fs = fp->f_fs;
189 daddr_t ind_block_num;
194 * Index structure of an inode:
196 * di_db[0..NDADDR-1] hold block numbers for blocks
199 * di_ib[0] index block 0 is the single indirect block
200 * holds block numbers for blocks
201 * NDADDR .. NDADDR + NINDIR(fs)-1
203 * di_ib[1] index block 1 is the double indirect block
204 * holds block numbers for INDEX blocks for blocks
205 * NDADDR + NINDIR(fs) ..
206 * NDADDR + NINDIR(fs) + NINDIR(fs)**2 - 1
208 * di_ib[2] index block 2 is the triple indirect block
209 * holds block numbers for double-indirect
211 * NDADDR + NINDIR(fs) + NINDIR(fs)**2 ..
212 * NDADDR + NINDIR(fs) + NINDIR(fs)**2
213 * + NINDIR(fs)**3 - 1
216 if (file_block < NDADDR) {
218 *disk_block_p = fp->f_di.di_db[file_block];
222 file_block -= NDADDR;
226 * nindir[1] = NINDIR**2
227 * nindir[2] = NINDIR**3
230 for (level = 0; level < NIADDR; level++) {
231 if (file_block < fp->f_nindir[level])
233 file_block -= fp->f_nindir[level];
235 if (level == NIADDR) {
236 /* Block number too high */
240 ind_block_num = fp->f_di.di_ib[level];
242 for (; level >= 0; level--) {
243 if (ind_block_num == 0) {
244 *disk_block_p = 0; /* missing */
248 if (fp->f_blkno[level] != ind_block_num) {
249 if (fp->f_blk[level] == NULL)
251 malloc(fs->fs_bsize);
253 rc = (f->f_dev->dv_strategy)(f->f_devdata, F_READ,
254 fsbtodb(fp->f_fs, ind_block_num),
257 &fp->f_blksize[level]);
260 if (fp->f_blksize[level] != fs->fs_bsize)
262 fp->f_blkno[level] = ind_block_num;
265 ind_p = (daddr_t *)fp->f_blk[level];
268 idx = file_block / fp->f_nindir[level - 1];
269 file_block %= fp->f_nindir[level - 1];
273 ind_block_num = ind_p[idx];
276 *disk_block_p = ind_block_num;
282 * Read a portion of a file into an internal buffer. Return
283 * the location in the buffer and the amount in the buffer.
290 buf_read_file(struct open_file *f, char **buf_p, size_t *size_p)
292 struct file *fp = (struct file *)f->f_fsdata;
293 struct fs *fs = fp->f_fs;
300 off = blkoff(fs, fp->f_seekp);
301 file_block = lblkno(fs, fp->f_seekp);
302 block_size = dblksize(fs, &fp->f_di, file_block);
304 if (file_block != fp->f_buf_blkno) {
305 rc = block_map(f, file_block, &disk_block);
309 if (fp->f_buf == NULL)
310 fp->f_buf = malloc(fs->fs_bsize);
312 if (disk_block == 0) {
313 bzero(fp->f_buf, block_size);
314 fp->f_buf_size = block_size;
317 rc = (f->f_dev->dv_strategy)(f->f_devdata, F_READ,
318 fsbtodb(fs, disk_block),
319 block_size, fp->f_buf, &fp->f_buf_size);
324 fp->f_buf_blkno = file_block;
328 * Return address of byte in buffer corresponding to
329 * offset, and size of remainder of buffer after that
332 *buf_p = fp->f_buf + off;
333 *size_p = block_size - off;
336 * But truncate buffer at end of file.
338 if (*size_p > fp->f_di.di_size - fp->f_seekp)
339 *size_p = fp->f_di.di_size - fp->f_seekp;
345 * Search a directory for a name and return its
352 search_directory(char *name, struct open_file *f, ino_t *inumber_p)
354 struct file *fp = (struct file *)f->f_fsdata;
362 length = strlen(name);
365 while (fp->f_seekp < fp->f_di.di_size) {
366 rc = buf_read_file(f, &buf, &buf_size);
370 dp = (struct direct *)buf;
371 edp = (struct direct *)(buf + buf_size);
373 if (dp->d_ino == (ino_t)0)
375 #if BYTE_ORDER == LITTLE_ENDIAN
376 if (fp->f_fs->fs_maxsymlinklen <= 0)
380 namlen = dp->d_namlen;
381 if (namlen == length &&
382 !strcmp(name, dp->d_name)) {
384 *inumber_p = dp->d_ino;
388 dp = (struct direct *)((char *)dp + dp->d_reclen);
390 fp->f_seekp += buf_size;
399 ufs_open(const char *upath, struct open_file *f)
403 ino_t inumber, parent_inumber;
409 char namebuf[MAXPATHLEN+1];
413 /* allocate file system specific data structure */
414 fp = malloc(sizeof(struct file));
415 bzero(fp, sizeof(struct file));
416 f->f_fsdata = (void *)fp;
418 /* allocate space and read super block */
422 rc = (f->f_dev->dv_strategy)(f->f_devdata, F_READ,
423 SBOFF / DEV_BSIZE, SBSIZE, (char *)fs, &buf_size);
427 if (buf_size != SBSIZE || fs->fs_magic != FS_MAGIC ||
428 fs->fs_bsize > MAXBSIZE || fs->fs_bsize < sizeof(struct fs)) {
437 * Calculate indirect block levels.
444 for (level = 0; level < NIADDR; level++) {
446 fp->f_nindir[level] = mult;
451 if ((rc = read_inode(inumber, f)) != 0)
454 cp = path = strdup(upath);
462 * Remove extra separators
470 * Check that current node is a directory.
472 if ((fp->f_di.di_mode & IFMT) != IFDIR) {
478 * Get next component of path name.
484 while ((c = *cp) != '\0' && c != '/') {
485 if (++len > MAXNAMLEN) {
495 * Look up component in current directory.
496 * Save directory inumber in case we find a
499 parent_inumber = inumber;
500 rc = search_directory(ncp, f, &inumber);
506 * Open next component.
508 if ((rc = read_inode(inumber, f)) != 0)
512 * Check for symbolic link.
514 if ((fp->f_di.di_mode & IFMT) == IFLNK) {
515 int link_len = fp->f_di.di_size;
520 if (link_len + len > MAXPATHLEN ||
521 ++nlinks > MAXSYMLINKS) {
526 bcopy(cp, &namebuf[link_len], len + 1);
528 if (link_len < fs->fs_maxsymlinklen) {
529 bcopy(fp->f_di.di_shortlink, namebuf,
530 (unsigned) link_len);
533 * Read file for symbolic link
537 struct fs *fs = fp->f_fs;
540 buf = malloc(fs->fs_bsize);
541 rc = block_map(f, (daddr_t)0, &disk_block);
546 rc = (f->f_dev->dv_strategy)(f->f_devdata,
547 F_READ, fsbtodb(fs, disk_block),
548 fs->fs_bsize, buf, &buf_size);
552 bcopy((char *)buf, namebuf, (unsigned)link_len);
556 * If relative pathname, restart at parent directory.
557 * If absolute pathname, restart at root.
561 inumber = parent_inumber;
563 inumber = (ino_t)ROOTINO;
565 if ((rc = read_inode(inumber, f)) != 0)
571 * Found terminal component.
590 ufs_close(struct open_file *f)
592 struct file *fp = (struct file *)f->f_fsdata;
599 for (level = 0; level < NIADDR; level++) {
600 if (fp->f_blk[level])
601 free(fp->f_blk[level]);
611 * Copy a portion of a file into kernel memory.
612 * Cross block boundaries when necessary.
618 ufs_read(struct open_file *f, void *start, size_t size, size_t *resid)
620 struct file *fp = (struct file *)f->f_fsdata;
628 if (fp->f_seekp >= fp->f_di.di_size)
631 rc = buf_read_file(f, &buf, &buf_size);
636 if (csize > buf_size)
639 bcopy(buf, addr, csize);
641 fp->f_seekp += csize;
651 ufs_seek(struct open_file *f, off_t offset, int where)
653 struct file *fp = (struct file *)f->f_fsdata;
657 fp->f_seekp = offset;
660 fp->f_seekp += offset;
663 fp->f_seekp = fp->f_di.di_size - offset;
668 return (fp->f_seekp);
672 ufs_stat(struct open_file *f, struct stat *sb)
674 struct file *fp = (struct file *)f->f_fsdata;
676 /* only important stuff */
677 sb->st_mode = fp->f_di.di_mode;
678 sb->st_uid = fp->f_di.di_uid;
679 sb->st_gid = fp->f_di.di_gid;
680 sb->st_size = fp->f_di.di_size;
685 ufs_readdir(struct open_file *f, struct dirent *d)
687 struct file *fp = (struct file *)f->f_fsdata;
694 * assume that a directory entry will not be split across blocks
697 if (fp->f_seekp >= fp->f_di.di_size)
699 error = buf_read_file(f, &buf, &buf_size);
702 dp = (struct direct *)buf;
703 fp->f_seekp += dp->d_reclen;
704 if (dp->d_ino == (ino_t)0)
706 d->d_type = dp->d_type;
707 strcpy(d->d_name, dp->d_name);
713 * Sanity checks for old file systems.
715 * XXX - goes away some day.
718 ffs_oldfscompat(struct fs *fs)
722 fs->fs_npsect = max(fs->fs_npsect, fs->fs_nsect); /* XXX */
723 fs->fs_interleave = max(fs->fs_interleave, 1); /* XXX */
724 if (fs->fs_postblformat == FS_42POSTBLFMT) /* XXX */
725 fs->fs_nrpos = 8; /* XXX */
726 if (fs->fs_inodefmt < FS_44INODEFMT) { /* XXX */
727 quad_t sizepb = fs->fs_bsize; /* XXX */
729 fs->fs_maxfilesize = fs->fs_bsize * NDADDR - 1; /* XXX */
730 for (i = 0; i < NIADDR; i++) { /* XXX */
731 sizepb *= NINDIR(fs); /* XXX */
732 fs->fs_maxfilesize += sizepb; /* XXX */
734 fs->fs_qbmask = ~fs->fs_bmask; /* XXX */
735 fs->fs_qfmask = ~fs->fs_fmask; /* XXX */