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>
87 static int ufs_open(const char *path, struct open_file *f);
88 static int ufs_write(struct open_file *f, const void *buf, size_t size,
90 static int ufs_close(struct open_file *f);
91 static int ufs_read(struct open_file *f, void *buf, size_t size, size_t *resid);
92 static off_t ufs_seek(struct open_file *f, off_t offset, int where);
93 static int ufs_stat(struct open_file *f, struct stat *sb);
94 static int ufs_readdir(struct open_file *f, struct dirent *d);
95 static int ufs_mount(const char *dev, const char *path, void **data);
96 static int ufs_unmount(const char *dev, void *data);
98 struct fs_ops ufs_fsops = {
101 .fo_close = ufs_close,
103 .fo_write = ufs_write,
106 .fo_readdir = ufs_readdir,
107 .fo_mount = ufs_mount,
108 .fo_unmount = ufs_unmount
115 off_t f_seekp; /* seek pointer */
116 struct fs *f_fs; /* pointer to super-block */
118 struct ufs1_dinode di1;
119 struct ufs2_dinode di2;
120 } f_di; /* copy of on-disk inode */
121 int f_nindir[UFS_NIADDR];
122 /* number of blocks mapped by
123 indirect block at level i */
124 char *f_blk[UFS_NIADDR]; /* buffer for indirect block at
126 size_t f_blksize[UFS_NIADDR];
128 ufs2_daddr_t f_blkno[UFS_NIADDR];/* disk address of block in buffer */
129 ufs2_daddr_t f_buf_blkno; /* block number of data block */
130 char *f_buf; /* buffer for data block */
131 size_t f_buf_size; /* size of data block */
132 int f_inumber; /* inumber */
134 #define DIP(fp, field) \
135 ((fp)->f_fs->fs_magic == FS_UFS1_MAGIC ? \
136 (fp)->f_di.di1.field : (fp)->f_di.di2.field)
138 typedef struct ufs_mnt {
141 STAILQ_ENTRY(ufs_mnt) um_link;
144 typedef STAILQ_HEAD(ufs_mnt_list, ufs_mnt) ufs_mnt_list_t;
145 static ufs_mnt_list_t mnt_list = STAILQ_HEAD_INITIALIZER(mnt_list);
147 static int read_inode(ino_t, struct open_file *);
148 static int block_map(struct open_file *, ufs2_daddr_t, ufs2_daddr_t *);
149 static int buf_read_file(struct open_file *, char **, size_t *);
150 static int buf_write_file(struct open_file *, const char *, size_t *);
151 static int search_directory(char *, struct open_file *, ino_t *);
152 static int ufs_use_sa_read(void *, off_t, void **, int);
154 /* from ffs_subr.c */
155 int ffs_sbget(void *, struct fs **, off_t, char *,
156 int (*)(void *, off_t, void **, int));
158 * Request standard superblock location in ffs_sbget
160 #define STDSB -1 /* Fail if check-hash is bad */
161 #define STDSB_NOHASHFAIL -2 /* Ignore check-hash failure */
164 * Read a new inode into a file structure.
167 read_inode(ino_t inumber, struct open_file *f)
169 struct file *fp = (struct file *)f->f_fsdata;
170 struct fs *fs = fp->f_fs;
179 * Read inode and save it.
181 buf = malloc(fs->fs_bsize);
183 rc = (f->f_dev->dv_strategy)(f->f_devdata, F_READ,
184 fsbtodb(fs, ino_to_fsba(fs, inumber)), fs->fs_bsize,
188 if (rsize != fs->fs_bsize) {
193 if (fp->f_fs->fs_magic == FS_UFS1_MAGIC)
194 fp->f_di.di1 = ((struct ufs1_dinode *)buf)
195 [ino_to_fsbo(fs, inumber)];
197 fp->f_di.di2 = ((struct ufs2_dinode *)buf)
198 [ino_to_fsbo(fs, inumber)];
201 * Clear out the old buffers
206 for (level = 0; level < UFS_NIADDR; level++)
207 fp->f_blkno[level] = -1;
208 fp->f_buf_blkno = -1;
211 fp->f_inumber = inumber;
218 * Given an offset in a file, find the disk block number that
219 * contains that block.
222 block_map(struct open_file *f, ufs2_daddr_t file_block,
223 ufs2_daddr_t *disk_block_p)
225 struct file *fp = (struct file *)f->f_fsdata;
226 struct fs *fs = fp->f_fs;
229 ufs2_daddr_t ind_block_num;
233 * Index structure of an inode:
235 * di_db[0..UFS_NDADDR-1] hold block numbers for blocks
238 * di_ib[0] index block 0 is the single indirect block
239 * holds block numbers for blocks
240 * UFS_NDADDR .. UFS_NDADDR + NINDIR(fs)-1
242 * di_ib[1] index block 1 is the double indirect block
243 * holds block numbers for INDEX blocks for blocks
244 * UFS_NDADDR + NINDIR(fs) ..
245 * UFS_NDADDR + NINDIR(fs) + NINDIR(fs)**2 - 1
247 * di_ib[2] index block 2 is the triple indirect block
248 * holds block numbers for double-indirect
250 * UFS_NDADDR + NINDIR(fs) + NINDIR(fs)**2 ..
251 * UFS_NDADDR + NINDIR(fs) + NINDIR(fs)**2
252 * + NINDIR(fs)**3 - 1
255 if (file_block < UFS_NDADDR) {
257 *disk_block_p = DIP(fp, di_db[file_block]);
261 file_block -= UFS_NDADDR;
265 * nindir[1] = NINDIR**2
266 * nindir[2] = NINDIR**3
269 for (level = 0; level < UFS_NIADDR; level++) {
270 if (file_block < fp->f_nindir[level])
272 file_block -= fp->f_nindir[level];
274 if (level == UFS_NIADDR) {
275 /* Block number too high */
279 ind_block_num = DIP(fp, di_ib[level]);
281 for (; level >= 0; level--) {
282 if (ind_block_num == 0) {
283 *disk_block_p = 0; /* missing */
287 if (fp->f_blkno[level] != ind_block_num) {
288 if (fp->f_blk[level] == (char *)0)
290 malloc(fs->fs_bsize);
292 rc = (f->f_dev->dv_strategy)(f->f_devdata, F_READ,
293 fsbtodb(fp->f_fs, ind_block_num),
296 &fp->f_blksize[level]);
299 if (fp->f_blksize[level] != fs->fs_bsize)
301 fp->f_blkno[level] = ind_block_num;
305 idx = file_block / fp->f_nindir[level - 1];
306 file_block %= fp->f_nindir[level - 1];
310 if (fp->f_fs->fs_magic == FS_UFS1_MAGIC)
311 ind_block_num = ((ufs1_daddr_t *)fp->f_blk[level])[idx];
313 ind_block_num = ((ufs2_daddr_t *)fp->f_blk[level])[idx];
316 *disk_block_p = ind_block_num;
322 * Write a portion of a file from an internal buffer.
325 buf_write_file(struct open_file *f, const char *buf_p, size_t *size_p)
327 struct file *fp = (struct file *)f->f_fsdata;
328 struct fs *fs = fp->f_fs;
330 ufs_lbn_t file_block;
331 ufs2_daddr_t disk_block;
336 * Calculate the starting block address and offset.
338 off = blkoff(fs, fp->f_seekp);
339 file_block = lblkno(fs, fp->f_seekp);
340 block_size = sblksize(fs, DIP(fp, di_size), file_block);
342 rc = block_map(f, file_block, &disk_block);
347 /* Because we can't allocate space on the drive */
351 * Truncate buffer at end of file, and at the end of
354 if (*size_p > DIP(fp, di_size) - fp->f_seekp)
355 *size_p = DIP(fp, di_size) - fp->f_seekp;
356 if (*size_p > block_size - off)
357 *size_p = block_size - off;
360 * If we don't entirely occlude the block and it's not
361 * in memory already, read it in first.
363 if (((off > 0) || (*size_p + off < block_size)) &&
364 (file_block != fp->f_buf_blkno)) {
366 if (fp->f_buf == (char *)0)
367 fp->f_buf = malloc(fs->fs_bsize);
370 rc = (f->f_dev->dv_strategy)(f->f_devdata, F_READ,
371 fsbtodb(fs, disk_block),
372 block_size, fp->f_buf, &fp->f_buf_size);
376 fp->f_buf_blkno = file_block;
380 * Copy the user data into the cached block.
382 bcopy(buf_p, fp->f_buf + off, *size_p);
385 * Write the block out to storage.
389 rc = (f->f_dev->dv_strategy)(f->f_devdata, F_WRITE,
390 fsbtodb(fs, disk_block),
391 block_size, fp->f_buf, &fp->f_buf_size);
396 * Read a portion of a file into an internal buffer. Return
397 * the location in the buffer and the amount in the buffer.
400 buf_read_file(struct open_file *f, char **buf_p, size_t *size_p)
402 struct file *fp = (struct file *)f->f_fsdata;
403 struct fs *fs = fp->f_fs;
405 ufs_lbn_t file_block;
406 ufs2_daddr_t disk_block;
410 off = blkoff(fs, fp->f_seekp);
411 file_block = lblkno(fs, fp->f_seekp);
412 block_size = sblksize(fs, DIP(fp, di_size), file_block);
414 if (file_block != fp->f_buf_blkno) {
415 if (fp->f_buf == (char *)0)
416 fp->f_buf = malloc(fs->fs_bsize);
418 rc = block_map(f, file_block, &disk_block);
422 if (disk_block == 0) {
423 bzero(fp->f_buf, block_size);
424 fp->f_buf_size = block_size;
427 rc = (f->f_dev->dv_strategy)(f->f_devdata, F_READ,
428 fsbtodb(fs, disk_block),
429 block_size, fp->f_buf, &fp->f_buf_size);
434 fp->f_buf_blkno = file_block;
438 * Return address of byte in buffer corresponding to
439 * offset, and size of remainder of buffer after that
442 *buf_p = fp->f_buf + off;
443 *size_p = block_size - off;
446 * But truncate buffer at end of file.
448 if (*size_p > DIP(fp, di_size) - fp->f_seekp)
449 *size_p = DIP(fp, di_size) - fp->f_seekp;
455 * Search a directory for a name and return its
459 search_directory(char *name, struct open_file *f, ino_t *inumber_p)
461 struct file *fp = (struct file *)f->f_fsdata;
469 length = strlen(name);
472 while (fp->f_seekp < DIP(fp, di_size)) {
473 rc = buf_read_file(f, &buf, &buf_size);
477 dp = (struct direct *)buf;
478 edp = (struct direct *)(buf + buf_size);
480 if (dp->d_ino == (ino_t)0)
482 #if BYTE_ORDER == LITTLE_ENDIAN
483 if (fp->f_fs->fs_maxsymlinklen <= 0)
487 namlen = dp->d_namlen;
488 if (namlen == length &&
489 !strcmp(name, dp->d_name)) {
491 *inumber_p = dp->d_ino;
495 dp = (struct direct *)((char *)dp + dp->d_reclen);
497 fp->f_seekp += buf_size;
506 ufs_open(const char *upath, struct open_file *f)
510 ino_t inumber, parent_inumber;
515 char namebuf[MAXPATHLEN+1];
521 /* allocate file system specific data structure */
523 fp = calloc(1, sizeof(struct file));
526 f->f_fsdata = (void *)fp;
528 dev = disk_fmtdev(f->f_devdata);
529 /* Is this device mounted? */
530 STAILQ_FOREACH(mnt, &mnt_list, um_link) {
531 if (strcmp(dev, mnt->um_dev) == 0)
536 /* read super block */
538 if ((rc = ffs_sbget(f, &fs, STDSB_NOHASHFAIL, "stand",
539 ufs_use_sa_read)) != 0) {
543 struct open_file *sbf;
546 /* get superblock from mounted file system */
547 sbf = fd2open_file(mnt->um_fd);
554 * Calculate indirect block levels.
561 for (level = 0; level < UFS_NIADDR; level++) {
563 fp->f_nindir[level] = mult;
567 inumber = UFS_ROOTINO;
568 if ((rc = read_inode(inumber, f)) != 0)
571 cp = path = strdup(upath);
579 * Remove extra separators
587 * Check that current node is a directory.
589 if ((DIP(fp, di_mode) & IFMT) != IFDIR) {
595 * Get next component of path name.
601 while ((c = *cp) != '\0' && c != '/') {
602 if (++len > UFS_MAXNAMLEN) {
612 * Look up component in current directory.
613 * Save directory inumber in case we find a
616 parent_inumber = inumber;
617 rc = search_directory(ncp, f, &inumber);
623 * Open next component.
625 if ((rc = read_inode(inumber, f)) != 0)
629 * Check for symbolic link.
631 if ((DIP(fp, di_mode) & IFMT) == IFLNK) {
632 int link_len = DIP(fp, di_size);
637 if (link_len + len > MAXPATHLEN ||
638 ++nlinks > MAXSYMLINKS) {
643 bcopy(cp, &namebuf[link_len], len + 1);
645 if (link_len < fs->fs_maxsymlinklen) {
646 if (fp->f_fs->fs_magic == FS_UFS1_MAGIC)
647 cp = (caddr_t)(fp->f_di.di1.di_db);
649 cp = (caddr_t)(fp->f_di.di2.di_db);
650 bcopy(cp, namebuf, (unsigned) link_len);
653 * Read file for symbolic link
656 ufs2_daddr_t disk_block;
657 struct fs *fs = fp->f_fs;
660 buf = malloc(fs->fs_bsize);
661 rc = block_map(f, (ufs2_daddr_t)0, &disk_block);
666 rc = (f->f_dev->dv_strategy)(f->f_devdata,
667 F_READ, fsbtodb(fs, disk_block),
668 fs->fs_bsize, buf, &buf_size);
672 bcopy((char *)buf, namebuf, (unsigned)link_len);
676 * If relative pathname, restart at parent directory.
677 * If absolute pathname, restart at root.
681 inumber = parent_inumber;
683 inumber = (ino_t)UFS_ROOTINO;
685 if ((rc = read_inode(inumber, f)) != 0)
691 * Found terminal component.
701 if (mnt == NULL && fp->f_fs != NULL) {
702 free(fp->f_fs->fs_csp);
703 free(fp->f_fs->fs_si);
712 * A read function for use by standalone-layer routines.
715 ufs_use_sa_read(void *devfd, off_t loc, void **bufp, int size)
721 f = (struct open_file *)devfd;
722 if ((*bufp = malloc(size)) == NULL)
724 error = (f->f_dev->dv_strategy)(f->f_devdata, F_READ, loc / DEV_BSIZE,
725 size, *bufp, &buf_size);
728 if (buf_size != size)
734 ufs_close(struct open_file *f)
737 struct file *fp = (struct file *)f->f_fsdata;
745 for (level = 0; level < UFS_NIADDR; level++) {
746 free(fp->f_blk[level]);
750 dev = disk_fmtdev(f->f_devdata);
751 STAILQ_FOREACH(mnt, &mnt_list, um_link) {
752 if (strcmp(dev, mnt->um_dev) == 0)
756 if (mnt == NULL && fp->f_fs != NULL) {
757 free(fp->f_fs->fs_csp);
758 free(fp->f_fs->fs_si);
767 * Copy a portion of a file into kernel memory.
768 * Cross block boundaries when necessary.
771 ufs_read(struct open_file *f, void *start, size_t size, size_t *resid)
773 struct file *fp = (struct file *)f->f_fsdata;
781 if (fp->f_seekp >= DIP(fp, di_size))
784 rc = buf_read_file(f, &buf, &buf_size);
789 if (csize > buf_size)
792 bcopy(buf, addr, csize);
794 fp->f_seekp += csize;
804 * Write to a portion of an already allocated file.
805 * Cross block boundaries when necessary. Can not
809 ufs_write(struct open_file *f, const void *start, size_t size, size_t *resid)
811 struct file *fp = (struct file *)f->f_fsdata;
814 const char *addr = start;
817 while ((size != 0) && (csize != 0)) {
818 if (fp->f_seekp >= DIP(fp, di_size))
821 if (csize >= 512) csize = 512; /* XXX */
823 rc = buf_write_file(f, addr, &csize);
827 fp->f_seekp += csize;
837 ufs_seek(struct open_file *f, off_t offset, int where)
839 struct file *fp = (struct file *)f->f_fsdata;
843 fp->f_seekp = offset;
846 fp->f_seekp += offset;
849 fp->f_seekp = DIP(fp, di_size) - offset;
855 return (fp->f_seekp);
859 ufs_stat(struct open_file *f, struct stat *sb)
861 struct file *fp = (struct file *)f->f_fsdata;
863 /* only important stuff */
864 sb->st_mode = DIP(fp, di_mode);
865 sb->st_uid = DIP(fp, di_uid);
866 sb->st_gid = DIP(fp, di_gid);
867 sb->st_size = DIP(fp, di_size);
868 sb->st_mtime = DIP(fp, di_mtime);
870 * The items below are ufs specific!
871 * Other fs types will need their own solution
872 * if these fields are needed.
874 sb->st_ino = fp->f_inumber;
876 * We need something to differentiate devs.
877 * fs_id is unique but 64bit, we xor the two
878 * halves to squeeze it into 32bits.
880 sb->st_dev = (dev_t)(fp->f_fs->fs_id[0] ^ fp->f_fs->fs_id[1]);
886 ufs_readdir(struct open_file *f, struct dirent *d)
888 struct file *fp = (struct file *)f->f_fsdata;
895 * assume that a directory entry will not be split across blocks
898 if (fp->f_seekp >= DIP(fp, di_size))
900 error = buf_read_file(f, &buf, &buf_size);
903 dp = (struct direct *)buf;
904 fp->f_seekp += dp->d_reclen;
905 if (dp->d_ino == (ino_t)0)
907 d->d_type = dp->d_type;
908 strcpy(d->d_name, dp->d_name);
913 ufs_mount(const char *dev, const char *path, void **data)
920 mnt = calloc(1, sizeof(*mnt));
924 mnt->um_dev = strdup(dev);
925 if (mnt->um_dev == NULL)
928 if (asprintf(&fs, "%s%s", dev, path) < 0)
931 mnt->um_fd = open(fs, O_RDONLY);
933 if (mnt->um_fd == -1)
936 /* Is it ufs file system? */
937 f = fd2open_file(mnt->um_fd);
938 if (strcmp(f->f_ops->fs_name, "ufs") == 0)
939 STAILQ_INSERT_TAIL(&mnt_list, mnt, um_link);
957 ufs_unmount(const char *dev __unused, void *data)
959 ufs_mnt_t *mnt = data;
961 STAILQ_REMOVE(&mnt_list, mnt, ufs_mnt, um_link);