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;
201 * Given an offset in a file, find the disk block number that
202 * contains that block.
205 block_map(f, file_block, disk_block_p)
207 ufs2_daddr_t file_block;
208 ufs2_daddr_t *disk_block_p; /* out */
210 struct file *fp = (struct file *)f->f_fsdata;
211 struct fs *fs = fp->f_fs;
214 ufs2_daddr_t ind_block_num;
218 * Index structure of an inode:
220 * di_db[0..NDADDR-1] hold block numbers for blocks
223 * di_ib[0] index block 0 is the single indirect block
224 * holds block numbers for blocks
225 * NDADDR .. NDADDR + NINDIR(fs)-1
227 * di_ib[1] index block 1 is the double indirect block
228 * holds block numbers for INDEX blocks for blocks
229 * NDADDR + NINDIR(fs) ..
230 * NDADDR + NINDIR(fs) + NINDIR(fs)**2 - 1
232 * di_ib[2] index block 2 is the triple indirect block
233 * holds block numbers for double-indirect
235 * NDADDR + NINDIR(fs) + NINDIR(fs)**2 ..
236 * NDADDR + NINDIR(fs) + NINDIR(fs)**2
237 * + NINDIR(fs)**3 - 1
240 if (file_block < NDADDR) {
242 *disk_block_p = DIP(fp, di_db[file_block]);
246 file_block -= NDADDR;
250 * nindir[1] = NINDIR**2
251 * nindir[2] = NINDIR**3
254 for (level = 0; level < NIADDR; level++) {
255 if (file_block < fp->f_nindir[level])
257 file_block -= fp->f_nindir[level];
259 if (level == NIADDR) {
260 /* Block number too high */
264 ind_block_num = DIP(fp, di_ib[level]);
266 for (; level >= 0; level--) {
267 if (ind_block_num == 0) {
268 *disk_block_p = 0; /* missing */
272 if (fp->f_blkno[level] != ind_block_num) {
273 if (fp->f_blk[level] == (char *)0)
275 malloc(fs->fs_bsize);
277 rc = (f->f_dev->dv_strategy)(f->f_devdata, F_READ,
278 fsbtodb(fp->f_fs, ind_block_num),
281 &fp->f_blksize[level]);
284 if (fp->f_blksize[level] != fs->fs_bsize)
286 fp->f_blkno[level] = ind_block_num;
290 idx = file_block / fp->f_nindir[level - 1];
291 file_block %= fp->f_nindir[level - 1];
295 if (fp->f_fs->fs_magic == FS_UFS1_MAGIC)
296 ind_block_num = ((ufs1_daddr_t *)fp->f_blk[level])[idx];
298 ind_block_num = ((ufs2_daddr_t *)fp->f_blk[level])[idx];
301 *disk_block_p = ind_block_num;
307 * Write a portion of a file from an internal buffer.
310 buf_write_file(f, buf_p, size_p)
313 size_t *size_p; /* out */
315 struct file *fp = (struct file *)f->f_fsdata;
316 struct fs *fs = fp->f_fs;
318 ufs_lbn_t file_block;
319 ufs2_daddr_t disk_block;
324 * Calculate the starting block address and offset.
326 off = blkoff(fs, fp->f_seekp);
327 file_block = lblkno(fs, fp->f_seekp);
328 block_size = sblksize(fs, DIP(fp, di_size), file_block);
330 rc = block_map(f, file_block, &disk_block);
335 /* Because we can't allocate space on the drive */
339 * Truncate buffer at end of file, and at the end of
342 if (*size_p > DIP(fp, di_size) - fp->f_seekp)
343 *size_p = DIP(fp, di_size) - fp->f_seekp;
344 if (*size_p > block_size - off)
345 *size_p = block_size - off;
348 * If we don't entirely occlude the block and it's not
349 * in memory already, read it in first.
351 if (((off > 0) || (*size_p + off < block_size)) &&
352 (file_block != fp->f_buf_blkno)) {
354 if (fp->f_buf == (char *)0)
355 fp->f_buf = malloc(fs->fs_bsize);
358 rc = (f->f_dev->dv_strategy)(f->f_devdata, F_READ,
359 fsbtodb(fs, disk_block),
360 block_size, fp->f_buf, &fp->f_buf_size);
364 fp->f_buf_blkno = file_block;
368 * Copy the user data into the cached block.
370 bcopy(buf_p, fp->f_buf + off, *size_p);
373 * Write the block out to storage.
377 rc = (f->f_dev->dv_strategy)(f->f_devdata, F_WRITE,
378 fsbtodb(fs, disk_block),
379 block_size, fp->f_buf, &fp->f_buf_size);
384 * Read a portion of a file into an internal buffer. Return
385 * the location in the buffer and the amount in the buffer.
388 buf_read_file(f, buf_p, size_p)
390 char **buf_p; /* out */
391 size_t *size_p; /* out */
393 struct file *fp = (struct file *)f->f_fsdata;
394 struct fs *fs = fp->f_fs;
396 ufs_lbn_t file_block;
397 ufs2_daddr_t disk_block;
401 off = blkoff(fs, fp->f_seekp);
402 file_block = lblkno(fs, fp->f_seekp);
403 block_size = sblksize(fs, DIP(fp, di_size), file_block);
405 if (file_block != fp->f_buf_blkno) {
406 if (fp->f_buf == (char *)0)
407 fp->f_buf = malloc(fs->fs_bsize);
409 rc = block_map(f, file_block, &disk_block);
413 if (disk_block == 0) {
414 bzero(fp->f_buf, block_size);
415 fp->f_buf_size = block_size;
418 rc = (f->f_dev->dv_strategy)(f->f_devdata, F_READ,
419 fsbtodb(fs, disk_block),
420 block_size, fp->f_buf, &fp->f_buf_size);
425 fp->f_buf_blkno = file_block;
429 * Return address of byte in buffer corresponding to
430 * offset, and size of remainder of buffer after that
433 *buf_p = fp->f_buf + off;
434 *size_p = block_size - off;
437 * But truncate buffer at end of file.
439 if (*size_p > DIP(fp, di_size) - fp->f_seekp)
440 *size_p = DIP(fp, di_size) - fp->f_seekp;
446 * Search a directory for a name and return its
450 search_directory(name, f, inumber_p)
453 ino_t *inumber_p; /* out */
455 struct file *fp = (struct file *)f->f_fsdata;
463 length = strlen(name);
466 while (fp->f_seekp < DIP(fp, di_size)) {
467 rc = buf_read_file(f, &buf, &buf_size);
471 dp = (struct direct *)buf;
472 edp = (struct direct *)(buf + buf_size);
474 if (dp->d_ino == (ino_t)0)
476 #if BYTE_ORDER == LITTLE_ENDIAN
477 if (fp->f_fs->fs_maxsymlinklen <= 0)
481 namlen = dp->d_namlen;
482 if (namlen == length &&
483 !strcmp(name, dp->d_name)) {
485 *inumber_p = dp->d_ino;
489 dp = (struct direct *)((char *)dp + dp->d_reclen);
491 fp->f_seekp += buf_size;
496 static int sblock_try[] = SBLOCKSEARCH;
508 ino_t inumber, parent_inumber;
514 char namebuf[MAXPATHLEN+1];
518 /* allocate file system specific data structure */
519 fp = malloc(sizeof(struct file));
520 bzero(fp, sizeof(struct file));
521 f->f_fsdata = (void *)fp;
523 /* allocate space and read super block */
524 fs = malloc(SBLOCKSIZE);
528 * Try reading the superblock in each of its possible locations.
530 for (i = 0; sblock_try[i] != -1; i++) {
531 rc = (f->f_dev->dv_strategy)(f->f_devdata, F_READ,
532 sblock_try[i] / DEV_BSIZE, SBLOCKSIZE,
533 (char *)fs, &buf_size);
536 if ((fs->fs_magic == FS_UFS1_MAGIC ||
537 (fs->fs_magic == FS_UFS2_MAGIC &&
538 fs->fs_sblockloc == sblock_try[i])) &&
539 buf_size == SBLOCKSIZE &&
540 fs->fs_bsize <= MAXBSIZE &&
541 fs->fs_bsize >= sizeof(struct fs))
544 if (sblock_try[i] == -1) {
549 * Calculate indirect block levels.
556 for (level = 0; level < NIADDR; level++) {
558 fp->f_nindir[level] = mult;
563 if ((rc = read_inode(inumber, f)) != 0)
566 cp = path = strdup(upath);
574 * Remove extra separators
582 * Check that current node is a directory.
584 if ((DIP(fp, di_mode) & IFMT) != IFDIR) {
590 * Get next component of path name.
596 while ((c = *cp) != '\0' && c != '/') {
597 if (++len > MAXNAMLEN) {
607 * Look up component in current directory.
608 * Save directory inumber in case we find a
611 parent_inumber = inumber;
612 rc = search_directory(ncp, f, &inumber);
618 * Open next component.
620 if ((rc = read_inode(inumber, f)) != 0)
624 * Check for symbolic link.
626 if ((DIP(fp, di_mode) & IFMT) == IFLNK) {
627 int link_len = DIP(fp, di_size);
632 if (link_len + len > MAXPATHLEN ||
633 ++nlinks > MAXSYMLINKS) {
638 bcopy(cp, &namebuf[link_len], len + 1);
640 if (link_len < fs->fs_maxsymlinklen) {
641 if (fp->f_fs->fs_magic == FS_UFS1_MAGIC)
642 cp = (caddr_t)(fp->f_di.di1.di_db);
644 cp = (caddr_t)(fp->f_di.di2.di_db);
645 bcopy(cp, namebuf, (unsigned) link_len);
648 * Read file for symbolic link
651 ufs2_daddr_t disk_block;
652 struct fs *fs = fp->f_fs;
655 buf = malloc(fs->fs_bsize);
656 rc = block_map(f, (ufs2_daddr_t)0, &disk_block);
661 rc = (f->f_dev->dv_strategy)(f->f_devdata,
662 F_READ, fsbtodb(fs, disk_block),
663 fs->fs_bsize, buf, &buf_size);
667 bcopy((char *)buf, namebuf, (unsigned)link_len);
671 * If relative pathname, restart at parent directory.
672 * If absolute pathname, restart at root.
676 inumber = parent_inumber;
678 inumber = (ino_t)ROOTINO;
680 if ((rc = read_inode(inumber, f)) != 0)
686 * Found terminal component.
708 struct file *fp = (struct file *)f->f_fsdata;
711 f->f_fsdata = (void *)0;
712 if (fp == (struct file *)0)
715 for (level = 0; level < NIADDR; level++) {
716 if (fp->f_blk[level])
717 free(fp->f_blk[level]);
727 * Copy a portion of a file into kernel memory.
728 * Cross block boundaries when necessary.
731 ufs_read(f, start, size, resid)
735 size_t *resid; /* out */
737 struct file *fp = (struct file *)f->f_fsdata;
745 if (fp->f_seekp >= DIP(fp, di_size))
748 rc = buf_read_file(f, &buf, &buf_size);
753 if (csize > buf_size)
756 bcopy(buf, addr, csize);
758 fp->f_seekp += csize;
768 * Write to a portion of an already allocated file.
769 * Cross block boundaries when necessary. Can not
773 ufs_write(f, start, size, resid)
777 size_t *resid; /* out */
779 struct file *fp = (struct file *)f->f_fsdata;
785 while ((size != 0) && (csize != 0)) {
786 if (fp->f_seekp >= DIP(fp, di_size))
789 if (csize >= 512) csize = 512; /* XXX */
791 rc = buf_write_file(f, addr, &csize);
795 fp->f_seekp += csize;
805 ufs_seek(f, offset, where)
810 struct file *fp = (struct file *)f->f_fsdata;
814 fp->f_seekp = offset;
817 fp->f_seekp += offset;
820 fp->f_seekp = DIP(fp, di_size) - offset;
826 return (fp->f_seekp);
834 struct file *fp = (struct file *)f->f_fsdata;
836 /* only important stuff */
837 sb->st_mode = DIP(fp, di_mode);
838 sb->st_uid = DIP(fp, di_uid);
839 sb->st_gid = DIP(fp, di_gid);
840 sb->st_size = DIP(fp, di_size);
845 ufs_readdir(struct open_file *f, struct dirent *d)
847 struct file *fp = (struct file *)f->f_fsdata;
854 * assume that a directory entry will not be split across blocks
857 if (fp->f_seekp >= DIP(fp, di_size))
859 error = buf_read_file(f, &buf, &buf_size);
862 dp = (struct direct *)buf;
863 fp->f_seekp += dp->d_reclen;
864 if (dp->d_ino == (ino_t)0)
866 d->d_type = dp->d_type;
867 strcpy(d->d_name, dp->d_name);