1 /* $NetBSD: ufs.c,v 1.20 1998/03/01 07:15:39 ross Exp $ */
4 * Copyright (c) 2002 Networks Associates Technology, Inc.
7 * This software was developed for the FreeBSD Project by Marshall
8 * Kirk McKusick and Network Associates Laboratories, the Security
9 * Research Division of Network Associates, Inc. under DARPA/SPAWAR
10 * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS
13 * Copyright (c) 1982, 1989, 1993
14 * The Regents of the University of California. All rights reserved.
16 * This code is derived from software contributed to Berkeley by
17 * The Mach Operating System project at Carnegie-Mellon University.
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions
22 * 1. Redistributions of source code must retain the above copyright
23 * notice, this list of conditions and the following disclaimer.
24 * 2. Redistributions in binary form must reproduce the above copyright
25 * notice, this list of conditions and the following disclaimer in the
26 * documentation and/or other materials provided with the distribution.
27 * 3. All advertising materials mentioning features or use of this software
28 * must display the following acknowledgement:
29 * This product includes software developed by the University of
30 * California, Berkeley and its contributors.
31 * 4. Neither the name of the University nor the names of its contributors
32 * may be used to endorse or promote products derived from this software
33 * without specific prior written permission.
35 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
36 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
38 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
39 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
40 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
41 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
42 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
43 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
44 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48 * Copyright (c) 1990, 1991 Carnegie Mellon University
49 * All Rights Reserved.
53 * Permission to use, copy, modify and distribute this software and its
54 * documentation is hereby granted, provided that both the copyright
55 * notice and this permission notice appear in all copies of the
56 * software, derivative works or modified versions, and any portions
57 * thereof, and that both notices appear in supporting documentation.
59 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
60 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
61 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
63 * Carnegie Mellon requests users of this software to return to
65 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
66 * School of Computer Science
67 * Carnegie Mellon University
68 * Pittsburgh PA 15213-3890
70 * any improvements or extensions that they make and grant Carnegie the
71 * rights to redistribute these changes.
74 #include <sys/cdefs.h>
75 __FBSDID("$FreeBSD$");
78 * Stand-alone file reading package.
81 #include <sys/param.h>
82 #include <sys/disklabel.h>
84 #include <ufs/ufs/dinode.h>
85 #include <ufs/ufs/dir.h>
86 #include <ufs/ffs/fs.h>
91 #define COMPAT_UFS /* DUX has old format file systems */
94 static int ufs_open(const char *path, struct open_file *f);
95 static int ufs_write(struct open_file *f, void *buf, size_t size, size_t *resid);
96 static int ufs_close(struct open_file *f);
97 static int ufs_read(struct open_file *f, void *buf, size_t size, size_t *resid);
98 static off_t ufs_seek(struct open_file *f, off_t offset, int where);
99 static int ufs_stat(struct open_file *f, struct stat *sb);
100 static int ufs_readdir(struct open_file *f, struct dirent *d);
102 struct fs_ops ufs_fsops = {
117 off_t f_seekp; /* seek pointer */
118 struct fs *f_fs; /* pointer to super-block */
120 struct ufs1_dinode di1;
121 struct ufs2_dinode di2;
122 } f_di; /* copy of on-disk inode */
123 int f_nindir[NIADDR];
124 /* number of blocks mapped by
125 indirect block at level i */
126 char *f_blk[NIADDR]; /* buffer for indirect block at
128 size_t f_blksize[NIADDR];
130 ufs2_daddr_t f_blkno[NIADDR];/* disk address of block in buffer */
131 ufs2_daddr_t f_buf_blkno; /* block number of data block */
132 char *f_buf; /* buffer for data block */
133 size_t f_buf_size; /* size of data block */
135 #define DIP(fp, field) \
136 ((fp)->f_fs->fs_magic == FS_UFS1_MAGIC ? \
137 (fp)->f_di.di1.field : (fp)->f_di.di2.field)
139 static int read_inode(ino_t, struct open_file *);
140 static int block_map(struct open_file *, ufs2_daddr_t, ufs2_daddr_t *);
141 static int buf_read_file(struct open_file *, char **, size_t *);
142 static int buf_write_file(struct open_file *, char *, size_t *);
143 static int search_directory(char *, struct open_file *, ino_t *);
146 * Read a new inode into a file structure.
149 read_inode(inumber, f)
153 struct file *fp = (struct file *)f->f_fsdata;
154 struct fs *fs = fp->f_fs;
163 * Read inode and save it.
165 buf = malloc(fs->fs_bsize);
167 rc = (f->f_dev->dv_strategy)(f->f_devdata, F_READ,
168 fsbtodb(fs, ino_to_fsba(fs, inumber)), fs->fs_bsize,
172 if (rsize != fs->fs_bsize) {
177 if (fp->f_fs->fs_magic == FS_UFS1_MAGIC)
178 fp->f_di.di1 = ((struct ufs1_dinode *)buf)
179 [ino_to_fsbo(fs, inumber)];
181 fp->f_di.di2 = ((struct ufs2_dinode *)buf)
182 [ino_to_fsbo(fs, inumber)];
185 * Clear out the old buffers
190 for (level = 0; level < NIADDR; level++)
191 fp->f_blkno[level] = -1;
192 fp->f_buf_blkno = -1;
200 * Given an offset in a file, find the disk block number that
201 * contains that block.
204 block_map(f, file_block, disk_block_p)
206 ufs2_daddr_t file_block;
207 ufs2_daddr_t *disk_block_p; /* out */
209 struct file *fp = (struct file *)f->f_fsdata;
210 struct fs *fs = fp->f_fs;
213 ufs2_daddr_t ind_block_num;
217 * Index structure of an inode:
219 * di_db[0..NDADDR-1] hold block numbers for blocks
222 * di_ib[0] index block 0 is the single indirect block
223 * holds block numbers for blocks
224 * NDADDR .. NDADDR + NINDIR(fs)-1
226 * di_ib[1] index block 1 is the double indirect block
227 * holds block numbers for INDEX blocks for blocks
228 * NDADDR + NINDIR(fs) ..
229 * NDADDR + NINDIR(fs) + NINDIR(fs)**2 - 1
231 * di_ib[2] index block 2 is the triple indirect block
232 * holds block numbers for double-indirect
234 * NDADDR + NINDIR(fs) + NINDIR(fs)**2 ..
235 * NDADDR + NINDIR(fs) + NINDIR(fs)**2
236 * + NINDIR(fs)**3 - 1
239 if (file_block < NDADDR) {
241 *disk_block_p = DIP(fp, di_db[file_block]);
245 file_block -= NDADDR;
249 * nindir[1] = NINDIR**2
250 * nindir[2] = NINDIR**3
253 for (level = 0; level < NIADDR; level++) {
254 if (file_block < fp->f_nindir[level])
256 file_block -= fp->f_nindir[level];
258 if (level == NIADDR) {
259 /* Block number too high */
263 ind_block_num = DIP(fp, di_ib[level]);
265 for (; level >= 0; level--) {
266 if (ind_block_num == 0) {
267 *disk_block_p = 0; /* missing */
271 if (fp->f_blkno[level] != ind_block_num) {
272 if (fp->f_blk[level] == (char *)0)
274 malloc(fs->fs_bsize);
276 rc = (f->f_dev->dv_strategy)(f->f_devdata, F_READ,
277 fsbtodb(fp->f_fs, ind_block_num),
280 &fp->f_blksize[level]);
283 if (fp->f_blksize[level] != fs->fs_bsize)
285 fp->f_blkno[level] = ind_block_num;
289 idx = file_block / fp->f_nindir[level - 1];
290 file_block %= fp->f_nindir[level - 1];
294 if (fp->f_fs->fs_magic == FS_UFS1_MAGIC)
295 ind_block_num = ((ufs1_daddr_t *)fp->f_blk[level])[idx];
297 ind_block_num = ((ufs2_daddr_t *)fp->f_blk[level])[idx];
300 *disk_block_p = ind_block_num;
306 * Write a portion of a file from an internal buffer.
309 buf_write_file(f, buf_p, size_p)
312 size_t *size_p; /* out */
314 struct file *fp = (struct file *)f->f_fsdata;
315 struct fs *fs = fp->f_fs;
317 ufs_lbn_t file_block;
318 ufs2_daddr_t disk_block;
323 * Calculate the starting block address and offset.
325 off = blkoff(fs, fp->f_seekp);
326 file_block = lblkno(fs, fp->f_seekp);
327 block_size = sblksize(fs, DIP(fp, di_size), file_block);
329 rc = block_map(f, file_block, &disk_block);
334 /* Because we can't allocate space on the drive */
338 * Truncate buffer at end of file, and at the end of
341 if (*size_p > DIP(fp, di_size) - fp->f_seekp)
342 *size_p = DIP(fp, di_size) - fp->f_seekp;
343 if (*size_p > block_size - off)
344 *size_p = block_size - off;
347 * If we don't entirely occlude the block and it's not
348 * in memory already, read it in first.
350 if (((off > 0) || (*size_p + off < block_size)) &&
351 (file_block != fp->f_buf_blkno)) {
353 if (fp->f_buf == (char *)0)
354 fp->f_buf = malloc(fs->fs_bsize);
357 rc = (f->f_dev->dv_strategy)(f->f_devdata, F_READ,
358 fsbtodb(fs, disk_block),
359 block_size, fp->f_buf, &fp->f_buf_size);
363 fp->f_buf_blkno = file_block;
367 * Copy the user data into the cached block.
369 bcopy(buf_p, fp->f_buf + off, *size_p);
372 * Write the block out to storage.
376 rc = (f->f_dev->dv_strategy)(f->f_devdata, F_WRITE,
377 fsbtodb(fs, disk_block),
378 block_size, fp->f_buf, &fp->f_buf_size);
383 * Read a portion of a file into an internal buffer. Return
384 * the location in the buffer and the amount in the buffer.
387 buf_read_file(f, buf_p, size_p)
389 char **buf_p; /* out */
390 size_t *size_p; /* out */
392 struct file *fp = (struct file *)f->f_fsdata;
393 struct fs *fs = fp->f_fs;
395 ufs_lbn_t file_block;
396 ufs2_daddr_t disk_block;
400 off = blkoff(fs, fp->f_seekp);
401 file_block = lblkno(fs, fp->f_seekp);
402 block_size = sblksize(fs, DIP(fp, di_size), file_block);
404 if (file_block != fp->f_buf_blkno) {
405 if (fp->f_buf == (char *)0)
406 fp->f_buf = malloc(fs->fs_bsize);
408 rc = block_map(f, file_block, &disk_block);
412 if (disk_block == 0) {
413 bzero(fp->f_buf, block_size);
414 fp->f_buf_size = block_size;
417 rc = (f->f_dev->dv_strategy)(f->f_devdata, F_READ,
418 fsbtodb(fs, disk_block),
419 block_size, fp->f_buf, &fp->f_buf_size);
424 fp->f_buf_blkno = file_block;
428 * Return address of byte in buffer corresponding to
429 * offset, and size of remainder of buffer after that
432 *buf_p = fp->f_buf + off;
433 *size_p = block_size - off;
436 * But truncate buffer at end of file.
438 if (*size_p > DIP(fp, di_size) - fp->f_seekp)
439 *size_p = DIP(fp, di_size) - fp->f_seekp;
445 * Search a directory for a name and return its
449 search_directory(name, f, inumber_p)
452 ino_t *inumber_p; /* out */
454 struct file *fp = (struct file *)f->f_fsdata;
462 length = strlen(name);
465 while (fp->f_seekp < DIP(fp, di_size)) {
466 rc = buf_read_file(f, &buf, &buf_size);
470 dp = (struct direct *)buf;
471 edp = (struct direct *)(buf + buf_size);
473 if (dp->d_ino == (ino_t)0)
475 #if BYTE_ORDER == LITTLE_ENDIAN
476 if (fp->f_fs->fs_maxsymlinklen <= 0)
480 namlen = dp->d_namlen;
481 if (namlen == length &&
482 !strcmp(name, dp->d_name)) {
484 *inumber_p = dp->d_ino;
488 dp = (struct direct *)((char *)dp + dp->d_reclen);
490 fp->f_seekp += buf_size;
495 static int sblock_try[] = SBLOCKSEARCH;
507 ino_t inumber, parent_inumber;
513 char namebuf[MAXPATHLEN+1];
517 /* allocate file system specific data structure */
518 fp = malloc(sizeof(struct file));
519 bzero(fp, sizeof(struct file));
520 f->f_fsdata = (void *)fp;
522 /* allocate space and read super block */
523 fs = malloc(SBLOCKSIZE);
527 * Try reading the superblock in each of its possible locations.
529 for (i = 0; sblock_try[i] != -1; i++) {
530 rc = (f->f_dev->dv_strategy)(f->f_devdata, F_READ,
531 sblock_try[i] / DEV_BSIZE, SBLOCKSIZE,
532 (char *)fs, &buf_size);
535 if ((fs->fs_magic == FS_UFS1_MAGIC ||
536 (fs->fs_magic == FS_UFS2_MAGIC &&
537 fs->fs_sblockloc == numfrags(fs, sblock_try[i]))) &&
538 buf_size == SBLOCKSIZE &&
539 fs->fs_bsize <= MAXBSIZE &&
540 fs->fs_bsize >= sizeof(struct fs))
543 if (sblock_try[i] == -1) {
548 * Calculate indirect block levels.
555 for (level = 0; level < NIADDR; level++) {
557 fp->f_nindir[level] = mult;
562 if ((rc = read_inode(inumber, f)) != 0)
565 cp = path = strdup(upath);
573 * Remove extra separators
581 * Check that current node is a directory.
583 if ((DIP(fp, di_mode) & IFMT) != IFDIR) {
589 * Get next component of path name.
595 while ((c = *cp) != '\0' && c != '/') {
596 if (++len > MAXNAMLEN) {
606 * Look up component in current directory.
607 * Save directory inumber in case we find a
610 parent_inumber = inumber;
611 rc = search_directory(ncp, f, &inumber);
617 * Open next component.
619 if ((rc = read_inode(inumber, f)) != 0)
623 * Check for symbolic link.
625 if ((DIP(fp, di_mode) & IFMT) == IFLNK) {
626 int link_len = DIP(fp, di_size);
631 if (link_len + len > MAXPATHLEN ||
632 ++nlinks > MAXSYMLINKS) {
637 bcopy(cp, &namebuf[link_len], len + 1);
639 if (link_len < fs->fs_maxsymlinklen) {
640 if (fp->f_fs->fs_magic == FS_UFS1_MAGIC)
641 cp = (caddr_t)(fp->f_di.di1.di_db);
643 cp = (caddr_t)(fp->f_di.di2.di_db);
644 bcopy(cp, namebuf, (unsigned) link_len);
647 * Read file for symbolic link
650 ufs2_daddr_t disk_block;
651 struct fs *fs = fp->f_fs;
654 buf = malloc(fs->fs_bsize);
655 rc = block_map(f, (ufs2_daddr_t)0, &disk_block);
660 rc = (f->f_dev->dv_strategy)(f->f_devdata,
661 F_READ, fsbtodb(fs, disk_block),
662 fs->fs_bsize, buf, &buf_size);
666 bcopy((char *)buf, namebuf, (unsigned)link_len);
670 * If relative pathname, restart at parent directory.
671 * If absolute pathname, restart at root.
675 inumber = parent_inumber;
677 inumber = (ino_t)ROOTINO;
679 if ((rc = read_inode(inumber, f)) != 0)
685 * Found terminal component.
706 struct file *fp = (struct file *)f->f_fsdata;
709 f->f_fsdata = (void *)0;
710 if (fp == (struct file *)0)
713 for (level = 0; level < NIADDR; level++) {
714 if (fp->f_blk[level])
715 free(fp->f_blk[level]);
725 * Copy a portion of a file into kernel memory.
726 * Cross block boundaries when necessary.
729 ufs_read(f, start, size, resid)
733 size_t *resid; /* out */
735 struct file *fp = (struct file *)f->f_fsdata;
743 if (fp->f_seekp >= DIP(fp, di_size))
746 rc = buf_read_file(f, &buf, &buf_size);
751 if (csize > buf_size)
754 bcopy(buf, addr, csize);
756 fp->f_seekp += csize;
766 * Write to a portion of an already allocated file.
767 * Cross block boundaries when necessary. Can not
771 ufs_write(f, start, size, resid)
775 size_t *resid; /* out */
777 struct file *fp = (struct file *)f->f_fsdata;
783 while ((size != 0) && (csize != 0)) {
784 if (fp->f_seekp >= DIP(fp, di_size))
787 if (csize >= 512) csize = 512; /* XXX */
789 rc = buf_write_file(f, addr, &csize);
793 fp->f_seekp += csize;
803 ufs_seek(f, offset, where)
808 struct file *fp = (struct file *)f->f_fsdata;
812 fp->f_seekp = offset;
815 fp->f_seekp += offset;
818 fp->f_seekp = DIP(fp, di_size) - offset;
823 return (fp->f_seekp);
831 struct file *fp = (struct file *)f->f_fsdata;
833 /* only important stuff */
834 sb->st_mode = DIP(fp, di_mode);
835 sb->st_uid = DIP(fp, di_uid);
836 sb->st_gid = DIP(fp, di_gid);
837 sb->st_size = DIP(fp, di_size);
842 ufs_readdir(struct open_file *f, struct dirent *d)
844 struct file *fp = (struct file *)f->f_fsdata;
851 * assume that a directory entry will not be split across blocks
854 if (fp->f_seekp >= DIP(fp, di_size))
856 error = buf_read_file(f, &buf, &buf_size);
859 dp = (struct direct *)buf;
860 fp->f_seekp += dp->d_reclen;
861 if (dp->d_ino == (ino_t)0)
863 d->d_type = dp->d_type;
864 strcpy(d->d_name, dp->d_name);