1 /* $FreeBSD: src/lib/libstand/ufs.c,v 1.5.6.1 2000/05/04 13:47:53 ps Exp $ */
2 /* $NetBSD: ufs.c,v 1.20 1998/03/01 07:15:39 ross Exp $ */
6 * The Regents of the University of California. All rights reserved.
8 * This code is derived from software contributed to Berkeley by
9 * The Mach Operating System project at Carnegie-Mellon University.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * Copyright (c) 1990, 1991 Carnegie Mellon University
37 * All Rights Reserved.
41 * Permission to use, copy, modify and distribute this software and its
42 * documentation is hereby granted, provided that both the copyright
43 * notice and this permission notice appear in all copies of the
44 * software, derivative works or modified versions, and any portions
45 * thereof, and that both notices appear in supporting documentation.
47 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
48 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
49 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
51 * Carnegie Mellon requests users of this software to return to
53 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
54 * School of Computer Science
55 * Carnegie Mellon University
56 * Pittsburgh PA 15213-3890
58 * any improvements or extensions that they make and grant Carnegie the
59 * rights to redistribute these changes.
63 * Stand-alone file reading package.
66 #include <sys/param.h>
71 #include <vfs/ufs/dinode.h>
72 #include <vfs/ufs/dir.h>
73 #include <vfs/ufs/fs.h>
76 static int ufs_open(const char *path
, struct open_file
*f
);
77 static int ufs_close(struct open_file
*f
);
78 static int ufs_read(struct open_file
*f
, void *buf
, size_t size
, size_t *resid
);
79 static off_t
ufs_seek(struct open_file
*f
, off_t offset
, int where
);
80 static int ufs_stat(struct open_file
*f
, struct stat
*sb
);
81 static int ufs_readdir(struct open_file
*f
, struct dirent
*d
);
83 struct fs_ops ufs_fsops
= {
98 off_t f_seekp
; /* seek pointer */
99 struct fs
*f_fs
; /* pointer to super-block */
100 struct ufs1_dinode f_di
; /* copy of on-disk inode */
101 int f_nindir
[UFS_NIADDR
];
102 /* number of blocks mapped by
103 indirect block at level i */
104 char *f_blk
[UFS_NIADDR
];/* buffer for indirect block at
106 size_t f_blksize
[UFS_NIADDR
];
108 daddr_t f_blkno
[UFS_NIADDR
];/* disk address of block in buffer */
109 char *f_buf
; /* buffer for data block */
110 size_t f_buf_size
; /* size of data block */
111 daddr_t f_buf_blkno
; /* block number of data block */
114 static int read_inode(ino_t
, struct open_file
*);
115 static int block_map(struct open_file
*, daddr_t
, daddr_t
*);
116 static int buf_read_file(struct open_file
*, char **, size_t *);
117 static int search_directory(char *, struct open_file
*, ino_t
*);
119 static void ffs_oldfscompat(struct fs
*);
123 * Read a new inode into a file structure.
126 read_inode(ino_t inumber
, struct open_file
*f
)
128 struct file
*fp
= (struct file
*)f
->f_fsdata
;
129 struct fs
*fs
= fp
->f_fs
;
138 * Read inode and save it.
140 buf
= malloc(fs
->fs_bsize
);
142 rc
= (f
->f_dev
->dv_strategy
)(f
->f_devdata
, F_READ
,
143 fsbtodb(fs
, ino_to_fsba(fs
, inumber
)), fs
->fs_bsize
,
147 if (rsize
!= fs
->fs_bsize
) {
153 struct ufs1_dinode
*dp
;
155 dp
= (struct ufs1_dinode
*)buf
;
156 fp
->f_di
= dp
[ino_to_fsbo(fs
, inumber
)];
160 * Clear out the old buffers
165 for (level
= 0; level
< UFS_NIADDR
; level
++)
166 fp
->f_blkno
[level
] = -1;
167 fp
->f_buf_blkno
= -1;
175 * Given an offset in a file, find the disk block number that
176 * contains that block.
182 block_map(struct open_file
*f
, daddr_t file_block
, daddr_t
*disk_block_p
)
184 struct file
*fp
= (struct file
*)f
->f_fsdata
;
185 struct fs
*fs
= fp
->f_fs
;
188 daddr_t ind_block_num
;
193 * Index structure of an inode:
195 * di_db[0..UFS_NDADDR-1] hold block numbers for blocks
198 * di_ib[0] index block 0 is the single indirect block
199 * holds block numbers for blocks
200 * UFS_NDADDR .. UFS_NDADDR + NINDIR(fs)-1
202 * di_ib[1] index block 1 is the double indirect block
203 * holds block numbers for INDEX blocks for blocks
204 * UFS_NDADDR + NINDIR(fs) ..
205 * UFS_NDADDR + NINDIR(fs) + NINDIR(fs)**2 - 1
207 * di_ib[2] index block 2 is the triple indirect block
208 * holds block numbers for double-indirect
210 * UFS_NDADDR + NINDIR(fs) + NINDIR(fs)**2 ..
211 * UFS_NDADDR + NINDIR(fs) + NINDIR(fs)**2
212 * + NINDIR(fs)**3 - 1
215 if (file_block
< UFS_NDADDR
) {
217 *disk_block_p
= fp
->f_di
.di_db
[file_block
];
221 file_block
-= UFS_NDADDR
;
225 * nindir[1] = NINDIR**2
226 * nindir[2] = NINDIR**3
229 for (level
= 0; level
< UFS_NIADDR
; level
++) {
230 if (file_block
< fp
->f_nindir
[level
])
232 file_block
-= fp
->f_nindir
[level
];
234 if (level
== UFS_NIADDR
) {
235 /* Block number too high */
239 ind_block_num
= fp
->f_di
.di_ib
[level
];
241 for (; level
>= 0; level
--) {
242 if (ind_block_num
== 0) {
243 *disk_block_p
= 0; /* missing */
247 if (fp
->f_blkno
[level
] != ind_block_num
) {
248 if (fp
->f_blk
[level
] == NULL
)
250 malloc(fs
->fs_bsize
);
252 rc
= (f
->f_dev
->dv_strategy
)(f
->f_devdata
, F_READ
,
253 fsbtodb(fp
->f_fs
, ind_block_num
),
256 &fp
->f_blksize
[level
]);
259 if (fp
->f_blksize
[level
] != fs
->fs_bsize
)
261 fp
->f_blkno
[level
] = ind_block_num
;
264 ind_p
= (daddr_t
*)fp
->f_blk
[level
];
267 idx
= file_block
/ fp
->f_nindir
[level
- 1];
268 file_block
%= fp
->f_nindir
[level
- 1];
272 ind_block_num
= ind_p
[idx
];
275 *disk_block_p
= ind_block_num
;
281 * Read a portion of a file into an internal buffer. Return
282 * the location in the buffer and the amount in the buffer.
289 buf_read_file(struct open_file
*f
, char **buf_p
, size_t *size_p
)
291 struct file
*fp
= (struct file
*)f
->f_fsdata
;
292 struct fs
*fs
= fp
->f_fs
;
299 off
= blkoff(fs
, fp
->f_seekp
);
300 file_block
= lblkno(fs
, fp
->f_seekp
);
301 block_size
= dblksize(fs
, &fp
->f_di
, file_block
);
303 if (file_block
!= fp
->f_buf_blkno
) {
304 rc
= block_map(f
, file_block
, &disk_block
);
308 if (fp
->f_buf
== NULL
)
309 fp
->f_buf
= malloc(fs
->fs_bsize
);
311 if (disk_block
== 0) {
312 bzero(fp
->f_buf
, block_size
);
313 fp
->f_buf_size
= block_size
;
316 rc
= (f
->f_dev
->dv_strategy
)(f
->f_devdata
, F_READ
,
317 fsbtodb(fs
, disk_block
),
318 block_size
, fp
->f_buf
, &fp
->f_buf_size
);
323 fp
->f_buf_blkno
= file_block
;
327 * Return address of byte in buffer corresponding to
328 * offset, and size of remainder of buffer after that
331 *buf_p
= fp
->f_buf
+ off
;
332 *size_p
= block_size
- off
;
335 * But truncate buffer at end of file.
337 if (*size_p
> fp
->f_di
.di_size
- fp
->f_seekp
)
338 *size_p
= fp
->f_di
.di_size
- fp
->f_seekp
;
344 * Search a directory for a name and return its
351 search_directory(char *name
, struct open_file
*f
, ino_t
*inumber_p
)
353 struct file
*fp
= (struct file
*)f
->f_fsdata
;
361 length
= strlen(name
);
364 while (fp
->f_seekp
< fp
->f_di
.di_size
) {
365 rc
= buf_read_file(f
, &buf
, &buf_size
);
369 dp
= (struct direct
*)buf
;
370 edp
= (struct direct
*)(buf
+ buf_size
);
372 if (dp
->d_ino
== (ino_t
)0)
374 if (dp
->d_type
== DT_WHT
)
376 #if BYTE_ORDER == LITTLE_ENDIAN
377 if (fp
->f_fs
->fs_maxsymlinklen
<= 0)
381 namlen
= dp
->d_namlen
;
382 if (namlen
== length
&&
383 !strcmp(name
, dp
->d_name
)) {
385 *inumber_p
= dp
->d_ino
;
389 dp
= (struct direct
*)((char *)dp
+ dp
->d_reclen
);
391 fp
->f_seekp
+= buf_size
;
400 ufs_open(const char *upath
, struct open_file
*f
)
404 ino_t inumber
, parent_inumber
;
410 char namebuf
[MAXPATHLEN
+1];
414 /* allocate file system specific data structure */
415 fp
= malloc(sizeof(struct file
));
416 bzero(fp
, sizeof(struct file
));
417 f
->f_fsdata
= (void *)fp
;
419 /* allocate space and read super block */
423 rc
= (f
->f_dev
->dv_strategy
)(f
->f_devdata
, F_READ
,
424 SBOFF
/ DEV_BSIZE
, SBSIZE
, (char *)fs
, &buf_size
);
428 if (buf_size
!= SBSIZE
|| fs
->fs_magic
!= FS_MAGIC
||
429 fs
->fs_bsize
> MAXBSIZE
|| fs
->fs_bsize
< sizeof(struct fs
)) {
438 * Calculate indirect block levels.
447 for (level
= 0; level
< UFS_NIADDR
; level
++) {
451 fp
->f_nindir
[level
] = mult
;
456 inumber
= UFS_ROOTINO
;
457 if ((rc
= read_inode(inumber
, f
)) != 0)
460 cp
= path
= strdup(upath
);
468 * Remove extra separators
476 * Check that current node is a directory.
478 if ((fp
->f_di
.di_mode
& IFMT
) != IFDIR
) {
484 * Get next component of path name.
490 while ((c
= *cp
) != '\0' && c
!= '/') {
491 if (++len
> MAXNAMLEN
) {
501 * Look up component in current directory.
502 * Save directory inumber in case we find a
505 parent_inumber
= inumber
;
506 rc
= search_directory(ncp
, f
, &inumber
);
512 * Open next component.
514 if ((rc
= read_inode(inumber
, f
)) != 0)
518 * Check for symbolic link.
520 if ((fp
->f_di
.di_mode
& IFMT
) == IFLNK
) {
521 int link_len
= fp
->f_di
.di_size
;
526 if (link_len
+ len
> MAXPATHLEN
||
527 ++nlinks
> MAXSYMLINKS
) {
532 bcopy(cp
, &namebuf
[link_len
], len
+ 1);
534 if (link_len
< fs
->fs_maxsymlinklen
) {
535 bcopy(fp
->f_di
.di_shortlink
, namebuf
,
536 (unsigned) link_len
);
539 * Read file for symbolic link
543 struct fs
*fs
= fp
->f_fs
;
546 buf
= malloc(fs
->fs_bsize
);
547 rc
= block_map(f
, (daddr_t
)0, &disk_block
);
552 rc
= (f
->f_dev
->dv_strategy
)(f
->f_devdata
,
553 F_READ
, fsbtodb(fs
, disk_block
),
554 fs
->fs_bsize
, buf
, &buf_size
);
558 bcopy((char *)buf
, namebuf
, (unsigned)link_len
);
562 * If relative pathname, restart at parent directory.
563 * If absolute pathname, restart at root.
567 inumber
= parent_inumber
;
569 inumber
= (ino_t
)UFS_ROOTINO
;
571 if ((rc
= read_inode(inumber
, f
)) != 0)
577 * Found terminal component.
597 ufs_close(struct open_file
*f
)
599 struct file
*fp
= (struct file
*)f
->f_fsdata
;
606 for (level
= 0; level
< UFS_NIADDR
; level
++) {
607 if (fp
->f_blk
[level
])
608 free(fp
->f_blk
[level
]);
618 * Copy a portion of a file into kernel memory.
619 * Cross block boundaries when necessary.
625 ufs_read(struct open_file
*f
, void *start
, size_t size
, size_t *resid
)
627 struct file
*fp
= (struct file
*)f
->f_fsdata
;
635 if (fp
->f_seekp
>= fp
->f_di
.di_size
)
638 rc
= buf_read_file(f
, &buf
, &buf_size
);
643 if (csize
> buf_size
)
646 bcopy(buf
, addr
, csize
);
648 fp
->f_seekp
+= csize
;
658 ufs_seek(struct open_file
*f
, off_t offset
, int where
)
660 struct file
*fp
= (struct file
*)f
->f_fsdata
;
664 fp
->f_seekp
= offset
;
667 fp
->f_seekp
+= offset
;
670 fp
->f_seekp
= fp
->f_di
.di_size
- offset
;
675 return (fp
->f_seekp
);
679 ufs_stat(struct open_file
*f
, struct stat
*sb
)
681 struct file
*fp
= (struct file
*)f
->f_fsdata
;
683 /* only important stuff */
684 sb
->st_mode
= fp
->f_di
.di_mode
;
685 sb
->st_uid
= fp
->f_di
.di_uid
;
686 sb
->st_gid
= fp
->f_di
.di_gid
;
687 sb
->st_size
= fp
->f_di
.di_size
;
692 ufs_readdir(struct open_file
*f
, struct dirent
*d
)
694 struct file
*fp
= (struct file
*)f
->f_fsdata
;
701 * assume that a directory entry will not be split across blocks
704 if (fp
->f_seekp
>= fp
->f_di
.di_size
)
706 error
= buf_read_file(f
, &buf
, &buf_size
);
709 dp
= (struct direct
*)buf
;
710 fp
->f_seekp
+= dp
->d_reclen
;
711 if (dp
->d_ino
== (ino_t
)0)
713 d
->d_type
= dp
->d_type
;
714 strcpy(d
->d_name
, dp
->d_name
);
720 * Sanity checks for old file systems.
722 * XXX - goes away some day.
725 ffs_oldfscompat(struct fs
*fs
)
729 fs
->fs_npsect
= max(fs
->fs_npsect
, fs
->fs_nsect
); /* XXX */
730 fs
->fs_interleave
= max(fs
->fs_interleave
, 1); /* XXX */
731 if (fs
->fs_postblformat
== FS_42POSTBLFMT
) /* XXX */
732 fs
->fs_nrpos
= 8; /* XXX */
733 if (fs
->fs_inodefmt
< FS_44INODEFMT
) { /* XXX */
734 quad_t sizepb
= fs
->fs_bsize
; /* XXX */
736 fs
->fs_maxfilesize
= fs
->fs_bsize
* UFS_NDADDR
- 1; /* XXX */
737 for (i
= 0; i
< UFS_NIADDR
; i
++) { /* XXX */
738 sizepb
*= NINDIR(fs
); /* XXX */
739 fs
->fs_maxfilesize
+= sizepb
; /* XXX */
741 fs
->fs_qbmask
= ~fs
->fs_bmask
; /* XXX */
742 fs
->fs_qfmask
= ~fs
->fs_fmask
; /* XXX */