xref: /freebsd/lib/libufs/inode.c (revision b00ab754)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2002 Juli Mallett.  All rights reserved.
5  *
6  * This software was written by Juli Mallett <jmallett@FreeBSD.org> for the
7  * FreeBSD project.  Redistribution and use in source and binary forms, with
8  * or without modification, are permitted provided that the following
9  * conditions are met:
10  *
11  * 1. Redistribution of source code must retain the above copyright notice,
12  *    this list of conditions and the following disclaimer.
13  * 2. Redistribution in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
21  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
26  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
33 #include <sys/param.h>
34 #include <sys/mount.h>
35 #include <sys/disklabel.h>
36 #include <sys/stat.h>
37 
38 #include <ufs/ufs/ufsmount.h>
39 #include <ufs/ufs/dinode.h>
40 #include <ufs/ffs/fs.h>
41 
42 #include <errno.h>
43 #include <fcntl.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <unistd.h>
48 
49 #include <libufs.h>
50 
51 int
52 getino(struct uufsd *disk, void **dino, ino_t inode, int *mode)
53 {
54 	ino_t min, max;
55 	caddr_t inoblock;
56 	struct ufs1_dinode *dp1;
57 	struct ufs2_dinode *dp2;
58 	struct fs *fs;
59 
60 	ERROR(disk, NULL);
61 
62 	fs = &disk->d_fs;
63 	if (inode >= (ino_t)fs->fs_ipg * fs->fs_ncg) {
64 		ERROR(disk, "inode number out of range");
65 		return (-1);
66 	}
67 	inoblock = disk->d_inoblock;
68 	min = disk->d_inomin;
69 	max = disk->d_inomax;
70 
71 	if (inoblock == NULL) {
72 		inoblock = malloc(fs->fs_bsize);
73 		if (inoblock == NULL) {
74 			ERROR(disk, "unable to allocate inode block");
75 			return (-1);
76 		}
77 		disk->d_inoblock = inoblock;
78 	}
79 	if (inode >= min && inode < max)
80 		goto gotit;
81 	bread(disk, fsbtodb(fs, ino_to_fsba(fs, inode)), inoblock,
82 	    fs->fs_bsize);
83 	disk->d_inomin = min = inode - (inode % INOPB(fs));
84 	disk->d_inomax = max = min + INOPB(fs);
85 gotit:	switch (disk->d_ufs) {
86 	case 1:
87 		dp1 = &((struct ufs1_dinode *)inoblock)[inode - min];
88 		if (mode != NULL)
89 			*mode = dp1->di_mode & IFMT;
90 		if (dino != NULL)
91 			*dino = dp1;
92 		return (0);
93 	case 2:
94 		dp2 = &((struct ufs2_dinode *)inoblock)[inode - min];
95 		if (mode != NULL)
96 			*mode = dp2->di_mode & IFMT;
97 		if (dino != NULL)
98 			*dino = dp2;
99 		return (0);
100 	default:
101 		break;
102 	}
103 	ERROR(disk, "unknown UFS filesystem type");
104 	return (-1);
105 }
106 
107 int
108 putino(struct uufsd *disk)
109 {
110 	struct fs *fs;
111 
112 	fs = &disk->d_fs;
113 	if (disk->d_inoblock == NULL) {
114 		ERROR(disk, "No inode block allocated");
115 		return (-1);
116 	}
117 	if (bwrite(disk, fsbtodb(fs, ino_to_fsba(&disk->d_fs, disk->d_inomin)),
118 	    disk->d_inoblock, disk->d_fs.fs_bsize) <= 0)
119 		return (-1);
120 	return (0);
121 }
122