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. 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
41 * Copyright (c) 1990, 1991 Carnegie Mellon University
42 * All Rights Reserved.
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>
76 #include <vfs/ufs/dinode.h>
77 #include <vfs/ufs/dir.h>
78 #include <vfs/ufs/fs.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
= {
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
111 size_t f_blksize
[NIADDR
];
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
*);
124 static void ffs_oldfscompat(struct fs
*);
128 * Read a new inode into a file structure.
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
;
143 * Read inode and save it.
145 buf
= malloc(fs
->fs_bsize
);
147 rc
= (f
->f_dev
->dv_strategy
)(f
->f_devdata
, F_READ
,
148 fsbtodb(fs
, ino_to_fsba(fs
, inumber
)), fs
->fs_bsize
,
152 if (rsize
!= fs
->fs_bsize
) {
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
170 for (level
= 0; level
< NIADDR
; level
++)
171 fp
->f_blkno
[level
] = -1;
172 fp
->f_buf_blkno
= -1;
180 * Given an offset in a file, find the disk block number that
181 * contains that block.
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
;
193 daddr_t ind_block_num
;
198 * Index structure of an inode:
200 * di_db[0..NDADDR-1] hold block numbers for blocks
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
215 * NDADDR + NINDIR(fs) + NINDIR(fs)**2 ..
216 * NDADDR + NINDIR(fs) + NINDIR(fs)**2
217 * + NINDIR(fs)**3 - 1
220 if (file_block
< NDADDR
) {
222 *disk_block_p
= fp
->f_di
.di_db
[file_block
];
226 file_block
-= NDADDR
;
230 * nindir[1] = NINDIR**2
231 * nindir[2] = NINDIR**3
234 for (level
= 0; level
< NIADDR
; level
++) {
235 if (file_block
< fp
->f_nindir
[level
])
237 file_block
-= fp
->f_nindir
[level
];
239 if (level
== NIADDR
) {
240 /* Block number too high */
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 */
252 if (fp
->f_blkno
[level
] != ind_block_num
) {
253 if (fp
->f_blk
[level
] == NULL
)
255 malloc(fs
->fs_bsize
);
257 rc
= (f
->f_dev
->dv_strategy
)(f
->f_devdata
, F_READ
,
258 fsbtodb(fp
->f_fs
, ind_block_num
),
261 &fp
->f_blksize
[level
]);
264 if (fp
->f_blksize
[level
] != fs
->fs_bsize
)
266 fp
->f_blkno
[level
] = ind_block_num
;
269 ind_p
= (daddr_t
*)fp
->f_blk
[level
];
272 idx
= file_block
/ fp
->f_nindir
[level
- 1];
273 file_block
%= fp
->f_nindir
[level
- 1];
277 ind_block_num
= ind_p
[idx
];
280 *disk_block_p
= ind_block_num
;
286 * Read a portion of a file into an internal buffer. Return
287 * the location in the buffer and the amount in the buffer.
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
;
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
);
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
;
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
);
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
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
;
349 * Search a directory for a name and return its
356 search_directory(char *name
, struct open_file
*f
, ino_t
*inumber_p
)
358 struct file
*fp
= (struct file
*)f
->f_fsdata
;
366 length
= strlen(name
);
369 while (fp
->f_seekp
< fp
->f_di
.di_size
) {
370 rc
= buf_read_file(f
, &buf
, &buf_size
);
374 dp
= (struct direct
*)buf
;
375 edp
= (struct direct
*)(buf
+ buf_size
);
377 if (dp
->d_ino
== (ino_t
)0)
379 #if BYTE_ORDER == LITTLE_ENDIAN
380 if (fp
->f_fs
->fs_maxsymlinklen
<= 0)
384 namlen
= dp
->d_namlen
;
385 if (namlen
== length
&&
386 !strcmp(name
, dp
->d_name
)) {
388 *inumber_p
= dp
->d_ino
;
392 dp
= (struct direct
*)((char *)dp
+ dp
->d_reclen
);
394 fp
->f_seekp
+= buf_size
;
403 ufs_open(const char *upath
, struct open_file
*f
)
407 ino_t inumber
, parent_inumber
;
413 char namebuf
[MAXPATHLEN
+1];
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 */
426 rc
= (f
->f_dev
->dv_strategy
)(f
->f_devdata
, F_READ
,
427 SBOFF
/ DEV_BSIZE
, SBSIZE
, (char *)fs
, &buf_size
);
431 if (buf_size
!= SBSIZE
|| fs
->fs_magic
!= FS_MAGIC
||
432 fs
->fs_bsize
> MAXBSIZE
|| fs
->fs_bsize
< sizeof(struct fs
)) {
441 * Calculate indirect block levels.
448 for (level
= 0; level
< NIADDR
; level
++) {
450 fp
->f_nindir
[level
] = mult
;
455 if ((rc
= read_inode(inumber
, f
)) != 0)
458 cp
= path
= strdup(upath
);
466 * Remove extra separators
474 * Check that current node is a directory.
476 if ((fp
->f_di
.di_mode
& IFMT
) != IFDIR
) {
482 * Get next component of path name.
488 while ((c
= *cp
) != '\0' && c
!= '/') {
489 if (++len
> MAXNAMLEN
) {
499 * Look up component in current directory.
500 * Save directory inumber in case we find a
503 parent_inumber
= inumber
;
504 rc
= search_directory(ncp
, f
, &inumber
);
510 * Open next component.
512 if ((rc
= read_inode(inumber
, f
)) != 0)
516 * Check for symbolic link.
518 if ((fp
->f_di
.di_mode
& IFMT
) == IFLNK
) {
519 int link_len
= fp
->f_di
.di_size
;
524 if (link_len
+ len
> MAXPATHLEN
||
525 ++nlinks
> MAXSYMLINKS
) {
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
);
537 * Read file for symbolic link
541 struct fs
*fs
= fp
->f_fs
;
544 buf
= malloc(fs
->fs_bsize
);
545 rc
= block_map(f
, (daddr_t
)0, &disk_block
);
550 rc
= (f
->f_dev
->dv_strategy
)(f
->f_devdata
,
551 F_READ
, fsbtodb(fs
, disk_block
),
552 fs
->fs_bsize
, buf
, &buf_size
);
556 bcopy((char *)buf
, namebuf
, (unsigned)link_len
);
560 * If relative pathname, restart at parent directory.
561 * If absolute pathname, restart at root.
565 inumber
= parent_inumber
;
567 inumber
= (ino_t
)ROOTINO
;
569 if ((rc
= read_inode(inumber
, f
)) != 0)
575 * Found terminal component.
594 ufs_close(struct open_file
*f
)
596 struct file
*fp
= (struct file
*)f
->f_fsdata
;
603 for (level
= 0; level
< NIADDR
; level
++) {
604 if (fp
->f_blk
[level
])
605 free(fp
->f_blk
[level
]);
615 * Copy a portion of a file into kernel memory.
616 * Cross block boundaries when necessary.
622 ufs_read(struct open_file
*f
, void *start
, size_t size
, size_t *resid
)
624 struct file
*fp
= (struct file
*)f
->f_fsdata
;
632 if (fp
->f_seekp
>= fp
->f_di
.di_size
)
635 rc
= buf_read_file(f
, &buf
, &buf_size
);
640 if (csize
> buf_size
)
643 bcopy(buf
, addr
, csize
);
645 fp
->f_seekp
+= csize
;
655 ufs_seek(struct open_file
*f
, off_t offset
, int where
)
657 struct file
*fp
= (struct file
*)f
->f_fsdata
;
661 fp
->f_seekp
= offset
;
664 fp
->f_seekp
+= offset
;
667 fp
->f_seekp
= fp
->f_di
.di_size
- offset
;
672 return (fp
->f_seekp
);
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
;
689 ufs_readdir(struct open_file
*f
, struct dirent
*d
)
691 struct file
*fp
= (struct file
*)f
->f_fsdata
;
698 * assume that a directory entry will not be split across blocks
701 if (fp
->f_seekp
>= fp
->f_di
.di_size
)
703 error
= buf_read_file(f
, &buf
, &buf_size
);
706 dp
= (struct direct
*)buf
;
707 fp
->f_seekp
+= dp
->d_reclen
;
708 if (dp
->d_ino
== (ino_t
)0)
710 d
->d_type
= dp
->d_type
;
711 strcpy(d
->d_name
, dp
->d_name
);
717 * Sanity checks for old file systems.
719 * XXX - goes away some day.
722 ffs_oldfscompat(struct fs
*fs
)
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 */
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 */
738 fs
->fs_qbmask
= ~fs
->fs_bmask
; /* XXX */
739 fs
->fs_qfmask
= ~fs
->fs_fmask
; /* XXX */