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 $ */
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
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. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * Copyright (c) 1990, 1991 Carnegie Mellon University
38 * All Rights Reserved.
42 * Permission to use, copy, modify and distribute this software and its
43 * documentation is hereby granted, provided that both the copyright
44 * notice and this permission notice appear in all copies of the
45 * software, derivative works or modified versions, and any portions
46 * thereof, and that both notices appear in supporting documentation.
48 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
49 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
50 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
52 * Carnegie Mellon requests users of this software to return to
54 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
55 * School of Computer Science
56 * Carnegie Mellon University
57 * Pittsburgh PA 15213-3890
59 * any improvements or extensions that they make and grant Carnegie the
60 * rights to redistribute these changes.
64 * Stand-alone file reading package.
67 #include <sys/param.h>
72 #include <vfs/ufs/dinode.h>
73 #include <vfs/ufs/dir.h>
74 #include <vfs/ufs/fs.h>
77 static int ufs_open(const char *path
, struct open_file
*f
);
78 static int ufs_close(struct open_file
*f
);
79 static int ufs_read(struct open_file
*f
, void *buf
, size_t size
, size_t *resid
);
80 static off_t
ufs_seek(struct open_file
*f
, off_t offset
, int where
);
81 static int ufs_stat(struct open_file
*f
, struct stat
*sb
);
82 static int ufs_readdir(struct open_file
*f
, struct dirent
*d
);
84 struct fs_ops ufs_fsops
= {
99 off_t f_seekp
; /* seek pointer */
100 struct fs
*f_fs
; /* pointer to super-block */
101 struct ufs1_dinode f_di
; /* copy of on-disk inode */
102 int f_nindir
[NIADDR
];
103 /* number of blocks mapped by
104 indirect block at level i */
105 char *f_blk
[NIADDR
]; /* buffer for indirect block at
107 size_t f_blksize
[NIADDR
];
109 daddr_t f_blkno
[NIADDR
];/* disk address of block in buffer */
110 char *f_buf
; /* buffer for data block */
111 size_t f_buf_size
; /* size of data block */
112 daddr_t f_buf_blkno
; /* block number of data block */
115 static int read_inode(ino_t
, struct open_file
*);
116 static int block_map(struct open_file
*, daddr_t
, daddr_t
*);
117 static int buf_read_file(struct open_file
*, char **, size_t *);
118 static int search_directory(char *, struct open_file
*, ino_t
*);
120 static void ffs_oldfscompat(struct fs
*);
124 * Read a new inode into a file structure.
127 read_inode(ino_t inumber
, struct open_file
*f
)
129 struct file
*fp
= (struct file
*)f
->f_fsdata
;
130 struct fs
*fs
= fp
->f_fs
;
139 * Read inode and save it.
141 buf
= malloc(fs
->fs_bsize
);
143 rc
= (f
->f_dev
->dv_strategy
)(f
->f_devdata
, F_READ
,
144 fsbtodb(fs
, ino_to_fsba(fs
, inumber
)), fs
->fs_bsize
,
148 if (rsize
!= fs
->fs_bsize
) {
154 struct ufs1_dinode
*dp
;
156 dp
= (struct ufs1_dinode
*)buf
;
157 fp
->f_di
= dp
[ino_to_fsbo(fs
, inumber
)];
161 * Clear out the old buffers
166 for (level
= 0; level
< NIADDR
; level
++)
167 fp
->f_blkno
[level
] = -1;
168 fp
->f_buf_blkno
= -1;
176 * Given an offset in a file, find the disk block number that
177 * contains that block.
183 block_map(struct open_file
*f
, daddr_t file_block
, daddr_t
*disk_block_p
)
185 struct file
*fp
= (struct file
*)f
->f_fsdata
;
186 struct fs
*fs
= fp
->f_fs
;
189 daddr_t ind_block_num
;
194 * Index structure of an inode:
196 * di_db[0..NDADDR-1] hold block numbers for blocks
199 * di_ib[0] index block 0 is the single indirect block
200 * holds block numbers for blocks
201 * NDADDR .. NDADDR + NINDIR(fs)-1
203 * di_ib[1] index block 1 is the double indirect block
204 * holds block numbers for INDEX blocks for blocks
205 * NDADDR + NINDIR(fs) ..
206 * NDADDR + NINDIR(fs) + NINDIR(fs)**2 - 1
208 * di_ib[2] index block 2 is the triple indirect block
209 * holds block numbers for double-indirect
211 * NDADDR + NINDIR(fs) + NINDIR(fs)**2 ..
212 * NDADDR + NINDIR(fs) + NINDIR(fs)**2
213 * + NINDIR(fs)**3 - 1
216 if (file_block
< NDADDR
) {
218 *disk_block_p
= fp
->f_di
.di_db
[file_block
];
222 file_block
-= NDADDR
;
226 * nindir[1] = NINDIR**2
227 * nindir[2] = NINDIR**3
230 for (level
= 0; level
< NIADDR
; level
++) {
231 if (file_block
< fp
->f_nindir
[level
])
233 file_block
-= fp
->f_nindir
[level
];
235 if (level
== NIADDR
) {
236 /* Block number too high */
240 ind_block_num
= fp
->f_di
.di_ib
[level
];
242 for (; level
>= 0; level
--) {
243 if (ind_block_num
== 0) {
244 *disk_block_p
= 0; /* missing */
248 if (fp
->f_blkno
[level
] != ind_block_num
) {
249 if (fp
->f_blk
[level
] == NULL
)
251 malloc(fs
->fs_bsize
);
253 rc
= (f
->f_dev
->dv_strategy
)(f
->f_devdata
, F_READ
,
254 fsbtodb(fp
->f_fs
, ind_block_num
),
257 &fp
->f_blksize
[level
]);
260 if (fp
->f_blksize
[level
] != fs
->fs_bsize
)
262 fp
->f_blkno
[level
] = ind_block_num
;
265 ind_p
= (daddr_t
*)fp
->f_blk
[level
];
268 idx
= file_block
/ fp
->f_nindir
[level
- 1];
269 file_block
%= fp
->f_nindir
[level
- 1];
273 ind_block_num
= ind_p
[idx
];
276 *disk_block_p
= ind_block_num
;
282 * Read a portion of a file into an internal buffer. Return
283 * the location in the buffer and the amount in the buffer.
290 buf_read_file(struct open_file
*f
, char **buf_p
, size_t *size_p
)
292 struct file
*fp
= (struct file
*)f
->f_fsdata
;
293 struct fs
*fs
= fp
->f_fs
;
300 off
= blkoff(fs
, fp
->f_seekp
);
301 file_block
= lblkno(fs
, fp
->f_seekp
);
302 block_size
= dblksize(fs
, &fp
->f_di
, file_block
);
304 if (file_block
!= fp
->f_buf_blkno
) {
305 rc
= block_map(f
, file_block
, &disk_block
);
309 if (fp
->f_buf
== NULL
)
310 fp
->f_buf
= malloc(fs
->fs_bsize
);
312 if (disk_block
== 0) {
313 bzero(fp
->f_buf
, block_size
);
314 fp
->f_buf_size
= block_size
;
317 rc
= (f
->f_dev
->dv_strategy
)(f
->f_devdata
, F_READ
,
318 fsbtodb(fs
, disk_block
),
319 block_size
, fp
->f_buf
, &fp
->f_buf_size
);
324 fp
->f_buf_blkno
= file_block
;
328 * Return address of byte in buffer corresponding to
329 * offset, and size of remainder of buffer after that
332 *buf_p
= fp
->f_buf
+ off
;
333 *size_p
= block_size
- off
;
336 * But truncate buffer at end of file.
338 if (*size_p
> fp
->f_di
.di_size
- fp
->f_seekp
)
339 *size_p
= fp
->f_di
.di_size
- fp
->f_seekp
;
345 * Search a directory for a name and return its
352 search_directory(char *name
, struct open_file
*f
, ino_t
*inumber_p
)
354 struct file
*fp
= (struct file
*)f
->f_fsdata
;
362 length
= strlen(name
);
365 while (fp
->f_seekp
< fp
->f_di
.di_size
) {
366 rc
= buf_read_file(f
, &buf
, &buf_size
);
370 dp
= (struct direct
*)buf
;
371 edp
= (struct direct
*)(buf
+ buf_size
);
373 if (dp
->d_ino
== (ino_t
)0)
375 if (dp
->d_type
== DT_WHT
)
377 #if BYTE_ORDER == LITTLE_ENDIAN
378 if (fp
->f_fs
->fs_maxsymlinklen
<= 0)
382 namlen
= dp
->d_namlen
;
383 if (namlen
== length
&&
384 !strcmp(name
, dp
->d_name
)) {
386 *inumber_p
= dp
->d_ino
;
390 dp
= (struct direct
*)((char *)dp
+ dp
->d_reclen
);
392 fp
->f_seekp
+= buf_size
;
401 ufs_open(const char *upath
, struct open_file
*f
)
405 ino_t inumber
, parent_inumber
;
411 char namebuf
[MAXPATHLEN
+1];
415 /* allocate file system specific data structure */
416 fp
= malloc(sizeof(struct file
));
417 bzero(fp
, sizeof(struct file
));
418 f
->f_fsdata
= (void *)fp
;
420 /* allocate space and read super block */
424 rc
= (f
->f_dev
->dv_strategy
)(f
->f_devdata
, F_READ
,
425 SBOFF
/ DEV_BSIZE
, SBSIZE
, (char *)fs
, &buf_size
);
429 if (buf_size
!= SBSIZE
|| fs
->fs_magic
!= FS_MAGIC
||
430 fs
->fs_bsize
> MAXBSIZE
|| fs
->fs_bsize
< sizeof(struct fs
)) {
439 * Calculate indirect block levels.
448 for (level
= 0; level
< NIADDR
; level
++) {
452 fp
->f_nindir
[level
] = mult
;
458 if ((rc
= read_inode(inumber
, f
)) != 0)
461 cp
= path
= strdup(upath
);
469 * Remove extra separators
477 * Check that current node is a directory.
479 if ((fp
->f_di
.di_mode
& IFMT
) != IFDIR
) {
485 * Get next component of path name.
491 while ((c
= *cp
) != '\0' && c
!= '/') {
492 if (++len
> MAXNAMLEN
) {
502 * Look up component in current directory.
503 * Save directory inumber in case we find a
506 parent_inumber
= inumber
;
507 rc
= search_directory(ncp
, f
, &inumber
);
513 * Open next component.
515 if ((rc
= read_inode(inumber
, f
)) != 0)
519 * Check for symbolic link.
521 if ((fp
->f_di
.di_mode
& IFMT
) == IFLNK
) {
522 int link_len
= fp
->f_di
.di_size
;
527 if (link_len
+ len
> MAXPATHLEN
||
528 ++nlinks
> MAXSYMLINKS
) {
533 bcopy(cp
, &namebuf
[link_len
], len
+ 1);
535 if (link_len
< fs
->fs_maxsymlinklen
) {
536 bcopy(fp
->f_di
.di_shortlink
, namebuf
,
537 (unsigned) link_len
);
540 * Read file for symbolic link
544 struct fs
*fs
= fp
->f_fs
;
547 buf
= malloc(fs
->fs_bsize
);
548 rc
= block_map(f
, (daddr_t
)0, &disk_block
);
553 rc
= (f
->f_dev
->dv_strategy
)(f
->f_devdata
,
554 F_READ
, fsbtodb(fs
, disk_block
),
555 fs
->fs_bsize
, buf
, &buf_size
);
559 bcopy((char *)buf
, namebuf
, (unsigned)link_len
);
563 * If relative pathname, restart at parent directory.
564 * If absolute pathname, restart at root.
568 inumber
= parent_inumber
;
570 inumber
= (ino_t
)ROOTINO
;
572 if ((rc
= read_inode(inumber
, f
)) != 0)
578 * Found terminal component.
598 ufs_close(struct open_file
*f
)
600 struct file
*fp
= (struct file
*)f
->f_fsdata
;
607 for (level
= 0; level
< NIADDR
; level
++) {
608 if (fp
->f_blk
[level
])
609 free(fp
->f_blk
[level
]);
619 * Copy a portion of a file into kernel memory.
620 * Cross block boundaries when necessary.
626 ufs_read(struct open_file
*f
, void *start
, size_t size
, size_t *resid
)
628 struct file
*fp
= (struct file
*)f
->f_fsdata
;
636 if (fp
->f_seekp
>= fp
->f_di
.di_size
)
639 rc
= buf_read_file(f
, &buf
, &buf_size
);
644 if (csize
> buf_size
)
647 bcopy(buf
, addr
, csize
);
649 fp
->f_seekp
+= csize
;
659 ufs_seek(struct open_file
*f
, off_t offset
, int where
)
661 struct file
*fp
= (struct file
*)f
->f_fsdata
;
665 fp
->f_seekp
= offset
;
668 fp
->f_seekp
+= offset
;
671 fp
->f_seekp
= fp
->f_di
.di_size
- offset
;
676 return (fp
->f_seekp
);
680 ufs_stat(struct open_file
*f
, struct stat
*sb
)
682 struct file
*fp
= (struct file
*)f
->f_fsdata
;
684 /* only important stuff */
685 sb
->st_mode
= fp
->f_di
.di_mode
;
686 sb
->st_uid
= fp
->f_di
.di_uid
;
687 sb
->st_gid
= fp
->f_di
.di_gid
;
688 sb
->st_size
= fp
->f_di
.di_size
;
693 ufs_readdir(struct open_file
*f
, struct dirent
*d
)
695 struct file
*fp
= (struct file
*)f
->f_fsdata
;
702 * assume that a directory entry will not be split across blocks
705 if (fp
->f_seekp
>= fp
->f_di
.di_size
)
707 error
= buf_read_file(f
, &buf
, &buf_size
);
710 dp
= (struct direct
*)buf
;
711 fp
->f_seekp
+= dp
->d_reclen
;
712 if (dp
->d_ino
== (ino_t
)0)
714 d
->d_type
= dp
->d_type
;
715 strcpy(d
->d_name
, dp
->d_name
);
721 * Sanity checks for old file systems.
723 * XXX - goes away some day.
726 ffs_oldfscompat(struct fs
*fs
)
730 fs
->fs_npsect
= max(fs
->fs_npsect
, fs
->fs_nsect
); /* XXX */
731 fs
->fs_interleave
= max(fs
->fs_interleave
, 1); /* XXX */
732 if (fs
->fs_postblformat
== FS_42POSTBLFMT
) /* XXX */
733 fs
->fs_nrpos
= 8; /* XXX */
734 if (fs
->fs_inodefmt
< FS_44INODEFMT
) { /* XXX */
735 quad_t sizepb
= fs
->fs_bsize
; /* XXX */
737 fs
->fs_maxfilesize
= fs
->fs_bsize
* NDADDR
- 1; /* XXX */
738 for (i
= 0; i
< NIADDR
; i
++) { /* XXX */
739 sizepb
*= NINDIR(fs
); /* XXX */
740 fs
->fs_maxfilesize
+= sizepb
; /* XXX */
742 fs
->fs_qbmask
= ~fs
->fs_bmask
; /* XXX */
743 fs
->fs_qfmask
= ~fs
->fs_fmask
; /* XXX */