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.5 2005/03/13 15:10:03 swildner 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. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the University of
23 * California, Berkeley and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41 * Copyright (c) 1990, 1991 Carnegie Mellon University
42 * All Rights Reserved.
46 * Permission to use, copy, modify and distribute this software and its
47 * documentation is hereby granted, provided that both the copyright
48 * notice and this permission notice appear in all copies of the
49 * software, derivative works or modified versions, and any portions
50 * thereof, and that both notices appear in supporting documentation.
52 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
53 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
54 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
56 * Carnegie Mellon requests users of this software to return to
58 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
59 * School of Computer Science
60 * Carnegie Mellon University
61 * Pittsburgh PA 15213-3890
63 * any improvements or extensions that they make and grant Carnegie the
64 * rights to redistribute these changes.
68 * Stand-alone file reading package.
71 #include <sys/param.h>
73 #include <vfs/ufs/dinode.h>
74 #include <vfs/ufs/dir.h>
75 #include <vfs/ufs/fs.h>
79 static int ufs_open(const char *path, struct open_file *f);
80 static int ufs_close(struct open_file *f);
81 static int ufs_read(struct open_file *f, void *buf, size_t size, size_t *resid);
82 static off_t ufs_seek(struct open_file *f, off_t offset, int where);
83 static int ufs_stat(struct open_file *f, struct stat *sb);
84 static int ufs_readdir(struct open_file *f, struct dirent *d);
86 struct fs_ops ufs_fsops = {
101 off_t f_seekp; /* seek pointer */
102 struct fs *f_fs; /* pointer to super-block */
103 struct dinode f_di; /* copy of on-disk inode */
104 int f_nindir[NIADDR];
105 /* number of blocks mapped by
106 indirect block at level i */
107 char *f_blk[NIADDR]; /* buffer for indirect block at
109 size_t f_blksize[NIADDR];
111 daddr_t f_blkno[NIADDR];/* disk address of block in buffer */
112 char *f_buf; /* buffer for data block */
113 size_t f_buf_size; /* size of data block */
114 daddr_t f_buf_blkno; /* block number of data block */
117 static int read_inode(ino_t, struct open_file *);
118 static int block_map(struct open_file *, daddr_t, daddr_t *);
119 static int buf_read_file(struct open_file *, char **, size_t *);
120 static int search_directory(char *, struct open_file *, ino_t *);
122 static void ffs_oldfscompat(struct fs *);
126 * Read a new inode into a file structure.
129 read_inode(inumber, f)
133 struct file *fp = (struct file *)f->f_fsdata;
134 struct fs *fs = fp->f_fs;
143 * Read inode and save it.
145 buf = malloc(fs->fs_bsize);
147 rc = (f->f_dev->dv_strategy)(f->f_devdata, F_READ,
148 fsbtodb(fs, ino_to_fsba(fs, inumber)), fs->fs_bsize,
152 if (rsize != fs->fs_bsize) {
160 dp = (struct dinode *)buf;
161 fp->f_di = dp[ino_to_fsbo(fs, inumber)];
165 * Clear out the old buffers
170 for (level = 0; level < NIADDR; level++)
171 fp->f_blkno[level] = -1;
172 fp->f_buf_blkno = -1;
180 * Given an offset in a file, find the disk block number that
181 * contains that block.
184 block_map(f, file_block, disk_block_p)
187 daddr_t *disk_block_p; /* out */
189 struct file *fp = (struct file *)f->f_fsdata;
190 struct fs *fs = fp->f_fs;
193 daddr_t ind_block_num;
198 * Index structure of an inode:
200 * di_db[0..NDADDR-1] hold block numbers for blocks
203 * di_ib[0] index block 0 is the single indirect block
204 * holds block numbers for blocks
205 * NDADDR .. NDADDR + NINDIR(fs)-1
207 * di_ib[1] index block 1 is the double indirect block
208 * holds block numbers for INDEX blocks for blocks
209 * NDADDR + NINDIR(fs) ..
210 * NDADDR + NINDIR(fs) + NINDIR(fs)**2 - 1
212 * di_ib[2] index block 2 is the triple indirect block
213 * holds block numbers for double-indirect
215 * NDADDR + NINDIR(fs) + NINDIR(fs)**2 ..
216 * NDADDR + NINDIR(fs) + NINDIR(fs)**2
217 * + NINDIR(fs)**3 - 1
220 if (file_block < NDADDR) {
222 *disk_block_p = fp->f_di.di_db[file_block];
226 file_block -= NDADDR;
230 * nindir[1] = NINDIR**2
231 * nindir[2] = NINDIR**3
234 for (level = 0; level < NIADDR; level++) {
235 if (file_block < fp->f_nindir[level])
237 file_block -= fp->f_nindir[level];
239 if (level == NIADDR) {
240 /* Block number too high */
244 ind_block_num = fp->f_di.di_ib[level];
246 for (; level >= 0; level--) {
247 if (ind_block_num == 0) {
248 *disk_block_p = 0; /* missing */
252 if (fp->f_blkno[level] != ind_block_num) {
253 if (fp->f_blk[level] == (char *)0)
255 malloc(fs->fs_bsize);
257 rc = (f->f_dev->dv_strategy)(f->f_devdata, F_READ,
258 fsbtodb(fp->f_fs, ind_block_num),
261 &fp->f_blksize[level]);
264 if (fp->f_blksize[level] != fs->fs_bsize)
266 fp->f_blkno[level] = ind_block_num;
269 ind_p = (daddr_t *)fp->f_blk[level];
272 idx = file_block / fp->f_nindir[level - 1];
273 file_block %= fp->f_nindir[level - 1];
277 ind_block_num = ind_p[idx];
280 *disk_block_p = ind_block_num;
286 * Read a portion of a file into an internal buffer. Return
287 * the location in the buffer and the amount in the buffer.
290 buf_read_file(f, buf_p, size_p)
292 char **buf_p; /* out */
293 size_t *size_p; /* out */
295 struct file *fp = (struct file *)f->f_fsdata;
296 struct fs *fs = fp->f_fs;
303 off = blkoff(fs, fp->f_seekp);
304 file_block = lblkno(fs, fp->f_seekp);
305 block_size = dblksize(fs, &fp->f_di, file_block);
307 if (file_block != fp->f_buf_blkno) {
308 rc = block_map(f, file_block, &disk_block);
312 if (fp->f_buf == (char *)0)
313 fp->f_buf = malloc(fs->fs_bsize);
315 if (disk_block == 0) {
316 bzero(fp->f_buf, block_size);
317 fp->f_buf_size = block_size;
320 rc = (f->f_dev->dv_strategy)(f->f_devdata, F_READ,
321 fsbtodb(fs, disk_block),
322 block_size, fp->f_buf, &fp->f_buf_size);
327 fp->f_buf_blkno = file_block;
331 * Return address of byte in buffer corresponding to
332 * offset, and size of remainder of buffer after that
335 *buf_p = fp->f_buf + off;
336 *size_p = block_size - off;
339 * But truncate buffer at end of file.
341 if (*size_p > fp->f_di.di_size - fp->f_seekp)
342 *size_p = fp->f_di.di_size - fp->f_seekp;
348 * Search a directory for a name and return its
352 search_directory(name, f, inumber_p)
355 ino_t *inumber_p; /* out */
357 struct file *fp = (struct file *)f->f_fsdata;
365 length = strlen(name);
368 while (fp->f_seekp < fp->f_di.di_size) {
369 rc = buf_read_file(f, &buf, &buf_size);
373 dp = (struct direct *)buf;
374 edp = (struct direct *)(buf + buf_size);
376 if (dp->d_ino == (ino_t)0)
378 #if BYTE_ORDER == LITTLE_ENDIAN
379 if (fp->f_fs->fs_maxsymlinklen <= 0)
383 namlen = dp->d_namlen;
384 if (namlen == length &&
385 !strcmp(name, dp->d_name)) {
387 *inumber_p = dp->d_ino;
391 dp = (struct direct *)((char *)dp + dp->d_reclen);
393 fp->f_seekp += buf_size;
408 ino_t inumber, parent_inumber;
414 char namebuf[MAXPATHLEN+1];
418 /* allocate file system specific data structure */
419 fp = malloc(sizeof(struct file));
420 bzero(fp, sizeof(struct file));
421 f->f_fsdata = (void *)fp;
423 /* allocate space and read super block */
427 rc = (f->f_dev->dv_strategy)(f->f_devdata, F_READ,
428 SBLOCK, SBSIZE, (char *)fs, &buf_size);
432 if (buf_size != SBSIZE || fs->fs_magic != FS_MAGIC ||
433 fs->fs_bsize > MAXBSIZE || fs->fs_bsize < sizeof(struct fs)) {
442 * Calculate indirect block levels.
449 for (level = 0; level < NIADDR; level++) {
451 fp->f_nindir[level] = mult;
456 if ((rc = read_inode(inumber, f)) != 0)
459 cp = path = strdup(upath);
467 * Remove extra separators
475 * Check that current node is a directory.
477 if ((fp->f_di.di_mode & IFMT) != IFDIR) {
483 * Get next component of path name.
489 while ((c = *cp) != '\0' && c != '/') {
490 if (++len > MAXNAMLEN) {
500 * Look up component in current directory.
501 * Save directory inumber in case we find a
504 parent_inumber = inumber;
505 rc = search_directory(ncp, f, &inumber);
511 * Open next component.
513 if ((rc = read_inode(inumber, f)) != 0)
517 * Check for symbolic link.
519 if ((fp->f_di.di_mode & IFMT) == IFLNK) {
520 int link_len = fp->f_di.di_size;
525 if (link_len + len > MAXPATHLEN ||
526 ++nlinks > MAXSYMLINKS) {
531 bcopy(cp, &namebuf[link_len], len + 1);
533 if (link_len < fs->fs_maxsymlinklen) {
534 bcopy(fp->f_di.di_shortlink, namebuf,
535 (unsigned) link_len);
538 * Read file for symbolic link
542 struct fs *fs = fp->f_fs;
545 buf = malloc(fs->fs_bsize);
546 rc = block_map(f, (daddr_t)0, &disk_block);
551 rc = (f->f_dev->dv_strategy)(f->f_devdata,
552 F_READ, fsbtodb(fs, disk_block),
553 fs->fs_bsize, buf, &buf_size);
557 bcopy((char *)buf, namebuf, (unsigned)link_len);
561 * If relative pathname, restart at parent directory.
562 * If absolute pathname, restart at root.
566 inumber = parent_inumber;
568 inumber = (ino_t)ROOTINO;
570 if ((rc = read_inode(inumber, f)) != 0)
576 * Found terminal component.
597 struct file *fp = (struct file *)f->f_fsdata;
600 f->f_fsdata = (void *)0;
601 if (fp == (struct file *)0)
604 for (level = 0; level < NIADDR; level++) {
605 if (fp->f_blk[level])
606 free(fp->f_blk[level]);
616 * Copy a portion of a file into kernel memory.
617 * Cross block boundaries when necessary.
620 ufs_read(f, start, size, resid)
624 size_t *resid; /* out */
626 struct file *fp = (struct file *)f->f_fsdata;
634 if (fp->f_seekp >= fp->f_di.di_size)
637 rc = buf_read_file(f, &buf, &buf_size);
642 if (csize > buf_size)
645 bcopy(buf, addr, csize);
647 fp->f_seekp += csize;
657 ufs_seek(f, offset, where)
662 struct file *fp = (struct file *)f->f_fsdata;
666 fp->f_seekp = offset;
669 fp->f_seekp += offset;
672 fp->f_seekp = fp->f_di.di_size - offset;
677 return (fp->f_seekp);
685 struct file *fp = (struct file *)f->f_fsdata;
687 /* only important stuff */
688 sb->st_mode = fp->f_di.di_mode;
689 sb->st_uid = fp->f_di.di_uid;
690 sb->st_gid = fp->f_di.di_gid;
691 sb->st_size = fp->f_di.di_size;
696 ufs_readdir(struct open_file *f, struct dirent *d)
698 struct file *fp = (struct file *)f->f_fsdata;
705 * assume that a directory entry will not be split across blocks
708 if (fp->f_seekp >= fp->f_di.di_size)
710 error = buf_read_file(f, &buf, &buf_size);
713 dp = (struct direct *)buf;
714 fp->f_seekp += dp->d_reclen;
715 if (dp->d_ino == (ino_t)0)
717 d->d_type = dp->d_type;
718 strcpy(d->d_name, dp->d_name);
724 * Sanity checks for old file systems.
726 * XXX - goes away some day.
734 fs->fs_npsect = max(fs->fs_npsect, fs->fs_nsect); /* XXX */
735 fs->fs_interleave = max(fs->fs_interleave, 1); /* XXX */
736 if (fs->fs_postblformat == FS_42POSTBLFMT) /* XXX */
737 fs->fs_nrpos = 8; /* XXX */
738 if (fs->fs_inodefmt < FS_44INODEFMT) { /* XXX */
739 quad_t sizepb = fs->fs_bsize; /* XXX */
741 fs->fs_maxfilesize = fs->fs_bsize * NDADDR - 1; /* XXX */
742 for (i = 0; i < NIADDR; i++) { /* XXX */
743 sizepb *= NINDIR(fs); /* XXX */
744 fs->fs_maxfilesize += sizepb; /* XXX */
746 fs->fs_qbmask = ~fs->fs_bmask; /* XXX */
747 fs->fs_qfmask = ~fs->fs_fmask; /* XXX */