fdisk - Use heads = 255 on file images
[dragonfly.git] / lib / libstand / ufs.c
bloba47a5aadffab2452dcc4fd820e3c4b45e1a760d6
1 /* $FreeBSD: src/lib/libstand/ufs.c,v 1.5.6.1 2000/05/04 13:47:53 ps Exp $ */
2 /* $DragonFly: src/lib/libstand/ufs.c,v 1.9 2006/04/03 01:58:45 dillon Exp $ */
3 /* $NetBSD: ufs.c,v 1.20 1998/03/01 07:15:39 ross Exp $ */
5 /*-
6 * Copyright (c) 1993
7 * The Regents of the University of California. All rights reserved.
9 * This code is derived from software contributed to Berkeley by
10 * The Mach Operating System project at Carnegie-Mellon University.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the University of
23 * California, Berkeley and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * SUCH DAMAGE.
41 * Copyright (c) 1990, 1991 Carnegie Mellon University
42 * All Rights Reserved.
44 * Author: David Golub
46 * Permission to use, copy, modify and distribute this software and its
47 * documentation is hereby granted, provided that both the copyright
48 * notice and this permission notice appear in all copies of the
49 * software, derivative works or modified versions, and any portions
50 * thereof, and that both notices appear in supporting documentation.
52 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
53 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
54 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
56 * Carnegie Mellon requests users of this software to return to
58 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
59 * School of Computer Science
60 * Carnegie Mellon University
61 * Pittsburgh PA 15213-3890
63 * any improvements or extensions that they make and grant Carnegie the
64 * rights to redistribute these changes.
68 * Stand-alone file reading package.
71 #include <sys/param.h>
72 #include <sys/time.h>
74 #include "stand.h"
76 #include <vfs/ufs/dinode.h>
77 #include <vfs/ufs/dir.h>
78 #include <vfs/ufs/fs.h>
79 #include "string.h"
81 static int ufs_open(const char *path, struct open_file *f);
82 static int ufs_close(struct open_file *f);
83 static int ufs_read(struct open_file *f, void *buf, size_t size, size_t *resid);
84 static off_t ufs_seek(struct open_file *f, off_t offset, int where);
85 static int ufs_stat(struct open_file *f, struct stat *sb);
86 static int ufs_readdir(struct open_file *f, struct dirent *d);
88 struct fs_ops ufs_fsops = {
89 "ufs",
90 ufs_open,
91 ufs_close,
92 ufs_read,
93 null_write,
94 ufs_seek,
95 ufs_stat,
96 ufs_readdir
100 * In-core open file.
102 struct file {
103 off_t f_seekp; /* seek pointer */
104 struct fs *f_fs; /* pointer to super-block */
105 struct ufs1_dinode f_di; /* copy of on-disk inode */
106 int f_nindir[NIADDR];
107 /* number of blocks mapped by
108 indirect block at level i */
109 char *f_blk[NIADDR]; /* buffer for indirect block at
110 level i */
111 size_t f_blksize[NIADDR];
112 /* size of buffer */
113 daddr_t f_blkno[NIADDR];/* disk address of block in buffer */
114 char *f_buf; /* buffer for data block */
115 size_t f_buf_size; /* size of data block */
116 daddr_t f_buf_blkno; /* block number of data block */
119 static int read_inode(ino_t, struct open_file *);
120 static int block_map(struct open_file *, daddr_t, daddr_t *);
121 static int buf_read_file(struct open_file *, char **, size_t *);
122 static int search_directory(char *, struct open_file *, ino_t *);
123 #ifdef COMPAT_UFS
124 static void ffs_oldfscompat(struct fs *);
125 #endif
128 * Read a new inode into a file structure.
130 static int
131 read_inode(ino_t inumber, struct open_file *f)
133 struct file *fp = (struct file *)f->f_fsdata;
134 struct fs *fs = fp->f_fs;
135 char *buf;
136 size_t rsize;
137 int rc;
139 if (fs == NULL)
140 panic("fs == NULL");
143 * Read inode and save it.
145 buf = malloc(fs->fs_bsize);
146 twiddle();
147 rc = (f->f_dev->dv_strategy)(f->f_devdata, F_READ,
148 fsbtodb(fs, ino_to_fsba(fs, inumber)), fs->fs_bsize,
149 buf, &rsize);
150 if (rc)
151 goto out;
152 if (rsize != fs->fs_bsize) {
153 rc = EIO;
154 goto out;
158 struct ufs1_dinode *dp;
160 dp = (struct ufs1_dinode *)buf;
161 fp->f_di = dp[ino_to_fsbo(fs, inumber)];
165 * Clear out the old buffers
168 int level;
170 for (level = 0; level < NIADDR; level++)
171 fp->f_blkno[level] = -1;
172 fp->f_buf_blkno = -1;
174 out:
175 free(buf);
176 return (rc);
180 * Given an offset in a file, find the disk block number that
181 * contains that block.
183 * Parameters:
184 * disk_block_p: out
186 static int
187 block_map(struct open_file *f, daddr_t file_block, daddr_t *disk_block_p)
189 struct file *fp = (struct file *)f->f_fsdata;
190 struct fs *fs = fp->f_fs;
191 int level;
192 int idx;
193 daddr_t ind_block_num;
194 daddr_t *ind_p;
195 int rc;
198 * Index structure of an inode:
200 * di_db[0..NDADDR-1] hold block numbers for blocks
201 * 0..NDADDR-1
203 * di_ib[0] index block 0 is the single indirect block
204 * holds block numbers for blocks
205 * NDADDR .. NDADDR + NINDIR(fs)-1
207 * di_ib[1] index block 1 is the double indirect block
208 * holds block numbers for INDEX blocks for blocks
209 * NDADDR + NINDIR(fs) ..
210 * NDADDR + NINDIR(fs) + NINDIR(fs)**2 - 1
212 * di_ib[2] index block 2 is the triple indirect block
213 * holds block numbers for double-indirect
214 * blocks for blocks
215 * NDADDR + NINDIR(fs) + NINDIR(fs)**2 ..
216 * NDADDR + NINDIR(fs) + NINDIR(fs)**2
217 * + NINDIR(fs)**3 - 1
220 if (file_block < NDADDR) {
221 /* Direct block. */
222 *disk_block_p = fp->f_di.di_db[file_block];
223 return (0);
226 file_block -= NDADDR;
229 * nindir[0] = NINDIR
230 * nindir[1] = NINDIR**2
231 * nindir[2] = NINDIR**3
232 * etc
234 for (level = 0; level < NIADDR; level++) {
235 if (file_block < fp->f_nindir[level])
236 break;
237 file_block -= fp->f_nindir[level];
239 if (level == NIADDR) {
240 /* Block number too high */
241 return (EFBIG);
244 ind_block_num = fp->f_di.di_ib[level];
246 for (; level >= 0; level--) {
247 if (ind_block_num == 0) {
248 *disk_block_p = 0; /* missing */
249 return (0);
252 if (fp->f_blkno[level] != ind_block_num) {
253 if (fp->f_blk[level] == NULL)
254 fp->f_blk[level] =
255 malloc(fs->fs_bsize);
256 twiddle();
257 rc = (f->f_dev->dv_strategy)(f->f_devdata, F_READ,
258 fsbtodb(fp->f_fs, ind_block_num),
259 fs->fs_bsize,
260 fp->f_blk[level],
261 &fp->f_blksize[level]);
262 if (rc)
263 return (rc);
264 if (fp->f_blksize[level] != fs->fs_bsize)
265 return (EIO);
266 fp->f_blkno[level] = ind_block_num;
269 ind_p = (daddr_t *)fp->f_blk[level];
271 if (level > 0) {
272 idx = file_block / fp->f_nindir[level - 1];
273 file_block %= fp->f_nindir[level - 1];
274 } else
275 idx = file_block;
277 ind_block_num = ind_p[idx];
280 *disk_block_p = ind_block_num;
282 return (0);
286 * Read a portion of a file into an internal buffer. Return
287 * the location in the buffer and the amount in the buffer.
289 * Parameters:
290 * buf_p: out
291 * size_p: out
293 static int
294 buf_read_file(struct open_file *f, char **buf_p, size_t *size_p)
296 struct file *fp = (struct file *)f->f_fsdata;
297 struct fs *fs = fp->f_fs;
298 long off;
299 daddr_t file_block;
300 daddr_t disk_block;
301 size_t block_size;
302 int rc;
304 off = blkoff(fs, fp->f_seekp);
305 file_block = lblkno(fs, fp->f_seekp);
306 block_size = dblksize(fs, &fp->f_di, file_block);
308 if (file_block != fp->f_buf_blkno) {
309 rc = block_map(f, file_block, &disk_block);
310 if (rc)
311 return (rc);
313 if (fp->f_buf == NULL)
314 fp->f_buf = malloc(fs->fs_bsize);
316 if (disk_block == 0) {
317 bzero(fp->f_buf, block_size);
318 fp->f_buf_size = block_size;
319 } else {
320 twiddle();
321 rc = (f->f_dev->dv_strategy)(f->f_devdata, F_READ,
322 fsbtodb(fs, disk_block),
323 block_size, fp->f_buf, &fp->f_buf_size);
324 if (rc)
325 return (rc);
328 fp->f_buf_blkno = file_block;
332 * Return address of byte in buffer corresponding to
333 * offset, and size of remainder of buffer after that
334 * byte.
336 *buf_p = fp->f_buf + off;
337 *size_p = block_size - off;
340 * But truncate buffer at end of file.
342 if (*size_p > fp->f_di.di_size - fp->f_seekp)
343 *size_p = fp->f_di.di_size - fp->f_seekp;
345 return (0);
349 * Search a directory for a name and return its
350 * i_number.
352 * Parameters:
353 * inumber_p: out
355 static int
356 search_directory(char *name, struct open_file *f, ino_t *inumber_p)
358 struct file *fp = (struct file *)f->f_fsdata;
359 struct direct *dp;
360 struct direct *edp;
361 char *buf;
362 size_t buf_size;
363 int namlen, length;
364 int rc;
366 length = strlen(name);
368 fp->f_seekp = 0;
369 while (fp->f_seekp < fp->f_di.di_size) {
370 rc = buf_read_file(f, &buf, &buf_size);
371 if (rc)
372 return (rc);
374 dp = (struct direct *)buf;
375 edp = (struct direct *)(buf + buf_size);
376 while (dp < edp) {
377 if (dp->d_ino == (ino_t)0)
378 goto next;
379 #if BYTE_ORDER == LITTLE_ENDIAN
380 if (fp->f_fs->fs_maxsymlinklen <= 0)
381 namlen = dp->d_type;
382 else
383 #endif
384 namlen = dp->d_namlen;
385 if (namlen == length &&
386 !strcmp(name, dp->d_name)) {
387 /* found entry */
388 *inumber_p = dp->d_ino;
389 return (0);
391 next:
392 dp = (struct direct *)((char *)dp + dp->d_reclen);
394 fp->f_seekp += buf_size;
396 return (ENOENT);
400 * Open a file.
402 static int
403 ufs_open(const char *upath, struct open_file *f)
405 char *cp, *ncp;
406 int c;
407 ino_t inumber, parent_inumber;
408 struct file *fp;
409 struct fs *fs;
410 int rc;
411 size_t buf_size;
412 int nlinks = 0;
413 char namebuf[MAXPATHLEN+1];
414 char *buf = NULL;
415 char *path = NULL;
417 /* allocate file system specific data structure */
418 fp = malloc(sizeof(struct file));
419 bzero(fp, sizeof(struct file));
420 f->f_fsdata = (void *)fp;
422 /* allocate space and read super block */
423 fs = malloc(SBSIZE);
424 fp->f_fs = fs;
425 twiddle();
426 rc = (f->f_dev->dv_strategy)(f->f_devdata, F_READ,
427 SBOFF / DEV_BSIZE, SBSIZE, (char *)fs, &buf_size);
428 if (rc)
429 goto out;
431 if (buf_size != SBSIZE || fs->fs_magic != FS_MAGIC ||
432 fs->fs_bsize > MAXBSIZE || fs->fs_bsize < sizeof(struct fs)) {
433 rc = EINVAL;
434 goto out;
436 #ifdef COMPAT_UFS
437 ffs_oldfscompat(fs);
438 #endif
441 * Calculate indirect block levels.
444 int mult;
445 int level;
447 mult = 1;
448 for (level = 0; level < NIADDR; level++) {
449 mult *= NINDIR(fs);
450 fp->f_nindir[level] = mult;
454 inumber = ROOTINO;
455 if ((rc = read_inode(inumber, f)) != 0)
456 goto out;
458 cp = path = strdup(upath);
459 if (path == NULL) {
460 rc = ENOMEM;
461 goto out;
463 while (*cp) {
466 * Remove extra separators
468 while (*cp == '/')
469 cp++;
470 if (*cp == '\0')
471 break;
474 * Check that current node is a directory.
476 if ((fp->f_di.di_mode & IFMT) != IFDIR) {
477 rc = ENOTDIR;
478 goto out;
482 * Get next component of path name.
485 int len = 0;
487 ncp = cp;
488 while ((c = *cp) != '\0' && c != '/') {
489 if (++len > MAXNAMLEN) {
490 rc = ENOENT;
491 goto out;
493 cp++;
495 *cp = '\0';
499 * Look up component in current directory.
500 * Save directory inumber in case we find a
501 * symbolic link.
503 parent_inumber = inumber;
504 rc = search_directory(ncp, f, &inumber);
505 *cp = c;
506 if (rc)
507 goto out;
510 * Open next component.
512 if ((rc = read_inode(inumber, f)) != 0)
513 goto out;
516 * Check for symbolic link.
518 if ((fp->f_di.di_mode & IFMT) == IFLNK) {
519 int link_len = fp->f_di.di_size;
520 int len;
522 len = strlen(cp);
524 if (link_len + len > MAXPATHLEN ||
525 ++nlinks > MAXSYMLINKS) {
526 rc = ENOENT;
527 goto out;
530 bcopy(cp, &namebuf[link_len], len + 1);
532 if (link_len < fs->fs_maxsymlinklen) {
533 bcopy(fp->f_di.di_shortlink, namebuf,
534 (unsigned) link_len);
535 } else {
537 * Read file for symbolic link
539 size_t buf_size;
540 daddr_t disk_block;
541 struct fs *fs = fp->f_fs;
543 if (!buf)
544 buf = malloc(fs->fs_bsize);
545 rc = block_map(f, (daddr_t)0, &disk_block);
546 if (rc)
547 goto out;
549 twiddle();
550 rc = (f->f_dev->dv_strategy)(f->f_devdata,
551 F_READ, fsbtodb(fs, disk_block),
552 fs->fs_bsize, buf, &buf_size);
553 if (rc)
554 goto out;
556 bcopy((char *)buf, namebuf, (unsigned)link_len);
560 * If relative pathname, restart at parent directory.
561 * If absolute pathname, restart at root.
563 cp = namebuf;
564 if (*cp != '/')
565 inumber = parent_inumber;
566 else
567 inumber = (ino_t)ROOTINO;
569 if ((rc = read_inode(inumber, f)) != 0)
570 goto out;
575 * Found terminal component.
577 rc = 0;
578 out:
579 if (buf)
580 free(buf);
581 if (path)
582 free(path);
583 if (rc) {
584 f->f_fsdata = NULL;
585 if (fp->f_buf)
586 free(fp->f_buf);
587 free(fp->f_fs);
588 free(fp);
590 return (rc);
593 static int
594 ufs_close(struct open_file *f)
596 struct file *fp = (struct file *)f->f_fsdata;
597 int level;
599 f->f_fsdata = NULL;
600 if (fp == NULL)
601 return (0);
603 for (level = 0; level < NIADDR; level++) {
604 if (fp->f_blk[level])
605 free(fp->f_blk[level]);
607 if (fp->f_buf)
608 free(fp->f_buf);
609 free(fp->f_fs);
610 free(fp);
611 return (0);
615 * Copy a portion of a file into kernel memory.
616 * Cross block boundaries when necessary.
618 * Parameters:
619 * resid: out
621 static int
622 ufs_read(struct open_file *f, void *start, size_t size, size_t *resid)
624 struct file *fp = (struct file *)f->f_fsdata;
625 size_t csize;
626 char *buf;
627 size_t buf_size;
628 int rc = 0;
629 char *addr = start;
631 while (size != 0) {
632 if (fp->f_seekp >= fp->f_di.di_size)
633 break;
635 rc = buf_read_file(f, &buf, &buf_size);
636 if (rc)
637 break;
639 csize = size;
640 if (csize > buf_size)
641 csize = buf_size;
643 bcopy(buf, addr, csize);
645 fp->f_seekp += csize;
646 addr += csize;
647 size -= csize;
649 if (resid)
650 *resid = size;
651 return (rc);
654 static off_t
655 ufs_seek(struct open_file *f, off_t offset, int where)
657 struct file *fp = (struct file *)f->f_fsdata;
659 switch (where) {
660 case SEEK_SET:
661 fp->f_seekp = offset;
662 break;
663 case SEEK_CUR:
664 fp->f_seekp += offset;
665 break;
666 case SEEK_END:
667 fp->f_seekp = fp->f_di.di_size - offset;
668 break;
669 default:
670 return (-1);
672 return (fp->f_seekp);
675 static int
676 ufs_stat(struct open_file *f, struct stat *sb)
678 struct file *fp = (struct file *)f->f_fsdata;
680 /* only important stuff */
681 sb->st_mode = fp->f_di.di_mode;
682 sb->st_uid = fp->f_di.di_uid;
683 sb->st_gid = fp->f_di.di_gid;
684 sb->st_size = fp->f_di.di_size;
685 return (0);
688 static int
689 ufs_readdir(struct open_file *f, struct dirent *d)
691 struct file *fp = (struct file *)f->f_fsdata;
692 struct direct *dp;
693 char *buf;
694 size_t buf_size;
695 int error;
698 * assume that a directory entry will not be split across blocks
700 again:
701 if (fp->f_seekp >= fp->f_di.di_size)
702 return (ENOENT);
703 error = buf_read_file(f, &buf, &buf_size);
704 if (error)
705 return (error);
706 dp = (struct direct *)buf;
707 fp->f_seekp += dp->d_reclen;
708 if (dp->d_ino == (ino_t)0)
709 goto again;
710 d->d_type = dp->d_type;
711 strcpy(d->d_name, dp->d_name);
712 return (0);
715 #ifdef COMPAT_UFS
717 * Sanity checks for old file systems.
719 * XXX - goes away some day.
721 static void
722 ffs_oldfscompat(struct fs *fs)
724 int i;
726 fs->fs_npsect = max(fs->fs_npsect, fs->fs_nsect); /* XXX */
727 fs->fs_interleave = max(fs->fs_interleave, 1); /* XXX */
728 if (fs->fs_postblformat == FS_42POSTBLFMT) /* XXX */
729 fs->fs_nrpos = 8; /* XXX */
730 if (fs->fs_inodefmt < FS_44INODEFMT) { /* XXX */
731 quad_t sizepb = fs->fs_bsize; /* XXX */
732 /* XXX */
733 fs->fs_maxfilesize = fs->fs_bsize * NDADDR - 1; /* XXX */
734 for (i = 0; i < NIADDR; i++) { /* XXX */
735 sizepb *= NINDIR(fs); /* XXX */
736 fs->fs_maxfilesize += sizepb; /* XXX */
737 } /* XXX */
738 fs->fs_qbmask = ~fs->fs_bmask; /* XXX */
739 fs->fs_qfmask = ~fs->fs_fmask; /* XXX */
740 } /* XXX */
742 #endif