1 /* $NetBSD: ufs.c,v 1.20 1998/03/01 07:15:39 ross Exp $ */
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * The Mach Operating System project at Carnegie-Mellon University.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 * Copyright (c) 1990, 1991 Carnegie Mellon University
40 * All Rights Reserved.
44 * Permission to use, copy, modify and distribute this software and its
45 * documentation is hereby granted, provided that both the copyright
46 * notice and this permission notice appear in all copies of the
47 * software, derivative works or modified versions, and any portions
48 * thereof, and that both notices appear in supporting documentation.
50 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
51 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
52 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
54 * Carnegie Mellon requests users of this software to return to
56 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
57 * School of Computer Science
58 * Carnegie Mellon University
59 * Pittsburgh PA 15213-3890
61 * any improvements or extensions that they make and grant Carnegie the
62 * rights to redistribute these changes.
65 #include <sys/cdefs.h>
66 __FBSDID("$FreeBSD$");
69 * Stand-alone file reading package.
72 #include <sys/param.h>
74 #include <ufs/ufs/dinode.h>
75 #include <ufs/ufs/dir.h>
76 #include <ufs/ffs/fs.h>
81 #define COMPAT_UFS /* DUX has old format file systems */
84 static int ufs_open(const char *path, struct open_file *f);
85 static int ufs_write(struct open_file *f, void *buf, size_t size, size_t *resid);
86 static int ufs_close(struct open_file *f);
87 static int ufs_read(struct open_file *f, void *buf, size_t size, size_t *resid);
88 static off_t ufs_seek(struct open_file *f, off_t offset, int where);
89 static int ufs_stat(struct open_file *f, struct stat *sb);
90 static int ufs_readdir(struct open_file *f, struct dirent *d);
92 struct fs_ops ufs_fsops = {
107 off_t f_seekp; /* seek pointer */
108 struct fs *f_fs; /* pointer to super-block */
109 struct dinode f_di; /* copy of on-disk inode */
110 int f_nindir[NIADDR];
111 /* number of blocks mapped by
112 indirect block at level i */
113 char *f_blk[NIADDR]; /* buffer for indirect block at
115 size_t f_blksize[NIADDR];
117 daddr_t f_blkno[NIADDR];/* disk address of block in buffer */
118 char *f_buf; /* buffer for data block */
119 size_t f_buf_size; /* size of data block */
120 daddr_t f_buf_blkno; /* block number of data block */
123 static int read_inode(ino_t, struct open_file *);
124 static int block_map(struct open_file *, daddr_t, daddr_t *);
125 static int buf_read_file(struct open_file *, char **, size_t *);
126 static int buf_write_file(struct open_file *, char *, size_t *);
127 static int search_directory(char *, struct open_file *, ino_t *);
129 static void ffs_oldfscompat(struct fs *);
133 * Read a new inode into a file structure.
136 read_inode(inumber, f)
140 struct file *fp = (struct file *)f->f_fsdata;
141 struct fs *fs = fp->f_fs;
150 * Read inode and save it.
152 buf = malloc(fs->fs_bsize);
154 rc = (f->f_dev->dv_strategy)(f->f_devdata, F_READ,
155 fsbtodb(fs, ino_to_fsba(fs, inumber)), fs->fs_bsize,
159 if (rsize != fs->fs_bsize) {
167 dp = (struct dinode *)buf;
168 fp->f_di = dp[ino_to_fsbo(fs, inumber)];
172 * Clear out the old buffers
177 for (level = 0; level < NIADDR; level++)
178 fp->f_blkno[level] = -1;
179 fp->f_buf_blkno = -1;
187 * Given an offset in a file, find the disk block number that
188 * contains that block.
191 block_map(f, file_block, disk_block_p)
194 daddr_t *disk_block_p; /* out */
196 struct file *fp = (struct file *)f->f_fsdata;
197 struct fs *fs = fp->f_fs;
200 daddr_t ind_block_num;
205 * Index structure of an inode:
207 * di_db[0..NDADDR-1] hold block numbers for blocks
210 * di_ib[0] index block 0 is the single indirect block
211 * holds block numbers for blocks
212 * NDADDR .. NDADDR + NINDIR(fs)-1
214 * di_ib[1] index block 1 is the double indirect block
215 * holds block numbers for INDEX blocks for blocks
216 * NDADDR + NINDIR(fs) ..
217 * NDADDR + NINDIR(fs) + NINDIR(fs)**2 - 1
219 * di_ib[2] index block 2 is the triple indirect block
220 * holds block numbers for double-indirect
222 * NDADDR + NINDIR(fs) + NINDIR(fs)**2 ..
223 * NDADDR + NINDIR(fs) + NINDIR(fs)**2
224 * + NINDIR(fs)**3 - 1
227 if (file_block < NDADDR) {
229 *disk_block_p = fp->f_di.di_db[file_block];
233 file_block -= NDADDR;
237 * nindir[1] = NINDIR**2
238 * nindir[2] = NINDIR**3
241 for (level = 0; level < NIADDR; level++) {
242 if (file_block < fp->f_nindir[level])
244 file_block -= fp->f_nindir[level];
246 if (level == NIADDR) {
247 /* Block number too high */
251 ind_block_num = fp->f_di.di_ib[level];
253 for (; level >= 0; level--) {
254 if (ind_block_num == 0) {
255 *disk_block_p = 0; /* missing */
259 if (fp->f_blkno[level] != ind_block_num) {
260 if (fp->f_blk[level] == (char *)0)
262 malloc(fs->fs_bsize);
264 rc = (f->f_dev->dv_strategy)(f->f_devdata, F_READ,
265 fsbtodb(fp->f_fs, ind_block_num),
268 &fp->f_blksize[level]);
271 if (fp->f_blksize[level] != fs->fs_bsize)
273 fp->f_blkno[level] = ind_block_num;
276 ind_p = (daddr_t *)fp->f_blk[level];
279 idx = file_block / fp->f_nindir[level - 1];
280 file_block %= fp->f_nindir[level - 1];
284 ind_block_num = ind_p[idx];
287 *disk_block_p = ind_block_num;
293 * Write a portion of a file from an internal buffer.
296 buf_write_file(f, buf_p, size_p)
299 size_t *size_p; /* out */
301 struct file *fp = (struct file *)f->f_fsdata;
302 struct fs *fs = fp->f_fs;
310 * Calculate the starting block address and offset.
312 off = blkoff(fs, fp->f_seekp);
313 file_block = lblkno(fs, fp->f_seekp);
314 block_size = dblksize(fs, &fp->f_di, file_block);
316 rc = block_map(f, file_block, &disk_block);
321 return (EFBIG); /* Because we can't allocate space on the drive */
324 * Truncate buffer at end of file, and at the end of
327 if (*size_p > fp->f_di.di_size - fp->f_seekp)
328 *size_p = fp->f_di.di_size - fp->f_seekp;
329 if (*size_p > block_size - off)
330 *size_p = block_size - off;
333 * If we don't entirely occlude the block and it's not
334 * in memory already, read it in first.
336 if (((off > 0) || (*size_p + off < block_size)) &&
337 (file_block != fp->f_buf_blkno)) {
339 if (fp->f_buf == (char *)0)
340 fp->f_buf = malloc(fs->fs_bsize);
343 rc = (f->f_dev->dv_strategy)(f->f_devdata, F_READ,
344 fsbtodb(fs, disk_block),
345 block_size, fp->f_buf, &fp->f_buf_size);
349 fp->f_buf_blkno = file_block;
353 * Copy the user data into the cached block.
355 bcopy(buf_p,fp->f_buf + off,*size_p);
358 * Write the block out to storage.
362 rc = (f->f_dev->dv_strategy)(f->f_devdata, F_WRITE,
363 fsbtodb(fs, disk_block),
364 block_size, fp->f_buf, &fp->f_buf_size);
369 * Read a portion of a file into an internal buffer. Return
370 * the location in the buffer and the amount in the buffer.
373 buf_read_file(f, buf_p, size_p)
375 char **buf_p; /* out */
376 size_t *size_p; /* out */
378 struct file *fp = (struct file *)f->f_fsdata;
379 struct fs *fs = fp->f_fs;
386 off = blkoff(fs, fp->f_seekp);
387 file_block = lblkno(fs, fp->f_seekp);
388 block_size = dblksize(fs, &fp->f_di, file_block);
390 if (file_block != fp->f_buf_blkno) {
391 if (fp->f_buf == (char *)0)
392 fp->f_buf = malloc(fs->fs_bsize);
394 rc = block_map(f, file_block, &disk_block);
398 if (disk_block == 0) {
399 bzero(fp->f_buf, block_size);
400 fp->f_buf_size = block_size;
403 rc = (f->f_dev->dv_strategy)(f->f_devdata, F_READ,
404 fsbtodb(fs, disk_block),
405 block_size, fp->f_buf, &fp->f_buf_size);
410 fp->f_buf_blkno = file_block;
414 * Return address of byte in buffer corresponding to
415 * offset, and size of remainder of buffer after that
418 *buf_p = fp->f_buf + off;
419 *size_p = block_size - off;
422 * But truncate buffer at end of file.
424 if (*size_p > fp->f_di.di_size - fp->f_seekp)
425 *size_p = fp->f_di.di_size - fp->f_seekp;
431 * Search a directory for a name and return its
435 search_directory(name, f, inumber_p)
438 ino_t *inumber_p; /* out */
440 struct file *fp = (struct file *)f->f_fsdata;
448 length = strlen(name);
451 while (fp->f_seekp < fp->f_di.di_size) {
452 rc = buf_read_file(f, &buf, &buf_size);
456 dp = (struct direct *)buf;
457 edp = (struct direct *)(buf + buf_size);
459 if (dp->d_ino == (ino_t)0)
461 #if BYTE_ORDER == LITTLE_ENDIAN
462 if (fp->f_fs->fs_maxsymlinklen <= 0)
466 namlen = dp->d_namlen;
467 if (namlen == length &&
468 !strcmp(name, dp->d_name)) {
470 *inumber_p = dp->d_ino;
474 dp = (struct direct *)((char *)dp + dp->d_reclen);
476 fp->f_seekp += buf_size;
491 ino_t inumber, parent_inumber;
497 char namebuf[MAXPATHLEN+1];
501 /* allocate file system specific data structure */
502 fp = malloc(sizeof(struct file));
503 bzero(fp, sizeof(struct file));
504 f->f_fsdata = (void *)fp;
506 /* allocate space and read super block */
510 rc = (f->f_dev->dv_strategy)(f->f_devdata, F_READ,
511 SBLOCK, SBSIZE, (char *)fs, &buf_size);
515 if (buf_size != SBSIZE || fs->fs_magic != FS_MAGIC ||
516 fs->fs_bsize > MAXBSIZE || fs->fs_bsize < sizeof(struct fs)) {
525 * Calculate indirect block levels.
532 for (level = 0; level < NIADDR; level++) {
534 fp->f_nindir[level] = mult;
539 if ((rc = read_inode(inumber, f)) != 0)
542 cp = path = strdup(upath);
550 * Remove extra separators
558 * Check that current node is a directory.
560 if ((fp->f_di.di_mode & IFMT) != IFDIR) {
566 * Get next component of path name.
572 while ((c = *cp) != '\0' && c != '/') {
573 if (++len > MAXNAMLEN) {
583 * Look up component in current directory.
584 * Save directory inumber in case we find a
587 parent_inumber = inumber;
588 rc = search_directory(ncp, f, &inumber);
594 * Open next component.
596 if ((rc = read_inode(inumber, f)) != 0)
600 * Check for symbolic link.
602 if ((fp->f_di.di_mode & IFMT) == IFLNK) {
603 int link_len = fp->f_di.di_size;
608 if (link_len + len > MAXPATHLEN ||
609 ++nlinks > MAXSYMLINKS) {
614 bcopy(cp, &namebuf[link_len], len + 1);
616 if (link_len < fs->fs_maxsymlinklen) {
617 bcopy(fp->f_di.di_shortlink, namebuf,
618 (unsigned) link_len);
621 * Read file for symbolic link
625 struct fs *fs = fp->f_fs;
628 buf = malloc(fs->fs_bsize);
629 rc = block_map(f, (daddr_t)0, &disk_block);
634 rc = (f->f_dev->dv_strategy)(f->f_devdata,
635 F_READ, fsbtodb(fs, disk_block),
636 fs->fs_bsize, buf, &buf_size);
640 bcopy((char *)buf, namebuf, (unsigned)link_len);
644 * If relative pathname, restart at parent directory.
645 * If absolute pathname, restart at root.
649 inumber = parent_inumber;
651 inumber = (ino_t)ROOTINO;
653 if ((rc = read_inode(inumber, f)) != 0)
659 * Found terminal component.
680 struct file *fp = (struct file *)f->f_fsdata;
683 f->f_fsdata = (void *)0;
684 if (fp == (struct file *)0)
687 for (level = 0; level < NIADDR; level++) {
688 if (fp->f_blk[level])
689 free(fp->f_blk[level]);
699 * Copy a portion of a file into kernel memory.
700 * Cross block boundaries when necessary.
703 ufs_read(f, start, size, resid)
707 size_t *resid; /* out */
709 struct file *fp = (struct file *)f->f_fsdata;
717 if (fp->f_seekp >= fp->f_di.di_size)
720 rc = buf_read_file(f, &buf, &buf_size);
725 if (csize > buf_size)
728 bcopy(buf, addr, csize);
730 fp->f_seekp += csize;
740 * Write to a portion of an already allocated file.
741 * Cross block boundaries when necessary. Can not
745 ufs_write(f, start, size, resid)
749 size_t *resid; /* out */
751 struct file *fp = (struct file *)f->f_fsdata;
757 while ((size != 0) && (csize != 0)) {
758 if (fp->f_seekp >= fp->f_di.di_size)
761 if (csize >= 512) csize = 512; /* XXX */
763 rc = buf_write_file(f, addr, &csize);
767 fp->f_seekp += csize;
777 ufs_seek(f, offset, where)
782 struct file *fp = (struct file *)f->f_fsdata;
786 fp->f_seekp = offset;
789 fp->f_seekp += offset;
792 fp->f_seekp = fp->f_di.di_size - offset;
797 return (fp->f_seekp);
805 struct file *fp = (struct file *)f->f_fsdata;
807 /* only important stuff */
808 sb->st_mode = fp->f_di.di_mode;
809 sb->st_uid = fp->f_di.di_uid;
810 sb->st_gid = fp->f_di.di_gid;
811 sb->st_size = fp->f_di.di_size;
816 ufs_readdir(struct open_file *f, struct dirent *d)
818 struct file *fp = (struct file *)f->f_fsdata;
825 * assume that a directory entry will not be split across blocks
828 if (fp->f_seekp >= fp->f_di.di_size)
830 error = buf_read_file(f, &buf, &buf_size);
833 dp = (struct direct *)buf;
834 fp->f_seekp += dp->d_reclen;
835 if (dp->d_ino == (ino_t)0)
837 d->d_type = dp->d_type;
838 strcpy(d->d_name, dp->d_name);
844 * Sanity checks for old file systems.
846 * XXX - goes away some day.
854 fs->fs_npsect = max(fs->fs_npsect, fs->fs_nsect); /* XXX */
855 fs->fs_interleave = max(fs->fs_interleave, 1); /* XXX */
856 if (fs->fs_postblformat == FS_42POSTBLFMT) /* XXX */
857 fs->fs_nrpos = 8; /* XXX */
858 if (fs->fs_inodefmt < FS_44INODEFMT) { /* XXX */
859 quad_t sizepb = fs->fs_bsize; /* XXX */
861 fs->fs_maxfilesize = fs->fs_bsize * NDADDR - 1; /* XXX */
862 for (i = 0; i < NIADDR; i++) { /* XXX */
863 sizepb *= NINDIR(fs); /* XXX */
864 fs->fs_maxfilesize += sizepb; /* XXX */
866 fs->fs_qbmask = ~fs->fs_bmask; /* XXX */
867 fs->fs_qfmask = ~fs->fs_fmask; /* XXX */