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);
94 static int ufs_mount(const char *dev, const char *path, void **data);
95 static int ufs_unmount(const char *dev, void *data);
97 struct fs_ops ufs_fsops = {
100 .fo_close = ufs_close,
102 .fo_write = ufs_write,
105 .fo_readdir = ufs_readdir,
106 .fo_mount = ufs_mount,
107 .fo_unmount = ufs_unmount
114 off_t f_seekp; /* seek pointer */
115 struct fs *f_fs; /* pointer to super-block */
117 struct ufs1_dinode di1;
118 struct ufs2_dinode di2;
119 } f_di; /* copy of on-disk inode */
120 int f_nindir[UFS_NIADDR];
121 /* number of blocks mapped by
122 indirect block at level i */
123 char *f_blk[UFS_NIADDR]; /* buffer for indirect block at
125 size_t f_blksize[UFS_NIADDR];
127 ufs2_daddr_t f_blkno[UFS_NIADDR];/* disk address of block in buffer */
128 ufs2_daddr_t f_buf_blkno; /* block number of data block */
129 char *f_buf; /* buffer for data block */
130 size_t f_buf_size; /* size of data block */
131 int f_inumber; /* inumber */
133 #define DIP(fp, field) \
134 ((fp)->f_fs->fs_magic == FS_UFS1_MAGIC ? \
135 (fp)->f_di.di1.field : (fp)->f_di.di2.field)
137 typedef struct ufs_mnt {
140 STAILQ_ENTRY(ufs_mnt) um_link;
143 typedef STAILQ_HEAD(ufs_mnt_list, ufs_mnt) ufs_mnt_list_t;
144 static ufs_mnt_list_t mnt_list = STAILQ_HEAD_INITIALIZER(mnt_list);
146 static int read_inode(ino_t, struct open_file *);
147 static int block_map(struct open_file *, ufs2_daddr_t, ufs2_daddr_t *);
148 static int buf_read_file(struct open_file *, char **, size_t *);
149 static int buf_write_file(struct open_file *, const char *, size_t *);
150 static int search_directory(char *, struct open_file *, ino_t *);
151 static int ufs_use_sa_read(void *, off_t, void **, int);
153 /* from ffs_subr.c */
154 int ffs_sbget(void *devfd, struct fs **fsp, off_t sblock, int flags,
156 int (*readfunc)(void *devfd, off_t loc, void **bufp, int size));
157 int ffs_sbsearch(void *, struct fs **, int, char *,
158 int (*)(void *, off_t, void **, int));
161 * Read a new inode into a file structure.
164 read_inode(ino_t inumber, struct open_file *f)
166 struct file *fp = (struct file *)f->f_fsdata;
167 struct fs *fs = fp->f_fs;
176 * Read inode and save it.
178 buf = malloc(fs->fs_bsize);
180 rc = (f->f_dev->dv_strategy)(f->f_devdata, F_READ,
181 fsbtodb(fs, ino_to_fsba(fs, inumber)), fs->fs_bsize,
185 if (rsize != fs->fs_bsize) {
190 if (fp->f_fs->fs_magic == FS_UFS1_MAGIC)
191 fp->f_di.di1 = ((struct ufs1_dinode *)buf)
192 [ino_to_fsbo(fs, inumber)];
194 fp->f_di.di2 = ((struct ufs2_dinode *)buf)
195 [ino_to_fsbo(fs, inumber)];
198 * Clear out the old buffers
203 for (level = 0; level < UFS_NIADDR; level++)
204 fp->f_blkno[level] = -1;
205 fp->f_buf_blkno = -1;
208 fp->f_inumber = inumber;
215 * Given an offset in a file, find the disk block number that
216 * contains that block.
219 block_map(struct open_file *f, ufs2_daddr_t file_block,
220 ufs2_daddr_t *disk_block_p)
222 struct file *fp = (struct file *)f->f_fsdata;
223 struct fs *fs = fp->f_fs;
226 ufs2_daddr_t ind_block_num;
230 * Index structure of an inode:
232 * di_db[0..UFS_NDADDR-1] hold block numbers for blocks
235 * di_ib[0] index block 0 is the single indirect block
236 * holds block numbers for blocks
237 * UFS_NDADDR .. UFS_NDADDR + NINDIR(fs)-1
239 * di_ib[1] index block 1 is the double indirect block
240 * holds block numbers for INDEX blocks for blocks
241 * UFS_NDADDR + NINDIR(fs) ..
242 * UFS_NDADDR + NINDIR(fs) + NINDIR(fs)**2 - 1
244 * di_ib[2] index block 2 is the triple indirect block
245 * holds block numbers for double-indirect
247 * UFS_NDADDR + NINDIR(fs) + NINDIR(fs)**2 ..
248 * UFS_NDADDR + NINDIR(fs) + NINDIR(fs)**2
249 * + NINDIR(fs)**3 - 1
252 if (file_block < UFS_NDADDR) {
254 *disk_block_p = DIP(fp, di_db[file_block]);
258 file_block -= UFS_NDADDR;
262 * nindir[1] = NINDIR**2
263 * nindir[2] = NINDIR**3
266 for (level = 0; level < UFS_NIADDR; level++) {
267 if (file_block < fp->f_nindir[level])
269 file_block -= fp->f_nindir[level];
271 if (level == UFS_NIADDR) {
272 /* Block number too high */
276 ind_block_num = DIP(fp, di_ib[level]);
278 for (; level >= 0; level--) {
279 if (ind_block_num == 0) {
280 *disk_block_p = 0; /* missing */
284 if (fp->f_blkno[level] != ind_block_num) {
285 if (fp->f_blk[level] == (char *)0)
287 malloc(fs->fs_bsize);
289 rc = (f->f_dev->dv_strategy)(f->f_devdata, F_READ,
290 fsbtodb(fp->f_fs, ind_block_num),
293 &fp->f_blksize[level]);
296 if (fp->f_blksize[level] != fs->fs_bsize)
298 fp->f_blkno[level] = ind_block_num;
302 idx = file_block / fp->f_nindir[level - 1];
303 file_block %= fp->f_nindir[level - 1];
307 if (fp->f_fs->fs_magic == FS_UFS1_MAGIC)
308 ind_block_num = ((ufs1_daddr_t *)fp->f_blk[level])[idx];
310 ind_block_num = ((ufs2_daddr_t *)fp->f_blk[level])[idx];
313 *disk_block_p = ind_block_num;
319 * Write a portion of a file from an internal buffer.
322 buf_write_file(struct open_file *f, const char *buf_p, size_t *size_p)
324 struct file *fp = (struct file *)f->f_fsdata;
325 struct fs *fs = fp->f_fs;
327 ufs_lbn_t file_block;
328 ufs2_daddr_t disk_block;
333 * Calculate the starting block address and offset.
335 off = blkoff(fs, fp->f_seekp);
336 file_block = lblkno(fs, fp->f_seekp);
337 block_size = sblksize(fs, DIP(fp, di_size), file_block);
339 rc = block_map(f, file_block, &disk_block);
344 /* Because we can't allocate space on the drive */
348 * Truncate buffer at end of file, and at the end of
351 if (*size_p > DIP(fp, di_size) - fp->f_seekp)
352 *size_p = DIP(fp, di_size) - fp->f_seekp;
353 if (*size_p > block_size - off)
354 *size_p = block_size - off;
357 * If we don't entirely occlude the block and it's not
358 * in memory already, read it in first.
360 if (((off > 0) || (*size_p + off < block_size)) &&
361 (file_block != fp->f_buf_blkno)) {
363 if (fp->f_buf == (char *)0)
364 fp->f_buf = malloc(fs->fs_bsize);
367 rc = (f->f_dev->dv_strategy)(f->f_devdata, F_READ,
368 fsbtodb(fs, disk_block),
369 block_size, fp->f_buf, &fp->f_buf_size);
373 fp->f_buf_blkno = file_block;
377 * Copy the user data into the cached block.
379 bcopy(buf_p, fp->f_buf + off, *size_p);
382 * Write the block out to storage.
386 rc = (f->f_dev->dv_strategy)(f->f_devdata, F_WRITE,
387 fsbtodb(fs, disk_block),
388 block_size, fp->f_buf, &fp->f_buf_size);
393 * Read a portion of a file into an internal buffer. Return
394 * the location in the buffer and the amount in the buffer.
397 buf_read_file(struct open_file *f, char **buf_p, size_t *size_p)
399 struct file *fp = (struct file *)f->f_fsdata;
400 struct fs *fs = fp->f_fs;
402 ufs_lbn_t file_block;
403 ufs2_daddr_t disk_block;
407 off = blkoff(fs, fp->f_seekp);
408 file_block = lblkno(fs, fp->f_seekp);
409 block_size = sblksize(fs, DIP(fp, di_size), file_block);
411 if (file_block != fp->f_buf_blkno) {
412 if (fp->f_buf == NULL)
413 fp->f_buf = malloc(fs->fs_bsize);
415 rc = block_map(f, file_block, &disk_block);
419 if (disk_block == 0) {
420 bzero(fp->f_buf, block_size);
421 fp->f_buf_size = block_size;
424 rc = (f->f_dev->dv_strategy)(f->f_devdata, F_READ,
425 fsbtodb(fs, disk_block),
426 block_size, fp->f_buf, &fp->f_buf_size);
431 fp->f_buf_blkno = file_block;
435 * Return address of byte in buffer corresponding to
436 * offset, and size of remainder of buffer after that
439 *buf_p = fp->f_buf + off;
440 *size_p = block_size - off;
443 * But truncate buffer at end of file.
445 if (*size_p > DIP(fp, di_size) - fp->f_seekp)
446 *size_p = DIP(fp, di_size) - fp->f_seekp;
452 * Search a directory for a name and return its
456 search_directory(char *name, struct open_file *f, ino_t *inumber_p)
458 struct file *fp = (struct file *)f->f_fsdata;
466 length = strlen(name);
469 while (fp->f_seekp < DIP(fp, di_size)) {
470 rc = buf_read_file(f, &buf, &buf_size);
474 dp = (struct direct *)buf;
475 edp = (struct direct *)(buf + buf_size);
477 if (dp->d_ino == (ino_t)0)
479 #if BYTE_ORDER == LITTLE_ENDIAN
480 if (fp->f_fs->fs_maxsymlinklen <= 0)
484 namlen = dp->d_namlen;
485 if (namlen == length &&
486 !strcmp(name, dp->d_name)) {
488 *inumber_p = dp->d_ino;
492 dp = (struct direct *)((char *)dp + dp->d_reclen);
494 fp->f_seekp += buf_size;
503 ufs_open(const char *upath, struct open_file *f)
507 ino_t inumber, parent_inumber;
512 char namebuf[MAXPATHLEN+1];
518 /* allocate file system specific data structure */
520 fp = calloc(1, sizeof(struct file));
523 f->f_fsdata = (void *)fp;
525 dev = devformat((struct devdesc *)f->f_devdata);
526 /* Is this device mounted? */
527 STAILQ_FOREACH(mnt, &mnt_list, um_link) {
528 if (strcmp(dev, mnt->um_dev) == 0)
533 /* read super block */
535 if ((rc = ffs_sbget(f, &fs, UFS_STDSB, UFS_NOHASHFAIL, "stand",
536 ufs_use_sa_read)) != 0) {
540 struct open_file *sbf;
543 /* get superblock from mounted file system */
544 sbf = fd2open_file(mnt->um_fd);
551 * Calculate indirect block levels.
558 for (level = 0; level < UFS_NIADDR; level++) {
560 fp->f_nindir[level] = mult;
564 inumber = UFS_ROOTINO;
565 if ((rc = read_inode(inumber, f)) != 0)
568 cp = path = strdup(upath);
576 * Remove extra separators
584 * Check that current node is a directory.
586 if ((DIP(fp, di_mode) & IFMT) != IFDIR) {
592 * Get next component of path name.
598 while ((c = *cp) != '\0' && c != '/') {
599 if (++len > UFS_MAXNAMLEN) {
609 * Look up component in current directory.
610 * Save directory inumber in case we find a
613 parent_inumber = inumber;
614 rc = search_directory(ncp, f, &inumber);
620 * Open next component.
622 if ((rc = read_inode(inumber, f)) != 0)
626 * Check for symbolic link.
628 if ((DIP(fp, di_mode) & IFMT) == IFLNK) {
629 int link_len = DIP(fp, di_size);
634 if (link_len + len > MAXPATHLEN ||
635 ++nlinks > MAXSYMLINKS) {
640 bcopy(cp, &namebuf[link_len], len + 1);
642 if (link_len < fs->fs_maxsymlinklen) {
643 bcopy(DIP(fp, di_shortlink), namebuf,
644 (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)UFS_ROOTINO;
679 if ((rc = read_inode(inumber, f)) != 0)
685 * Found terminal component.
695 if (mnt == NULL && fp->f_fs != NULL) {
696 free(fp->f_fs->fs_csp);
697 free(fp->f_fs->fs_si);
706 * A read function for use by standalone-layer routines.
709 ufs_use_sa_read(void *devfd, off_t loc, void **bufp, int size)
715 f = (struct open_file *)devfd;
716 if ((*bufp = malloc(size)) == NULL)
718 error = (f->f_dev->dv_strategy)(f->f_devdata, F_READ, loc / DEV_BSIZE,
719 size, *bufp, &buf_size);
722 if (buf_size != size)
728 ufs_close(struct open_file *f)
731 struct file *fp = (struct file *)f->f_fsdata;
739 for (level = 0; level < UFS_NIADDR; level++) {
740 free(fp->f_blk[level]);
744 dev = devformat((struct devdesc *)f->f_devdata);
745 STAILQ_FOREACH(mnt, &mnt_list, um_link) {
746 if (strcmp(dev, mnt->um_dev) == 0)
750 if (mnt == NULL && fp->f_fs != NULL) {
751 free(fp->f_fs->fs_csp);
752 free(fp->f_fs->fs_si);
761 * Copy a portion of a file into kernel memory.
762 * Cross block boundaries when necessary.
765 ufs_read(struct open_file *f, void *start, size_t size, size_t *resid)
767 struct file *fp = (struct file *)f->f_fsdata;
775 if (fp->f_seekp >= DIP(fp, di_size))
778 rc = buf_read_file(f, &buf, &buf_size);
783 if (csize > buf_size)
786 bcopy(buf, addr, csize);
788 fp->f_seekp += csize;
798 * Write to a portion of an already allocated file.
799 * Cross block boundaries when necessary. Can not
803 ufs_write(struct open_file *f, const void *start, size_t size, size_t *resid)
805 struct file *fp = (struct file *)f->f_fsdata;
808 const char *addr = start;
811 while ((size != 0) && (csize != 0)) {
812 if (fp->f_seekp >= DIP(fp, di_size))
815 if (csize >= 512) csize = 512; /* XXX */
817 rc = buf_write_file(f, addr, &csize);
821 fp->f_seekp += csize;
831 ufs_seek(struct open_file *f, off_t offset, int where)
833 struct file *fp = (struct file *)f->f_fsdata;
837 fp->f_seekp = offset;
840 fp->f_seekp += offset;
843 fp->f_seekp = DIP(fp, di_size) - offset;
849 return (fp->f_seekp);
853 ufs_stat(struct open_file *f, struct stat *sb)
855 struct file *fp = (struct file *)f->f_fsdata;
857 /* only important stuff */
858 sb->st_mode = DIP(fp, di_mode);
859 sb->st_uid = DIP(fp, di_uid);
860 sb->st_gid = DIP(fp, di_gid);
861 sb->st_size = DIP(fp, di_size);
862 sb->st_mtime = DIP(fp, di_mtime);
864 * The items below are ufs specific!
865 * Other fs types will need their own solution
866 * if these fields are needed.
868 sb->st_ino = fp->f_inumber;
870 * We need something to differentiate devs.
871 * fs_id is unique but 64bit, we xor the two
872 * halves to squeeze it into 32bits.
874 sb->st_dev = (dev_t)(fp->f_fs->fs_id[0] ^ fp->f_fs->fs_id[1]);
880 ufs_readdir(struct open_file *f, struct dirent *d)
882 struct file *fp = (struct file *)f->f_fsdata;
889 * assume that a directory entry will not be split across blocks
893 if (fp->f_seekp >= DIP(fp, di_size))
895 error = buf_read_file(f, &buf, &buf_size);
898 dp = (struct direct *)buf;
899 fp->f_seekp += dp->d_reclen;
900 } while (dp->d_ino == (ino_t)0);
902 d->d_type = dp->d_type;
903 strcpy(d->d_name, dp->d_name);
908 ufs_mount(const char *dev, const char *path, void **data)
915 mnt = calloc(1, sizeof(*mnt));
919 mnt->um_dev = strdup(dev);
920 if (mnt->um_dev == NULL)
923 if (asprintf(&fs, "%s%s", dev, path) < 0)
926 mnt->um_fd = open(fs, O_RDONLY);
928 if (mnt->um_fd == -1)
931 /* Is it ufs file system? */
932 f = fd2open_file(mnt->um_fd);
933 if (strcmp(f->f_ops->fs_name, "ufs") == 0)
934 STAILQ_INSERT_TAIL(&mnt_list, mnt, um_link);
952 ufs_unmount(const char *dev __unused, void *data)
954 ufs_mnt_t *mnt = data;
956 STAILQ_REMOVE(&mnt_list, mnt, ufs_mnt, um_link);