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.3 2003/08/08 04:18:34 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. 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>
80 #define COMPAT_UFS /* DUX has old format file systems */
83 static int ufs_open(const char *path, struct open_file *f);
84 static int ufs_close(struct open_file *f);
85 static int ufs_read(struct open_file *f, void *buf, size_t size, size_t *resid);
86 static off_t ufs_seek(struct open_file *f, off_t offset, int where);
87 static int ufs_stat(struct open_file *f, struct stat *sb);
88 static int ufs_readdir(struct open_file *f, struct dirent *d);
90 struct fs_ops ufs_fsops = {
105 off_t f_seekp; /* seek pointer */
106 struct fs *f_fs; /* pointer to super-block */
107 struct dinode f_di; /* copy of on-disk inode */
108 int f_nindir[NIADDR];
109 /* number of blocks mapped by
110 indirect block at level i */
111 char *f_blk[NIADDR]; /* buffer for indirect block at
113 size_t f_blksize[NIADDR];
115 daddr_t f_blkno[NIADDR];/* disk address of block in buffer */
116 char *f_buf; /* buffer for data block */
117 size_t f_buf_size; /* size of data block */
118 daddr_t f_buf_blkno; /* block number of data block */
121 static int read_inode(ino_t, struct open_file *);
122 static int block_map(struct open_file *, daddr_t, daddr_t *);
123 static int buf_read_file(struct open_file *, char **, size_t *);
124 static int search_directory(char *, struct open_file *, ino_t *);
126 static void ffs_oldfscompat(struct fs *);
130 * Read a new inode into a file structure.
133 read_inode(inumber, f)
137 register struct file *fp = (struct file *)f->f_fsdata;
138 register struct fs *fs = fp->f_fs;
147 * Read inode and save it.
149 buf = malloc(fs->fs_bsize);
151 rc = (f->f_dev->dv_strategy)(f->f_devdata, F_READ,
152 fsbtodb(fs, ino_to_fsba(fs, inumber)), fs->fs_bsize,
156 if (rsize != fs->fs_bsize) {
162 register struct dinode *dp;
164 dp = (struct dinode *)buf;
165 fp->f_di = dp[ino_to_fsbo(fs, inumber)];
169 * Clear out the old buffers
174 for (level = 0; level < NIADDR; level++)
175 fp->f_blkno[level] = -1;
176 fp->f_buf_blkno = -1;
184 * Given an offset in a file, find the disk block number that
185 * contains that block.
188 block_map(f, file_block, disk_block_p)
191 daddr_t *disk_block_p; /* out */
193 register struct file *fp = (struct file *)f->f_fsdata;
194 register struct fs *fs = fp->f_fs;
197 daddr_t ind_block_num;
202 * Index structure of an inode:
204 * di_db[0..NDADDR-1] hold block numbers for blocks
207 * di_ib[0] index block 0 is the single indirect block
208 * holds block numbers for blocks
209 * NDADDR .. NDADDR + NINDIR(fs)-1
211 * di_ib[1] index block 1 is the double indirect block
212 * holds block numbers for INDEX blocks for blocks
213 * NDADDR + NINDIR(fs) ..
214 * NDADDR + NINDIR(fs) + NINDIR(fs)**2 - 1
216 * di_ib[2] index block 2 is the triple indirect block
217 * holds block numbers for double-indirect
219 * NDADDR + NINDIR(fs) + NINDIR(fs)**2 ..
220 * NDADDR + NINDIR(fs) + NINDIR(fs)**2
221 * + NINDIR(fs)**3 - 1
224 if (file_block < NDADDR) {
226 *disk_block_p = fp->f_di.di_db[file_block];
230 file_block -= NDADDR;
234 * nindir[1] = NINDIR**2
235 * nindir[2] = NINDIR**3
238 for (level = 0; level < NIADDR; level++) {
239 if (file_block < fp->f_nindir[level])
241 file_block -= fp->f_nindir[level];
243 if (level == NIADDR) {
244 /* Block number too high */
248 ind_block_num = fp->f_di.di_ib[level];
250 for (; level >= 0; level--) {
251 if (ind_block_num == 0) {
252 *disk_block_p = 0; /* missing */
256 if (fp->f_blkno[level] != ind_block_num) {
257 if (fp->f_blk[level] == (char *)0)
259 malloc(fs->fs_bsize);
261 rc = (f->f_dev->dv_strategy)(f->f_devdata, F_READ,
262 fsbtodb(fp->f_fs, ind_block_num),
265 &fp->f_blksize[level]);
268 if (fp->f_blksize[level] != fs->fs_bsize)
270 fp->f_blkno[level] = ind_block_num;
273 ind_p = (daddr_t *)fp->f_blk[level];
276 idx = file_block / fp->f_nindir[level - 1];
277 file_block %= fp->f_nindir[level - 1];
281 ind_block_num = ind_p[idx];
284 *disk_block_p = ind_block_num;
290 * Read a portion of a file into an internal buffer. Return
291 * the location in the buffer and the amount in the buffer.
294 buf_read_file(f, buf_p, size_p)
296 char **buf_p; /* out */
297 size_t *size_p; /* out */
299 register struct file *fp = (struct file *)f->f_fsdata;
300 register struct fs *fs = fp->f_fs;
302 register daddr_t file_block;
307 off = blkoff(fs, fp->f_seekp);
308 file_block = lblkno(fs, fp->f_seekp);
309 block_size = dblksize(fs, &fp->f_di, file_block);
311 if (file_block != fp->f_buf_blkno) {
312 rc = block_map(f, file_block, &disk_block);
316 if (fp->f_buf == (char *)0)
317 fp->f_buf = malloc(fs->fs_bsize);
319 if (disk_block == 0) {
320 bzero(fp->f_buf, block_size);
321 fp->f_buf_size = block_size;
324 rc = (f->f_dev->dv_strategy)(f->f_devdata, F_READ,
325 fsbtodb(fs, disk_block),
326 block_size, fp->f_buf, &fp->f_buf_size);
331 fp->f_buf_blkno = file_block;
335 * Return address of byte in buffer corresponding to
336 * offset, and size of remainder of buffer after that
339 *buf_p = fp->f_buf + off;
340 *size_p = block_size - off;
343 * But truncate buffer at end of file.
345 if (*size_p > fp->f_di.di_size - fp->f_seekp)
346 *size_p = fp->f_di.di_size - fp->f_seekp;
352 * Search a directory for a name and return its
356 search_directory(name, f, inumber_p)
359 ino_t *inumber_p; /* out */
361 register struct file *fp = (struct file *)f->f_fsdata;
362 register struct direct *dp;
369 length = strlen(name);
372 while (fp->f_seekp < fp->f_di.di_size) {
373 rc = buf_read_file(f, &buf, &buf_size);
377 dp = (struct direct *)buf;
378 edp = (struct direct *)(buf + buf_size);
380 if (dp->d_ino == (ino_t)0)
382 #if BYTE_ORDER == LITTLE_ENDIAN
383 if (fp->f_fs->fs_maxsymlinklen <= 0)
387 namlen = dp->d_namlen;
388 if (namlen == length &&
389 !strcmp(name, dp->d_name)) {
391 *inumber_p = dp->d_ino;
395 dp = (struct direct *)((char *)dp + dp->d_reclen);
397 fp->f_seekp += buf_size;
410 register char *cp, *ncp;
412 ino_t inumber, parent_inumber;
418 char namebuf[MAXPATHLEN+1];
422 /* allocate file system specific data structure */
423 fp = malloc(sizeof(struct file));
424 bzero(fp, sizeof(struct file));
425 f->f_fsdata = (void *)fp;
427 /* allocate space and read super block */
431 rc = (f->f_dev->dv_strategy)(f->f_devdata, F_READ,
432 SBLOCK, SBSIZE, (char *)fs, &buf_size);
436 if (buf_size != SBSIZE || fs->fs_magic != FS_MAGIC ||
437 fs->fs_bsize > MAXBSIZE || fs->fs_bsize < sizeof(struct fs)) {
446 * Calculate indirect block levels.
453 for (level = 0; level < NIADDR; level++) {
455 fp->f_nindir[level] = mult;
460 if ((rc = read_inode(inumber, f)) != 0)
463 cp = path = strdup(upath);
471 * Remove extra separators
479 * Check that current node is a directory.
481 if ((fp->f_di.di_mode & IFMT) != IFDIR) {
487 * Get next component of path name.
490 register int len = 0;
493 while ((c = *cp) != '\0' && c != '/') {
494 if (++len > MAXNAMLEN) {
504 * Look up component in current directory.
505 * Save directory inumber in case we find a
508 parent_inumber = inumber;
509 rc = search_directory(ncp, f, &inumber);
515 * Open next component.
517 if ((rc = read_inode(inumber, f)) != 0)
521 * Check for symbolic link.
523 if ((fp->f_di.di_mode & IFMT) == IFLNK) {
524 int link_len = fp->f_di.di_size;
529 if (link_len + len > MAXPATHLEN ||
530 ++nlinks > MAXSYMLINKS) {
535 bcopy(cp, &namebuf[link_len], len + 1);
537 if (link_len < fs->fs_maxsymlinklen) {
538 bcopy(fp->f_di.di_shortlink, namebuf,
539 (unsigned) link_len);
542 * Read file for symbolic link
546 register struct fs *fs = fp->f_fs;
549 buf = malloc(fs->fs_bsize);
550 rc = block_map(f, (daddr_t)0, &disk_block);
555 rc = (f->f_dev->dv_strategy)(f->f_devdata,
556 F_READ, fsbtodb(fs, disk_block),
557 fs->fs_bsize, buf, &buf_size);
561 bcopy((char *)buf, namebuf, (unsigned)link_len);
565 * If relative pathname, restart at parent directory.
566 * If absolute pathname, restart at root.
570 inumber = parent_inumber;
572 inumber = (ino_t)ROOTINO;
574 if ((rc = read_inode(inumber, f)) != 0)
580 * Found terminal component.
601 register struct file *fp = (struct file *)f->f_fsdata;
604 f->f_fsdata = (void *)0;
605 if (fp == (struct file *)0)
608 for (level = 0; level < NIADDR; level++) {
609 if (fp->f_blk[level])
610 free(fp->f_blk[level]);
620 * Copy a portion of a file into kernel memory.
621 * Cross block boundaries when necessary.
624 ufs_read(f, start, size, resid)
628 size_t *resid; /* out */
630 register struct file *fp = (struct file *)f->f_fsdata;
631 register size_t csize;
635 register char *addr = start;
638 if (fp->f_seekp >= fp->f_di.di_size)
641 rc = buf_read_file(f, &buf, &buf_size);
646 if (csize > buf_size)
649 bcopy(buf, addr, csize);
651 fp->f_seekp += csize;
661 ufs_seek(f, offset, where)
666 register struct file *fp = (struct file *)f->f_fsdata;
670 fp->f_seekp = offset;
673 fp->f_seekp += offset;
676 fp->f_seekp = fp->f_di.di_size - offset;
681 return (fp->f_seekp);
689 register struct file *fp = (struct file *)f->f_fsdata;
691 /* only important stuff */
692 sb->st_mode = fp->f_di.di_mode;
693 sb->st_uid = fp->f_di.di_uid;
694 sb->st_gid = fp->f_di.di_gid;
695 sb->st_size = fp->f_di.di_size;
700 ufs_readdir(struct open_file *f, struct dirent *d)
702 struct file *fp = (struct file *)f->f_fsdata;
709 * assume that a directory entry will not be split across blocks
712 if (fp->f_seekp >= fp->f_di.di_size)
714 error = buf_read_file(f, &buf, &buf_size);
717 dp = (struct direct *)buf;
718 fp->f_seekp += dp->d_reclen;
719 if (dp->d_ino == (ino_t)0)
721 d->d_type = dp->d_type;
722 strcpy(d->d_name, dp->d_name);
728 * Sanity checks for old file systems.
730 * XXX - goes away some day.
738 fs->fs_npsect = max(fs->fs_npsect, fs->fs_nsect); /* XXX */
739 fs->fs_interleave = max(fs->fs_interleave, 1); /* XXX */
740 if (fs->fs_postblformat == FS_42POSTBLFMT) /* XXX */
741 fs->fs_nrpos = 8; /* XXX */
742 if (fs->fs_inodefmt < FS_44INODEFMT) { /* XXX */
743 quad_t sizepb = fs->fs_bsize; /* XXX */
745 fs->fs_maxfilesize = fs->fs_bsize * NDADDR - 1; /* XXX */
746 for (i = 0; i < NIADDR; i++) { /* XXX */
747 sizepb *= NINDIR(fs); /* XXX */
748 fs->fs_maxfilesize += sizepb; /* XXX */
750 fs->fs_qbmask = ~fs->fs_bmask; /* XXX */
751 fs->fs_qfmask = ~fs->fs_fmask; /* XXX */