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.6 2005/08/27 20:23:05 joerg 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>
76 #include <vfs/ufs/dinode.h>
77 #include <vfs/ufs/dir.h>
78 #include <vfs/ufs/fs.h>
81 static int ufs_open(const char *path, struct open_file *f);
82 static int ufs_close(struct open_file *f);
83 static int ufs_read(struct open_file *f, void *buf, size_t size, size_t *resid);
84 static off_t ufs_seek(struct open_file *f, off_t offset, int where);
85 static int ufs_stat(struct open_file *f, struct stat *sb);
86 static int ufs_readdir(struct open_file *f, struct dirent *d);
88 struct fs_ops ufs_fsops = {
103 off_t f_seekp; /* seek pointer */
104 struct fs *f_fs; /* pointer to super-block */
105 struct dinode f_di; /* copy of on-disk inode */
106 int f_nindir[NIADDR];
107 /* number of blocks mapped by
108 indirect block at level i */
109 char *f_blk[NIADDR]; /* buffer for indirect block at
111 size_t f_blksize[NIADDR];
113 daddr_t f_blkno[NIADDR];/* disk address of block in buffer */
114 char *f_buf; /* buffer for data block */
115 size_t f_buf_size; /* size of data block */
116 daddr_t f_buf_blkno; /* block number of data block */
119 static int read_inode(ino_t, struct open_file *);
120 static int block_map(struct open_file *, daddr_t, daddr_t *);
121 static int buf_read_file(struct open_file *, char **, size_t *);
122 static int search_directory(char *, struct open_file *, ino_t *);
124 static void ffs_oldfscompat(struct fs *);
128 * Read a new inode into a file structure.
131 read_inode(inumber, f)
135 struct file *fp = (struct file *)f->f_fsdata;
136 struct fs *fs = fp->f_fs;
145 * Read inode and save it.
147 buf = malloc(fs->fs_bsize);
149 rc = (f->f_dev->dv_strategy)(f->f_devdata, F_READ,
150 fsbtodb(fs, ino_to_fsba(fs, inumber)), fs->fs_bsize,
154 if (rsize != fs->fs_bsize) {
162 dp = (struct dinode *)buf;
163 fp->f_di = dp[ino_to_fsbo(fs, inumber)];
167 * Clear out the old buffers
172 for (level = 0; level < NIADDR; level++)
173 fp->f_blkno[level] = -1;
174 fp->f_buf_blkno = -1;
182 * Given an offset in a file, find the disk block number that
183 * contains that block.
186 block_map(f, file_block, disk_block_p)
189 daddr_t *disk_block_p; /* out */
191 struct file *fp = (struct file *)f->f_fsdata;
192 struct fs *fs = fp->f_fs;
195 daddr_t ind_block_num;
200 * Index structure of an inode:
202 * di_db[0..NDADDR-1] hold block numbers for blocks
205 * di_ib[0] index block 0 is the single indirect block
206 * holds block numbers for blocks
207 * NDADDR .. NDADDR + NINDIR(fs)-1
209 * di_ib[1] index block 1 is the double indirect block
210 * holds block numbers for INDEX blocks for blocks
211 * NDADDR + NINDIR(fs) ..
212 * NDADDR + NINDIR(fs) + NINDIR(fs)**2 - 1
214 * di_ib[2] index block 2 is the triple indirect block
215 * holds block numbers for double-indirect
217 * NDADDR + NINDIR(fs) + NINDIR(fs)**2 ..
218 * NDADDR + NINDIR(fs) + NINDIR(fs)**2
219 * + NINDIR(fs)**3 - 1
222 if (file_block < NDADDR) {
224 *disk_block_p = fp->f_di.di_db[file_block];
228 file_block -= NDADDR;
232 * nindir[1] = NINDIR**2
233 * nindir[2] = NINDIR**3
236 for (level = 0; level < NIADDR; level++) {
237 if (file_block < fp->f_nindir[level])
239 file_block -= fp->f_nindir[level];
241 if (level == NIADDR) {
242 /* Block number too high */
246 ind_block_num = fp->f_di.di_ib[level];
248 for (; level >= 0; level--) {
249 if (ind_block_num == 0) {
250 *disk_block_p = 0; /* missing */
254 if (fp->f_blkno[level] != ind_block_num) {
255 if (fp->f_blk[level] == (char *)0)
257 malloc(fs->fs_bsize);
259 rc = (f->f_dev->dv_strategy)(f->f_devdata, F_READ,
260 fsbtodb(fp->f_fs, ind_block_num),
263 &fp->f_blksize[level]);
266 if (fp->f_blksize[level] != fs->fs_bsize)
268 fp->f_blkno[level] = ind_block_num;
271 ind_p = (daddr_t *)fp->f_blk[level];
274 idx = file_block / fp->f_nindir[level - 1];
275 file_block %= fp->f_nindir[level - 1];
279 ind_block_num = ind_p[idx];
282 *disk_block_p = ind_block_num;
288 * Read a portion of a file into an internal buffer. Return
289 * the location in the buffer and the amount in the buffer.
292 buf_read_file(f, buf_p, size_p)
294 char **buf_p; /* out */
295 size_t *size_p; /* out */
297 struct file *fp = (struct file *)f->f_fsdata;
298 struct fs *fs = fp->f_fs;
305 off = blkoff(fs, fp->f_seekp);
306 file_block = lblkno(fs, fp->f_seekp);
307 block_size = dblksize(fs, &fp->f_di, file_block);
309 if (file_block != fp->f_buf_blkno) {
310 rc = block_map(f, file_block, &disk_block);
314 if (fp->f_buf == (char *)0)
315 fp->f_buf = malloc(fs->fs_bsize);
317 if (disk_block == 0) {
318 bzero(fp->f_buf, block_size);
319 fp->f_buf_size = block_size;
322 rc = (f->f_dev->dv_strategy)(f->f_devdata, F_READ,
323 fsbtodb(fs, disk_block),
324 block_size, fp->f_buf, &fp->f_buf_size);
329 fp->f_buf_blkno = file_block;
333 * Return address of byte in buffer corresponding to
334 * offset, and size of remainder of buffer after that
337 *buf_p = fp->f_buf + off;
338 *size_p = block_size - off;
341 * But truncate buffer at end of file.
343 if (*size_p > fp->f_di.di_size - fp->f_seekp)
344 *size_p = fp->f_di.di_size - fp->f_seekp;
350 * Search a directory for a name and return its
354 search_directory(name, f, inumber_p)
357 ino_t *inumber_p; /* out */
359 struct file *fp = (struct file *)f->f_fsdata;
367 length = strlen(name);
370 while (fp->f_seekp < fp->f_di.di_size) {
371 rc = buf_read_file(f, &buf, &buf_size);
375 dp = (struct direct *)buf;
376 edp = (struct direct *)(buf + buf_size);
378 if (dp->d_ino == (ino_t)0)
380 #if BYTE_ORDER == LITTLE_ENDIAN
381 if (fp->f_fs->fs_maxsymlinklen <= 0)
385 namlen = dp->d_namlen;
386 if (namlen == length &&
387 !strcmp(name, dp->d_name)) {
389 *inumber_p = dp->d_ino;
393 dp = (struct direct *)((char *)dp + dp->d_reclen);
395 fp->f_seekp += buf_size;
410 ino_t inumber, parent_inumber;
416 char namebuf[MAXPATHLEN+1];
420 /* allocate file system specific data structure */
421 fp = malloc(sizeof(struct file));
422 bzero(fp, sizeof(struct file));
423 f->f_fsdata = (void *)fp;
425 /* allocate space and read super block */
429 rc = (f->f_dev->dv_strategy)(f->f_devdata, F_READ,
430 SBLOCK, SBSIZE, (char *)fs, &buf_size);
434 if (buf_size != SBSIZE || fs->fs_magic != FS_MAGIC ||
435 fs->fs_bsize > MAXBSIZE || fs->fs_bsize < sizeof(struct fs)) {
444 * Calculate indirect block levels.
451 for (level = 0; level < NIADDR; level++) {
453 fp->f_nindir[level] = mult;
458 if ((rc = read_inode(inumber, f)) != 0)
461 cp = path = strdup(upath);
469 * Remove extra separators
477 * Check that current node is a directory.
479 if ((fp->f_di.di_mode & IFMT) != IFDIR) {
485 * Get next component of path name.
491 while ((c = *cp) != '\0' && c != '/') {
492 if (++len > MAXNAMLEN) {
502 * Look up component in current directory.
503 * Save directory inumber in case we find a
506 parent_inumber = inumber;
507 rc = search_directory(ncp, f, &inumber);
513 * Open next component.
515 if ((rc = read_inode(inumber, f)) != 0)
519 * Check for symbolic link.
521 if ((fp->f_di.di_mode & IFMT) == IFLNK) {
522 int link_len = fp->f_di.di_size;
527 if (link_len + len > MAXPATHLEN ||
528 ++nlinks > MAXSYMLINKS) {
533 bcopy(cp, &namebuf[link_len], len + 1);
535 if (link_len < fs->fs_maxsymlinklen) {
536 bcopy(fp->f_di.di_shortlink, namebuf,
537 (unsigned) link_len);
540 * Read file for symbolic link
544 struct fs *fs = fp->f_fs;
547 buf = malloc(fs->fs_bsize);
548 rc = block_map(f, (daddr_t)0, &disk_block);
553 rc = (f->f_dev->dv_strategy)(f->f_devdata,
554 F_READ, fsbtodb(fs, disk_block),
555 fs->fs_bsize, buf, &buf_size);
559 bcopy((char *)buf, namebuf, (unsigned)link_len);
563 * If relative pathname, restart at parent directory.
564 * If absolute pathname, restart at root.
568 inumber = parent_inumber;
570 inumber = (ino_t)ROOTINO;
572 if ((rc = read_inode(inumber, f)) != 0)
578 * Found terminal component.
599 struct file *fp = (struct file *)f->f_fsdata;
602 f->f_fsdata = (void *)0;
603 if (fp == (struct file *)0)
606 for (level = 0; level < NIADDR; level++) {
607 if (fp->f_blk[level])
608 free(fp->f_blk[level]);
618 * Copy a portion of a file into kernel memory.
619 * Cross block boundaries when necessary.
622 ufs_read(f, start, size, resid)
626 size_t *resid; /* out */
628 struct file *fp = (struct file *)f->f_fsdata;
636 if (fp->f_seekp >= fp->f_di.di_size)
639 rc = buf_read_file(f, &buf, &buf_size);
644 if (csize > buf_size)
647 bcopy(buf, addr, csize);
649 fp->f_seekp += csize;
659 ufs_seek(f, offset, where)
664 struct file *fp = (struct file *)f->f_fsdata;
668 fp->f_seekp = offset;
671 fp->f_seekp += offset;
674 fp->f_seekp = fp->f_di.di_size - offset;
679 return (fp->f_seekp);
687 struct file *fp = (struct file *)f->f_fsdata;
689 /* only important stuff */
690 sb->st_mode = fp->f_di.di_mode;
691 sb->st_uid = fp->f_di.di_uid;
692 sb->st_gid = fp->f_di.di_gid;
693 sb->st_size = fp->f_di.di_size;
698 ufs_readdir(struct open_file *f, struct dirent *d)
700 struct file *fp = (struct file *)f->f_fsdata;
707 * assume that a directory entry will not be split across blocks
710 if (fp->f_seekp >= fp->f_di.di_size)
712 error = buf_read_file(f, &buf, &buf_size);
715 dp = (struct direct *)buf;
716 fp->f_seekp += dp->d_reclen;
717 if (dp->d_ino == (ino_t)0)
719 d->d_type = dp->d_type;
720 strcpy(d->d_name, dp->d_name);
726 * Sanity checks for old file systems.
728 * XXX - goes away some day.
736 fs->fs_npsect = max(fs->fs_npsect, fs->fs_nsect); /* XXX */
737 fs->fs_interleave = max(fs->fs_interleave, 1); /* XXX */
738 if (fs->fs_postblformat == FS_42POSTBLFMT) /* XXX */
739 fs->fs_nrpos = 8; /* XXX */
740 if (fs->fs_inodefmt < FS_44INODEFMT) { /* XXX */
741 quad_t sizepb = fs->fs_bsize; /* XXX */
743 fs->fs_maxfilesize = fs->fs_bsize * NDADDR - 1; /* XXX */
744 for (i = 0; i < NIADDR; i++) { /* XXX */
745 sizepb *= NINDIR(fs); /* XXX */
746 fs->fs_maxfilesize += sizepb; /* XXX */
748 fs->fs_qbmask = ~fs->fs_bmask; /* XXX */
749 fs->fs_qfmask = ~fs->fs_fmask; /* XXX */