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. Neither the name of the University nor the names of its contributors
28 * may be used to endorse or promote products derived from this software
29 * without specific prior written permission.
31 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
32 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
33 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
35 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
39 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
40 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44 * Copyright (c) 1990, 1991 Carnegie Mellon University
45 * All Rights Reserved.
49 * Permission to use, copy, modify and distribute this software and its
50 * documentation is hereby granted, provided that both the copyright
51 * notice and this permission notice appear in all copies of the
52 * software, derivative works or modified versions, and any portions
53 * thereof, and that both notices appear in supporting documentation.
55 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
56 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
57 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
59 * Carnegie Mellon requests users of this software to return to
61 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
62 * School of Computer Science
63 * Carnegie Mellon University
64 * Pittsburgh PA 15213-3890
66 * any improvements or extensions that they make and grant Carnegie the
67 * rights to redistribute these changes.
70 #include <sys/cdefs.h>
71 __FBSDID("$FreeBSD$");
74 * Stand-alone file reading package.
77 #include <sys/param.h>
78 #include <sys/disklabel.h>
80 #include <ufs/ufs/dinode.h>
81 #include <ufs/ufs/dir.h>
82 #include <ufs/ffs/fs.h>
86 static int ufs_open(const char *path, struct open_file *f);
87 static int ufs_write(struct open_file *f, const void *buf, size_t size,
89 static int ufs_close(struct open_file *f);
90 static int ufs_read(struct open_file *f, void *buf, size_t size, size_t *resid);
91 static off_t ufs_seek(struct open_file *f, off_t offset, int where);
92 static int ufs_stat(struct open_file *f, struct stat *sb);
93 static int ufs_readdir(struct open_file *f, struct dirent *d);
95 struct fs_ops ufs_fsops = {
110 off_t f_seekp; /* seek pointer */
111 struct fs *f_fs; /* pointer to super-block */
113 struct ufs1_dinode di1;
114 struct ufs2_dinode di2;
115 } f_di; /* copy of on-disk inode */
116 int f_nindir[UFS_NIADDR];
117 /* number of blocks mapped by
118 indirect block at level i */
119 char *f_blk[UFS_NIADDR]; /* buffer for indirect block at
121 size_t f_blksize[UFS_NIADDR];
123 ufs2_daddr_t f_blkno[UFS_NIADDR];/* disk address of block in buffer */
124 ufs2_daddr_t f_buf_blkno; /* block number of data block */
125 char *f_buf; /* buffer for data block */
126 size_t f_buf_size; /* size of data block */
127 int f_inumber; /* inumber */
129 #define DIP(fp, field) \
130 ((fp)->f_fs->fs_magic == FS_UFS1_MAGIC ? \
131 (fp)->f_di.di1.field : (fp)->f_di.di2.field)
133 static int read_inode(ino_t, struct open_file *);
134 static int block_map(struct open_file *, ufs2_daddr_t, ufs2_daddr_t *);
135 static int buf_read_file(struct open_file *, char **, size_t *);
136 static int buf_write_file(struct open_file *, const char *, size_t *);
137 static int search_directory(char *, struct open_file *, ino_t *);
138 static int ufs_use_sa_read(void *, off_t, void **, int);
140 /* from ffs_subr.c */
141 int ffs_sbget(void *, struct fs **, off_t, char *,
142 int (*)(void *, off_t, void **, int));
145 * Read a new inode into a file structure.
148 read_inode(inumber, f)
152 struct file *fp = (struct file *)f->f_fsdata;
153 struct fs *fs = fp->f_fs;
162 * Read inode and save it.
164 buf = malloc(fs->fs_bsize);
166 rc = (f->f_dev->dv_strategy)(f->f_devdata, F_READ,
167 fsbtodb(fs, ino_to_fsba(fs, inumber)), fs->fs_bsize,
171 if (rsize != fs->fs_bsize) {
176 if (fp->f_fs->fs_magic == FS_UFS1_MAGIC)
177 fp->f_di.di1 = ((struct ufs1_dinode *)buf)
178 [ino_to_fsbo(fs, inumber)];
180 fp->f_di.di2 = ((struct ufs2_dinode *)buf)
181 [ino_to_fsbo(fs, inumber)];
184 * Clear out the old buffers
189 for (level = 0; level < UFS_NIADDR; level++)
190 fp->f_blkno[level] = -1;
191 fp->f_buf_blkno = -1;
194 fp->f_inumber = inumber;
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..UFS_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 * UFS_NDADDR .. UFS_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 * UFS_NDADDR + NINDIR(fs) ..
230 * UFS_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 * UFS_NDADDR + NINDIR(fs) + NINDIR(fs)**2 ..
236 * UFS_NDADDR + NINDIR(fs) + NINDIR(fs)**2
237 * + NINDIR(fs)**3 - 1
240 if (file_block < UFS_NDADDR) {
242 *disk_block_p = DIP(fp, di_db[file_block]);
246 file_block -= UFS_NDADDR;
250 * nindir[1] = NINDIR**2
251 * nindir[2] = NINDIR**3
254 for (level = 0; level < UFS_NIADDR; level++) {
255 if (file_block < fp->f_nindir[level])
257 file_block -= fp->f_nindir[level];
259 if (level == UFS_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;
506 ino_t inumber, parent_inumber;
511 char namebuf[MAXPATHLEN+1];
515 /* allocate file system specific data structure */
516 fp = malloc(sizeof(struct file));
517 bzero(fp, sizeof(struct file));
518 f->f_fsdata = (void *)fp;
520 /* read super block */
522 if ((rc = ffs_sbget(f, &fs, -1, "stand", ufs_use_sa_read)) != 0)
526 * Calculate indirect block levels.
533 for (level = 0; level < UFS_NIADDR; level++) {
535 fp->f_nindir[level] = mult;
539 inumber = UFS_ROOTINO;
540 if ((rc = read_inode(inumber, f)) != 0)
543 cp = path = strdup(upath);
551 * Remove extra separators
559 * Check that current node is a directory.
561 if ((DIP(fp, di_mode) & IFMT) != IFDIR) {
567 * Get next component of path name.
573 while ((c = *cp) != '\0' && c != '/') {
574 if (++len > UFS_MAXNAMLEN) {
584 * Look up component in current directory.
585 * Save directory inumber in case we find a
588 parent_inumber = inumber;
589 rc = search_directory(ncp, f, &inumber);
595 * Open next component.
597 if ((rc = read_inode(inumber, f)) != 0)
601 * Check for symbolic link.
603 if ((DIP(fp, di_mode) & IFMT) == IFLNK) {
604 int link_len = DIP(fp, di_size);
609 if (link_len + len > MAXPATHLEN ||
610 ++nlinks > MAXSYMLINKS) {
615 bcopy(cp, &namebuf[link_len], len + 1);
617 if (link_len < fs->fs_maxsymlinklen) {
618 if (fp->f_fs->fs_magic == FS_UFS1_MAGIC)
619 cp = (caddr_t)(fp->f_di.di1.di_db);
621 cp = (caddr_t)(fp->f_di.di2.di_db);
622 bcopy(cp, namebuf, (unsigned) link_len);
625 * Read file for symbolic link
628 ufs2_daddr_t disk_block;
629 struct fs *fs = fp->f_fs;
632 buf = malloc(fs->fs_bsize);
633 rc = block_map(f, (ufs2_daddr_t)0, &disk_block);
638 rc = (f->f_dev->dv_strategy)(f->f_devdata,
639 F_READ, fsbtodb(fs, disk_block),
640 fs->fs_bsize, buf, &buf_size);
644 bcopy((char *)buf, namebuf, (unsigned)link_len);
648 * If relative pathname, restart at parent directory.
649 * If absolute pathname, restart at root.
653 inumber = parent_inumber;
655 inumber = (ino_t)UFS_ROOTINO;
657 if ((rc = read_inode(inumber, f)) != 0)
663 * Found terminal component.
682 * A read function for use by standalone-layer routines.
685 ufs_use_sa_read(void *devfd, off_t loc, void **bufp, int size)
691 f = (struct open_file *)devfd;
692 if ((*bufp = malloc(size)) == NULL)
694 error = (f->f_dev->dv_strategy)(f->f_devdata, F_READ, loc / DEV_BSIZE,
695 size, *bufp, &buf_size);
698 if (buf_size != size)
707 struct file *fp = (struct file *)f->f_fsdata;
710 f->f_fsdata = (void *)0;
711 if (fp == (struct file *)0)
714 for (level = 0; level < UFS_NIADDR; level++) {
715 if (fp->f_blk[level])
716 free(fp->f_blk[level]);
726 * Copy a portion of a file into kernel memory.
727 * Cross block boundaries when necessary.
730 ufs_read(f, start, size, resid)
734 size_t *resid; /* out */
736 struct file *fp = (struct file *)f->f_fsdata;
744 if (fp->f_seekp >= DIP(fp, di_size))
747 rc = buf_read_file(f, &buf, &buf_size);
752 if (csize > buf_size)
755 bcopy(buf, addr, csize);
757 fp->f_seekp += csize;
767 * Write to a portion of an already allocated file.
768 * Cross block boundaries when necessary. Can not
772 ufs_write(f, start, size, resid)
776 size_t *resid; /* out */
778 struct file *fp = (struct file *)f->f_fsdata;
781 const char *addr = start;
784 while ((size != 0) && (csize != 0)) {
785 if (fp->f_seekp >= DIP(fp, di_size))
788 if (csize >= 512) csize = 512; /* XXX */
790 rc = buf_write_file(f, addr, &csize);
794 fp->f_seekp += csize;
804 ufs_seek(f, offset, where)
809 struct file *fp = (struct file *)f->f_fsdata;
813 fp->f_seekp = offset;
816 fp->f_seekp += offset;
819 fp->f_seekp = DIP(fp, di_size) - offset;
825 return (fp->f_seekp);
833 struct file *fp = (struct file *)f->f_fsdata;
835 /* only important stuff */
836 sb->st_mode = DIP(fp, di_mode);
837 sb->st_uid = DIP(fp, di_uid);
838 sb->st_gid = DIP(fp, di_gid);
839 sb->st_size = DIP(fp, di_size);
840 sb->st_mtime = DIP(fp, di_mtime);
842 * The items below are ufs specific!
843 * Other fs types will need their own solution
844 * if these fields are needed.
846 sb->st_ino = fp->f_inumber;
848 * We need something to differentiate devs.
849 * fs_id is unique but 64bit, we xor the two
850 * halves to squeeze it into 32bits.
852 sb->st_dev = (dev_t)(fp->f_fs->fs_id[0] ^ fp->f_fs->fs_id[1]);
858 ufs_readdir(struct open_file *f, struct dirent *d)
860 struct file *fp = (struct file *)f->f_fsdata;
867 * assume that a directory entry will not be split across blocks
870 if (fp->f_seekp >= DIP(fp, di_size))
872 error = buf_read_file(f, &buf, &buf_size);
875 dp = (struct direct *)buf;
876 fp->f_seekp += dp->d_reclen;
877 if (dp->d_ino == (ino_t)0)
879 d->d_type = dp->d_type;
880 strcpy(d->d_name, dp->d_name);